<?php require_once('../auth/auth.php');?>
<?php 
 $gtype    = $_GET['gtype'];
 $gbid     = $_GET['gbid'];
 $gbrid    = $_GET['gbrid'];
 $fdate    = $_GET['fdate'];
 $edate    = $_GET['edate'];

if ($gtype=='Sales'){
 $contype = " AND (ttype = 'ECSL' || ttype = 'ERSL' || ttype = 'INSL')";
}else if($gtype=='R'){
 $contype = " AND (ttype = 'R' || ttype = 'INSR' ||   ttype = 'W_R' ||  ttype = 'R_R' || ttype = 'R_I' || ttype = 'JUR')";
}else{
 $contype = " AND ttype = '$gtype'";
}                 


 $d_sub = mysqli_query($conn,"SELECT tid,ttype,ac_name,amount,invoice FROM transaction LEFT JOIN accounts ON (accounts.acid=(IF((ttype = 'EPUR' || ttype = 'R' || ttype = 'INSR' || ttype = 'W_R' || ttype = 'R_R' || ttype = 'R_I'), transaction.cr_acid, transaction.dr_acid))) WHERE transaction.coid = '$coid' AND transaction.bid = '$gbid' AND transaction.brid = '$gbrid' AND transaction.active='1' $contype AND date(date) BETWEEN '$fdate' AND '$edate' GROUP BY invoice ORDER BY tid") or die(mysqli_error($conn));
    $ddata = [];
  while($dldg= mysqli_fetch_assoc($d_sub)){
    $ddata[]=$dldg;
  }
?>
<table style="width:100%;" id="subtable<?php echo $gtype;?>">
  <thead>
    <tr>
       <th class="text-center" style="width:5%;">S/l</th>
       <th style="width:75%;">Account Name</th>
       <th style="width:75%;">Type</th>
       <th class="text-center" style="width:10%;">Voucher</th>
       <th class="text-center" style="width:10%;">Amount</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

<script id="script">
  $(document).ready(function () {
      let ddata = '';
      let x = 1;
      $.each(<?php echo json_encode($ddata)?>, function (key, svalue) {

        ddata += '<tr>';
        ddata += '<td class="text-center">' + x++ + '</td>';
        ddata += '<td>' + svalue.ac_name +'</td>';


	    ddata += '<td class="text-center btn-outline-dark" onclick="vprint(this)" type="'+svalue.ttype+'" tid="'+svalue.tid+'">' + vtype(svalue.ttype) + '</td>';




        ddata += '<td class="text-center btn-outline-dark" onclick="vprint(this)" invoice="'+svalue.invoice+'" tid="'+svalue.tid+'" type="'+svalue.ttype+'">' + (svalue.invoice).split("-")[5] + '</td>';

        ddata += '<td class="text-right">' + bden.format(Number(svalue.amount)) + '</td>';

        ddata += '</tr>';
      });
      
      $('#subtable<?php echo $gtype;?> tbody').html(ddata);
      $('#script').remove();
        toastr.success(Showing, yourdata);
  }); 
</script>
<script>
  function vprint(tdd){
     let ttp = $(tdd).attr("type");
     let tid = $(tdd).attr("tid");
     let invoice = $(tdd).attr("invoice");
     if (ttp=='R') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../voucher/re_money_receipt.php?tid='+tid,'#mdview');  
     } else if(ttp=='P') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../voucher/pay_money_receipt.php?tid='+tid,'#mdview'); 
     } else if(ttp=='C') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../voucher/con_money_receipt.php?tid='+tid,'#mdview');
     } else if(ttp=='OP') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../voucher/op_money_receipt.php?tid='+tid,'#mdview');
     } else if(ttp=='EPUR'){
    <?php if ($printer_t=='P') { ?>
      $("#modal-sm").modal({backdrop: "static"});
      pagecall('../purchase/purchase_invoice_pos.php?invoice='+invoice,'#smdview');
    <?php } elseif ($printer_t=='R') { ?>
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../purchase/purchase_invoice.php?invoice='+invoice,'#mdview');
    <?php } ?>
     } else if(ttp=='ERSL'){
    <?php if ($printer_t=='P') { ?>
      $("#modal-sm").modal({backdrop: "static"});
      pagecall('../sales/sales_invoice_pos.php?invoice='+invoice,'#smdview');
    <?php } elseif ($printer_t=='R') { ?>
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../sales/sales_invoice.php?invoice='+invoice,'#mdview');
    <?php } ?>
     } else if(ttp=='ECSL'){
    <?php if ($printer_t=='P') { ?>
      $("#modal-sm").modal({backdrop: "static"});
      pagecall('../sales/c_sales_invoice_pos.php?invoice='+invoice,'#smdview');
    <?php } elseif ($printer_t=='R') { ?>
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../sales/c_sales_invoice.php?invoice='+invoice,'#mdview');
    <?php } ?>
     }
  }  
</script>