<?php
ob_start();
session_start();
include "config/config.php";
include "config/session.php";



$current_date_time = date("d/m/Y h:i:s A");


$ids = $_GET['id'];
$statement = $pdo->prepare("SELECT s.*, c.customer_name, c.mobile_no, c.address,
c.gst_no, c.pan_no, c.state as customer_state,
su.name as created_sales_man, su.emp_code as created_sales_man_code,
(SELECT count(id) FROM tbl_sale_items WHERE tbl_sale_items.sale_id=s.id) as product_count,
(SELECT COALESCE(SUM(cross_weight),0) FROM tbl_sale_items WHERE tbl_sale_items.sale_id=s.id) as total_cross_weight,
(SELECT COALESCE(SUM(net_weight),0) FROM tbl_sale_items WHERE tbl_sale_items.sale_id=s.id) as total_net_weight,
loc.business_name, loc.business_logo
FROM `tbl_sales` s 
LEFT JOIN tbl_customers c ON c.id=s.customer_id 
LEFT JOIN tbl_users su ON su.id=s.created_user_id
LEFT JOIN tbl_business_branch loc ON loc.id=s.location_id
WHERE s.id=?");
$statement->execute(array($ids));
$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

if($session_id==""){ 
    echo "<center>Access Declined</center>";
} else {
     
     
     
if($totalData>0){
    
    $business_name = $datas[0]['business_name'];
    $business_logo = $datas[0]['business_logo'];

    $customer_name = $datas[0]['customer_name'];
    $customer_address = $datas[0]['address'];
    $customer_mobile = $datas[0]['mobile_no'];
    $customer_gst = $datas[0]['gst_no'];
    $customer_state = $datas[0]['customer_state'];
    $customer_pan_no = $datas[0]['pan_no'];
    
    
    $sales_man_name = $datas[0]['created_sales_man'];
    $sales_man_code = $datas[0]['created_sales_man_code'];
    
    
    $invoice_no = $datas[0]['invoice_no'];
    $invoice_date = date('d-m-Y', strtotime($datas[0]['date']));
    $created_date_time = date('d-m-Y h:i:s A', strtotime($datas[0]['created_at']));
    
    $grand_total = $datas[0]['grand_total'];
    $total_amount = $datas[0]['total_amount'];
    $gst_amount = $datas[0]['gst_amount'];
    $cgst_sgst_amount = $gst_amount/2;
    
    
    $gst_percentage = $datas[0]['gst_percentage'];
    $cgst_sgst_percentage = $gst_percentage/2;
    
    $round_off = $grand_total - floor($grand_total);
    
    $roundedValue = round($grand_total);
    
    
    
    $product_count = $datas[0]['product_count'];
    $total_cross_weight = $datas[0]['total_cross_weight'];
    $total_net_weight = $datas[0]['total_net_weight'];
    
    
    
    
    
$statement_items = $pdo->prepare("SELECT e.*,
    tbl_products_name.name as product_name, p.hsn_code, p.quantity, 
    tbl_ids_purity.short_name
    FROM tbl_sale_items e
    LEFT JOIN tbl_product_variants p ON p.id=e.product_id
    LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=p.purity
    LEFT JOIN tbl_products_name ON tbl_products_name.id=p.product_name
    WHERE e.sale_id=?");
$statement_items->execute(array($ids));
$items_datas = $statement_items->fetchAll(PDO::FETCH_ASSOC);
$items_totalData = $statement_items->rowCount();


$statement_payments = $pdo->prepare("SELECT p.*,tbl_payment_type.name as payment_type_name
                                    FROM `tbl_payments` p 
                                    LEFT JOIN tbl_payment_type ON tbl_payment_type.id=p.payment_type
                                    WHERE p.sale_id=?");
$statement_payments->execute(array($ids));
$payment_datas = $statement_payments->fetchAll(PDO::FETCH_ASSOC);
$payment_totalData = $statement_payments->rowCount();

   $number = $grand_total;
   $no = floor($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);
  $points = ($point) ?
    "." . $words[$point / 10] . " " . 
          $words[$point = $point % 10] : '';
  $amount_in_words =  $result . "Rupees Only";
  
    $statement_price = $pdo->prepare("SELECT tbl_prices.* FROM `tbl_prices` WHERE tbl_prices.product_id=?");
    $statement_price->execute(array(1));
    $price_data = $statement_price->fetchAll(PDO::FETCH_ASSOC);


?>

<style>
	#bottom-design{
		width: 100%;
        background:#581845;
        height: 7px;width: 100%;
        background:#581845;
        height: 7px;
	}
	.titles{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.values{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
	.colon{
	    font-size: 10px;
	    font-weight: 600;
	    color: black;
	    vertical-align: top;
	}
</style>

<table style="width: 100%;">
    <tbody>
        <tr>
            <td style="width: 30%; vertical-align: top;"></td>
            <td style="width: 35%;text-align: center;vertical-align: top;">
                <img src="logo/spm-logos.png" style="text-align:center; width: 50%;object-fit:contain">
            </td>
            <td style="width: 35%;text-align: right;vertical-align: top;"> </td>

        </tr>
    </tbody>
</table>
<p style="text-align:left;"><strong>Original For Recipient</strong></p>
<table style="width:100%;border-collapse: collapse;">
	<tr style="border-top: 1px solid black;border-left: 1px solid black;border-right: 1px solid black;border-collapse: collapse;">
		<td style="font-weight:1000;border: 1px solid black;padding:8px">
			<center><b>PRO-FORMA INVOICE</b></center>
		</td>
	</tr>
</table>

<table style="width: 100%;border-collapse: collapse;">
	<tr style="border-bottom: 1px solid black;border-bottom: 1px solid black;border-collapse: collapse;">
        <td style="width: 30%;border-left: 1px solid black"></td>
		<td style="width: 60%;border-bottom: 1px solid black;font-size: 11px;border-collapse: collapse;vertical-align:top;">
			<table style="width: 100%;">
                <tr>
					<td style="width: 35%;" class="titles">Transaction Id</td>
					<td style="width: 1%;vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$invoice_no?></td>
				</tr>
                <tr>
					<td style="width: 35%;" class="titles">Transaction Date</td>
					<td style="width: 1%;vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$invoice_date?></td>
				</tr>
				<tr>
					<td style="width: 35%;" class="titles">Customer Name</td>
					<td style="width: 1%;vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$customer_name?></td>
				</tr>
				<tr>
					<td style="" class="titles">Address</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$customer_address?></td>
				</tr>
				<tr>
					<td style="" class="titles">GSTIN</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$customer_gst?></td>
				</tr>
				<tr>
					<td style="" class="titles">Mobile</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$customer_mobile?></td>
				</tr>
				<?php if(!empty($customer_pan_no)){ ?>
				<tr>
					<td style="" class="titles">PAN</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$customer_pan_no?></td>
				</tr>
                <?php } ?>
				<tr>
					<td style="" class="titles">Gold Rate</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=number_format($price_data[0]['amount'], 2)?></td>
				</tr>
                <tr>
					<td style="" class="titles">Platinum Rate</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"></td>
				</tr>
                <tr>
					<td style="" class="titles">ST</td>
					<td style="vertical-align: top;" class="colon">:</td>
					<td style="" class="values"><?=$sales_man_name?> (<?=$sales_man_code?>)</td>
				</tr>
				
			</table>
		</td>
        <td style="width: 20%;border-right: 1px solid black"></td>
	</tr>
</table>


<table style="width: 100%;border-collapse: collapse;">
	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
		<td style="width: 10%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Description</b></td>
		<td style="width: 4%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Qty(Nos)</b></td>
		<td style="width: 4%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>G.Wt</b></td>
		<td style="width: 4%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Stone Wt</b></td>
		<td style="width: 4%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>N.Wt</b></td>
		<td style="width: 6%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Rate/MRP</b></td>
		<td style="width: 6%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>VA Rate</b></td>
		<td style="width: 6%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>VA.Type</b></td>
		<td style="width: 9%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Total Amount</b></td>
		<td style="width: 10%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Rate After Disc</b></td>
		<td style="width: 6%; border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><b>Amount</b></td>
    </tr>
    
	<?php  if($items_totalData>0){
	    
	        $total_qty = $total_cross = $total_stone = $total_net = $total_product_price = 0;
            
            foreach($items_datas as $items){
			
                $product_name = $items['product_name'];
                $quantity = $items['quantity'];
                $cross_weight = $items['cross_weight'];
                $stone_weight = $items['stone_weight'];
                $net_weight = $items['net_weight'];
                $per_gram_amount = $items['per_gram_amount'];
                $wastage = $items['wastage'];
                $wastage_amount = $items['wastage_amount'];
                $product_total= $items['total'];
                $wastage_value = $items['wastage_value'];
                
                $total_qty += $quantity;
                $total_cross += $cross_weight;
                $total_stone += $stone_weight;
                $total_net += $net_weight;
                $total_product_price += $product_total;
                

	?>

	    <tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
		
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$product_name?></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$quantity?></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$cross_weight?></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$stone_weight?></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$net_weight?></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$per_gram_amount?></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$wastage_value?></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;">Percent</td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: right;"><?=number_format($wastage_amount,2)?><br><?=number_format($product_total,2)?></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: right;"></td>
    			<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: right;">0.00<br><?=number_format($product_total,2)?></td>
    
    	</tr>


	
       
        
    <?php } ?>
    
        <tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
    		<td colspan="1" style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold;text-align: right;font-weight: 1000;"><b>Total</b></td>
            <td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px; text-align: center;"><?=$total_qty?></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><?=$total_cross?></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><?=$total_stone?></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: center;"><?=$total_net?></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold;"></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold;"></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold;"></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: right;"><b><?=number_format($total_product_price,2)?></b></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold;"></td>
    		<td style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 11px;font-weight:bold; text-align: right;"><b><?=number_format($total_product_price,2)?></b></td>
            
    	</tr>
    
    
    <? }else{  } ?>
	

	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
		<td colspan="10" style="border-bottom: 1px solid black;border-left: 1px solid black;width: 4%;font-size: 11px;text-align: left;font-weight: 1000;font-weight:bold; vertical-align: top;">
		    <b>Payment Details</b>
		    <br>
		    <?php if($payment_totalData>0){
		        foreach($payment_datas as $payments){
		            $payment_type = $payments['payment_type_name'];
		            $payment_amount = $payments['amount'];
		            echo "$payment_type &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp : &nbsp ".number_format($payment_amount, 2)."";
		        }
		    }else{ 
		        echo "<b>No Payments Found</b>";
		    } ?>
		    
		</td>

        <td colspan="2" style="border-bottom: 1px solid black;border-left: 1px solid black;width: 4%;font-size: 11px;text-align: left;font-weight:bold;">
		    <b>Taxable Value</b><br>
		    CGST - <?=$cgst_sgst_percentage?>%<br>
		    SGST - <?=$cgst_sgst_percentage?>%<br>
		    <b>Invoice Value</b><br>
		    Round Off
		</td>
		<td colspan="2" style="border-bottom: 1px solid black;border-left: 1px solid black;width: 6%;font-size: 11px;text-align: right;font-weight:bold;">
		    <b><?=number_format($total_amount, 2)?></b><br>
		    <?=number_format($cgst_sgst_amount, 2)?><br>
		    <?=number_format($cgst_sgst_amount, 2)?><br>
		    <b><?=number_format($grand_total, 2)?></b><br>
		    <?=number_format($round_off, 2)?>
		</td>
		
		
	</tr>
	
	
	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
		<td colspan="10" style="border-bottom: 1px solid black;border-left: 1px solid black;width: 4%;font-size: 11px;text-align: left;font-weight: bolder;"><b><?=$amount_in_words?></b></td>
		
		<td colspan="2"style="border-bottom: 1px solid black;border-left: 1px solid black;width: 4%;font-size: 11px;text-align: right;font-weight: bolder;text-align:left">Net Value</td>
		<td colspan="2"style="border-bottom: 1px solid black;border-left: 1px solid black;width: 4%;font-size: 11px;text-align: right;font-weight: bolder;"><?=number_format($roundedValue, 2)?></td>
		

	</tr>
	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
		<td colspan="17" style="border-bottom: 1px solid black;border-left: 1px solid black;font-size: 12px;text-align: left;font-weight:bold;color: black;">
            <b>****Other Information ****</b><br>
            <p><b> Total Sales Amount  : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?=number_format($total_amount, 2)?> </b><br>
            <b> Total Sales Including GST  : <?=number_format($grand_total, 2)?> </b><br>
            <b> Total Sales Wt  : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?=$cross_weight?> </b><br>
            <b> Total Sales Net Wt  : &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?=$net_weight?> </b></p>
        </td>
	</tr>
	
	
	
	
	<tr style="border-bottom: 1px solid black;border-right: 1px solid black;border-left: 1px solid black;border-collapse: collapse;">
	    
		<td colspan="11" style="border-bottom: 1px solid black; font-size: 11px;text-align: left;font-weight:bold;">
		    </br>
		    <b>
		    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Customer Signature
		    </b>
		    <br><br><br>
		    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Generated by: <?=$session_name?>, Print date: <?=$current_date_time?>
		    </br>
		</td>
		
		<td colspan="6" style="border-bottom: 1px solid black; font-size: 11px;font-weight: bold;">
		    <b>FOR <?=$business_name?></b>
		    <br><br><br>
		    <b>Authorised Signatory</b>
		</td>
	</tr>
	
	
	
</table>



<?php } else { ?>

    <center>No Records Found</center>

<?php } ?>

<?php } ?>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" ></script>
<script> 
    window.print(); 
    setTimeout(function(){window.close();},10000);
</script>