<?php require_once('../auth/auth.php');require_once('../auth/function.php'); ?>
<section class="content">
  <div class="container-fluid">
    <div class="row">
     <div class="col-md-12">
       <div class="card card-primary card-outline direct-chat direct-chat-primary shadow-none" style="border-left:1px solid #969c9d;border-right:1px solid #969c9d;border-bottom:1px solid #969c9d;">
         <div class="card-header" style="padding: 5px 8px;"><h3 class="card-title">New Item Add</h3> <div class="card-tools"> 
          <span class="mark1" onclick="copy(this);" title="Copy Invoice"><?php echo $invoice = invoice("STO");?></span>
          <button type="button" class="btn btn-tool" data-card-widget="remove"> <i class="fas fa-times"></i>  </button> </div>
        </div>
        <form id="item_form" action="#" method="post" autocomplete="off">
          <input type="hidden" name="newitem" id="newitem" value="0"/>
          <input type="hidden" name="op_invoice" value="<?php echo $invoice;?>">
          <script type="text/javascript">
            $('.product,.service,.rawmatrials,.assest').hide();
            function changeFunc(selectedValue){
              if (selectedValue == "P"){$('.product').show();} else {$('.product').hide();}
              if (selectedValue == "S"){$('.service').show();}else{ $('.service').hide();}
              if (selectedValue == "R"){ $('.rawmatrials').show();}else{$('.rawmatrials').hide();}
              if (selectedValue == "A"){$('.assest').show(); }else{ $('.assest').hide();}
            }

            function changeFunc_wa(){
              var war_type = document.getElementById("war_type");
              var selectedValue = war_type.options[war_type.selectedIndex].value;
                if (selectedValue == "D") {$('#Warranty').show();} else { $('#Warranty').hide();}
                if (selectedValue == "M") {$('#Warranty1').show();} else { $('#Warranty1').hide();}
                if (selectedValue == "Y") { $('#Warranty2').show(); } else {$('#Warranty2').hide();}
                if (selectedValue == "L") { $('#Warranty3').show();} else {$('#Warranty3').hide();} 
            }

      </script>
      <div class="card-body">
        <div class="col-md-12">
          <div class="row">
            <div class="col-md-4"> </div>   
            <div class="col-md-4">
              <div class="form-group">
                <label>Select item type   </label>
                <select name="inventory_type" id="pro_type"  class="form-control" style="width: 100%;" required="true" onchange="changeFunc(this.value);">
                 <option ></option>
                 <option value="P">Product</option>
                 <option value="S">Service </option>
                 <option value="R">Raw Materials</option>
                 <option value="A">Assest</option>
               </select>
             </div>
           </div>
           <div class="col-md-4"></div> 

        <div class="col-md-2 product">
             <div class="form-group">
              <label>Wharehouse Location</label>
              <select name="war_id" id="war_id" class="form-control select2" style="width: 100%;" required="true">
                <?php 
         $punit_query= mysqli_query($conn,"SELECT * FROM business_w_house  WHERE coid='$coid' AND bid='$bid' ") or die (mysqli_error($conn));
         while($rotunit= mysqli_fetch_assoc($punit_query)){
           echo '<option value='.$rotunit["bwid"].'>'.$rotunit["warehouse_code"].'('.$rotunit["war_location"].') </option>'; 
         }?> 
             </select>
           </div>
         </div>



           <div class="col-md-2 product">
             <div class="form-group">
              <label>Select Group   </label>
              <select name="pgid" id="pgid" class="form-control select2" style="width: 100%;" required="true">
               <?php 
               $pgroup_query= mysqli_query($conn,"SELECT * FROM item_group  WHERE coid='$coid' AND bid='$bid' AND active='1' ") or die (mysqli_error($conn));
               while($rowgroup= mysqli_fetch_assoc($pgroup_query)){
                 echo '<option value='.$rowgroup["pgid"].'>'.$rowgroup["pgroup_name"].' </option>';
               }?> 
             </select>
           </div>
         </div>
         <div class="col-md-2 product">
           <div class="form-group">
            <label>Select Catagory</label>
            <select name="pcid" id="pcid" class="form-control select2" style="width: 100%;" required="true">
             <?php 
             $pcatt_query= mysqli_query($conn,"SELECT * FROM item_catagory  WHERE coid='$coid' AND bid='$bid' AND active='1' ") or die (mysqli_error($conn));
             while($rowcat= mysqli_fetch_assoc($pcatt_query)){
               echo '<option value='.$rowcat["pcid"].'>'.$rowcat["pcat_name"].' </option>';
             }?> 
           </select>
         </div>
       </div>
       <div class="col-md-2 product">
         <div class="form-group">
          <label>Select Brand   </label>
          <select name="pbid" id="pbid" class="form-control select2" style="width: 100%;" required="true">
           <?php 
           $pbrand_query= mysqli_query($conn,"SELECT * FROM item_brand  WHERE coid='$coid' AND bid='$bid'  AND active='1' ") or die (mysqli_error($conn));
           while($rowbrand= mysqli_fetch_assoc($pbrand_query)){
             echo '<option value='.$rowbrand["pbid"].'>'.$rowbrand["pbrand_name"].' </option>';
           }?> 
         </select>
       </div>
     </div>
     <div class="col-md-2 product">
      <div class="form-group">
        <label>Item Code<b class="text-danger"> *</b></label>    <ss style="float: right; color:red" onclick="getElementById('random-number').value=Math.floor(Math.random()*1006660045)"><i class="text-danger fa fa-compress"></i></ss> 
   <sss style="text-align: right;" id="item_code_status"></sss> 
        <input type="text" name="item_code"   onkeyup="checkitem_code();"  id="random-number"  class="form-control form-control-sm " placeholder="Item Code" required="true">
      </div>
    </div>

         <div class="col-md-2 product">
      <div class="form-group">
        <label>SKU/HSN Code <b class="text-danger"> *</b></label>       
        <input type="text" name="item_sku"    value="<?php echo $invoice = skucode("SKU");?>" class="form-control form-control-sm "  placeholder="Item/SKU Code" required="true">
      </div>
    </div>

 <script type="text/javascript">
     $(document).ready(function(){
        $('#service_details').change(function(){
            var val = $('#service_details').val();
            $('#item_details').val(val);
        });
        $('#service_name').change(function(){
            var val = $('#service_name').val();
            $('#item_name').val(val);
        });
     });
