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

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

	$prod=$_POST['prodsearch'];
	$cost1=$_POST['cost1'];
	$cost2=$_POST['cost2'];

	if($_POST['date1']==''){
		$strDate1="01-01-1980";
	}else{
		$strDate1=$_POST['date1'];
	}

	if($_POST['date2']==''){
		$strDate2="01-01-2100";
	}else{
		$strDate2=$_POST['date2'];
	}
	
	$date1=date_format($_POST['date1'],'Y-m-d');
	$date2=date_format($_POST['date2'],'Y-m-d');
	
	if($prod==''){
		if($cost1==''){
			if($cost2==''){
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 
						WHERE $date1>=PDDATE 
						ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE $date1>=PDDATE AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
			}else{
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC<=$cost2 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 
						WHERE PDSLEPRC<=$cost2 AND $date1>=PDDATE 
						ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC<=$cost2 AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC<=$cost2 AND $date1>=PDDATE AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
			}			
		}else{
			if($cost2==''){
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC>=$cost1 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 
						WHERE PDSLEPRC>=$cost1 AND $date1>=PDDATE 
						ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC>=$cost1 AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC>=$cost1 AND $date1>=PDDATE AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
			}else{
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 
						WHERE PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2 AND $date1>=PDDATE 
						ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2 AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2 AND $date1>=PDDATE AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
			}
		}
		
	}	
	else if($prod!=''){
		if($cost1==''){
			if($cost2==''){
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 
					WHERE PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%' 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 
						WHERE PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%' AND $date1>=PDDATE 
						ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%' AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%' AND $date1>=PDDATE AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
			}else{
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND PDSLEPRC<=$cost2 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 
						WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND PDSLEPRC<=$cost2 AND $date1>=PDDATE 
						ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND PDSLEPRC<=$cost2 AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND PDSLEPRC<=$cost2 AND $date1>=PDDATE AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
			}			
		}else{
			if($cost2==''){
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND PDSLEPRC>=$cost1 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 
						WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND PDSLEPRC>=$cost1 AND $date1>=PDDATE 
						ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND PDSLEPRC>=$cost1 AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND PDSLEPRC>=$cost1 AND $date1>=PDDATE AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
			}else{
				if($date1=='' AND $date2==''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND (PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2) 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2==''){
						$sql="SELECT * FROM prod001 
						WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND (PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2) AND $date1>=PDDATE 
						ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1=='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND (PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2) AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
				if($date1!='' AND $date2!=''){
					$sql="SELECT * FROM prod001 
					WHERE (PDNO LIKE '%$prod%' OR PDNAME LIKE '%$prod%') AND (PDSLEPRC>=$cost1 AND PDSLEPRC<=$cost2) AND $date1>=PDDATE AND $date2<=PDDATE 
					ORDER BY PDDATE DESC,PDNO ASC";
				}
			}
		}	
	}
	//$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 Header(){
			//$this->Image('alen.jpg')
			$this->AddFont('THSarabunNew_b','B','THSarabunNew_b.php');
			$this->AddFont('THSarabunNew_bi','I','THSarabunNew_bi.php');
			$this->AddFont('THSarabunNew','','THSarabunNew.php');

			$this->SetFont('THSarabunNew_b','B','20');
			//$this->Cell(0,10,"My PDF Page",1,1,"C");
			$this->Cell(0,10,iconv('UTF-8','cp874','บริษัท พราวเอเซีย จำกัด'),0,1,'C');
			$this->SetFont('THSarabunNew','','15');

			$total = page_total_rows();
			$prod=$_POST['prodsearch'];
			$cost1=$_POST['cost1'];
			$cost2=$_POST['cost2'];

			$this->Cell(0,2,iconv('UTF-8','cp874',
				'ข้อมูลสินค้า'.' รหัส/ชื่อ : '.$prod.' ช่วงราคา : '.
				number_format($cost1).' - '.number_format($cost2).' บาท'),0,1,'C');
			$this->Cell(0,10,iconv('UTF-8','cp874',
				'วัน Update รหัสสินค้า : '.
				date('d-m-Y',strtotime('$strDate1')).' ถึง '.
				date('d-m-Y',strtotime('$strDate2'))),0,1,'C');
			
			$this->SetFont('THSarabunNew_b','B','11');
			$this->Cell(20,5,iconv( 'UTF-8','cp874',date("d-m-Y")),0,0,'L');
			$this->Cell(30,5,iconv( 'UTF-8','cp874','* 1st index, ** 2nd index'),0,0,'L');
			$this->Cell(146,5,iconv( 'UTF-8','cp874','ทั้งหมด '.$total.' Records'),0,1,'R');
			
			$this->Cell(0,1,iconv('UTF-8','cp874','-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'),0,1,'L');
			$this->Cell(6,5,iconv('UTF-8','cp874','No.'),0,0,'L');
			$this->Cell(18,5,iconv('UTF-8','cp874','Code **'),0,0,'L');
			$this->Cell(65,5,iconv('UTF-8','cp874','Name'),0,0,'L');
			$this->Cell(30,5,iconv('UTF-8','cp874','ประเภท'),0,0,'L');		
			$this->Cell(22,5,iconv('UTF-8','cp874','             ขาย'),0,0,'L');
			$this->Cell(22,5,iconv('UTF-8','cp874','             ทุน'),0,0,'L');
			$this->Cell(10,5,iconv('UTF-8','cp874','ส่วนลด'),0,0,'C');
			$this->Cell(15,5,iconv('UTF-8','cp874','UpDte *'),0,1,'L');
			
			$this->Cell(24,5,iconv('UTF-8','cp874',''),0,0,'L');
			$this->Cell(65,5,iconv('UTF-8','cp874','จาก'),0,0,'L');
			$this->Cell(100,5,iconv('UTF-8','cp874','หมายเหตุ'),0,1,'L');
			$this->Cell(0,1,iconv('UTF-8','cp874','-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'),0,1,'L');
		}
		
		function Footer()	{
		//นับจากขอบกระดาษด้านล่างขึ้นมา 10 มม.
		$this->SetY( -10 );
		//กำหนดใช้ตัวอักษร Arial ตัวเอียง ขนาด 5
		$this->SetFont('THSarabunNew_bi','I',10);//พิมพ์ หมายเลขหน้า ตรงมุมขวาล่าง
		$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->SetFont('THSarabunNew','','11');
	$count=1;
	require_once "../functions/stringLenTh.php";
	while($data=mysqli_fetch_array($result))
		{	
			$line=substr($data['PDNAME'],0,44);	
			$pdf->Cell(6,5,iconv('UTF-8','cp874',$count.'.'),0,0);
			$pdf->Cell(18,5,iconv('UTF-8','cp874',$data['PDNO']),0,0);
			$pdf->Cell(65,5,iconv('UTF-8','cp874',$line),0,0);
			$pdf->Cell(30,5,iconv('UTF-8','cp874',mb_substr($data['PDKIND'],0,30)),0,0);	
			$pdf->Cell(22,5,iconv('UTF-8','cp874',number_format($data['PDSLEPRC'],2)),0,0,'R');	
			$pdf->Cell(22,5,iconv('UTF-8','cp874',number_format($data['PDCSTPRC'],2)),0,0,'R');	
			$pdf->Cell(10,5,iconv('UTF-8','cp874',number_format($data['PDDISC'],2)),0,0,'R');
			//$time=strtotime($data[PDDATE]);	
			$pdf->Cell(15,5,iconv('UTF-8','cp874',DATE('d-m-Y',strtotime($data['PDDATE']))),0,1,'L');

			$pdf->Cell(24,5,iconv('UTF-8','cp874',''),0,0,'L');
			$pdf->Cell(65,5,iconv('UTF-8','cp874',mb_substr($data['PDFROM'],0,65)),0,0,'L');
			$pdf->Cell(100,5,iconv('UTF-8','cp874',mb_substr($data['PDNOTE'],0,100)),0,1,'L');
			$count=$count+1;
		}	
	$pdf->Output('PA'.date('dmy').'.pdf' , 'D' );	
?>