<?php 
require_once("config/configDB.php"); 
require "userPermission.php";
require "financial_year_calculation.php";

$accountyear    = $financialYearStart."-".$financialYearEnd;
$sql_customer = mysqli_query($conn,"SELECT id,sname FROM supplier_master WHERE isdelete='1'  order by sname ASC ");
$row_customer = '';

$sql_employee = mysqli_query($conn,"SELECT id,employeename FROM employee_master WHERE isdelete='1' order by ename ASC ");
$row_employee = '';

$sqlproduct = mysqli_query($conn,"SELECT id, productname,product_tamil FROM product_master WHERE isdelete='1'  order by productname ASC ");

$sql_product = '';

while($prow = mysqli_fetch_array($sqlproduct)){

    $sql_product =$sql_product."<option value='".$prow['id']."'>".$prow['productname'].'/'.$prow['product_tamil']."</option>";

}


$saleid  = $_GET['id'];

$sql = mysqli_query($conn,"SELECT * FROM purchaseheader WHERE id='$saleid' and status='' ");
 
$row=mysqli_fetch_array($sql,MYSQLI_ASSOC);

$invoiceID  = $row['invoice_no'];
$purchase_date   = $row['purchase_date'];
$supplier_invoice      = $row['supplier_invoice'];
$supplier_invoice_date      = $row['supplier_invoice_date'];
$cid        = $row['supplier_id'];
//$po_number=$row['po_number'];


$remark=$row['remark'];
$user=$row['user'];




$customer_result = mysqli_query($conn,"SELECT id,saddress,area,pincode  FROM supplier_master WHERE id='$cid' ");

$crow = mysqli_fetch_array($customer_result,MYSQLI_ASSOC);

$cusomerAddress = $crow['saddress'].",".$crow['area']."-".$crow['pincode'];

date_default_timezone_set('Asia/Kolkata');
$currentTime = date( 'h:i:s A', time () );

$saleDetails_SQL ="SELECT sii.*,pm.id,pm.productname,pm.hsn_code  FROM purchasedetails as sii LEFT JOIN product_master as pm on pm.id=sii.productID  WHERE sii.purchaseheader_id = '$saleid'";

$saleDetails_Result = mysqli_query($conn,$saleDetails_SQL); 
?>

<!DOCTYPE html>
<html>
<head>
    <title>Purchase Add</title>
    <?php require 'header.php' ?>
    <style>
    
    .table-grid{
        display: grid;
        width:95%;
        max-width: 95vw;
        margin:0px auto;
        height:100px;
        background-color: sandybrown;
        grid-template-columns: 200px repeat(8, auto) 90px;
    }
    .each-section{
        background-color: aqua;
        display:grid;
        grid-template-rows: 50% 50%;
    }
    .each-section div{
        background-color: white;
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 0px 10px;
        border: 1px solid #ccc;
    }
    .section-label{
        font-weight:700;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size:14px;
       
    }
    .section-input{
        background-color: #eee !important;
    }
    
    @media screen and (max-width:720px){
        .table-grid{
            display: flex;
            flex-direction: column;
            height:auto;
        }
        .each-section{
            grid-template-rows: 100%;
            grid-template-columns: 40% 60%;
            padding: 0px 0px;
        }
        .each-section div{
            padding: 10px;
        }
        .section-input input[type=button]{
            width:80%;
        }
    }
</style>


    
</head>
<body>
    <?php require 'menu.php' ?>
<div class="container-fluid">
<form class="form-horizontal" action="purchaseUpdate.php" method="post" onSubmit="return checkForm(this);" autocomplete="off">

<p align="center" id="msg" style="color:red;"></p>

