<?php require_once('../auth/auth.php');require_once('../auth/acid_balance.php');require_once('../auth/function.php');?>
<?php

$inacc= mysqli_query($conn,"SELECT co_name,owner_name,co_phone,co_email,co_address,co_logo FROM company WHERE coid='$coid' LIMIT 1") or die(mysqli_error($conn));
$cor= mysqli_fetch_assoc($inacc);
$co_logo=$cor['co_logo'];

 $custom_field= mysqli_query($conn,"SELECT customer FROM custom_field WHERE coid='$coid' LIMIT 1 ") or die(mysqli_error($conn));
      $rowcf= mysqli_fetch_assoc ($custom_field);
   
      $p=$rowcf['customer'];
      function input_check($ap)
      {
       if (strpos($GLOBALS['p'], $ap) !== false) { return 1;} 
      }

   $search     = $_GET['search'];
   $bbid       = $_GET['bbid'];
   $bbrid      = $_GET['bbrid'];
   $debitor_id = $_GET['debitor_id'];
   $nrow       = $_GET['nrow'];
   $ttype      = $_GET['ttype'];
   $areaid     = $_GET['areaid'];
   $customer_type = $_GET['customer_type'];
    @$us_em_id    = $_GET['us_em_id'];
   
    if (@$us_em_id=='0') {
       @$emicond = ' '   ;
    }elseif (@$us_em_id >0){
        @$emicond= "  AND accounts.em_id=$us_em_id  ";
    }
    
    if($bbid=='0') {
       $bbidcond = '';
    }elseif ($bbid >0){
        $bbidcond= " AND accounts.bid=$bbid ";
    }

    if ($bbrid=='0') {
       $bbridcond = '';
    }elseif ($bbrid >0){
        $bbridcond= " AND accounts.brid=$bbrid ";
    }
    
       if ($customer_type=='0') {
       $custype_condi = '' ;
    }elseif ($customer_type == 'retail'){
          $custype_condi = " AND accounts.acsub_type='retail' ";
    }elseif ($customer_type == 'ws'){
          $custype_condi = " AND accounts.acsub_type='ws'  ";
    }elseif ($customer_type == 'distri'){
          $custype_condi = " AND accounts.acsub_type='distri' ";
    }elseif ($customer_type == 'emi'){
          $custype_condi = " AND accounts.acsub_type='emi' ";
    }elseif ($customer_type == 'B2B'){
          $custype_condi = " AND accounts.acsub_type='B2B' ";
    }

    if ($areaid=='0') {
       $area_condi = '' ;
    }elseif ($areaid >0){
        $area_condi= " AND accounts.ac_area=$areaid  ";
    }

    if ($ttype=='0') {
       $ttype_condi = '';
    }elseif ($ttype =='dr'){
        $ttype_condi= " AND accounts.balance > 0  ";
    }elseif ($ttype =='cr'){
        $ttype_condi= " AND accounts.balance < 0  ";
    }

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


     $debitor_query= mysqli_query($conn,"SELECT accounts.*,route_name FROM accounts 
        LEFT JOIN route_area ON (accounts.ac_area=route_area.roid)
        WHERE accounts.coid='$coid' $bbidcond $bbridcond $blacksearch $area_condi $ttype_condi  $custype_condi  $emicond  AND gcatid='$debitor_id' ORDER BY accounts.acid LIMIT $nrow ") or die (mysqli_error($conn));
     $dataa = [];
     while ($prow= mysqli_fetch_assoc($debitor_query)){ $dataa[]=$prow;}



?>

 <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:77px;">
                <?php else : ?>
                     <img class="profile-user-img img-fluid img-circle  " src="../../dist/img/a.png" style="width:80px;height:77px;"></div>
                <?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 language(35);?></th>
       </tr>
       <tr>
          <th class="thborder text-center">
      
       Debitor           
              </th>
       </tr>
       <tr>
          <th class="thborder text-center border-bottom-0"><?php echo date('d/m/y', strtotime($co_f_year_start));?> To <?php echo date('d/m/y', strtotime($date));?>
          </th>
       </tr>
    </thead>
 </table>
<table id="debitor_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:%;"><?php echo language(164);?></th>
      
        <?php if(input_check('alias_info')==1){ ?>
        <th class="text-center" style="width:%;"><?php echo language(110);?></th>
          <?php } ?>
         <th class="text-center" style="width:%;"><?php echo language(118);?></th>
         
        <?php if(input_check('email')==1){ ?>
        <th class="text-center" style="width:%;"><?php echo language(112);?></th>
        <?php } ?>
        
        <th class="text-center" style="width:%;"><?php echo language(111);?> </th>
        <th class="text-center" style="width:%;"><?php echo language(116);?> </th>
        
         <?php if(input_check('cus_type_info')==1){ ?>
        <th class="text-center" style="width:%;"><?php echo language(119);?> </th>
        <?php } ?>
        
        <?php if(input_check('area_info')==1){ ?>
        <th class="text-center" style="width:%;"><?php echo language(224);?> </th>
         <?php } ?>
        
        <th class="text-center" style="width:%;"><?php echo language(233);?></th>
    </tr>
</thead>
 <tbody id="debitor_show"></tbody>
     <tfoot> 
        <tr class="sticky-bottom">
           
             <?php if(input_check('alias_info')==1){ ?>
              <th class="text-right" colspan="1"> </th>
             <?php } ?>
           
           
            <th class="text-right" colspan="" style="width:%;"></th>
             <?php if(input_check('email')==1){ ?>
            <th class="text-right" colspan="1"><dt id="t_value"></dt></th>
            <?php } ?>
            
            <?php if(input_check('cus_type_info')==1){ ?>
             <th class="text-right" colspan="1"> </th>
             <?php }?> 
             
             <?php if(input_check('area_info')==1){ ?>
              <th class="text-right" colspan="1"> </th>
              <?php } ?>
               <th class="text-right" colspan="1"> </th>
               <th class="text-right" colspan="1"> </th>
               <th class="text-right" colspan="1"> </th>
                <th class="text-right" colspan="1"> <?php echo language(233);?> </th>
            <th class="text-right" colspan="1"><dt id="t_value"></dt></th>
        </tr>                                 
     </tfoot>   
</table>
<script id="debitview" type="text/javascript">
   $(document).ready(function(){
       var dataa = '';
       var i     = 0;
       var t_val = 0;
       $.each(<?php echo json_encode($dataa);?>, function(key,value){
       t_val  += Number(value.balance);

       dataa += '<tr>';    
       dataa += '<td class="text-center">'+ ++i +'</td>';
       dataa += '<td onclick="acprofile('+value.acid+')">' + value.ac_name +  ' </td>';
         <?php if(input_check('alias_info')==1){ ?>
       dataa += '<td class="text-center">' + value.ac_alise + '</td>';
       <?php } ?>
       dataa += '<td class="text-center">' + value.outlet_id + '</td>';
       
       <?php if(input_check('email')==1){ ?>
       dataa += '<td class="text-center">' + value.ac_email + '</td>';
       <?php } ?>
       
       dataa += '<td class="text-center">' + value.ac_number + '</td>';
       dataa += '<td>' + value.ac_details + '</td>';
       <?php if(input_check('cus_type_info')==1){ ?>
       dataa += '<td>' + value.acsub_type + '</td>';
       <?php } ?>
       <?php if(input_check('area_info')==1){ ?>
       dataa += '<td class="text-center">' + value.route_name + '</td>';
  <?php } ?>
    
       dataa += '<td class="text-right">' + value.balance + '</td>';
       dataa += '</tr>';
   });
    $("#debitor_show").html(dataa);
      $("#t_value").text(bden.format(t_val));

    $("#debitview").remove();
   });
   
   
</script>
<script type="text/javascript">
   function acprofile(acid){    
     $("#modal-xl").modal({backdrop: "static"});
     pagecall('../accounts/ac_profile.php?acid='+acid,'#xlmdview');   
   }  
</script>
