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

$saleid  = $_GET['id'];

$username=$_SESSION['username'];

$sql_uname=mysqli_query($conn,"select * from user_login where username='$username' and flag=1");
$rowuser=mysqli_fetch_array($sql_uname,MYSQLI_ASSOC);

$uname   = $rowuser['ename'];

$sql_customer = mysqli_query($conn,"SELECT id,cname FROM customer_master WHERE isdelete='1' order by cname ASC  ");
$row_customer = '';

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

}



$sql = mysqli_query($conn,"select invoice_no,payment_type,pending_tray,in_tray,out_tray,collection_amount,discount, sale_date,user, customer_id,cname, area, area_tamil,employeeid,ename,vehicleno,billing_address,cash_amount,bank_amount,del_address,notes, itemcount, tamount,pending_amount FROM saleheader WHERE id='$saleid' and status='' ");
 
$row=mysqli_fetch_array($sql,MYSQLI_ASSOC);

$invoiceID  = $row['invoice_no'];
$saleDate   = $row['sale_date'];

$area      = $row['area'];
$area_tamil=$row['area_tamil'];
$employeeid=$row['employeeid'];
$ename=$row['ename'];
$vehicleno=$row['vehicleno'];

$notes      = $row['notes'];

$cid        = $row['customer_id'];
$cname        = $row['cname'];
$payment_type=$row['payment_type'];

$billing_address=$row['billing_address'];
$del_address=$row['del_address'];
$notes=$row['notes'];
$user=$row['user'];

$pending_amount=$row['pending_amount'];
$collection_amount=$row['collection_amount'];
$discount=$row['discount'];

$pending_tray=$row['pending_tray'];
$collected_tray_eqty=$row['in_tray'];
$tray_out_qty=$row['out_tray'];

$cash_amount=$row['cash_amount'];
$bank_amount=$row['bank_amount'];



$sql_customer = mysqli_query($conn,"SELECT id,cname FROM customer_master WHERE isdelete='1' and area='$area' order by cname ASC  ");
$row_customer = '';



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

$crow = mysqli_fetch_array($customer_result,MYSQLI_ASSOC);

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

// $saleDetails_SQL ="SELECT sii.*,pm.id,pm.productname,pm.isdelete,pm.hsn_code  FROM podetails as sii LEFT JOIN product_master as pm on pm.id=sii.productID  WHERE sii.saleheader_id = '$saleid'";
$saleDetails_SQL ="SELECT sii.* ,pm.productname,pm.product_tamil FROM saledetails as sii LEFT JOIN product_master as pm on pm.id=sii.productID  WHERE sii.saleheader_id = '$saleid'  and sii.status=''";

//echo $saleDetails_SQL;

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

$sql_tray = mysqli_query($conn,"select td.invoice_no,td.id,td.tray_id,td.pending_tray,td.collected_tray_eqty,td.tray_out_qty,pm.productname FROM tray_details as td inner join product_master as pm on pm.id=td.tray_id WHERE td.sales_inv='$invoiceID' and td.status='' ");

//echo "select td.invoice_no,td.id,td.tray_id,td.pending_tray,td.collected_tray_eqty,td.tray_out_qty,pm.productname FROM tray_details as td inner join product_master as pm on pm.id=td.tray_id WHERE td.sales_inv='$invoiceID' and td.status=''";
 
$row_tray=mysqli_fetch_array($sql_tray,MYSQLI_ASSOC);

$productid=$row_tray['tray_id'];
$productnm=$row_tray['productname'];

/*$pending_tray=$row_tray['pending_tray'];
$collected_tray_eqty=$row_tray['collected_tray_eqty'];
$tray_out_qty=$row_tray['tray_out_qty'];*/
$tray_id=$row_tray['id'];

$tray_invoice=$row_tray['invoice_no'];



?>

<!DOCTYPE html>
<html>
<head>
    <title>Sales Edit</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="salesUpdate.php" method="post" onSubmit="return checkForm(this);" autocomplete="off">
<div class=" col-sm-12">
    <div class=" col-sm-3">
        <label for="inputlg">S. 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">Area</label>
      <select class="form-control input-lg   "  name="area" id="area" required>
      <option value="<?php echo $area;?>"><?php echo $area;?>/<?php echo $area_tamil;?> </option>
      <?php
	  $sql_area=mysqli_query($conn,"Select * from area_master where isdelete='1' order by areaname ASC");
	  while($row_area=mysqli_fetch_array($sql_area))
	  {
	  ?>
      <option value="<?php echo $row_area['areaname'];?>"><?php echo $row_area['areaname'];?>/<?php echo $row_area['area_tamil'];?></option>
      <?php
	  }
	  ?>
      </select>
    </div>
    
    <div class="col-sm-4">
        <label for="inputlg">Customer </label>
        
       

       <select class="form-control input-lg customerName-select  "  name="customerName">
        <option value="">Please Select the Customer </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['cname']; ?> </option>
        <?php
        }
        ?>
        </select>
    </div>
    
    <div class=" col-sm-2">
        <label for="inputlg">Date</label>
        <input class="form-control input-lg datepicker-sale" id="date" type="date" name="sales_order_date" autocomplete="off" value="<?php echo $saleDate; ?>">
    </div>   
