<?php 
session_start();
require('../../fpdf/fpdf.php');

class PDF_result extends FPDF {
	 	var $totalval;
		var $mrpval;
		var $qtyval;
		var $excessval;
		var $getYaxis;
	function __construct ($orientation = 'P', $unit = 'pt', $format = 'A4', $margin = 10) {
		$this->FPDF($orientation, $unit, $format);
		$this->SetTopMargin($margin);
		$this->SetLeftMargin($margin);
		$this->SetRightMargin($margin);
		
		$this->SetAutoPageBreak(true, $margin);
	}
	

	
 function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number
    $this->Cell(0,10,'This is a computer generated Invoice'."(Page ".$this->PageNo().")",0,0,'C');
}



function numtowords($number){ 
$no = round($number);
//$point = round($number - $no, 2) * 100;
   $hundred = null;
   $digits_1 = strlen($no);
   $i = 0;
   $str = array();
   $words = array('0' => '', '1' => 'One', '2' => 'Two',
    '3' => 'Three', '4' => 'Four', '5' => 'Five', '6' => 'Six',
    '7' => 'Seven', '8' => 'Eight', '9' => 'Nine',
    '10' => 'Ten', '11' => 'Eleven', '12' => 'Twelve',
    '13' => 'Thirteen', '14' => 'Fourteen',
    '15' => 'Fifteen', '16' => 'Sixteen', '17' => 'Seventeen',
    '18' => 'Eighteen', '19' =>'Nineteen', '20' => 'Twenty',
    '30' => 'Thirty', '40' => 'Forty', '50' => 'Fifty',
    '60' => 'Sixty', '70' => 'Seventy',
    '80' => 'Eighty', '90' => 'Ninety');
   $digits = array('', 'Hundred', 'Thousand', 'Lakh', 'Crore');
   while ($i < $digits_1) {
     $divider = ($i == 2) ? 10 : 100;
     $number = floor($no % $divider);
     $no = floor($no / $divider);
     $i += ($divider == 10) ? 1 : 2;
     if ($number) {
        $plural = (($counter = count($str)) && $number > 9) ? 's' : null;
        $hundred = ($counter == 1 && $str[0]) ? ' and ' : null;
        $str [] = ($number < 21) ? $words[$number] .
            " " . $digits[$counter] . $plural . " " . $hundred
            :
            $words[floor($number / 10) * 10]
            . " " . $words[$number % 10] . " "
            . $digits[$counter] . $plural . " " . $hundred;
     } else $str[] = null;
  }
  $str = array_reverse($str);
  $result = implode('', $str);
  
 

    return $result . "Rupees  " ; 
} 


	
function Generate_Table(&$billdet_res_row,$con) {
	$billno=$_POST['orderid'];
	 $this->SetY($this->GetY() + 20);
	$this->getYaxis = $this->GetY();
	$bill_res=mysqli_query($con,"SELECT * FROM   drcrnoteregdet WHERE orderid =  '$billno' ORDER BY srno");
	
	$w=array(20,55,100,220,30,40,50,75,16,11);
	$this->SetFont('Arial', '', 8);
	$this->Cell($w[0], 25, "S.No", '1', 0, 'C');
	$this->Cell($w[1], 25, "Style Code", '1', 0, 'C');
	$this->Cell($w[1], 25, "Product Desp.", '1', 0, 'C');
	$this->Cell($w[1], 25, "Shade", '1', 0, 'L');
	$this->Cell($w[3], 25, "", '1', 0, 'C');
	$this->Cell($w[5], 25, "QTY", '1', 0, 'C');
	$this->Cell($w[5], 25, "MRP", '1', 0, 'C');
	$this->Cell($w[5], 25, "WSP", '1', 0, 'C');
	$this->Cell($w[5]+5, 25, "Amount", '1', 1, 'C');
	
	$wd='10';
	$this->SetXY($w[0]+$w[1]+$w[1]+$w[1]+10,$this->GetY()-23);
	$this->SetFont('Arial', 'B', 6);
	$this->Cell($w[9], $wd, "28", '0', 0, 'C');
	$this->Cell($w[9], $wd, "30", '0', 0, 'C');
	$this->Cell($w[9], $wd, "32", '0', 0, 'C');
	$this->Cell($w[9], $wd, "34", '0', 0, 'C');
	$this->Cell($w[8], $wd, "36", '0', 0, 'C');
	$this->Cell($w[8], $wd, "38", '0', 0, 'C');
	$this->Cell($w[8], $wd, "40", '0', 0, 'C');
	$this->Cell($w[8], $wd, "42", '0', 0, 'C');
	$this->Cell($w[8], $wd, "44", '0', 0, 'C');
	$this->Cell($w[8], $wd, "46", '0', 0, 'C');
	$this->Cell($w[8], $wd, "48", '0', 0, 'C');
	$this->Cell($w[8], $wd, "50", '0', 0, 'C');
	$this->Cell($w[8], $wd, "52", '0', 0, 'C');
	$this->Cell($w[8], $wd, "54", '0', 0, 'C');
	$this->Cell($w[8], $wd, "56", '0', 1, 'C');
	
	$this->SetXY($w[0]+$w[1]+$w[1]+$w[1]+10,$this->GetY());
	$this->SetFont('Arial', 'B', 6);
	$this->Cell($w[9], $wd, "S", '0', 0, 'C');
	$this->Cell($w[9], $wd, "M", '0', 0, 'C');
	$this->Cell($w[9], $wd, "L", '0', 0, 'C');
	$this->Cell($w[9], $wd, "XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "2XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "3XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "4XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "5XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "6XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "7XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "8XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "9XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "10XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "11XL", '0', 0, 'C');
	$this->Cell($w[8], $wd, "12XL", '0', 1, 'C');
	
	$this->Ln(5);
	 
	$this->SetFont('Arial', '',10);
	$fill = false;
		$x = $this->GetX();
		$y = $this->GetY();
	while($row=mysqli_fetch_array($bill_res)) {
		if($c>600)
		{
			$this->AddPage();
			$this->bill_header($billdet_res_row);
			$c=0;
			$this->SetFont('Arial', 'B', 9);
			$this->Cell($w[0], 25, "SNo", '1', 0, 'C');
			$this->Cell($w[1], 25, "PRODUCT", '1', 0, 'C');
			$this->Cell($w[1], 25, "Product Desp.", '1', 0, 'C');
			$this->Cell($w[1], 25, "SHADE", '1', 0, 'C');
			$this->Cell($w[3], 25, "Size / Qty (Pieces)", '1', 0, 'C');
			$this->Cell($w[5], 25, "QTY", '1', 0, 'C');
			$this->Cell($w[5], 25, "UNIT", '1', 0, 'C');
			$this->Cell($w[5], 25, "RATE", '1', 0, 'C');
			$this->Cell($w[5]+5, 25, "AMOUNT", '1', 1, 'C');
			$this->SetFont('Arial', '');
			$this->Ln(5);
	
		}
			
			$this->SetFont('Arial', '', 8);
			$this->Cell($w[0], 12,$row['srno'], 0, 0, 'L');
			$x = $this->GetX();
			$y1 = $this->GetY();
			$this->MultiCell($w[1], 12, $row['prodcode'],0,'L');	
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY($x + $w[1], $this->GetY() - $yH);
			
			
			
			$x = $this->GetX();
			$y1 = $this->GetY();
			$this->MultiCell($w[1], $yH, iconv("UTF-8", "ISO-8859-2", $row['cat']), 0,'L');	
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY($x + $w[1], $this->GetY() - $yH);
			
			
			
			$x = $this->GetX();
			$y1 = $this->GetY();
			$this->MultiCell($w[1], $yH, $row['colorcode'],0,'L');	
			$y2 = $this->GetY();
			$yH = $y2 - $y1;
			$this->SetXY($x + $w[1], $this->GetY() - $yH);
			
			
			
			
			 
			$x = $this->GetX();
			$y1 = $this->GetY();
            $this->SetFont('Arial', '', 7);
			$jobvar_res=mysqli_query($con,"SELECT * FROM   drcrnotevar WHERE job_id =  '$billno' and prodname = '$row[prodcode]' and srno = '$row[srno]'  ORDER BY entryid");
			$ival=1;
			$iinc=0;
			while($jobvarrow=mysqli_fetch_array($jobvar_res)) {
				if($ival>=3){
					$iinc=2;
					}
			$this->Cell($w[9]+2+$iinc,  $yH,  $jobvarrow['qnty'],0,0,'L');
				
			 //$qty = $jobvarrow['qnty']."--".$qty;
			 $ival++;
		     } 
			
			
		
		$this->SetFont('Arial', '', 8);
		$this->Cell($w[5], $yH, $row['qty'], 0, 0, 'R');
		$this->Cell($w[5], $yH, $row['mrp'], 0, 0, 'R');
		$this->Cell($w[5], $yH, $row['pcost'], 0, 0, 'R');
		$this->Cell($w[5], $yH, $row['pamt'], 0, 1, 'R');
		
		$c=$this->GetY();
		$this->totalval += $row['pamt'];
		$this->mrpval += $row['mrp'];
		$this->qtyval += $row['qty'];
		$this->excessval += $row['qty'] *$row['mrp'];
		$this->Line(10,$this->GetY(),580,$this->GetY());
			}
		$y=$this->GetY();
	
	
	$this->bill_footer($billdet_res_row);	
}//end of Generate_Table()	



