 <?php
require('pdf/fpdf.php');
require('../config/dbcon.php');//connect
$query=mysqli_query($connect,"Select * from company_mst");
while($row=mysqli_fetch_array($query)){
	$company_name = $row["company_name"];
	$company_address_one = $row["company_address_one"];
	$company_address_two = $row["company_address_two"];
	$company_address_three = $row["company_address_three"];
	$company_address_four = $row["company_address_four"];
	$company_place = $row["company_place"];
	$company_pin = $row["company_pin"];
	$company_st_code = $row["company_st_code"];
	$company_st_name = $row["company_st_name"];
	$company_phone = $row["company_phone"];
	$company_mobile = $row["company_mobile"];
	$company_email = $row["company_email"];
	$company_gstno = $row["company_gstno"];
	$company_pan_no = $row["company_pan_no"];
	$company_bank_name = $row["company_bank_name"];
	$company_bank_accno = $row["company_bank_accno"];
	$company_ifsc_code = $row["company_ifsc_code"];
	$company_bank_address = $row["company_bank_address"];
}
$query=mysqli_query($connect,"Select * from invtotalprt_mst");
while($row=mysqli_fetch_array($query)){
	$inv_number = $row["inv_number"];
	$inv_date = $row["inv_date"];
	$customer_code = $row["customer_code"];
	$customer_name = $row["customer_name"];
	$cust_address_one = $row["cust_address_one"];
	$cust_address_two = $row["cust_address_two"];
	$cust_address_three = $row["cust_address_three"];
	$cust_place = $row["cust_place"];
	$cust_phone = $row["cust_phone"];
	$cust_pin_code = $row["cust_pin_code"];
	$cust_email = $row["cust_email"];
	$cust_mobile = $row["cust_mobile"];
	$cust_gstno = $row["cust_gstno"];
	$cust_st_code = $row["cust_st_code"];
	$cust_st_name = $row["cust_st_name"];
	$cust_pan_no = $row["cust_pan_no"];
	$po_no = $row["po_no"];
	$po_date = $row["po_date"];
	$challan_no = $row["challan_no"];
	$challan_date = $row["challan_date"];
	$lr_no = $row["lr_no"];
	$lr_dt = $row["lr_dt"];
	$transporter_name = $row["transporter_name"];
	$no_of_parcel = $row["no_of_parcel"];
	$rsinwords = $row["rsinwords"];
	$due_days = $row["due_days"];
	$due_date = $row["due_date"];
	$tax_code = $row["tax_code"];
	$total_qty = $row["total_qty"];
	$total_disc_amt = $row["total_disc_amt"];
	$total_list_value = $row["total_list_value"];
	$total_sales_val = $row["total_sales_val"];
	$total_sgstamt = $row["total_sgstamt"];
	$total_cgstamt = $row["total_cgstamt"];
	$total_igstamt = $row["total_igstamt"];
	$total_val = $row["total_val"];
	$roundoff = $row["roundoff"];
	$total_inv_val = $row["total_inv_val"];	
}
$query=mysqli_query($connect,"Select * from customer_mst where customer_code = '".$customer_code."'");
while($row=mysqli_fetch_array($query)){
	$clbalance = $row["clbalance"];	
}
/*A4 width : 219mm*/
//$pdf = new FPDF('P','mm',array(300,240));
$pdf = new FPDF('P','mm',array(360,250));

$pdf->AddPage();
//$pdf->SetTopMargin(250);
/*output the result*/
/*set font to Arial, bold, 14pt*/
$pdf->SetFont('Arial','B',10);
/*Cell(width , height , text , border , end line , [align] )*/

$pdf -> Line(10, 10, 247, 10); // TOP LEFT TO RIGHT LINE
$pdf -> Line(10, 10, 10, 325); //FIST LEFT TOP TO BOTTOM LINE
$pdf -> Line(10, 37, 247, 37); //SECOND LEFT TOP TO BOTTOM LINE///////////////