</div>

<div class=" col-sm-12">
        
    <div class=" col-sm-3">
               <!--<label for="inputlg">Area</label>-->
       <input type="hidden" class="form-control input-lg " name="area_tamil" id="area_tamil" required value="<?php echo $area_tamil; ?>">
    </div>
    
    <!--<div class=" col-sm-3">
               <label for="inputlg">EMP&nbsp;Name</label>
       <select class="form-control input-lg  "  name="employeeid" required>
       <option value="<?php echo $employeeid; ?>"><?php echo $ename; ?></option>
        <?php
		
		if($username!='admin')
		{
		
		$sql_marketing = mysqli_query($conn,"SELECT id,ename FROM employee_master WHERE isdelete='1' and ename='$uname' order by ename ASC ");
		}
		else
		{
		$sql_marketing = mysqli_query($conn,"SELECT id,ename FROM employee_master WHERE isdelete='1'  order by ename ASC ");
		
		}
		
		while($rm=mysqli_fetch_array($sql_marketing))
		{
		?>
       
        <option value="<?php echo $rm['id'];?>"><?php echo $rm['ename'];?></option>
        
        
        <?php
		}
		?>
       </select>
    </div>
    
    <div class=" col-sm-2">
               <label for="inputlg">Vehicle.No</label>
       
       
       <select  class="form-control input-lg " name="vehicleno" required>
       <option value="<?php echo $vehicleno; ?>"><?php echo $vehicleno; ?></option>
       <?php
	   $sql_vehicle=mysqli_query($conn,"select * from vehicle_master");
	   while($row_vehicle=mysqli_fetch_array($sql_vehicle))
	   {
	   
	   ?>
       <option value="<?php echo $row_vehicle['vehicle'];?>"><?php echo $row_vehicle['vehicle'];?></option>
       <?php
	   }
	   ?>
       
       </select>
    </div>-->
    
       <div class=" col-sm-3">
        <!--<label for="inputlg">Invoice Address</label>-->
        <input type="hidden" class="form-control input-lg"  id="caddress" name="billing_address" value="<?php echo $billing_address; ?>"    >
    </div>
    
     <div class=" col-sm-3">
       <!-- <label for="inputlg">Invoice Address</label>-->
        <input type="hidden" class="form-control input-lg"  id="caddress" name="billing_address" value="<?php echo $billing_address; ?>"    >
    </div>
    
</div>

    
    <div class=" col-sm-12">
    
     &nbsp;
    </div>
    
    <div class=" col-sm-12">

<h4 style="color:red;">Payment Collection</h4>
    
     <div class=" col-sm-2">
        <label for="inputlg">Pending&nbsp;Amount</label>
        <input class="form-control input-lg total-purchase-amount"  type="text" name="pending_tamount" id="pending_tamount" autocomplete="off" readonly value="<?php echo $pending_amount; ?>" > 
       
    </div>
    
    <div class=" col-sm-2">
        <label for="inputlg">Received Amount</label>
        <input class="form-control input-lg pay-amount"  type="number" name="payamount" id="payamount" autocomplete="off" required value="<?php echo $collection_amount; ?>" >    
    </div>
    
    <div class=" col-sm-2">
        <label for="inputlg">Discount Amount</label>
        <input class="form-control input-lg "  type="number" name="discount" id="discount" autocomplete="off" required value="<?php echo $discount; ?>" > 
    </div>
    
     <div class=" col-sm-2">
        <label for="inputlg">Cash Amount</label>
        <input class="form-control input-lg "  type="number" name="cash_amount" autocomplete="off" required value="<?php echo $cash_amount; ?>"> 
    </div>
    
    <div class=" col-sm-2">
        <label for="inputlg">Bank Amount</label>
        <input class="form-control input-lg "  type="number" name="bank_amount" autocomplete="off" required value="<?php echo $bank_amount; ?>"> 
    </div>
    
    <div class=" col-sm-2">
        <label for="inputlg">Pay Type</label>
        <select class="form-control input-lg selectpicker select-payment-type" data-live-search="true" name="paymenttype"  >
            <option value="<?php echo $payment_type; ?>" ><?php echo $payment_type; ?></option>
           <option>Bank</option>
           <option>Cash</option>
            <option>Cash And Bank</option>
       <!--<option>Gpay</option>
       <option>Phonepe</option>
       
       <option>Cheque</option>
       <option>Other Upi</option>-->
          
          </select>        
    </div>
    
    
    <div class=" col-sm-12">