function bill_header(&$billdet_res_row,$con){
$this->SetFont('Arial', '',9);
$headY=$this->GetY();
$this->Cell(0, 10, "Subject to Mumbai Jurisdiction", 0, 1, 'L');
$this->SetFont('Arial', 'B',9);

$this->SetY($headY);
$this->Cell(0, 10, "CREDIT NOTE", 0, 1, 'C');






$ty=$this->GetY();
$this->make_border($this->GetY(),830);	


$this->SetY($ty+2);
$this->SetFont('Arial', 'B',12);
$this->MultiCell(0, 14, $_SESSION[SESS_CMP],'0','C');
$this->SetFont('Arial', '',9);
$this->MultiCell(0, 13, $_SESSION[SESS_CMPADD1],'0','C');


$this->MultiCell(0, 13, "$_SESSION[SESS_CMPADD1_1]",'L','C');


$this->SetFont('Arial', 'B');

$othersY = $this->GetY();
$this->SetFont('Arial', 'B');
$this->Cell(30, 13, 'M/s.','LT',0 ,'L');
$this->SetFont('Arial', '');
$this->Cell(270, 13, " $billdet_res_row[name]",'RT',1 ,'L');
	
$this->SetFont('Arial', '');

$party=	"$billdet_res_row[address]";
	
$this->SetFont('Arial', '', 8);		
$this->MultiCell(300, 13, $party,'L','L');	



$this->SetFont('Arial', '',9);
$this->Cell(60, 13, 'VAT TIN No. :','L',0 ,'L');
$this->SetFont('Arial', '');
$this->Cell(240, 13, " $billdet_res_row[cmpvat]",'R',1 ,'L');

$this->SetFont('Arial', '',9);
$this->Cell(60, 13, 'CST TIN No. :','L',0 ,'L');
$this->SetFont('Arial', '');
$this->Cell(240, 13, " $billdet_res_row[tin]",'R',1 ,'L');







$this->SetXY(310,$othersY);
$this->SetFont('Arial', 'B');
$this->Cell(70, 13, "Credit Note No.",'LT',0);
$this->SetFont('Arial', '');
$this->Cell(80, 13, ": $billdet_res_row[orderid]",'T',0);
$this->SetFont('Arial', 'B');
$this->Cell(50, 13, 'Date','T',0 ,'L');
$this->SetFont('Arial', '');
$this->Cell(70, 13, ": $billdet_res_row[orderdate]",'TR',1 ,'L');





$this->SetFont('Arial', '');
$this->SetX(310);
$this->SetFont('Arial', '');
$this->Cell(70, 13, "Packing Slip No. ",'0',0);
$this->SetFont('Arial', '');
$this->Cell(80, 13, ": $billdet_res_row[refno]",'0',0);
$this->SetFont('Arial', '');
$this->Cell(50, 13, 'Date','0',0 ,'L');
$this->SetFont('Arial', '');
$this->Cell(70, 13, ": $billdet_res_row[dlvdate]",'0',1 ,'L');




$this->SetX(310);
$this->SetFont('Arial', '');
$this->Cell(70, 13, "L.R.No.",'0',0);
$this->SetFont('Arial', '');
$this->Cell(80, 13, ": $billdet_res_row[lrno]",'0',0);
$this->SetFont('Arial', '');
$this->Cell(50, 13, 'Date','0',0 ,'0');
$this->SetFont('Arial', '');
$this->Cell(70, 13, ": $billdet_res_row[lrdate]",'0',1 ,'L');

$this->SetX(310);
$this->SetFont('Arial', '');
$this->Cell(70, 13, "Transport",'0',0);
$this->SetFont('Arial', '');
$this->Cell(200, 13, ": $billdet_res_row[shipvia]" ,'0',1);



$this->SetX(310);
$this->SetFont('Arial', '');
$this->Cell(70, 13, "Cases",'0',0);
$this->SetFont('Arial', '');
$this->Cell(200, 13, ": $billdet_res_row[totparcel]",'0',1);


	$this->line(310,$othersY,310,$this->GetY()+20);
$this->SetFont('Arial', '');
}//end of bill_header()


