<?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']) ? $_GET['datefrom'] : date('d-m-Y', strtotime("-30 days"));
$dateto = isset($_GET['dateto']) ? $_GET['dateto'] : date('d-m-Y');

$dateFromFormatted = date('Y-m-d', strtotime($datefrom));
$dateToFormatted = date('Y-m-d', strtotime($dateto));

// Filter for reports
$filterReportOpening = " WHERE DATE_FORMAT(date,'%Y-%m-%d')>='2016-01-01' and DATE_FORMAT(date,'%Y-%m-%d')<'$dateFromFormatted'";
$filterReport = " WHERE DATE_FORMAT(date,'%Y-%m-%d')>='$dateFromFormatted' and DATE_FORMAT(date,'%Y-%m-%d')<='$dateToFormatted'";

$openingData = PettyCashIn::GetPettyCashOpeningBalance($filterReportOpening);
$transactions = PettyCashIn::GetPettyCashLedgerData($filterReport);

// Calculate opening balance
$balance = 0;
$totalInflow = 0;
$totalOutflow = 0;

foreach ($openingData as $item) {
    $amount = $item->amount;
    $balance += ($item->type == 'inflow') ? $amount : -$amount;
}

class PDF extends FPDF
{
    function Header()
    {
        global $datefrom, $dateto;
        $this->Image(__DIR__ . '/../../../images/logo_left_print.png',10,5,30);
        $this->Image(__DIR__ . '/../../../images/logo_right_print.png',257,5,30);
        $this->SetFont('Arial','B',12);
        $this->Cell(0,10,'Petty Cash Ledger',0,1,'C');
        $this->SetFont('Arial','',10);
        $this->Cell(0,10,"Period: $datefrom to $dateto",0,1,'C');
        $this->Ln(5);

        $this->SetFont('Arial','B',8);
        $this->SetFillColor(230,230,230);
        $this->Cell(25,6,'Date',1,0,'C',true);
        $this->Cell(65,6,'Supplier',1,0,'C',true);
        $this->Cell(50,6,'Sub Category',1,0,'C',true);
        $this->Cell(50,6,'Description',1,0,'C',true);
        $this->Cell(30,6,'Credit',1,0,'C',true);
        $this->Cell(30,6,'Debit',1,0,'C',true);
        $this->Cell(30,6,'Balance',1,1,'C',true);
    }
}

$pdf = new PDF();
$pdf->AddPage('L');
$pdf->SetFont('Arial','',8);

// Opening balance
$pdf->SetFont('Arial','B',8);
$pdf->Cell(190,6,"Opening Balance as of " . date('d M Y', strtotime($datefrom)),1);
$pdf->Cell(30,6,'',1);
$pdf->Cell(30,6,'',1);
$pdf->Cell(30,6,number_format($balance,2),1,1,'R');

$pdf->SetFont('Arial','',8);

foreach ($transactions as $row) {
    $date = date('d-m-Y', strtotime($row->date));
    $supplier = $row->supplier_name;
    $subcat = $row->category_name;
    $desc = $row->description;
    $type = $row->type;
    $amount = $row->amount;

    if ($type == 'inflow') {
        $balance += $amount;
        $totalInflow += $amount;
        $credit = number_format($amount, 2);
        $debit = '';
    } else {
        $balance -= $amount;
        $totalOutflow += $amount;
        $credit = '';
        $debit = number_format($amount, 2);
    }

    $pdf->Cell(25,6,$date,1);
    $pdf->Cell(65,6,$supplier,1);
    $pdf->Cell(50,6,$subcat,1);
    $pdf->Cell(50,6,$desc,1);
    $pdf->Cell(30,6,$credit,1,0,'R');
    $pdf->Cell(30,6,$debit,1,0,'R');
    $pdf->Cell(30,6,number_format($balance,2),1,1,'R');
}

// Totals row
$pdf->SetFont('Arial','B',8);
$pdf->Cell(190,6,'Total',1);
$pdf->Cell(30,6,number_format($totalInflow,2),1,0,'R');
$pdf->Cell(30,6,number_format($totalOutflow,2),1,0,'R');
$pdf->Cell(30,6,'',1,1);

$pdf->Output();
