<?php require_once('../auth/auth.php');?>
<?php
   $search     = $_GET['search'];
   $bbid       = $_GET['bbid'];
   $bbrid      = $_GET['bbrid'];
   $item_gid   = isset($_GET['item_gid'])?$_GET['item_gid']:0;
   $item_cid   = isset($_GET['item_cid'])?$_GET['item_cid']:0;
   $item_brnd  = isset($_GET['item_brnd'])?$_GET['item_brnd']:0;
   $nrow       = $_GET['nrow'];
   $qtymn      = isset($_GET['qtymn'])?$_GET['qtymn']:'';
   $qtymx      = isset($_GET['qtymx'])?$_GET['qtymx']:'';
   $item_qty   = isset($_GET['item_qty'])?$_GET['item_qty']:0;
   $inven_type = 'P';
   $asc_desc  = isset($_GET['asc_desc'])?$_GET['asc_desc']:0;
   
$inacc= mysqli_query($conn,"SELECT co_name,owner_name,co_phone,co_email,co_address,co_logo FROM company WHERE coid='$coid' LIMIT 1") or die(mysqli_error($conn));
$cor= mysqli_fetch_assoc($inacc);
$co_logo=$cor['co_logo'];


if ($item_gid=='0') {
   $congid = '';
}elseif ($item_gid >0){
    $congid= " AND item.pgid=$item_gid";
}

if ($item_cid=='0') {
   $concid = '';
}elseif ($item_cid >0){
    $concid= " AND item.pcid=$item_cid";
}

if ($item_brnd=='0') {
   $conbrnd = '';
}elseif ($item_brnd >0){
    $conbrnd= " AND item.pbid=$item_brnd";
}

if ($bbid=='0') {
   $bbidcond = '';
}elseif ($bbid >0){
    $bbidcond= " AND item.bid=$bbid";
}

if ($bbrid=='0') {
   $bbridcond = '';
}elseif ($bbrid >0){
    $bbridcond= " AND item.brid=$bbrid";
}

