<?php
// Include the main TCPDF library (search for installation path).
$this->mypdf_class->tcpdf();
 
global $inv_trans_pdf ;
global $inv_data_pdf ;

$inv_data_pdf = $inv_data[0];
$inv_trans_pdf = $inv_trans;

class MYPDF extends TCPDF 
{
    //Page header
    public function Header() 
    {
    	global $inv_data_pdf ;
    	global $inv_trans_pdf ;

    	$inv_no = isset($inv_data_pdf['inv_entry_no']) ? strtoupper($inv_data_pdf['inv_entry_no']) : "";
		$dispatch_by = isset($inv_data_pdf['inv_dispatch_by']) ? strtoupper($inv_data_pdf['inv_dispatch_by']) : "";
		$inv_terms = isset($inv_data_pdf['inv_terms']) ? strtoupper($inv_data_pdf['inv_terms']) : "";
		$inv_date = isset($inv_data_pdf['inv_invoice_date']) ? date('d-m-Y',strtotime($inv_data_pdf['inv_invoice_date'])) : "";
		$fin_year = isset($inv_data_pdf['inv_fin_year']) ? strtoupper($inv_data_pdf['inv_fin_year']) : "";
		$address = isset($inv_data_pdf['acc_billing_address']) ? strtoupper($inv_data_pdf['acc_billing_address']) : "";
		$pan_no = isset($inv_data_pdf['acc_pan']) ? $inv_data_pdf['acc_pan'] : "";
		$gst_no = isset($inv_data_pdf['acc_gst']) ? $inv_data_pdf['acc_gst'] : "";
		$state_name = isset($inv_data_pdf['state_name']) ? $inv_data_pdf['state_name'] : "";
		$state_code = isset($inv_data_pdf['state_code']) ? $inv_data_pdf['state_code'] : "";
		$city_name = isset($inv_data_pdf['city_name']) ? $inv_data_pdf['city_name'] : "";
		$acc_name = isset($inv_data_pdf['acc_name']) ? $inv_data_pdf['acc_name'] : "";
		
		$this->SetFont('Helvetica', 'c', 8);
		
		$tbl_header = "";
	
		$tbl_header .= <<<EOD
		<table cellpadding="3">
			<tr>
				<td width="100%" align="center" >
					<b style="font-size:11px;">TAX INVOICE</b>
				</td>
			</tr>
		</table>
		
		<table border="1" cellpadding="3" style="font-size:10px;">
			<tr>
				<td width="50%"  rowspan="4" height="90px">				
					<table cellpadding="2">
						<tr><td><b>$acc_name</b></td></tr>
						<tr><td width="100%"><b>$address</b></td></tr>
						<tr><td width="100%">STATE : <b>$state_name</b> &nbsp;&nbsp;&nbsp;&nbsp; STATE CODE: <b>$state_code</b></td></tr>
					</table>
				</td>
				<td width="25%">INVOICE NO.: <b>$inv_no / $fin_year</b></td>
        		<td width="25%">DATE: <b>$inv_date</b></td>			
			</tr>
			<tr>
        		<td colspan="2">PLACE OF SUPPLY: <b>$city_name - $state_name</b></td>
    		</tr>
		    <tr>
		       <td>DESPATCH BY: <b>$dispatch_by</b></td>
		       <td>TERMS: <b>$inv_terms</b></td>
		    </tr>
		     <tr>
		       <td>PAN NO: <b>$pan_no</b></td>
		       <td>GST NO : <b>$gst_no</b></td>
		    </tr>
		</table>
		
EOD;

			$sr_width 			= '7%';		
			$item_width 		= '30%';
			$hsn_width	 		= '15%';
			$qty_width	 		= '15%';
			$rate_width 		= '15%';
			$amt_width	 		= '18%';

			// $content_height 	=  '100';
			// $line_height 		=  '131';
			$content_height 	=  '60';
			$line_height 		=  '165';

			$sr_line_up 		= $sr_line_down			= '20';			
			$item_line_up 		= $item_line_down 		= '79.3';			
			$hsn_line_up 		= $hsn_line_down 		= '109';			
			$qty_line_up 		= $qty_line_down 		= '138.6';			
			$rate_line_up 		= $rate_line_down 		= '168.3';						
			$first_line_up 		= $first_line_down 		= '6';						
			$last_line_up 		= $last_line_down 		= '204';						

		$tbl_header .= <<<EOD
		<table cellpadding="6" border="1" style="font-size:9;">
			<tr>
				<td width="$sr_width" align="center"><b>SR NO</b></td>
				<td width="$item_width" align="center"><b>DESCRIPTION</b></td>
				<td width="$hsn_width" align="center"><b>HSN CODE</b></td>
				<td width="$qty_width" align="center"><b>CARATS</b></td>	
				<td width="$rate_width" align="center"><b>RATE</b></td>
				<td width="$amt_width" align="center"><b>AMOUNT</b></td>

EOD;
				$tbl_header .= <<<EOD
			</tr>
		</table>
EOD;

		// $this->writeHTML($tbl_header, true, false, false, false, '');
		$this->writeHTMLCell(198, 193, 6, $content_height, $tbl_header, 0, 0, 0, true, 'P', true);
		$yy = $this->GetY();
		$yy = $yy - - 38;
		
		// $this->line(5,$yy,5,136);
        $this->line($sr_line_up,$yy,$sr_line_down,$line_height);
        $this->line($item_line_up,$yy,$item_line_down,$line_height);
        $this->line($hsn_line_up,$yy,$hsn_line_down,$line_height);
        $this->line($qty_line_up,$yy,$qty_line_down,$line_height);
        $this->line($rate_line_up,$yy,$rate_line_down,$line_height);

        $this->line($first_line_up,$yy,$first_line_down,$line_height);
        $this->line($last_line_up,$yy,$last_line_down,$line_height);
        $this->SetTopMargin($yy +1);
    }

