<?php 
    $i = 1;
    $ItemList = "<tr><th width='30'>Sno</th><th>Item</th><th width='100'>HSN</th><th width='60'>Unit</th><th width='100'>Rate</th><th width='80'>Qty</th><th width='100'>Total</th></tr>";
    foreach ($itemsDetails as $itemArr) {
        $item = (object) $itemArr;
        $rateList = "<td align='right'>$item->rate</td>";
        $ItemList .= "<tr><td>$i</td>
        <td><strong>$item->item_name</strong></td>
        <td align='center'>$item->hsn_code</td>
        <td align='center'>$item->unit</td>" . $rateList . "<td align='right'>$item->qty</td>
        <td align='right'>" . sprintf("%.2f", ($item->amount + $item->discount_amount)) . "</td>
        </tr>";
        $i++;
    }
    $taxWrap = [];
    foreach ($itemTaxDetails as $taxArr) {
        $tax = (object) $taxArr;
        $taxWrap[$tax->tax_name . '@' . $tax->tax_percentage . '%']['taxableAmnt'][] = $tax->taxable_amount;
        $taxWrap[$tax->tax_name . '@' . $tax->tax_percentage . '%']['taxAmnt'][] = $tax->tax_amount;
    }
    $TaxBreakup = '';
    foreach ($taxWrap as $key => $val) {
        $TaxBreakup .= "
        <tr>
        <td>$key</td>
        <td align='right'>" . sprintf("%.2f", array_sum($val['taxableAmnt'])) . "</td>
        <td align='right'>" . sprintf("%.2f", array_sum($val['taxAmnt'])) . "</td>
        </tr>
        ";
    }
?>
<link type="text/css" rel="stylesheet" href="<?php echo base_url(); ?>public/css/pdfs.css" />
<table class="leadHeader">
    <tr>
        <td colspan="2" width="50%" class="comp tbr lbr">
            <?php if (!empty($compImg)) {
            ?><img src="<?php echo $compImg; ?>" height="30px" />
            <?php } ?>
        </td>
        <td colspan="2" rowspan="3" width="50%" class="tbr lbr rbr bbr" valign="top" style="padding:0;">
            <table class="branchLeadDetails" style="width:250px">
                <tr>
                    <td><strong>GST No</strong></td>
                    <td><?php echo $branchDetails->gstin; ?></td>
                </tr>
                <tr>
                    <td><strong>State/State Code</strong></td>
                    <td><?php echo $branchDetails->state_name; ?> / <?php echo $branchDetails->state_code; ?></td>
                </tr>
                <tr>
                    <td><strong>PAN</strong></td>
                    <td><?php echo $branchDetails->pan_no; ?></td>
                </tr>
                <tr>
                    <td><strong>Email</strong></td>
                    <td><?php echo $branchDetails->email; ?></td>
                </tr>
                <tr>
                    <td><strong>Phone No</strong></td>
                    <td><?php echo $branchDetails->phone; ?></td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td colspan="2" width="50%" class="comp lbr"><?php echo $compData->name; ?> </td>
    </tr>
    <tr>
        <td colspan="2" height="30px" valign="top" class="bbr lbr"><?php echo $branchDetails->address; ?></td>
    </tr>
    <tr>
        <td colspan="4" style="text-align:center; font-size:14px;" class="lbr rbr bbr">
            <strong><?php echo $masterData->pdf_title; ?></strong>
        </td>
    </tr>
    <tr>
        <td colspan="2" class="lbr"><strong>Bill To:</strong></td>
        <td colspan="2" rowspan="7" class="lbr bbr rbr" valign="top">
            <table class="branchLeadDetails">
                <tr>
                    <td><strong>Lead No</strong>:</td>
                    <td><?php echo $masterData->id; ?> </td>
                </tr>
                <tr>
                    <td><strong>Lead Date</strong>:</td>
                    <td><?php echo $masterData->lead_date; ?></td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td colspan="2" class="lbr"><strong><?php echo $masterData->client_name; ?></strong></td>
    </tr>
    <tr>
        <td colspan="2" class="lbr" height="20px" valign="top"><?php echo $masterData->address; ?></td>
    </tr>
    <tr>
        <td colspan="2" class="lbr"><strong>Contact Person:</strong> <?php echo !empty($masterData->contact_person) ? $masterData->contact_person : $masterData->client_name; ?></td>
    </tr>
    <tr>
        <td colspan="0" class="lbr"><strong>State:</strong> <?php echo @$masterData->state; ?></td>
        <td colspan="0"><strong>State Code: <?php echo @$masterData->state_code; ?></strong></td>
    </tr>
    <tr>
        <td colspan="2" class="lbr bbr"><strong>GSTIN No:</strong> <?php echo @$masterData->client_gst; ?></td>
    </tr>
