<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title><?= $page_title;?> - 80mm</title>
    <style>
        * {
            font-family: 'Courier New', 'Arial', monospace;
            font-size: 12px;
            line-height: 1.4;
            margin: 0;
            padding: 0;
        }
        body {
            padding: 0;
            margin: 0;
            width: 80mm;
        }
        .container {
            width: 100%;
            max-width: 80mm;
            padding: 4mm;
        }
        .center {
            text-align: center;
        }
        .left {
            text-align: left;
        }
        .right {
            text-align: right;
        }
        .bold {
            font-weight: bold;
        }
        .large {
            font-size: 15px;
            font-weight: bold;
        }
        .medium {
            font-size: 13px;
        }
        .small {
            font-size: 10px;
        }
        .tiny {
            font-size: 9px;
        }
        .line {
            border-bottom: 1px dashed #000;
            margin: 4px 0;
        }
        .line-solid {
            border-bottom: 1px solid #000;
            margin: 4px 0;
        }
        .line-double {
            border-bottom: 2px double #000;
            margin: 4px 0;
        }
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            text-align: left;
            padding: 2px 1px;
            vertical-align: top;
        }
        th {
            font-weight: bold;
            border-bottom: 1px dashed #000;
            font-size: 11px;
        }
        .total-row {
            font-weight: bold;
            font-size: 13px;
        }
        .two-col {
            display: table;
            width: 100%;
        }
        .two-col .col {
            display: table-cell;
            width: 50%;
            vertical-align: top;
        }
        .barcode {
            text-align: center;
            margin: 6px 0;
            font-family: 'Libre Barcode 128', monospace;
            font-size: 48px;
            letter-spacing: 0;
        }
        .qr-code {
            text-align: center;
            margin: 6px 0;
        }
        .qr-code img {
            max-width: 120px;
            height: auto;
        }
        .footer {
            margin-top: 6px;
            text-align: center;
            font-size: 10px;
        }
        .company-logo {
            text-align: center;
            margin: 4px 0;
        }
        .company-logo img {
            max-width: 100px;
            height: auto;
        }
        .section-header {
            background: #f0f0f0;
            padding: 2px 4px;
            font-weight: bold;
            margin: 4px 0 2px 0;
            font-size: 11px;
        }
        @media print {
            body {
                -webkit-print-color-adjust: exact;
                print-color-adjust: exact;
            }
            .no-print {
                display: none;
            }
            @page {
                margin: 0;
                size: 80mm auto;
            }
        }
    </style>
</head>
<?php
$CI =& get_instance();

// Fetch company information
$company_id = $this->session->userdata('company_id') ?: 1;
$q1=$this->db->query("select * from db_company where id=$company_id and status=1");
$res1=$q1->row();
$company_name		=$res1->company_name;
$company_mobile		=$res1->mobile;
$company_phone		=$res1->phone;
$company_email		=$res1->email;
$company_country	=$res1->country;
$company_state		=$res1->state;
$company_city		=$res1->city;
$company_address	=$res1->address;
$company_postcode	=$res1->postcode;
$company_gst_no		=$res1->gst_no;
$company_vat_number	=$res1->vat_no;
$company_logo		=$res1->company_logo;
$bank_details		=$res1->bank_details;
$upi_code			=$res1->upi_code;
$upi_id				=$res1->upi_id;

// Company logo path
$logo_path = '';
if(!empty($company_logo)){
    $logo_path = resolve_upload_url($company_logo, 'company/');
}

// UPI QR code path
$upi_qr_path = '';
if(!empty($upi_code)){
    $upi_qr_path = resolve_upload_url($upi_code, 'upi/');
}

// Fetch currency information
$q_currency=$this->db->query("select a.currency from db_currency a,db_sitesettings b where a.id=b.currency_id");
$res_currency=$q_currency->row();
$currency = !empty($res_currency) ? $res_currency->currency : '';

