<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');require_once('../auth/function.php');?>
<?php
    $search      = $_GET['search'];
    $bbid        = $_GET['bbid'];
    $bbrid       = $_GET['bbrid'];
    $fdate       = $_GET['fdate'];
    $edate       = $_GET['edate'];
    $nrow        = $_GET['nrow'];
    $vou_type    = $_GET['vou_type'];
    $acid        = $_GET['acid'];
    $s_uid       = $_GET['s_uid'];
    $status      = $_GET['status'];
    if ($bbid=='0') {
       $bbidcond = '';
    }elseif ($bbid >0){
        $bbidcond= " AND transaction.bid=$bbid";
    }
    if ($bbrid=='0') {
       $bbridcond = '';
    }elseif ($bbrid >0){
        $bbridcond= " AND transaction.brid=$bbrid";
    }



    if ($status=='all') {
       $statuscond = '';
    }elseif ($status=='0'){
       $statuscond = " AND transaction.active=$status";
    }elseif ($status >0){
        $statuscond= " AND transaction.active=$status";
    }
    
    
    
     
    if ($vou_type=='0') {
    	 $voucond = '';
    }elseif ($vou_type=='R' ){
    	  $voucond = " AND (transaction.ttype = 'R')" ;
    }elseif ($vou_type=='INSR' ){
    	  $voucond = " AND (transaction.ttype = 'INSR')" ;
    }elseif ($vou_type=='P' ){
    	  $voucond = " AND (transaction.ttype = 'P')" ;
    }elseif ($vou_type=='JUR' ){
    	  $voucond = " AND (transaction.ttype = 'JUR')" ;
    }elseif ($vou_type=='C' ){
    	  $voucond = " AND (transaction.ttype = 'C')" ;
    }elseif ($vou_type=='W_R' ){
    	  $voucond = " AND (transaction.ttype = 'W_R')" ;
    }elseif ($vou_type=='R_R' ){
    	  $voucond = " AND (transaction.ttype = 'R_R')" ;
    }elseif ($vou_type=='R_I'){
    	   $voucond = " AND (transaction.ttype = 'R_I')" ;
    }
    
    
     if ($acid=='0') {
    	 $accond = '';
    }elseif ($acid >0){
    	  $accond = "AND (transaction.dr_acid='$acid' || transaction.cr_acid='$acid')" ;
    }
    
    
        if($s_uid == '0'){
         $s_uidcond = '';
    }elseif ($s_uid > 0){
       $s_uidcond=  " AND transaction.em_id='$s_uid' ";
    }

    if ($search =='0') {
       $blacksearch='';
     }else{
       $blacksearch = " AND (a.ac_name LIKE '%$search%' || a.ac_alise LIKE '%$search%' || a.outlet_id LIKE '%$search%' || a1.ac_name LIKE '%$search%' || a1.ac_alise LIKE '%$search%' || a1.outlet_id LIKE '%$search%')";
     }

    $acledger = mysqli_query($conn,"SELECT tid,transaction.active,a.ac_name dr_name,a.outlet_id dr_outlet,a1.ac_name cr_name,a1.outlet_id cr_outlet,dr_acid,cr_acid,amount,transaction.em_id,em_employee.em_acname,
    DATE_FORMAT(date, '%d/%m/%Y') date,details,ttype,invoice,note FROM tran_cheque transaction 
    LEFT JOIN accounts a ON (a.acid=transaction.dr_acid ) 
    LEFT JOIN accounts a1 ON (a1.acid=transaction.cr_acid )
    LEFT JOIN em_employee ON (em_employee.em_id=transaction.em_id)
    WHERE transaction.coid = '$coid' $bbidcond $bbridcond $voucond $s_uidcond $blacksearch $statuscond  $accond AND date(date) BETWEEN '$fdate'  AND '$edate' AND ttype in('OP','OPINS','SOP','EADJ','R','INSR','P','JUR','C','R_I','R_R','W_R') GROUP BY invoice ORDER BY date(date),tid LIMIT $nrow") or die(mysqli_error($conn));
     $data = [];
    while($ldg= mysqli_fetch_assoc($acledger)){$data[]=$ldg; }

?>

  <table id="lgtable<?php echo $acid;?>" class="ttable table-sm table-bordered table-hover" >
                 <thead>
                    <tr class="sticky-top">
                       <th class="text-center" style="width:3%;"><?php echo language(15);?></th>
                       <th class="text-center" style="width:10%;"><?php echo language(131);?></th>
                       <th class="text-center" style="width:15%;"><?php echo language(408);?></th>
                       <th class="text-center" style="width:15%;"><?php echo language(295);?></th>
                       <th class="text-center" style="width:15%;"><?php echo language(217);?></th>                       
                       <th class="text-center" style="width:15%;"><?php echo language(218);?></th>
                       <th class="text-center" style="width:10%;"><?php echo language(377);?></th>
                       <th class="text-center" style="width:10%;"><?php echo language(151);?></th>
                       <th class="text-center" style="width:10%;"><?php echo language(290);?></th>
                       <th class="text-center" style="width:10%;"><?php echo language(214);?></th>
                       <th class="text-center" style="width:10%;">#</th>
                       <th class="text-center" style="width:10%;">#</th>


                    </tr>
                 </thead>
                 <tbody>
                     
                     
                 </tbody>
                 <tfoot> 
                    <tr class="sticky-bottom">
                       <td class="text-right" colspan="7"><dt>Current Total</dt></td>
                       
                  
                          <td class="text-center"><dt id="sumcr"></dt></td>
             
                    </tr>                               	
                 </tfoot>
              </table>
<script>
	$(document).ready(function () {
			var data = '';
			var i = 1;
			let t_bill     = 0;
			$.each(<?php echo json_encode($data)?>, function (key, value) {
               t_bill      += Number(value.amount);


				data += '<tr>';
				data += '<td class="text-center">' + i++ + '</td>';
				data += '<td class="text-center">' +value.date + '</td>';
				data += '<td class="text-center">' +value.note + '</td>';

				
				data += '<td>' + value.details + '</td>';
				
					if(value.cr_acid==0){
				   data += '<td>' + 'N/A'+'</td>';
                }else{
                   data += '<td>' + '<a href="#" onclick="ac_profile('+value.cr_acid+')">'+value.cr_name+'  '+ value.cr_outlet +' </a>'+'</td>';
                }
				
				
				if(value.dr_acid==0){
				   data += '<td>' + 'N/A'+'</td>';
                }else{
                   data += '<td>' + '<a href="#" onclick="ac_profile('+value.dr_acid+')">'+value.dr_name+'  '+ value.dr_outlet +' </a>'+'</td>';
                }

            if (value.active==1) {
				data += '<td class="text-center bg-danger" >' + vtype(value.ttype) + '</td>';
}else{
            data += '<td class="text-center btn-outline-dark" onclick="vprint(this)" type="'+value.ttype+'" tid="'+value.tid+'">' + vtype(value.ttype) + '</td>';

}
   
                data += '<td class="text-center btn-outline-dark" onclick="cheques_load('+value.tid+')">' + (value.invoice).split("-")[5] + '</td>';
				data += '<td class="text-center">' + value.amount + ' </td>';
				data += '<td class="text-center">' + value.em_acname + '   </td>';

if (value.active==1) {

            data += '<td class="text-center">'  + 'Success Withdrwon' + '   </td>';

}else{
               data += '<td class="text-center">'  + 'Pendding' + '   </td>';

}
				data += '<td class="text-center">' + 'Re Open' + '   </td>';


				data += '</tr>';
			});
			
			$("#sumcr").text(bden.format(t_bill));
			$('#lgtable<?php echo $acid;?> tbody').append(data);
            toastr.success(Showing, Successfully);
	});
</script>

<script>
  function vprint(tdd){
     var ttp = $(tdd).attr("type");
     var tid = $(tdd).attr("tid");
     var invoice = $(tdd).next().text();
     if (ttp=='R') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../voucher/normal_recevied_preview.php?n_re_invoice='+invoice,'#mdview');  
     } else if(ttp=='P' || ttp=='R_R') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../cheque_payment/withdrow_voucher_index.php?tid='+tid+'&ttp='+ttp,'#mdview'); 

      // pagecall('../cheque_payment/cheques_preview.php?tid='+tid+'&ttp='+ttp,'#mdview'); 


     } else if(ttp=='C') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../voucher/con_money_receipt.php?tid='+tid,'#mdview');
     } else if(ttp=='OP' || 'SOP') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../voucher/op_money_receipt.php?tid='+tid,'#mdview');
     }
  }
</script>

<script>
 function ac_profile(acid){    
   $("#modal-xl").modal({backdrop: "static"});
    pagecall('../accounts/ac_profile.php?acid='+acid,'#xlmdview');   
   } 
   
    function cheques_load(tid){    
   $("#modal-xl").modal({backdrop: "static"});
    pagecall('../cheque_payment/cheques_preview.php?tid='+tid,'#mdview');   
   } 
   
   
</script>
