<?php  include "header.php"; 

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


<style>
    th, td{
        padding: 5px;
    }
    .table tbody tr:hover td {
        background-color: transparent !important;
        color: black !important;
    }
</style>



<div class="content-wrapper">
        <section class="content-header">
            <h1>View Purchases Details</h1>
            <ol class="breadcrumb">
                <li><a href="index.php"><i class="fa fa-dashboard"></i> Home</a></li>
                <li class="active">View Purchases Details</li>            
            </ol>
        </section>


        <div class="clearfix"></div>







<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box box-primary" style="box-shadow: rgba(0, 0, 0, 0.16) 0px 3px 6px, rgba(0, 0, 0, 0.23) 0px 3px 6px;">
                
                
                
                
                
                <div class="box-body">
                    
                    
                    
                <div id="content-div">
                    <div class="col-lg-12">
                        
<?php                       
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></br>

<table class="table table-striped" style="width: 100%;">
  <thead>
    <tr>
      <th scope="col" style="width: 1%; background: brown; color: white;">S.No</th>
      <th scope="col" style="width: 29%; background: brown; color: white;">Product Name</th>
      <th scope="col" style="width: 20%; background: brown; color: white;">Weight Details</th>
      <th scope="col" style="width: 30%; background: brown; color: white;">Purity Details</th>
      <th scope="col" style="width: 30%; background: brown; color: white;">Amount Details</th>
      <th scope="col" style="width: 20%; background: brown; color: white;">Total Amount</th>
    </tr>
  </thead>
  <tbody>
      
