<?php include("parent/callMe.php"); ?>
<?php  include("sessioncheck.php"); ?>
<?php

 $branch=$_SESSION['branch'];
    
     $session1;
      if($_REQUEST['session']!=NULL){

       $session1=$_REQUEST['session'];
     }else {
       $session1=$_SESSION['current_session'];
     }

    if($_REQUEST['student']!=NULL){

       $m->where("status", '0');
       $m->where("branch_id", $_SESSION['branch']);
       $m->where("student_id", $_REQUEST['student']);
       $WorkAssignListVal = $m->get("edu_student_details");

      }
     // print_r($WorkAssignListVal)

      $m->where("bill_id", $_REQUEST['id']);
      $m->where("branch_id", $_SESSION['branch']);
      $billConfig=$m->get(" eduportal_canteen_salebills_config");
 
      

  // settings month details

    $default;
    
    if($_GET["month"]!=NULL ){
      $default= $_GET["month"];
    }
    else{
      $default= date("m");
    }
  
      $m->where("Month_Id", $default);
      $month_name=$m->get("eduportal_month_config");

    $date;
    
    if($_GET["id"]!=NULL ){

      $date= $billConfig[0]["invoice_date"];

    }

    else {

      $date= date("Y-m-d");
    }
 
     ?>

<!DOCTYPE html>
<html lang="en">
  <head>
    
    <title>Canteen Invoice | EduPortal</title>
    <?php include('RootMe/css_links.php');?>

<style>

.thStyle{background-color:gray;color:white;}

tr th {padding:1.5%;border:1px solid white;}
tr td {padding:1%;}

@media only screen and (max-width: 768px) {

/* For mobile phones: */
.popup_style{margin-top:20%;}

}
</style>   
  </head>
  
<script>
function showResult1(str) {
  if (str.length==0) { 
    document.getElementById("livesearch1").innerHTML="";
    document.getElementById("livesearch1").style.border="0px";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else {  // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (this.readyState==4 && this.status==200) {
      document.getElementById("livesearch1").innerHTML=this.responseText;
      document.getElementById("livesearch1").style.border="1px solid #A5ACB2";
      //alert(this.responseText);
    }
  }
  xmlhttp.open("GET","live_search_revenue.php?q="+str,true);
  xmlhttp.send();
}



function getProductDetails(str,row)
{
   // alert(str+" === "+row); 
   var ajx;  // The variable that makes Ajax possible!

  if(window.XMLHttpRequest)
  ajx=new XMLHttpRequest();
  ajx.onreadystatechange = function()
  {

    if(ajx.readyState == 4)
    {

          var vars=ajx.responseText.split(",");
         // alert(ajx.responseText);
         /*document.getElementById("product_id"+row).value= vars[0];*/
         //document.getElementById("stock"+row).value= vars[1];
         document.getElementById("cost_price"+row).value= vars[2];
         document.getElementById("sale_price"+row).value= vars[3];      
        // alert(vars[3]); 
         
        
            
    }
  }
  ajx.open("GET", "get_productDetails.php?id="+str);
  ajx.send(); 
}


function getComboDetails(str)
    {
     // alert(studentid+"=="+cell_no);

   

        var ajx;  // The variable that makes Ajax possible!

        if (window.XMLHttpRequest)
            ajx = new XMLHttpRequest();
        ajx.onreadystatechange = function ()
        {
          //alert("786");
            if (ajx.readyState == 4)
            {
               // alert(ajx.responseText);  
                document.getElementById("myID").innerHTML = ajx.responseText;
                document.getElementById("tableNone").style.display = "none";

            }
        }
        
        ajx.open("GET", "get_combo_details.php?combo_type="+str);
        ajx.send();

    }




function CalculateGrossAmount(qty,row){

//alert(qty+" === "+row); 
  var rate=document.getElementById("sale_price"+row).value;
   
  if(rate=="" || rate==null){
    alert("Please select product!!!");
  }
else {

  document.getElementById("balance"+row).value=Number(rate)*Number(qty);
  

}

CalculateTotalAmount(qty,row);

}


