<?php  include "header.php"; 

$ids = $_GET['id'];
$statement = $pdo->prepare("SELECT * FROM tbl_purchases e where e.id=?");
$statement->execute(array($ids));
$datas = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

if($totalData>0){
    $supplier_id = $datas[0]['supplier_id'];
    $date = $datas[0]['date'];
    $invoice_no = $datas[0]['invoice_no'];
    $total_cross_weight = $datas[0]['total_cross_weight'];
    $total_stone_weight = $datas[0]['total_stone_weight'];
    $total_net_weight = $datas[0]['total_net_weight'];
    $per_gm_stone_amount = $datas[0]['per_gm_stone_amount'];
    $gst_percentage = $datas[0]['gst_percentage'];
    
    $tax_type = $datas[0]['tax_type'];
    $gst_type = $datas[0]['gst_type'];
}
    
$statementss = $pdo->prepare("SELECT * FROM tbl_purchases_items e where e.purchase_id=?");
$statementss->execute(array($ids));
$datasss = $statementss->fetchAll(PDO::FETCH_ASSOC);
$totalDatass = $statementss->rowCount();
?>



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


        <div class="col-lg-12 alerts">
            <div id="custom-alerts" style="display:none;">
                <div class="alert alert-dismissable">
                    <div class="custom-msg"></div>
                </div>
            </div>
            
            <?php if ($error)  { ?>
            <div class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-ban"></i> Error</h4>
                <?= $error; ?>
            </div>
            <?php $error=0;} if ($warning) { ?>
            <div class="alert alert-warning alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-warning"></i> Warning</h4>
                <?= $warning; ?>
            </div>
            <?php  $warning=0;} if ($message) { ?>
            <div class="alert alert-success alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4>    <i class="icon fa fa-check"></i> Success</h4>
                <?= $message; ?>
            </div>
            <?php $message=0; } ?>
        </div>
        <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">

                        <form id="form_details" autocomplete="off">
                            
                            <input type="hidden" name="ids" value="<?=$ids?>">
                            
                            
                            
                            
                                                                 
                        <div class="row">
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="supplier_id">Supplier</label>                                   
                                    <select name="supplier_id" class="form-control select2" style="width:100%" id="supplier_id" required="required">
                                        <option value="">Select Supplier</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_suppliers` where status=1");
                                            $statement->execute();
                                            $location_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($location_result as $result) { ?>
                                                <option value="<?=$result['id']?>" <?= ($supplier_id==$result['id']) ? "selected":"" ?>><?=$result['name']?></option>
                                        <?php  }  ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="invoice_no">Invoice No</label>                                    
                                    <input type="invoice_no" name="invoice_no" value="<?=$invoice_no?>" placeholder="Enter the Invoice No" class="form-control" id="invoice_no"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label class="required" for="date">Date</label>                                    
                                    <input type="date" name="date" value="<?=$date?>"  class="form-control" id="date"  required="required" />
                                </div>
                            </div>
                            
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label class="required" for="per_stone_amount">Per Gram Stone Amount</label>                                    
                                    <input type="text" name="per_stone_amount" value="<?=$per_gm_stone_amount?>" oninput="StoneAmount()" class="form-control" id="per_stone_amount"  required="required" onkeypress="return isNumberKey(this, event);" />
                                </div>
                            </div>
                            
                            
                            <div class="col-md-2" style="margin: 25px 0px;">
                                <span id="add_new_products" class="btn btn-primary btn-xs"><i class="fa fa-plus"></i> Add New Products</span>
                            </div>
                            
                            
                            
                            
    <!-- Product Items Starts Here -->
                            <div class="col-lg-12">
                                
            <?php if($totalDatass>0){
                foreach($datasss as $res){ 
                    
                    $items_id = $res['id'];
                    $product_ids = $res['product_ids'];
                    $product_name = $res['product_name'];
                    $category = $res['category'];
                    $product_type = $res['product_type'];
                    $unit = $res['unit'];
                    $cross_weight = $res['cross_weight'];
                    $stone_weight = $res['stone_weight'];
                    $net_weight = $res['net_weight'];
                    $stone_cost = $res['stone_cost'];
                    $purity = $res['purity'];
                    $touch = $res['touch'];
                    $per_gram_amount = $res['per_gram_amount'];
                    $making_charges = $res['making_charges'];
                    $total_amount = $res['total_amount'];
                
                
            ?>
            
                                <div class="row porow" style="background: aquamarine; border-radius: 10px; padding: 10px 0px 10px 0px; margin: 5px 0px 15px 0px; box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px; margin-top: 25px;">
                                        
                                        <input class="count" value="<?=$items_id?>" hidden>
                                        <input name="product_id[]" class="product_ids" value="0" hidden>
                                        <input name="purchase_order_items_id[]" class="" value="<?=$items_id?>" hidden>
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="product" class="required">Products</label> 
                                                <select name="product[]" id="product_<?=$items_id?>" onchange="productKarat(<?=$items_id?>)" class="form-control select22" required>
                                                    <option value="">Select Product</option>
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_products` WHERE status=1");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>" <?= ($product_ids==$ress['id']) ? "selected":"" ?>><?=$ress['name']?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group">
                                                <label for="product_name" class="required">Product Name</label> 
                                                <select name="product_name[]" id="product_name_<?=$items_id?>" class="form-control select22" required>
                                                    <option value="">Select Product Name</option>
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_products_name` WHERE status=1 and type=2");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>" <?= ($product_name==$ress['id']) ? "selected":"" ?>><?=$ress['name']?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group" style="">
                                                <label for="category_name" class="required">Category</label> 
                                                <select name="category_name[]" id="category_name" class="form-control select22" required>
                                                    <option value="">Select Category</option>
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_category` WHERE status=1");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>" <?= ($category==$ress['id']) ? "selected":"" ?>><?=$ress['name']?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                    
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group" style="">
                                                <label for="product_type">Product Type <span style="color: red">*</span></label>
                                                <select name="product_type[]" id="product_type_<?=$items_id?>" onchange="productType(<?=$items_id?>)" class="form-control select22">
                                                    <option value="1" <?= ($product_type==1) ? "selected":"" ?>>Plain</option>
                                                    <option value="2" <?= ($product_type==2) ? "selected":"" ?>>Stone</option>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-3" hidden>
                                            <div class="form-group" style="">
                                                <label for="unit">Unit <span style="color: red">*</span></label> 
                                                <select name="unit[]" id="unit_<?=$items_id?>" class="form-control select22" onchange="UnitChange(<?=$items_id?>)" required data-count="<?=$items_id?>">
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_units` WHERE status=1");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>" <?= ($unit==$ress['id']) ? "selected":"" ?>><?=$ress['name']?> (<?=$ress['short_name']?>)</option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        
                                        <div id="plain_section_<?=$items_id?>"></div>
                            
                                        <div id="stone_section_<?=$items_id?>"></div>
                                        
                                        
                                        <!-- Weight Sections Starts Here -->
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="cross_weight">Cross Weight</label>                                   
                                     <input type="text" name="cross_weight[]" placeholder="Enter the Cross Weight" value="<?=$cross_weight?>" oninput="calculateNetWeight(<?=$items_id?>)" class="form-control cross_wt" id="cross_weight_<?=$items_id?>" onkeypress="return isNumberKey(this, event);" required="required" data-count="<?=$items_id?>" />
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="stone_weight">Stone Weight</label>                                   
                                     <input type="text" name="stone_weight[]" placeholder="Enter the Stone Weight" value="<?=$stone_weight?>" oninput="calculateNetWeight(<?=$items_id?>)" class="form-control stone_wt" id="stone_weight_<?=$items_id?>"  onkeypress="return isNumberKey(this, event);" required="required"  data-count="<?=$items_id?>"/>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="net_weight">Net Weight</label>                                   
                                     <input type="text" name="net_weight[]" placeholder="Enter the Net Weight" value="<?=$net_weight?>"  class="form-control net_wt" id="net_weight_<?=$items_id?>" onkeypress="return isNumberKey(this, event);" required="required"  data-count="<?=$items_id?>"/>
                                </div>
                            </div>
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="stone_cost">Stone Cost</label>                                   
                                     <input type="text" name="stone_cost[]" placeholder="Enter the Stone Cost" value="<?=$stone_cost?>"  class="form-control" id="stone_cost_<?=$items_id?>" onkeypress="return isNumberKey(this, event);" required="required" data-count="<?=$items_id?>" />
                                </div>
                            </div>
                                        <!-- Weight Sections Endsss Here -->
                                        
                                  
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="purity">Purity <span style="color: red">*</span></label> 
                                                <select name="purity[]" id="purity_<?=$items_id?>" class="form-control select22" onchange="purityChange(<?=$items_id?>)" required>
                                                    <option value="">Select Purity</option>
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_purity` WHERE status=1");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>" <?= ($purity==$ress['id']) ? "selected":"" ?>><?=$ress['name']?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="touch">Touch</label>
                                                <input type="text" name="touch[]" class="form-control touch" value="<?=$touch?>" id="touch_<?=$items_id?>" placeholder="Touch">
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="today_gram_amount">Per Gram Amount <span style="color: red">*</span></label>
                                                <input type="text" name="today_gram_amount[]" class="form-control" value="<?=$per_gram_amount?>" id="today_gram_amount_<?=$items_id?>" oninput="ProductTotal(<?=$items_id?>); GrandTotal();" placeholder="Today Gram Amount">
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="making_charges">Making Charges <span style="color: red">*</span></label>
                                                <input type="text" name="making_charges[]" class="form-control" value="<?=$making_charges?>" id="making_charges_<?=$items_id?>" oninput="ProductTotal(<?=$items_id?>); GrandTotal();" placeholder="Making Charges">
                                            </div>
                                        </div>
                                        
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="total_pro_amount">Total Amount <span style="color: red">*</span></label>
                                                <input type="text" name="total_pro_amount[]" class="form-control total_product_amount" value="<?=$total_amount?>" id="total_amount_<?=$items_id?>" placeholder="Total Amount" readonly>
                                            </div>
                                        </div>
                                        
                                        
                                        <div class="col-lg-1">
                                            <span class="btn btn-danger btn-sm remove_product" style="margin-top: 28px;"><i class="bi bi-trash3-fill"></i></span>
                                        </div>
                                        
                                        
                                        
                                    </div>
            
            <?php } } ?>                
                            
                            
                            </div>
    <!-- Product Items Endss Here -->
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            <div class="col-lg-12" id="product_tbody">
                                
                            </div>

                        </div>
                        
                        
                        <div class="row">
                            
                            
                            
                            
                            
                            <div class="col-md-4">
                                <div class="col-lg-12" style="background: #ebebe1; padding-top: 10px;margin-top: 20px;padding-bottom: 20px;border-radius: 15px;box-shadow: 2px 1px 2px 2px;">
                                    <div class="col-lg-6" style="padding-top: 10px;">
                                        <b>Total Items</b>
                                    </div>
                                    <div class="col-lg-6" style="padding-top: 10px;">
                                        <span id="total_product_items" style="font-weight: bold;">0</span>
                                    </div>
                                    <div class="col-lg-6" style="padding-top: 10px;">
                                        <b>Total</b>
                                    </div>
                                    <div class="col-lg-6" style="padding-top: 10px;">
                                        <i class="fa fa-inr"></i> <span id="total_product_amount" style="font-weight: bold;">0.00</span>
                                    </div>
                                    <div class="col-lg-6" style="padding-top: 15px;">
                                        <b>Select Tax (%)</b>
                                    </div>
                                    
                                    <div class="col-lg-6" style="padding-top: 15px;">
                                        <select name="gst_percentage"  class="form-control" id="tax" style="width: 100%;" onkeyup="calculate()" onchange="calculate()">
                                            <option value="0">0% GST</option>
                                        <?php $statement = $pdo->prepare("SELECT * FROM `tbl_gst` WHERE status=1 ORDER BY gst ASC");
                                            $statement->execute();
                                            $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                            foreach ($resultss as $ress) { ?>
                                                <option value="<?=$ress['gst']?>" <?= ($gst_percentage==$ress['gst']) ? "selected":"" ?>><?=$ress['name']?></option>
                                        <?php } ?>
                                        </select>
                                    </div>
                                    
                                    <div class="col-lg-6" style="padding-top: 10px;">
                                        <div class="form-group">
                                            <label class="" for="insurance_policy">Select Tax Type</label>
                                        </div>
                                     </div>
                                    
                                    <div class="col-lg-6" style="padding-top: 10px;">
                                        <div class="form-group">
                                            <select name="tax_type" id="tax_type" onkeyup="calculate()" style="width: 100%;" onchange="calculate()" required="required" class="form-control">
                                                <option value="1" <?= ($tax_type==1) ? "selected":"" ?>>Exclusive</option>
                                                <option value="2" <?= ($tax_type==2) ? "selected":"" ?>>Inclusive</option>
                                            </select>
                                        </div>
                                     </div>
                                    
                                     
                                    <div class="col-lg-6">
                                        <b>Select GST Type</b>
                                     </div>
                                    
                                    <div class="col-lg-6">
                                        <div class="form-group">
                                            <select name="gst_type" id="gst_type" onkeyup="calculate()" onchange="calculate()" required="required" class="form-control">
                                                <option value="1" <?= ($gst_type==1) ? "selected":"" ?>>GST</option>
                                                <option value="2" <?= ($gst_type==2) ? "selected":"" ?>>IGST</option>
                                            </select>
                                        </div>
                                    </div>
                                    
                                    
                                    <!-- GST HERE -->
                                    <div class="row" id="div-cgst">
                                            <div class="col-md-8" >
                                                    <div class="form-group">
                                                        <div class="form-group">
                                                            <label for="code">Total CGST &nbsp; <span style="color: blue;font-weight: 900;font-size: 18px;" id='cgst_tax_per' name='cgst_tax_per'>0.00</span> % :</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
                                                        </div>
                                                    </div>
                                            </div>
                                            <div class="col-md-4">
                                                    <div class="form-group">
                                                        <span style="color: blue;font-weight: 900;font-size: 18px;" id='cgst_tax_amount'>0.00</span>
                                                    </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row" id="div-sgst">
                                            <div class="col-md-8" >
                                                    <div class="form-group">
                                                        <div class="form-group">
                                                            <label for="code">Total SGST &nbsp; <span style="color: blue;font-weight: 900;font-size: 18px;" id='sgst_tax_per'>0.00</span> % :</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
                                                        </div>
                                                    </div>
                                            </div>
                                            <div class="col-md-4">
                                                    <div class="form-group">
                                                        <span style="color: blue;font-weight: 900;font-size: 18px;" id='sgst_tax_amount'>0.00</span>
                                                        
                                                    </div>
                                            </div>
                                        </div>
                                        
                                        <div class="row" id="div-sgst">
                                            <div class="col-md-8" >
                                                    <div class="form-group">
                                                        <div class="form-group">
                                                            <label for="code">Total &nbsp; <span id='gst_type_span'></span> &nbsp; <span style="color: blue;font-weight: 900;font-size: 18px;" id='gst_tax_per' name='gst_tax_per'>0.00</span> % :</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                            <input type="hidden" name="gst_amount" value="" id="gst_amount">
                                                        </div>
                                                    </div>
                                            </div>
                                            <div class="col-md-4">
                                                    <div class="form-group">
                                                        <span style="color: blue;font-weight: 900;font-size: 18px;" id='total_tax'>0.00</span>
                                                    </div>
                                            </div>
                                        </div>
                                        
                                        
                                        <!-- Hidden GST Values Here -->
                                        <input type="hidden" name="gst_or_igst_amount" id="total_tax_input" value="">
                                        <input type="hidden" name="cgst_or_sgst_amount" id="cgst_tax_per_input" value="">
                                        <!-- Hidden GST Values Here -->
                                        
                                        
                                        <div class="row" id="" style="padding-top: 20px;">
                                            <div class="col-md-6" >
                                                    <div class="form-group">
                                                        <div class="form-group">
                                                            <label for="code">Total Amount After Tax:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                        </div>
                                                    </div>
                                            </div>
                                            <div class="col-md-6">
                                                    <div class="form-group">
                                                        <input type="text" name="tax_after_total" id="tax_after_total" class="form-control" value="" required="required" style="width: 90%; background: #fd0a36;; color: white;font-weight: 600;font-size: 20px;" readonly>
                                                        <!--<span style="color: blue;font-weight: 900;font-size: 18px;" id='tax_after_total'>0.00</span> -->
                                                    </div>
                                            </div>
                                        </div>
                                    <!-- GST HERE -->
                                                 
                                </div>
                            </div>
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            
                            <div class="col-md-4">
                            
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="required" for="total_cross_weight">Total Cross Weight</label>                                   
                                         <input type="text" name="total_cross_weight" placeholder="Total Cross Weight" value=""  class="form-control" id="total_cross_weight"  required="required"  readonly/>
                                    </div>
                                </div>
                                
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="required" for="total_stone_weight">Total Stone Weight</label>                                   
                                         <input type="text" name="total_stone_weight" placeholder="Total Stone Weight" value="0"  class="form-control" id="total_stone_weight"  required="required"  readonly/>
                                    </div>
                                </div>
                                
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="required" for="total_net_weight">Total Net Weight</label>                                   
                                         <input type="text" name="total_net_weight" placeholder="Total Net Weight" value=""  class="form-control" id="total_net_weight"  required="required" readonly />
                                    </div>
                                </div>
                            
                            </div>
                            
                            
                            
                            <div class="col-md-4">
                                
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="required" for="total_amount">Total Amount</label>                                   
                                         <input type="text" name="total_amount" placeholder="Total Amount" value=""  class="form-control" id="total_amount"  required="required" readonly />
                                    </div>
                                </div>
                                
                                <div class="col-md-12">
                                    <div class="form-group" style="">
                                        <label for="discount_type">Discount Type <span style="color: red">*</span></label>
                                        <select name="discount_type" id="discount_type" onchange="discountType()" class="form-control select22" style="width: 100%;">
                                            <option value="1">Percentage</option>
                                            <option value="2">Fixed Amount</option>
                                        </select>
                                    </div>
                                </div>
                                
                                
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="optional" for="discount">Discount <span id="discount_span"></span></label>                                   
                                         <input type="text" name="discount" placeholder="Discount" value="0"  oninput="calculateTotal()" class="form-control" id="discount"  />
                                    </div>
                                </div>
                                
                                
                                <div class="col-md-12"><b style="color: blue;">Discount (<i class="fa fa-inr"></i>) : <span id="discount_amount_span">0.00</span></b></div>
                                <input type="hidden" name="discount_amount" id="discount_amount" value="" >
                                
                                
                                
                                <!--<div class="col-md-12">
                                    <div class="form-group" style="">
                                        <label for="total_amount">Total Amount After Discount</label>
                                        <input type="text" id="total_amount" class="form-control" readonly>
                                    </div>
                                </div>-->
                                
                                
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="required" for="grand_amount">Grand Amount</label>                                   
                                         <input type="text" name="grand_amount" placeholder="Grand Amount" value=""  class="form-control" id="grand_amount"  required="required" readonly />
                                    </div>
                                </div>
                            </div>
                            
                            
                            
                            
                            
                            
                            
                            
                        </div>
                        
                        
                        

                        <div class="form-group" style="text-align:center;">
                            <input type="submit"  id="submit" value="Update Purchase" class="btn btn-success" />
                        </div>
                        
                        </form>

                    </div>
                    
                    
                        <div class="form-group" style="text-align:center;">
                            <span onclick="location.reload();" class="btn btn-primary" ><i class="fa fa-refresh"></i> Reset Form</span>
                            <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="container" id="success-div" style="display: none; margin-top: 40px; margin-bottom: 40px;">
                        <center><b class="blinking" style="font-size:22px;">Purchase Details Updated Successfully...</b><br>
                            <img src="img/success.gif" style="width: 250px;">
                            <br><br>
                            <span onclick="location.reload();" class="btn btn-info"><i class="fa fa-refresh"></i> Reset Form</span>
                            &nbsp;&nbsp;
                            <a href="purchases-list.php"><button class="btn btn-warning"><i class="fa fa-chevron-left"></i> Back to List</button></a>
                        </center>
                    </div>
                
                    
                    <div class="clearfix"></div>
                </div>
                
                
                
                
            </div>
        </div>
    </div>