function bill_footer(&$billdet_res_row){
	$y=550;
	$this->Line(10,$y,580,$y);
	//$this->Line(10,$fotty,10,$this->GetY());
	
	$this->SetY($y);
	
	$this->SetFont('helvetica', 'B',13);
$this->Line(16,$this->GetY(),583,$this->GetY());
$this->Ln(10);
$this->Cell(0, 15, "Remark :  ".$billdet_res_row['notes'],0,0,'L');

$this->SetFont('helvetica', 'B',15);

$this->SetXY(350,$this->GetY());
$this->Cell(0, 13, "Total Amt Payable :   ",0,0,'L');
$this->SetXY(370,$this->GetY());
$this->Cell(0, 13, $this->totalamt.".00",0,1,'R');


$this->SetY($this->GetY()+50);
$this->SetFont('helvetica', 'B',12);
$this->Cell(50, 13, "Received By",0,0,'L');
$this->SetY($this->GetY());
$this->Cell(0, 13, "Checked By",0,0,'C');
$this->SetXY(600,$this->GetY());
$this->Cell(0, 13, "Autho.Sign",0,0,'R');
	
	
	
	
	
	$t=$this->GetY();
	$this->SetFont('Arial', 'B',9);
	
	$this->SetXY(430,$y);
		
		$this->line(30,$this->getYaxis,30,$this->GetY());
		$this->line(85,$this->getYaxis,85,$this->GetY());
		$this->line(140,$this->getYaxis,140,$this->GetY());
		$this->line(195,$this->getYaxis,195,$this->GetY());
		$this->line(415,$this->getYaxis,415,$this->GetY());
		$this->line(455,$this->getYaxis,455,$this->GetY());
		$this->line(495,$this->getYaxis,495,$this->GetY());
		$this->line(535,$this->getYaxis,535,$this->GetY());
		
	
		
		//$this->SetY($t+10);
		
	$this->make_border($start_border,$end_border);
		//$this->Line(10,800,590,800);
		//$this->Line(10,802,590,802);
		//$this->SetY(810);
		//$this->Cell(75, 15, 'Prepared By:');
		//$this->Cell(350, 15, $billdet_res_row['prepby'],0,0,'L');
		//$this->Cell(75, 15, 'Checked By');
		//$this->Cell(0, 15, $billdet_res_row['checkby'],0,0,'L');
		//$this->Cell(0, 15, "SUBJECT TO MUMBAI JURISDICTION",0,0,'C');
	}// end of bill_footer()


	
	function make_border($start_border,$end_border)
	{
		$this->Line(10,$start_border,580,$start_border);
			//$this->Line(16,$start_border,584,$start_border);
			
			
			$this->Line(10,$start_border,10,$end_border-5); 
			//$this->Line(16,$start_border,16,$end_border); 
			
			
			$this->Line(580,$start_border,580,$end_border-5);
			//$this->Line(586,$start_border,586,$end_border);
			
			$this->Line(10,$end_border-5,580,$end_border-5);
			//$this->Line(10,$end_border,586,$end_border);
		}

}//end of class


