<?php
include_once('../fpdf/fpdfreportlogo.php');
include_once('../classes/manager.class.php');
include_once('../includes/number_to_words.php');
$m = new Manager();

if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}

$voucherId = $_GET['id'] ?? 0;
$voucher = ExpenseTaxSetup::GetExpenseTaxSetupById($voucherId);
$issuerName = '-';
$approverName = '-';

if (!empty($voucher->issuer_id)) {
    $issuer = EmployeeAuthoritySetup::GetEmployeeAuthorityById($voucher->issuer_id);
    $issuerName = $issuer ? $issuer->name : '-';
}

if (!empty($voucher->approver_id)) {
    $approver = EmployeeAuthoritySetup::GetEmployeeAuthorityById($voucher->approver_id);
    $approverName = $approver ? $approver->name : '-';
}

if (!$voucher) {
    die("Voucher not found.");
}

$supplierName = '';
if ($voucher->supplier_id > 0) {
    $supplier = SupplierSetup::GetSupplierSetupById($voucher->supplier_id);
    $supplierName = $supplier ? $supplier->name : '';
} else {
    $supplierName = $voucher->paid_to;
}

$bankName = '';
if ($voucher->bank_id > 0) {
    $bank = BankSetup::GetBankSetupById($voucher->bank_id);
    if ($bank) {
        $bankName = $bank->bank_name;
    }
}

$amount = $voucher->amount;
$amountWords = 'Rupees ' . ucwords(NumberToWords::convert($amount)) . " only";
$voucherDate = date('d-M-Y', strtotime($voucher->date));

// PDF Class with Header
class PDF extends FPDF
{
    function Header()
    {
        $this->Image(PROJECT_URL . 'images/logo_left_print.png', 10, 6, 20);
        $this->Image(PROJECT_URL . 'images/logo_right_print.png', 170, 6, 20);
        $this->SetFont('Arial', 'B', 16);
        $this->Cell(0, 8, 'Debit / Credit Voucher', 0, 1, 'C');
        $this->Ln();
    }
}

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

// Voucher Info
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(94,6,"Voucher No: ".$voucher->voucher_no,1,0);
$pdf->Cell(94,6,"Date: ".$voucherDate,1,1,'R');
$pdf->Ln(2);

// Debit Section
$pdf->SetFont('Arial','B',12);
$pdf->Cell(188,6,'Debit',1,1,'C');
$pdf->SetFont('Arial','B',11);
$pdf->Cell(114,6,'Title',1,0,'C');
$pdf->Cell(40,6,'Cash / Cheque',1,0,'C');
$pdf->Cell(34,6,'Amount',1,1,'C');

$pdf->SetFont('Arial','',9);
$pdf->Cell(114,6,$supplierName." (Bill No: ".$voucher->bill_no.")",1,0);
$pdf->SetFont('Arial','',11);
$pdf->Cell(40,6,($voucher->payment_type=='cash' ? 'Cash' : $voucher->chq_no),1,0);
$pdf->Cell(34,6,number_format($amount,0),1,1,'R');

$pdf->SetFont('Arial','B',11);
$pdf->Cell(154,6,'Total',1,0,'R');
$pdf->Cell(34,6,number_format($amount,0),1,1,'R');



// Credit Section
$pdf->Ln(2);
$pdf->SetFont('Arial','B',12);
$pdf->Cell(188,6,'Credit',1,1,'C');
$pdf->SetFont('Arial','B',11);
$pdf->Cell(114,6,'Title',1,0,'C');
$pdf->Cell(40,6,'Cash / Cheque',1,0,'C');
$pdf->Cell(34,6,'Amount',1,1,'C');

$pdf->SetFont('Arial','',11);
$pdf->Cell(114,6,($voucher->payment_type=='cash' ? 'Cash' : $bankName),1,0);
$pdf->Cell(40,6,($voucher->payment_type=='cash' ? 'Cash' : $voucher->chq_no),1,0);
$pdf->Cell(34,6,number_format($amount,0),1,1,'R');

$pdf->SetFont('Arial','B',11);
$pdf->Cell(154,6,'Total',1,0,'R');
$pdf->Cell(34,6,number_format($amount,0),1,1,'R');

// Amount in Words
$pdf->Ln(2);
$pdf->SetFont('Arial','B',11);
$pdf->Cell(188,6,'Amount in Words',1,1,'C');
$pdf->SetFont('Arial','',11);
$pdf->MultiCell(188,6,$amountWords,1);

// Remarks
$pdf->Ln(2);
$pdf->SetFont('Arial','B',11);
$pdf->Cell(188,6,'Remarks',1,1,'C');
$pdf->SetFont('Arial','',11);
$pdf->MultiCell(188,6,$voucher->description,1);

 // Signatures
$pdf->Ln(2);
$pdf->SetFont('Arial','B',11);
$pdf->Cell(50,6,'Issued by:',1,0,'C');
$pdf->Cell(50,6,'Approved by:',1,0,'C');
$pdf->Cell(88,6,'Received by:',1,1,'C');

// Leave space for signatures
$pdf->SetFont('Arial','',11);
$pdf->Cell(50,20,"",1,0,'C');    // increased height to 20mm
$pdf->Cell(50,20,"",1,0,'C');
$pdf->Cell(88,20,"",1,1,'C');

// Below line optionally adds names
$pdf->Cell(50,6, $issuerName,1,0,'C');
$pdf->Cell(50,6, $approverName,1,0,'C');
$pdf->Cell(88,6, $supplierName,1,1,'C');

$pdf->Output();
?>