</section>
<?php include "footer.php"; ?>


<script>
$(document).ready(function() {
    
var count = 1;
$("#add_new_products").click(function(){
    
    var row=`<div class="row porow" style="background: aquamarine; border-radius: 10px; padding: 10px 0px 10px 0px; 
    margin: 5px 0px 15px 0px; box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px; margin-top: 25px;">
                                        
                                        <input class="count" value="`+count+`" hidden>
                                        <input name="product_id[]" class="product_ids" value="0" hidden>
                                        <input name="purchase_order_items_id[]" class="" value="0" hidden>
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="product" class="required">Products</label> 
                                                <select name="product[]" id="product_`+count+`" onchange="productKarat(`+count+`)" class="form-control select22" required>
                                                    <option value="">Select Product</option>
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_products` WHERE status=1");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group">
                                                <label for="product_name" class="required">Product Name</label> 
                                                <select name="product_name[]" id="product_name_`+count+`" class="form-control select22" required>
                                                    <option value="">Select Product Name</option>
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_products_name` WHERE status=1 and type=2");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group" style="">
                                                <label for="category_name" class="required">Category</label> 
                                                <select name="category_name[]" id="category_name" class="form-control select22" required>
                                                    <option value="">Select Category</option>
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_category` WHERE status=1");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                    
                                        
                                        <div class="col-lg-3">
                                            <div class="form-group" style="">
                                                <label for="product_type">Product Type <span style="color: red">*</span></label>
                                                <select name="product_type[]" id="product_type_`+count+`" onchange="productType(`+count+`)" class="form-control select22">
                                                    <option value="1">Plain</option>
                                                    <option value="2">Stone</option>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-3" hidden>
                                            <div class="form-group" style="">
                                                <label for="unit">Unit <span style="color: red">*</span></label> 
                                                <select name="unit[]" id="unit_`+count+`" class="form-control select22" onchange="UnitChange(`+count+`)" required data-count="`+count+`">
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_units` WHERE status=1");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>"><?=$ress['name']?> (<?=$ress['short_name']?>)</option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        
                                        <div id="plain_section_`+count+`"></div>
                            
                                        <div id="stone_section_`+count+`"></div>
                                        
                                    
                                        
                                  
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="purity">Purity <span style="color: red">*</span></label> 
                                                <select name="purity[]" id="purity_`+count+`" class="form-control select22" onchange="purityChange(`+count+`)" required>
                                                    <option value="">Select Purity</option>
                                                    <?php $statement = $pdo->prepare("SELECT * FROM `tbl_ids_purity` WHERE status=1");
                                                        $statement->execute();
                                                        $resultss = $statement->fetchAll(PDO::FETCH_ASSOC); 
                                                        foreach ($resultss as $ress) { ?>
                                                            <option value="<?=$ress['id']?>"><?=$ress['name']?></option>
                                                    <?php } ?>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="touch">Touch</label>
                                                <input type="text" name="touch[]" class="form-control touch" value="" id="touch_`+count+`" placeholder="Touch">
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="today_gram_amount">Per Gram Amount <span style="color: red">*</span></label>
                                                <input type="text" name="today_gram_amount[]" class="form-control" id="today_gram_amount_`+count+`" oninput="ProductTotal(`+count+`); GrandTotal();" placeholder="Today Gram Amount">
                                            </div>
                                        </div>
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="making_charges">Making Charges <span style="color: red">*</span></label>
                                                <input type="text" name="making_charges[]" class="form-control" id="making_charges_`+count+`" oninput="ProductTotal(`+count+`); GrandTotal();" placeholder="Making Charges">
                                            </div>
                                        </div>
                                        
                                        
                                        <div class="col-lg-2">
                                            <div class="form-group">
                                                <label for="total_pro_amount">Total Amount <span style="color: red">*</span></label>
                                                <input type="text" name="total_pro_amount[]" class="form-control total_product_amount" value="0.00" id="total_amount_`+count+`" placeholder="Total Amount" readonly>
                                            </div>
                                        </div>
                                        
                                        
                                        <div class="col-lg-1">
                                            <span class="btn btn-danger btn-sm remove_product" style="margin-top: 28px;"><i class="bi bi-trash3-fill"></i></span>
                                        </div>
                                        
                                        
                                        
                                    </div>`;
        
        $("#product_tbody").append(row);
        $(".select22").select2();
        productType(count);
        productCount();
        count++;
});
});
</script>



























