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

$sales_data_pdf = $sales_data[0];
$sales_trans_pdf = $sales_trans;

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

    	$sale_no = isset($sales_data_pdf['sale_entry_no']) ? strtoupper($sales_data_pdf['sale_entry_no']) : "";

		$sale_date = (!empty($sales_data_pdf['sale_entry_date']) && $sales_data_pdf['sale_entry_date'] != '1970-01-01') ? date('d-m-Y', strtotime($sales_data_pdf['sale_entry_date'])) : "";

		$sale_bill_date = (!empty($sales_data_pdf['sale_bill_date']) && $sales_data_pdf['sale_bill_date'] != '1970-01-01') ? date('d-m-Y', strtotime($sales_data_pdf['sale_bill_date'])) : "";

		$sale_buyer_order_date = (!empty($sales_data_pdf['sale_buyer_order_date']) && $sales_data_pdf['sale_buyer_order_date'] != '1970-01-01') ? date('d-m-Y', strtotime($sales_data_pdf['sale_buyer_order_date'])) : "";

		$sale_buyer_order_no = isset($sales_data_pdf['sale_buyer_order_no']) && $sales_data_pdf['sale_buyer_order_no'] > 0 ? $sales_data_pdf['sale_buyer_order_no'] : "";

		$sale_delivery_note = isset($sales_data_pdf['sale_delivery_note']) ? strtoupper($sales_data_pdf['sale_delivery_note']) : "";

		$sale_dispatch_doc_no = isset($sales_data_pdf['sale_dispatch_doc_no']) ? strtoupper($sales_data_pdf['sale_dispatch_doc_no']) : "";

		$sale_dispatched_through = isset($sales_data_pdf['sale_dispatched_through']) ? strtoupper($sales_data_pdf['sale_dispatched_through']) : "";

		$sale_destination = isset($sales_data_pdf['sale_destination']) ? strtoupper($sales_data_pdf['sale_destination']) : "";

		$sale_terms_of_delivery = isset($sales_data_pdf['sale_terms_of_delivery']) ? strtoupper($sales_data_pdf['sale_terms_of_delivery']) : "";

		$sale_delivery_note_date = (!empty($sales_data_pdf['sale_delivery_note_date']) && $sales_data_pdf['sale_delivery_note_date'] != '1970-01-01') ? date('d-m-Y', strtotime($sales_data_pdf['sale_delivery_note_date'])) : "";

		$fin_year = isset($sales_data_pdf['sale_fin_year']) ? strtoupper($sales_data_pdf['sale_fin_year']) : "";
		
		$address = isset($sales_data_pdf['acc_billing_address']) ? strtoupper($sales_data_pdf['acc_billing_address']) : "";

		$pan_no = isset($sales_data_pdf['acc_pan']) ? $sales_data_pdf['acc_pan'] : "";

		$gst_no = isset($sales_data_pdf['acc_gst']) ? $sales_data_pdf['acc_gst'] : "";

		$state_name = isset($sales_data_pdf['state_name']) ? $sales_data_pdf['state_name'] : "";

		$state_code = isset($sales_data_pdf['state_code']) ? $sales_data_pdf['state_code'] : "";

		$city_name = isset($sales_data_pdf['city_name']) ? $sales_data_pdf['city_name'] : "";

		$acc_name = isset($sales_data_pdf['acc_name']) ? $sales_data_pdf['acc_name'] : "";

		$acc_days = isset($sales_data_pdf['acc_days']) ? $sales_data_pdf['acc_days'] : "";

		$acc_discount = isset($sales_data_pdf['acc_discount']) ? $sales_data_pdf['acc_discount'] : "";

		$shipping_name = isset($sales_data_pdf['shipping_name']) ? $sales_data_pdf['shipping_name'] : "-";
		$shipping_mobile = isset($sales_data_pdf['shipping_mobile']) ? $sales_data_pdf['shipping_mobile'] : "-";
		$shipping_address = isset($sales_data_pdf['shipping_address']) ? $sales_data_pdf['shipping_address'] : "-";
		$shipping_gst = isset($sales_data_pdf['shipping_gst']) ? $sales_data_pdf['shipping_gst'] : "-";
		$shipping_pan = isset($sales_data_pdf['shipping_pan']) ? $sales_data_pdf['shipping_pan'] : "-";
		$shipping_state = isset($sales_data_pdf['shipping_state']) ? $sales_data_pdf['shipping_state'] : "-";
		$shipping_city = isset($sales_data_pdf['shipping_city']) ? $sales_data_pdf['shipping_city'] : "-";

		$acc_mobile = isset($sales_data_pdf['acc_mobile']) ? $sales_data_pdf['acc_mobile'] : "";
		$acc_address = isset($sales_data_pdf['acc_address']) ? $sales_data_pdf['acc_address'] : "";
		$acc_gst = isset($sales_data_pdf['acc_gst']) ? $sales_data_pdf['acc_gst'] : "";
		$acc_pan = isset($sales_data_pdf['acc_pan']) ? $sales_data_pdf['acc_pan'] : "";
		$acc_state = isset($sales_data_pdf['acc_state']) ? $sales_data_pdf['acc_state'] : "";
		$acc_city = isset($sales_data_pdf['acc_city']) ? $sales_data_pdf['acc_city'] : "";
		
		$this->SetFont('Helvetica', 'c', 8);
		
		$tbl_header = "";
	
		$tbl_header .= <<<EOD