    // Page footer
    public function Footer() 
    {
    	$this->SetFont('Helvetica', 'c', 8);
    	global $inv_trans_pdf;
    	global $inv_data_pdf;
    	global $cnt2 ;
    	$footer_margin_bottom = '220';
    	$page_of_page_margin_bottom = '247';    	
		   
		$inv_addon = isset($inv_data_pdf['inv_addon']) ? $inv_data_pdf['inv_addon'] : "";
		$total_qty=isset($inv_data_pdf['inv_total_qty']) ? $inv_data_pdf['inv_total_qty'] : "";
		$subtotal_amt = isset($inv_data_pdf['inv_sub_total_amt']) ? number_format(round($inv_data_pdf['inv_sub_total_amt'])) : "";
		$total_sgst = isset($inv_data_pdf['inv_total_sgst']) ? round($inv_data_pdf['inv_total_sgst']) : "";
		$total_cgst = isset($inv_data_pdf['inv_total_cgst']) ? round($inv_data_pdf['inv_total_cgst']) : "";
		$total_igst = isset($inv_data_pdf['inv_total_igst']) ? round($inv_data_pdf['inv_total_igst']) : "";
		
		$subtotal_amt1 = isset($inv_data_pdf['inv_sub_total_amt']) ? round($inv_data_pdf['inv_sub_total_amt']) : "";
		$total_final = $total_sgst + $total_cgst + $total_igst + $subtotal_amt1;
		
        $total_final = number_format($total_final);
        
        $inv_date = isset($inv_data_pdf['inv_invoice_date']) ? date('d-m-Y',strtotime($inv_data_pdf['inv_invoice_date'])) : "";
        $today = strtotime('2022-07-18');
        $gst_date = strtotime($inv_date);
        
        if ($gst_date > $today)
        {
            $cgst = '0.75%'; 
            $sgst = '0.75%'; 
            $igst = '1.50%'; 
        }else
        {
            $cgst =  '0.125%'; 
            $sgst =  '0.125%'; 
            $igst = '0.25%'; 
        }
        $total_final1 = $total_sgst + $total_cgst + $total_igst + $subtotal_amt1;
		$word = strtoupper(number_to_word($total_final1));
		
		$rate_cgst = 0; $rate_sgst = 0;$rate_igst = 0;
		if(!empty($inv_trans_pdf)):
			foreach ($inv_trans_pdf as $key => $value):
				$rate_cgst = $rate_cgst + $value['invt_per_cgst'];
				$rate_sgst = $rate_sgst + $value['invt_per_sgst'];
				$rate_igst = $rate_igst + $value['invt_per_igst'];
			endforeach;
		endif;

    	$tbl_footer = "";    	
    	$tbl_footer .= <<<EOD

    	<table  border="1" cellpadding="" style="font-size:11px;">
		  	<tr>
		  		<td colspan="2" width="100%">
					<table  border="1" cellpadding="3" style="font-size:11px;">			
				  	<tr>
				  		<td width="37%" style="text-align:right"><b>Total</b></td>
				  		<td width="15%"></td>
						<td width="15%" style="text-align:center"><b>$total_qty</b></td>
						<td width="15%"></td>
						<td width="18%" style="text-align:center"><b>$subtotal_amt&nbsp;&nbsp;&nbsp;</b></td>
				  	</tr>
				  	</table>
				</td>
			</tr>
			<tr>
				<td colspan="2" width="100%">
					<table  cellpadding="0" style="font-size:11px;">			
				  	<tr>
				    	<td width="60%">
						    <table cellpadding="3">
						    	<tr>
									<td style="font-size:11px;"><b>$word</b></td>
								</tr><br/><br/>
									
								<tr>
									<td style="font-size:10px;"><b>TERMS AND CONDITIONS:-</b></td>
								</tr>
								<tr>
									<td>1. Subject to Mumbai Jurisdiction.<br/>2. Goods once sold shall not be taken back or exchanged.<br/>3. All figures are rounded off to the nearest rupee.<br/> 4. E. & O.E.</td>
								</tr>
							</table>
				    	</td>				    	
						<td width="40%">
							<table border="0" cellpadding="2" style="font-size:10px;">
								<tr>
									<td width="55%" style="text-align:right"><b>SGST $sgst</b></td>
									<td width="45%" style="border-left:1.5px solid #000;text-align:center;"><b>$total_sgst &nbsp;&nbsp;&nbsp;</b></td>
								</tr>
								<tr>
									<td width="55%" style="text-align:right"><b>CGST $cgst</b></td>
									<td width="45%" style="border-left:1.5px solid #000;text-align:center;"><b>$total_cgst&nbsp;&nbsp;&nbsp;</b></td>
								</tr>
								<tr>
									<td width="55%" style="text-align:right"><b>IGST $igst</b></td>
									<td width="45%" style="border-left:1.5px solid #000;text-align:center;"><b>$total_igst&nbsp;&nbsp;&nbsp;</b></td>
								</tr>
								
								<tr>
									<td width="55%" style="text-align:right;font-size:11px;"><b>TOTAL BILL AMOUNT</b></td>
									<td width="45%" style="border:1.5px solid #000;text-align:center"><b>$total_final&nbsp;&nbsp;&nbsp;</b></td>
								</tr>
							</table>
						</td>
				  	</tr>
				  	<tr>
				  		<td style="font-size:11px;">&nbsp;&nbsp;<b>$inv_addon</b></td>					
				  	</tr>					
				</table><br/><br/>
				</td>
			</tr>
			
			<tr>
				<td width="100%">
					<table cellpadding="2">
						<tr>
							<td width="50%"><b>GST NO:-xxxxxxx.</b></td>
							<td>BANK DETAILS:- <b>TEST PVT LTD</b></td>
						</tr>
						<tr>
							<td  width="50%"><b>STATE CODE: MH-27.</b></td>
							<td  width="50%">BANK NAME:<b> AXIS BANK, BDB, BANDRA(E) MUMBAI- 400051.</b></td>
						</tr>
						<tr>
							<td  width="50%"><b>PAN NO:-xxxx</b></td>
							<td  width="50%">ACCOUNT NO: <b>xxxxxx</b></td>
						</tr>
						<tr>
							<td  width="50%" style="font-size:10px">Note: We dont accept cheques. Payments should be made via NEFT/RTGS</td>
							<td  width="50%">IFSC CODE: <b>xxxxxxx</b></td>
						</tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table cellpadding="2">
						<tr>
							<td colspan="2">
								<table cellpadding="2">
									<tr><td>The diamonds herein invoiced has been purchased from legitimate sourced not involved in funding conflict and in compliance with U.N. Resolutions. The Seller hereby gaurantees that these diamonds are conflcit free based on personal knowlegde and/or written guarantees provided by the suppliers of these diamonds.</td></tr>
									<tr><td>We hereby certify that our registration certificate under the GST Act,2017 is in force on the date on which the sale of goods specified in this tax invoiceis made by us and that the transaction of sale covered by this tax invoice has been effected by us and it shall be accounted for in the turnover of sales while filing of return and the due tax, if any, payable on the salehas been paid or shall be paid.</td></tr>
								
								</table>
							</td>
						</tr>
						<tr><td colspan="2" style="text-align:right;"><b style="font-size:13px;font-weight:700">FOR TEST LTD.</b><br/><br/></td></tr>
						<tr>
							<td><b>BUYER SIGNATURE</b></td>
							<td style="text-align:right"><b>DIRECTOR</b></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
    	
		
EOD;

		// $this->writeHTML($tbl_footer, true, false, false, false, '');
		$this->writeHTMLCell(198, 160, 6, 165, $tbl_footer, 0, 0, 0, true, 'P', true);
        
  
        // Set font
        // $this->SetFont('helvetica', 'c', 8);
        // Page number
        // $this->Cell(0, $page_of_page_margin_bottom, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

// create new PDF document
// $pdf = new TCPDF('P', PDF_UNIT, array('160','200'), true, 'UTF-8', false);
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Imran Khan');
$pdf->SetTitle('Invoice Pdf');
$pdf->SetSubject('Invoice Pdf');
// $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(5, PDF_MARGIN_TOP, 5);
$pdf->SetMargins(8, 10, 5);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(84);

$pdf->SetAutoPageBreak(TRUE , 115);


// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) 
{
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

	$sr_width 			= '7%';				
	$item_width 		= '30%';
	$hsn_width	 		= '15%';
	$qty_width	 		= '15%';
	$rate_width 		= '15%';
	$amt_width	 		= '18%';


$pdf->SetFont('Helvetica', 'c', 8);
$row_cnt = count($inv_trans_pdf);

$tbl = "";
$pdf->AddPage();
	$tbl .= <<<EOD

			<table cellspacing="3" style="font-size:10px;">
EOD;
					for($i=0; $i<$row_cnt; $i++)
					{
						$sr_no = $i +1;
						$product_name = $inv_trans_pdf[$i]['pro_name'];
						$qty = $inv_trans_pdf[$i]['invt_qty'];
						$rate = number_format($inv_trans_pdf[$i]['invt_rate']);
						$sub_amt = number_format(round($inv_trans_pdf[$i]['invt_sub_total_amt']));
						$total_amt = $inv_trans_pdf[$i]['invt_total_amt'];
						$hsn_code = $inv_trans_pdf[$i]['pro_hsn_code'];

						$tbl .= <<<EOD
							<tr>
							<td width="$sr_width" align="center">$sr_no</td>
							<td width="$item_width" align="center"><b>$product_name</b></td>
							<td width="$hsn_width" align="center">$hsn_code </td>
							<td width="$qty_width" align="center">$qty</td>	
							<td width="$rate_width" align="center">$rate</td>
							<td width="$amt_width" align="center">$sub_amt</td>
							</tr>
EOD;
					}

			$tbl .= <<<EOD
					</table>
				
EOD;
// set auto page breaks
// $pdf->SetAutoPageBreak(TRUE, $set_auto_page_break);
$pdf->writeHTML($tbl, true, false, false, false, '');
// $pdf->IncludeJS("print();");
// ---------------------------------------------------------

//Close and output PDF document
ob_end_clean();
$pdf->Output('Invoice.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+