<?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'];

   $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 t1.bid=$bbid";
    }

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

    if ($search =='0') {
         $blacksearch='';
      } else {
         $blacksearch = " AND (accounts.ac_name LIKE '%$search%' || accounts.ac_alise LIKE '%$search%' || accounts.ac_number LIKE '%$search%' || t1.invoice LIKE '%$search%')";
    }


     $inv_query= mysqli_query($conn,"SELECT DATE_FORMAT(t1.date, '%d/%m/%Y') tdate,t1.invoice,t1.amount,ac_name,ac_number,em_acname,
      COALESCE((SELECT amount FROM transaction WHERE invoice =t1.invoice AND note='epur_discount' LIMIT 1),0) commission,
      COALESCE((SELECT amount FROM transaction WHERE invoice =t1.invoice AND note='epur_payment' LIMIT 1),0) payment,
      COALESCE((SELECT amount FROM transaction WHERE invoice =t1.invoice AND note='epur_freight' LIMIT 1),0) freight
      FROM transaction t1 
        LEFT JOIN accounts ON (accounts.acid=t1.cr_acid)
        LEFT JOIN user ON (user.uid=t1.userid)
        LEFT JOIN em_employee ON (em_employee.em_id=user.us_em_id)
        WHERE t1.coid='$coid' $bbidcond $bbridcond $blacksearch AND t1.ttype='$itype' AND date(t1.date) BETWEEN '$fdate' AND '$edate' GROUP BY t1.invoice ORDER BY t1.date 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(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="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 style="width:17%;"><?php echo language(215);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(111);?></th>
        <th class="text-center" style="width:7%;"><?php echo language(151);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(253);?></th>
        <th class="text-center" style="width:7%;"><?php echo language(88);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(145);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(146);?></th>
        <th class="text-center" style="width:8%;"><?php echo language(255);?></th>
        <th class="text-center" style="width:7%;"><?php echo language(254);?></th>
        <th class="text-center" style="width:13%;"><?php echo language(256);?></th>
    </tr>
</thead>
 <tbody id="pinv_show"></tbody>
     <tfoot> 
        <tr class="sticky-bottom">
            <th class="text-right" colspan="5"><?php echo language(233);?></th>
            <th class="text-right"><dt id="t_bill"></dt></th>
            <th class="text-right"><dt id="t_comson"></dt></th>
            <th class="text-right"><dt id="t_net_bill"></dt></th>
            <th class="text-right"><dt id="t_payment"></dt></th>
            <th class="text-right"><dt id="t_due_bill"></dt></th>
            <th class="text-right"><dt id="t_freight"></dt></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;
       let t_comson   = 0;
       let net_bill   = 0;
       let t_net_bill = 0;
       let t_payment  = 0;
       let due_bill   = 0;
       let t_due_bill = 0;
       let t_freight  = 0;
       
       $.each(<?php echo json_encode($dataa);?>, function(key,value){
       t_bill      += Number(value.amount);
       t_comson    += Number(value.commission);
       net_bill     = (Number(value.amount)-Number(value.commission));
       t_net_bill  += Number(net_bill);
       t_payment   += Number(value.payment);
       due_bill     = (Number(net_bill)-Number(value.payment));
       t_due_bill  += Number(due_bill);
       t_freight   += Number(value.freight);


       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';
       dataa += '<td class="text-center">' + value.tdate + '</td>';
       dataa += '<td>' + value.ac_name +  '</td>';
       dataa += '<td class="text-center">' + value.ac_number + '</td>';
       dataa += '<td class="text-center btn-outline-dark" onclick="vprint(this)" invoice="'+value.invoice+'">' + (value.invoice).split("-")[5] + '</td>';
       dataa += '<td class="text-right">' + bden.format(Number(value.amount)) + '</td>';
       dataa += '<td class="text-right">' + bden.format(Number(value.commission)) + '</td>';
       dataa += '<td class="text-right">' + bden.format(net_bill) + '</td>';
       dataa += '<td class="text-right">' + bden.format(Number(value.payment)) + '</td>';
       dataa += '<td class="text-right">' + bden.format(due_bill) + '</td>';
       dataa += '<td class="text-right">' + bden.format(Number(value.freight)) + '</td>';
       dataa += '<td class="text-center">' + value.em_acname + '</td>';
       dataa += '</tr>';
   });
      $("#pinv_show").html(dataa);
      $("#t_bill").text(bden.format(t_bill));
      $("#t_comson").text(bden.format(t_comson));
      $("#t_net_bill").text(bden.format(t_net_bill));
      $("#t_payment").text(bden.format(t_payment));
      $("#t_due_bill").text(bden.format(t_due_bill));
      $("#t_freight").text(bden.format(t_freight));
      $("#inv_script").remove();
   });
</script>
<script>
  function vprint(tdd){
    <?php if ($printer_t=='P') { ?>
      $("#modal-sm").modal({backdrop: "static"});
      pagecall('../purchase/purchase_invoice_pos.php?invoice='+ $(tdd).attr('invoice'),'#smdview');
    <?php } elseif ($printer_t=='R') { ?>
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../purchase/purchase_invoice.php?invoice='+ $(tdd).attr('invoice'),'#mdview');
    <?php }?>
  }  

</script>

