<?php
// Monthly invoice generation: Get the invoice report for the current month

// Database connection
require_once '../includes/db_connection.php'; // This should define $conn (mysqli)

// Current month
$month = date("Y-m-"); // e.g., 2025-08-

// Get all merchants with pay-per-invoice enabled
$sql = "SELECT * FROM partners_merchant WHERE merchant_isInvoice = 'Yes'";
$result = $conn->query($sql);

if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_object()) {
        $id = (int)$row->merchant_id;

        // Get all invoices for this merchant in the current month
        $stmt = $conn->prepare("SELECT * FROM partners_invoice WHERE invoice_merchantid = ? AND invoice_date LIKE CONCAT(?, '%')");
        $stmt->bind_param("is", $id, $month);
        $stmt->execute();
        $invoiceResult = $stmt->get_result();

        // You can now process $invoiceResult for each merchant
        while ($invoiceRow = $invoiceResult->fetch_object()) {
            // Example: process invoice row
            // echo "Invoice ID: " . $invoiceRow->invoice_id . "<br>";
        }

        $stmt->close();
    }
}
?>
