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

$queryd11 = $this->db->query("SELECT * FROM customer_invoice1 where bill_no='$payid'");
  $rowd11 = $queryd11->row(); 
  

   $queryd1 = $this->db->query("SELECT SUM(total) AS rrt FROM customer_invoice1 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();

$bill_no=$payid;
?>
<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>

<style>
    .bg-ss:nth-child(2n)
    {
        background-color:white;
    }
    
    .form-group {
    margin: 2px 0;
}

.card{
    margin-bottom:3px;
}


.form-control
{
    height:28px;
}



</style>
    <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">Invoice</li>
                                
                            </ol>
                        </div>
                        <h4 class="page-title text-uppercase"><?= $page;?></h4>
                    </div>
                </div>
            </div>     
            <!-- end page title --> 

            <div class="row">
                <div class="col-md-12 col-xl-12">
                    <div class="card-box">
                        
                        
                          <form action="<?php echo base_url().'master/invoice';?>?payid=<?= $bill_no?>" method="post">
                                 
                        <div class="row">
                            <div class="col-sm-12">
 
 <div class="alert alert-success col-12 msg" id="success" role="alert" style="display:none;">
  <i class="fa fa-check-circle" aria-hidden="true"></i> <b>Success !! </b> <b id="success1"></b>
</div>

<div class="alert alert-danger col-12 msg" id="danger" role="alert" style="display:none;">
 <i class="fa fa-exclamation-triangle" aria-hidden="true"></i> <b>Error !! </b>  <b id="danger1"></b>
</div> 
                                
                                 <div class="row"> 
                                  
                                  
                              <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Customer ID <span class="text-danger">*</span></label>
                                                <input name="cid"  value="<?= $rowds1->cust_id?>" type="text" id="cid" required="" class="form-control" >
                                                   
                                            </div>
                                        </div> 
                                        
                                        
                                        
                                        <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Mobile <span class="text-danger">*</span></label>
                                                <input name="phone"  value="<?= $rowds1->cust_phone?>" type="text" id="phone" required="" class="form-control" >
                                                   
                                            </div>
                                        </div>
                                        
                                        
                                        
                                     <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Name <span class="text-danger">*</span></label>
                                                <input name="full_name"  value="<?= $rowds1->cust_name?>"  type="text" id="full_name" required="" class="form-control" >
                                              
                                                
                                                <input  id="l1"   type="hidden" class="form-control" >
                                                <input  id="l2"  type="hidden" class="form-control" >
                                                <input  id="l3"  type="hidden" class="form-control" >
                                                   
                                            </div>
                                        </div>
                                 
                                     <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Address <span class="text-danger">*</span></label>
                                                <input name="address"  value="<?= $rowds1->cust_address?>" type="text" id="address" required="" class="form-control" >
                                                   
                                            </div>
                                        </div>
                                 
                                   
                                        
                                        
                                     <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Date <span class="text-danger">*</span></label>
                                                <input name="date"  type="date" value="<?= date('Y-m-d')?>"  id="date" required="" class="form-control" >
                                                   
                                            </div>
                                        </div>
                                        
                                        
                                       
                                        
                                               <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Invoice No. <span class="text-danger">*</span></label>
                                                <input name="bill_no" id="bill_nos" readonly value="<?= $bill_no?>"  type="text"  required="" class="form-control" >
                                               
                                                   
                                            </div>
                                        </div>
                                   </div>
                                   
               
                                   
                                      <div class="row p-1 mt-1" > 
                                      
                                      

                                     <div class="col-sm-3">
                                            <div class="form-group">
                                                <label for="mcat_id">Product <span class="text-danger">*</span></label>
                                              <select class="form-control" data-toggle="select2" name="pidn" id="pid">
                                                 <option value="">Select Product</option>    
                                                  <?php 
                                                  foreach($product as $row)
                                                  {
                   
                                            ?>
                                                  <option value="<?php echo $row['p_id'];?>"><?php echo $row['p_name'];?></option>    
                                              <?php  }  
                                                  
                                                  ?>  
                                                </select>
                                              
                                              
                                                   
                                            </div>
                                        </div>
                                 
                                   <!--style="display:none;"-->
                                        <div class="col-sm-2 col-6"  id="size">
                                            <div class="form-group">
                                                <label for="mcat_id">Size <span class="text-danger">*</span></label>
                                              <select class="form-control" data-toggle="select2"  name="size_idn" id="size_id">
                                                 <option value="">Select Size</option>
                                                
                                                </select>
                                            </div>
                                        </div> 
                                        
                                        
                                         <div class="col-sm-2 col-6"  id="color">
                                            <div class="form-group">
                                                <label for="mcat_id">Color </label>
                                              <select  class="form-control" data-toggle="select2"  bane="color_idn" id="color_id">
                                                 <option value="">Select Color</option>
                                                
                                                </select>
                                            </div>
                                        </div> 
                                        
                                        
                                         <div class="col-sm-1 col-6"  id="color">
                                            <div class="form-group">
                                                <label for="mcat_id">MRP </label>
                                              <input type="text"  class="form-control"  value="0"  name="mrpn" id="mrp">
                                            </div>
                                        </div> 
                                        
                                        
                                        <div class="col-sm-2 col-6"  id="color" >
                                            <div class="form-group">
                                                <label for="mcat_id">Qty <span class="text-danger">*</span></label>
                                              
                                            
                                            
                                            
                                            
   <div class="input-group mb-3">
  <div class="input-group-prepend" style="width:50%;">
   <input style="height:28px;" type="number" value="0" class="form-control text-center" readonly name="oqtyn" id="oqty">
  </div>

  <input style="height:28px;" type="number"  class="form-control text-center" onkeyup="calculate()" placeholder="QTY" name="qtyn" id="qty">
  <input style="height:28px;" type="hidden"  class="form-control text-center"  id="rid">
</div>   
                                            </div>
                                        </div> 
                                        
                                  
                                  
                                  
                                  
                                     <div class="col-sm-2 col-12"  id="total" >
                                            <div class="form-group">
                                                <label for="mcat_id">Total <span class="text-danger">*</span></label>
                                              
                                            
                                            
                                            
                                            
   <div class="input-group mb-3">
  <div class="input-group-prepend" style="width:60%;">
   <input style="height:28px;" type="number" value="0" class="form-control text-center" readonly bane="total" id="totals">
  </div>

    <a href="javascript:void(0)" onclick="add_products()" style="width:40%;" class="btn btn-success btn-sm"   bane="submit" id="submit">Add</a>
                                              

</div>   
                                            </div>
                                        </div> 
                                        
                                        
                                     
                                    
                                   </div>
                                  
            </div>
					    </div> 
					  
					    <div class="row">
                            <div class="col-sm-12">
					     <div class="parent" id="valueadd">
					         
	
	<?php
	$t=0;
	$ts=0;
	$this->db->where('bill_no',$bill_no);
	$query3 = $this->db->get('customer_invoice1');
    foreach ($query3->result() as $row)
    {
	
          $this->load->model('Common_model');
          $table2 = 'tbl_product';
		  $column2['p_id'] = $row->pid;
		  $product_data = $this->Common_model->Result($column2,$table2); 
		  
		  
	
          $table21 = 'tbl_size';
		  $column21['size_id'] = $row->size_id;
		  $size_data = $this->Common_model->Result($column21,$table21); 
		  
		  
		  $table211 = 'tbl_color';
		  $column211['color_id'] = $row->color_id;
		  $color_data = $this->Common_model->Result($column211,$table211); 
		  
		  
		  $table2111 = 'tbl_product_size';
		  $column2111['id'] = $row->tps;
		  $stock_data = $this->Common_model->Result($column2111,$table2111); 
	?>
					         
		<div class='card bg-ss py-2 px-1 '  id='ss<?php echo $t; ?>'>
          <div class='row'>
              
         <div class='col-sm-3 col-6' ><div class='form-group'><input type='text' value='<?php echo $product_data['p_name'];?>' readonly class='form-control'   name='p_name'> <input type='hidden' value='<?php echo $row->pid;?>' id='pid<?php echo $t; ?>' readonly class='form-control'   name='pid[]'> </div></div>   
          <div class='col-sm-2 col-6' ><div class='form-group'><input type='text' value='<?php echo $size_data['size_name'];?>' readonly class='form-control'   name='size_name'><input type='hidden' value='<?php echo $row->size_id;?>' id='size<?php echo $t; ?>' readonly class='form-control'   name='size[]'> </div></div>   
          <div class='col-sm-2 col-6' ><div class='form-group'><input type='text' value='<?php if(!empty($color_data['color_name'])) {echo $color_data['color_name'];}else { echo 'NA';}?>' readonly class='form-control'   name='color_name'> <input type='hidden' value='<?php echo $row->color_id;?>' id='color<?php echo $t; ?>' readonly class='form-control'   name='color[]'></div></div>   
          <div class='col-sm-1 col-6' ><div class='form-group'><input type='text' value='<?php echo $row->mrp;?>' id='mrp<?php echo $t; ?>' readonly class='form-control'   name='mrp[]'></div></div>    
          <div class='col-sm-2 col-6' ><div class='form-group'><input type='text' value='<?php echo $row->qty;?>' id='qty<?php echo $t; ?>'  readonly class='form-control text-center'   name='qty[]'><input type='hidden'  value='<?php echo $stock_data['p_qty'];?>' id='oqty<?php echo $t; ?>' readonly class='form-control'   name='oqty[]'></div></div>    
          <div class='col-sm-2 col-6' ><div class='input-group mb-3'><div class='input-group mb-3'><div class='input-group-prepend' style='width:59%;'><input type='text' value='<?php echo $row->total;?>' id='totals<?php echo $t; ?>' readonly class='form-control text-center'   name='totals[]'><input type='hidden'  value='<?php echo $row->user_id;?>' readonly class='form-control'   name='uid[]'><input type='hidden'  value='<?php echo $row->tps;?>' id='rid<?php echo $t; ?>' readonly class='form-control'   name='rid[]'></div><a href='javascript:void(0)' onclick='del(<?php echo $t; ?>)' style='width:40%;' class='btn btn-danger btn-sm  btnp' >Dell</a></div></div></div></div></div>   
       <?php 
       
       $t=$t+1;
       $ts+=$row->total;
    
    
    
    $queryd111 = $this->db->query("SELECT * FROM customer_invoice_list1 where bill_no='$payid'");
     $rowd111 = $queryd111->row();
 
 
 
       } ?>    
					         
					     </div>    
					    
					    
					    </div>
					    </div>
					    
					    <div class="row bg-light"> 
                                  
                                  
                                   <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Total Amount <span class="text-danger">*</span></label>
                                                <input name="tamt1" readonly value="<?php echo $ts;?>"  type="text" id="tamt" required="" class="form-control" >
                                                
                                                   
                                            </div>
                                        </div>
                                        
                                        
                                   <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Discount (Rs) <span class="text-danger">*</span></label>
                                                <input name="dis"  value="<?php echo $rowd111->dis ?>"  type="text" id="pamt" required="" class="form-control" >
                                                
                                                   
                                            </div>
                                        </div>
                                        
                                   <div class="col-sm-2">
                                            <div class="form-group">
                                                <label for="mcat_id">Bill Amount <span class="text-danger">*</span></label>
                                                <input name="tamt" readonly value="<?php echo $ts-$rowd111->dis;?>"  type="text" id="tdue" required="" class="form-control" >
                                                
                                                   
                                            </div>
                                        </div>
                                        
                                        
                                  <div class="col-sm-2" style="display:none;">
                                            <div class="form-group">
                                                <label for="mcat_id">Paid <span class="text-danger">*</span></label>
                                                <input name="pamt"  type="text"  required="" value="0"  class="form-control" >
                                                   
                                            </div>
                                        </div>
                                        
                                        
                                        <div class="col-sm-2" style="display:none;">
                                            <div class="form-group">
                                                <label for="mcat_id">Dues Amount <span class="text-danger">*</span></label>
                                                <input name="tdue" readonly type="text"  value="0"  required="" class="form-control" >
                                                   
                                            </div>
                                        </div> 
                                   
                                   
                                   <div class="col-sm-2" style="display:none;">
                                            <div class="form-group">
                                               <label for="mcat_id">Pay Mode <span class="text-danger">*</span></label>
                                              <select  class="form-control" data-toggle="select2"   name="mode" id="mode">
                                                 <option value="1">Cash</option>
                                                 <option value="2">Cheque</option>
                                                 <option value="2">Online</option>
                                                
                                                </select>
                                            </div>
                                        </div> 
                                        
                                        <div class="col-sm-2" style="display:none;" id="utr1">
                                            <div class="form-group">
                                                <label for="mcat_id">UTR/Cheque Number</label>
                                                <input name="utr"  type="text" id="utr" class="form-control" >
                                                   
                                            </div>
                                        </div>  
                                     
                                     
                                     
                                     
                                      <div class="col-sm-2">
                                            <div class="form-group">
                                                 <center>   <input type="submit" style="width:100%;margin-top:27px;"  class="btn btn-success btn-sm"   value="Make Invoice"></center> 
                                                </div>
                                                </div>
                                   </div>
                                   
					    </form>
					</div>
				</div>
				<!-- /.col-md-6 -->
			</div>
			
			
			
			
	
			<!-- /.row -->
		</div>
		<!-- /.container-fluid -->
	</div>
	<!-- /.content -->
</div>
<!-- /.content-wrapper -->




<?php $this->load->view('master/common/footer2.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("product_size1",JSON.stringify(<?php print_r($product_size1);?>));
localStorage.setItem("color",JSON.stringify(<?php print_r($color);?>));
localStorage.setItem("size",JSON.stringify(<?php print_r($size_name);?>));   
localStorage.setItem("customer",JSON.stringify(<?php print_r($customer);?>));   

</script>

<script>

$('#mode').change(function(){
        var mode = $(this).val();
      if(mode==1)
      {
         $('#utr1').css('display','none'); 
      }
      else
      {
           $('#utr1').css('display','block'); 
      }
        
});




$('#phone').keyup(function(){
        var phone = $(this).val();
    
     customer=JSON.parse(localStorage.getItem("customer"));
    
    for (y = 0; y < customer.length; y++) {
  size_id14 = customer[y].size_id;
  mob = customer[y].cust_phone;
  cid = customer[y].cust_id;
  name = customer[y].cust_name;
  address = customer[y].cust_address;

  
  if(mob==phone)
  {
      $('#full_name').val(name);
      $('#cid').val(cid);
      $('#address').val(address);
      break;
  }
  if(mob!=phone)
  {
 
      $('#full_name').val('');
      $('#cid').val('');
      $('#address').val('');
  }
}
        
});




$('#cid').keyup(function(){
        var phone = $(this).val();
    
     customer=JSON.parse(localStorage.getItem("customer"));
    
    for (y = 0; y < customer.length; y++) {
  size_id14 = customer[y].size_id;
  mob = customer[y].cust_id;
  name = customer[y].cust_name;
  ph = customer[y].cust_phone;
  address = customer[y].cust_address;

  
  if(mob==phone)
  {
      $('#full_name').val(name);
      $('#phone').val(ph);
      $('#address').val(address);
      break;
  }
  if(mob!=phone)
  {
 
      $('#full_name').val('');
      $('#phone').val('');
      $('#address').val('');
  }
}
        
});

</script>



<script>
  

	$("#pamt").keyup(function(){
		var pd=$("#pamt").val();
		var gt=$("#tamt").val();
		var x='0';
		
		var q=parseFloat(pd);
		var s=parseFloat(gt);
		var rest=s-q;
		if(q>s)
		{
	
			$("#tdue").val(gt);
				$("#pamt").val(x);
		 $('#success').css('display','none');
          $('#danger').css('display','block');
          $('#danger1').html('You input wrong value');
		}
		else
		{
	if(pd=='')
   {	
		$("#pamt").val(x);
		$("#tdue").val(gt);
		
   }		

else
{
		$("#tdue").val(rest);
}		
		}
		
		
		 setTimeout(function(){ $('.msg').fadeOut(1000); }, 2000); 
	});

</script>
<script>
    function calculate()
    {
        
       mrp= parseFloat($('#mrp').val());
       qty= parseFloat($('#qty').val());
       totals= mrp*qty;
        
        $('#totals').val(totals);
    }
</script>


<script>
$('#pid').change(function(){
        var p_id1 = $(this).val();
       var selectedText = $("#pid option:selected").html();
       $('#l1').val(selectedText);
  
     
    product_size=JSON.parse(localStorage.getItem("product_size1"));
    size=JSON.parse(localStorage.getItem("size"));
    const numbers=[];
var i;
var t ='<option value="">Select Size</option>';
 
 console.log(t);
for (i = 0; i < product_size.length; i++) {
  text = product_size[i].size_id;
  pid = product_size[i].p_id;

//alert(pid);     
  
  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); 

 

 $('#oqty').val(0);   
  $('#rid').val(0); 
  $('#mrp').val(0);  
  $('#totals').val(0);  
  $('#qty').val('');  
  
  var t1 ='<option value="">Select Color</option>';
  $('#color_id').html(t1);
    });
    </script>






<script>
$('#size_id').change(function(){
        var p_id = $('#pid').val();
        var size_id = $(this).val();
         product_size4=JSON.parse(localStorage.getItem("product_size"));
         product_size41=JSON.parse(localStorage.getItem("product_size1"));
    
     var selectedText = $("#size_id option:selected").html();
       $('#l2').val(selectedText);


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

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

  
 for (y1 = 0; y1 < product_size41.length; y1++) {
  size_id141 = product_size41[y1].size_id;
  pid141 = product_size41[y1].p_id;
  mrps = product_size41[y1].p_current_price;


  
  if(pid141==p_id && size_id==size_id141)
  {
      var mrp = mrps;
  }
} 
     
    product_color=JSON.parse(localStorage.getItem("product_size1"));
    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);   
   $('#mrp').val(mrp);   
   
$('#rid').val(rid1); 
}
else
{
   $('#oqty').val(0); 
   $('#mrp').val(0);  
}
    });
