<?php

session_start();


if(!isset($_SESSION['user_id'])){

    header("Location: login.php");
    exit;

}


require_once "config.php";


// TCPDF

require_once "tcpdf-main/tcpdf.php";



$id=$_GET['vendor'] ?? 0;



if(!$id){

    die("Invalid Vendor");

}






// ==========================
// GET VENDOR DETAILS
// ==========================


$stmt=$pdo->prepare("

SELECT *

FROM vendors

WHERE id=?

");


$stmt->execute([$id]);


$vendor=$stmt->fetch();



if(!$vendor){

    die("Vendor not found");

}






// ==========================
// GET EXPENSE RECORDS
// ==========================


$stmt=$pdo->prepare("

SELECT


expenses.*,


expense_categories.category_name



FROM expenses



LEFT JOIN expense_categories


ON expenses.category_id = expense_categories.id



WHERE expenses.vendor_id = ?



ORDER BY expenses.expense_date DESC


");


$stmt->execute([$id]);


$expenses=$stmt->fetchAll();







// ==========================
// TOTAL AMOUNT
// ==========================


$stmt=$pdo->prepare("

SELECT

COALESCE(SUM(total),0)


FROM expenses


WHERE vendor_id=?


");


$stmt->execute([$id]);


$total=$stmt->fetchColumn();







// ==========================
// TCPDF CUSTOM CLASS
// ==========================


class VendorStatementPDF extends TCPDF
{



public function Header()
{


    // WATERMARK

    $this->SetAlpha(0.08);


    $this->SetFont(
        'helvetica',
        'B',
        55
    );


    $this->SetTextColor(
        100,
        100,
        100
    );


    $this->StartTransform();


    $this->Rotate(
        45,
        80,
        160
    );


    $this->Text(
        35,
        150,
        'EXPENSE PRO'
    );


    $this->StopTransform();


    $this->SetAlpha(1);



    // HEADER TITLE


    $this->SetFont(
        'helvetica',
        'B',
        16
    );


    $this->SetTextColor(
        6,
        95,
        70
    );


    $this->Cell(
        0,
        10,
        'EXPENSE PRO',
        0,
        1,
        'C'
    );



    $this->SetFont(
        'helvetica',
        '',
        10
    );


    $this->SetTextColor(
        80,
        80,
        80
    );


    $this->Cell(
        0,
        6,
        'Vendor Expense Statement',
        0,
        1,
        'C'
    );


    $this->Ln(5);


}





public function Footer()
{


    $this->SetY(-15);


    $this->SetFont(
        'helvetica',
        'I',
        8
    );


    $this->SetTextColor(
        100,
        100,
        100
    );



    $this->Cell(
        0,
        10,
        'Generated by Expense Pro | Page '
        .$this->getAliasNumPage()
        .' of '
        .$this->getAliasNbPages(),
        0,
        0,
        'C'
    );



}



}