  <?php
  include 'connect.php'; // include database connection file
  include 'header.php'; // include header file
  include 'nav.php'; // include navigation menu file

session_start();
if(!isset($_SESSION['emailadd'])){
   header("Location: index.php");
}

?>


<main>
  <h2>Open Billing</h2>
  <link rel="stylesheet" type="text/css" href="style.css">
      <style>
      /* Define table styles */
      table {
        border-collapse: collapse;
        width: 100%;
      }
      
      th, td {
        text-align: left;
        padding: 8px;
      }
      
      th {
        background-color: #4CAF50;
        color: white;
      }
      
      tr:nth-child(even) {background-color: #f2f2f2;}
      
      /* Define "Add Data" button styles */
      .add-data-button {
        background-color: #4CAF50;
        color: white;
        border: none;
        padding: 10px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        margin: 4px 2px;
        cursor: pointer;
        border-radius: 4px;
      }
      
      .add-data-button:hover {
        background-color: #3e8e41;
      }
    </style>

  <?php
// Get total open invoice by aging buckets for each client
$query = "SELECT supregno, 
                 SUM(CASE WHEN DATEDIFF(CURDATE(), poduedate) < 30 THEN totamt - totpay ELSE 0 END) AS aging_0_30,
                 SUM(CASE WHEN DATEDIFF(CURDATE(), poduedate) BETWEEN 30 AND 59 THEN totamt - totpay ELSE 0 END) AS aging_30_60,
                 SUM(CASE WHEN DATEDIFF(CURDATE(), poduedate) BETWEEN 60 AND 89 THEN totamt - totpay ELSE 0 END) AS aging_60_90,
                 SUM(CASE WHEN DATEDIFF(CURDATE(), poduedate) BETWEEN 90 AND 119 THEN totamt - totpay ELSE 0 END) AS aging_90_120,
                 SUM(CASE WHEN DATEDIFF(CURDATE(), poduedate) BETWEEN 120 AND 179 THEN totamt - totpay ELSE 0 END) AS aging_120_180,
                 SUM(CASE WHEN DATEDIFF(CURDATE(), poduedate) >= 180 THEN totamt - totpay ELSE 0 END) AS aging_180_plus
          FROM sup_invhead 
          WHERE totamt - totpay > 0
          GROUP BY supregno
          HAVING SUM(totamt - totpay) > 0";
$result = mysqli_query($conn, $query);

// Initialize column subtotals
$aging_0_30_subtotal = 0;
$aging_30_60_subtotal = 0;
$aging_60_90_subtotal = 0;
$aging_90_120_subtotal = 0;
$aging_120_180_subtotal = 0;
$aging_180_plus_subtotal = 0;

// Generate the HTML table
echo '<table>';
echo '<tr><th>Supplier</th><th>0-30 Days</th><th>31-60 Days</th><th>61-90 Days</th><th>91-120 Days</th><th>121-180 Days</th><th>180+ Days</th></tr>';
while ($row = mysqli_fetch_assoc($result)) {
  echo '<tr>';
  echo '<td>' . $row['supregno'] . '</td>';
  echo '<td>' . number_format($row['aging_0_30'], 2) . '</td>';
  echo '<td>' . number_format($row['aging_30_60'], 2) . '</td>';
  echo '<td>' . number_format($row['aging_60_90'], 2) . '</td>';
  echo '<td>' . number_format($row['aging_90_120'], 2) . '</td>';
  echo '<td>' . number_format($row['aging_120_180'], 2) . '</td>';
  echo '<td>' . number_format($row['aging_180_plus'], 2) . '</td>';
  echo '</tr>';

  // Add column subtotals
  $aging_0_30_subtotal += $row['aging_0_30'];
  $aging_30_60_subtotal += $row['aging_30_60'];
  $aging_60_90_subtotal += $row['aging_60_90'];
  $aging_90_120_subtotal += $row['aging_90_120'];
  $aging_120_180_subtotal += $row['aging_120_180'];
  $aging_180_plus_subtotal += $row['aging_180_plus'];
}

// Add column subtotals row
echo '<tr>';
echo '<td><b>Subtotal</b></td>';
echo '<td>' . number_format($aging_0_30_subtotal, 2) . '</td>';
echo '<td>' . number_format($aging_30_60_subtotal, 2) . '</td>';
echo '<td>' . number_format($aging_60_90_subtotal, 2) . '</td>';
echo '<td>' . number_format($aging_90_120_subtotal, 2) . '</td>';
echo '<td>' . number_format($aging_120_180_subtotal, 2) . '</td>';
echo '<td>' . number_format($aging_180_plus_subtotal, 2) . '</td>';
echo '</tr>';

echo '</table>';

  
  ?>

</main>