</script>




<script>
$('#color_id').change(function(){
        var p_id = $('#pid').val();
        var size_id = $('#size_id').val();
         var color_id = $(this).val();
         
           var selectedText = $("#color_id option:selected").html();
       $('#l3').val(selectedText);
        
product_size4=JSON.parse(localStorage.getItem("product_size1"));
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;
      
  }
}


product_size41=JSON.parse(localStorage.getItem("product_size1"));
 for (y1 = 0; y1 < product_size41.length; y1++) {
  size_id141 = product_size41[y1].size_id;
  pid141 = product_size41[y1].p_id;
  mrps = product_size41[y1].p_current_price;


  
  if(pid141==p_id && size_id==size_id141)
  {
      var mrp = mrps;
  }
} 
  





  $('#oqty').val(qty);   
  $('#rid').val(rid1); 
  $('#mrp').val(mrp);   
 
    });
</script>














<script>
    function add_products()
    {
        
      full_name=$('#full_name').val();
        address=$('#address').val();
        phone=$('#phone').val();    
        bill_no=$('#bill_nos').val();    
        
   
     
     if(full_name==''|| address=='' || phone=='')
     {
          $('#success').css('display','none');
          $('#danger').css('display','block');
          $('#danger1').html('Invalid Customer Details');
       }
    
     else
     {
        
        pid=$('#pid').val();
        pid1=$('#pid').html();
        rid=$('#rid').val();
        
       
        
        mrp=parseFloat($('#mrp').val());
        tptals=parseFloat($('#totals').val());
         qty=parseFloat($('#qty').val());
         
        size=$('#size_id').val();
        color=$('#color_id').val();
        oqty=parseFloat($('#oqty').val());
        
        
    
       
        uid='no';
        l1=$('#l1').val();
        l2=$('#l2').val();
        l3=$('#l3').val();
  
   if(pid!='' && pid!=null) 
   { 
       
   if(size!='' && size!=null) 
   {
       
      if(qty>0)
      {
       if(qty<=oqty ) 
      {
          
       var count = $('.parent').find('.row').length;
       ds=0;
       
       for (y = 0; y < count; y++) {
  
       rids=$('#rid'+y).val();
       
       if(rids==rid)
       {
           ds=1;
           aa=y;
           break;
       }
       } 
 
 
 
 
  

     
    
     
    if(ds==0)
     {
          var a1  = "<div class='card bg-ss py-2 px-1 '  id='ss"+count+"'>";
          var a2  = "<div class='row' >";
         a="<div class='col-sm-3 col-6' ><div class='form-group'><input type='text' value='"+l1+"' readonly class='form-control'   name='p_name'> <input type='hidden' value='"+pid+"' id='pid"+count+"' readonly class='form-control'   name='pid[]'> </div></div>";   
          b="<div class='col-sm-2 col-6' ><div class='form-group'><input type='text' value='"+l2+"' readonly class='form-control'   name='size_name'><input type='hidden' value='"+size+"' id='size"+count+"' readonly class='form-control'   name='size[]'> </div></div>";   
          c="<div class='col-sm-2 col-6' ><div class='form-group'><input type='text' value='"+l3+"' readonly class='form-control'   name='color_name'> <input type='hidden' value='"+color+"' id='color"+count+"' readonly class='form-control'   name='color[]'></div></div>";    
          d="<div class='col-sm-1 col-6' ><div class='form-group'><input type='text' value='"+mrp+"' id='mrp"+count+"' readonly class='form-control'   name='mrp[]'></div></div>";    
          e="<div class='col-sm-2 col-6' ><div class='form-group'><input type='text' value='"+qty+"' id='qty"+count+"'  readonly class='form-control text-center'   name='qty[]'><input type='hidden'  value='"+oqty+"' id='oqty"+count+"' readonly class='form-control'   name='oqty[]'></div></div>";    
          f="<div class='col-sm-2 col-6' ><div class='input-group mb-3'><div class='input-group mb-3'><div class='input-group-prepend' style='width:59%;'><input type='text' value='"+totals+"' id='totals"+count+"' readonly class='form-control text-center'   name='totals[]'><input type='hidden'  value='"+uid+"' readonly class='form-control'   name='uid[]'><input type='hidden'  value='"+rid+"' id='rid"+count+"' readonly class='form-control'   name='rid[]'></div><a href='javascript:void(0)' onclick='del("+count+")' style='width:40%;' class='btn btn-danger btn-sm  btnp' >Dell</a></div></div></div></div></div>";    
          var row = a1+a2+a+b+c+d+e+f;   
          $("#valueadd").append(row); 
          
          $.post("<?php echo base_url().'master/invoice1/dels1_invoice';?>",
       {
           phone: phone,bill_no:bill_no,pid:pid,size_id:size,color_id:color,mrp:mrp,qty:qty,total:tptals,tps:rid
       },
 function(data){
  // alert(data); 
     
 });
          
     
  tamt=parseFloat($('#tamt').val());
       $('#tamt').val(tamt+tptals)
       
        tdue=parseFloat($('#tdue').val());
       $('#tdue').val(tdue+tptals)  
     
      let storageProducts1 = JSON.parse(localStorage.getItem('product_size1'));
            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]);
         
         
         
     }
     else
     {
        qtyr=parseFloat($('#qty'+aa).val()); 
        mrpr=parseFloat($('#mrp'+aa).val()); 
        totalsr=parseFloat($('#totals'+aa).val()); 
        
        
        
        $('#qty'+aa).val(qtyr+qty);
        //$('#mrp'+aa).val(mrpr+mrp)
        $('#totals'+aa).val(tptals+totalsr)
    
 if(mrpr==mrp)   
  {  
         
      $.post("<?php echo base_url().'master/invoice1/dels1_invoice';?>",
       {
           phone: phone,bill_no:bill_no,pid:pid,size_id:size,color_id:color,mrp:mrp,qty:qty,total:tptals,tps:rid
       },
 function(data){
  // alert(data); 
     
 });
 
 
   tamt=parseFloat($('#tamt').val());
       $('#tamt').val(tamt+tptals)   
       
       
        tdue=parseFloat($('#tdue').val());
       $('#tdue').val(tdue+tptals)  
     
      let storageProducts1 = JSON.parse(localStorage.getItem('product_size1'));
            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]);
  }
  else
  {
          $('#success').css('display','none');
          $('#danger').css('display','block');
           $('#danger1').html('Pre MRP Amount Not Matched'); 
           
           
           
  }
     }
     
       
           /* 
            let storageProducts = JSON.parse(localStorage.getItem('product_size'));
            let products = storageProducts.filter(product2 => product2.id != rid );
            products.push(products1[0]);
            localStorage.setItem('product_size1', 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);         
    }
    
    
    
    
  function del(id)
  {
      
        bill_no=$('#bill_nos').val();
        
        
        
        pid=$('#pid'+id).val();
      
        rid=$('#rid'+id).val();
        size=$('#size_id'+id).val();
        color=$('#color_id'+id).val();
        oqty=parseFloat($('#oqty'+id).val());
        qty=parseFloat($('#qty'+id).val());
      totalsr=parseFloat($('#totals'+id).val()); 
      
            let storageProducts1 = JSON.parse(localStorage.getItem('product_size1'));
            let products1 = storageProducts1.filter(product1 => product1.id == rid);
            c=products1[0].p_qty=parseFloat(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_size1', JSON.stringify(products));
            
     
      tamt=parseFloat($('#tamt').val());
       $('#tamt').val(tamt-totalsr)  
     
     
      $.post("<?php echo base_url().'master/invoice1/dels_invoice';?>",
       {
           id: bill_no,rid:rid,pid:pid
       },
 function(data, status){
   //   alert(data);
    //$("#category").html(data);
 });
  
     
       
            
      $("#ss"+id).remove();
  }
    
    
    
</script>





