<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');
$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];
$page_title="Customer Ageing Summary";



?>


<!DOCTYPE html>
<html lang="en">
<style type="text/css">
<!--
.style7 {font-weight: bold}
-->
</style>


<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->

<?php
  do_calander('#fdate');
  do_calander('#tdate');

  ?>
  

<body>

  <!-- ======= Header ======= -->
 
  
  <?php 
  require_once('inc_files/inc_header.php');
  ?>
  
  <!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <?php  require_once('inc_files/inc_menu.php'); ?><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
		  <li class="breadcrumb-item"> Reports</li>    
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section">
      <div class="row">
	  
	
		<div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
            <p class="card-title" style="font-size:14px; margin-bottom:0; padding-bottom:0;"><a href="61_reports_all.php?active_id=901&menu_id=9">< Back To Report List </a></p>

              <!-- No Labels Form -->
           <form class="row g-3" action="" method="post" name="codz" id="codz">
   		       
			
	<table width="80%" border="0" align="center">
      
      
      <tr height="50">
        <td align="right">Customer:&nbsp;&nbsp;</td>
        <td colspan="3">
		 <?php 
				 auto_complete_from_db_sql($conn,'SELECT concat(a.ref_no," :: ",a.full_name), concat(a.ref_no," :: ",a.full_name) FROM accounts_reference a
					 WHERE ref_type=1','acc_reference'); 
				 ?>
                  <input type="text" class="form-control" id="acc_reference" name="acc_reference" value="<?php echo $_POST['acc_reference']?>" >		</td>
        <td rowspan="3" align="center" ><strong>
          &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> Run Report&nbsp;&nbsp;</button>
        </strong></td>
      </tr>
      
      <tr height="50">
        <td align="right">As of:&nbsp;&nbsp;</td>
        <td width="156"><strong>
          <input type="text" name="tdate" id="tdate" class="form-control" style="width:150px;" value="<?php echo isset($_POST['tdate'])?$_POST['tdate']:date('Y-m-d');?>" />
        </strong></td>
        <td width="61" align="center" >&nbsp;</td>
        <td width="208" >&nbsp;</td>
      </tr>
    </table>
               
  </form><!-- End No Labels Form -->

            </div>
          </div>
		
		</div>
		
		
	  
	  
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			<br>
			
			
             
			
			 
             

              <!-- Table with stripped rows -->
              <table width="36%" class="table table-sm" style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col">Code</th>
                    <th width="30%" scope="col">Customer </th>
                    <th width="12%" scope="col"><div style="text-align:right"><?php /*?><?=$first_f;?>  <?=$first_t;?><?php */?> 1-30</div> </th>
                    <th width="12%" scope="col"><div style="text-align:right"><?php /*?><?=$second_f;?>  <?=$second_t;?><?php */?> 31-60</div></th>
                    <th width="12%" scope="col"><div style="text-align:right"><?php /*?><?=$third_f;?>  <?=$third_t;?><?php */?> 61-90</div></th>
                    <th width="12%" scope="col"><div style="text-align:right"><?php /*?><?=$last_t;?><?php */?>   91 and Over</div></th>
                    <th width="12%" scope="col"><div style="text-align:right">TOTAL</div></th>
                  </tr>
                </thead>
                <tbody>
				
				
<?php
					
  $acc_reference = $_POST['acc_reference'];
  $reference=explode(' :: ',$acc_reference);
  $ref_no=$reference[0];
  
if($ref_no!=''){
$ref_con=" and a.ref_no='".$ref_no."'";
}


if($_POST['fdate']==''){
$_POST['fdate']=date('Y-m-01');
}

if($_POST['tdate']==''){
$_POST['tdate']=date('Y-m-d');
}

//if($_POST['fdate']!=''&&$_POST['tdate']!='')
$date_con .= ' and m.jv_date between "'.$_POST['fdate'].'" and "'.$_POST['tdate'].'"';
					

	  $first_t = $_POST['tdate'];
      $first_f =  date('Y-m-d', strtotime($first_t.' - 29 days'));
	 
	  $second_t =  date('Y-m-d', strtotime($first_f.' - 1 days'));
	  $second_f =  date('Y-m-d', strtotime($second_t.' - 29 days'));
	  
	  $third_t =  date('Y-m-d', strtotime($second_f.' - 1 days'));
	  $third_f =  date('Y-m-d', strtotime($third_t.' - 29 days'));
	  
	  $last_f='2000-01-01';
	  $last_t =  date('Y-m-d', strtotime($third_f.' - 1 days'));
	  
		
		


	?>
				
				
				
				
		<?php
		
		// $sql = "select ledger_id, ref_no, sum(dr_amt-cr_amt) as opening_amt from journal where jv_date <'".$_POST['fdate']."' group by ledger_id, ref_no";
//		 $query = mysqli_query($conn,$sql);
//		 while($info=mysqli_fetch_object($query)){
//  		 $opening_amt[$info->ledger_id][$info->ref_no]=$info->opening_amt;
//		 }
		 
		 
		
		
 		 $sql = "select ledger_id, ref_no, sum(dr_amt) as first_dr from journal where jv_date between '".$first_f."' and '".$first_t."' and tr_from='Sales' group by ledger_id, ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $first_dr[$info->ledger_id][$info->ref_no]=$info->first_dr;
		}
		
		 $sql = "select ledger_id, ref_no, sum(cr_amt) as first_cr from journal_receive_payment where tr_date between '".$first_f."' and '".$first_t."' group by ledger_id, ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $first_cr[$info->ledger_id][$info->ref_no]=$info->first_cr;
		}
		
		
		$sql = "select ledger_id, ref_no, sum(dr_amt) as second_dr from journal where jv_date between '".$second_f."' and '".$second_t."' and tr_from='Sales' group by ledger_id, ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $second_dr[$info->ledger_id][$info->ref_no]=$info->second_dr;
		}
		
		$sql = "select ledger_id, ref_no, sum(cr_amt) as second_cr from journal_receive_payment where tr_date between '".$second_f."' and '".$second_t."' group by ledger_id, ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $second_cr[$info->ledger_id][$info->ref_no]=$info->second_cr;
		}
		
		
		
		 $sql = "select ledger_id, ref_no, sum(dr_amt) as third_dr from journal where jv_date between '".$third_f."' and '".$third_t."' and tr_from='Sales' group by ledger_id, ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $third_dr[$info->ledger_id][$info->ref_no]=$info->third_dr;
		}
		
		$sql = "select ledger_id, ref_no, sum(cr_amt) as third_cr from journal_receive_payment where tr_date between '".$third_f."' and '".$third_t."' group by ledger_id, ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $third_cr[$info->ledger_id][$info->ref_no]=$info->third_cr;
		}
		
		
		 $sql = "select ledger_id, ref_no, sum(dr_amt) as last_dr from journal where jv_date <='".$last_t."' and tr_from='Sales' group by ledger_id, ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $last_dr[$info->ledger_id][$info->ref_no]=$info->last_dr;
		}
		
		
		 $sql = "select ledger_id, ref_no, sum(cr_amt) as last_cr from journal_receive_payment where tr_date <='".$last_t."' group by ledger_id, ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $last_cr[$info->ledger_id][$info->ref_no]=$info->last_cr;
		}
		

	
						
						
					
						$sql = 'select a.* from accounts_reference a where a.ref_type=1 '.$ref_con.' order by a.ref_no';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td><?php echo $data->ref_no;?></td>
                   <td><?php echo $data->full_name;?></td>
                   <td align="right">
					<a href="61_sales_05.php?show=show&fdate=<?=$first_f;?>&tdate=<?=$first_t;?>&acc_reference=<?=$data->ref_no.' :: '.$data->full_name?>" target="_blank" style="text-decoration:none; ">
                     <?php echo number_format($first_dr_amt=$first_dr[$data->ledger_id][$data->ref_no]-$first_cr[$data->ledger_id][$data->ref_no],2); $total_first_dr_amt +=$first_dr_amt;?>
					 </a>
                   </td>
                   <td align="right">
				   <a href="61_sales_05.php?show=show&fdate=<?=$second_f;?>&tdate=<?=$second_t;?>&acc_reference=<?=$data->ref_no.' :: '.$data->full_name?>" target="_blank" style="text-decoration:none; ">
                     <?php echo number_format($second_dr_amt=$second_dr[$data->ledger_id][$data->ref_no]-$second_cr[$data->ledger_id][$data->ref_no],2); $total_second_dr_amt +=$second_dr_amt;?>
					 </a>
                   </td>
                   <td align="right">
				    <a href="61_sales_05.php?show=show&fdate=<?=$third_f;?>&tdate=<?=$third_t;?>&acc_reference=<?=$data->ref_no.' :: '.$data->full_name?>" target="_blank" style="text-decoration:none; ">
                     <?php echo number_format($third_dr_amt=$third_dr[$data->ledger_id][$data->ref_no]-$third_cr[$data->ledger_id][$data->ref_no],2); $total_third_dr_amt +=$third_dr_amt;?>
					 </a>
                  </td>
                   <td align="right">
				   <a href="61_sales_05.php?show=show&fdate=<?=$last_f;?>&tdate=<?=$last_t;?>&acc_reference=<?=$data->ref_no.' :: '.$data->full_name?>" target="_blank" style="text-decoration:none; ">
                     <?php echo number_format($last_dr_amt=$last_dr[$data->ledger_id][$data->ref_no]-$last_cr[$data->ledger_id][$data->ref_no],2); $total_last_dr_amt +=$last_dr_amt;?>
					 </a>
                   </td>
                   <td align="right"><span style=" font-size:14px; color:#000000;">
                     <?php echo number_format($total_due_amt = ($first_dr_amt+$second_dr_amt+$third_dr_amt+$last_dr_amt),2); $grand_total_due +=$total_due_amt;?>
                   </span></td>
                  </tr>
				  <?php }?>
                 <tr>
                   <td>&nbsp;</td>
                   <td align="center"><strong>TOTAL</strong></td>
                   <td align="right"><span class="style7">
                     <?php echo number_format($total_first_dr_amt,2);  ?>
                   </span></td>
                   <td align="right"><span class="style7">
                     <?php echo number_format($total_second_dr_amt,2);  ?>
                   </span></td>
                   <td align="right"><span class="style7">
                     <?php echo number_format($total_third_dr_amt,2);  ?>
                   </span></td>
                   <td align="right"><span class="style7">
                     <?php echo number_format($total_last_dr_amt,2);  ?>
                   </span></td>
                   <td align="right"><span class="style7">
                     <?php echo number_format($grand_total_due,2);  ?>
                   </span></td>
                  </tr>
                </tbody>
              </table>
			  
			
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

  </main><!-- End #main -->

  
 <!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= End Footer ======= -->

</body>

</html>