</script>

    <div class="col-md-4 product">
      <div class="form-group">
        <label>Item/Model Name <b class="text-danger"> *</b> <sss style="text-align: right;" id="item_code_status1"></label>
        <input type="text" name="item_name"  onkeyup="checkitem_code();"  id="item_name" class="form-control form-control-sm inputselect" placeholder="Item/Model Name" required="true">
      </div>
    </div>
    <div class="col-md-2 product">
      <div class="form-group">
        <label>Details  <b class="text-danger"> *</b></label>
        <input type="text" name="item_details" id="item_details" class="form-control form-control-sm inputselect" placeholder="item Details"  required="true">
      </div>
    </div>
    <!-- service block -->
    <div class="col-md-4 service">
      <div class="form-group">
        <label>Service Name <b class="text-danger"> *</b></label>
        <input type="text" name="service_name" id="service_name" value="No Service" class="form-control form-control-sm inputselect" placeholder="Service Name"  required="true">
      </div>
    </div>

    <div class="col-md-3 service">
      <div class="form-group">
        <label>Details  <b class="text-danger"> *</b></label>
        <input type="text" name="service_details"  id="service_details"  value="No Details" class="form-control form-control-sm inputselect" placeholder="service Details"  required="true">
      </div>
    </div>
    <!-- service block -->
    <div class="col-md-2 product">
      <div class="form-group">
        <label>Opening Qty <b class="text-danger"> *</b></label>
        <input type="number" name="item_qty" value="0" class="form-control form-control-sm inputselect" required="true">
      </div>
    </div>


      <div class="col-md-2 product">
      <div class="form-group">
        <label> Select Batch </label>
        <select name="pro_batchid" id="pro_batchid" class="form-control select2" style="width: 100%;" required="true">
        <?php 
         $batch_query= mysqli_query($conn,"SELECT * FROM batch_no  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND active='1' ") or die (mysqli_error($conn));
         while($rotax= mysqli_fetch_assoc($batch_query)){
           echo '<option value='.$rotax["batid"].'>'.$rotax["batch_no"].'('.$rotax["mate_code"].') </option>';
         }
         ?> 
       </select>
     </div>
   </div>



      <div class="col-md-2 product">
      <div class="form-group">
        <label>Purchase Tax Group</label>
        <select name="pro_ptax" id="pro_ptax" class="form-control select2" style="width: 100%;" required="true">
         <?php 
         $purtax_query= mysqli_query($conn,"SELECT * FROM pro_tax  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='purchase' AND active='1' ") or die (mysqli_error($conn));
         while($rotax= mysqli_fetch_assoc($purtax_query)){
           echo '<option value='.$rotax["ptid"].'>'.$rotax["tax_group"].'('.$rotax["tax_margin"].' %) </option>';
         }?> 
       </select>
     </div>
   </div>
      <div class="col-md-2 product">
      <div class="form-group">
        <label>Sales Tax Group</label>
        <select name="pro_stax" id="pro_stax" class="form-control select2" style="width: 100%;" required="true">
         <?php 
         $saletax_query= mysqli_query($conn,"SELECT * FROM pro_tax  WHERE coid='$coid' AND bid='$bid' AND brid='$brid' AND sp_type='sales' AND active='1' ") or die (mysqli_error($conn));
         while($rotax= mysqli_fetch_assoc($saletax_query)){
           echo '<option value='.$rotax["ptid"].'>'.$rotax["tax_group"].'('.$rotax["tax_margin"].' %) </option>';
         }?> 
       </select>
     </div>
   </div>

   <div class="col-md-2 product" style="display: none;" id="product4">
     <div class="form-group">
       <label>Unit Type </label>
       <select  id="pro_unit_t" onchange="ufaun(this.value)"  class="form-control select2"  style="width: 100%;" required="true" >
        <option value="0" ></option>
         <?php 
         $punit_query= mysqli_query($conn,"SELECT * FROM pro_unit WHERE coid='$coid' ") or die (mysqli_error($conn));
         while($rotunit= mysqli_fetch_assoc($punit_query)){
           echo '<option value='.$rotunit["puid"].','.$rotunit["unit_convart"].'>'.$rotunit["unit_name"].'('.$rotunit["unit_symble"].') ('.$rotunit["unit_convart"].')</option>'; 
         }?> 
       </select>
     </div>
   </div>