&nbsp;
</div>
    </div>
    
    <div class=" col-sm-12">
     <p id="availqty" align="center" style="color:#FF0000"></p>
     <div class=" col-sm-4">
        <label for="inputlg">Tray Name</label>
        <select class="form-control" name="tray_product" id="tray_product" >
           <?php
		   $sql_tray=mysqli_query($conn,"select * from product_master where product_type='Return'");
		   while($row_tray=mysqli_fetch_array($sql_tray))
		   {
		   ?>
              <option value="<?php echo  $row_tray['id'];?>"><?php echo  $row_tray['productname'];?></option>
              <?php
		   }
		   ?>
        </select>
    </div>
   
   <div class=" col-sm-2">
        <label for="inputlg">Pending Tray</label>
<input class="form-control " type="text" id="pending_tray" name="pending_tray" value="<?php echo $pending_tray;?>" required readonly>
    </div>
    
    
    
     <div class=" col-sm-2">
        <label for="inputlg">Out&nbsp;Qty</label>
        <input class="form-control" type="text" id="tray_out" name="tray_out"  value="<?php echo number_format($tray_out_qty,2,'.','');?>" required style="background:#00F4F4"> 
    </div>
    
     <div class=" col-sm-2">
        <label for="inputlg">Empty Qty</label>
<input class="form-control " type="text" id="tray_eqty" name="tray_eqty" value="<?php echo number_format($collected_tray_eqty,2,'.','');?>" required style="background:#FFA042">
<input type="hidden" name="tray_id" value="<?php echo number_format($tray_id,2,'.','');?>">
<input type="hidden" value="<?php echo $tray_invoice;?>" name="tray_invoice">
    </div>
    
    
    
   
    
    
    
    
     
    
    </div>
    
    <div class=" col-sm-12">
    &nbsp;
    </div>
    


<p id="availqty" align="center" style="color:#FF0000"></p> 
      <div class=" col-sm-12">
    <h4 style="color:red;">Sales</h4>
     <div class=" col-sm-3">
        <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">Lot.No</label>
<input class="form-control " type="text" id="lot_number">
    </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 type="text" class="form-control productrate"  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-1">
        <label for="inputlg">Action</label>
<input type="button" class="btn btn-default addProduct-sale form-control" id="addbtn" 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"  id="saleDetailsAdd">
    <thead>
      <tr style="background-color:  lightsteelblue; border: 1px solid black;">
        <!-- <th>#</th> -->
        <!-- <th>Pro code</th> -->
        <th>Pro Name</th>
        <!--<th>Lot.No</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['salenetamt']-($SDetail_row['sale_cgst']+$SDetail_row['sale_sgst']));
            
           // $gst = $gst+(($SDetail_row['sale_cgst']+$SDetail_row['sale_sgst']));


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

        
            $saleDetailsTable ='<tr id="'.$trlength.'">';
            
             $saleDetailsTable .='<td class="product_name">'.$SDetail_row['productname'].'/'.$SDetail_row['product_tamil'].'<input class="form-control productid" name="productID[]"  value="'.$SDetail_row['productID'].'" type="hidden"></td>';
			
			//$saleDetailsTable .='<td>'.$SDetail_row['lot_number'].'<input class="form-control lot_number" name="lot_number[]" type="hidden" value="'.$SDetail_row['lot_number'].'" ></td>';
            
            $saleDetailsTable .='<td>'.$SDetail_row['sale_eqty'].'<input class="form-control eqtyinput" name="eqtyinput[]" type="hidden" value="'.$SDetail_row['sale_eqty'].'" ></td>';
            
            $saleDetailsTable .='<td>'.$SDetail_row['sale_prate'].'<input class="form-control prateinput" name="prateinput[]" type="hidden" value="'.$SDetail_row['sale_prate'].'" ></td>';
            
         
            
            $saleDetailsTable .='<td>'.$SDetail_row['salenetamt'].'<input class="form-control salenetamt" name="salenetamt[]" type="hidden" value="'.$SDetail_row['salenetamt'].'" ></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" class="btn btn-default">Submit</button>
    </div>
</div>

</form>
    <?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/saleadd.js"></script>
    <!-- Include jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

<script>

  var checkForm = function(form) {

    //
    // validate form fields
    //

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

</script>


<script>
$(document).ready(function() {
  $("#addbtn").click(function(){
  var myButton = $("#addbtn");

  // Enable the button using jQuery when needed
  // For example, enable it after a certain delay (3 seconds in this case)
  setTimeout(function() {
    myButton.prop("disabled", false);
  }, 3000);
});
$(".product-select-box").change(function(){
$("#addbtn").hide();
});

$("#eqty").keyup(function(){
var qty=$(this).val();

if(qty!=0)
{

$("#addbtn").show();
}
else
{
$("#addbtn").show();
}
});

});
</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>
$(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  });
    
  //$('input[name="delivery_date"]').datepicker( "setDate", new Date()); 

});
</script>


</body>
</html>