<?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="Supplier Ageing Details";



?>


<!DOCTYPE html>
<html lang="en">


<!-- ======= <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">Supplier:&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=2','acc_reference'); 
				 ?>
                  <input type="text" class="form-control" id="acc_reference" name="acc_reference" value="<?php echo $_REQUEST['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">Date Interval:&nbsp;&nbsp;</td>
        <td width="156"><strong>
          <input type="text" name="fdate" id="fdate" class="form-control" style="width:150px;" value="<?php echo isset($_REQUEST['fdate'])?$_REQUEST['fdate']:date('Y-m-01');?>" />
        </strong></td>
        <td width="61" align="center" ><strong> -to- </strong></td>
        <td width="208" ><strong>
          <input type="text" name="tdate" id="tdate" class="form-control" style="width:150px;" value="<?php echo isset($_REQUEST['tdate'])?$_REQUEST['tdate']:date('Y-m-d');?>" />
        </strong></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="8%" scope="col">Date</th>
                    <th width="12%" scope="col">Bill</th>
                    <th width="30%" scope="col">Supplier </th>
                    <th width="12%" scope="col">Terms</th>
                    <th width="8%" scope="col">Due date</th>
                    <th width="10%" scope="col"><center>DUE DAYS</center> </th>
                    <th width="10%" scope="col" align="right"><div style="text-align:right">Bill AMT</div></th>
                    <th width="10%" scope="col" align="right"><div style="text-align:right">Balance Due</div></th>
                  </tr>
                </thead>
                <tbody>
		<?php
		
		$sql = "select ref_no, full_name from accounts_reference where 1 group by ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $full_name[$info->ref_no]=$info->full_name;
		}
		
		$sql = "select id, acc_terms, net_days from acc_terms where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $acc_terms[$info->id]=$info->acc_terms;
		 $net_days[$info->id]=$info->net_days;
	
		}
		
		 $sql = "select group_id, group_name from ledger_group where 1 group by group_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $group_name[$info->group_id]=$info->group_name;
	
		}
		
		
  $acc_reference = $_REQUEST['acc_reference'];
  $reference=explode(' :: ',$acc_reference);
  $ref_no=$reference[0];
  


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

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

//if($_POST['fdate']!=''&&$_POST['tdate']!='')
$date_con .= ' and m.jv_date between "'.$_REQUEST['fdate'].'" and "'.$_REQUEST['tdate'].'"';
					
					
					
		 $sql = "select ref_no, tr_no, sum(dr_amt) as received_amt from journal_pay_bill where 1 group by ref_no, tr_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $received_amt[$info->ref_no][$info->tr_no]=$info->received_amt;
		}
					
					if($ref_no!='')
					$ref_con=" and m.ref_no='".$ref_no."'";
					
						$sql = 'select m.jv_no, m.view_jv_no, m.jv_date, m.ref_no, m.acc_terms, sum(j.cr_amt) as inv_amt, j.ledger_id  from journal_master m, journal j
						where m.jv_no=j.jv_no  and m.tr_from="Purchase" and j.cr_amt>0 '.$ref_con.$date_con.' group by j.jv_no order by m.jv_date, m.jv_no';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
					
					
<?php
$t_date=date('Y-m-d');
$f_dt = $data->jv_date;		   
//$t_dt =  date('Y-m-d', strtotime('+1 day', strtotime($t_date)));
$t_dt =  date('Y-m-d');

$from_date = strtotime($f_dt);
$to_date = strtotime($t_dt);
$datediff = $to_date - $from_date;
$count_days = round($datediff / (60 * 60 * 24));

		 
		 
		 if($balance_due=$data->inv_amt-$received_amt[$data->ref_no][$data->jv_no]>0) {
		   ?>
			    <tr>
                   <td><?php echo date('d-M-y',strtotime($data->jv_date));?></td>
                   <td><a href="view_file/purchase_bill_print_view.php?jv_no=<?php echo $data->jv_no;;?>" target="_blank"><?php echo $data->view_jv_no;?></a></td>
                   <td><?php echo $full_name[$data->ref_no];?></td>
                   <td><?php echo $acc_terms[$data->acc_terms];?></td>
     			   <td><?php 
						$chkoutdate = $data->jv_date;
   		 				$DueDate =  date('Y-m-d', strtotime($chkoutdate.' + '.$net_days[$data->acc_terms].' days'));?>
						<?php echo date('d-M-y',strtotime($DueDate));?></td>
 				   <td><center><?php echo $count_days;?> DAYS</center></td>
                   <td align="right"><?php echo number_format($data->inv_amt,2); $total_inv_amt+=$data->inv_amt;?></td>
                   <td align="right"><?php echo number_format($balance_due=$data->inv_amt-$received_amt[$data->ref_no][$data->jv_no],2); $total_balance_due+=$balance_due;?></td>
                 </tr>
				  <?php }} ?>
                 <tr>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="center"><strong>TOTAL</strong></td>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="right"><strong><?php echo number_format($total_inv_amt,2);?></strong></td>
                   <td align="right"><strong><?php echo number_format($total_balance_due,2);?></strong></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>