<script type="text/javascript">
       function ufaun (uiddd){
       var unitlive = uiddd.split(",");
       $("#pro_unit").val(unitlive[0]);
       $("#unit_value").val(unitlive[1]);
      }
</script>

<input type="hidden" id="pro_unit" class="product" name="pro_unit_t">
<input type="hidden" id="unit_value" class="product" name="unit_value">

   <div class="col-md-2 product">
    <div class="form-group">
      <label>Cost Price <b class="text-danger"> *</b></label>
      <input type="number" name="item_cost" class="form-control form-control-sm inputselect" value="0" style="text-align:center;" required="true">
    </div>
  </div>
  <div class="col-md-2 product">
    <div class="form-group">
      <label>Lowest(MRP)  <b class="text-danger"> *</b></label>
      <input type="number" name="min_sales" class="form-control form-control-sm inputselect" value="0" style="text-align:center;" required="true">
    </div>
  </div>

  <div class="col-md-2 product">
    <div class="form-group">
      <label>Sales (MRP)  <b class="text-danger"> *</b></label>
      <input type="number" name="mrp_sales" class="form-control form-control-sm inputselect" value="0" style="text-align:center;" required="true">
    </div>
  </div>
  <div class="col-md-3 service">
    <div class="form-group">
      <label>Sales (MRP)  <b class="text-danger"> *</b></label>
      <input type="number" name="ser_mrp_sales"  value="0" class="form-control form-control-sm inputselect"  style="text-align:center;" required="true">
    </div>
  </div>

  <div class="col-md-2 product">
    <div class="form-group">
      <label>Notification  <b class="text-danger fa fa-bell"> *</b></label>
      <input type="number" name="item_notif"  value="0" class="form-control form-control-sm inputselect" placeholder="0" style="text-align:center;" required="true">
    </div>
  </div>

  <div class="col-md-2 product">
    <div class="form-group">
      <label>Warranty <b class="text-danger"> *</b></label>
      <select name="war_type" id="war_type"  class="form-control" style="width: 100%;" required="true" onchange="changeFunc_wa(this.value);">
       <option  value="N">None</option>
       <option value="D" >Days</option>
       <option value="M" >Monthly </option>
       <option value="Y" >Yearly</option>
       <option value="L" >Lifetime</option>
     </select> 
   </div>
 </div>
 <div class="col-md-1" style="display: none;" id="Warranty">
  <div class="form-group">
    <label>Days   <b class="text-danger"> *</b></label>
    <input type="text" name="wa_d" value="0" class="form-control form-control-sm inputselect" placeholder="" style="text-align:center;">
  </div>
