<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require('fpdf.php');

$mrnumber=$_REQUEST['mrnumber'];
$dates=$_REQUEST['dates'];

$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$pdf = new FPDF();

$sqlst="select sum(pcs_qty*pcs_price) from market_returns where  mr_numbers='".$mrnumber."' and dates='".$dates."'";
$aqry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($aqry);

if($rst[0]>0){
$pdf->AddPage('P');
$pdf->SetDisplayMode('real','default');

$pdf->SetFont('Arial','BU',11);

$tmp="MARKET RETURN STATEMENT	";
$pdf->Cell(180,7,$tmp,0,1,'C');

$sqlst="select mr_numbers,dates, agent_names,territory_names,areas from market_returns where mr_numbers='".$mrnumber."' and dates='".$dates."'";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

$pdf->SetFont('Arial','B',9);
$nm=ucwords($rst[2]);
$tmp="\nNo: ".$rst[0]."	\n".$nm."	\n".$rst[3];
if($rst[4]!=""){
$tmp=$tmp."-".$rst[4];}
$tmp=$tmp."\nDate: ".$dates."\n";

$pdf->Write(4,$tmp);

$pdf->Ln(2);
$pdf->SetFont('Arial','B',9);
$tmp="ITEM	";
$pdf->Cell(62,5,$tmp,0,0);
$tmp="REASON	";
$pdf->Cell(25,5,$tmp,0,0,'L');
$tmp="QTY	";
$pdf->Cell(12,5,$tmp,0,0,'C');
$tmp="PRICE	";
$pdf->Cell(18,5,$tmp,0,0,'C');
$tmp="GROSS	";
$pdf->Cell(18,5,$tmp,0,0,'C');
$tmp="DISC	";
$pdf->Cell(12,5,$tmp,0,0,'C');
$tmp="NET	";
$pdf->Cell(18,5,$tmp,0,0,'C');
$tmp="ACTION TAKEN	";
$pdf->Cell(20,5,$tmp,0,1,'C');
	

$sqlst="select item_ranges from market_returns where  mr_numbers='".$mrnumber."' and dates='".$dates."' group by item_ranges";
$aqry=mysqli_query($conn,$sqlst);

$grandgross=0;
$grandnet=0;


while ($arst=mysqli_fetch_row($aqry)){
$pdf->SetFont('Arial','U',9);
$tmp=$arst[0]." Items	";
$pdf->Cell(100,5,$tmp,0,1,'L');


$sqlst="select item_ranges,mr_types from market_returns where mr_numbers='".$mrnumber."' and dates='".$dates."'  and item_ranges='".$arst[0]."' group by item_ranges,mr_types";
$qry=mysqli_query($conn,$sqlst);

$itemsgtotals=0;
$itemsntotals=0;

$pdf->SetFont('Arial','',9);
while($rst=mysqli_fetch_row($qry)){

$sqlst="select item_names,mr_types,pcs_qty,pcs_price,(pcs_price*pcs_qty),discount_rates,actions from market_returns where mr_numbers='".$mrnumber."' and dates='".$dates."' and item_ranges='".$rst[0]."' and mr_types='".$rst[1]."' order by item_names,mr_types";
$sqry=mysqli_query($conn,$sqlst);
$gtotals=0;
$ntotals=0;
$discounts;

while($srst=mysqli_fetch_row($sqry)){
$gtotals=$gtotals+$srst[4];
$ntotals=$ntotals+$srst[4]*(1-($srst[5]/100));

$ritems="";
if(strlen($srst[0])>33){
$ritems=substr($srst[0],0,33);
$ritems=$ritems."..";}
else{
$ritems=$srst[0];}

$pdf->SetFont('Arial','',8);
$pdf->Cell(62,5,$ritems,0,0,'L');
$pdf->SetFont('Arial','',9);
$pdf->Cell(25,5,$rst[1],0,0,'L');
$pdf->Cell(12,5,number_format($srst[2],0,'.',',')."	",0,0,'R');
$pdf->Cell(18,5,number_format($srst[3],2,'.',',')."	",0,0,'R');
$pdf->Cell(20,5,number_format($srst[4],2,'.',',')."	",0,0,'R');
$pdf->Cell(12,5,number_format($srst[5],1,'.',',')." %	",0,0,'R');
$pdf->Cell(18,5,number_format($srst[4]*(1-($srst[5]/100)),2,'.',',')."	",0,0,'R');
$pdf->Cell(20,5,$srst[6]."	",0,1,'L');
}

//$tmp="Sub total	";
//$pdf->Cell(117,5,$tmp,0,0,'R');
//$pdf->Cell(20,5,number_format($gtotals,2,'.',',')."	",0,0,'R');
//$tmp="	";
//$pdf->Cell(12,5,$tmp,0,0,'R');
//$pdf->Cell(18,5,number_format($ntotals,2,'.',',')."	",0,1,'R');
//$pdf->Write(5,"\n");

$itemsgtotals=$itemsgtotals+$gtotals;
$itemsntotals=$itemsntotals+$ntotals;

}

//$pdf->SetFont('Arial','B',9);
//$tmp="Total	";
//$pdf->Cell(117,5,$tmp,0,0,'R');
//$pdf->Cell(20,5,number_format($itemsgtotals,2,'.',',')."	",0,0,'R');
//$tmp="	";
//$pdf->Cell(12,5,$tmp,0,0,'R');
//$pdf->Cell(18,5,number_format($itemsntotals,2,'.',',')."	",0,1,'R');
//$pdf->SetFont('Arial','',9);

$grandgross=$grandgross+$itemsgtotals;
$grandnet=$grandnet+$itemsntotals;
}

//Line spaces removed since need to set print margings

$pdf->Ln(6);

$pdf->SetFont('Arial','B',9);
$tmp="Total	";
$pdf->Cell(117,5,$tmp,0,0,'R');
$pdf->Cell(20,5,number_format($grandgross,2,'.',',')."	",0,0,'R');
$tmp="	";
$pdf->Cell(11,5,$tmp,0,0,'R');
$pdf->Cell(20,5,number_format($grandnet,2,'.',',')."	",0,1,'R');

$pdf->SetFont('Arial','',9);

//line by line spaces need to impement the printing purposes

$pdf->Ln(12);
$tmp="-----------------------------------	";
$pdf->Cell(50,5,$tmp,0,0,'R');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'R');
$tmp="----------------------------	";
$pdf->Cell(50,5,$tmp,0,0,'R');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'R');
$tmp="----------------------------	";
$pdf->Cell(50,5,$tmp,0,1,'R');
$tmp="Prepared - Dhanenthran	";
$pdf->Cell(50,5,$tmp,0,0,'R');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'R');
$tmp="Checked - Dilrukshi	";
$pdf->Cell(50,5,$tmp,0,0,'R');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'R');
$tmp="Checked - QC	";
$pdf->Cell(50,5,$tmp,0,1,'R');

$pdf->Ln(12);

//Line by line spaces need to implement for the printing purposes

//$pdf->Ln(13);
$tmp="----------------------------	";
$pdf->Cell(50,5,$tmp,0,0,'R');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'R');
$tmp="----------------------------	";
$pdf->Cell(50,5,$tmp,0,0,'R');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'R');
$tmp="----------------------------	";
$pdf->Cell(50,5,$tmp,0,1,'R');
$tmp="Checked - Prabodani	";
$pdf->Cell(50,5,$tmp,0,0,'R');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'R');
$tmp="Accountant - Nuwan	";
$pdf->Cell(50,5,$tmp,0,0,'R');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'R');
$tmp="JT Managing Director	";
$pdf->Cell(50,5,$tmp,0,1,'R');
//$pdf->Ln(13);

//$pdf->Ln(8);
//$pdf->SetFont('Arial','B',9);
//$pdf->Cell(50,5,"**Marketing comments:-",0,0,'R');

///Line by Line space need to implement for the printing purposes

}

$pdf->Ln(2);


$pdf->output();

