<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['rm_id'])):
header('Location:../index.php');
endif;
$cval=0;
$grn_no=0;
$edate=date("Y-m-d"); 
$userid=$_SESSION['rm_id'];
//////////////// Get Sequence Number ----------------------- 
 $querydoc=mysqli_query($con,"select * from esequence where seq_id=5 ")or die(mysqli_error());

    while($row=mysqli_fetch_array($querydoc)){ 
    $invd_no=$row['cno'];  
    }

$loc_id=1;
?>
<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Sales Management </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>



</head>
<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
           <!-- partial -->
 <div class="main-panel">        
 <div class="content-wrapper">


     <div class="col-22 grid-margin">
     <div class="card">
          <div class="card-header bg-primary text-white">
           New Sales Order
    </div>
     <div class="card-body">
       <form class="needs-validation" novalidate method="post" action="einv_new_add.php" enctype="multipart/form-data" onsubmit="return validateForm()">                   
                   <input type="hidden" class="form-control" id="loc_id" name="loc_id" value=<?php echo $loc_id ; ?> readonly> 
                   <input type="hidden" class="form-control" id="gdate" name="gdate" value= <?php echo $edate;?> readonly >
                   <input type="hidden" class="form-control" id="invtype" name="invtype" value= 1 readonly >
                   <input type="hidden" class="form-control" id="lname" name="lname" value=<?php echo $invd_no ; ?> readonly>
                   <input type="hidden" class="form-control" id="emp_id" name="emp_id" value= 1 readonly >
                   <input type="hidden" class="form-control" id="r_id" name="r_id" value=<?php  echo $r_id; ?>>
                   <input type="hidden" class="form-control" id="note" name="note" >
                   
                   <div class="row">       
                    
                   <div class="col-md-4">
                   <div class="form-group row">
                    <label class="col-sm-3 col-form-label">Customer: </label>
                    <div class="col-sm-9">
                    <select class="form-control select2" style="width: 100%;" name="cus_id" id="cus_id" required onchange="checkVAT(this.value)">
           <option value="0">~~~Select ~~~~</option>  
          <?php            
          $querydoc=mysqli_query($con,"select *,CONCAT(`customer_master`.`cus_name`,', ',`customer_master`.`cus_code`) AS `cus`
          
          from customer_master  ")or die(mysqli_error());
          
          while($rowd=mysqli_fetch_array($querydoc)){
          ?>
          <option value="<?php echo $rowd['cus_id'];?>"><?php echo $rowd['cus'];?></option>        
          <?php }?>
          </select> 
            </div>
            </div>
            </div>
                      <div class="col-md-4">                  
                          <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Delevery To</label>  
                          <div class="col-sm-9">
                            <select class="form-control select2" style="width: 100%;" name="deliver" id="deliver" required >
                            </select> 
                          </div> 
                          </div> 
                      </div>
                      
                      
                  <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Supplier Code </label>
                          <div class="col-sm-8">
                            <input type="text" class="form-control" id="sup_code" name="sup_code" readonly >
                          </div>
                        </div>
                    </div>

            

</div> <!-------End of first form raw------->



  <div class="row">   
  
     <div class="col-md-4">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">PO</label>
                          <div class="col-sm-8">
                            <input type="text" class="form-control" id="po" name="po"  >
                          </div>
                        </div>
                    </div>
  
  
  
  <div class="col-md-4">
  <div class="form-group row">
  <label class="col-sm-3 col-form-label">VAT Invoice: </label>
  <div class="col-sm-9">
       <input type="checkbox" id="vat_invoice" name="vat_invoice" >
 </div>
</div>

</div>
               
</div> <div class="col-md-4">
   
   <button type="submit" class="btn btn-primary">Next</button>
   <button type="button" class="btn btn-light" onclick="window.location.href='home.php';">Cancel</button>
    </div>  </div> <!-------End ofsecond raw------->
       
      
          
    </form>
    </div>
     </div><!-----End of <div class="card-body">--~~~~~~---->
     </div><!-----End of <div class="card">--~~~~~~---->

 <div class="col-lg-12 grid-margin stretch-card">
                <div class="card">
                    <div class="card-body">
                        <h4>History</h4>


 <button type="button" class="btn btn-warning" onclick="document.location='home.php'"><i class="fas fa-arrow-left"></i> Go Back</button>
 <form method="post" action="#" enctype="multipart/form-data" style="display: inline;">
        <button type="submit" formaction="inv_remove_all.php" class="btn btn-danger" style="background-color: red; border-color: darkred; color: white;">
        <i class="fas fa-trash-alt"></i> Delete All
      </button>
    </form>
    <div class="table-responsive mt-3">
                            <table class="table table-hover">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>No</th>
                          <th>Date</th>
                          <th>Location</th>
                          <th>Customer</th>
                          <th>Employee</th>
                          <th>VAT</th>
                          <th>Edit/Remove</th> 
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;
$query=mysqli_query($con,"SELECT einv_temp_header.*,stock_locations.loc_name,customer_master.cus_name,employee_master.emp_name
 FROM einv_temp_header
LEFT JOIN stock_locations ON stock_locations.loc_id=inv_temp_header.loc_id
LEFT JOIN customer_master ON customer_master.cus_id=inv_temp_header.cus_id
LEFT JOIN employee_master ON employee_master.emp_id=inv_temp_header.emp_id where inv_type=1 ")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $invd_id=$row['invd_id'];
                $invd_date=$row['invd_date']; 
                $loc_name=$row['loc_name'];
                $cus_name=$row['cus_name'];
                $vat_inv=$row['vat_inv'];
                $vat_status = ($vat_inv == 1) ? "VAT" : "Non VAT";

              

 ?>
   <tr>
                         <td><?php echo $rcount;  ?></td>
                         <td><?php echo $row['invd_id'];?></td>
                         <td><?php echo $row['invd_date'];?></td>
                         <td><?php echo $row['loc_name'];?></td>
                         <td><?php echo $row['cus_name'];?></td>
                          <td><?php echo $row['emp_name'];?></td>
                           <td><?php echo $vat_status;?></td>
                         <td> <form method="post" action="#" enctype="multipart/form-data">
                            <input type="hidden" class="form-control pull-right" id="inv_no" name="inv_no"  value=" <?php echo $invd_id; ?>">
         <button type="submit" formaction="inv_new1N.php" class="btn btn-outline-secondary btn-rounded btn-icon">
                        <i class="fas fa-edit text-danger"></i>
                      </button>


            <button type="submit" formaction="inv_remove.php" class="btn btn-outline-secondary btn-rounded btn-icon">
            <i class="fas fa-trash text-danger"></i>
            </button>



                    </form></td>
 

</tr>

 <?php }  ?>

                      </tbody>
                    </table>
                  </div>
     </div><!-----End of <div class="card-body">--~~~~~~---->
     </div><!-----End of <div class="card">--~~~~~~---->
     </div><!-----End of  <div class="col-22 grid-margin">--~~~~~~---->




     </div><!-----End of   <div class="content-wrapper">--~~~~~~---->
       



 </div><!-----End of main panel--~~~~~~~~~~~~~~~~~~~~~~~--------------->
 </div><!-----Dont Delete this DIV--~~~~~~~~~~~~~~~~~~~~~~~--------------->
</div>
  <!-- container-scroller -->
<?php include('../includes/footer.php');?>
<?php include('../includes/commonjs.php');?>
 
</body>


</html>

<script>
    $( document ).ready(function() {
         // alert("Please Select Order Type");

    });

function validateForm() { 
  
    //````````Location````````````````````````````````````
   if ($('#loc_id').val() == 0){
    //if($('#loc_id').val().trim().length == 0) {
           alert("Please select the Location !!!");               
            return false;
    } ////``````````````````````````````````````````

    

    //````````Customer````````````````````````````````````
   if ($('#cus_id').val() == 0){
    //if($('#loc_id').val().trim().length == 0) {
           alert("Please select the Customer !!!");               
            return false;
    } ////


        //````````Employee````````````````````````````````````
   if ($('#emp_id').val() == 0){
    //if($('#loc_id').val().trim().length == 0) {
           alert("Please select the Employee !!!");               
            return false;
    } ////





 } ///validateForm() { 

</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
  })

