<?php
include('../../../classes/manager.class.php');
include('../../../classes/PHPMailer/PHPMailerAutoload.php');
include('../../../fpdf/fpdf1.php');

session_start();
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

$m = new Manager();
$datefrom = isset($_GET['datefrom']) ? $_GET['datefrom'] : date('d-m-Y', strtotime("-30 days"));
$dateto = isset($_GET['dateto']) ? $_GET['dateto'] : date('d-m-Y');
$cat_type = isset($_GET['cat_type']) ? $_GET['cat_type'] : 'All';

$qryDate = date("Y-m-d", strtotime($datefrom));
$qryTo = date("Y-m-d", strtotime($dateto));

$catType = "";
if ($cat_type !== 'All') {
    $catType .= " AND cs.is_mart = '$cat_type'";
}

$reportData = Invoice::getLedgerReport($qryDate, $qryTo, $catType);
$openingBalanceData = $reportData['opening_balance'] ?? [];
$transactionData = $reportData['transactions'] ?? [];

class PDF extends FPDF {
    function Header() {
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(0, 10, 'Customer Type Ledger Report', 0, 1, 'C');
        $this->SetFont('Arial', '', 10);
        $this->Cell(0, 5, "From: " . $_GET['datefrom'] . " To: " . $_GET['dateto'], 0, 1, 'C');
        $this->Ln(5);
        $this->SetFont('Arial', 'B', 8);
    $this->Cell(8, 7, 'S.No.', 1);
    $this->Cell(15, 7, 'Head', 1);
    $this->Cell(18, 7, 'Date', 1);
    $this->Cell(25, 7, 'Reference Code', 1);
    $this->Cell(50, 7, 'Customer', 1);
    $this->Cell(12, 7, 'Status', 1);
    $this->Cell(22, 7, 'Payment Type', 1);
    $this->Cell(20, 7, 'Bank Name', 1);
    $this->Cell(20, 7, 'CHQ Number', 1);
    $this->Cell(30, 7, 'CHQ Date', 1);
    $this->Cell(20, 7, 'Debit', 1);
    $this->Cell(20, 7, 'Credit', 1);
    $this->Cell(20, 7, 'Balance', 1);
    $this->Ln();
    }

    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('L', 'mm', 'A4');
$pdf->AddPage();
$pdf->SetFont('Arial', '', 8);

$counter = 1;
$grandTotalDebit = $grandTotalCredit = $grandTotalBalance = 0;
// [total_payment] => 513318
//            [total_invoice] => 543408
//error_log(print_r($openingBalanceData, true));

foreach ($openingBalanceData as $ob) {
    $customerId = $ob->customer_id;
    $customerName = $ob->customer_name;
  $totalPayment = $ob->total_payment;
   $totalInvoice = $ob->total_invoice;
    $openingBalance = $ob->opening_balance;
    $runningBalance = $openingBalance;
  
    

    $pdf->SetFont('Arial', '', 8);
    $pdf->Cell(280, 7, "Customer: " . $customerName, 1, 1, 'L');

    $pdf->SetFont('Arial', '', 8);
    $pdf->Cell(8, 7, $counter, 1);
    //$pdf->Cell(25, 7, '-', 1);
    $pdf->Cell(58, 7, 'Opening Balance', 1);
    $pdf->Cell(154, 7, $customerName, 1);
    $pdf->Cell(20, 7, $totalInvoice, 1);
    $pdf->Cell(20, 7, $totalPayment, 1);
    $pdf->Cell(20, 7, number_format($openingBalance, 2), 1);
    $pdf->Ln();

    $subtotalDebit = $totalInvoice;
    $subtotalCredit = $totalPayment;
//error_log(print_r($transactionData, true));

    foreach ($transactionData as $transaction) {
        if ($transaction->customer_id === $customerId) {
            $counter++;
            $debit = floatval($transaction->debit);
            $credit = floatval($transaction->credit);
            $runningBalance += ($credit - $debit);

            $subtotalDebit += $debit;
            $subtotalCredit += $credit;

            $pdf->Cell(8, 7, $counter, 1);
            $pdf->Cell(15, 7, $transaction->transaction_type, 1);
            $pdf->Cell(18, 7, date('d-m-Y', strtotime($transaction->create_date)), 1);
            $pdf->Cell(25, 7, $transaction->reference_code, 1);
            $pdf->Cell(50, 7, $transaction->customer_name, 1);
            $pdf->Cell(12, 7, $transaction->status, 1);
            $pdf->Cell(22, 7, $transaction->payment_type, 1);
            $pdf->Cell(20, 7, $transaction->from_bank_name, 1);
            $pdf->Cell(20, 7, $transaction->cheque_number, 1);
            $pdf->Cell(30, 7, $transaction->cheque_date, 1);            
            $pdf->Cell(20, 7, number_format($debit, 2), 1);
            $pdf->Cell(20, 7, number_format($credit, 2), 1);
            $pdf->Cell(20, 7, number_format($runningBalance, 2), 1);
            $pdf->Ln();
          
        }
    }

    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(220, 7, "Subtotal for " . $customerName, 1, 0, 'R');
    $pdf->Cell(20, 7, number_format($subtotalDebit, 2), 1);
    $pdf->Cell(20, 7, number_format($subtotalCredit, 2), 1);
    $pdf->Cell(20, 7, number_format($runningBalance, 2), 1);
    $pdf->Ln();

    $grandTotalDebit += $subtotalDebit;
    $grandTotalCredit += $subtotalCredit;
    $grandTotalBalance += $runningBalance;
}

$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(220, 7, "Grand Total", 1, 0, 'R');
$pdf->Cell(20, 7, number_format($grandTotalDebit, 2), 1);
$pdf->Cell(20, 7, number_format($grandTotalCredit, 2), 1);
$pdf->Cell(20, 7, number_format($grandTotalBalance, 2), 1);

$pdf->Output();


?>