<script>

// On Key Press - Weight Calculation Starts Here
function calculateNetWeight(count) {
    var crossWeight = parseFloat($("#cross_weight_"+count).val()) || 0;
    var stoneWeight = parseFloat($("#stone_weight_"+count).val()) || 0;

    var netWeight = crossWeight - stoneWeight;

    if (!isNaN(netWeight)) {
        $("#net_weight_"+count).val(netWeight);
    } else {
        $("#net_weight_"+count).val("");
    }
}
function plain_NetWeight(count){
    var crossWeight = parseFloat($("#cross_weight_"+count).val()) || 0;
    $("#net_weight_"+count).val(crossWeight);
}
// On Key Press - Weight Calculation Endsss Here



// Get Today Amount - Here
function GetTodayAmount(count){
    var product_ids = $("#product_"+count).val();
    var purity_ids = $('#purity_'+count).val(); 
    var date = $('#date').val(); 
   
    $.ajax({
            url: 'api/get-values-api.php',
            type: 'post',
            dataType: 'json',
            data: {type:'get_product_rates',product_ids:product_ids, purity_ids:purity_ids, date:date},
            success: function (response) {
                console.log(response);
                if(response['error']==false){
                    console.log(response);
                    $("#today_gram_amount_"+count).val(response['today_rate']);
                    ProductTotal(count);
                    GrandTotal();
                }else{
                    $("#today_gram_amount_"+count).val("0");
                }
            }
        });
}