$pdf -> Line(10, 42, 247, 42); //SECOND LEFT TOP TO BOTTOM LINE (AFTER COMPANY DETAILS)///////////////
$pdf -> Line(247, 10, 247, 325);//FIST RIGHT TOP TO BOTTOM LINE
$pdf -> Line(10, 80, 247, 80); //SECOND LEFT TOP TO BOTTOM LINE (AFTER COMPANY DETAILS)
$pdf -> Line(10, 240, 247, 240);//BOTTOM LINE (AFTER PRODUCT DETAILS)
$pdf -> Line(10, 325, 247, 325);//LAST BOTTOM LINE (END OF PAGE)
$pdf -> Line(10, 325, 247, 325);//LAST BOTTOM LINE (END OF PAGE)


$pdf->Cell(0,6,'GST No:'.''.$company_gstno,0,0,'L');
$pdf->SetFont('Arial','',10);
$pdf->Cell(-10,6,'[   ]',0,0,'R');
$pdf->Cell(17,6,'Original    ',0,0,'R');
$pdf->Cell(-17,15,'[   ]',0,0,'R');
$pdf->Cell(17,15,'Duplicate ',0,0,'R');
$pdf->Cell(-17,24,'[   ]',0,0,'R');
$pdf->Cell(16,24,'Triplicate ',0,0,'R');
//=========================================
$pdf->SetFont('Arial','B',22);
$pdf->Cell(-236 ,0,'',0,0);
$pdf->Cell(0,20,$company_name,0,0,'C');
$pdf->SetFont('Arial','',9);
$pdf->Cell(-230 ,0,'',0,0);

$pdf->Cell(0,30,$company_address_one." ".$company_address_two ,0,0,'C');
$pdf->Cell(-230 ,0,'',0,0);

$pdf->Cell(0,36,$company_address_three." ".$company_address_four." ".$company_place." ".$company_pin,0,0,'C');

$pdf->Cell(-230 ,0,'',0,0);
$pdf->Cell(0,42,"Ph. " .$company_phone." Mob. ".$company_mobile." Email. ".$company_email,0,0,'C');

//=========================================
$pdf->SetFont('Arial','',10);
$pdf->Cell(-230 ,0,'',0,0);

$pdf->Cell(0,50,'PAN No. :'.$company_pan_no,0,0,'L');
//$pdf->SetFont('Arial','',10);
if($tax_code == "GST-B2B"){
	$billtype = 'CREDIT BILL';
	$invtype = 'TAX INVOICE';
}
if($tax_code == "GST-B2C"){
	$billtype = 'CASH BILL';
	$invtype = 'RETAIL INVOICE';
}
if($tax_code == "I-GST"){
	$billtype = 'CREDIT BILL';
	$invtype = 'TAX INVOICE (INTER-STATE)';
}
$pdf->Cell(7,50,$billtype,0,0,'R');
//=========================================
$pdf->Cell(-237 ,0,'',0,0);
$pdf->Cell(0,60,'Reverse Charge : No',0,0,'L');

$pdf->SetFont('Arial','B',12);
$pdf->Cell(-227 ,0,'',0,0);
$pdf->Cell(0,60,$invtype,0,0,'C');

$pdf->SetFont('Arial','',10);
$pdf->Cell(-50 ,0,'',0,0);
$pdf->Cell(57,60,'State Code :'.$company_st_code,0,0,'R');
//------------------------------------------------------------------------- end of company header part
$pdf->SetFont('Arial','B',12);

$invn_date = date('d/m/Y', strtotime(date($inv_date)));

$pdf->Cell(-120 ,20,'',0,0);
$pdf->Cell(23 ,72,'Invoice No',0,0);
$pdf->Cell(3 ,72,' :',0,0);
$pdf->Cell(40 ,72,$inv_number,0,0);
$pdf->Cell(25 ,72,'Invoice Date',0,0);
$pdf->Cell(3 ,72,' :',0,0);
$pdf->Cell(0 ,72,$invn_date,0,0);

$pdf->SetFont('Arial','',10);
$pdf -> Line(125, 50, 247, 50);
$pdf -> Line(125, 85, 125, 42);
$pdf -> Line(10, 248, 247, 248);//LAST BOTTOM LINE (END OF PAGE)
$pdf -> Line(10, 284, 247, 284);//LAST BOTTOM LINE (END OF PAGE)
$pdf -> Line(10, 292, 247, 292);//LAST BOTTOM LINE (END OF PAGE)
$pdf->Cell(-113 ,10,'',0,0);