</div>

<div class="col-md-1" style="display: none;" id="Warranty1">
  <div class="form-group">
    <label>Monthly    <b class="text-danger"> *</b></label>
    <input type="text" name="wa_m" value="0" class="form-control form-control-sm inputselect" placeholder="" style="text-align:center;" >
  </div>
</div>
<div class="col-md-1" style="display: none;" id="Warranty2">
  <div class="form-group">
    <label>Yearly    <b class="text-danger"> *</b></label>
    <input type="text" name="wa_y" value="0" class="form-control form-control-sm inputselect" placeholder="" style="text-align:center;" >
  </div>
</div>
<div class="col-md-1" style="display: none;" id="Warranty3">
  <div class="form-group">
    <label>Lifetime    <b class="text-danger"> *</b></label>
    <input type="text" name="wa_l" value="0" class="form-control form-control-sm inputselect" placeholder="" style="text-align:center;" >
  </div>
</div>
<div class="col-md-1 product">
  <div class="form-group">
    <label>Weight   <b class="text-danger"> *</b></label>
    <input type="text" name="item_weight"  value="0" class="form-control form-control-sm inputselect" placeholder="Weight" style="text-align:center;" required="true">
  </div>
</div>
<div class="col-md-2 product">
  <div class="form-group">
    <label>Discount Margin%   <b class="text-danger"> *</b></label>
    <input type="number" name="item_disc"  value="0" class="form-control form-control-sm inputselect"  style="text-align:center;" required="true">
  </div>
</div>
  <input type="hidden" name="item_op_date" class="form-control form-control-sm product service" value="<?php echo $co_f_year_start;?>">



  <div class="col-md-12 assest rawmatrials" >
    <center style="color: red;">  
    <b>Dear Customer, This service is not currently available.</b>
    <hr>
     সম্মানিত গ্রাহক এই সার্ভিসটি আপাতত চালু নেই|
    </center>
  </div>  

 
  <div class="col-md-12 rawmatrials" >
    <center style="color: red;"> 
    <b>Dear Customer, This service is not currently available.</b>
    <hr>
    সম্মানিত গ্রাহক এই সার্ভিসটি আপাতত চালু নেই|
    </center>
  </div>  







</div>
<center>
  <div class="card-footer" style="padding: 8px 5px;">
   <button type="submit" id="save" class="btn btn-success btn-xs">Save</button>
   <button type="reset" class="btn btn-secondary btn-xs">Reset</button>
 </div>