<table border="0" cellpadding="3" style="font-size:10px;">
	<tr>
		<td style="font-size:18px; text-align:center;"><b>Tax Invoice</b></td>
	</tr>
</table>	

<table border="1" cellpadding="3" style="font-size:10px;">
    <tr>
        <!-- LEFT SIDE (ROWSPAN = 4) -->
        <td width="50%" rowspan="4" height="90px">
            <table>
                <tr>
                    <td style="font-size:12px;"><b>Ruchita Gems</b></td>
                </tr>
                <tr>
                    <td> D/8/3 Bhadran Nagar 
						<br> Behind N L College 
						<br> S V Road 
						<br> Malad West 
						<br> Mumbai 400064 
						<br> GSTIN/UIN: 27AQDPS0123M1ZS 
						<br> State Name: Maharashtra, Code: 27 
						<br> E-Mail: ruchitagems0705@gmail.com
                    </td>
                </tr>
            </table>
        </td>

        <!-- MIDDLE -->
        <td width="25%">Invoice No.<br><b>$sale_no / $fin_year</b></td>

		<td width="25%">Dated<br><b>$sale_date</b></td>
    </tr>

    <tr>
        <td width="25%">Delivery Note <br><b>$sale_delivery_note</b></td>
        <td width="25%">Mode/Terms of Payment <br><b>DAYS: $acc_days &nbsp;&nbsp; DISCOUNT: $acc_discount</b></td>

    </tr>

    <tr>
        <td width="25%">Buyer’s Order No. <br><b>$sale_buyer_order_no</b></td>
        <td width="25%">Dated <br><b>$sale_buyer_order_date</b></td>
    </tr>

    <tr>
        <td width="25%">Dispatch Doc No. <br><b>$sale_dispatch_doc_no</b></td>
        <td width="25%">Delivery Note Date <br><b>$sale_delivery_note_date</b></td>
    </tr>
</table>





<table border="0" cellpadding="3" style="font-size:10px;">
	<tr>
        <!-- LEFT SIDE (ROWSPAN = 4) -->
        <td width="50%" rowspan="4" height="90px" style="border-left:1px solid black; border-right:1px solid black;">
            <table>
				<tr>
                    <td>Consignee (Ship to)</td>
                </tr>
                <tr>
                    <td style="font-size:12px;"><b>$shipping_name</b></td>
                </tr>
                <tr>
                    <td> Mobile: $shipping_mobile 
						<br> $shipping_address 
						<br> GSTIN/UIN: $shipping_gst 
						<br> PAN/IT No.: $shipping_pan 
						<br> State: $shipping_state, City: $shipping_city
                    </td>
                </tr>
            </table>
        </td>

        <!-- MIDDLE -->
        <td width="25%" style="border-left:1px solid black; border-right:1px solid black; border-bottom:1px solid black;">Dispatched through<br><b>$sale_dispatched_through</b></td>

		<td width="25%" style="border-left:1px solid black; border-right:1px solid black; border-bottom:1px solid black;">Destination<br><b>$sale_destination</b></td>
    </tr>

    <tr>
        <td width="50%" style="border-right:1px solid black;">Terms of Delivery <br><b>&nbsp;$sale_terms_of_delivery</b></td>
    </tr>
	<tr>
        <td width="50%" style="border-right:1px solid black;">&nbsp;</td>
    </tr>
	<tr>
        <td width="50%" style="border-right:1px solid black;">&nbsp;</td>
    </tr>




	<tr>
        <!-- LEFT SIDE (ROWSPAN = 4) -->
        <td width="50%" rowspan="4" height="90px" style="border-left:1px solid black; border-right:1px solid black; border-top:1px solid black;">
            <table>
				<tr>
                    <td>Buyer (Bill to)</td>
                </tr>
                <tr>
                    <td style="font-size:12px;"><b>$acc_name</b></td>
                </tr>
                <tr>
                    <td> Mobile: $acc_mobile 
						<br> $acc_address
						<br> GSTIN/UIN: $acc_gst 
						<br> PAN/IT No.: $acc_pan  
						<br> State: $acc_state, City: $acc_city
                    </td>
                </tr>
            </table>
        </td>

        <!-- MIDDLE -->
        <td width="25%"></td>

		<td width="25%" style="border-right:1px solid black;"></td>
    </tr>
	
    <tr>
        <td width="25%"></td>
        <td width="25%" style="border-right:1px solid black;">&nbsp;</td>
    </tr>
	<tr>
        <td width="25%"></td>
        <td width="25%" style="border-right:1px solid black;">&nbsp;</td>
    </tr>
	<tr>
        <td width="25%"></td>
        <td width="25%" style="border-right:1px solid black;">&nbsp;</td>
    </tr>