$pdf->Cell(23 ,90,'P O No.',0,0);
$pdf->Cell(3 ,90,' :',0,0);
$pdf->Cell(40 ,90,$po_no,0,0);
$pdf->Cell(25 ,90,'P O Dt',0,0);
$pdf->Cell(3 ,90,' :',0,0);
$pdf->Cell(10 ,90,$po_date,0,0);

$pdf->Cell(-104 ,10,'',0,0);
$pdf->Cell(23 ,100,'Challan No.',0,0);
$pdf->Cell(3 ,100,' :',0,0);
$pdf->Cell(40 ,100,$challan_no,0,0);
$pdf->Cell(25 ,100,'Challan Dt',0,0);
$pdf->Cell(3 ,100,' :',0,0);
$pdf->Cell(10 ,100,$challan_date,0,0);


$pdf->Cell(-104 ,10,'',0,0);
$pdf->Cell(23 ,110,'Due Days',0,0);
$pdf->Cell(3 ,110,' :',0,0);
$pdf->Cell(40 ,110,$due_days,0,0);
$pdf->Cell(25 ,110,'Due Dt',0,0);
$pdf->Cell(3 ,110,' :',0,0);
$pdf->Cell(10 ,110,$due_date,0,0);

$pdf->Cell(-104 ,10,'',0,0);
$pdf->Cell(23 ,120,'A/C Balance',0,0);
$pdf->Cell(3 ,120,' :',0,0);
$pdf->Cell(30 ,120,$clbalance,0,0);

$pdf->SetFont('Arial','B',10);
$pdf->Cell(-172 ,20,'',0,0);

$pdf->Cell(100 ,70,'M/S '.$customer_name,0,0);
$pdf->SetFont('Arial','',9);
$pdf->Cell(-100 ,20,'',0,0);

$pdf->Cell(25 ,80,$cust_address_one,0,0);
$pdf->Cell(-25 ,20,'',0,0);
$pdf->Cell(25 ,90,$cust_address_two,0,0);
$pdf->Cell(-25 ,20,'',0,0);
$pdf->Cell(25 ,100,$cust_address_three,0,0);
$pdf->Cell(-25 ,20,'',0,0);
$pdf->Cell(25 ,110,$cust_place." ".$cust_pin_code." ". $cust_st_name,0,0);
$pdf->Cell(-25 ,20,'',0,0);
$pdf->Cell(25 ,120,$cust_phone." ".$cust_mobile,0,0);

$pdf->SetFont('Arial','B',10);
$pdf->Cell(-25 ,20,'',0,0);

$pdf->Cell(25 ,135,'GST No. :'.$cust_gstno,0,0);
$pdf->Cell(60 ,20,'',0,0);

$pdf->Cell(25 ,135,'State Code: '.$cust_st_code,0,0);

$pdf->SetFont('Arial','',10);
$pdf->Cell(-110 ,20,'',0,0);

$pdf->Cell(25 ,145,'Transporter :'.$transporter_name,0,0);

$pdf->Cell(90 ,20,'',0,0);

$pdf->Cell(25 ,145,'LR/Vhcl. No.'.$lr_no,0,0);

$pdf->Cell(26 ,20,'',0,0);

$pdf->Cell(25 ,145,'Parcel/Cases.'.$no_of_parcel,0,0);

$height = 150;
$pdf->Cell(0 ,75,'',0,1);
$pdf->SetFont('Arial','B',9);
/*Heading Of the table*/
$pdf->Cell(10 ,5,'Sl',1,0,'C');
$pdf->Cell(26 ,5,'Part Number',1,0,'L');
$pdf->Cell(60 ,5,'Description',1,0,'L');
$pdf->Cell(14 ,5,'HSN',1,0,'C');
$pdf->Cell(14 ,5,'Qty',1,0,'R');
$pdf->Cell(15 ,5,'Price',1,0,'R');
$pdf->Cell(12 ,5,'Disc%',1,0,'R');
$pdf->Cell(12 ,5,'Rate',1,0,'R');
$pdf->Cell(15 ,5,'Taxable',1,0,'C');
$pdf->Cell(13 ,5,'SGST%',1,0,'C');
$pdf->Cell(13 ,5,'CGST%',1,0,'C');
$pdf->Cell(13 ,5,'IGST%',1,0,'C');
$pdf->Cell(20 ,5,'Total',1,1,'R');/*end of line*/
/*Heading Of the table end*/

