<?php  $this->load->view('staff/common/header.php',$data);
$base_name = $data['base_name'];


$queryd11 = $this->db->query("SELECT * FROM customer_invoice where bill_no='$payid'");
  $rowd11 = $queryd11->row(); 
  
  
   $queryd1 = $this->db->query("SELECT SUM(total) AS rrt FROM customer_invoice where bill_no='$payid'");
$rowd1 = $queryd1->row(); 
$total=$rowd1->rrt;

$bill_no1=$rowd11->bill_no;
$c_id=$rowd11->customer_id;

 $queryd1 = $this->db->query("SELECT SUM(paid) AS rrt FROM customer_payment where bill_no='$payid'");
 $rowd1 = $queryd1->row(); 
 $paid=$rowd1->rrt+0;
 
 
 $queryds1 = $this->db->query("SELECT * FROM tbl_customer where cust_phone='$c_id'");
 $rowds1 = $queryds1->row(); 
?>
<link href="<?php echo base_url();?>assets/libs/select2/select2.min.css" rel="stylesheet" type="text/css" />
<script src="<?php echo base_url();?>assets/libs/select2/select2.min.js"></script>
    <div class="content">
        
        <!-- Start Content-->
        <div class="container-fluid">
            
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box">
                        <div class="page-title-right">
                            <ol class="breadcrumb m-0">
                                <li class="breadcrumb-item">Staff</li>
                                <li class="breadcrumb-item">Print Invoice</li>
                            </ol>
                        </div>
                        <h4 class="page-title text-uppercase"><?= $page;?></h4>
                    </div>
                </div>
            </div>     
            <!-- end page title --> 

         
			
			
			
			
			 <div class="row order_list" >
                <div class="ord col-md-12 col-xl-12">
                    <div class="card-box">
                        
                          <div class="row">                
              
            <div class="col-md-4" >   
             <img src="<?= base_url('uploads/website/logo/'.$data['logo']);?>" alt="" ><br>
                <?php
             echo $data['contact_address'].'<br>';
            echo  $data['contact_email'].'<br>';
          echo $data['contact_phone'].'<br>';
      
                
                ?>
			</div>		        
				   <div class="col-md-8" >      
					          <table id="key-table" class="table table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
					               <tr>
                                            <td>Name : <?= $rowds1->cust_name?></td>
                                         
                                            <td>Mobile : <?= $rowds1->cust_phone?></td>
        								</tr>    
        									<tr>
        								  <td colspan='2'> Address : <?= $rowds1->cust_address?></td>  
        								 
        								</tr>
        								<tr>
                                            <td>Bill No : <?= $payid?></td>
                                            <td>Bill Date : <?= date('d-m-Y',strtotime($rowd11->addon))?></td>
                                           
        								</tr>
					        </table>
					    </div>
           </div>
                        <div class="row" style="margin-top:10px;">
                            
                            
                            
                            
                            <div class="col-sm-12 table-responsive">
                                 <!-- <table id="key-table" class="table table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
					                   
					        </table>-->
							    <table id="key-table" class="table table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                                    <thead>
                                        <tr>
                                            <th>S.no.</th>
            								<th>Product Name</th>
            								<th>Size</th>
            								<th>Color</th>
            								 <th>Price</th>
            							    <th>qty</th>
            							    <th>Total</th>
            							 <!--   <th>Action</th>-->
                                        </tr>
                                    </thead>


                                        <tbody>
                                        
								<?php 
								$i=1;
				   $query3 = $this->db->where('user_id',$_SESSION['staff']['id']);
				   $query3 = $this->db->where('bill_no',$payid);
				   $query3 = $this->db->get('customer_invoice');
                   foreach ($query3->result() as $row)
                   {

									?>
								        <tr>
                                            <td><?= $i;?></td>
        								
                                                <td><?php
                                                $pid=$row->pid;
                                                $this->db->where('p_id',$pid); $pro = $this->db->get('tbl_product')->row_array();
                                                echo $pname=$pro['p_name'];
                                                ?></td>
                                         
                                                
                                                <td><?php
                                                $size=$row->size_id;
                                                $this->db->where('size_id',$size); $pro = $this->db->get('tbl_size')->row_array();
                                                echo $sizename=$pro['size_name'];
                                                ?></td>
                                                
                                             
                                               
                                                 <td><?php
                                                $colori=$row->color_id;
                                                if(empty($colori))
                                                {
                                                  echo $colorname='N/A';
                                                }
                                                else
                                                {
                                                $this->db->where('color_id',$colori); $pro = $this->db->get('tbl_color')->row_array();
                                                echo $colorname =$pro['color_name'];
                                                }
                                                ?></td>
                                                
                                                
                                                <td><?php echo $row->mrp;?></td>
                                                <td><?php echo  $row->qty;?></td>
                                                <td><?php echo $row->total;?></td>
                                              
                                                
                                                     
                                        
                                        <!--        
                                          <td>
        								
        									<input type="hidden"  id="pname_<?php echo $row->id;?>" value="<?= $pname?>"> 
        									<input type="hidden"  id="sizename_<?php echo $row->id;?>" value="<?= $sizename?>"> 
        									<input type="hidden"  id="colorname_<?php echo $row->id;?>" value="<?= $colorname?>"> 
        									<input type="hidden"  id="prestock_<?php echo $row->id;?>" value="<?= $prestock?>"> 
        									
        									
									         <button type="button" class="btn btn-primary btn-sm" onclick="updstock(<?php echo $row->id;?>)" data-toggle="modal" data-target="#exampleModal">
                                             <i class="far fa-edit"></i> Edit
                                             </button> 
        									</td>-->
        								</tr>
								<?php 
								$i++;
								}?>
							<tr>
							      <td colspan="6" class="text-right">Total</td>
							      <td><?php echo $total;?></td>
							</tr>
								<tr>
							      <td colspan="6" class="text-right">Paid</td>
							      <td><?php echo $paid;?></td>
							</tr>
							<tr>
							      <td colspan="6" class="text-right">Dues</td>
							      <td><?php echo $total-$paid;?></td>
							</tr>
							</tbody>
							</table>
							</div>
					    </div>
					</div>
				</div>
				<!-- /.col-md-6 -->
			</div>
			<!-- /.row -->
		</div>
		<!-- /.container-fluid -->
	</div>
	<!-- /.content -->
