<?php
	error_reporting(0);
 	include "../pagination.php";   
	include "../functions/dblink.php";

	$find = "ทั้งหมด";
	$condition = 1;

	$prod=$_POST['prodsearch'];
	$cost1=$_POST['cost1'];
	$cost2=$_POST['cost2'];
	$date1=$_POST['date1'];
	$date2=$_POST['date2'];
	
	if($prod==''){
		if($cost1==''){
			if($cost2==''){
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 WHERE $date1>=PDDATE";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 WHERE $date2<=PDDATE";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 WHERE $date1>=PDDATE AND $date2<=PDDATE";
				}	
			}
			}else{
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 AND PDSLEPRC<=$cost2";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 WHERE $date1>=PDDATE AND PDSLEPRC<=$cost2";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 WHERE $date2<=PDDATE AND PDSLEPRC<=$cost2";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 WHERE $date1>=PDDATE AND $date2<=PDDATE AND PDSLEPRC<=$cost2";
				}
			}
					
		}else{
			if($cost2==''){
				$sql="SELECT * FROM prod001 WHERE PDSLEPRC>=$cost1";
			}else{
				$sql="SELECT * FROM prod001 WHERE PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2";
			}
		}
		
	}	
	else if($prod!=''){
		if($cost1==''){
			if($cost2==''){
				$sql="SELECT * FROM prod001 
		      		  WHERE PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%'";
			}else{
				$sql="SELECT * FROM prod001 
		      		  WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND
						     PDSLEPRC<=$cost2";
			}			
		}else{
			if($cost2==''){
				$sql="SELECT * FROM prod001 
		      		  WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND
						     PDSLEPRC>=$cost1";
			}else{
				$sql="SELECT * FROM prod001 
		      		  WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND
					        (PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2)";
			}
		}	
	}
	//$sql="SELECT * FROM prod001";
	$result=mysqli_query($con,$sql);
	
	$rs = page_query($con, $sql,1000);	
	
	$first = page_start_row();
	$last = page_stop_row();
	$total = page_total_rows();
	if($total == 0) {$first = 0;}

	define('FPDF_FONTPATH','fpdf/font/');
	require("fpdf/fpdf.php");

	class PDF extends FPDF
	{
		function Footer()	{
		//นับจากขอบกระดาษด้านล่างขึ้นมา 10 มม.
		$this->SetY( -10 );
		//กำหนดใช้ตัวอักษร Arial ตัวเอียง ขนาด 5
		$this->SetFont('THSarabunNew_bi','I',5);//พิมพ์ หมายเลขหน้า ตรงมุมขวาล่าง
		$this->Cell(0,10, 'page '.$this->PageNo().' of  tp' ,0,0,'R');
		}
	}
    
	$pdf=new PDF('P','mm','A4');
	///var_dump(get_class_methods($pdf));
	$pdf->AliasNbPages( 'tp' );
	
	$pdf->AddPage();
	//$pdf->Image('alen.jpg')
	$pdf->AddFont('THSarabunNew_b','B','THSarabunNew_b.php');
	$pdf->AddFont('THSarabunNew_bi','I','THSarabunNew_bi.php');
	$pdf->AddFont('THSarabunNew','','THSarabunNew.php');

	$pdf->SetFont('THSarabunNew_b','B','20');
	//$pdf->Cell(0,10,"My PDF Page",1,1,"C");
    $pdf->Cell(0,2,iconv('UTF-8','cp874','บริษัท พราวเอเซีย จำกัด'),0,1,'C');
	$pdf->SetFont('THSarabunNew','','15');
	$pdf->Cell(0,10,iconv('UTF-8','cp874',
	      'ข้อมูลสินค้า'.' รหัส/ชื่อ : '.$prod.' ช่วงราคา : '.
		  number_format($cost1).' - '.number_format($cost2).' บาท'),0,1,'C');

	$pdf->SetFont('THSarabunNew_b','B','11');
	$pdf->Cell(0,5,iconv('UTF-8','cp874',date("d-m-Y")),0,0,'L');
    $pdf->Cell(0,5,iconv('UTF-8','cp874','ทั้งหมด '.$total.' Records'),0,1,'R');
	
	$pdf->Cell(0,1,iconv('UTF-8','cp874','-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'),0,1,'L');
	$pdf->Cell(6,5,iconv('UTF-8','cp874','No.'),0,0,'L');
	$pdf->Cell(18,5,iconv('UTF-8','cp874','Code'),0,0,'L');
	$pdf->Cell(53,5,iconv('UTF-8','cp874','Name'),0,0,'L');
	$pdf->Cell(32,5,iconv('UTF-8','cp874','ประเภท'),0,0,'L');	
	$pdf->Cell(13,5,iconv('UTF-8','cp874','ขาย'),0,0,'C');
    $pdf->Cell(30,5,iconv('UTF-8','cp874','จาก'),0,0,'L');
	$pdf->Cell(35,5,iconv('UTF-8','cp874','  หมายเหตุ'),0,1,'L');

	$pdf->Cell(109,5,iconv('UTF-8','cp874',''),0,0,'L');
	$pdf->Cell(13,5,iconv('UTF-8','cp874','ทุน'),0,0,'C');
	$pdf->Cell(15,5,iconv('UTF-8','cp874','ส่วนลด'),0,0,'L');
	$pdf->Cell(15,5,iconv('UTF-8','cp874','ราคาวันที่'),0,1,'L');
	$pdf->Cell(0,1,iconv('UTF-8','cp874','-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'),0,1,'L');

	$pdf->SetFont('THSarabunNew','','11');
	$count=1;
	while($data=mysqli_fetch_array($result))
		{	
			$pdf->Cell(6,5,iconv('UTF-8','cp874',$count.'.'),0,0,'L');
			$pdf->Cell(18,5,iconv('UTF-8','cp874',$data[PDNO]),0,0,'L');
			$pdf->Cell(53,5,iconv('UTF-8','cp874',$data[PDNAME]),0,0,'L');
			$pdf->Cell(30,5,iconv('UTF-8','cp874',$data[PDKIND]),0,0,'L');	
			$pdf->Cell(15,5,iconv('UTF-8','cp874',number_format($data[PDSLEPRC])),0,0,'R');
			$pdf->Cell(30,5,iconv('UTF-8','cp874',$data[PDFROM]),0,0,'L');
			$pdf->Cell(35,5,iconv('UTF-8','cp874','  '.$data[PDNOTE]),0,1,'L');

			$pdf->Cell(107,5,iconv('UTF-8','cp874',''),0,0,'L');
			$pdf->Cell(15,5,iconv('UTF-8','cp874',number_format($data[PDCSTPRC])),0,0,'R');
			$pdf->Cell(3,5,iconv('UTF-8','cp874',''),0,0,'L');	
			$pdf->Cell(6,5,iconv('UTF-8','cp874',number_format($data[PDDISC])),0,0,'C');
			$pdf->Cell(6,5,iconv('UTF-8','cp874',''),0,0,'L');
			$time=strtotime($data['PDDATE']);
			$pdf->Cell(15,5,iconv('UTF-8','cp874',DATE('d-m-Y',$time)),0,1,'L');
			$count=$count+1;
		}	
	$pdf->Output('PA'.date('dmy').'.pdf' , 'D' );	
?>