function CalculateTotalAmount(qty,row){

//alert(qty+" === "+row); 

var productAmount=document.getElementsByName("balance[]");

var Tamount=0;
for(i=0;i<productAmount.length;i++){


Tamount= parseInt(Tamount)+ parseInt(productAmount[i].value);

}

document.getElementById("total_amount").value=Tamount; 
document.getElementById("net_total").value=Tamount;

 
/*alert(balance);*/
  
 
}

function DiscountAmount(disc){

  var totalNet=parseInt(document.getElementById("total_amount").value)-parseInt(disc);

  document.getElementById("net_total").value= totalNet;
}

</script>

<script type="text/javascript">

function HideDate(str){
  
  if(str=="1"){
    
    document.getElementById("studentName").style.display="block";
    document.getElementById("col3").style.display="block";
   
  }
  else {
    
    document.getElementById("studentName").style.display="none";
    document.getElementById("col3").style.display="none";
   
   
  }
}

</script>


 <!-- jQuery -->
    <script src="js/ajax_functions.js"></script>  
  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
        <div class="col-md-3 left_col">
     
            <?php include('RootMe/sidebar.php');?>

              <?php include('RootMe/header.php');?>
     
        <!-- page content -->
          <div class="right_col" role="main">
            <a href="<?php echo $_SESSION['dashboard'];?>">Dashboard </a> &nbsp; <i style="" class="fa fa-angle-double-right"></i>  &nbsp; 
            Add Create Sale Bill
          <br><br>
          <div class="" style="background-color: white;">
           
<section class="add_sub_agent" id="table_sub_agent">
  <div class="container">

 <div class="" style="margin-top:10px;">
      
        <div class="panel panel-default">
             <div class="panel-heading" id="std_section">
      

                 
          



<div class="panel-body">

<div class="container">
  <div class="row">

   <div class="col-md-12" style="background-color: white;">

      <!-- Nav tabs -->
      <div class="card">
             

                <!-- Tab panes -->
                <div class="tab-content">





<div >
<form class="form-style-7" action="<?php echo DOMAIN.AgentDirectory;?>parent/InsertMe.php" method="post" enctype="multipart/form-data">

  <?php if(!isset($_GET['id'])){?> 
  
       <input type="hidden" name="action" value="AddCanteenSaleBill" />
       <input type="hidden" name="student" value="<?php echo $_REQUEST['student'];?>" />
       <input type="hidden" name="class_id" value="<?php echo $WorkAssignListVal[0]['class_id'];?>" />
 <?php } else { ?>

        <input type="hidden" name="action" value="Update_CanteenSaleBill" />
        <input type="hidden" name="update_id"  value="<?php echo $_GET['id'];?>">

<?php } ?>
   



