<?php require_once('../auth/auth.php');?>
<?php
   $search     = $_GET['search'];
   $bbid       = $_GET['bbid'];
   $bbrid      = $_GET['bbrid'];
   $itype      = $_GET['itype'];
   $nrow       = $_GET['nrow'];
   $fdate      = $_GET['fdate'];
   $edate      = $_GET['edate'];



$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_name        = $cor['co_name'];
   $owner_name     = $cor['owner_name'];
   $co_address     = $cor['co_address'];
   $co_phone       = $cor['co_phone'];
   $co_email       = $cor['co_email'];
   $co_logo        = $cor['co_logo']; 
   
   
   
   
    if ($bbid=='0') {
       $bbidcond = '';
    }elseif ($bbid >0){
        $bbidcond= " AND order_list.bid=$bbid";
    }

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

    if ($itype=='0') {
       $itypecond = " AND (order_list.order_type='ECSL' || order_type='ERSL' || order_type='INSL')";
       $itype_show='Sales';
    }elseif ($itype =='EPUR'){
        $itypecond= " AND order_list.order_type='EPUR'";
        $itype_show='Purchase';
    }

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


     $inv_query= mysqli_query($conn,"SELECT DATE_FORMAT(order_list.datetime, '%d/%m/%Y') date,order_list.order_type,sum(order_list.qty) as totalqty,item.item_name,item.item_sku,item.item_code,item.item_details,item.item_weight,item.mrp_purchase,item.mrp_sales
      FROM order_list  LEFT JOIN item ON (item.pro_id=order_list.pro_id)
        WHERE order_list.coid='$coid' $bbidcond $bbridcond $blacksearch $itypecond AND date(order_list.datetime) BETWEEN '$fdate' AND '$edate' 
        GROUP BY order_list.pro_id ORDER BY order_list.datetime ASC LIMIT $nrow;") or die (mysqli_error($conn));
     $dataa = [];
     while ($invrow= mysqli_fetch_assoc($inv_query)){ $dataa[]=$invrow;}
?>

 <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(187); ?></th>
       </tr>
       <tr>
          <th class="thborder text-center">
      
              
            <?php  
                if ($itype=='0') {
                       echo language(186);
                    }elseif ($itype =='EPUR'){
                       echo language(185);
                }
            ?>           
              </th>
       </tr>
       <tr>
          <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($fdate));?> To <?php echo date('d/m/y', strtotime($edate));?>
          </th>
       </tr>
    </thead>
 </table>
<table id="isumm_print" class="ttable" cellpadding="0" cellspacing="0">
       <input type="hidden" id="vounameset" value="<?php echo $itype_show ?> Item Summary Report <?php echo date('d/M/y', strtotime($fdate));?> To <?php echo date('d/M/y', strtotime($edate));?> ">

<thead>
    <tr class="sticky-top">
        <th class="text-center" style="width:3%;"><?php echo language(15);?></th>
        <th class="text-center" style="width:15%;"><?php echo language(78);?></th>
        <th class="text-center" style="width:30%;"><?php echo language(97)?></th>
        <th style="width:15%;"><?php echo language(79);?></th>
        <th class="text-center" style="width:12%;"><?php echo language(139);?></th>
        <th class="text-center" style="width:15%;"><?php echo language(81);?></th>
        <th class="text-center" style="width:15%;"><?php echo language(84);?></th>
    </tr>
</thead>
 <tbody id="isumm_show"></tbody>
     <tfoot> 
        <tr class="sticky-bottom">
            <th class="text-right" colspan="4"><?php echo language(233);?></th>
            <th class="text-center"><dt id="t_bill"></dt></th>
            <th class="text-right"></th>
            <th class="text-right"></th>

        </tr>                                 
     </tfoot>   
</table>
<script id="inv_script" type="text/javascript">
   $(document).ready(function(){
       let dataa      = '';
       let i          = 0;
       let t_bill     = 0;
         $.each(<?php echo json_encode($dataa);?>, function(key,value){
         t_bill      += Number(value.totalqty);
       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';
       dataa += '<td class="text-center">' + value.item_code + '</td>';
       dataa += '<td>'+ value.item_name +' '+ value.item_details +'  '+ value.item_weight +'</td>';
       dataa += '<td class="text-center">' + value.item_sku + '</td>';
       dataa += '<td class="text-center">' + value.totalqty +  '</td>';
       dataa += '<td class="text-center">' + value.mrp_purchase +  '</td>';
       dataa += '<td class="text-center">' + value.mrp_sales +  '</td>';




       dataa += '</tr>';
   });
      $("#isumm_show").append(dataa);
      $("#t_bill").text(bden.format(t_bill));
      $("#inv_script").remove();
   });
</script>