// Product Total - Here
function ProductTotal(count){
    var per_stone_amount = $("#per_stone_amount").val() || 0;
    
    var unit = $("#unit_"+count).val();
    var per_gm_amount = parseFloat($("#today_gram_amount_"+count).val()) || 0;
    var cross_weight = parseFloat($("#cross_weight_"+count).val()) || 0;
    var net_weight = parseFloat($("#net_weight_"+count).val()) || 0;
    var stone_weight = parseFloat($("#stone_weight_"+count).val()) || 0;
    var making_charges = parseFloat($("#making_charges_"+count).val()) || 0;
    if(unit==1){
        cal_s = per_stone_amount*stone_weight;
        cal = per_gm_amount*net_weight;
        total = cal_s+cal+making_charges;
        
        $("#stone_cost_"+count).val(cal_s.toFixed(2));
        $("#total_amount_"+count).val(total.toFixed(2));
    }else if(unit==2){
        cal_s = per_stone_amount*(stone_weight*1000);
        cal = per_gm_amount*(net_weight*1000);
        total = cal_s+cal+making_charges;
        $("#total_amount_"+count).val(total.toFixed(2));
        $("#stone_cost_"+count).val(cal_s.toFixed(2));
        
    }
    
}



function UnitChange(count){
    GetTodayAmount(count);
}
function purityChange(count){
    GetTodayAmount(count);
}