<h3 style="text-align:left;margin-top:2%;margin-left:1.3%;"> Create Sale Bill</h3>

  <!-- <a href="pos_datewise_discount_report.php" class="btn btn-success" style="float:right;margin-top:-39px;">Datewise Discount Report</a>
  
  <a href="pos_datewise_commitment_report.php" class="btn btn-success" style="float:right;margin-top:-39px;">Datewise Commitment Report</a> -->

  <a href="view_canteen_salesbill.php" class="btn btn-success" style="float:right;margin-top:-39px;">View Sale Bill List</a>

  <a href="add_combo.php" class="btn btn-success" style="float:right;margin-top:-39px;">Combo Sale</a>
        <div class="row">


                  <div class="col-md-3 col-xs-6" style="padding: 8px;">

                     <div class="form-group ">

                        <label class="required">Select Customer</label>
                            <select name="student_id" id="student_id" class="chosen-multi-select inputfield form-control" required >
                    <option value="">Select Customer</option>

                    <?php

                    $session123=$_SESSION['current_session'];
                    $branch123=$_SESSION['branch'];

                    $sql = "SELECT student_id, first_name, last_name, class_id, section_id, session_id, ref_no, candidate_roll_no, family_code, cell_no FROM  edu_student_details  WHERE branch_id='$branch123' and status='0' and session_id='$session123' ";
                    
                      $WorkAssignList = $m->query($sql);

                    foreach($WorkAssignList as $WorkAssignListVal)

                    { 
                    
                    $ClassID=$WorkAssignListVal['class_id'];

                    $SectionID=$WorkAssignListVal['section_id'];

                    $sqlcls = "SELECT * FROM  edu_class_config  WHERE branch_id='$branch123' and class_id='$ClassID' ";

                    $ClassName = $m->query($sqlcls);


                    $sqlsec = "SELECT * FROM  edu_adm_section_detail  WHERE branch_id='$branch123' and class_id='$ClassID'  and section_id='$SectionID'";

                    $SectionName = $m->query($sqlsec);


                      ?>
                     
                       <option <?php if($billConfig[0]['student_id']==$WorkAssignListVal['student_id']){echo "selected";} ?> value="<?php echo $WorkAssignListVal['student_id']; ?>"><?php echo $WorkAssignListVal['first_name']." | ".$WorkAssignListVal['last_name']." | ".$ClassName[0]['Class_Name']." | ".$WorkAssignListVal['family_code']; ?></option>

                            <?php
                    } ?>

                      <option value="Other">Other</option>

                </select>
                        </div>


                </div>


                

               
               
                   <div class="col-md-2 col-xs-6" style="padding: 8px;">

                     <div class="form-group ">

                        <label class="required">Select Warehouse</label>
                              <select name="warehouse_id" id="warehouse_id" class="chosen-multi-select inputfield form-control" >
                    <option value="">Select Warehouse</option>

                    <?php
                   
                    $branch123=$_SESSION['branch'];

                    $sql = "SELECT * FROM  edu_warehouse_config  WHERE branch_id='$branch123' ";
                    
                      $WorkAssignList = $m->query($sql);

                    foreach($WorkAssignList as $warehouse)

                    {                 
                       ?>
                     
                       <option <?php if($billConfig[0]['warehouse_id']==$warehouse['warehouse_id']){echo "selected";} ?> value="<?php echo $warehouse['warehouse_id']; ?>"><?php echo $warehouse['wharehouse_name']; ?></option>

                            <?php
                    } ?>

                </select>
                        </div>


                </div> 

               <div class="col-md-2 col-xs-6" style="padding: 8px;">
               <div class="form-group ">

                  <label class="required"> Invoice Date</label>
                      <input name="date" id="date" value="<?php echo $date;?>" type="date" class="form-control"  required>      
                  </div>
              </div>

              <div class="col-md-2 col-xs-6" style="padding: 8px;">

                     <div class="form-group ">

                        <label class="required">Select Combo</label>
                              <select name="warehouse_id" id="warehouse_id" class="chosen-multi-select inputfield form-control" onChange="getComboDetails(this.value)" >
                    <option value="">Select Combo</option>

                    <?php
                   
                    $branchID=$_SESSION['branch'];

                    $sql = "SELECT * FROM  eduportal_canteen_cmbo  WHERE branch_id='$branchID' ";
                    
                      $WorkAssignList = $m->query($sql);

                    foreach($WorkAssignList as $combo)

                    {                 
                       ?>
                     
                       <option <?php if($_REQUEST['student']==$combo['combo_id']){echo "selected";} ?> value="<?php echo $combo['combo_id']; ?>"><?php echo $combo['combo_type']; ?></option>

                            <?php
                    } ?>

                </select>
                        </div>


                </div> 

              <div class="col-md-3 col-xs-6" style="padding: 8px;margin-top: 22px;">
             
              <input  class="add-row btn btn-success" name="pronam" type="button" value="Add Row" />

              <input class="delete-row btn btn-danger" name="pronam" type="button" value="Delete Row" / >
           

              </div>

               

      </div>  <!-- ending row-->     

        <div class="row">

    <div id="myID">


    </div>
    <div id="tableNone">     
    <table id="mytable" class="table table-striped table-bordered"style="background-color: white;" >
                      <thead>
                        <tr>

                            <th width="3%">Del</th>
                            <th width="30%">  Product Details  </th>
                            <th width="15%">  Quantity  </th>
                            <th width="15%">  Cost Price  </th>
                            <th width="15%">  Sale Price  </th>                     
                            <th width="15%">  Balance  </th>
                                   
                          </tr>
                      </thead>
                       <tbody>