</table>
<br />
<table class="leadItemContent">
    <?php echo $ItemList; ?>
    <?php for ($i = 0; $i < 5 - $line; $i++) {   ?>
        <tr>
            <td style="border-top:none;border-bottom:none;"> &nbsp; </td>
            <td style="border-top:none;border-bottom:none;"> &nbsp; </td>
            <td style="border-top:none;border-bottom:none;"> &nbsp; </td>
            <td style="border-top:none;border-bottom:none;"> &nbsp; </td>
            <td style="border-top:none;border-bottom:none;"> &nbsp; </td>
            <td style="border-top:none;border-bottom:none;"> &nbsp; </td>
            <td style="border-top:none;border-bottom:none;"> &nbsp; </td>

        </tr>
    <?php } ?>
    <tr>
        <td colspan="6" align="right"><strong>Total: </strong></td>
        <td align='right'><strong><?php echo $masterData->gross_amt; ?></strong></td>
    </tr>
</table>
<div style=" bottom: 0mm;">
    <table class="leadTaxFooter">
        <tr>
            <td colspan="2" rowspan="5" class="tbr lbr rbr bbr">
                <?php if (!empty($TaxBreakup)) { ?>
                    <table class="taxBrkup">
                        <tr>
                            <th>Tax Type</th>
                            <th>Taxable Amt</th>
                            <th>Tax Amt</th>
                        </tr>
                        <?php echo $TaxBreakup; ?>
                    </table>
                <?php } ?>
                <table>
                    <tr>
                        <td><strong>Notes:</strong></td>
                        <td>
                            <pre><?php echo $masterData->notes; ?></pre>
                        </td>
                    </tr>
                    <tr>
                        <td><strong>Tax Amt:</strong></td>
                        <td style="word-wrap: break-word; white-space: normal;">
                            <?php echo htmlspecialchars($TaxAmountInWords); ?>
                        </td>
                    </tr>
                    <tr>
                        <td><strong>Amt:</strong></td>
                        <td style="word-wrap: break-word; white-space: normal;">
                            <?php echo htmlspecialchars($AmountInWords); ?>
                        </td>
                    </tr>
                </table>
            </td>
            <td class="tbr rbr"><strong>Gross Amount:</strong></td>
            <td class="tbr rbr" align="right"><?php echo $masterData->gross_amt; ?></td>
        </tr>

        <tr>
            <td class="tbr rbr"><strong>Discount:</strong></td>
            <td class="tbr rbr" align="right"><?php echo $masterData->discount_amt; ?></td>
        </tr>

        <tr>
            <td class="tbr rbr"><strong>Tax:</strong></td>
            <td class="tbr rbr" align="right"><?php echo $masterData->tax_amt; ?></td>
        </tr>

        <tr>
            <td class="tbr rbr"><strong>Round Off:</strong></td>
            <td class="tbr rbr" align="right"><?php echo $masterData->round_off_amt; ?></td>
        </tr>

        <tr>
            <td class="tbr bbr rbr"><strong>Net Amount:</strong></td>
            <td class="tbr bbr rbr" align="right"><strong><?php echo $masterData->net_amount; ?></strong></td>
        </tr>

        <tr>
            <td class="tbr lbr rbr bbr" colspan="4">
                <?php if ($masterData->payment_terms != '') { ?>
                    <pre><strong>Payment Terms:</strong> <?php echo $masterData->payment_terms; ?> <br/></pre>
                <?php } else { ?>
                    &nbsp; <br />
                <?php } ?>
                <?php if ($masterData->delivery_terms != '') { ?>
                    <pre><strong>Delivery Terms:</strong> <?php echo $masterData->delivery_terms; ?><br/>
                    </pre>
                <?php } else { ?>
                    &nbsp; <br />
                <?php } ?>
                <!-- <?php if ($masterData->packing != '') { ?>
                    <pre><strong>Packing:</strong> <?php echo $masterData->packing; ?><br/></pre>
                <?php }  ?>
                <?php if ($masterData->ship_to != '') { ?>
                    <pre><strong>Ship To:</strong> <?php echo $masterData->ship_to; ?></pre>
                <?php }  ?> -->
            </td>
        </tr>

        <tr>
            <!-- <td class="lbr rbr bbr" style="vertical-align: top;" width="40%">
                <strong>Mode Of Transport:</strong> <br />

                <strong>Insurance:</strong><br />

                <strong>Frieght:</strong>
            </td>
            <td class="lbr bbr" style="vertical-align: top;">
                <?php echo $masterData->transport; ?> <br />

                <?php echo $masterData->insurance; ?> <br />

                <?php echo $masterData->freight; ?>
            </td> -->

            <td class="lbr rbr" colspan="4" style="vertical-align: top;" <?php echo ($compData->name != "SAN Softwares Pvt Ltd") ? 'colspan = "2"' : ""; ?>>
                <strong>Name:</strong><?php echo $compData->name; ?>
                <br />
                <strong>Bank:</strong><?php echo $branchDetails->bank_name; ?> <br />
                <strong>Account No:</strong><?php echo $branchDetails->bank_acc_no; ?><br />
                <strong>IFSC Code:</strong><?php echo $branchDetails->bank_ifsc_code; ?>
            </td>
        </tr>

        <tr>
            <td colspan="2" rowspan="3" class="tbr bbr lbr rbr" style="vertical-align: top">
                <strong>Terms & Conditions:</strong>
                <?php echo $branchDetails->invoice_remarks; ?>
            </td>

            <td colspan="2" class="tbr rbr" align="right"><strong>For <?php echo $compData->name; ?></strong></td>
        </tr>

        <tr>
            <td colspan="2" class="rbr" height="30px" align="right"><?php if (!empty($branchDetails->sign_img_path)) { ?>
                    <img src="<?php echo $branchDetails->sign_img_path; ?>" style="max-height:40px;" />
                <?php } ?>
            </td>
        </tr>

        <tr>
            <td colspan="2" class="bbr rbr" align="right">
                <strong><?php echo $branchDetails->signatory; ?></strong>
            </td>
        </tr>

        <tr>
            <td colspan="2" style="vertical-align: top" rowspan="3" class="tbr bbr lbr rbr">

                <strong> I have read the terms and conditions and I hereby accept
                    the proposal and agree to the terms and conditions as stated above.</strong><br>

            </td>

            <td colspan="2" class="tbr rbr" align="right">
                <strong>Accepted By : <?php echo $masterData->client_name; ?></strong>
            </td>
        </tr>

        <tr>
            <td colspan="2" class="rbr" height="30px" align="right">


            </td>

        </tr>

        <tr>
            <td colspan="2" class="bbr rbr" align="right">
                <strong><?php echo $branchDetails->signatory; ?></strong>
            </td>
        </tr>

    </table>
</div>