function discountType(){
    var discount_type = $("#discount_type").val();
    
    if(discount_type == 1){
        $("#discount_span").text("(%)");
    } else if(discount_type == 2){
        $("#discount_span").text("Amount");
    }
    calculateTotal();
}

function calculateTotal() {
    var totalAmount = parseFloat($("#tax_after_total").val()) || 0;
    var discount = parseFloat($("#discount").val()) || 0;
    var discount_type = $("#discount_type").val();
    
    if (discount_type == 1) {
        var discountAmount = (totalAmount * discount / 100);
        totalAmount -= discountAmount;
        $("#discount_amount_span").text(discountAmount.toFixed(2));
        $("#discount_amount").val(discountAmount);
    } else if (discount_type == 2) {
        totalAmount -= discount;
        $("#discount_amount_span").text(discount.toFixed(2));
        $("#discount_amount").val(discount);
    }
    
    $("#grand_amount").val(totalAmount.toFixed(2));
}

















function productType(count){
    $("#plain_section_"+count).empty();
    $("#stone_section_"+count).empty();
    
    var product_type = $("#product_type_"+count).val();
   
    if(product_type==1){
        var row=`<div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="cross_weight">Cross Weight</label>                                   
                                     <input type="text" name="cross_weight[]" placeholder="Enter the Cross Weight" value="" oninput="plain_NetWeight(`+count+`)" class="form-control cross_wt" id="cross_weight_`+count+`" onkeypress="return isNumberKey(this, event);" required="required" data-count="`+count+`" />
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="net_weight">Net Weight</label>                                   
                                     <input type="text" name="net_weight[]" placeholder="Enter the Net Weight" value=""  class="form-control net_wt" id="net_weight_`+count+`" onkeypress="return isNumberKey(this, event);" required="required" readonly />
                                </div>
                            </div>
                            <input type="hidden" name="stone_weight[]"  value="0" id="stone_weight_`+count+`"  required="required" />
                            <input type="hidden" name="stone_cost[]"  value="0" id="stone_cost_`+count+`"  required="required" />
                            `;
        $("#plain_section_"+count).append(row);
    }else{
        var row=`<div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="cross_weight">Cross Weight</label>                                   
                                     <input type="text" name="cross_weight[]" placeholder="Enter the Cross Weight" value="" oninput="calculateNetWeight(`+count+`)" class="form-control cross_wt" id="cross_weight_`+count+`" onkeypress="return isNumberKey(this, event);" required="required" data-count="`+count+`" />
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="stone_weight">Stone Weight</label>                                   
                                     <input type="text" name="stone_weight[]" placeholder="Enter the Stone Weight" value="" oninput="calculateNetWeight(`+count+`)" class="form-control stone_wt" id="stone_weight_`+count+`"  onkeypress="return isNumberKey(this, event);" required="required"  data-count="`+count+`"/>
                                </div>
                            </div>
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="net_weight">Net Weight</label>                                   
                                     <input type="text" name="net_weight[]" placeholder="Enter the Net Weight" value=""  class="form-control net_wt" id="net_weight_`+count+`" onkeypress="return isNumberKey(this, event);" required="required"  data-count="`+count+`"/>
                                </div>
                            </div>
                            
                            
                            <div class="col-md-3">
                                <div class="form-group">
                                    <label class="required" for="stone_cost">Stone Cost</label>                                   
                                     <input type="text" name="stone_cost[]" placeholder="Enter the Stone Cost" value=""  class="form-control" id="stone_cost_`+count+`" onkeypress="return isNumberKey(this, event);" required="required" data-count="`+count+`" />
                                </div>
                            </div>`;
        $("#stone_section_"+count).append(row);
    }

}

