<?php
require('fpdf.php');
$mrnumber=$_REQUEST['agents'];
$dates=$_REQUEST['terrys'];

$starts=$_REQUEST['starts'];
$ends=$_REQUEST['ends'];


$conn=mysqli_connect("localhost","primarysales","S5L7lF01EQZvQrGK","primarysales_dianaold");
$pdf = new FPDF('L','mm',Letter);
$sqlst="select sum(box_qty*box_price) from rc_orders where agent_names='$mrnumber' and territory_names='$dates' and dates between '$starts' and '$ends' ";
$aqry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($aqry);
$pdf->setTopMargin(18);
if($rst[0]>0){
$pdf->AddPage('P');
$pdf->SetDisplayMode('real','default');
$pdf->SetFont('Arial','BU',12);
$tmp="ORDER APPROVAL STATEMENT -- DISTRIBUTOR	";
$pdf->Cell(180,7,$tmp,0,1,'C');

$sqlst="select regions,chanell, agent_names,territory_names,zones,executive_names,naration from rc_orders where agent_names='$mrnumber' and territory_names='$dates' and dates between '$starts' and '$ends' and agent_types='DIRECT' and rep_names='' ";
$qry=mysqli_query($conn,$sqlst);
$rst=mysqli_fetch_row($qry);

$pdf->SetFont('Arial','B',11);
$nm=ucwords($rst[2]);
$nmm=ucwords($rst[5]);
$tmp="\nChannel: ".$rst[1]." \n".$nm." \nASM/ASE:".$nmm."	\n".$rst[3];
if($rst[4]!=""){
$tmp=$tmp."-".$rst[0];}
$tmp=$tmp."\nDate: ".$starts."\n";

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

$pdf->Ln(2);
$pdf->SetFont('Arial','B',10);
$tmp="RANGE	";
$pdf->Cell(30,7,$tmp,1,0);
$tmp="GROSS-ORDER	";
$pdf->Cell(30,7,$tmp,1,0,'L');
$tmp="DISCOUNT	";
$pdf->Cell(25,7,$tmp,1,0,'C');
$tmp="%	";
$pdf->Cell(10,7,$tmp,1,0,'C');
$tmp="NET-ORDER	";
$pdf->Cell(30,7,$tmp,1,0,'C');
$tmp="CHQ-REALIZ	";
$pdf->Cell(28,7,$tmp,1,0,'C');
$tmp="BANK-GURUNTE	";
$pdf->Cell(32,7,$tmp,1,1,'C');


$sqlst="select item_ranges,sum(box_price*box_qty),sum(box_qty*box_price*discount_rates),discount_rates from rc_orders where agent_names='$mrnumber' and territory_names='$dates' and dates between '$starts' and '$ends' and agent_types='DIRECT' and rep_names='' group by item_ranges order by item_ranges ";
$sqry=mysqli_query($conn,$sqlst);

$gtotals=0;
$distot=0;
$disrate=0;
$ntotals=0;

while($srst=mysqli_fetch_row($sqry)){
$gtotals=$gtotals+$srst[1];
$distot=$distot+$srst[2];
$tty=$distot*-1;
$ntotals=$ntotals+$srst[1]-$srst[2];

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

$pdf->SetFont('Arial','',10);
$pdf->Cell(30,7,$ritems,1,0,'L');
$pdf->SetFont('Arial','',10);
$pdf->Cell(30,7,number_format($srst[1],2,'.',',')."	",1,0,'R');
$pdf->Cell(25,7,number_format($srst[2],2,'.',',')."	",1,0,'R');
$pdf->Cell(10,7,number_format($srst[2]/$srst[1]*100,1,'.',',')."	",1,0,'R');
$pdf->Cell(30,7,number_format($srst[1]-$srst[2],2,'.',',')."	",1,0,'R');
$pdf->Cell(28,7,$srst[6]."	",1,0,'R');
$pdf->Cell(32,7,$srst[6]."	",1,1,'R');
}

$pdf->SetFont('Arial','B',10);
$tmp="TOTAL	";
$pdf->Cell(30,7,$tmp,1,0,'L');
$pdf->Cell(30,7,number_format($gtotals,2,'.',',')."	",1,0,'R');
$pdf->Cell(25,7,number_format($distot,2,'.',',')."	",1,0,'R');
$tmp="	";
$pdf->Cell(10,7,number_format($distot/$gtotals*100,1,'.',',')."	",1,0,'R');
$pdf->Cell(30,7,number_format($ntotals,2,'.',',')."	",1,0,'R');
$pdf->Cell(28,7,$tmp,1,0,'L');
$pdf->Cell(32,7,$tmp,1,1,'L');

$pdf->SetFont('Arial','',10);
$pdf->Ln(12);
$tmp="----------------------------	";
$pdf->Cell(50,5,$tmp,0,0,'c');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'c');
$tmp="----------------------------	";
$pdf->Cell(50,5,$tmp,0,0,'c');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'c');
$tmp="----------------------------	";
$pdf->Cell(50,5,$tmp,0,1,'c');
$tmp="Prepared-Niroshan	";
$pdf->Cell(50,5,$tmp,0,0,'c');
$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'c');
$tmp="Sales/Accounts	";
$pdf->Cell(50,5,$tmp,0,0,'c');

$tmp="	";
$pdf->Cell(10,5,$tmp,0,0,'c');
$tmp="JT Managing Director	";
$pdf->Cell(50,5,$tmp,0,1,'c');


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

$pdf->output();

