<?php
require('session.php');
require('pdf-generate/fpdf.php');
//error_reporting(-1);
$s_bill_no = base64_decode($_REQUEST['billinfo']);

$rslt_main_det = $db->get_a_line(" select t1.*,DATE_FORMAT(t1.s_bill_date,'%d-%b-%Y') AS disp_bill_date, t2.company_name, t2.c_address, t2.c_city, t2.c_state, t2.c_country, t2.c_landline, t2.c_mobile, t3.cus_name, t3.cus_address, t3.cus_city, t3.cus_state, t3.cus_country,t3.cus_postcode,t3.cus_gstno from t_sales_entry_main t1 inner join m_company t2 on t1.company_id=t2.company_id inner join m_customer t3 on t1.cus_id = t3.cus_id  where 1=1 and t1.IsActive=1 and t1.s_bill_no='".$s_bill_no."' ");

$rslt_pdt_det =$db->get_rsltset("select * from t_sales_entry_details where 1=1 and IsActive=1 and s_bill_no='".$s_bill_no."'  ");


function getAmountInWords($no) {   
	
	//creating array  of word for each digit
	 $words = array('0'=> 'Zero' ,'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' => 'ninty','100' => 'hundred','1000' => 'thousand','100000' => 'lac','10000000' => 'crore');
	 //$words = array('0'=> '0' ,'1'=> '1' ,'2'=> '2' ,'3' => '3','4' => '4','5' => '5','6' => '6','7' => '7','8' => '8','9' => '9','10' => '10','11' => '11','12' => '12','13' => '13','14' => '14','15' => '15','16' => '16','17' => '17','18' => '18','19' => '19','20' => '20','30' => '30','40' => '40','50' => '50','60' => '60','70' => '70','80' => '80','90' => '90','100' => '100','1000' => '1000','100000' => '100000','10000000' => '10000000');
	 
	 
	 //for decimal number taking decimal part
	 
	$cash=(int)$no;  //take number wihout decimal
	$decpart = $no - $cash; //get decimal part of number
	
	$decpart=sprintf("%01.2f",$decpart); //take only two digit after decimal
	
	$decpart1=substr($decpart,2,1); //take first digit after decimal
	$decpart2=substr($decpart,3,1);   //take second digit after decimal  
	
	$decimalstr='';
	
	//if given no. is decimal than  preparing string for decimal digit's word
	
	if($decpart>0)
	{
	 $decimalstr.="point ".$numbers[$decpart1]." ".$numbers[$decpart2];
	}
	 
	if($no == 0)
		return ' ';
	else {
		$novalue='';
		$highno=$no;
		$remainno=0;
		$value=100;
		$value1=1000;       
		while($no>=100)    {
			if(($value <= $no) &&($no  < $value1))    {
			$novalue=$words["$value"];
			$highno = (int)($no/$value);
			$remainno = $no % $value;
			break;
			}
			$value= $value1;
			$value1 = $value * 100;
		}       
		if(array_key_exists("$highno",$words))  //check if $high value is in $words array
			return $words["$highno"]." ".$novalue." ".getAmountInWords($remainno).$decimalstr;  //recursion
		else {
			$unit=$highno%10;
			$ten =(int)($highno/10)*10;
			return $words["$ten"]." ".$words["$unit"]." ".$novalue." ".getAmountInWords($remainno
			).$decimalstr; //recursion
		}
	}
}
$invoice_template = ($rslt_main_det['company_id']==1)?'template_sarasawathi.jpg':'template_lakshmibai.jpg';
$pdf = new FPDF();
$pdf->AddPage();
$dir_path = $_SERVER['DOCUMENT_ROOT'].'/admin/';

$pdf->Image($dir_path.'pdf-generate/'.$invoice_template,2,2,-310);

$pdf->SetFont('Arial','B',11);
//Bill No
$pdf->SetXY(168, 70);
$pdf->Cell(0,0,"Bill No: ".$rslt_main_det['actu_bill_no']);

//Bill Date
$pdf->SetXY(168, 75);
$pdf->Cell(0,0,"Date: ".date('d-m-Y',strtotime($rslt_main_det['s_bill_date'])));

$pdf->SetFont('Arial','B',10);
//Customer Details
//1, Name
$pdf->SetXY(30, 65);
$pdf->Cell(0,0,'M/S. '.$rslt_main_det['cus_name']);
//2,Address
$pdf->SetXY(30, 70);
$pdf->Cell(0,0,$rslt_main_det['cus_address']);
//3,Postal Code
//4,City
$pdf->SetXY(30, 75);
$pdf->Cell(0,0,$rslt_main_det['cus_city']);

