<?php
ob_start();
session_start();
include "config/config.php";
$session_id = $_SESSION['spm_gold']['id'];

$statement_users = $pdo->prepare("SELECT * FROM  tbl_users WHERE tbl_users.id=?");
$statement_users->execute(array($session_id));
$user_datas = $statement_users->fetchAll(PDO::FETCH_ASSOC);
$session_user_name = $user_datas[0]['name'];
$current_date_time = date("d/m/Y h:i:s A");
$shop_name = "SPM GOLD & DIAMONDS";

$ids = $_GET['id'];
$statement = $pdo->prepare("SELECT tbl_purchases.*,
                (SELECT COALESCE(SUM(amount),0) FROM tbl_payments WHERE tbl_payments.purchase_id=tbl_purchases.id and accounts=2) as purchase_paid_amount,
                (SELECT COUNT(id) FROM tbl_purchases_items WHERE tbl_purchases_items.purchase_id=tbl_purchases.id) as product_total_items,
                tbl_suppliers.name as supplier_name,
                tbl_suppliers.mobile_no 
                FROM tbl_purchases 
                    LEFT JOIN tbl_suppliers ON tbl_purchases.supplier_id=tbl_suppliers.id 
                    where tbl_purchases.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){
    
    $supplier_name = $datas[0]['supplier_name'];
    $supplier_mobile_no = $datas[0]['mobile_no'];
    $product_total_items = $datas[0]['product_total_items'];
    
    
    $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']));
    
    $per_stone_cost = $datas[0]['per_gm_stone_amount'];
            
            
            $total_paid_amount = $datas[0]['purchase_paid_amount'];
            $product_amount = $datas[0]['total_amount'];
            $grand_total = $datas[0]['grand_total'];
            $paid_amount = $datas[0]['paid'];
            $gst_amount = $datas[0]['gst_amount'];
            $total_amount_after_tax = $datas[0]['total_amount_after_tax'];
            
            $current_balance = $grand_total-$total_paid_amount;
            
            if($datas[0]["gst_percentage"]!=0){
                if($datas[0]["tax_type"]==1){
    		 	    $tax_type = "Exclusive";
    		 	}else if($datas[0]["tax_type"]==2){
    		 	    $tax_type ="Inclusive";
    		 	}else{
    		 	    $tax_type="Null";
    		 	}
    		 	
    		 	if($datas[0]["gst_type"]==1){
    		 	   $gst_type="GST"; 
    		 	}else if($datas[0]["gst_type"]==2){
    		 	   $gst_type="IGST";  
    		 	}else{
    		 	    $gst_type="Null";
    		 	}
            }else{
                $tax_type="";
                $gst_type=""; 
            }
            
        $gst_percentage = $datas[0]['gst_percentage'];
    
    
    
    if($total_paid_amount==$grand_total){
        $nestedData ="<span class='btn btn-success btn-xs'>Payment Completed</span>";
    }else if($total_paid_amount!=0){
        $nestedData ="<span class='btn btn-warning btn-xs'>Payment Due</span>";
    }else if($total_paid_amount==0){
        $nestedData ="<span class='btn btn-danger btn-xs'>Payment Pending</span>";
    }else{
        $nestedData ="<span class='btn btn-danger btn-xs'>NULL</span>";
    }
    
    
    
    
    
$statement_items = $pdo->prepare("SELECT e.*,
tbl_products_name.name as product_name, p.hsn_code, p.quantity, 
tbl_ids_purity.short_name,
tbl_units.name as unit_name, tbl_units.short_name as unit_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_units ON tbl_units.id=p.unit
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();

?>
<?php
   $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";
?>


<table style="width: 100%;">
    <tr>
        <th>
            <center><?=$shop_name?></center>
        </th>
    </tr>
</table>

</br>

<table style="width: 100%;">
    <thead>
        <tr>
            <th style="text-align: left;">Supplier Name:</th>
            <th style="text-align: left;"><?=$supplier_name?></th>
            <th style="text-align: left;">Invoice Date:</th>
            <th style="text-align: right;"><?=$invoice_date?></th>
        </tr>
        <tr>
            <th style="text-align: left;">Supplier Mobile No:</th>
            <th style="text-align: left;"><?=$supplier_mobile_no?></th>
            <th style="text-align: left;">Invoice No:</th>
            <th style="text-align: right;"><?=$invoice_no?></th>
        </tr>
        
        <tr>
            <th style="text-align: left;">Product Items</th>
            <th style="text-align: left;"><?=$product_total_items?></th>
            <th style="text-align: left;">Product Amount</th>
            <th style="text-align: right;"><?=number_format($product_amount, 2)?></th>
        </tr>
        <tr>
            <th style="text-align: left;">Payment Status</th>
            <th style="text-align: left;"><?=$nestedData?></th>
            <th style="text-align: left;">GST Amount</th>
            <th style="text-align: right;"><?=number_format($gst_amount, 2)?></th>
        </tr>
        
        <tr>
            <th style="text-align: left;">GST</th>
            <th style="text-align: left;"><?=$gst_percentage?> %</th>
            <th style="text-align: left;">Tax After Amount</th>
            <th style="text-align: right;"><?=number_format($total_amount_after_tax, 2)?></th>
        </tr>
        <tr>
            <th style="text-align: left;">Tax Type - GST Type</th>
            <th style="text-align: left;"><?=$tax_type?> - <?=$gst_type?></th>
            <th style="text-align: left;">Grand Total</th>
            <th style="text-align: right;"><?=number_format($grand_total, 2)?></th>
        </tr>
        
        <tr>
            <th style="text-align: left;"></th>
            <th style="text-align: left;"></th>
            <th style="text-align: left;"></th>
            <th style="text-align: left;"></th>
        </tr>
    </thead>
</table>

</br>
<table class="table table-striped" style="width: 100%;">
  <thead>
    <tr>
      <th scope="col" style="width: 1%">S.No</th>
      <th scope="col" style="width: 29%">Product Name</th>
      <th scope="col" style="width: 20%">Weight Details</th>
      <th scope="col" style="width: 30%">Per Gram Cost - Per Stone Cost</th>
      <th scope="col" style="width: 20%">Total Amount</th>
    </tr>
  </thead>
  <tbody>
      
<?php
    $statement = $pdo->prepare("SELECT *,
    (SELECT name FROM tbl_products_name WHERE tbl_products_name.id=p.product_name) as product_name 
    FROM tbl_purchases_items p WHERE p.purchase_id=?");
    $statement->execute(array($ids));
    $sales_items_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData>0){
        $i=1;
        foreach($sales_items_data as $data){
            
?>
            
            <tr>
              <td style="text-align: center;"><?=$i++?></td>
              <td style="text-align: center;"><?=$data['product_name']?></td>
              <td style="text-align: center;">
                  Cross Wt: <b><?=$data['cross_weight']?></b></br>
                  Stone Wt: <b><?=$data['stone_weight']?></b></br>
                  Net Wt: <b><?=$data['net_weight']?></b></br>
              </td>
              <td style="text-align: center;">
                  <?=$data['per_gram_amount']?> - <?=$per_stone_cost?>
              </td>
              <td style="text-align: right; padding-right: 35px;"><?=number_format($data['total_amount'],2)?></td>
            </tr>
            
            <tr>
                
            </tr>
            
<?php   }
    }else{
        echo "<tr><td colspan='6' style='text-align: center;'>No Purchase Items Found</td></tr>";
    }
?>
  </tbody>
</table>

<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>

<?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();
</script>