<?php
 $stock; 
 for ($i=0;$i<1;$i++){ 

// now getting order details to update 

if(isset($_GET['id'])){

  $qry="select * from eduportal_canteen_salebills_details where branch_id='$branch' and bill_id =".$_GET['id']." order by bill_detail_id asc";
  $Data = $m->query($qry);
//print_r($qry);
// getting product details 
}

?>    

<?php if (!isset($_GET['id'])) { ?>
  

          
          <tr>

            <td></td>
            <td id="pro_row">   
            <select name="product_id[]" id="product_id" class="form-control" onChange="getProductDetails(this.value,<?php echo $i;?>)">

            <option value="">Select Product </option>                

                <?php
                $m->where("branch_id", $_SESSION['branch']);
                $WorkAssignList = $m->get("eduportal_product_config");
                foreach($WorkAssignList as $WorkAssignListVal)
                { ?>

                   <option <?php if($Data[0]['product_id']==$WorkAssignListVal['product_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['product_id']; ?>"><?php echo $WorkAssignListVal['product_name']; ?></option>

                <?php } ?>

            </select>                             
              
            </td>
            
            <input type="hidden" name="stock[]" id="stock<?php echo $i;?>" value="<?php echo $stock; ?>" />

            <td> <input class="form-control" onKeyUp="CalculateGrossAmount(this.value,<?php echo $i;?>)"  value="<?php echo $Data[0]['amount']; ?>" min="0" name="quantity[]" 
            id="quantity<?php echo $i;?>" type="text" /></td>


            <td>  
             <input class="form-control" name="cost_price[]"
             id="cost_price<?php echo $i;?>"  value="<?php echo $Data[0]['remarks']; ?>"  type="text" />           

             </td>

             <td>  

             <input class="form-control" name="sale_price[]"
             id="sale_price<?php echo $i;?>"  value="<?php echo $Data[0]['remarks']; ?>"  type="text" />            

             </td>           

             <td>  

             <input class="form-control" name="balance[]"
             id="balance<?php echo $i;?>"  value="<?php echo $Data[0]['remarks']; ?>"  type="text" />            

             </td>
            
            
          </tr>


           

 <?php } else { 


    foreach($Data as $DataSaleBill)
      {

  
//echo $DataSaleBill['product_id'];

?>
                <tr>

            
            <td id="pro_row">   
             <select name="product_id[]" id="product_id" class="form-control" onChange="getProductDetails(this.value,<?php echo $i;?>)">

            <option value="">Select Product </option>                

                <?php
                $m->where("branch_id", $_SESSION['branch']);
                $WorkAssignList = $m->get("eduportal_product_config");
                foreach($WorkAssignList as $WorkAssignListVal)
                { ?>

                   <option <?php if($DataSaleBill['product_id']==$WorkAssignListVal['product_id']){echo 'selected';}?> value="<?php echo $WorkAssignListVal['product_id']; ?>"><?php echo $WorkAssignListVal['product_name']; ?></option>

                <?php } ?>

            </select>  
                            
              
              </td>
            

            <td> <input class="form-control" onKeyUp="CalculateGrossAmount(this.value,<?php echo $i;?>)"  value="<?php echo $DataSaleBill['product_qty']; ?>" min="0" name="quantity[]" 
            id="quantity<?php echo $i;?>" type="text" /></td>


            <td>  
             <input class="form-control" name="cost_price[]"
             id="cost_price<?php echo $i;?>"  value="<?php echo $DataSaleBill['cost_price']; ?>"  type="text" />           

             </td>

             <td>  

             <input class="form-control" name="sale_price[]"
             id="sale_price<?php echo $i;?>"  value="<?php echo $DataSaleBill['pro_rate']; ?>"  type="text" />            

             </td>           

             <td>  

             <input class="form-control" name="balance[]"
             id="balance<?php echo $i;?>"  value="<?php echo $DataSaleBill['net_total']; ?>"  type="text" />            

             </td>
            
            
          </tr>



<?php  
$stock="";
 } } } ?>    


   </tbody>

                    </table>

<div class="col-md-12">

<div class="col-md-3">
<div class="form-group ">

<label class=""> Commitment Date</label>
<input name="commitment_date" id="date" value="" type="date" class="form-control">      
</div>
</div>


<div class="col-md-9">
<div class="form-group ">

<label class=""> Commitment Remarks</label>
<input name="commitment_remarks" id="date" value="" type="text" class="form-control">      
</div>
</div>

</div>

    <div class="col-md-3 col-sm-3 col-xs-12 pull-right">
                      
           <div class="item form-group ">
                 <label>Net Total</label>
                 <input  name="net_total" value="<?php echo $billConfig[0]['total_amount'];?>" readonly id="net_total" class="form-control pull-right">
             
           </div>

       </div>

        <div class="col-md-3 col-sm-3 col-xs-12 pull-right">

           
           <div class="item form-group ">
                 <label>Total Discount</label>
                 <input  name="total_discount" onKeyUp="DiscountAmount(this.value)" id="total_discount" value="<?php echo $billConfig[0]['discount_amount'];?>" class="form-control pull-right">
             
           </div>

       </div>

        <div class="col-md-3 col-sm-3 col-xs-12 pull-right">
         
           <div class="item form-group ">
                 <label>Total Amount</label>
                 <input  name="total_amount" value="<?php echo $billConfig[0]['grossamount'];?>" readonly id="total_amount" class="form-control pull-right">
             
           </div>

       </div>

                  </div>

       <br><br><br><br>             

                    <input  type="submit"style="font-size:18px;margin-top: 29px;float: right;" name="submit"  value="Save" class="btn btn-success">

                    <input  type="submit"style="font-size:18px;margin-top: 29px;float: right;" name="print"  value="Print & Save" class="btn btn-success">
    </div>   <!--  ending 12 column-->


    </form>
</div>

          

             </table>

</div>


                    </div>
                    



                </div>
      

      </div>

       </div> <!-- col 9 -->
  </div>
</div>  <!-- container -->


    <br><br>

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

            </div>
          </div>
        </div>
        <!-- /page content -->
      
  <?php include('RootMe/js_linksscript.php');?>

    
    <script type="text/javascript">
        $(document).ready(function(){
            $(".add-row").click(function(){
                
                var pro;

                var AddRow=document.getElementsByName("quantity[]").length;
                var AddRow=AddRow+0;
//alert(AddRow);
                $.ajax({url: "get_products.php?id="+AddRow,success: function(result){
                    
                    //alert(result);
                    
                var markup = "<tr><td><input type='checkbox' style='width: 20px; height: 20px;' class='check' name='record'></td><td>"+result+"</td><td><input class='form-control' name='quantity[]' onKeyUp='CalculateGrossAmount(this.value,"+AddRow+")' id='quantity"+AddRow+"' type='number' /></td><td><input class='form-control' name='cost_price[]' id='cost_price"+AddRow+"' type='text' /></td><td><input class='form-control' name='sale_price[]' id='sale_price"+AddRow+"' type='text' /></td><td><input class='form-control' name='balance[]' id='balance"+AddRow+"' type='text' /></td></tr>";
                $("#mytable tbody").append(markup);
              
              
                  }});


              /*  var name = $("#name").val();
                var email = $("#email").val();
              */
              
            });
            
            // Find and remove selected table rows
            $(".delete-row").click(function(){
                $("table tbody").find('input[name="record"]').each(function(){
                  if($(this).is(":checked")){
                        $(this).parents("tr").remove();
                    }
                });
            });
        });    
    </script>


    <!-- searchable dropdown -->
    
    <link href="autosearch_select/select2.css" rel="stylesheet">
    <link href="autosearch_select/select2-default.css" rel="stylesheet">
    <script src="autosearch_select/select2.min.js"></script>
    
   <script>
    $(function() {
    $('.chosen-select').select2( { width:'100%', maximumSelectionSize: 1 } );
    $(document).ready(function() {
    $(".chosen-multi-select").select2( { width:'100%', } ); }); }); function slideout(){ setTimeout(function(){
    $(".alert-success").fadeOut("slow", function () { });
    $(".alert-danger").fadeOut("slow", function () { }); }, 4000);}
    </script>
  </body>
</html>