<div class=" col-sm-12">
    <div class=" col-sm-3">
        <label for="inputlg">Invoice No</label>
        <input class="form-control input-lg" type="text" name="saleinvoice" value="<?php echo $invoiceID; ?>" readonly>
         <input type="hidden" value='<?php echo $saleid; ?>' name="edit_id">
         <input type="hidden" value='<?php echo $user; ?>' name="user">
    </div>
    <div class=" col-sm-3">
        <label for="inputlg">Date</label>
        <input class="form-control input-lg datepicker-sale" id="date" type="text" name="saledate" autocomplete="off" value="<?php echo $purchase_date;?>">
       
    </div>
    
    <div class="col-sm-4">
        <label for="inputlg">Supplier </label>

         <select class="form-control input-lg customerName-select selectpicker" data-show-subtext="true" data-live-search="true" name="customerName" id="customerName-select" required>
        <option value="">Please Select the Supplier </option>
         <?php
        while($row = mysqli_fetch_array($sql_customer)){
        ?>
        <option value="<?php echo $row['id'] ?>" <?php if( $cid == $row['id'] ): ?> selected="selected"<?php endif; ?> > <?php echo $row['sname']; ?> </option>
        <?php
        }
        ?>
        </select>
    </div>
    </div>
    <div class=" col-sm-12">
    
   
    
     <div class=" col-sm-3">
        <label for="inputlg">Supplier.InvNo</label>

        <input class="form-control input-lg" type="text" name="supplier_invoice" value="<?php echo $supplier_invoice;?>">
    </div>
    
     <div class=" col-sm-3">
        <label for="inputlg">Supplier.Inv Date</label>

        <input class="form-control input-lg" type="date" name="supplier_invoice_date" value="<?php echo $supplier_invoice_date;?>">
    </div>
     
</div>



<div class=" col-sm-12">
    
     <div class=" col-sm-6">
        <label for="inputlg">Supplier Address</label>
        <textarea class="form-control input-lg" type="text" id="caddress" ><?php echo $cusomerAddress;?></textarea>
    </div>
    
    <div class=" col-sm-6">
        <label for="inputlg">Remarks</label>
        <textarea class="form-control input-lg" type="text" name="remark" ><?php echo $remark;?></textarea>
    </div>
   
    </div>
<div class=" col-sm-12">
 <br><br>
 </div>



<div class=" col-sm-12">
    
     <div class=" col-sm-4">
        <label for="inputlg">Pro Name</label>
        <select class="form-control product-select-box " data-show-subtext="true" data-live-search="true" name="product" id="product-select-box">
            <option value="">Select the Product </option>
            <?php echo  $sql_product; ?>
        </select>
    </div>
   
    
     <div class=" col-sm-2">
        <label for="inputlg">Qty</label>
<input class="form-control eqty-input" type="text" id="eqty">
    </div>
    
    
    
    <div class=" col-sm-2">
        <label for="inputlg">Rate</label>
        <input class="form-control productrate" type="text" id="prate">
    </div>
    
    
    
    
     <div class=" col-sm-2">
     <label for="inputlg">Net AMT</label>
<input class="form-control " type="text" id="netamt" readonly>
    </div>
    <div class=" col-sm-2">
        <label for="inputlg">Action</label>
<input type="button" class="btn btn-default addProduct-sale form-control" value="Add">
    </div>
    </div>