function productKarat(count){
    
    $('#purity_'+count).empty();  
    var product_ids = $("#product_"+count).val();
    $.ajax({
            url: 'api/get-values-api.php',
            type: 'post',
            dataType: 'json',
            data: {type:'get_purity',product_ids:product_ids},
            success: function (response) {
                console.log(response);
                if(response['error']==false){
                    var m='';
                    for(var n=0;n<response['data'].length;n++){
                        m+='<option value='+response['data'][n]['id']+'>'+response['data'][n]['name']+'</option>';
                    }
                 $('#purity_'+count).append(m);  
                 $('#purity_'+count).trigger('change');
                }
            }
        });
    
}


</script>
<script>
    
    
    
    $("body").on("keyup",".cross_wt",function(){
          var cross_wt = Number($(this).val());
          var count = $(this).data("count");
          CrossWeight();
          ProductTotal(count);
          GrandTotal();
    });
    
    $("body").on("keyup",".stone_wt",function(){
          var stone_wt = Number($(this).val());
          var count = $(this).data("count");
          StoneWeight();
          ProductTotal(count);
          GrandTotal();
    });
    
    $("body").on("keyup",".net_wt",function(){
          var net_wt = Number($(this).val());
          NetWeight();
    });
    
        
    function GrandTotal(){
          var tot=0;
          $(".total_product_amount").each(function(){
            tot+=Number($(this).val());
          });
          $("#total_amount").val(tot.toFixed(2));
          $("#total_product_amount").text(tot.toFixed(2));
          calculate();
    }
        
    function StoneAmount(){
        $(".count").each(function(){
            count=Number($(this).val());
            ProductTotal(count);
          });
    }
    function CrossWeight(){
          var tot=0;
          $(".cross_wt").each(function(){
            tot+=Number($(this).val());
          });
          $("#total_cross_weight").val(tot);
          NetWeight();

    }
    function StoneWeight(){
          var tot=0;
          $(".stone_wt").each(function(){
            tot+=Number($(this).val());
          });
          $("#total_stone_weight").val(tot);
          NetWeight();

    }
    function NetWeight(){
          var tot=0;
          $(".net_wt").each(function(){
            tot+=Number($(this).val());
          });
          $("#total_net_weight").val(tot);

    }
    $(document).on('click', '.remove_product', function() {
        $(this).closest('.porow').remove();
        CrossWeight();
        StoneWeight();
        NetWeight();
        GrandTotal();
        productCount();
    });
    
    $(document).on('keyup', '#per_stone_amount', function() {
        
    });
    