</div>
<!-- /.content-wrapper -->

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Update Stock</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
  <div class="row"> 
 <div class="col-md-6">
  <div class="form-group mb-2">
    <label>Product Name</label>
    <input type="text" readonly id="pname" class="form-control"  >
    <input type="hidden" readonly id="id" class="form-control"  >
  </div>
  </div>
  <div class="col-md-6">
   <div class="form-group mb-2">
    <label>Size</label>
    <input type="text" readonly id="sizename" class="form-control"  >
  </div>
   </div>
  <div class="col-md-6">
   <div class="form-group mb-2">
    <label>Color</label>
    <input type="text" readonly id="colorname" class="form-control"  >
  </div>
   </div>
  <div class="col-md-6">
   <div class="form-group mb-2">
    <label>Pre Stock</label>
    <input type="text" readonly id="prestock" class="form-control"  >
  </div>
   </div>
  <div class="col-md-6">
   <div class="form-group mb-2">
    <label>New Stock</label>
    <input type="number"  id="newstock" class="form-control"  >
  </div>
</div>  
 
  <div class="col-md-6">
       <div class="form-group mt-4">
    <button type="button" class="btn btn-primary" onclick="updstk()" data-dismiss="modal">Save changes</button>
      
      
  </div> 
  </div> 
      
    </div>  
      </div>
    
    </div>
  </div>
</div>


