<?php
// Include database connection file, header file, and navigation menu file
include 'connect.php';
include 'header.php';
include 'nav.php';

// Check if user is logged in and has permission to view this page
session_start();
if(!isset($_SESSION['emailadd'])){
   header("Location: index.php");
}


// Get the clientregno of the client to retrieve open invoices for (replace 12345 with the actual clientregno value)
$clientregno = $_GET['clientregno'];

// Get the open invoices for the client
$query = "SELECT invno, invdate, invduedate, totamt, totrec, DATEDIFF(CURDATE(), invduedate) AS age
          FROM invhead 
          WHERE totamt - totrec > 0 AND clientregno = '$clientregno'";
$result = mysqli_query($conn, $query);

// Get the name of the client from the customer table
$query2 = "SELECT custname FROM customer WHERE clientregno = '$clientregno'";
$result2 = mysqli_query($conn, $query2);
$row2 = mysqli_fetch_assoc($result2);
$clientname = $row2['custname'];

// Initialize variables to hold the totals for each aging category
$aging_0_30 = 0;
$aging_31_60 = 0;
$aging_61_90 = 0;
$aging_91_120 = 0;
$aging_121_180 = 0;
$aging_180_plus = 0;

// Generate the HTML table
echo '<main>';
echo '<h2>Open Invoices for ' . $clientname . '</h2>';
echo '<link rel="stylesheet" type="text/css" href="style.css">';
echo '<table>';
echo '<tr><th>Invoice No.</th><th>Invoice Date</th><th>Due Date</th><th>Age (Days)</th><th>Total Amount</th><th>Total Received</th><th>Balance</th></tr>';
while ($row = mysqli_fetch_assoc($result)) {
  echo '<tr>';
  echo '<td><a href="show_invoice.php?invno=' . $row['invno'] . '">' . $row['invno'] . '</a></td>';
  echo '<td>' . $row['invdate'] . '</td>';
  echo '<td>' . $row['invduedate'] . '</td>';
  echo '<td>' . $row['age'] . '</td>';
  echo '<td>' . number_format($row['totamt'], 2) . '</td>';
  echo '<td>' . number_format($row['totrec'], 2) . '</td>';
  echo '<td>' . number_format($row['totamt'] - $row['totrec'], 2) . '</td>';
  echo '</tr>';

  // Update totals for each aging category
  if ($row['age'] <= 30) {
    $aging_0_30 += ($row['totamt'] - $row['totrec']);
  } elseif ($row['age'] <= 60) {
    $aging_31_60 += ($row['totamt'] - $row['totrec']);
  } elseif ($row['age'] <= 90){
    $aging_61_90 += ($row['totamt'] - $row['totrec']);
  }  elseif ($row['age'] <= 120) {
    $aging_61_90 += ($row['totamt'] - $row['totrec']);
  } elseif ($row['age'] <= 180) {
    $aging_91_120 += ($row['totamt'] - $row['totrec']);
  } elseif ($row['age'] <= 180) {
    $aging_121_180 += ($row['totamt'] - $row['totrec']);
  } else {
    $aging_180_plus += ($row['totamt'] - $row['totrec']);
  }
}

// Add a row for the subtotals for each aging category
echo '<tr>';
echo '<td colspan="7"><h3>Aging Summary</h3></td>';
echo '</tr>';
echo '<tr>';
echo '<td>0-30 Days:</td><td colspan="6">' . number_format($aging_0_30, 2) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>31-60 Days:</td><td colspan="6">' . number_format($aging_31_60, 2) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>61-90 Days:</td><td colspan="6">' . number_format($aging_61_90, 2) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>91-120 Days:</td><td colspan="6">' . number_format($aging_91_120, 2) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>121-180 Days:</td><td colspan="6">' . number_format($aging_121_180, 2) . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td>180+ Days:</td><td colspan="6">' . number_format($aging_180_plus, 2) . '</td>';
echo '</tr>';

echo '</table>';
echo '</main>';

// Close database connection
mysqli_close($conn);