<?php
    $colspan="6";
    $statement = $pdo->prepare("SELECT p.*,
    (SELECT name FROM tbl_products_name WHERE tbl_products_name.id=p.product_name) as product_name,
    tbl_ids_purity.short_name,
    tbl_ids_products.name as products_ids_name,
    tbl_category.name as category_name
    FROM tbl_purchases_items p 
    LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=p.purity
    LEFT JOIN tbl_ids_products ON tbl_ids_products.id=p.product_ids
    LEFT JOIN tbl_category ON tbl_category.id=p.category
    WHERE p.purchase_id=?");
    $statement->execute(array($ids));
    $sales_items_data = $statement->fetchAll(PDO::FETCH_ASSOC);
    $totalData = $statement->rowCount();
    if($totalData>0){
        $i=1;
        $total_amount = 0;
        foreach($sales_items_data as $data){
            $total_amount+= $data['total_amount'];
            
            $making_charges = $data['making_charges'];
            
?>
            
            <tr style="background: green; color: white;">
              <td style="text-align: center;"><?=$i++?></td>
              <td style="text-align: center;">
                  <b>Products: <?=$data['products_ids_name']?></b></br>
                  <b>Product Name: <?=$data['product_name']?></b></br>
                  <b>Category: <?=$data['category_name']?></b></br>
              </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;">
                  Purity: <b><?=$data['short_name']?></b></br>
                  Gold Rate: <b><?=number_format($data['per_gram_amount'],2)?></b></br>
                  Stone Cost: <b><?=number_format($per_stone_cost,2)?></b></br>
                  Touch: <b><?=$data['touch']?></b></br>
              </td>
              <td style="text-align: center;">
                  Gold Amount: <b><?=number_format($data['per_gram_amount']*$data['net_weight'], 2)?></b></br>
                  Stone Cost: <b><?=number_format($per_stone_cost*$data['stone_weight'], 2)?></b></br>
                  Making Charges: <b><?=number_format($data['making_charges'], 2)?></b></br>
              </td>
              <td style="text-align: right; padding-right: 35px;"><?=number_format($data['total_amount'],2)?></td>
            </tr>
            
            <!--<tr>
                <th colspan="<?=$colspan?>">Barcode Items Details:</th>
            </tr>-->
            
            
        <?php
            $statements_items_barcode = $pdo->prepare("SELECT p.*,
            tbl_category.name as category_name, tbl_sub_category.name as sub_category_name,
            tbl_ids_products.name as product,
                tbl_units.name as unit_name, tbl_units.short_name as unit_short_name,
                tbl_ids_purity.name as purity_name,
                tbl_products_name.name as products_name,
                tbl_products_name.quantity as products_name_quantity,
                tbl_product_variants.id as main_product_id,
                tbl_product_variants.sales_status, tbl_product_variants.hsn_code,
                tbl_wastage.name as wastage_name, tbl_wastage.percentage as wastage_percentage,
                tbl_prices.amount as today_gold_rate
                FROM `tbl_barcode_items` p 
                LEFT JOIN tbl_category ON tbl_category.id=p.category
                LEFT JOIN tbl_sub_category ON tbl_sub_category.id=p.sub_category
                LEFT JOIN tbl_ids_purity ON tbl_ids_purity.id=p.purity
                LEFT JOIN tbl_ids_products ON tbl_ids_products.id=p.pro_ids
                LEFT JOIN tbl_units ON tbl_units.id=p.unit
                LEFT JOIN tbl_products_name ON tbl_products_name.id=p.product_name
                LEFT JOIN tbl_product_variants ON tbl_product_variants.barcode_items_id=p.id
                LEFT JOIN tbl_wastage ON tbl_wastage.id=p.wastage
                LEFT JOIN tbl_prices ON tbl_prices.id=p.purity
                WHERE p.purchase_items_id=?");
                
                
            $statements_items_barcode->execute(array($data['id']));
            $items_resultss_barcode = $statements_items_barcode->fetchAll(PDO::FETCH_ASSOC);
            $items_totalDatas_barcode = $statements_items_barcode->rowCount();
                
            if($items_totalDatas_barcode>0){
                    $i=1;
                    foreach($items_resultss_barcode as $barcode){ 
                        
                        $unit_name = "(".$barcode['unit_short_name'].")";
                        
                        $barcode_items_id = $barcode['id'];
                        $purchase_items_id = $barcode['purchase_items_id'];
                        $barcode_category = $barcode['category'];
                        $barcode_sub_category = $barcode['sub_category'];
                        $barcode_product_type = $barcode['type'];
                        $barcode_unit = $barcode['unit'];
                        $barcode_purity = $barcode['purity'];
                        $barcode_wastage = $barcode['wastage'];
                        $b_cross_weight = $barcode['cross_weight'];
                        $b_stone_weight = $barcode['stone_weight'];
                        $b_net_weight = $barcode['net_weight'];
                        $b_stone_cost = $barcode['stone_cost'];
                        $b_others_weight = $barcode['others_weight'];
                        
                        $b_product_name = $barcode['product_name'];
                        $b_product_ids = $barcode['pro_ids'];
                        
                        $b_quantity = $barcode['quantity'];
                        
                        $main_product_id = $barcode['main_product_id'];
                        $purchase_price = $barcode['purchase_price'];
                        
                        if($barcode_product_type==1){
                            $productt_type = "Plain";
                        }else{
                            $productt_type = "Stone";
                        }
                        
                        $making_charges = $barcode['making_charges'];
                        
                        
                        if($barcode['wastage_name']==""){
                            $wastage_name = "0%";
                            $wastage_percentage = 0;
                        }else{
                            $wastage_name = $barcode['wastage_name'];
                            $wastage_percentage = $barcode['wastage_percentage'];
                        }
                        $stone_cost = $barcode['stone_cost'];
                        $today_gold_rate = $barcode['today_gold_rate'];
                        
                        $wastage_amount = $today_gold_rate*($wastage_percentage/100);
                        $product_amount = $today_gold_rate*$b_net_weight;
                        
                        $total_wastage_amount = $product_amount/$wastage_percentage;
                        
                        $grand_total = $product_amount+$stone_cost+$making_charges;
                        
                        $selling_price = $product_amount+$stone_cost+$total_wastage_amount;
                        
                        if($barcode['sales_status']==0){
                            $sales_status = "<span class='btn btn-danger btn-xs'>Saled</span>";
                        }else{
                            $sales_status = "<span class='btn btn-success btn-xs'>Available</span>";
                        }
                    ?>
                    
            <tr>
                <td colspan="2">
                    <b>Product Name: <?=$barcode['products_name']?></b></br>
                    <b>Category: <?=$barcode['category_name']?></b></br>
                    <b>Sub Category: <?=$barcode['sub_category_name']?></b></br>
                    <b>Barcode Code: <?=$barcode['barcode_unique_id']?></b></br>
                    <b>HSN Code: <?=$barcode['hsn_code']?></b></br>
                    <b>Status: <?=$sales_status?></b>
                </td>
                
                <td>
                    <b>Cross Wt: <?=number_format($b_cross_weight, 2)?></b></br>
                    <b>Stone Wt: <?=number_format($b_stone_weight, 2)?></b></br>
                    <b>Net Wt: <?=number_format($b_net_weight, 2)?></b></br>
                    <b>Tag Wt: <?=number_format($b_others_weight, 2)?></b></br>
                </td>
                
                <td>
                    <b>Product Type: <?=$productt_type?></b></br>
                    <b>Quantity: <?=$b_quantity?></b></br>
                    <b>Wastage: <?=$wastage_name?></b></br>
                    <b>Today Rate: <?=number_format($today_gold_rate, 2)?></b>
                </td>
                
                <td>
                    <b>Purchase Price: <?=number_format($purchase_price, 2)?></b></br>
                    <b>Product Amount: <?=number_format($product_amount, 2)?></b></br>
                    <b>Stone Cost: <?=number_format($stone_cost, 2)?></b></br>
                    <b>Wastage Amount: <?=number_format($wastage_amount, 2)?></b></br>
                    <b>Total Wastage: <?=number_format($total_wastage_amount, 2)?></b></br>
                    <b>Making Charges: <?=number_format($making_charges, 2)?></b></br>
                    <b>Selling Price: <b style="color: green;"><?=number_format($selling_price, 2)?></b></b></br>
                </td>
                
                <td>
                    <b><i class="fa fa-inr"></i> <?=number_format($grand_total, 2)?></b></br>
                </td>
                
            </tr>
                    
            <?php } } ?>
            
<?php   }
    }else{
        echo "<tr><td colspan='".$colspan."' style='text-align: center;'>No Purchase Items Found</td></tr>";
    }
?>
  </tbody>
  
  
  <tfoot>
        <tr style="background: blue; color: white;">
          <th  colspan="4"></th>
          <th scope="col">Grand Total:</th>
          <th scope="col" style="text-align: right; padding: 5px;">
              <i class="fa fa-inr"></i> <?=number_format($total_amount, 2)?>
          </th>
        </tr>
  </tfoot>
</table>



<?php } else { ?>

<center>No Records Found</center>
<?php } ?>

                       

                    </div>
                    
                    
                    
                    
                    
                        <div class="form-group" style="text-align:center;">
                            <a onclick="location.reload();"><button class="btn btn-primary" ><i class="fa fa-refresh"></i> Refresh</button></a>
                            <a href="purchases-list.php"><button class="btn btn-warning" ><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </div>
                        
                        
                </div>        
                
                
<div class="container" id="loading-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
        <center> <img src="img/loading.gif" style="width: 250px;"><br><b class="blinking">Processing Please Wait...</b></center>
</div>
                    
                    
                    

                
                    
                    <div class="clearfix"></div>
                </div>
                
                
                
                
            </div>
        </div>
    </div>
</section>
<?php include "footer.php"; ?>