<script>
    function updstock(id)
    {
       a= $("#pname_"+id).val();
       b= $("#sizename_"+id).val();
       c= $("#colorname_"+id).val();
       d= $("#prestock_"+id).val();
        
        $("#id").val(id);
        $("#pname").val(a);
        $("#sizename").val(b);
        $("#colorname").val(c);
        $("#prestock").val(d);
        
        
    }
    
    
 function updstk()  
  {
       id= $("#id").val();
       prestock=$("#prestock").val(); 
       newstock=$("#newstock").val(); 
       
      if(newstock>0) 
      {
       $.post("<?php echo base_url().'master/Staff/upd_stock';?>",
          {
          id: id,prestock:prestock,newstock:newstock
              
          },
          function(data){
          if(data==0)
            {
          $('#danger').css('display','block');
          $('#success').css('display','none');
          $('#danger1').html('Stock not available');
            }
            
        
            
             if(data==1)
            {
          $('#success').css('display','block');
          $('#danger').css('display','none');
          $('#success1').html('Stock Updated Success');   
        
          $(".order_list").load(location.href + " .ord");
          
     
         
           
            }
          });
      }
      else
      {
             $('#danger').css('display','block');
          $('#success').css('display','none');
          $('#danger1').html('Enter a Valid Stock');
      }
          setTimeout(function(){ $('.msg').fadeOut(1000); }, 2000); 
  }  
    
</script>

<?php $this->load->view('staff/common/footer.php'); ?>
<script src="<?php echo base_url();?>assets/js/pages/form-advanced.init.js"></script>

<script>
 localStorage.setItem("product_size",JSON.stringify(<?php print_r($product_size);?>));
localStorage.setItem("color",JSON.stringify(<?php print_r($color);?>));
localStorage.setItem("size",JSON.stringify(<?php print_r($size_name);?>));   
</script>




<script>
 
 
 
    $('#pid').change(function(){
        var p_id1 = $(this).val();
    product_size=JSON.parse(localStorage.getItem("product_size"));
    size=JSON.parse(localStorage.getItem("size"));
    const numbers=[];
var i;
var t ='<option value="">Select Size</option>';

for (i = 0; i < product_size.length; i++) {
  text = product_size[i].size_id;
  pid = product_size[i].p_id;
 
 
 
  
  if(pid==p_id1 )
  {

for (x = 0; x < size.length; x++) {   
   
  if(size[x].size_id==text )
  {
     var l= size[x].size_name;
   
  }
      
  }
  
 
  

 

var term = text; // search term (regex pattern)
var search = new RegExp(term , 'i'); // prepare a regex object
let b = numbers.filter(item => search.test(item));
if(b!=text)
{
    t +='<option value="'+text+'">'+l+'</option>';
}
numbers.push(text);    

}




}

$('#size_id').html(t); 

 

  
 
    });
    </script>






<script>
$('#size_id').change(function(){
        var p_id = $('#pid').val();
        var size_id = $(this).val();
         product_size4=JSON.parse(localStorage.getItem("product_size"));
    


for (y = 0; y < product_size4.length; y++) {
  size_id14 = product_size4[y].size_id;
  pid14 = product_size4[y].p_id;
  p_qty4 = product_size4[y].p_qty;

  
  if(pid14==p_id && size_id==size_id14)
  {
      var qty = p_qty4;
  }
}

  
  
     
    product_color=JSON.parse(localStorage.getItem("product_size"));
    color=JSON.parse(localStorage.getItem("color"));
var i;
var t ='<option value="">Select Color</option>';


for (i = 0; i < product_color.length; i++) {
  size_id1 = product_color[i].size_id;
  pid1 = product_color[i].p_id;
  colorid = product_color[i].color_id;
  rid = product_color[i].id;
  
  if(pid1==p_id && size_id==size_id1)
  {
var tq=0;
for (x = 0; x < color.length; x++) {   
  l=''; 
    if(color[x].color_id==colorid )
  {
     var l= color[x].color_name;
      if(l!='undefined'){  
          tq++;
         
           t +='<option value="'+colorid+'">'+l+'</option>';
      }
  }
 
     
  }
  
 rid1=rid;

  }
 
}


   $('#color_id').html(t);
 if(tq==0)
 {
   $('#oqty').val(qty);   
   
$('#rid').val(rid1); 
}
else
{
   $('#oqty').val(0);    
}
    });
