<?php
require("$_SERVER[DOCUMENT_ROOT]/fpdf.php");
include "$_SERVER[DOCUMENT_ROOT]/root.class.php";

$db = new db_safeguard();

/* EXTEND FPDF FOR HEADER & FOOTER */
class PDF extends FPDF
{
    function Footer()
    {
        $this->SetY(-12);
        $this->SetFont('Arial', 'I', 8);
        $this->SetTextColor(120, 120, 120);
        $this->Cell(0, 10, 'Page ' . $this->PageNo(), 0, 0, 'C');

        $this->SetY(-12);
        $this->SetFont('Arial', 'I', 8);
        $this->SetTextColor(120, 120, 120);
        $this->Cell(0, 10, 'Generated: ' . date('Y-m-d H:i'), 0, 0, 'L');
    }
}

$pdf = new PDF();
$pdf->SetAutoPageBreak(true, 20);
$pdf->SetMargins(10, 15, 10);
$pdf->SetFont('Arial', '', 10);

/* BRAND COLORS */
$blue = [22, 78, 167];   // Professional Blue
$orange = [249, 115, 22]; // Accent Orange
$lightGray = [245, 247, 250];

/* GET ALL STOCK TYPES */
$stock_type_res = $db->query("stock_types", "SELECT * FROM stock_types");

