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

   $co_rs = mysqli_query($conn,"SELECT co_name,owner_name,co_address,co_phone,co_email FROM company WHERE coid='$coid'") or die (mysqli_error($conn));
   $cor= mysqli_fetch_assoc($co_rs);

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

    if ($bbrid=='0') {
       $bbridcond = '';
       $branch_name = 'All Branch';       
    }elseif ($bbrid >0){
        $bbridcond= " AND o.brid=$bbrid";
        $branch_name = mysqli_fetch_assoc(mysqli_query($conn,"SELECT branch_name FROM branch WHERE coid='$coid' AND brid='$bbrid'"))['branch_name'];
    }

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

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

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

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


     $inv_query= mysqli_query($conn,"SELECT o.pro_id,DATE_FORMAT(o.datetime, '%d/%m/%Y') tdate,SUM(o.qty) qty,SUM(o.t_amount) amount,SUM(o.dis_amount) discount,item.item_details,item.item_name,item.item_code,unit_symble,pgroup_name,pcat_name,pbrand_name,em_acname
      FROM order_list o 
        INNER JOIN item ON (item.pro_id=o.pro_id)
        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)
        LEFT JOIN user ON (user.uid=o.userid)
        LEFT JOIN em_employee ON (em_employee.em_id=user.us_em_id)
        WHERE o.coid='$coid' $bbidcond $bbridcond $gidcond $cidcond $brndcond $blacksearch AND o.order_type='EPUR' AND date(o.datetime) BETWEEN '$fdate' AND '$edate' GROUP BY date(o.datetime),o.pro_id ORDER BY o.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%;">
             <img class="profile-user-img img-fluid img-circle" src="../../dist/img/a.png" style="width:80px;height:77px;">
          </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 $branch_name;?></th>
       </tr>
       <tr>
          <th class="thborder text-center"><?php echo language(313);?></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="pu_main_table" class="ttable" cellpadding="0" cellspacing="0">
<thead>
    <tr class="sticky-top">
        <th class="text-center" style="width:2%;"><?php echo language(15);?></th>
        <th class="text-center" style="width:7%;"><?php echo language(131);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(76);?></th>
        <th  style="width:13%;"><?php echo language(77);?> </th>
        <th class="text-center" style="width:5%;"><?php echo language(78);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(139);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(81);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(88);?></th>
        <th class="text-center" style="width:5%;"><?php echo language(145);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(256);?></th>
    </tr>
</thead>
 <tbody id="pinvr_show"></tbody>
     <tfoot> 
        <tr class="sticky-bottom">
            <th class="text-right" colspan="5"><?php echo language(233);?></th>
            <th class="text-center"><dt id="t_qtyy"></dt></th>
            <th class="text-right"><dt id="t_amount"></dt></th>
            <th class="text-right"><dt id="t_discount"></dt></th>
            <th class="text-right"><dt id="t_net_bill"></dt></th>
            <th class="text-right"></th>
        </tr>                                 
     </tfoot>   
</table>
<script id="invr_script" type="text/javascript">
   $(document).ready(function(){
       let dataa      = '';
       let i          = 0;
       let t_qtyy     = 0;
       let t_amount   = 0;
       let t_discount = 0;
       let net_bill   = 0;
       let t_net_bill = 0;

       
       $.each(<?php echo json_encode($dataa);?>, function(key,value){
       t_qtyy      += Number(value.qty);
       t_amount    += Number(value.amount);
       t_discount  += Number(value.discount);
       net_bill     = (Number(value.amount)-Number(value.discount));
       t_net_bill  += Number(net_bill);


       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';
       dataa += '<td class="text-center">' + value.tdate + '</td>';
       dataa += '<td class="btn-outline-dark" onclick="itemprofile('+value.pro_id+')">'+ value.pgroup_name  +' - '+ value.pcat_name +' - '+ value.pbrand_name +'</td>';
       dataa += '<td>'+value.item_details+' '+ value.item_name +'</td>';
       dataa += '<td class="text-center">' + value.item_code +  '</td>';
       dataa += '<td class="text-center">' + bden.format(value.qty) +' '+ value.unit_symble +'</td>';

       dataa += '<td class="text-right">' + bden.format(Number(value.amount)) + '</td>';
       dataa += '<td class="text-right">' + bden.format(Number(value.discount)) + '</td>';
       dataa += '<td class="text-right">' + bden.format(net_bill) + '</td>';
       dataa += '<td class="text-center">' + value.em_acname + '</td>';
       dataa += '</tr>';
   });
      $("#pinvr_show").html(dataa);
      $("#t_qtyy").text(bden.format(t_qtyy));
      $("#t_amount").text(bden.format(t_amount));
      $("#t_discount").text(bden.format(t_discount));
      $("#t_net_bill").text(bden.format(t_net_bill));
      $("#invr_script").remove();
   });
</script>
<script>
 function itemprofile(productid){    
   $("#modal-xl").modal({backdrop: "static"});
    pagecall('../itemstock/item_profile.php?pro_id='+productid,'#xlmdview');   
   } 
</script>