$query=mysqli_query($connect,"Select * from invprt_mst where inv_number = '".$inv_number."'");
$num_rows = mysqli_num_rows($query);
$rcount = $num_rows; // number of rows in inv_mst

$i = 1;
if($rcount <= 30){
$pdf->SetFont('Arial','',9);
$query=mysqli_query($connect,"Select * from invprt_mst where inv_number = '".$inv_number."'");
	while($row=mysqli_fetch_array($query)){
		
		$rcount = $rcount + 1;
		$height = $height -5;
		$pdf->Cell(10 ,5,$i,0,0,'C');
		$i = $i + 1;
		$pdf->Cell(26 ,5,$row["partno"],0,0);
		$pdf->Cell(59 ,5,$row["description_one"],0,0);
		$pdf->SetFont('Arial','',8);
		$pdf->Cell(16 ,5,$row["hsn_code"],0,0,'C');
		$pdf->SetFont('Arial','',9);
		$pdf->Cell(13 ,5,$row["qty"],0,0,'R');
		$pdf->Cell(15 ,5,number_format($row["list"],2, '.', ''),0,0,'R');
		$pdf->Cell(12 ,5,number_format($row["disc_per"],2, '.', ''),0,0,'R');
		$pdf->Cell(12 ,5,number_format($row["rate_one"],2, '.', ''),0,0,'R');
		$pdf->Cell(15 ,5,number_format($row["sales_val"],2, '.', ''),0,0,'R');
		$pdf->Cell(13 ,5,number_format($row["sgst_per"],2, '.', ''),0,0,'R');
		$pdf->Cell(13 ,5,number_format($row["cgst_per"],2, '.', ''),0,0,'R');
		$pdf->Cell(13 ,5,number_format($row["igst_per"],2, '.', ''),0,0,'R');
		$pdf->Cell(20 ,5,number_format($row["inv_val"],2, '.', ''),0,1,'R');
	}
	for ($i = 30; $i <= 31; $i++) {
		$pdf -> Line(19.98, 240, 19.98, 85);
		$pdf -> Line(46, 240, 46, 85);
		$pdf -> Line(106, 248, 106, 85);
		$pdf -> Line(120, 248, 120, 85);
		$pdf -> Line(134, 248, 134, 85);
		$pdf -> Line(149, 248, 149, 85);
		$pdf -> Line(161, 248, 161, 85);
		$pdf -> Line(173, 248, 173, 85);
		$pdf -> Line(188, 292, 188, 85);
		$pdf -> Line(201, 240, 201, 85);
		$pdf -> Line(214, 248, 214, 85);
		$pdf -> Line(227, 240, 227, 85);
	}
}
$pdf->SetFont('Arial','B',10);
$pdf->Cell(10 ,$height,'',0,1);

//$subtotal = '151000.00';
$pdf->Cell(203 ,8,'S u b t o t a l ',0,0,'R');
$pdf->Cell(34 ,8,number_format($total_list_value,2, '.', ''),0,0,'R');

$pdf->SetFont('Arial','',9);
$totalgstamt = $total_sgstamt + $total_cgstamt + $total_igstamt;
$pdf->Cell(-180 ,0,'',0,1);
$pdf->Cell(17 ,8,'Total GST :',0,0,'L');
$pdf->Cell(33 ,8,$totalgstamt,0,0,'L'); //VENKAT