if ($asc_desc=='0') {
  $azcond = 'item.item_name';
}elseif ($asc_desc=='ASC'){
     $azcond= "item.item_name ASC";
}elseif ($asc_desc=='DESC'){
     $azcond= "item.item_name DESC";
}


 if ($search =='0') {
     $blacksearch='';
  } else {
     $blacksearch = " AND (item.item_code LIKE '%$search%' || item.item_name LIKE '%$search%' || item.item_details LIKE '%$search%' || item.location LIKE '%$search%')";
  }

  if ($qtymn =='' && $qtymx =='') {
      $qtymncon="";
  } elseif ($qtymn =='' && $qtymx >0) {
     $qtymncon=" AND item.item_qty <= $qtymx";
  } elseif ($qtymx =='' && $qtymn >=0) {
     $qtymncon=" AND item.item_qty = $qtymn";
  } elseif ($qtymn >= 0 && $qtymx > 0) {
      $qtymncon = " AND item.item_qty BETWEEN $qtymn AND $qtymx";
  } else {
     $qtymncon="";
  }
  
    if ($item_qty =='0') {
      $item_qtycon="";
  } elseif ($item_qty =='zero') {
     $item_qtycon=" AND item.item_qty=0";
  } elseif ($item_qty =='less') {
     $item_qtycon=" AND item.item_qty < 0";
  } elseif ($item_qty =='decimal') {
    $item_qtycon="AND FLOOR(item.item_qty) <> item.item_qty";
  }elseif ($item_qty =='zeroup') {
    $item_qtycon="AND item.item_qty>0";
  } else {
     $item_qtycon="";
  }

  
   if ($inven_type=='P') {
     $item= mysqli_query($conn,"SELECT item.*,unit_name,pgroup_name,pcat_name,pbrand_name FROM item 
        LEFT JOIN pro_unit ON (item.pro_unit=pro_unit.puid)
        LEFT JOIN item_group ON (item.pgid=item_group.pgid)
        LEFT JOIN item_catagory ON (item.pcid=item_catagory.pcid)
        LEFT JOIN item_brand ON (item.pbid=item_brand.pbid)
        WHERE item.coid='$coid' $blacksearch $bbidcond $bbridcond $congid $concid $conbrnd $qtymncon $item_qtycon AND pro_type='$inven_type' ORDER BY $azcond LIMIT $nrow ") or die (mysqli_error($conn));
     $dataa = [];
     while ($prow= mysqli_fetch_assoc($item)){ $dataa[]=$prow;
        $imei_status = $prow['imei_status'];
        $spro_id   = $prow['pro_id'];
        
        
      
         
     }
   } else if($inven_type=='S') { 
     $sarvice= mysqli_query($conn,"SELECT * FROM item WHERE  coid='$coid' AND pro_type='$inven_type' ") or die (mysqli_error($conn));
        while ($proww= mysqli_fetch_assoc($sarvice)){ $dataa[]=$proww;}
   }
   
   $custom_field= mysqli_query($conn,"SELECT item FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
      $rowcf= mysqli_fetch_assoc ($custom_field);
   
      $p=$rowcf['item'];
      function input_check($ap)
      {
       if (strpos($GLOBALS['p'], $ap) !== false) { return 1;} 
      }
   
   ?>
   <div class="card-body cscroll" style="overflow-x: auto;overflow-y: auto;">



 <table class="ttable border-bottom-0 d-none" id="header" cellpadding="0" cellspacing="0">
    <thead>
       <tr>
          <th rowspan="3" class="thborder border-bottom-0 text-center" style="width:10%;">
             
                <?php if($co_logo != ""): ?>
                     <img class="profile-user-img img-fluid img-circle" src="data:image/gif;base64,<?php echo $co_logo;?>" style="width:80px;height:77px;">
                <?php else : ?>
                     <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:77px;"></div>
                <?php endif; ?>
             
             
             
          </th>
          <th rowspan="3" class="thborder border-bottom-0" style="width:70%;">
                <dt style="font-size:18px;font-weight: 600;"><?php echo $cor['co_name'];?></dt>
                <dt><?php echo $cor['owner_name'];?></dt>
                <dt><?php echo $cor['co_address'];?></dt>
                <dt><?php echo $cor['co_phone'];?>#<?php echo $cor['co_email'];?></dt>
          </th>
          <th class="thborder text-center" style="width:20%;"><?php echo language(75);?></th>
       </tr>
       <tr>
          <th class="thborder text-center">
      
              
            <?php  
                if ($inven_type=='S') {
                       echo $inven_t= 'Service';
                    }elseif ($inven_type =='P'){
                       echo $inven_t= 'Product';
                }
            ?>           
              </th>
       </tr>
       <tr>
          <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($co_f_year_start));?> To <?php echo date('d/m/y', strtotime($date));?>
          </th>
       </tr>
    </thead>
 </table>


<table id="stock" class="ttable table-bordered table-hover" >
       <input type="hidden" id="vounameset" value="Stock Master File ">

   <thead class="text-nowrap">
      <tr class="sticky-top" style="backgroud:#e7e7e7;">
         <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
         <?php if ($inven_type=='P'){  ?>  
         <th class="text-center" style="width:7%;"><?php echo language(76);?></th>
         <th  style="width:25%;"><?php echo language(77);?> </th>
         <th class="text-center" style="width:5%;"><?php echo language(78);?></th>
         <th class="text-center <?php if(input_check('sku') !==1){ echo 'd-none';}?>" style="width:5%;"><?php echo language(79);?></th>        
         <th class="text-center" style="width:5%;"><?php echo language(80);?></th>
         
         <?php if(per('item_cost_show')==1){ ?>
         <th class="text-center" title="Purchase" style="width:5%;"><?php echo language(81);?></th>
         <?php } ?>
         
         <?php if(per('item_avg_show')==1){ ?>
         <th class="text-center" title="AVG" style="width:5%;"><?php echo language(82);?></th>
         <?php  } ?>
         
         <?php if(per('item_minsale_show')==1){ ?>
         <th class="text-center" title="Min MRP" style="width:5%;"><?php echo language(83);?></th>
         <?php } ?>
         
         <th class="text-center" title="MRP" style="width:5%;"><?php echo language(84);?> </th>
         <th class="text-center" style="width:5%;"><?php echo language(85);?></th>
         <th class="text-center" style="width:7%;"><?php echo language(86);?></th>
         <th class="text-center" style="width:5%;"><?php echo language(87);?></th>
         
                  <?php if(per('item_disc')==1){ ?>

         <th class="text-center"  title="Discount Margin" style="width:5%;"><?php echo language(88);?></th>
             <?php  } ?>
         <th class="text-center" style="width:5%;"><?php echo language(89);?></th>
         <?php } ?>  
         <?php if ($inven_type=='S'){  ?> 
         <th class="text-center" style="width:8%;">Service Name </th>
         <th class="text-center" style="width:20%;">Service Details </th>
         <th class="text-center" style="width:20%;">MRP </th>
         <?php } ?>  
         <th class="text-center" style="width:5%;"><?php echo language(90);?> </th>
      </tr>
   </thead>
   <tbody id="proshow"></tbody>
     <tfoot style="background:#e3e3e3;"> 
        <tr class="sticky-bottom">
            

         <?php if(per('item_cost_show')==1){ ?>
            <td> </td>
         <?php } ?>
            
         <?php if(per('item_avg_show')==1){ ?>
            <td> </td>
         <?php } ?>
        <?php if(per('item_minsale_show')==1){ ?>
         <?php } ?>
         

           <td class="text-right" colspan="2"><dt><?php echo language(91);?></dt></td>
           <td class="text-center"><dt id="tqty"></dt></td>


           <td class="text-right" colspan="3"><dt><?php echo language(210);?> </dt></td>
           <td class="text-center" colspan="2"><dt id="tsval"></dt></td>


           <td class="text-right" colspan="3"><dt><?php echo language(92);?></dt></td>
           <td class="text-center" colspan="3"><dt id="tval"></dt></td>

        </tr>                                 
     </tfoot>   
</table>

</div>
<script id="sview" type="text/javascript">
   $(document).ready(function(){
       var dataa = '';
       var i = 0;
       var status = '';
       var warrenty = [];
       var tqty     = 0;
       var tpur     = 0;
       var tavg     = 0;
       var tslm     = 0;
       var tslx     = 0;
       var tval     = 0;
       var tsval    = 0;
       $.each(<?php echo json_encode($dataa);?>, function(key,value){

       tqty  += Number(value.item_qty);
       tval  += Number((value.avg_cost*value.item_qty));
       tsval  += Number((value.mrp_sales*value.item_qty));

        
        if (value.active==1) {
          status = 'Active';
        } else {
          status = 'Deactive';
        }

        if (value.war_type=='N') {
          warrenty = 'None';
        } else if (value.war_type=='D') {
          warrenty = value.wa_dmy +' Days';
        } else if (value.war_type=='M') {
          warrenty = value.wa_dmy +' Month';
        } else if (value.war_type=='Y') {
          warrenty = value.wa_dmy +' Years';
        } else if (value.war_type=='L') {
          warrenty = value.wa_dmy +' Lifetime';
        }        


        if (value.imei_status=='0') {
          imei_status_check = '';
        } else if (value.imei_status=='1') {
          imei_status_check = '<i class="fa fa-barcode"> </i>';
        }   
        
        
       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';
       dataa += '<td class="btn-outline-dark text-nowrap" onclick="itemprofile('+value.pro_id+')">'+ <?php if(input_check('t_group')==1){?> value.pgroup_name  +' - '+ <?php } ?>  <?php if(input_check('t_category')==1){?> value.pcat_name +' - '+<?php } ?>value.pbrand_name +'</td>';
       dataa += '<td class="text-nowrap">  '+imei_status_check+' '+ value.item_name +'  '+value.item_details+'</td>';
       dataa += '<td class="text-center">' + value.item_code +  '  </td>';
       dataa += '<td class="text-center <?php if(input_check('sku') !==1){ echo 'd-none';}?> ">' + value.item_sku + '</td>';
       
          if (value.item_qty == 0){
        dataa += '<td class="text-center text-danger">' + bden.format(value.item_qty) +' '+ value.unit_name +'</td>';
       }else if (value.item_qty < 0){  
       dataa += '<td class="text-center text-orange ">' + bden.format(value.item_qty) +' '+ value.unit_name +'</td>';
       } else{
       dataa += '<td class="text-center">' + bden.format(value.item_qty) +' '+ value.unit_name +'</td>';
       }
       
    <?php if(per('item_cost_show')==1){ ?>
       dataa += '<td class="text-right">' + bden.format(value.mrp_purchase) + '</td>';
    <?php } ?>
    <?php if(per('item_avg_show')==1){ ?>
       dataa += '<td class="text-right">' + bden.format(value.avg_cost) + '</td>';
    <?php  } ?>
    <?php if(per('item_minsale_show')==1){ ?>
       dataa += '<td class="text-right">' + bden.format(value.min_sales) + '</td>';
     <?php } ?>
       
       dataa += '<td class="text-right">' + bden.format(value.mrp_sales) + '</td>';
       dataa += '<td class="text-center">' + value.item_notif + '</td>';
       dataa += '<td class="text-center">' + warrenty + '</td>';
       dataa += '<td class="text-center">' + value.location + '</td>';
       
           <?php if(per('item_disc')==1){ ?>

       dataa += '<td class="text-center">' + value.item_disc + '</td>';
       <?php } ?>
       
       dataa += '<td class="text-right">' + bden.format((value.avg_cost*value.item_qty)) + '</td>';
       dataa += '<td class="text-center">' + status + '</td>';   
       dataa += '</tr>';
   });
    $("#proshow").html(dataa);
    $("#tqty").text(bden.format(tqty));
    $("#tval").text(bden.format(tval));
    $("#tsval").text(bden.format(tsval));
    
    $("#sview").remove();
   });
   
   
   
   function itemprofile(productid){    
   $("#modal-xl").modal({backdrop: "static"});
   pagecall('../itemstock/item_profile.php?pro_id='+productid,'#xlmdview');   
   }  
</script>