<div class="container-fluid">
                                                                                       
  <div class="col-sm-12  table-responsive" style="padding-top: 25px;margin-top: 30px;">          
  <table class="table  table-striped table-bordered dt-responsive" style="overflow-x:auto;" id="saleDetailsAdd">
    <thead>
      <tr style="background-color:  lightsteelblue; border: 1px solid black;">
        <!-- <th>#</th> -->
        <!-- <th>Pro code</th> -->
        <th>Pro Name</th>
        
        <th>Qty</th>
        <th>Rate</th>
       
        <th>Net amt</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
    <?php
        $trlength = 0;
        $amount =   0;
        $gst    =   0;
        $nettotal = 0;
        while($SDetail_row = mysqli_fetch_array($saleDetails_Result))
        {

            $saleDetailsTable = '';

            $trlength++;
           // $amount = $amount+($SDetail_row['purchase_eqty']*($SDetail_row['purchase_prate']));
            
            //$gst = $gst+(($SDetail_row['purchase_cgst']+$SDetail_row['purchase_sgst']));


            $nettotal = $nettotal+($SDetail_row['purchasenetamt']);

        
            $saleDetailsTable ='<tr id="'.$trlength.'">';
            
            $saleDetailsTable .='<td class="product_name">'.$SDetail_row['productname'].'<input class="form-control productid" name="productID[]"  value="'.$SDetail_row['id'].'" type="hidden"></td>';
            
            $saleDetailsTable .='<td>'.$SDetail_row['purchase_eqty'].'<input class="form-control eqtyinput" name="eqtyinput[]" type="hidden" value="'.$SDetail_row['purchase_eqty'].'" ></td>';
            
            $saleDetailsTable .='<td>'.$SDetail_row['purchase_prate'].'<input class="form-control prateinput" name="prateinput[]" type="hidden" value="'.$SDetail_row['purchase_prate'].'" ></td>';
            
            //$saleDetailsTable .='<td>'.$SDetail_row['purchase_gst'].'<input class="form-control gstinput" name="gstinput[]" type="hidden" value="'.$SDetail_row['purchase_gst'].'" ></td>';
            
            //$saleDetailsTable .='<td>'.($SDetail_row['purchase_eqty']*($SDetail_row['purchase_prate'])).'<input class="form-control amtinput" name="amtinput[]" type="hidden" value="'.($SDetail_row['purchase_eqty']*($SDetail_row['purchase_prate'])).'" ></td>';
            
           // $saleDetailsTable .='<td>'.$SDetail_row['purchase_sgst'].'+'.$SDetail_row['purchase_sgst'].'<input class="form-control cgstinput" name="cgstinput[]" type="hidden" value="'.$SDetail_row['purchase_cgst'].'" ><input class="form-control sgstinput" name="sgstinput[]" type="hidden" value="'.$SDetail_row['purchase_sgst'].'" ></td>';
            
            $saleDetailsTable .='<td>'.$SDetail_row['purchasenetamt'].'<input class="form-control salenetamt" name="salenetamt[]" type="hidden" value="'.$SDetail_row['purchasenetamt'].'" ></td>';
            
            $saleDetailsTable .='<td><input type="button" class="btn btn-default removeProductDetails" id="removeProductDetails" value="Delete"></td>';
            $saleDetailsTable .='</tr>';

            echo $saleDetailsTable;
    ?>


        
    <?php } ?>  
    </tbody>
    <tfoot>
        <tr>
            <th></th>
            <th></th>
            <th>Total</th>
          
            <th class="saleT-NetAmt"><?php echo $nettotal; ?><input class="form-control saleT-NetAmt" name="totalNetamt" type="hidden" value="<?php echo $nettotal; ?>" ></th>
            <th></th>
        </tr>
        </tfoot>
  </table>
  </div>
</div>

<div class="form-group">        
    <div class="col-sm-12 text-center">
        <button type="submit" name="myButton" id="myButton" class="btn btn-default ">Submit</button>
    </div>
</div>

</form>
</div>
    <?php require 'footer.php' ?>
    <script type="text/javascript">var sql_product = "<?= $sql_product ?>";</script>
    <script type="text/javascript">var base_url = "<?= $base_url ?>";</script>
    <script type="text/javascript" language="javascript" src="js/purchaseadd.js"></script>
    <!-- Include jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

<!-- Include Date Range Picker -->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.1/js/bootstrap-datepicker.min.js"></script>
<script>

  var checkForm = function(form) {

    //
    // validate form fields
    //

    form.myButton.disabled = true;
    return true;
  };


</script>

    <script>
	$(document).ready(function(){
		var date_input=$('input[name="saledate"]'); //our date input has the name "date"
		var container=$('form >  .datepicker-sale').length>0 ? $('form > .datepicker-sale ').parent() : "body";
		date_input.datepicker({
			format: 'yyyy-mm-dd',
			container: container,
			todayHighlight: true,
			autoclose: true,
        });

        
        

	});
</script>


 
    

</body>
</html>