$pdf->SetXY(30, 80);
$pdf->Cell(0,0,$rslt_main_det['cus_postcode']);

//5,State
$pdf->SetXY(30, 85);
$pdf->Cell(0,0,$rslt_main_det['cus_state']);
//5,GST NO
$pdf->SetXY(30, 90);
$pdf->Cell(0,0,'GST No: '.$rslt_main_det['cus_gstno']);


   /*$pdf->SetFillColor(255,0,0);
    $pdf->SetTextColor(255);
    $pdf->SetDrawColor(128,0,0);
    $pdf->SetLineWidth(.3);
    $pdf->SetFont('Arial','B');*/
    $pdf->SetFillColor(255,0,0);
    $pdf->SetDrawColor(0,0,0);
    $pdf->SetLineWidth(.3);
    $header = array("S.No.",'Saree No','Particulars (HSN CODE: 50079010)','Qty','Amount');
    // Header
    $w = array(15, 25, 115, 10,25);
    $pdf->SetXY(10, 105);   
    for($i=0;$i<count($header);$i++){    	 	
        $pdf->Cell($w[$i], 5,$header[$i],1,0,'C');
    }
    $pdf->Ln();
    $i=1;
    $line_height = 110;
	$pdf->SetFont('Arial','B',8);
	$pdf->SetXY(10, $line_height);
	$total_lines = 16;
	$total_res = count($rslt_pdt_det);
	$lines_str = $total_lines - $total_res;
	foreach($rslt_pdt_det as $r){
		$pdf->SetXY(10, $line_height);
		$sku_code = mt_rand(100000,999999);
		//Line 1#detail
		//$pdf->SetXY(10, $line_height);
		$pdf->Cell(15,5,$i,1,0,'C');
		//$pdf->SetXY(20, $line_height);
		$pdf->Cell(25,5,$r['pdt_sku'],1,0,'C');
		//$pdf->SetXY(35, $line_height);
		$pdf->Cell(115,5,substr($r['saree_desc'],0,55),1,0,'L');
		//$pdf->SetXY(45, $line_height);
		$pdf->Cell(10,5,'1',1,0,'C');
		//$pdf->SetXY(80, $line_height);
		$pdf->Cell(25,5,$r['tot_amount'],1,0,'C');
		//$pdf->SetXY(196, $line_height);
		//$pdf->Cell(0,0,'-');
		$line_height += 5;
		$i++;
		$pdf->Ln();
	}
	
	//echo $total_res; exit;

	if($lines_str > 0){
		$total_cnt = ($total_res + $lines_str);
	  for($i=$total_res;$i<$total_cnt;$i++){  
	  	$pdf->SetXY(10, $line_height);

		$pdf->Cell(15,5,'',1,0,'C');
		//$pdf->SetXY(20, $line_height);
		$pdf->Cell(25,5,'',1,0,'C');
		$l = ($i == ($total_cnt-1)) ? 'Total' : '';
		if($i == ($total_cnt-1))
			$pdf->Cell(115,5,'Total',1,0,'R');
		else
			$pdf->Cell(115,5,'',1,0,'C');
		$l1 = ($i == ($total_cnt-1)) ? $total_res : '';
		$pdf->Cell(10,5,$l1,1,0,'C');
		$l2 = ($i == ($total_cnt-1)) ? $rslt_main_det['total_sale_amount'] : '';
		$pdf->Cell(25,5,$l2,1,0,'C');
		$pdf->Ln();
		$line_height += 5;
	  }
	}

	$line_height += 6;

	$left_hight = $line_height + 6;
	$label = ['BANK NAME', 'Account No', 'IFSC CODE'];	
	$label_Vaue = ['AXIS BANK', '915020052751058', 'UTIB0000534'];	

	for($i=0;$i<3;$i++){  
		$pdf->SetXY(10, $left_hight);
		$pdf->Cell(25,5,$label[$i],1,0,'C');
		$pdf->Cell(30,5,$label_Vaue[$i],1,0,'C');
		$left_hight += 5;
	}


	$label = ['Discount %', 'Sub Total', 'SGST 2.5%','CGST 2.5%','IGST 2.5%', 'NET TOTAL'];		
	$cgst_per = ($rslt_main_det['cgst_per']!=0)?$rslt_main_det['cgst_total']:'-';
	$sgst_per = ($rslt_main_det['sgst_per']!=0)?$rslt_main_det['sgst_total']:'-';
	$igst_per = ($rslt_main_det['igst_per']!=0)?$rslt_main_det['igst_total']:'-';

	$label_value = [$rslt_main_det['discount_per'], $rslt_main_det['after_discount_amt'],$sgst_per,$cgst_per,$igst_per,$rslt_main_det['final_sale_amt']];
	for($i=0;$i<6;$i++){  
		$pdf->SetXY(150, $line_height);
		$pdf->Cell(25,5,$label[$i],1,0,'C');
		$pdf->Cell(25,5,$label_value[$i],1,0,'C');
		$line_height += 5;
	}

	$line_height += 5;
	$pdf->SetFont('Arial','B',10);
	$pdf->Text(10,$line_height,'Amount in words: '.rtrim(ucfirst(getAmountInWords($rslt_main_det['final_sale_amt']))).' Only /-');
	$pdf->SetFont('Arial','B',9);
	$line_height += 10;
	$pdf->Text(10,$line_height,'TERMS & CONDITIONS');
	$line_height += 5;
	$pdf->Text(10,$line_height,'* Goods and sold will not be accepted back under any Circumstances');
	$line_height += 5;
	$pdf->Text(10,$line_height,'* All transactions are strictly');
	$line_height += 5;
	$pdf->Text(10,$line_height,'SUBJECT TO THE JURISDICTION OF KANCHIPURAM');
	$line_height += 5;
	$pdf->Text(10,$line_height,'only where company is situated');

	$pdf->SetFont('Arial','B',10);
	$pdf->Text(150,$line_height,'For Sri Saraswathi Silks');

    // Color and font restoration
    /*$pdf->SetFillColor(224,235,255);
    $pdf->SetTextColor(0);
    $pdf->SetFont('Arial');
    // Data
    $fill = false;
    foreach($data as $row)
    {
        $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
        $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
        $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill);
        $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill);
        $this->Ln();
        $fill = !$fill;
    }
    // Closing line
    $pdf->Cell(array_sum($w),0,'','T');


//Order Line Details 
$line_height = 108;
$i=1;
$pdf->SetFont('Arial','B',8);
foreach($rslt_pdt_det as $r){
	$sku_code = mt_rand(100000,999999);
	//Line 1#detail
	$pdf->SetXY(28, $line_height);
	$pdf->Cell(0,0,$i);
	$pdf->SetXY(35, $line_height);
	$pdf->Cell(0,0,$r['pdt_sku']);
	$pdf->SetXY(55, $line_height);
	$pdf->Cell(0,0,substr($r['saree_desc'],0,55));
	$pdf->SetXY(155, $line_height);
	$pdf->Cell(0,0,'1');
	$pdf->SetXY(175, $line_height);
	$pdf->Cell(0,0,$r['tot_amount']);
	$pdf->SetXY(196, $line_height);
	$pdf->Cell(0,0,'-');
	$line_height += 6;
	$i++;
}

//Total Display
//Total
$pdf->SetXY(175, 211);
$pdf->Cell(0,0,$rslt_main_det['total_sale_amount']);

//Less
$pdf->SetXY(175, 217);
$pdf->Cell(0,0,($rslt_main_det['total_sale_amount'] - $rslt_main_det['after_discount_amt']));

//Less Percent
//Less (1%)
$pdf->SetXY(146, 217);
$pdf->Cell(0,0,'('.$rslt_main_det['discount_per'].'%)');

//Taxable Amount
$pdf->SetXY(175, 223);
$pdf->Cell(0,0,$rslt_main_det['after_discount_amt']);

//GST Calculation
$cgst_per = ($rslt_main_det['igst_per']==0)?$rslt_main_det['cgst_total']:'-';
$sgst_per = ($rslt_main_det['igst_per']==0)?$rslt_main_det['sgst_total']:'-';
$igst_per = ($rslt_main_det['igst_per']!=0)?$rslt_main_det['igst_total']:'-';

//if($rslt_main_det['igst_per']==0) {
	//CGST
	$pdf->SetXY(175, 230);
	$pdf->Cell(0,0,$cgst_per);
	//SGST
	$pdf->SetXY(175, 236);
	$pdf->Cell(0,0,$sgst_per);
//}
//if($rslt_main_det['igst_per']!=0) {
	//IGST
	$pdf->SetXY(175, 242);
	$pdf->Cell(0,0,$igst_per);
//}
//GRAND Total
$pdf->SetFont('Arial','B',11);
$pdf->SetXY(170, 249);
$pdf->Cell(0,0,$rslt_main_det['final_sale_amt']);

$pdf->SetFont('Arial','B',8);
//Notes
$pdf->SetXY(40, 260);
$pdf->Cell(0,0,$rslt_main_det['notes']);

//GRAND Total in words
$pdf->SetFont('Arial','B',9);
$pdf->SetXY(53, 248);
$pdf->Cell(0,0,':'.ucfirst(getAmountInWords($rslt_main_det['final_sale_amt'])));*/
$pdf->Output();
?>