$pdf->Cell(35 ,22,'GST%',0,0,'R');
$pdf->Cell(22 ,22,'ON AMT',0,0,'R');
$pdf->Cell(22 ,22,'%     SGST',0,0,'R');
$pdf->Cell(22 ,22,'%     CGST',0,0,'R');
$pdf->Cell(25 ,22,'%     IGST',0,0,'R');
$pdf -> Line(95, 248, 95, 284);
$pdf -> Line(117, 248, 117, 284);
$pdf -> Line(140, 248, 140, 284);
$pdf -> Line(163, 248, 163, 284);
$pdf -> Line(83, 248, 83, 284);



   



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

$height = 31;
//------------------------------------------------------------------------------
$query=mysqli_query($connect,"Select sgst_per,cgst_per,igst_per,sum(sales_val) as totalsalesval,sum(sgst_amt) as totalsgstamt,sum(cgst_amt) as totalcgstamt,sum(igst_amt) as totaligstamt from invprt_mst where inv_number = '".$inv_number."' group by sgst_per,cgst_per,igst_per order by sgst_per");
while($row=mysqli_fetch_array($query)){
	$sgst_per = $row["sgst_per"];
	$totalsalesval = $row["totalsalesval"];
	$totalsgstamt = $row["totalsgstamt"];
	$totalcgstamt = $row["totalcgstamt"];
	$totaligstamt = $row["totaligstamt"];
	$cgst_per = $row["cgst_per"];
	$igst_per = $row["igst_per"];
	if($igst_per == '0'){
		$sgstper = $sgst_per * 2;	
		$pdf->Cell(-280 ,0,'',0,1);			
		$pdf->Cell(83 ,$height,$sgstper.'%',0,0,'R');
		$pdf->Cell(24.5 ,$height,number_format($totalsalesval,2, '.', ''),0,0,'R');
		$pdf->Cell(7 ,$height,number_format($sgst_per,2, '.', ''),0,0,'R');
		$pdf->Cell(16 ,$height,number_format($totalsgstamt,2, '.', ''),0,0,'R');
		$pdf->Cell(7 ,$height,number_format($cgst_per,2, '.', ''),0,0,'R');
		$pdf->Cell(16 ,$height,number_format($totalcgstamt,2, '.', ''),0,0,'R');
		$pdf->Cell(7 ,$height,number_format($igst_per,2, '.', ''),0,0,'R');
		$pdf->Cell(16 ,$height,number_format($totaligstamt,2, '.', ''),0,0,'R');
		$height = $height+10;
	}
	if($sgst_per == '0'){
		$igst_per = $igst_per;
		$sgstper = $sgst_per;
		$pdf->Cell(-280 ,0,'',0,1);			
		$pdf->Cell(83 ,$height,$igst_per.'%',0,0,'R');
		$pdf->Cell(24.5 ,$height,number_format($totalsalesval,2, '.', ''),0,0,'R');
		$pdf->Cell(7 ,$height,number_format($sgst_per,2, '.', ''),0,0,'R');
		$pdf->Cell(16 ,$height,number_format($totalsgstamt,2, '.', ''),0,0,'R');
		$pdf->Cell(7 ,$height,number_format($cgst_per,2, '.', ''),0,0,'R');
		$pdf->Cell(16 ,$height,number_format($totalcgstamt,2, '.', ''),0,0,'R');
		$pdf->Cell(8 ,$height,number_format($igst_per,2, '.', ''),0,0,'R');
		$pdf->Cell(16 ,$height,number_format($totaligstamt,2, '.', ''),0,0,'R');
		$height = $height+10;
	}
}
//------------------------------------------------------------------------------

$pdf->Cell(-180 ,0,'',0,1);
$pdf -> Line(83, 253, 188, 253);
$pdf -> Line(83, 258, 188, 258);
$pdf -> Line(83, 263, 188, 263);
$pdf -> Line(83, 268, 188, 268);
$pdf -> Line(83, 273, 188, 273);
$pdf -> Line(83, 278, 188, 278);
$pdf->SetFont('Arial','',9);
$pdf->Cell(124.5 ,8,$total_qty,0,0,'R');

$pdf->Cell(54,8,number_format($total_sales_val,2, '.', ''),0,0,'R');

