<?php
include_once('../../../classes/manager.class.php');
include_once('../../../fpdf/fpdfreportlogo.php'); // adjust path if needed
//include_once('../../../fpdf/fpdf1New.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));

$supplier_id = isset($_GET['supplier_id']) ? $_GET['supplier_id'] : 'All';
$category_id = isset($_GET['category_id']) ? $_GET['category_id'] : 'All';
$payment_type = isset($_GET['payment_type']) ? $_GET['payment_type'] : 'All';

$filter = "WHERE date BETWEEN '$dateFromFormatted' AND '$dateToFormatted'";
if ($supplier_id != 'All') $filter .= " AND supplier_id = '$supplier_id'";
if ($category_id != 'All') $filter .= " AND category_id = '$category_id'";
if ($payment_type != 'All') $filter .= " AND payment_type = '$payment_type'";

$data = ExpenseTaxSetup::GetExpenseTaxSetupList($filter);
$categories = ExpenseTaxCategorySetup::GetExpenseTaxCategorySetupList("ORDER BY parent_id, id");
$categoryMap = [];
foreach ($categories as $cat) {
    $categoryMap[$cat->id] = $cat;
}

$grouped = [];
foreach ($data as $row) {
    $parentId = isset($categoryMap[$row->category_id]) ? $categoryMap[$row->category_id]->parent_id : 0;
    $subId = $row->category_id;
    $grouped[$parentId][$subId][] = $row;
}

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', 255, 5, 30);
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(0, 10, 'Summary Expense Report', 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', 9);
        $this->SetFillColor(230, 230, 230);
        $this->Cell(80, 7, 'Category', 1, 0, 'C', true);
        $this->Cell(80, 7, 'Sub-Category', 1, 0, 'C', true);
        $this->Cell(30, 7, 'Total Amount', 1, 1, 'C', true);
    }
}

$pdf = new PDF();
$pdf->AddPage('P');
$pdf->SetFont('Arial', '', 9);

$grandTotal = 0;

uksort($grouped, function ($a, $b) use ($categoryMap) {
    $nameA = isset($categoryMap[$a]) ? $categoryMap[$a]->name : '';
    $nameB = isset($categoryMap[$b]) ? $categoryMap[$b]->name : '';
    return strcmp($nameA, $nameB);
});

foreach ($grouped as $parentId => $subcats) {
    $parentName = isset($categoryMap[$parentId]) ? $categoryMap[$parentId]->name : 'Uncategorized';
    $parentTotal = 0;

    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(190, 7, $parentName, 1, 1, 'L');

    uksort($subcats, function ($a, $b) use ($categoryMap) {
        $nameA = isset($categoryMap[$a]) ? $categoryMap[$a]->name : '';
        $nameB = isset($categoryMap[$b]) ? $categoryMap[$b]->name : '';
        return strcmp($nameA, $nameB);
    });

    foreach ($subcats as $subId => $entries) {
        $subName = isset($categoryMap[$subId]) ? $categoryMap[$subId]->name : 'Unknown';
        $subTotal = array_sum(array_map(function ($e) { return $e->amount; }, $entries));

        $pdf->SetFont('Arial', '', 9);
        $pdf->Cell(80, 6, '', 1);
        $pdf->Cell(80, 6, $subName, 1);
        $pdf->Cell(30, 6, number_format($subTotal, 2), 1, 1, 'R');

        $parentTotal += $subTotal;
    }

    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell(160, 7, "Sub Total --- $parentName", 1);
    $pdf->Cell(30, 7, number_format($parentTotal, 2), 1, 1, 'R');

    $grandTotal += $parentTotal;
}

$pdf->SetFont('Arial', 'B', 10);
$pdf->Ln(3);
$pdf->Cell(160, 8, 'Grand Total', 1);
$pdf->Cell(30, 8, number_format($grandTotal, 2), 1, 1, 'R');

$pdf->Output();
