<?php

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

$supplier_id = $_GET['supplier_id'] ?? 'All';
$ingredient_id = $_GET['ingredient_id'] ?? 'All';

$filterReport = "";

// Apply supplier filter if specified
if ($supplier_id && $supplier_id != 'All') {
    $filterReport .= " AND p.supplier_id = '$supplier_id'";
}

// Apply ingredient filter if specified
if ($ingredient_id && $ingredient_id != 'All') {
    $filterReport .= " AND i.ingredient_id = '$ingredient_id'";
}

// Fetch the data
$data = IngredientStock::GetBalanceStockReport($filterReport);

// Create PDF
$pdf = new FPDFWithOutWaterMark('L', 'mm', 'A4');
$pdf->SetMargins(0, 0, 0);
$pdf->AddPage();

$pdf->SetFont('Arial', 'B', 12);
$pdf->SetTitle('Balance Stock Report (Raw Material)');
$pdf->Ln();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 8, '', 0, 0, 'C');
$pdf->SetX(10);
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(0, 8, 'Printed Date: ' . date('d-m-Y h:i A'), 0, 0, 'L');
$pdf->Ln();
$pdf->SetFont('Arial', '', 14);

// Header
$pdf->Cell(0, 10, 'Balance Stock Report (Raw Material)', 0, 1, 'C');
$pdf->Ln(10);
$pdf->SetX(10);

// Table header

  function printTableHeader($pdf) {
    $pdf->Ln();
    $pdf->SetX(10);
    // Table headers
    $pdf->SetFont('Arial', 'B', 8);
    $pdf->Cell(22, 10, 'Receiving Date', 1);
    $pdf->Cell(27, 10,  'Reference Number', 1);
    $pdf->Cell(45, 10, 'Supplier', 1);
    $pdf->Cell(40, 10, 'Item Name', 1);
    $pdf->Cell(28, 10, 'Receiving Quantity', 1);
    $pdf->Cell(25, 10, 'Balance Quantity', 1);
     $pdf->Cell(19, 10, 'Price Per Kg', 1);
     $pdf->Cell(20, 10, 'Amount', 1);
     $pdf->Cell(18, 10, 'Sales Tax %', 1);
     $pdf->Cell(35, 10, 'Sales Tax Amount', 1);
    $pdf->Ln();
     }
printTableHeader($pdf);

// Table data
$pdf->SetFont('Arial', '', 8);
$grandTotals = [
    'receiving_quantity' => 0,
    'balance_quantity' => 0,
    'amount' => 0,
    'sales_tax_amount' => 0
];

if ($data) {
    foreach ($data as $row) {
        $pdf->SetX(10);
        $amount = $row->price_per_kg * $row->receiving_quantity;
        $salesTaxAmount = ($amount * $row->s_tax_rate) / 100;

        // Display row data
        $pdf->Cell(22, 10, date('d-m-Y', strtotime($row->receiving_date)), 1);
        $pdf->Cell(27, 10, $row->reference_number, 1);
      $pdf->SetFont('Arial', '', 7);
        $pdf->Cell(45, 10, $row->supplier_name, 1);
      $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(40, 10, $row->item_name, 1);
        $pdf->Cell(28, 10, number_format($row->receiving_quantity, 2), 1);
        $pdf->Cell(25, 10, number_format($row->balance_quantity, 2), 1);
        $pdf->Cell(19, 10, number_format($row->price_per_kg, 2), 1);
        $pdf->Cell(20, 10, number_format($amount, 2), 1);
        $pdf->Cell(18, 10, number_format($row->s_tax_rate, 2), 1);
        $pdf->Cell(35, 10, number_format($salesTaxAmount, 2), 1);
        $pdf->Ln();

        // Update grand totals
        $grandTotals['receiving_quantity'] += $row->receiving_quantity;
        $grandTotals['balance_quantity'] += $row->balance_quantity;
        $grandTotals['amount'] += $amount;
        $grandTotals['sales_tax_amount'] += $salesTaxAmount;
    }

    // Grand totals
   $pdf->SetX(10);
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->Cell(94, 10, 'Grand Total', 1);
    $pdf->SetFont('Arial', '', 10);
    $pdf->Cell(30, 10, '', 0);
    $pdf->Cell(30, 10, '', 0);
    $pdf->Cell(30, 10, '', 0);
    $pdf->Cell(30, 10, number_format($grandTotals['receiving_quantity'], 2), 1);
    $pdf->Cell(30, 10, number_format($grandTotals['balance_quantity'], 2), 1);
    $pdf->Cell(30, 10, '', 0);
    $pdf->Cell(30, 10, number_format($grandTotals['amount'], 2), 1);
    $pdf->Cell(30, 10, '', 0);
    $pdf->Cell(30, 10, number_format($grandTotals['sales_tax_amount'], 2), 1);
    $pdf->Ln();
} else {
    $pdf->Cell(0, 10, 'No data available for the selected filters.', 0, 1, 'C');
}

// Output PDF
$pdf->Output();
?>
