<?php
include_once('../../../classes/manager.class.php');
include_once('../../../classes/PHPMailer/PHPMailerAutoload.php');
include_once('../../../fpdf/fpdfreportlogoL.php');
$m = new Manager();
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

// Retrieve filter values
$datefrom = isset($_GET['datefrom']) ? date("Y-m-d", strtotime($_GET['datefrom'])) : date('Y-m-d', strtotime("-30 days"));
$dateto = isset($_GET['dateto']) ? date("Y-m-d", strtotime($_GET['dateto'])) : date('Y-m-d');
$supplierId = isset($_GET['supplier_name']) ? $_GET['supplier_name'] : 'All';

// Get Ledger Data
$result = SupplierSetup::GetSupplierLedger($datefrom, $dateto, $supplierId);
//$result = SupplierSetup::GetSupplierLedger($datefrom, $dateto, $supplierId);
$opening_balance = isset($result['opening_balance']) ? $result['opening_balance'] : [];
$ledger = isset($result['ledger']) ? $result['ledger'] : [];

// Create PDF
$pdf = new FPDF();

$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 14);
$pdf->Cell(190, 10, 'Supplier Ledger Summary Report', 0, 1, 'C');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(190, 6, "Date Range: " . date("d-m-Y", strtotime($datefrom)) . " to " . date("d-m-Y", strtotime($dateto)), 0, 1, 'C');
$pdf->Cell(190, 6, "Supplier: " . ($supplierId === 'All' ? 'All Suppliers' : $supplierId), 0, 1, 'C');
$pdf->Ln(10);

// Table Header
$pdf->SetFont('Arial', 'B', 10);
$pdf->SetFillColor(200, 200, 200);
$pdf->Cell(60, 8, 'Supplier Name', 1, 0, 'C', true);
$pdf->Cell(30, 8, 'Opening Balance', 1, 0, 'R', true);
$pdf->Cell(30, 8, 'Total Debit', 1, 0, 'R', true);
$pdf->Cell(30, 8, 'Total Credit', 1, 0, 'R', true);
$pdf->Cell(30, 8, 'Closing Balance', 1, 1, 'R', true);
$pdf->SetFont('Arial', '', 10);

// Table Data
$grandTotalOpening = $grandTotalDebit = $grandTotalCredit = $grandTotalBalance = 0;

$supplierIds = array_unique(array_merge(
    array_column($opening_balance, 'supplier_id'),
    array_column($ledger, 'supplier_id')
));

foreach ($supplierIds as $supplierId) {
    $supplierOpeningData = null;
    foreach ($opening_balance as $ob) {
        if ($ob->supplier_id  === $supplierId) {
            $supplierOpeningData = $ob;
            break;
        }
    }

    $supplierOpeningBalance = isset($supplierOpeningData->opening_balance) ? $supplierOpeningData->opening_balance : 0;
    $supplierName = isset($supplierOpeningData->supplier_name) ? $supplierOpeningData->supplier_name : 'Unknown';

    $supplierTotalDebit = $supplierTotalCredit = 0;
    $supplierRunningBalance = $supplierOpeningBalance;

    $supplierLedgerEntries = array();
    foreach ($ledger as $entry) {
        if ($entry->supplier_id === $supplierId) {
            $supplierLedgerEntries[] = $entry;
        }
    }

    foreach ($supplierLedgerEntries as $entry) {
        $debit = isset($entry->debit) ? (float)$entry->debit : 0;
        $credit = isset($entry->credit) ? (float)$entry->credit : 0;
        $supplierRunningBalance += ($debit - $credit);
        $supplierTotalDebit += $debit;
        $supplierTotalCredit += $credit;
    }

    if ($supplierRunningBalance == 0 && $supplierTotalDebit == 0 && $supplierTotalCredit == 0) continue;

    $pdf->Cell(60, 8, $supplierName, 1);
    $pdf->Cell(30, 8, number_format($supplierOpeningBalance, 2), 1, 0, 'R');
    $pdf->Cell(30, 8, number_format($supplierTotalDebit, 2), 1, 0, 'R');
    $pdf->Cell(30, 8, number_format($supplierTotalCredit, 2), 1, 0, 'R');
    $pdf->Cell(30, 8, number_format($supplierRunningBalance, 2), 1, 1, 'R');

    $grandTotalOpening += $supplierOpeningBalance;
    $grandTotalDebit += $supplierTotalDebit;
    $grandTotalCredit += $supplierTotalCredit;
    $grandTotalBalance += $supplierRunningBalance;
}

// Grand Totals
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(60, 8, 'Grand Total', 1, 0, 'R', true);
$pdf->Cell(30, 8, number_format($grandTotalOpening, 2), 1, 0, 'R', true);
$pdf->Cell(30, 8, number_format($grandTotalDebit, 2), 1, 0, 'R', true);
$pdf->Cell(30, 8, number_format($grandTotalCredit, 2), 1, 0, 'R', true);
$pdf->Cell(30, 8, number_format($grandTotalBalance, 2), 1, 1, 'R', true);

// Footer
//$pdf->Ln(10);
$pdf->SetFont('Arial', 'I', 8);
$pdf->Cell(190, 6, 'Printed By: ' . $_SESSION['sess_username'], 0, 1, 'L');
$pdf->Cell(190, 6, 'Printed On: ' . date('d-m-Y h:i A'), 0, 1, 'L');

$pdf->Output();
?>