</script>


<script>
function checkVAT(customerID) {
    //alert (customerID);
    if (customerID == 0) {
        $('#vat_invoice').prop('checked', false); // Uncheck the checkbox
        return;
    }

    $.ajax({
        url: 'check_vat.php',
        type: 'POST',
        data: { cus_id: customerID },
        dataType: 'json',
        success: function(response) {
            //alert(response.vat_required);
            if (response.vat_required == 1) {
                  $('#vat_invoice').prop('checked', true);
                // $('#vat_invoice').prop('checked', true).prop('disabled', true);
            } else {
                  $('#vat_invoice').prop('checked', false);
                 // $('#vat_invoice').prop('checked', false).prop('disabled', true);
            }
            
            if (response.sup_code) {
            $('#sup_code').val(response.sup_code);
                }       else {
                            $('#sup_code').val(''); // Clear the field if sup_code is not present
                            }
            
        },
        error: function(xhr, status, error) {
            console.error("Error: " + error);
        }
    });
    
    $.ajax({
    url: 'check_delivery.php',
    type: 'POST',
    data: { cus_id: customerID },
    dataType: 'json',
    success: function(response) {
       

        // Populate the <select> element (deliver)
        let select = $('#deliver');
        select.empty(); // Clear existing options

        if (response.length > 0) {
            response.forEach(function(item) {
                select.append(
                    $('<option>', {
                        value: item.delivery_id,
                        text: item.delivery_name
                    })
                );
            });
        } else {
            select.append(
                $('<option>', {
                    value: '',
                    text: 'No delivery options available'
                })
            );
        }
    },
    error: function(xhr, status, error) {
        console.error("Error: " + error);
    }
});

    
    
}
</script>


