<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');require_once('../auth/function.php');?>
<?php
$pritner_setup_query= mysqli_query($conn,"SELECT co_logo FROM company 
 WHERE coid ='$coid'  LIMIT 1 ") or die (mysql_error());
$rowps= mysqli_fetch_assoc ($pritner_setup_query);
$co_logo=$rowps['co_logo'];
    $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'];


   $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 transaction.bid=$bbid";
    }
    if ($bbrid=='0') {
       $bbridcond = '';
    }elseif ($bbrid >0){
        $bbridcond= " AND transaction.brid=$bbrid";
    }
    
    
     
    if ($vou_type=='0') {
    	 $voucond = '';
    }elseif ($vou_type=='OP' ){
    	  $voucond = " AND (transaction.ttype = 'OP')" ;
    }elseif ($vou_type=='OPINS' ){
    	  $voucond = " AND (transaction.ttype = 'OPINS')" ;
    }elseif ($vou_type=='SOP' ){
    	  $voucond = " AND (transaction.ttype = 'SOP')" ;
    }elseif ($vou_type=='EADJ' ){
    	  $voucond = " AND (transaction.ttype = 'EADJ')" ;
    }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,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 FROM 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 AND transaction.active='1' $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 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:75px;">
                        <?php else : ?>
                           <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:75px;">
                        <?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 $branch_name;?></th>
     </tr>
     <tr>
        
            <th class="thborder text-center"><?php echo 'Vouchar History';?></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="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(295);?></th>
                       <th class="text-center" style="width:15%;"><?php echo "Debitor";?></th>
                       <th class="text-center" style="width:15%;"><?php echo "Creditor";?></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>
                    </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>' + value.details + '</td>';
				data += '<td>' + '<a href="#" onclick="ac_profile('+value.dr_acid+')">'+value.dr_name+'  '+ value.dr_outlet +' </a>'+'</td>';
				data += '<td>' + '<a href="#" onclick="ac_profile('+value.cr_acid+')">'+value.cr_name+'  '+ value.cr_outlet +' </a>'+'</td>';
				data += '<td class="text-center btn-outline-dark" onclick="vprint(this)" invoice="'+value.invoice+'" type="'+value.ttype+'" tid="'+value.tid+'">' + vtype(value.ttype) + '</td>';
                data += '<td class="text-center btn-outline-dark" ">' + (value.invoice).split("-")[5] + '</td>';
				data += '<td class="text-center">' + value.amount + ' </td>';
				data += '<td class="text-center">' + value.em_acname + '   </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).attr("invoice");
     if (ttp=='R') {
      $("#modal-lg").modal({backdrop: "static"});
      pagecall('../voucher/re_money_receipt.php?re_invoice='+invoice,'#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' || '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');   
   } 
</script>