function productCount(){
    var rowCount = $('.porow').length;
    console.log("Product Row Count: "+rowCount);
    $('#total_product_items').text(rowCount);
    $('#payment_total_items').text(rowCount);
}
</script>



<script>
$(document).ready(function() {
    
    
   $('#form_details').on('submit', function(e){
                e.preventDefault();
                $('#loading-div').show();
                $('#content-div').hide();
                $("#submit").prop('disabled', true);
                var formData = new FormData(this);
                formData.append('type', 'update_purchase');
                    $.ajax({
                            url: 'apis/purchase.php',
                            type: 'post',
                            dataType: 'json',
                            data: formData,
                            cache: false,
                            contentType: false,
                            processData: false,
                            enctype: 'multipart/form-data',
                            success: function (response) {
                                console.log(response);
                                if(response['status']==200){
                                     /*alert(response['message']);*/
                                     /*$('#form_details').trigger("reset");*/
                                     $("#submit").prop('disabled', false);
                                     $('#success-div').show();
                                     $('#loading-div').hide();
                                }else{
                                    alert(response['message']);
                                    $("#submit").prop('disabled', false);
                                    $('#loading-div').hide();
                                    $('#content-div').show();
                                }
                            }
                        });
   });
});
</script>
<!-- New GST Calculation Strats Here -->
<script>
$(document).ready(function(){
    calculate();
    discountType();
    GrandTotal();
    productCount();
    
    CrossWeight();
    StoneWeight();
    NetWeight();
});