$pdf = new PDF_result();
$pdf->AddPage();


$billno=$_POST['orderid'];
include '../../db_connect.php';
	//to select the company's table in current session
include '../../db_table_select.php';


$billdet_res=mysqli_query($con,"SELECT `orderid`,   DATE_FORMAT(`orderdate`,'%d/%m/%Y') as `orderdate`, `ordertype`, `buyercode`, `billedto`, `refno`,  DATE_FORMAT(`dlvdate`,'%d/%m/%Y') as `dlvdate`, `ratetype`, `shipto`, `agentcode`, `distr`, `shipvia`, `shippingpaid`, `totparcel`, `drcrtype`, `lrno`, DATE_FORMAT(`lrdate`,'%d/%m/%Y') as `lrdate`,`comm`, `comm_on`, `notes`, drcrnotereg.`vat` as vat ,`packinglistno`,disamt, `ptotal`, `pdiscount`, `pnamount`,  `grtotal`, `status`, commst.name, commst.address, commst.vat as cmpvat , commst.tin, commst.telephone
FROM  drcrnotereg INNER JOIN commst ON buyercode=compid 
WHERE orderid = '$billno'");

$billdet_res_row=mysqli_fetch_assoc($billdet_res);
$pdf->SetFont('Arial', 'B', 10);
$pdf->bill_header($billdet_res_row,$con);
	
$pdf->Generate_Table($billdet_res_row,$con);	
$pdf->Output('drcrnote_entry.pdf', 'F');



?>
