<?php
// Variables available:
// $company, $inv, $client, $items
?>

<div class="tally-invoice transport-copy">

<!-- ================= HEADER ================= -->
<table class="ti-header" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td class="ti-head-left">
            <div class="ti-head-brand">
                <?php if(!empty($company['logo'])): ?>
                    <img src="../<?= $company['logo'] ?>" class="ti-head-logo" style="max-width:90px; max-height:70px; width:auto; height:auto;">
                <?php endif; ?>
                <span class="ti-head-company"><?= strtoupper($company['company_name']) ?></span>
            </div>
        </td>
        <td class="ti-head-right ti-tax-title">
            TRANSPORT COPY
        </td>
    </tr>
    <tr>
        <td class="ti-head-left ti-head-address">
            <?= nl2br(htmlspecialchars($company['address'])) ?><br>
            GSTIN: <?= htmlspecialchars($company['gst_no']) ?><br>
            Ph: <?= htmlspecialchars($company['phone']) ?>
        </td>
        <td class="ti-head-right ti-head-meta">
            <div><b>Invoice No:</b> <?= htmlspecialchars($inv['invoice_no']) ?></div>
            <div><b>Date:</b> <?= date('d-m-Y', strtotime($inv['invoice_date'])) ?></div>
            <svg id="barcode" data-invoice="<?= htmlspecialchars($inv['invoice_no']) ?>"></svg>
        </td>
    </tr>
</table>

<!-- ================= PARTY ================= -->
<table class="ti-party" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="50%" class="ti-party-box">
            <b>Consignor</b><br>
            <?= htmlspecialchars($company['company_name']) ?><br>
            <?= nl2br(htmlspecialchars($company['address'])) ?>
        </td>
        <td width="50%" class="ti-party-box">
            <b>Consignee</b><br>
            <?= htmlspecialchars($client['name']) ?><br>
            <?= nl2br(htmlspecialchars($client['address'] ?? '')) ?><br>
            Mobile: <?= htmlspecialchars($client['phone'] ?? '') ?>
        </td>
    </tr>
</table>

<!-- ================= ITEM TABLE (NO PRICE) ================= -->
<table class="ti-items" width="100%" cellpadding="0" cellspacing="0">
    <thead>
        <tr>
            <th width="5%">Sr</th>
            <th width="45%">Item Description</th>
            <th width="15%">HSN</th>
            <th width="10%">Unit</th>
            <th width="10%">Qty</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        $totalQty = 0;
        while($row = $items->fetch_assoc()):
            $totalQty += $row['sold_qty'];
        ?>
        <tr>
            <td class="c"><?= $i++ ?></td>
            <td>
                <?= htmlspecialchars($row['product_name']) ?>
                <?php if(!empty($row['item_description'])): ?>
                    <div class="item-sub-desc"><?= nl2br(htmlspecialchars($row['item_description'])) ?></div>
                <?php endif; ?>
            </td>
            <td class="c"><?= htmlspecialchars($row['hsn_code']) ?></td>
            <td class="c"><?= htmlspecialchars($row['unit']) ?></td>
            <td class="r"><?= $row['sold_qty'] ?></td>
        </tr>
        <?php endwhile; ?>

        <!-- Fill blank rows to keep 1 page -->
        <?php
        $maxRows = 15;
        $currentRows = $i - 1;
        for($b = 0; $b < ($maxRows - $currentRows); $b++):
        ?>
        <tr>
            <td>&nbsp;</td><td></td><td></td><td></td><td></td>
        </tr>
        <?php endfor; ?>
    </tbody>
</table>

<!-- ================= TOTAL QTY ================= -->
<table class="ti-totals" width="100%">
    <tr>
        <td><b>Total Quantity</b></td>
        <td class="r"><b><?= $totalQty ?></b></td>
    </tr>
</table>

<!-- ================= TRANSPORT DETAILS ================= -->
<table class="ti-transport" width="100%" cellpadding="0" cellspacing="0">
    <tr>
        <td width="33%">Vehicle No: __________________</td>
        <td width="33%">LR No: __________________</td>
        <td width="33%">Transporter: __________________</td>
    </tr>
</table>

<!-- ================= SIGN ================= -->
<table class="ti-decl-wrap" width="100%">
    <tr>
        <td width="65%">
            <b>Declaration:</b><br>
            This is a transport copy for movement of goods only. No commercial value.
        </td>
        <td width="35%" class="ti-sign-box">
            For <b><?= htmlspecialchars($company['company_name']) ?></b><br><br>
            Authorised Signatory
        </td>
    </tr>
</table>

</div>