$pdf->SetFont('Arial','B',10);
$pdf->Cell(-180 ,0,'',0,1);
$pdf->Cell(10 ,8,'',0,1);
$pdf->Cell(204 ,6,'DISCOUNT',0,0,'R');
$pdf->Cell(33 ,6,number_format($total_disc_amt,2, '.', ''),1,1,'R');

$pdf->Cell(204 ,6,'S-GST',0,0,'R');
$pdf->Cell(33 ,6,number_format($total_sgstamt,2, '.', ''),1,1,'R');

$pdf->Cell(204 ,6,'C-GST',0,0,'R');
$pdf->Cell(33 ,6,number_format($total_cgstamt,2, '.', ''),1,1,'R');

$pdf->Cell(204 ,6,'I-GST',0,0,'R');
$pdf->Cell(33 ,6,number_format($total_igstamt,2, '.', ''),1,1,'R');

$pdf->Cell(204 ,6,'TOTAL',0,0,'R'); // BEFORE ROUNDOFF
$pdf->Cell(33 ,6,number_format($total_val,2, '.', ''),1,1,'R');

$pdf->Cell(204 ,6,'ROUND OFF',0,0,'R');
$pdf->Cell(33 ,6,number_format($roundoff,2, '.', ''),1,1,'R');

$pdf->Cell(17 ,8,$rsinwords,0,0,'L');

$pdf->Cell(187 ,8,'T O T A L',0,0,'R');
$pdf->Cell(33 ,8,number_format($total_inv_val,2, '.', ''),1,1,'R');

$pdf->SetFont('Arial','BU',8);
$pdf->Cell(12,8,'Terms & Conditions:               E. & O.E.',0,0,'l');

$pdf->SetFont('Arial','B',9);
$pdf->Cell(223 ,8,'FOR, '.$company_name,0,0,'R');

$pdf->SetFont('Arial','',9);
$pdf->Cell(-20 ,0,'',0,1);
$pdf->Cell(10 ,18,'1. Goods Once sold will not be taken back or exchange.',0,0,'L');
$pdf->Cell(-200 ,0,'',0,1);
$pdf->Cell(10 ,26,'2. Interest @18 % per annum if payment not recived till due date.',0,0,'L');
$pdf->Cell(-200 ,0,'',0,1);
$pdf->Cell(10 ,33,'3. Cheque return charges will be borned by customer @ Rs200.00.',0,0,'L');
$pdf->Cell(-200 ,0,'',0,1);
$pdf->Cell(10 ,40,'4. Subject to '.$company_place.' Jurisdiction.',0,0,'L');
//$pdf->Cell(10 ,40,'4. Subject to SURAT Jurisdiction.',0,0,'L');
$pdf->Cell(-200 ,10,'',0,1);
$pdf->Cell(120 ,37,'Receiver'.'s Sign________________',0,0,'R');

$pdf->Cell(-200 ,0,'',0,1);
$pdf->Cell(235 ,37,'Authorised Sign.',0,0,'R');

$pdf->SetFont('Arial','B',10);
$pdf->Cell(-20 ,-53,'',0,1);
$pdf->Cell(180 ,7,'HDFC ACC. NO: '.$company_bank_accno,0,0,'L');
$pdf->Cell(-200 ,0,'',0,1);
$pdf->Cell(50 ,16,'IFSC CODE : '.$company_ifsc_code,0,0,'L');
$pdf->Cell(-200 ,0,'',0,1);
$pdf->Cell(50 ,25,'BRANCH : '.$company_bank_address,0,0,'L');
$pdf->Cell(-200 ,0,'',0,1);
$pdf->Cell(150 ,35,'Payment '.$due_days.' Days From Date of Invoice',0,0,'L');

//$this_directory = dirname(__FILE__);
//$path=$this_directory ."/Report/Pinvoice.pdf";
//$pdf->Output($path, "D");
//$pdf->Output();
//$pdf->Output('D');
$this_directory = dirname(__FILE__);
$path=$this_directory ."/Report/Invoice.pdf";
$pdf->Output($path,'F');
//$pdf->Output("../filename.pdf",'D');
//$pdf->Output('D:\yourfilename.pdf','D');
$pdf->Output();
?>