function calculate(){
        
var sub_total = parseFloat($('#total_amount').val()) || 0;
var payable=0;
var tax=$('#tax').val(); // Tax Percentage (GST %)
var tax_type=$('#tax_type').val(); // Tax Type (Inclusive, Exclusive)



if(tax==0){
    
  $('#tax_after_total').val(sub_total.toFixed(2));
  $('#grand_amount').val(sub_total.toFixed(2));
  $('#total_amount').val(sub_total.toFixed(2));
  $('#div-cgst').hide();
  $('#div-sgst').hide();
  $("#cgst_tax_amount").html(0);
  $("#sgst_tax_amount").html(0);
  $("#total_tax").html(0);
  
  //New One
  $('#cgst_tax_per_input').val(0);
  $('#total_tax_input').val(0);
  
  $('#cgst_tax_per').text(0);
  $('#sgst_tax_per').text(0);
  $('#gst_tax_per').text(0);
  
  
  
}else{

if(tax_type==1){ // Exclusive

    $('#tax_before_total').text(sub_total.toFixed(2));
    $('#tax_after_total').val((parseFloat(sub_total)+(parseFloat(sub_total)*tax/100)).toFixed(2));
    $('#grand_amount').val((parseFloat(sub_total)+(parseFloat(sub_total)*tax/100)).toFixed(2));
    
    $('#total_tax').text((sub_total*tax/100).toFixed(2));
    $('#gst_amount').val((sub_total*tax/100).toFixed(2));
    $('#total_tax_input').val((sub_total*tax/100));
    payable = sub_total+(sub_total*tax/100);
    $('#gst_tax_per').text(tax);
    $('#sgst_tax_per').text((tax/2).toFixed(2));
    $('#cgst_tax_per').text((tax/2).toFixed(2));
    $('#cgst_tax_amount').text(((sub_total*tax/100)/2).toFixed(2));
    $('#cgst_tax_per_input').val(parseFloat((sub_total*tax/100)/2).toFixed(2));
    $('#sgst_tax_amount').text(((sub_total*tax/100)/2).toFixed(2));
}else{
  
  $('#tax_before_total').text((parseFloat(sub_total)-(parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  $('#tax_after_total').val((parseFloat(sub_total)).toFixed(2));
  $('#grand_amount').val((parseFloat(sub_total)).toFixed(2));
  
  $('#total_tax').text(((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  $('#gst_amount').val(((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  $('#total_tax_input').val(((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax))).toFixed(2));
  payable = sub_total;
  $('#gst_tax_per').text(tax);
 
  $('#sgst_tax_per').text((tax/2).toFixed(2));
  $('#cgst_tax_per').text((tax/2).toFixed(2));
  
  
  var tax_sub=((parseFloat(sub_total)*parseFloat(tax))/(parseFloat(100)+parseFloat(tax)))/2;
  
  $('#cgst_tax_amount').text(parseFloat(tax_sub).toFixed(2));
  $('#cgst_tax_per_input').val(parseFloat(tax_sub).toFixed(2));
  $('#sgst_tax_amount').text(parseFloat(tax_sub).toFixed(2));
}  
 
  $('#div-cgst').show();
  $('#div-sgst').show();
  
  
  

}

var gst_type=$('#gst_type').val();

console.log('GST Type: '+gst_type);
  if(gst_type==1){
      $('#gst_type_span').text('GST');
      $('#div-cgst').show();
      $('#div-sgst').show();
  }else{
    $('#gst_type_span').text('IGST');
    $('#div-cgst').hide();
    $('#div-sgst').hide();
  }
  
  
  
  
  calculateTotal();
}
    

</script>
<!-- New GST Calculation Ends Here -->