<?php

include_once('../../../classes/manager.class.php');
include_once('../../../classes/PHPMailer/PHPMailerAutoload.php');
include_once('../../../fpdf/fpdfreportlogoL.php');
$m = new Manager();

class CustomPDF extends FPDFWithOutWaterMark {
    function Header() {
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(0, 10, 'Stock Report (Finish Material)', 0, 1, 'C');
        $this->Ln(5);
    }

    function Footer() {
        $this->SetY(-15);
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');
    }
}

$filterReport = "";
if (isset($product_id) && $product_id != 'All') {
    $filterReport .= " AND pp.product_id = '$product_id'";
}

$data = ProductionSetup::GetBalanceStockReportProducts($filterReport);

$pdf = new CustomPDF('L', 'mm', 'A4');
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 8);

// Define column widths
$colWidths = [50, 30, 48, 20, 20, 20, 20, 20, 20, 12, 20];

// Table Headers
$pdf->SetFillColor(200, 220, 255);
$pdf->Cell($colWidths[0], 10, 'Batch Date', 1, 0, 'C', true);
$pdf->Cell($colWidths[1], 10, 'Batch Number', 1, 0, 'C', true);
$pdf->Cell($colWidths[2], 10, 'Item Name', 1, 0, 'C', true);
$pdf->Cell($colWidths[3], 10, 'Prod Qty', 1, 0, 'C', true);
$pdf->Cell($colWidths[4], 10, 'Prod Cartons', 1, 0, 'C', true);
$pdf->Cell($colWidths[5], 10, 'Balance Qty', 1, 0, 'C', true);
$pdf->Cell($colWidths[6], 10, 'Rem Cartons', 1, 0, 'C', true);
$pdf->Cell($colWidths[7], 10, 'Cost/Unit', 1, 0, 'C', true);
$pdf->Cell($colWidths[8], 10, 'Amount', 1, 0, 'C', true);
$pdf->Cell($colWidths[9], 10, 'Tax %', 1, 0, 'C', true);
$pdf->Cell($colWidths[10], 10, 'Tax Amt', 1, 1, 'C', true);

// Initialize variables
$currentItem = null;
$groupTotals = [
    'Produced Quantity' => 0,
    'Produced Cartons' => 0,
    'Balance Quantity' => 0,
    'Remaining Cartons' => 0,
    'Cost Per Kg' => 0,
    'Amount' => 0,
    'S.Tax Rate %' => 0,
    'S.Tax Amount' => 0,
];
$grandTotals = [
    'Produced Quantity' => 0,
    'Produced Cartons' => 0,
    'Balance Quantity' => 0,
    'Remaining Cartons' => 0,
    'Cost Per Kg' => '-',
    'Amount' => 0,
    'S.Tax Rate %' => '-',
    'S.Tax Amount' => 0,
];

