<?php require_once('../auth/auth.php');?>
<?php
  $custom_field= mysqli_query($conn,"SELECT sales FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
  $rowcf= mysqli_fetch_assoc ($custom_field);

  $p=$rowcf['sales'];
  function input_check($ap)
  {
   if (strpos($GLOBALS['p'], $ap) !== false) { return 1;} 
  }

    $invoice = $_GET['invoice'];
    $order= mysqli_query($conn,"SELECT i.imei_status,i.pro_id,i.item_name,i.item_details,i.item_code,i.min_sales,o.t_amount,o.pro_unit,o.unit_val,o.eoid,o.qty,o.avg_price,o.net_amount,o.amount,o.dis_amount,o.order_type FROM order_list_edit o LEFT JOIN item i ON(o.pro_id=i.pro_id) WHERE o.coid='$coid' AND o.bid='$bid' AND o.brid='$brid' AND o.invoice='$invoice' AND o.active=0 ORDER BY eoid ASC") or die(mysqli_error($conn));
    $i=0;
    $sm_qty=0;
    $sm_disc=0;
    $sm_total=0;
    $avg_total = 0;
    while($orw= mysqli_fetch_assoc($order)){
        $pro_unit  = $orw['pro_unit'];
        $imei_status = $orw['imei_status'];
        $pro_id      = $orw['pro_id'];
        $order_type   = $orw['order_type'];
        $unit_val  = $orw['unit_val'];
        $sm_qty    += $orw['qty'];
        $sl_total  = $orw['net_amount'];
        $sm_total  += $sl_total;
    ?>

<tr>
    <td class="text-center"><?php echo ++$i;?></td>
    <td title="Avarage price Per Unit : <?php echo $orw['avg_price'];?>"><?php echo $orw['item_name']." ".$orw['item_details']." ".$orw['item_code'];?>
    </td>
    
    <?php if ($imei_status==1) {
         $count_row =mysqli_fetch_assoc(mysqli_query($conn,"SELECT  COUNT(*) as count_row FROM imei_tracker_edit 
       WHERE coid='$coid' AND bid='$bid' AND invoice='$invoice'  "))['count_row'];
    ?>
        <td onclick="imei_list_sale(<?php echo $pro_id; ?>)"><input type="number" class="form-control text-center form-control-sm inputselect sl_qty" value="<?php echo number_format($orw['qty'],$decimalitem);?>" iddd="<?php echo $orw['eoid'];?>" c_name="qty" step="0.001" min="0.001" required="true" readonly="true"></td>
        <td class="<?php if(input_check('u_type') !==1){echo 'd-none';}?>">
          <select iddd="<?php echo $orw['eoid'];?>" c_name="pro_unit" class="form-control select2" style="width: 100%;" required="true">
            <option value='<?php echo $pro_unit;?>' convert='<?php echo $unit_val;?>'>
                <?php echo mysqli_fetch_assoc(mysqli_query($conn,"SELECT unit_name FROM pro_unit WHERE coid='$coid' AND puid='$pro_unit' AND default_set='primary' LIMIt 1"))['unit_name'];
                ?>
            </option>
           <?php 
             $iut= mysqli_query($conn,"SELECT puid,unit_name,unit_convart FROM pro_unit WHERE coid='$coid' AND default_set='primary' ORDER BY puid DESC") or die(mysqli_error($conn));
                while($it= mysqli_fetch_assoc($iut)){
                echo '<option value='.$it["puid"].' convert='.$it["unit_convart"].'>'.$it["unit_name"].'</option>';
              }?>   
          </select>
        </td>
    <?php }else{ ?>
        <td><input type="number" class="form-control text-center form-control-sm inputselect sl_qty" value="<?php echo number_format($orw['qty'],$decimalitem);?>" iddd="<?php echo $orw['eoid'];?>" c_name="qty" step="0.001" min="0.001" required="true"></td>
        <td class="<?php if(input_check('u_type') !==1){echo 'd-none';}?>">
              <select iddd="<?php echo $orw['eoid'];?>" c_name="pro_unit" class="form-control select2" style="width: 100%;" required="true">
                <option value='<?php echo $pro_unit;?>' convert='<?php echo $unit_val;?>'>
                    <?php echo mysqli_fetch_assoc(mysqli_query($conn,"SELECT unit_name FROM pro_unit WHERE coid='$coid' AND puid='$pro_unit' LIMIt 1"))['unit_name'];
                    ?>
                </option>
               <?php 
                 $iut= mysqli_query($conn,"SELECT puid,unit_name,unit_convart FROM pro_unit WHERE coid='$coid' ORDER BY puid DESC") or die(mysqli_error($conn));
                    while($it= mysqli_fetch_assoc($iut)){
                    echo '<option value='.$it["puid"].' convert='.$it["unit_convart"].'>'.$it["unit_name"].'</option>';
                  }?>   
              </select>
        </td>
    <?php } ?>
    
    <script>
 function imei_list_sale(productid){    
   $("#modal-xl").modal({backdrop: "static"});
    pagecall('../imei_tracking/sales_imei_index.php?pro_id='+productid+'&invoice='+'<?php echo $invoice; ?>'+'&eoid='+'<?php echo $orw['eoid'];?>'+'&order_type='+'<?php echo $orw['order_type'];?>','#xlmdview');   
   } 
</script>


    
    
    
    

    	<td>
         <input type="number" class="form-control text-center form-control-sm inputselect sl_price" value="<?php echo $orw['amount'];?>" iddd="<?php echo $orw['eoid'];?>" c_name="amount" step="0.001" min="<?php echo $orw['min_sales'];?>" required="true">  
         <?php if(input_check('s_calculator') ==1){ ?>
		<input type="text" step="0.001"  class="form-control text-center form-control-sm text-white  salcal "  iddd="<?php echo $orw['eoid'];?>" placeholder="Start Calculator" style="width:100% ;background:#8d323a;"> 
     	<?php } ?>
	</td>
   <script type="text/javascript">
    $(document).ready(function() {
    	$('.salcal').abacus();
       $(".salcal").bind("keypress", function(e) {
          if (e.keyCode == 13) {
             return false;
          }
       });
    });
</script> 
    <td><input type="number" class="form-control text-center form-control-sm inputselect sl_disc" required="true" value="<?php echo $orw['dis_amount'];?>" step="0.001"  iddd="<?php echo $orw['eoid'];?>" c_name="dis_amount"></td>
    <td class="text-right" style="display:none"><dt id="sl_netp"><?php echo $orw['t_amount'];?></dt></td>
    <td class="text-right">
        <dt id="sl_total" class="sl_total"><?php echo $sl_total;?></dt>
    </td>
    <?php if ($imei_status==1 && $count_row ==0) { ?>
       <td class="text-center" onclick="odelete(<?php echo $orw['eoid'];?>)"><i class="fas fa-trash"></i></td>
    <?php }else if ($imei_status==1 && $count_row > 0){ ?>
       <td class="text-center" title="Please frist Remove Imei Code Then Delete"><i class="fas fa-close"></i></td>
    <?php }else{  ?>
       <td class="text-center" onclick="odelete(<?php echo $orw['eoid'];?>)"><i class="fas fa-trash"></i></td>
    <?php }  ?>
</tr>
<?php } ?>

    <tr>
       <td class="text-right" colspan="2"><dt><?php echo language(140);?> = </dt></td>
       <td class="text-center"><dt id="slsm_qty"><?php echo $sm_qty;?></dt></td>
       <td class="text-right <?php if(input_check('u_type') !==1){echo 'd-none';}?>"></td>
       <td class="text-right"></td>
       <td class="text-right"></td>
       <td class="text-right"><dt id="slsm_total"><?php echo $sm_total;?></dt></td>
       <td class="text-center"></td>
    </tr>

<script>
 $(document).ready(function() {
  $('select').select2();
  $('#sl_totalc<?php echo $invoice;?>').val(<?php echo $sm_total;?>);
  slblcal();
  $("[iddd]").on('input change', function(){    
   var iddd    = $(this).attr("iddd");
   var c_name  = $(this).attr("c_name");
   var c_value = Number($(this).val());
   var targt   = $(this).parent().parent();

        $.post("../sales/order_list_update.php",{iddd : iddd,c_name:c_name,c_value:c_value});
        var sl_qty   = Number(targt.find('.sl_qty').val());
        var sl_price = Number(targt.find('.sl_price').val());
        var sl_disc  = Number(targt.find('.sl_disc').val());
        var convert  = Number(targt.find(':selected').attr('convert'));

          if(!isNaN(sl_qty) && !isNaN(sl_price) && !isNaN(convert)){
              targt.find('#sl_netp').html(Number((sl_price*sl_qty*convert)));
              targt.find('#sl_total').html(Number((sl_price*sl_qty*convert)-sl_disc));
          }
          else
          {
              targt.find('#sl_netp').html('');
              targt.find('#sl_total').html('');
          }
          var qty_sum=0;
          $(".sl_qty").each(function(){
            qty_sum += +Number($(this).val());
          });
          $("#slsm_qty").html(qty_sum);
              
          var amtotal=0;
          $(".sl_total").each(function(){
              amtotal += +Number($(this).text());
          });


          $('#slsm_total').html(amtotal);
          $('#sl_totalc<?php echo $invoice;?>').val(amtotal);
          slblcal();
   });  
 });    
</script>

<script>
    function odelete(itemid) {
        $.post("../sales/order_list_delete.php",
            {item_id : itemid,oinvoice:'<?php echo $invoice;?>'},
         function(data){
          pagecall('../sales/order_list.php?invoice=<?php echo $invoice?>','#row_show<?php echo $invoice;?>',0);
        });     
    }
</script>