</table>
EOD;


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

		$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/SAC</b></td>
				<td width="$qty_width" align="center"><b>Qty</b></td>	
				<td width="$rate_width" align="center"><b>Rate</b></td>
				<td width="$amt_width" align="center"><b>AMOUNT</b></td>
			</tr>
		</table>
EOD;

		// Start header at top-left
		$this->SetXY(5, 5);

		// Let TCPDF calculate real header height
		$this->writeHTML($tbl_header, true, false, false, false, '');

		// 📍 Capture Y position EXACTLY after header
		$table_top_y = $this->GetY() - 8;   // small gap under header
		$table_bottom_y = 235;              // footer start (fixed)

		// Draw vertical column lines for full fixed height
		// Left outer border
		$this->line(5, $table_top_y, 5, $table_bottom_y);

		// Column dividers
		$this->line(19, $table_top_y, 19, $table_bottom_y);   // Sr
		$this->line(79, $table_top_y, 79, $table_bottom_y);   // Description
		$this->line(109,  $table_top_y, 109,  $table_bottom_y);   // HSN
		$this->line(139,$table_top_y,139,$table_bottom_y);    // Qty
		$this->line(169,$table_top_y,169,$table_bottom_y);    // Rate
		$this->line(205,  $table_top_y,205,  $table_bottom_y);    // Amount / right border

		$this->SetY($table_top_y + 2);      // Start item rows here
		$this->SetTopMargin($table_top_y + 2);
		$this->setPageMark();

    }

    // Page footer
    public function Footer()
	{
		// show footer ONLY on last page
		if ($this->getPage() != $this->getNumPages()) {
			return; // skip footer on all other pages
		}
		
    	$this->SetFont('Helvetica', 'c', 8);
    	global $sales_trans_pdf;
    	global $sales_data_pdf;
    	global $cnt2 ;
    	$footer_margin_bottom = '220';
    	$page_of_page_margin_bottom = '247';    	
		   
		$sale_addon = isset($sales_data_pdf['sale_addon']) ? $sales_data_pdf['sale_addon'] : "";
		$total_qty=isset($sales_data_pdf['sale_total_qty']) ? $sales_data_pdf['sale_total_qty'] : "";
		$subtotal_amt = isset($sales_data_pdf['sale_sub_total_amt']) ? number_format(round($sales_data_pdf['sale_sub_total_amt'])) : "";
		$total_sgst = isset($sales_data_pdf['sale_total_sgst']) ? round($sales_data_pdf['sale_total_sgst']) : "";
		$total_cgst = isset($sales_data_pdf['sale_total_cgst']) ? round($sales_data_pdf['sale_total_cgst']) : "";
		$total_igst = isset($sales_data_pdf['sale_total_igst']) ? round($sales_data_pdf['sale_total_igst']) : "";
		
		$subtotal_amt1 = isset($sales_data_pdf['sale_sub_total_amt']) ? round($sales_data_pdf['sale_sub_total_amt']) : "";
		$total_final = $total_sgst + $total_cgst + $total_igst + $subtotal_amt1;
		
        $total_final = number_format($total_final);
        
        $sale_date = isset($sales_data_pdf['sale_entry_date']) ? date('d-m-Y',strtotime($sales_data_pdf['sale_entry_date'])) : "";
        $today = strtotime('2022-07-18');
        $gst_date = strtotime($sale_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($sales_trans_pdf)):
			foreach ($sales_trans_pdf as $key => $value):
				$rate_cgst = $rate_cgst + $value['st_per_cgst'];
				$rate_sgst = $rate_sgst + $value['st_per_sgst'];
				$rate_igst = $rate_igst + $value['st_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="1">
						    	<tr>
									<td style="font-size:11px;">Amount Chargeable (in words) <br><b>$word</b></td>
								</tr><br/><br/>
							</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>$sale_addon</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="1">
						    	<tr>
									<td style="font-size:11px;"><b><u>Declaration</u></b></td>
								</tr>
								<tr>
									<td style="font-size:10px;">We declare that this invoice shows the actual price of the goods <br>described and that all particulars are true and correct.</td>
								</tr><br/><br/>
							</table>
				    	</td>				    	
						<td width="40%">
							 <table cellpadding="1">
						    	<tr>
									<td style="font-size:11px;"><b><u>Company's Bank Details</u></b>

									<br>
									Bank Name  
									  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  :  <b>Indusind Bank</b>

									<br>
									A/c No.   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :  <b>259869768852</b>

									<br>
									Branch & IFS Code    :  <b>BKC BRANCH & INDB0000342</b>
									</td>
								</tr>
							</table>
						</td>
				  	</tr>			
				</table>
				</td>
			</tr>

			<tr>
				<td colspan="2" width="100%">
					<table  cellpadding="0" style="font-size:11px;">			
				  	<tr>
				    	<td width="50%" style="border-right:1px solid black;">
						    <table cellpadding="1">
						    	<tr>
									<td style="font-size:11px;">Customer's Seal and Signature
									<br/><br/><br/>
									</td>
								</tr>
							</table>
				    	</td>				    	
						<td width="50%">
							 <table cellpadding="1">
						    	<tr>
									<td style="font-size:11px; text-align:right;">  
									<b>For Ruchita Gems</b>
									<br/><br/><br/>

									Authorised Signatory
									</td>
								</tr>
							</table>
						</td>
				  	</tr>			
				</table>
				</td>
			</tr>



			

			
		</table>
    	
		
EOD;

		// $this->writeHTML($tbl_footer, true, false, false, false, '');
		$this->writeHTMLCell(198, 200, 5, 235, $tbl_footer, 0, 0, 0, true, 'P', true);
        //  $this->writeHTML($tbl_footer, true, false, false, false, '');
  
        // 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, array('210','148'), true, 'UTF-8', false); 
$pdf = new MYPDF('P', PDF_UNIT, 'A4', true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Imran Khan');
$pdf->SetTitle('sales invoice Pdf');
$pdf->SetSubject('sales 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 , 67);


// 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 			= '6%';				
	$item_width 		= '29%';
	$hsn_width	 		= '15%';
	$qty_width	 		= '15%';
	$rate_width 		= '15%';
	$amt_width	 		= '18%';


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

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

			<table cellpadding="4" style="font-size:10px; line-height:12px;">
EOD;

			// echo "<pre>"; print_r($sales_trans_pdf); exit;

					for($i=0; $i<$row_cnt; $i++)
					{
						$sr_no = $i +1;
						$product_name = $sales_trans_pdf[$i]['st_dm_code'];
						$qty = $sales_trans_pdf[$i]['st_carat'];
						$rate = number_format($sales_trans_pdf[$i]['st_pc_amt']);
						$sub_amt = number_format(round($sales_trans_pdf[$i]['st_sub_total_amt']));
						$total_amt = $sales_trans_pdf[$i]['st_total_amt'];
						$hsn_code = $sales_trans_pdf[$i]['hsn_code'];
						$hsn_description = $sales_trans_pdf[$i]['hsn_description'];

						$tbl .= <<<EOD
							<tr>
								<td width="$sr_width" align="left">$sr_no</td>
								<td width="$item_width" align="center"><b>$hsn_description</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('sales Invoice.pdf', 'I');
//============================================================+
// END OF FILE
//============================================================+