<?php
include_once('../../../classes/manager.class.php');
include_once('../../../classes/PHPMailer/PHPMailerAutoload.php');
include_once('../../../fpdf/fpdfreportlogoL.php');

$m = new Manager();
$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';

$result = SupplierSetup::GetSupplierLedger($datefrom, $dateto, $supplierId);
$opening_balance = $result['opening_balance'] ?? [];
$ledger = $result['ledger'] ?? [];

class CustomPDF extends FPDFWithOutWaterMark {

//class PDF extends FPDF {
    function Header() {
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(0, 10, 'Supplier Ledger Summary Report', 0, 1, 'C');
        $this->SetFont('Arial', '', 10);
        global $datefrom, $dateto, $supplierId;
        $this->Cell(0, 10, "Date: $datefrom to $dateto | Supplier: $supplierId", 0, 1, 'C');
        $this->Ln(5);
        
        $this->SetFont('Arial', 'B', 10);
        $this->Cell(50, 10, 'Supplier Name', 1);
        $this->Cell(30, 10, 'Opening Balance', 1, 0, 'R');
        $this->Cell(30, 10, 'Total Debit', 1, 0, 'R');
        $this->Cell(30, 10, 'Total Credit', 1, 0, 'R');
        $this->Cell(30, 10, 'Closing Balance', 1, 1, 'R');
    }
    
    function Footer() {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Printed By: ' . $_SESSION['sess_username'], 0, 0, 'C');
    }
}

//$pdf = new PDF();
$pdf = new CustomPDF('P', 'mm', 'A4');
$pdf->AddPage();
$pdf->SetFont('Arial', '', 10);

$grandTotalDebit = $grandTotalCredit = $grandTotalBalance = $grandTotalOpening = 0;
$supplierIds = array_unique(array_merge(
    array_column($opening_balance, 'supplier_id'),
    array_column($ledger, 'supplier_id')
));

foreach ($supplierIds as $supplierId) {
    $supplierOpeningData = current(array_filter($opening_balance, fn($ob) => $ob->supplier_id === $supplierId));
    $supplierOpeningBalance = $supplierOpeningData->opening_balance ?? 0;
    $supplierName = $supplierOpeningData->supplier_name ?? 'Unknown';
    $supplierTotalDebit = $supplierTotalCredit = 0;
    $supplierRunningBalance = $supplierOpeningBalance;
    
    foreach (array_filter($ledger, fn($entry) => $entry->supplier_id === $supplierId) as $entry) {
        $debit = (float)($entry->debit ?? 0);
        $credit = (float)($entry->credit ?? 0);
        $supplierRunningBalance += ($debit - $credit);
        $supplierTotalDebit += $debit;
        $supplierTotalCredit += $credit;
    }
    
    if ($supplierRunningBalance == 0 && $supplierTotalDebit == 0 && $supplierTotalCredit == 0) continue;
    
    $pdf->Cell(50, 10, $supplierName, 1);
    $pdf->Cell(30, 10, number_format($supplierOpeningBalance, 2), 1, 0, 'R');
    $pdf->Cell(30, 10, number_format($supplierTotalDebit, 2), 1, 0, 'R');
    $pdf->Cell(30, 10, number_format($supplierTotalCredit, 2), 1, 0, 'R');
    $pdf->Cell(30, 10, number_format($supplierRunningBalance, 2), 1, 1, 'R');

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

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

$pdf->Output();
?>
