<?php
include("../classes/manager.class.php");
include('../includes/number_to_words.php');

$m = new Manager();
$output = '';

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

$voucherId = $_GET['id'] ?? 0;
$voucher = ExpenseTaxSetup::GetExpenseTaxSetupById($voucherId); // adjust to your actual method
$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 = '';
$accountNumber = '';
if ($voucher->bank_id > 0) {
    $bank = BankSetup::GetBankSetupById($voucher->bank_id);
    if ($bank) {
        $bankName = $bank->bank_name;
        $accountNumber = $bank->account_number;
    }
}

$amount = $voucher->amount;
$amountWords = ucwords(NumberToWords::convert($amount)) . " only";
$voucherDate = date('j-M-Y', strtotime($voucher->date));
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
    <style>
        table{
            font-size:18px;
        }
    </style>
</head>
<body>
   <!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <h3>Debit/Credit Voucher</h3>
            </div>
                
              <a href="expense_tax_setup.php?view" class="btn waves-effect waves-light">
    Back to Expense Setup
</a>
      
          
          <a href="expense_tax_voucher_print.php?id=<?= $voucher->id ?>" target="_blank" class="btn waves-effect waves-light" style="margin-left:10px;">
    Print
</a>

          
          <br>
          <br>
          <br>
          <hr>
<h5><div class="section-header center-align ">Debit / Credit Voucher</div></h5>

<table class="voucher-table striped highlight">
    <tr>
        <td><strong>Voucher No.</strong> <?= htmlspecialchars($voucher->voucher_no) ?></td>
        <td><strong>Date</strong> <?= $voucherDate ?></td>
    </tr>
</table>


<!-- CREDIT Section -->
<table class="voucher-table striped highlight">
    <tr><th class="section-header" colspan="4">Debit</th></tr>
    <tr>
        <th>Title</th>
      <th></th>
        <th>Cash / Cheque</th>
        <th>Amount</th>
    </tr>
    <tr>
        <td>
    <?= $supplierName ?><br>
            <strong>Bill No: <?= htmlspecialchars($voucher->bill_no) ?></strong>
        </td>
      <td></td>
        <td><?= $voucher->payment_type == 'cash' ? 'Cash' : $voucher->chq_no ?></td>
        <td><?= number_format($amount, 0) ?></td>
    </tr>
    <tr>
        <td colspan="3" class="section-title">Total</td>
        <td><?= number_format($amount, 0) ?></td>
    </tr>
</table>

<!-- AMOUNT IN WORDS -->
<table class="voucher-table striped highlight">
    <tr>
        <th class="section-header" colspan="2">Amount in Words</th>
    </tr>
    <tr>
        <td colspan="2"><?= $amountWords ?></td>
    </tr>
</table>
          
          <!-- DEBIT Section -->
<table class="voucher-table striped highlight">
    <tr><th class="section-header" colspan="4">Credit</th></tr>
    <tr>
        <th>Title</th>
      
        <th>Cash / Cheque</th>
        <th>Amount</th>
    </tr>
    <tr>
        <td><?= $voucher->payment_type == 'cash' ? 'Cash' : $bankName ?></td>
     
        <td><?= $voucher->payment_type == 'cash' ? 'Cash' : $voucher->chq_no ?></td>
        <td><?= number_format($amount, 0) ?></td>
    </tr>
    <tr>
        <td colspan="2" class="section-title">Total</td>
        <td><?= number_format($amount, 0) ?></td>
    </tr>
</table>

<!-- REMARKS -->
<table class="voucher-table striped highlight">
    <tr>
        <th class="section-header" colspan="2">Remarks</th>
    </tr>
    <tr>
        <td colspan="2"><?= $voucher->description ?></td>
    </tr>
</table>


<br>
          <hr>
<!-- SIGNATURES -->
<table>
    <tr>
        <th>Issued by:</th>
        <th>Approved by:</th>
        <th>Received by:</th>
    </tr>
    <tr class="signature">
      <td><?= ucwords($issuerName) ?><br>(ISSUER)</td>
      <td><?= ucwords($approverName) ?><br>(APPROVER)</td>
      <td><?= $supplierName ?><br>(PAYEE)</td>
	</tr>

</table>



<?php include('includes/inc_footer.php'); ?>
<?php include('includes/inc_js.php'); ?>
    <script type="application/javascript">

    $(window).on("load", function () {
        $("#loader-wrapper").fadeOut("slow");
    });

    </script>
</body>

</html>