// Loop through data
foreach ($data as $row) {
    // Check for group change
    if ($currentItem !== $row->{'Item Name'}) {
        // Print group totals if a group has ended
        if ($currentItem !== null) {
            $pdf->SetFont('Arial', 'B', 8);
            $pdf->Cell($colWidths[0] + $colWidths[1] + $colWidths[2], 10, "Total for {$currentItem}", 1, 0, 'C');
            $pdf->Cell($colWidths[3], 10, number_format($groupTotals['Produced Quantity'], 2), 1, 0, 'R');
            $pdf->Cell($colWidths[4], 10, number_format($groupTotals['Produced Cartons'], 2), 1, 0, 'R');
            $pdf->Cell($colWidths[5], 10, number_format($groupTotals['Balance Quantity'], 2), 1, 0, 'R');
            $pdf->Cell($colWidths[6], 10, number_format($groupTotals['Remaining Cartons'], 2), 1, 0, 'R');
            $pdf->Cell($colWidths[7], 10, number_format($groupTotals['Cost Per Kg'], 2), 1, 0, 'R');
            $pdf->Cell($colWidths[8], 10, number_format($groupTotals['Amount'], 2), 1, 0, 'R');
            $pdf->Cell($colWidths[9], 10, number_format($groupTotals['S.Tax Rate %'], 2), 1, 0, 'R');
            $pdf->Cell($colWidths[10], 10, number_format($groupTotals['S.Tax Amount'], 2), 1, 1, 'R');
            $groupTotals = array_fill_keys(array_keys($groupTotals), 0);
        }
        $currentItem = $row->{'Item Name'};
    }

    // Add row data
    $pdf->SetFont('Arial', '', 8);
    $pdf->Cell($colWidths[0], 10, $row->{'Batch Date'}, 1);
    $pdf->Cell($colWidths[1], 10, $row->{'Batch Number'}, 1);
    $pdf->Cell($colWidths[2], 10, $row->{'Item Name'}, 1);
    $pdf->Cell($colWidths[3], 10, number_format($row->{'Produced Quantity'}, 2), 1, 0, 'R');
    $pdf->Cell($colWidths[4], 10, number_format($row->{'Produced Cartons'}, 2), 1, 0, 'R');
    $pdf->Cell($colWidths[5], 10, number_format($row->{'Balance Quantity'}, 2), 1, 0, 'R');
    $pdf->Cell($colWidths[6], 10, number_format($row->{'Remaining Cartons'}, 2), 1, 0, 'R');
    $pdf->Cell($colWidths[7], 10, number_format($row->{'Cost Per Kg'}, 2), 1, 0, 'R');
    $pdf->Cell($colWidths[8], 10, number_format($row->{'Amount'}, 2), 1, 0, 'R');
    $pdf->Cell($colWidths[9], 10, $row->{'S.Tax Rate %'}, 1, 0, 'R');
    $pdf->Cell($colWidths[10], 10, number_format($row->{'S.Tax Amount'}, 2), 1, 1, 'R');

    // Update group totals
    $groupTotals['Produced Quantity'] += $row->{'Produced Quantity'};
    $groupTotals['Produced Cartons'] += $row->{'Produced Cartons'};
    $groupTotals['Balance Quantity'] += $row->{'Balance Quantity'};
    $groupTotals['Remaining Cartons'] += $row->{'Remaining Cartons'};
    $groupTotals['Cost Per Kg'] = '-';
    $groupTotals['Amount'] += $row->{'Amount'};
    $groupTotals['S.Tax Rate %'] = '-';
    $groupTotals['S.Tax Amount'] += $row->{'S.Tax Amount'};

    // Update grand totals
    foreach ($groupTotals as $key => $value) {
        $grandTotals[$key] += $value;
    }
}

// Print final group totals
if ($currentItem !== null) {
    $pdf->SetFont('Arial', 'B', 9);
    $pdf->Cell($colWidths[0] + $colWidths[1] + $colWidths[2], 10, "Total for {$currentItem}", 1, 0, 'C');
    $pdf->Cell($colWidths[3], 10, number_format($groupTotals['Produced Quantity'], 2), 1, 0, 'R');
    $pdf->Cell($colWidths[4], 10, number_format($groupTotals['Produced Cartons'], 2), 1, 0, 'R');
    $pdf->Cell($colWidths[5], 10, number_format($groupTotals['Balance Quantity'], 2), 1, 0, 'R');
    $pdf->Cell($colWidths[6], 10, number_format($groupTotals['Remaining Cartons'], 2), 1, 0, 'R');
    $pdf->Cell($colWidths[7], 10, number_format($groupTotals['Cost Per Kg'], 2), 1, 0, 'R');
    $pdf->Cell($colWidths[8], 10, number_format($groupTotals['Amount'], 2), 1, 0, 'R');
    $pdf->Cell($colWidths[9], 10, number_format($groupTotals['S.Tax Rate %'], 2), 1, 0, 'R');
    $pdf->Cell($colWidths[10], 10, number_format($groupTotals['S.Tax Amount'], 2), 1, 1, 'R');
}

// Print grand totals
$pdf->Ln(5);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell($colWidths[0] + $colWidths[1] + $colWidths[2], 10, "Grand Total", 1, 0, 'C');
$pdf->Cell($colWidths[3], 10, number_format($grandTotals['Produced Quantity'], 2), 1, 0, 'R');
$pdf->Cell($colWidths[4], 10, number_format($grandTotals['Produced Cartons'], 2), 1, 0, 'R');
$pdf->Cell($colWidths[5], 10, number_format($grandTotals['Balance Quantity'], 2), 1, 0, 'R');
$pdf->Cell($colWidths[6], 10, number_format($grandTotals['Remaining Cartons'], 2), 1, 0, 'R');
$pdf->Cell($colWidths[7], 10, '', 1, 0, 'R');
$pdf->Cell($colWidths[8], 10, number_format($grandTotals['Amount'], 2), 1, 0, 'R');
$pdf->Cell($colWidths[9], 10, '', 1, 0, 'R');
$pdf->Cell($colWidths[10], 10, number_format($grandTotals['S.Tax Amount'], 2), 1, 1, 'R');

$pdf->Output();
?>