// Fetch sales and customer information
$q3=$this->db->query("SELECT a.sales_due,a.customer_name,a.mobile,a.phone,a.gstin,a.tax_number,a.email,
                           a.opening_balance,a.country_id,a.state_id,a.city,
                           a.postcode,a.address,b.sales_date,b.created_time,b.reference_no,
                           b.sales_code,b.sales_note,b.payment_status,
                           coalesce(b.grand_total,0) as grand_total,
                           coalesce(b.subtotal,0) as subtotal,
                           coalesce(b.paid_amount,0) as paid_amount,
                           coalesce(b.other_charges_input,0) as other_charges_input,
                           other_charges_tax_id,
                           coalesce(b.other_charges_amt,0) as other_charges_amt,
                           discount_to_all_input,
                           b.discount_to_all_type,
                           coalesce(b.tot_discount_to_all_amt,0) as tot_discount_to_all_amt,
                           coalesce(b.round_off,0) as round_off,
                           b.created_by

                           FROM db_customers a,
                           db_sales b
                           WHERE
                           a.`id`=b.`customer_id` AND
                           b.`id`='$sales_id'
                           ");

$res3=$q3->row();
$customer_name=$res3->customer_name;
$customer_mobile=$res3->mobile;
$customer_phone=$res3->phone;
$customer_email=$res3->email;
$customer_country=$res3->country_id;
$customer_state=$res3->state_id;
$customer_city=$res3->city;
$customer_address=$res3->address;
$customer_postcode=$res3->postcode;
$customer_gst_no=$res3->gstin;
$customer_tax_number=$res3->tax_number;
$customer_opening_balance=$res3->opening_balance;
$sales_date=show_date($res3->sales_date);
$reference_no=$res3->reference_no;
$created_time=show_time($res3->created_time);
$sales_code=$res3->sales_code;
$sales_note=$res3->sales_note;
$seller_name=$res3->created_by;
$customer_due=$res3->sales_due;
$payment_status=$res3->payment_status;

$subtotal=$res3->subtotal;
$grand_total=$res3->grand_total;
$other_charges_input=$res3->other_charges_input;
$other_charges_tax_id=$res3->other_charges_tax_id;
$other_charges_amt=$res3->other_charges_amt;
$paid_amount=$res3->paid_amount;
$discount_to_all_input=$res3->discount_to_all_input;
$discount_to_all_type=$res3->discount_to_all_type;
$tot_discount_to_all_amt=$res3->tot_discount_to_all_amt;
$round_off=$res3->round_off;

// Calculate total tax
$total_tax = 0;
$q_items_tax = $this->db->query("SELECT SUM(tax_amt) as total_tax FROM db_salesitems WHERE sales_id='$sales_id'");
$res_tax = $q_items_tax->row();
if($res_tax){
    $total_tax = $res_tax->total_tax;
}

if(!empty($customer_country)){
    $customer_country = $this->db->query("select country from db_country where id='$customer_country'")->row()->country;
}
if(!empty($customer_state)){
    $customer_state = $this->db->query("select state from db_states where id='$customer_state'")->row()->state;
}

?>
<body onload="window.print()">
<div class="container">
    <!-- Company Logo -->
    <?php if(!empty($logo_path)): ?>
    <div class="company-logo">
        <img src="<?= $logo_path; ?>" alt="<?= $company_name; ?>">
    </div>
    <?php endif; ?>

    <!-- Company Header -->
    <div class="center large">
        <?= strtoupper($company_name); ?>
    </div>
    <div class="center small">
        <?= $company_address; ?><?= $company_city ? ', '.$company_city : ''; ?><?= $company_postcode ? ' - '.$company_postcode : ''; ?><?= $company_state ? ', '.$company_state : ''; ?><br>
        <?php if($company_mobile): ?>Tel: <?= $company_mobile; ?><?php endif; ?>
        <?php if($company_phone && $company_phone != $company_mobile): ?> | <?= $company_phone; ?><?php endif; ?>
        <?php if($company_email): ?><br>Email: <?= $company_email; ?><?php endif; ?>
    </div>
    <div class="center tiny">
        <?php if($company_gst_no): ?>GST: <?= $company_gst_no; ?><?php endif; ?>
        <?php if($company_vat_number): ?> | VAT: <?= $company_vat_number; ?><?php endif; ?>
    </div>
    <div class="line-double"></div>

    <!-- Invoice Header -->
    <div class="center medium bold">
        SALES INVOICE
    </div>

    <!-- Invoice and Customer Info in Two Columns -->
    <div class="two-col">
        <div class="col">
            <table class="small">
                <tr>
                    <td class="bold">Invoice #:</td>
                    <td><?= $sales_code; ?></td>
                </tr>
                <tr>
                    <td>Date:</td>
                    <td><?= $sales_date; ?></td>
                </tr>
                <tr>
                    <td>Time:</td>
                    <td><?= $created_time; ?></td>
                </tr>
                <?php if(!empty($reference_no)): ?>
                <tr>
                    <td>Ref:</td>
                    <td><?= $reference_no; ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
        <div class="col">
            <table class="small">
                <tr>
                    <td class="bold">Customer:</td>
                    <td><?= $customer_name; ?></td>
                </tr>
                <?php if(!empty($customer_mobile)): ?>
                <tr>
                    <td>Mobile:</td>
                    <td><?= $customer_mobile; ?></td>
                </tr>
                <?php endif; ?>
                <?php if(!empty($customer_gst_no)): ?>
                <tr>
                    <td>GST:</td>
                    <td><?= $customer_gst_no; ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>

    <?php if(!empty($customer_address) || !empty($customer_city)): ?>
    <div class="tiny">
        <strong>Address:</strong> <?= $customer_address; ?><?= $customer_city ? ', '.$customer_city : ''; ?><?= $customer_postcode ? ' - '.$customer_postcode : ''; ?>
    </div>
    <?php endif; ?>

    <div class="line"></div>

    <!-- Items Table -->
    <table style="font-size: 10px;">
        <thead>
        <tr>
            <th style="width: 6%;">#</th>
            <th style="width: 36%;">Item</th>
            <th style="width: 10%; text-align: right;">Qty</th>
            <th style="width: 16%; text-align: right;">Rate</th>
            <th style="width: 12%; text-align: right;">Tax</th>
            <th style="width: 20%; text-align: right;">Total</th>
        </tr>
        </thead>
        <tbody>
        <?php
        $i = 0;
        $q2 = $this->db->query("SELECT b.item_name, a.sales_qty, a.price_per_unit, a.tax_amt, a.total_cost
                                FROM db_salesitems a
                                JOIN db_items b ON a.item_id = b.id
                                WHERE a.sales_id = '$sales_id'");
        foreach ($q2->result() as $res2) {
            echo "<tr>
                    <td>".++$i."</td>
                    <td>{$res2->item_name}</td>
                    <td class='right'>{$res2->sales_qty}</td>
                    <td class='right'>".number_format($res2->price_per_unit, 2)."</td>
                    <td class='right'>".number_format($res2->tax_amt, 2)."</td>
                    <td class='right'>".number_format($res2->total_cost, 2)."</td>
                  </tr>";
        }
        ?>
        </tbody>
    </table>
    <div class="line-solid"></div>

    <!-- Totals Section -->
    <table>
        <tr>
            <td>Subtotal:</td>
            <td class="right"><?= $currency.' '.number_format($subtotal, 2); ?></td>
        </tr>
        <?php if($tot_discount_to_all_amt > 0): ?>
        <tr>
            <td>Discount<?= $discount_to_all_input > 0 ? ' ('.$discount_to_all_input.'%)' : ''; ?>:</td>
            <td class="right">- <?= $currency.' '.number_format($tot_discount_to_all_amt, 2); ?></td>
        </tr>
        <?php endif; ?>
        <?php if($total_tax > 0): ?>
        <tr>
            <td>Total Tax:</td>
            <td class="right"><?= $currency.' '.number_format($total_tax, 2); ?></td>
        </tr>
        <?php endif; ?>
        <?php if($other_charges_amt > 0): ?>
        <tr>
            <td>Other Charges:</td>
            <td class="right"><?= $currency.' '.number_format($other_charges_amt, 2); ?></td>
        </tr>
        <?php endif; ?>
        <?php if($round_off != 0): ?>
        <tr>
            <td>Round Off:</td>
            <td class="right"><?= $currency.' '.number_format($round_off, 2); ?></td>
        </tr>
        <?php endif; ?>
    </table>
    <div class="line-double"></div>

    <!-- Grand Total -->
    <table class="total-row">
        <tr>
            <td class="bold large">GRAND TOTAL:</td>
            <td class="right large"><?= $currency.' '.number_format($grand_total, 2); ?></td>
        </tr>
        <tr style="font-size: 12px;">
            <td>Paid Amount:</td>
            <td class="right"><?= $currency.' '.number_format($paid_amount, 2); ?></td>
        </tr>
        <?php if($payment_status != 'Paid'): ?>
        <tr style="font-size: 12px;">
            <td class="bold">Balance Due:</td>
            <td class="right bold"><?= $currency.' '.number_format($customer_due, 2); ?></td>
        </tr>
        <?php endif; ?>
    </table>
    <div class="line"></div>

    <!-- Payment Status -->
    <div class="center medium bold">
        <?php
        if($payment_status == 'Paid'){
            echo '✓ PAID';
        } elseif($payment_status == 'Partial'){
            echo 'PARTIALLY PAID';
        } else {
            echo 'UNPAID';
        }
        ?>
    </div>

    <?php if(!empty($sales_note)): ?>
    <div class="line"></div>
    <div class="small">
        <strong>Note:</strong> <?= $sales_note; ?>
    </div>
    <?php endif; ?>

    <?php if(!empty($bank_details)): ?>
    <div class="line"></div>
    <div class="tiny">
        <strong>Bank Details:</strong> <?= nl2br($bank_details); ?>
    </div>
    <?php endif; ?>

    <div class="line"></div>

    <!-- Barcode and QR Code Section -->
    <div class="two-col">
        <div class="col" style="width: 60%;">
            <div class="barcode">
                *<?= $sales_code; ?>*
            </div>
            <div class="center tiny">Scan for Invoice Lookup</div>
        </div>
        <?php if(!empty($upi_qr_path)): ?>
        <div class="col" style="width: 40%;">
            <div class="qr-code">
                <img src="<?= $upi_qr_path; ?>" alt="UPI Payment">
            </div>
            <div class="center tiny">Pay via UPI</div>
        </div>
        <?php endif; ?>
    </div>

    <div class="line"></div>

    <!-- Footer -->
    <div class="footer">
        <strong>Thank you for your business!</strong><br>
        We appreciate your patronage. Visit again!<br>
        <?php if($company_mobile): ?>Contact: <?= $company_mobile; ?><?php endif; ?>
        <?php if($company_email): ?> | <?= $company_email; ?><?php endif; ?>
    </div>

    <div class="line"></div>
    <div class="center tiny">
        <em>This is a computer-generated invoice</em><br>
        Powered by bitSoft Bangladesh
    </div>
</div>
</body>
</html>
