<?php
	error_reporting(0);
 	include "../pagination.php";   
	include "../functions/dblink.php";	   
	$find = "ทั้งหมด";
	$condition = 1;
	$seller=$_POST[seller];
	$datein1=$_POST[datein1];
	$datein2=$_POST[datein2];
	// if($seller==''){
	// 	$sql="SELECT * FROM quot002 
	//          WHERE QLOCK=0 AND QDATE>='$datein1' AND QDATE<='$datein2'
	// 		 ORDER BY QSMFNAME ASC";
	// }else{
	// 	$sql="SELECT * FROM quot002 
	//          WHERE QLOCK=0 AND QDATE>='$datein1' AND QDATE<='$datein2' AND QSMFNAME LIKE '%$seller%'
	// 		 ORDER BY QSMFNAME ASC";
	// }
	if($seller==''){
		$sql="SELECT * FROM quot002 
	         WHERE QDATE>='$datein1' AND QDATE<='$datein2'
			 ORDER BY QSMFNAME ASC";
	}else{
		$sql="SELECT * FROM quot002 
	         WHERE QDATE>='$datein1' AND QDATE<='$datein2' AND QSMFNAME LIKE '%$seller%'
			 ORDER BY QSMFNAME ASC";
	}
	
	$result=mysqli_query($con,$sql);
	
	$dt=date('Y-m-d');
	$date=substr($dt,8,2).'-'.substr($dt,5,2).'-'.strval((int)(substr($dt,0,4))+543);
	$d1=substr($datein1,8,2);
	$m1=substr($datein1,5,2);
	$date1=substr($datein1,8,2).'-'.substr($datein1,5,2).'-'.strval((int)(substr($datein1,0,4))+543);
	$d2=substr($datein2,8,2);
	$m2=substr($datein2,5,2);
	$date2=substr($datein2,8,2).'-'.substr($datein2,5,2).'-'.strval((int)(substr($datein2,0,4))+543);

	$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('L','mm','A3');
	///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','ใบเสนอราคาทั้งหมด ตามพนักงานขาย'),0,1,'C');
	$pdf->Cell(0,2,iconv( 'UTF-8','cp874','ช่วงวันที่ '.$date1.' - '.$date2),0,1,'C');

	$lines='----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------';
	$pdf->SetFont('THSarabunNew_b','B','11');
	$pdf->Cell(0,5,iconv( 'UTF-8','cp874',$date),0,0,'L');	
    $pdf->Cell(0,5,iconv( 'UTF-8','cp874','ทั้งหมด '.$total.' Records'),0,1,'R');
	$pdf->Cell(0,1,iconv( 'UTF-8','cp874',$lines),0,1,'L');
	$pdf->Cell(5,5,iconv( 'UTF-8','cp874','No.'),0,0,'C');
	$pdf->Cell(16,5,iconv( 'UTF-8','cp874','Quot. No.'),0,0,'L');
	$pdf->Cell(14,5,iconv( 'UTF-8','cp874','วันที่'),0,0,'L');
	$pdf->Cell(95,5,iconv( 'UTF-8','cp874','ชื่อ'),0,0,'L');
	$pdf->Cell(75,5,iconv( 'UTF-8','cp874','โครงการ'),0,0,'L');
	$pdf->Cell(50,5,iconv( 'UTF-8','cp874','ผู้ขาย'),0,0,'L');
	$pdf->Cell(15,5,iconv( 'UTF-8','cp874','ส่วนลด'),0,0,'R');
	$pdf->Cell(18,5,iconv( 'UTF-8','cp874','มูลค่างาน'),0,0,'C');
	$pdf->Cell(18,5,iconv( 'UTF-8','cp874','ทั้งสิ้น'),0,0,'C');
	$pdf->Cell(80,5,iconv( 'UTF-8','cp874','หมายเหตุ'),0,1,'L');
	$pdf->Cell(0,1,iconv( 'UTF-8','cp874',$lines),0,1,'L');

	$pdf->SetFont('THSarabunNew','','11');
	$gtot=0;
	$count=1;

	require_once "../functions/stringLenTh.php";
	while($data=mysqli_fetch_array($rs))
		{	
			$pdf->Cell(5,5,iconv( 'UTF-8','cp874',$count.'.'),0,0,'R');
			$pdf->Cell(16,5,iconv( 'UTF-8','cp874',$data[QNO]),0,0,'L');
			$qdate=$data[QDATE];
			$date=substr($qdate,8,2).'-'.substr($qdate,5,2).'-'.strval((int)(substr($qdate,0,4))+543);
			$pdf->Cell(95,5,iconv( 'UTF-8','cp874',mb_substr($data[QC_SUR].' '.$data[QC_NAME],0,95)),0,0,'L');
			$pdf->Cell(75,5,iconv( 'UTF-8','cp874',mb_substr($data[QPJNAME],0,75)),0,0,'L');
			$pdf->Cell(50,5,iconv( 'UTF-8','cp874',mb_substr($data[QSMFNAME].' '.$data[QSMLNAME],0,50)),0,0,'L');
			$pdf->Cell(15,5,iconv( 'UTF-8','cp874',number_format($data[QSUMDISC])),0,0,'R');
			$pdf->Cell(18,5,iconv( 'UTF-8','cp874',number_format($data[QSUM])),0,0,'R');
			$total=$data[QSUM]-$data[QSUMDISC];
			$pdf->Cell(18,5,iconv( 'UTF-8','cp874',number_format($total)),0,0,'R');
			$pdf->Cell(80,5,iconv( 'UTF-8','cp874',mb_substr($data[QNOTE],0,80)),0,1,'L');

			$gtot=$gtot+$total;
			$count=$count+1;
		}
		$pdf->SetFont('THSarabunNew_b','B','11');
		$pdf->Cell(0,5,iconv( 'UTF-8','cp874',$lines),0,1,'L');
		$pdf->SetFont('THSarabunNew_b','B','16');
		$pdf->Cell(306,5,iconv( 'UTF-8','cp874','รวมทั้งสิ้น '.'                              '.number_format($gtot)),0,1,'R');
		$pdf->SetFont('THSarabunNew_b','B','11');
		$pdf->Cell(0,5,iconv( 'UTF-8','cp874',$lines),0,1,'L');
   $date1=date_create("$datein1");
   $date2=date_create("$datein2");       
   $pdf->Output('QBNm'.date_format($date1,'dmy').date_format($date2,'dmy').'.pdf' , 'D' );
?>