while ($stock_type = $stock_type_res->fetch_assoc()) {

    $pdf->AddPage();

    /* HEADER BAR */
    // $pdf->SetFillColor($blue[0], $blue[1], $blue[2]);
    // $pdf->Rect(0, 0, 210, 20, 'F');

    $logo = $_SERVER['DOCUMENT_ROOT'] . '/icons/Savuki_Logo.png';

    if (file_exists($logo)) {
        $pdf->Image($logo, 150, 10, 50);
    }

    $pdf->Ln(10);

    // Header line - Orange

    $pdf->SetFont('Arial', 'B', 16);
    $pdf->SetY(15);
    $pdf->SetTextColor(0, 0, 0);
    $pdf->Cell(0, 10, strtoupper($stock_type['name']) . " STOCK REPORT", 0, 1, 'C');

    $pdf->Ln(10);

    /* TABLE HEADER */
    $pdf->SetFont('Arial', 'B', 10);
    $pdf->SetFillColor($blue[0], $blue[1], $blue[2]);
    $pdf->SetTextColor(255, 255, 255);

    if ($stock_type['name'] == 'OILS') {
        $pdf->Cell(25, 8, 'Stock No', 1, 0, 'C', true);
        $pdf->Cell(45, 8, 'Item Name', 1, 0, 'C', true);
        $pdf->Cell(40, 8, 'Supplier', 1, 0, 'C', true);
        $pdf->Cell(20, 8, 'Quantity', 1, 0, 'C', true);
        $pdf->Cell(50, 8, 'Vehicle Type', 1, 1, 'C', true);

    } else if ($stock_type['name'] == 'HYDRAULICS' || $stock_type['name'] == 'BOLTS & NUTS') {
        $pdf->Cell(25, 8, 'Stock No', 1, 0, 'C', true);
        $pdf->Cell(45, 8, 'Item Name', 1, 0, 'C', true);
        $pdf->Cell(25, 8, 'Type', 1, 0, 'C', true);
        $pdf->Cell(25, 8, 'Size', 1, 0, 'C', true);
        $pdf->Cell(40, 8, 'Supplier', 1, 0, 'C', true);
        $pdf->Cell(20, 8, 'Quantity', 1, 1, 'C', true);

    } elseif ($stock_type['name'] == 'PUMPS') {
        $pdf->Cell(25, 8, 'Stock No', 1, 0, 'C', true);
        $pdf->Cell(45, 8, 'Item Name', 1, 0, 'C', true);
        $pdf->Cell(50, 8, 'Pump Code', 1, 0, 'C', true);
        $pdf->Cell(40, 8, 'Supplier', 1, 0, 'C', true);
        $pdf->Cell(20, 8, 'Quantity', 1, 1, 'C', true);

    } elseif ($stock_type['name'] == 'HAMMER, BYTEL OR RIEMER') {
        $pdf->Cell(25, 8, 'Stock No', 1, 0, 'C', true);
        $pdf->Cell(45, 8, 'Item Name', 1, 0, 'C', true);
        $pdf->Cell(50, 8, 'Serial Number', 1, 0, 'C', true);
        $pdf->Cell(40, 8, 'Supplier', 1, 0, 'C', true);
        $pdf->Cell(20, 8, 'Quantity', 1, 1, 'C', true);

    } else {
        $pdf->Cell(25, 8, 'Stock No', 1, 0, 'C', true);
        $pdf->Cell(45, 8, 'Item Name', 1, 0, 'C', true);
        $pdf->Cell(50, 8, 'Stock Type', 1, 0, 'C', true);
        $pdf->Cell(40, 8, 'Supplier', 1, 0, 'C', true);
        $pdf->Cell(20, 8, 'Quantity', 1, 1, 'C', true);
    }



    $pdf->SetFont('Arial', '', 9);
    $pdf->SetTextColor(40, 40, 40);

    $stock_control_res = $db->query(
        "stock",
        "SELECT * FROM stock WHERE stock_type_id = '{$stock_type['record_id']}'"
    );

    if ($stock_control_res->num_rows > 0) {

        $fill = false;

        while ($stock_control = $stock_control_res->fetch_assoc()) {

            $stock_res = $db->query(
                "stock",
                "SELECT * FROM stock WHERE stock_no = '{$stock_control['stock_no']}'"
            );
            $stock = $stock_res->fetch_assoc();

            $supplier_res = $db->query(
                "suppliers",
                "SELECT * FROM suppliers WHERE record_id = '{$stock['supplier_id']}'"
            );
            $supplier = $supplier_res->fetch_assoc();

            $trans_res = $db->query(
                "stock_trans",
                "SELECT * FROM stock_trans WHERE stock_no = '{$stock_control['stock_no']}'"
            );
            $trans = $trans_res->fetch_assoc();

            /* ZEBRA ROWS */
            if ($fill) {
                $pdf->SetFillColor($lightGray[0], $lightGray[1], $lightGray[2]);
            } else {
                $pdf->SetFillColor(255, 255, 255);
            }

            if ($stock_control['stock_type_id'] == 1) {

                $pdf->Cell(25, 8, $stock['stock_no'], 1, 0, 'C', true);
                $pdf->Cell(45, 8, $stock['item_name'], 1, 0, 'L', true);
                $pdf->Cell(40, 8, $supplier['name'], 1, 0, 'L', true);
                $pdf->Cell(20, 8, $trans['quantity'], 1, 0, 'C', true);

                if ($stock['vehicle_type'] == 'ALL') {
                    $pdf->Cell(50, 8, 'ALL', 1, 1, 'L', true);

                } else if ($stock['vehicle_type'] == 'T') {
                    $pdf->Cell(50, 8, 'TRUCK', 1, 1, 'L', true);

                } else if ($stock['vehicle_type'] == 'D') {
                    $pdf->Cell(50, 8, 'DONKIE', 1, 1, 'L', true);

                } else if ($stock['vehicle_type'] == 'C') {
                    $pdf->Cell(50, 8, 'COMP', 1, 1, 'L', true);

                } else if ($stock['vehicle_type'] == 'CT') {
                    $pdf->Cell(50, 8, 'COMP TRUCK', 1, 1, 'L', true);

                } elseif ($stock['vehicle_type'] == 'OTHER') {
                    $pdf->Cell(50, 8, 'OTHER', 1, 1, 'L', true);

                } else {
                    $pdf->Cell(50, 8, $stock['vehicle_type'], 1, 1, 'L', true);
                }

            } else if ($stock_control['stock_type_id'] == 2) {

                $pdf->Cell(25, 8, $stock['stock_no'], 1, 0, 'C', true);
                $pdf->Cell(45, 8, $stock['item_name'], 1, 0, 'L', true);
                $pdf->Cell(25, 8, $stock['type'], 1, 0, 'L', true);
                $pdf->Cell(25, 8, $stock['size'], 1, 0, 'L', true);
                $pdf->Cell(40, 8, $supplier['name'], 1, 0, 'L', true);
                $pdf->Cell(20, 8, $trans['quantity'], 1, 1, 'C', true);

            } else if ($stock_control['stock_type_id'] == 3) {

                $pdf->Cell(25, 8, $stock['stock_no'], 1, 0, 'C', true);
                $pdf->Cell(45, 8, $stock['item_name'], 1, 0, 'L', true);
                $pdf->Cell(50, 8, $stock['pump_code'], 1, 0, 'L', true);
                $pdf->Cell(40, 8, $supplier['name'], 1, 0, 'L', true);
                $pdf->Cell(20, 8, $trans['quantity'], 1, 1, 'C', true);

            } else if ($stock_control['stock_type_id'] == 4) {

                $pdf->Cell(25, 8, $stock['stock_no'], 1, 0, 'C', true);
                $pdf->Cell(45, 8, $stock['item_name'], 1, 0, 'L', true);
                $pdf->Cell(50, 8, $stock['serial_number'], 1, 0, 'L', true);
                $pdf->Cell(40, 8, $supplier['name'], 1, 0, 'L', true);
                $pdf->Cell(20, 8, $trans['quantity'], 1, 1, 'C', true);

            } else if ($stock_control['stock_type_id'] == 5) {

                $pdf->Cell(25, 8, $stock['stock_no'], 1, 0, 'C', true);
                $pdf->Cell(45, 8, $stock['item_name'], 1, 0, 'L', true);
                $pdf->Cell(25, 8, $stock['type'], 1, 0, 'L', true);
                $pdf->Cell(25, 8, $stock['size'], 1, 0, 'L', true);
                $pdf->Cell(40, 8, $supplier['name'], 1, 0, 'L', true);
                $pdf->Cell(20, 8, $trans['quantity'], 1, 1, 'C', true);

            } else {

                $pdf->Cell(25, 8, $stock['stock_no'], 1, 0, 'C', true);
                $pdf->Cell(45, 8, $stock['item_name'], 1, 0, 'L', true);
                $pdf->Cell(50, 8, $stock_type['name'], 1, 0, 'L', true);
                $pdf->Cell(40, 8, $supplier['name'], 1, 0, 'L', true);
                $pdf->Cell(20, 8, $trans['quantity'], 1, 1, 'C', true);

            }

            $fill = !$fill;
        }

    } else {
        $pdf->Ln(5);
        $pdf->SetFont('Arial', 'B', 11);
        $pdf->SetTextColor($orange[0], $orange[1], $orange[2]);
        $pdf->Cell(0, 12, 'NO STOCK FOUND FOR THIS CATEGORY', 1, 1, 'C');
    }
}

/* OUTPUT */
$pdf->Output("I", "Stock_Report.pdf");
exit;
