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

$m = new Manager();
$loggedInUser = $_SESSION['sess_username'];

extract($_REQUEST);
$param_data = ControlConfifguration::GetConfiguration();

define('PROJECT_NAME', $param_data->name);
define('PROJECT_ADDRESS', $param_data->address);
define('PROJECT_TEL', $param_data->contact);
define('PROJECT_EMAIL', $param_data->email);
define('PROJECT_WEBSITE', $param_data->website);
define('PROJECT_COLOR', $param_data->color);
define('PROJECT_DISCOUNT', $param_data->is_discount);
define('PROJECT_TAX', $param_data->is_tax);

$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');
$supplier_id =  isset($_GET['supplier_name']) ? $_GET['supplier_name'] : 'All';

class CustomPDF extends FPDFWithOutWaterMark {
    function Header() {
        global $datefrom, $dateto, $supplierId;
      
        $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');
        $loggedInUser = $_SESSION['sess_username'];
        $this->Image(__DIR__ . '/../../../images/logo_left_print.png',10,5,25);
        $this->Image(__DIR__ . '/../../../images/logo_right_print.png',257,5,25);
        $this->SetFont('Arial', 'B', 16);
        $this->Cell(0, 8, PROJECT_NAME, 0, 0, 'C');
        $this->Ln();
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(0, 10, 'Supplier Ledger Report', 0, 1, 'C');
        $this->SetFont('Arial', '', 10);
        $this->Cell(0, 4, 'For the period of: ' . date('d-m-Y', strtotime($datefrom)) . ' - ' . date('d-m-Y', strtotime($dateto)), 0, 0, 'C');
        $this->Ln();
        $this->SetFont('Arial', '', 8);
        $this->Cell(227, 4, 'Printed By: '.$loggedInUser, 0 ,0,'L');
        $this->Cell(50, 4, 'Printed Date: ' . date('d-m-Y h:i A'), 0 ,0,'R');
        
        $this->Ln(5);

        $this->SetFont('Arial', 'B', 9);
        $this->Cell(30, 7, 'Type', 1);
        $this->Cell(20, 7, 'Date', 1);
        $this->Cell(20, 7, 'Reference #', 1);
        $this->Cell(25, 7, 'Payment Type', 1);
        $this->Cell(70, 7, 'Cheque Details', 1);
        $this->Cell(50, 7, 'Debit', 1, 0, 'R');
        $this->Cell(30, 7, 'Credit', 1, 0, 'R');
        $this->Cell(32, 7, 'Balance', 1, 0, 'R');
        $this->Ln();
    }

    function Footer() {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 7);
        $this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');
       // $this->Cell(0, 10, 'Printed By: ' . $_SESSION['sess_username'], 0, 0, 'C');
    }
}
$pdf = new CustomPDF('L', 'mm', 'A4');
//$pdf = new CustomPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 9);

$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';
$supplierId = $_GET['supplier_id'] ?? 'All';

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

$grandTotalDebit = $grandTotalCredit = $grandTotalBalance = 0;

foreach ($opening_balance as $ob) {
    $supplierName = $ob->supplier_name;
    $supplierRunningBalance = $ob->opening_balance;

    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(0, 7, "Supplier: $supplierName", 1, 1, 'L');
    $pdf->SetFont('Arial', '', 9);

    $pdf->Cell(30, 7, 'Opening Balance', 1);
    $pdf->Cell(20, 7, '-', 1);
    $pdf->Cell(20, 7, '-', 1);
    $pdf->Cell(25, 7, '-', 1);
    $pdf->Cell(70, 7, '-', 1);
    $pdf->Cell(50, 7, '-', 1, 0, 'R');
    $pdf->Cell(30, 7, '-', 1, 0, 'R');
    $pdf->Cell(32, 7, number_format($supplierRunningBalance, 2), 1, 0, 'R');
    $pdf->Ln();

    $supplierTotalDebit = $supplierTotalCredit = 0;
    foreach ($ledger as $entry) {
        if ($entry->supplier_id !== $ob->supplier_id) continue;

        $debit = (float)$entry->debit;
        $credit = (float)$entry->credit;
        $supplierRunningBalance += ($debit - $credit);
        $supplierTotalDebit += $debit;
        $supplierTotalCredit += $credit;

        $pdf->Cell(30, 7, ucfirst($entry->source), 1);
        $pdf->Cell(20, 7, date('d-m-Y', strtotime($entry->date)), 1);
        $pdf->Cell(20, 7, $entry->reference, 1);
        $pdf->Cell(25, 7, ucfirst($entry->supplier_payment_type), 1);
        $pdf->Cell(70, 7, $entry->cheque_details, 1);
        $pdf->Cell(50, 7, number_format($debit, 2), 1, 0, 'R');
        $pdf->Cell(30, 7, number_format($credit, 2), 1, 0, 'R');
        $pdf->Cell(32, 7, number_format($supplierRunningBalance, 2), 1, 0, 'R');
        $pdf->Ln();
    }

    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(165, 7, 'Total', 1, 0, 'R');
    $pdf->Cell(50, 7, number_format($supplierTotalDebit, 2), 1, 0, 'R');
    $pdf->Cell(30, 7, number_format($supplierTotalCredit, 2), 1, 0, 'R');
    $pdf->Cell(32, 7, number_format($supplierRunningBalance, 2), 1, 0, 'R');
    $pdf->Ln();
    $pdf->SetFont('Arial', '', 9);

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

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


$pdf->Output();
?>