</center>
</div>
</div>
</form>
<div class="card-body p-1">
  <tr>
    <th rowspan="3"  class="text-center" >
      <input type="text" id="search" class="form-control form-control-sm search"  placeholder="searching" required="true" style="border-radius: ;border:1px solid primary;width:100%; text-align:center;">
    </th>

  </tr>
  <table id="trbl_table" class=" ttable" cellpadding="0" cellspacing="0">
    <thead>
      <tr class="sticky-top">
        <th class="text-center" style="width:5%;">S/l</th>
        <th class="text-center" style="width:32%;">Group </th>
        <th class="text-center" style="width:8%;">Item Code </th>
        <th class="text-center" style="width:20%;">Item Name </th>
        <th class="text-center" style="width:5%;">Qty </th>
        <th class="text-center" style="width:8%;">Unit Type </th>
        <th class="text-center" style="width:8%;">Cost  </th>
        <th class="text-center" style="width:8%;">min sales </th>
        <th class="text-center" style="width:15%;">sales MRP </th>
        <th class="text-center" style="width:8%;">notification </th>
        <th class="text-center" style="width:8%;">warranty</th>
        <th class="text-center" style="width:8%;">Discount%</th>
        <th class="text-center" style="width:8%;">P-tax%</th>
        <th class="text-center" style="width:8%;">S-tax%</th>

        <th class="text-center" style="width:25%;">Qty*Cost</th>
        <th class="text-center" style="width:5%;">active </th>
      </tr>
    </thead>
    <tbody id="colview" class="searchtable">
      <?php
      $product_query= mysqli_query($conn,"SELECT * FROM item WHERE pro_type='P'  AND coid='$coid' AND $bid='$bid' AND brid='$brid' AND active='1' ") or die (mysqli_error($conn));
      $i=0;
      while ($rowpro= mysqli_fetch_array($product_query)){
       $companyid  =$rowpro['coid'];
       $pgid  =$rowpro['pgid'];
       $pcid  =$rowpro['pcid'];
       $pbid  =$rowpro['pbid'];
       // $pro_tax=$rowpro['pro_tax'];
         $pro_unit=$rowpro['pro_unit'];

       $co_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT co_name FROM company WHERE coid='$companyid'  LIMIT 1 "))['co_name'];  
       ?>
 
        <tr onclick="itemprofile(<?php echo $rowpro['pro_id']?>)">

        <td  style="font-size: 11.5px;text-align: center;"><?php echo ++$i ?></td>
        <td  style="font-size: 11.5px">
         <?php echo $pgroup_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT pgroup_name FROM item_group WHERE pgid='$pgid'  AND coid='$coid'  LIMIT 1 "))['pgroup_name'];?> > <?php echo $pcat_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT pcat_name FROM item_catagory WHERE pcid='$pcid'  AND coid='$coid'  LIMIT 1 "))['pcat_name']; ?> > <?php echo $pcat_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT pbrand_name FROM item_brand WHERE pbid='$pbid'  AND coid='$coid'  LIMIT 1 "))['pbrand_name'];?>
       </td>
       <td  style="font-size: 11.5px;"><?php echo $rowpro['item_code'];?> </td>
       <td  style="font-size: 11.5px;"><?php echo $rowpro['item_name'];?> <?php echo $rowpro['item_weight'];?> <?php echo $rowpro['item_details'];?> </td>
         <td  style="font-size: 11.5px;"><?php echo $rowpro['item_qty'];?> </td>


      <td style="font-size:11.5px;">   <?php    $unit_convart=mysqli_fetch_assoc(mysqli_query($conn,"SELECT unit_convart FROM pro_unit WHERE puid='".$rowpro['pro_unit']."'  AND coid='$coid'  LIMIT 1 "))['unit_convart'];

      $unit_convc=$rowpro['item_qty']/$unit_convart;
      echo number_format($unit_convc,2);
    echo $pcat_name=mysqli_fetch_assoc(mysqli_query($conn,"SELECT unit_name FROM pro_unit WHERE puid='$pro_unit' AND coid='$coid'  LIMIT 1 "))['unit_name'];
?></td>
  
      <td  style="font-size:11.5px;"><?php echo $rowpro['mrp_purchase'];?></td>
      <td  style="font-size:11.5px;"><?php echo $rowpro['min_sales'];?></td>
      <td  style="font-size:11.5px;"><?php echo $rowpro['mrp_sales'];?></td>
      <td  style="font-size:11.5px;"><?php echo $rowpro['item_notif'];?></td>
      <td  style="font-size:11.5px;"> <?php
        if ($rowpro['war_type']=='N') {echo "No warrenty";}else if ($rowpro['war_type']=='D'){echo $rowpro['wa_dmy']."Days";}else if ($rowpro['war_type']=='M'){echo $rowpro['wa_dmy']."Months";}else if ($rowpro['war_type']=='Y'){echo $rowpro['wa_dmy']."Years";}else if ($rowpro['war_type']=='L'){echo "Life Time";}
    ?> </td>
      <td  style="font-size:11.5px;"><?php echo $rowpro['item_disc'];?>%</td>
            <td  style="font-size:11.5px;"><?php
    echo $purchase_tax=mysqli_fetch_assoc(mysqli_query($conn,"SELECT tax_margin FROM pro_tax WHERE ptid='".$rowpro['pro_ptax']."' AND coid='$coid'  LIMIT 1 "))['tax_margin'];
           ?></td>

      <td  style="font-size:11.5px;"><?php 
    echo $purchase_tax=mysqli_fetch_assoc(mysqli_query($conn,"SELECT tax_margin FROM pro_tax WHERE ptid='".$rowpro['pro_stax']."' AND coid='$coid'  LIMIT 1 "))['tax_margin'];


  ?></td>

      <td  style="font-size:11.5px;">
        <?php
           echo $amount =$rowpro['item_qty']*$rowpro['mrp_purchase'];
         ?>
       </td>
      <td  style="font-size:11.5px;">
        <?php if ($rowpro['active']=='1') { echo "Active";}else if($rowpro['active']=='0'){echo "Deactive";}


    ?> </td>
    </tr>
  <?php } ?>