</script>

<script>
$('#color_id').change(function(){
        var p_id = $('#pid').val();
        var size_id = $('#size_id').val();
         var color_id = $(this).val();
        
product_size4=JSON.parse(localStorage.getItem("product_size"));
for (y = 0; y < product_size4.length; y++) {
  size_id14 = product_size4[y].size_id;
  color_id14 = product_size4[y].color_id;
  pid14 = product_size4[y].p_id;
  p_qty4 = product_size4[y].p_qty;
   rid = product_size4[y].id;
  

  
  if(pid14==p_id && size_id==size_id14 && color_id14==color_id)
  {
      var qty = p_qty4;
      rid1=rid;
      
  }
}

  $('#oqty').val(qty);   
  $('#rid').val(rid1);   
 
    });
</script>
<script>
    function add_product()
    {
        pid=$('#pid').val();
        rid=$('#rid').val();
        size=$('#size_id').val();
        color=$('#color_id').val();
        oqty=parseFloat($('#oqty').val());
        qty=parseFloat($('#qty').val());
        uid=$('#uid').val();
  
   if(pid!='' && pid!=null) 
   { 
       
   if(size!='' && size!=null) 
   {
       
      if(qty>0)
      {
       if(qty<=oqty ) 
      {
         $.post("<?php echo base_url().'master/Staff/add_product';?>",
          {
          pid: pid,size:size,color:color,oqty:oqty,qty:qty,uid:uid,rid:rid
              
          },
          function(data){
           if(data==0)
            {
          $('#danger').css('display','block');
          $('#success').css('display','none');
          $('#danger1').html('Stock not available');
            }
            
            if(data==1)
            {
          $('#success').css('display','block');
          $('#danger').css('display','none');
          $('#success1').html('Product Added Success');   
          
           $(".order_list").load(location.href + " .ord");
          
        
            let storageProducts1 = JSON.parse(localStorage.getItem('product_size'));
            let products1 = storageProducts1.filter(product1 => product1.id == rid);
            c=products1[0].p_qty=products1[0].p_qty-qty;
           
           $('#oqty').val(c);
           console.log(products1[0]);
            
            let storageProducts = JSON.parse(localStorage.getItem('product_size'));
            let products = storageProducts.filter(product2 => product2.id != rid );
            products.push(products1[0]);
            localStorage.setItem('product_size', JSON.stringify(products));
          
            }
            
             if(data==2)
            {
          $('#success').css('display','block');
          $('#danger').css('display','none');
          $('#success1').html('Product Updated Success');   
        
          $(".order_list").load(location.href + " .ord");
          
        
            let storageProducts1 = JSON.parse(localStorage.getItem('product_size'));
            let products1 = storageProducts1.filter(product1 => product1.id == rid);
            c=products1[0].p_qty=products1[0].p_qty-qty;
           
           $('#oqty').val(c);
           console.log(products1[0]);
            
            let storageProducts = JSON.parse(localStorage.getItem('product_size'));
            let products = storageProducts.filter(product2 => product2.id != rid );
            products.push(products1[0]);
            localStorage.setItem('product_size', JSON.stringify(products));
            
         
           
            }
          });
      }
      else
       {
            $('#success').css('display','none');
          $('#danger').css('display','block');
           $('#danger1').html('Stock not available');
       }
      }
       else
       {
         $('#success').css('display','none');
          $('#danger').css('display','block');
          $('#danger1').html('Please Enter Minimum Quantity');
       }
   }
       else
       {
         $('#success').css('display','none');
          $('#danger').css('display','block');
          $('#danger1').html('Please Select Size');
       }
       
   }
       else
       {
         $('#success').css('display','none');
          $('#danger').css('display','block');
          $('#danger1').html('Please Select Product');
       }
          
 setTimeout(function(){ $('.msg').fadeOut(1000); }, 2000);         
    }
</script>