</tbody>
</table>
</div>
</div>
</div>



</div>
</div>
</section>
<script>
 $(document).ready(function(){
   $("#war_id,#pgid,#pcid,#pbid,#puid,#pro_type,#pro_unit_t,#war_type,#pro_stax,#pro_ptax,#pro_batchid").select2();
 });  
</script>
<script>
 $(document).ready(function() {
   $('#item_form').on('submit', function(e){
     $('#save').prop('disabled', true);
     $('#save').html('<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> '+'Loading...');
     e.preventDefault();  
     $.post("../itemstock/item_db.php",$(this).serialize(),function(data){
      $('#item_form')[0].reset();
      $('#save').prop('disabled', false);
      $('#save').html('Save');
      $.post('../itemstock/item_add.php?running='+1,{},function(res){
       $('#stockhome').html(res);
       toastr.success(Showing, '<?php echo "Data Load Succefully " ?>');
     }); 
    });
   });


   $('.search').on('keyup',function(){
    var searchTerm = $(this).val().toLowerCase();
    $('.searchtable tr').each(function(){
      var lineStr = $(this).text().toLowerCase();
      if(lineStr.indexOf(searchTerm) === -1){
        $(this).hide();
      }else{
        $(this).show();
      }
    });
  });

   $('th').each(function (col) {
    $(this).hover(
      function () {
        $(this).addClass('focus');
      },
      function () {
        $(this).removeClass('focus');
      }
      );
    $(this).click(function () {
      if ($(this).is('.asc')) {
        $(this).removeClass('asc');
        $(this).addClass('desc selected');
        sortOrder = -1;
      } else {
        $(this).addClass('asc selected');
        $(this).removeClass('desc');
        sortOrder = 1;
      }
      $(this).siblings().removeClass('asc selected');
      $(this).siblings().removeClass('desc selected');
      var arrData = $('table').find('tbody >tr:has(td)').get();
      arrData.sort(function (a, b) {
        var val1 = $(a).children('td').eq(col).text().toUpperCase();
        var val2 = $(b).children('td').eq(col).text().toUpperCase();
        if ($.isNumeric(val1) && $.isNumeric(val2))
          return sortOrder == 1 ? val1 - val2 : val2 - val1;
        else
          return (val1 < val2) ? -sortOrder : (val1 > val2) ? sortOrder : 0;
      });
      $.each(arrData, function (index, row) {
        $('tbody').append(row);
      });
    });
  });
 });


   function itemprofile(productid){    
    $("#modal-xl").modal({backdrop: "static"});
    pagecall('../itemstock/item_profile.php?pro_id='+productid,'#xlmdview');   
}  

</script>

   <script type="text/javascript">
      function checkitem_code(){  
         var item_code=($('#random-number').val());
         var item_name=($('#item_name').val());
         if(item_code || item_name ){$.ajax({type: 'post',
            url: '../all_check/item_code_check.php',
            data: ({
              item_code:item_code,
              item_name:item_name
               }),success:function (response){

            $('#item_code_status,#item_code_status1').html(response);
          if(response==1) {$("#save").prop('disabled', true); }else{$("#save").prop('disabled', false);}}
        }
      }   
   </script>