<?php
require_once '../includes/auth.php';
requirePermission('reports_master_profit', 'view');
require_once '../db.php';
require_once '../includes/header.php';
require_once '../includes/sidebar.php';

$from        = $_GET['from']        ?? date('Y-m-01');
$to          = $_GET['to']          ?? date('Y-m-d');
$customer_id = $_GET['customer_id'] ?? '';
$view = $_GET['view'] ?? (isset($_GET['itemwise']) ? 'item' : 'invoice');

$itemWise = ($view === 'item');
$dateWise = ($view === 'date');

$customers = $conn->query("SELECT id, name FROM clients ORDER BY name ASC");

$types  = 'ss';
$params = [$from, $to];

$clientWhere = '';
if (!empty($customer_id)) {
    $clientWhere = ' AND mi.client_id = ? ';
    $params[]    = $customer_id;
    $types      .= 'i';
}

// Returns subquery: net returned sale (excl GST) and cost per sales_item
$retJoin = "
    LEFT JOIN (
        SELECT sri.sales_item_id,
               SUM(sri.return_qty)                     AS ret_qty,
               SUM(sri.taxable_amount)                 AS ret_sale_excl,
               SUM(sri.return_qty * s2.purchase_price) AS ret_cost
        FROM sales_return_items sri
        JOIN sales_returns sr ON sr.id = sri.sales_return_id AND sr.status = 'final'
        JOIN sales_items   s2 ON s2.id = sri.sales_item_id
        GROUP BY sri.sales_item_id
    ) ret ON ret.sales_item_id = s.id
";

// ── Summary (always aggregate, independent of view mode) ──────────────────
$summaryStmt = $conn->prepare("
    SELECT
        SUM(s.selling_price_excl * s.sold_qty)  AS gross_sale,
        SUM(s.purchase_price     * s.sold_qty)  AS gross_cost,
        SUM(COALESCE(ret.ret_sale_excl, 0))     AS total_returns,
        SUM(COALESCE(ret.ret_cost,      0))     AS total_ret_cost
    FROM master_invoices mi
    JOIN sales_invoices si ON si.master_invoice_id = mi.id AND si.status = 'final'
    JOIN sales_items    s  ON s.sales_invoice_id   = si.id
    $retJoin
    WHERE mi.invoice_date BETWEEN ? AND ?
    $clientWhere
");
$summaryStmt->bind_param($types, ...$params);
$summaryStmt->execute();
$sum = $summaryStmt->get_result()->fetch_assoc();

$grossSales   = (float)($sum['gross_sale']     ?? 0);
$grossCost    = (float)($sum['gross_cost']     ?? 0);
$grossProfit  = $grossSales - $grossCost;
$totalReturns = (float)($sum['total_returns']  ?? 0);
$totalRetCost = (float)($sum['total_ret_cost'] ?? 0);
$netSales     = $grossSales - $totalReturns;
$netCost      = $grossCost  - $totalRetCost;
$netProfit    = $netSales   - $netCost;
$profitMargin = $netSales > 0 ? ($netProfit / $netSales) * 100 : 0;

// ── Table query (view-mode dependent) ─────────────────────────────────────
$rows = [];
if ($itemWise) {
    $sql = "
        SELECT mi.id AS master_id, mi.master_no, mi.invoice_date,
               c.name AS customer_name,
               p.name AS product_name,
               s.batch_no,
               s.sold_qty,
               COALESCE(ret.ret_qty, 0)                                                        AS returned_qty,
               (s.sold_qty - COALESCE(ret.ret_qty, 0))                                        AS net_qty,
               s.purchase_price,
               s.selling_price_excl,
               ((s.selling_price_excl - s.purchase_price) * (s.sold_qty - COALESCE(ret.ret_qty, 0))) AS profit
        FROM master_invoices mi
        JOIN clients        c  ON c.id  = mi.client_id
        JOIN sales_invoices si ON si.master_invoice_id = mi.id AND si.status = 'final'
        JOIN sales_items    s  ON s.sales_invoice_id   = si.id
        JOIN products       p  ON p.id  = s.product_id
        $retJoin
        WHERE mi.invoice_date BETWEEN ? AND ?
          $clientWhere
        ORDER BY mi.id DESC, p.name ASC
    ";
} elseif ($dateWise) {
    $sql = "
        SELECT mi.invoice_date,
               SUM(s.selling_price_excl * s.sold_qty) - SUM(COALESCE(ret.ret_sale_excl, 0)) AS total_sale_excl,
               SUM(s.purchase_price     * s.sold_qty) - SUM(COALESCE(ret.ret_cost,      0)) AS total_cost,
               SUM((s.selling_price_excl - s.purchase_price) * s.sold_qty)
                   - SUM(COALESCE(ret.ret_sale_excl, 0) - COALESCE(ret.ret_cost, 0))         AS profit
        FROM master_invoices mi
        JOIN clients        c  ON c.id  = mi.client_id
        JOIN sales_invoices si ON si.master_invoice_id = mi.id AND si.status = 'final'
        JOIN sales_items    s  ON s.sales_invoice_id   = si.id
        $retJoin
        WHERE mi.invoice_date BETWEEN ? AND ?
          $clientWhere
        GROUP BY mi.invoice_date
        ORDER BY mi.invoice_date ASC
    ";
} else {
    $sql = "
        SELECT mi.id AS master_id, mi.master_no, mi.invoice_date,
               c.name AS customer_name,
               SUM(s.selling_price_excl * s.sold_qty) - SUM(COALESCE(ret.ret_sale_excl, 0)) AS total_sale_excl,
               SUM(s.purchase_price     * s.sold_qty) - SUM(COALESCE(ret.ret_cost,      0)) AS total_cost,
               SUM((s.selling_price_excl - s.purchase_price) * s.sold_qty)
                   - SUM(COALESCE(ret.ret_sale_excl, 0) - COALESCE(ret.ret_cost, 0))         AS profit
        FROM master_invoices mi
        JOIN clients        c  ON c.id  = mi.client_id
        JOIN sales_invoices si ON si.master_invoice_id = mi.id AND si.status = 'final'
        JOIN sales_items    s  ON s.sales_invoice_id   = si.id
        $retJoin
        WHERE mi.invoice_date BETWEEN ? AND ?
          $clientWhere
        GROUP BY mi.id
        ORDER BY mi.id DESC
    ";
}
$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) $rows[] = $row;

// ── Returns table query (view-mode dependent) ─────────────────────────────
$retRows   = [];
$retWhere  = " WHERE sr.return_date BETWEEN ? AND ? AND sr.status = 'final' ";
$retParams = [$from, $to];
$retTypes  = 'ss';
if (!empty($customer_id)) {
    $retWhere   .= ' AND sr.client_id = ? ';
    $retParams[] = $customer_id;
    $retTypes   .= 'i';
}

if ($dateWise) {
    $retSql = "
        SELECT sr.return_date,
               SUM(sri.taxable_amount)                  AS ret_sale_excl,
               SUM(sri.return_qty * si2.purchase_price) AS ret_cost
        FROM sales_returns       sr
        JOIN sales_invoices      inv ON inv.id  = sr.sales_invoice_id
        JOIN master_invoices     mi  ON mi.id   = inv.master_invoice_id
        JOIN sales_return_items  sri ON sri.sales_return_id = sr.id
        JOIN sales_items         si2 ON si2.id = sri.sales_item_id
        $retWhere
        GROUP BY sr.return_date
        ORDER BY sr.return_date ASC
    ";
} elseif ($itemWise) {
    $retSql = "
        SELECT sr.id, sr.return_no, sr.return_date, c.name AS customer_name,
               mi.master_no,  mi.id AS master_id,
               p.name         AS product_name,  si2.batch_no,
               sri.return_qty,
               sri.taxable_amount                          AS ret_sale_excl,
               (sri.return_qty * si2.purchase_price)       AS ret_cost
        FROM sales_returns       sr
        JOIN clients             c   ON c.id   = sr.client_id
        JOIN sales_invoices      inv ON inv.id  = sr.sales_invoice_id
        JOIN master_invoices     mi  ON mi.id   = inv.master_invoice_id
        JOIN sales_return_items  sri ON sri.sales_return_id = sr.id
        JOIN sales_items         si2 ON si2.id  = sri.sales_item_id
        JOIN products            p   ON p.id    = si2.product_id
        $retWhere
        ORDER BY sr.id DESC, p.name ASC
    ";
} else {
    $retSql = "
        SELECT sr.id, sr.return_no, sr.return_date, c.name AS customer_name,
               mi.master_no,  mi.id  AS master_id,
               SUM(sri.taxable_amount)                  AS ret_sale_excl,
               SUM(sri.return_qty * si2.purchase_price) AS ret_cost
        FROM sales_returns       sr
        JOIN clients             c   ON c.id   = sr.client_id
        JOIN sales_invoices      inv ON inv.id  = sr.sales_invoice_id
        JOIN master_invoices     mi  ON mi.id   = inv.master_invoice_id
        JOIN sales_return_items  sri ON sri.sales_return_id = sr.id
        JOIN sales_items         si2 ON si2.id  = sri.sales_item_id
        $retWhere
        GROUP BY sr.id
        ORDER BY sr.id DESC
    ";
}
$retStmt = $conn->prepare($retSql);
$retStmt->bind_param($retTypes, ...$retParams);
$retStmt->execute();
$retResult = $retStmt->get_result();
while ($rr = $retResult->fetch_assoc()) $retRows[] = $rr;

$colspan    = $itemWise ? 11 : ($dateWise ? 5 : 6);
$retColspan = $dateWise ? 4 : ($itemWise ? 10 : 7);
$exportQs   = http_build_query(['from' => $from, 'to' => $to, 'customer_id' => $customer_id, 'view' => $view]);
?>

<div class="page-header">
    <h2><i class="fas fa-sitemap" style="color:#0891b2;margin-right:8px;"></i>Master Sales Profit Report</h2>
    <div style="display:flex;gap:8px;">
        <a href="master_profit_export_excel.php?<?= $exportQs ?>" target="_blank" class="btn btn-success btn-sm">
            <i class="fas fa-file-excel"></i> Excel
        </a>
        <a href="master_profit_export_pdf.php?<?= $exportQs ?>" target="_blank" class="btn btn-danger btn-sm">
            <i class="fas fa-file-pdf"></i> PDF
        </a>
    </div>
</div>

<div style="background:#e0f2fe;border:1px solid #bae6fd;border-radius:8px;padding:10px 14px;margin-bottom:14px;font-size:13px;color:#0369a1;">
    <i class="fas fa-info-circle" style="margin-right:6px;"></i>
    Profit aggregated across <strong>all companies</strong> per master invoice, regardless of your active company.
    Returns are deducted from revenue and cost.
</div>

<!-- Filters -->
<div class="filter-panel">
    <form method="GET" class="filter-form">
        <div class="filter-group">
            <label>From</label>
            <input type="date" name="from" value="<?= htmlspecialchars($from) ?>">
        </div>
        <div class="filter-group">
            <label>To</label>
            <input type="date" name="to" value="<?= htmlspecialchars($to) ?>">
        </div>
        <div class="filter-group">
            <label>Customer</label>
            <select name="customer_id">
                <option value="">All Customers</option>
                <?php while ($c = $customers->fetch_assoc()): ?>
                    <option value="<?= $c['id'] ?>" <?= ($customer_id == $c['id']) ? 'selected' : '' ?>>
                        <?= htmlspecialchars($c['name']) ?>
                    </option>
                <?php endwhile; ?>
            </select>
        </div>
        <div class="filter-group">
            <label>View</label>
            <select name="view">
                <option value="invoice" <?= $view === 'invoice' ? 'selected' : '' ?>>Invoice Wise</option>
                <option value="date"    <?= $view === 'date'    ? 'selected' : '' ?>>Date Wise</option>
                <option value="item"    <?= $view === 'item'    ? 'selected' : '' ?>>Item Wise</option>
            </select>
        </div>
        <div class="filter-actions">
            <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> Generate</button>
            <a href="master_profit_report.php" class="btn btn-secondary">Reset</a>
        </div>
    </form>
</div>

<!-- Summary cards -->
<div class="stat-cards">
    <div class="stat-card">
        <div class="stat-card-label">Gross Sales (Excl GST)</div>
        <div class="stat-card-value" style="color:#0891b2;">&#8377; <?= number_format($grossSales, 2) ?></div>
    </div>
    <div class="stat-card">
        <div class="stat-card-label">Gross Cost</div>
        <div class="stat-card-value" style="color:var(--danger);">&#8377; <?= number_format($grossCost, 2) ?></div>
    </div>
    <div class="stat-card">
        <div class="stat-card-label">Gross Profit</div>
        <div class="stat-card-value" style="color:var(--success);">&#8377; <?= number_format($grossProfit, 2) ?></div>
    </div>
    <div class="stat-card">
        <div class="stat-card-label">Returns (Excl GST)</div>
        <div class="stat-card-value" style="color:#f59e0b;">&#8377; <?= number_format($totalReturns, 2) ?></div>
    </div>
    <div class="stat-card">
        <div class="stat-card-label">Returns Profit Impact</div>
        <div class="stat-card-value" style="color:#f59e0b;">&#8377; <?= number_format($totalReturns - $totalRetCost, 2) ?></div>
    </div>
    <div class="stat-card">
        <div class="stat-card-label">Net Profit</div>
        <div class="stat-card-value" style="color:<?= $netProfit >= 0 ? 'var(--success)' : 'var(--danger)' ?>;">
            &#8377; <?= number_format($netProfit, 2) ?>
        </div>
    </div>
    <div class="stat-card">
        <div class="stat-card-label">Profit Margin</div>
        <div class="stat-card-value" style="color:var(--info);">
            <?= $netSales > 0 ? number_format($profitMargin, 1) . '%' : '—' ?>
        </div>
    </div>
</div>

<!-- Master Invoices table -->
<div class="panel" style="margin-bottom:20px;">
    <div class="panel-header">
        <span class="panel-title">
            <i class="fas fa-sitemap" style="margin-right:6px;color:#0891b2;"></i>
            Master Invoices
        </span>
    </div>
    <div style="overflow-x:auto;">
        <table>
            <thead>
                <?php if ($itemWise): ?>
                <tr>
                    <th>Master Ref</th><th>Date</th><th>Customer</th><th>Product</th>
                    <th>Batch</th>
                    <th style="text-align:center;">Sold</th>
                    <th style="text-align:center;">Returned</th>
                    <th style="text-align:center;">Net Qty</th>
                    <th style="text-align:right;">Purchase Rate</th>
                    <th style="text-align:right;">Sell Rate (Excl GST)</th>
                    <th style="text-align:right;">Net Profit</th>
                </tr>
                <?php elseif ($dateWise): ?>
                <tr>
                    <th>Date</th>
                    <th style="text-align:right;">Net Sales (Excl GST)</th>
                    <th style="text-align:right;">Net Cost</th>
                    <th style="text-align:right;">Net Profit</th>
                    <th style="text-align:right;">Margin %</th>
                </tr>
                <?php else: ?>
                <tr>
                    <th>Master Ref</th><th>Date</th><th>Customer</th>
                    <th style="text-align:right;">Net Sales (Excl GST)</th>
                    <th style="text-align:right;">Net Cost</th>
                    <th style="text-align:right;">Net Profit</th>
                </tr>
                <?php endif; ?>
            </thead>
            <tbody>
            <?php if (count($rows) > 0): ?>
                <?php foreach ($rows as $row): ?>
                <tr>
                <?php if ($dateWise): ?>
                    <td><strong><?= date('d-m-Y', strtotime($row['invoice_date'])) ?></strong></td>
                    <td style="text-align:right;">&#8377; <?= number_format($row['total_sale_excl'], 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($row['total_cost'], 2) ?></td>
                    <?php $pp = ($row['total_sale_excl'] != 0) ? ($row['profit'] / $row['total_sale_excl']) * 100 : null; ?>
                    <td style="text-align:right;color:<?= $row['profit'] >= 0 ? 'var(--success)' : 'var(--danger)' ?>;">
                        <strong>&#8377; <?= number_format($row['profit'], 2) ?></strong>
                    </td>
                    <td style="text-align:right;color:var(--text-secondary);font-size:13px;">
                        <?= $pp !== null ? number_format($pp, 1) . '%' : '—' ?>
                    </td>
                <?php else: ?>
                    <td>
                        <a href="#" onclick="openDocPopup('master',<?= $row['master_id'] ?>,'<?= htmlspecialchars($row['master_no'], ENT_QUOTES) ?>');return false;"
                           style="font-weight:700;color:#0891b2;text-decoration:none;">
                            <?= htmlspecialchars($row['master_no']) ?>
                        </a>
                    </td>
                    <td><?= date('d-m-Y', strtotime($row['invoice_date'])) ?></td>
                    <td><?= htmlspecialchars($row['customer_name']) ?></td>
                    <?php if ($itemWise): ?>
                    <td><?= htmlspecialchars($row['product_name']) ?></td>
                    <td style="color:var(--text-secondary);font-size:12px;"><?= htmlspecialchars($row['batch_no']) ?></td>
                    <td style="text-align:center;"><?= number_format($row['sold_qty'], 2) ?></td>
                    <td style="text-align:center;color:var(--danger);">
                        <?= $row['returned_qty'] > 0 ? number_format($row['returned_qty'], 2) : '—' ?>
                    </td>
                    <td style="text-align:center;font-weight:600;"><?= number_format($row['net_qty'], 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($row['purchase_price'], 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($row['selling_price_excl'], 2) ?></td>
                    <?php else: ?>
                    <td style="text-align:right;">&#8377; <?= number_format($row['total_sale_excl'], 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($row['total_cost'], 2) ?></td>
                    <?php endif; ?>
                    <?php
                        $rowSale = $itemWise
                            ? ($row['selling_price_excl'] * $row['net_qty'])
                            : $row['total_sale_excl'];
                        $pp = ($rowSale != 0) ? ($row['profit'] / $rowSale) * 100 : null;
                    ?>
                    <td style="text-align:right;color:<?= $row['profit'] >= 0 ? 'var(--success)' : 'var(--danger)' ?>;">
                        <strong>&#8377; <?= number_format($row['profit'], 2) ?></strong>
                        <?php if ($pp !== null): ?>
                            <span style="font-size:11px;font-weight:400;opacity:0.8;">(<?= number_format($pp, 1) ?>%)</span>
                        <?php endif; ?>
                    </td>
                <?php endif; ?>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="<?= $colspan ?>" style="text-align:center;padding:30px;color:var(--text-muted);">
                        No invoices found.
                    </td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- Returns table -->
<div class="panel">
    <div class="panel-header">
        <span class="panel-title">
            <i class="fas fa-rotate-left" style="margin-right:6px;color:#f59e0b;"></i>
            Master Sales Returns (Credit Notes)
        </span>
        <?php if (count($retRows) > 0): ?>
        <span style="font-size:12px;color:var(--text-muted);font-weight:400;">
            <?= count($retRows) ?> row<?= count($retRows) > 1 ? 's' : '' ?> in period
        </span>
        <?php endif; ?>
    </div>
    <div style="overflow-x:auto;">
        <table>
            <thead>
                <?php if ($dateWise): ?>
                <tr>
                    <th>Date</th>
                    <th style="text-align:right;">Return Value (Excl GST)</th>
                    <th style="text-align:right;">Cost Recovered</th>
                    <th style="text-align:right;">Profit Impact</th>
                </tr>
                <?php elseif ($itemWise): ?>
                <tr>
                    <th>Credit Note</th>
                    <th>Return Date</th>
                    <th>Customer</th>
                    <th>Against Master Invoice</th>
                    <th>Product</th>
                    <th>Batch</th>
                    <th style="text-align:center;">Return Qty</th>
                    <th style="text-align:right;">Return Value (Excl GST)</th>
                    <th style="text-align:right;">Cost Recovered</th>
                    <th style="text-align:right;">Profit Impact</th>
                </tr>
                <?php else: ?>
                <tr>
                    <th>Credit Note</th>
                    <th>Return Date</th>
                    <th>Customer</th>
                    <th>Against Master Invoice</th>
                    <th style="text-align:right;">Return Value (Excl GST)</th>
                    <th style="text-align:right;">Cost Recovered</th>
                    <th style="text-align:right;">Profit Impact</th>
                </tr>
                <?php endif; ?>
            </thead>
            <tbody>
            <?php if (count($retRows) > 0): ?>
                <?php foreach ($retRows as $rr):
                    $impact = (float)$rr['ret_sale_excl'] - (float)$rr['ret_cost'];
                ?>
                <tr>
                <?php if ($dateWise): ?>
                    <td><strong><?= date('d-m-Y', strtotime($rr['return_date'])) ?></strong></td>
                    <td style="text-align:right;">&#8377; <?= number_format($rr['ret_sale_excl'], 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($rr['ret_cost'], 2) ?></td>
                    <td style="text-align:right;color:var(--danger);">
                        <strong>−&#8377; <?= number_format($impact, 2) ?></strong>
                    </td>
                <?php elseif ($itemWise): ?>
                    <td style="font-weight:700;color:#f59e0b;"><?= htmlspecialchars($rr['return_no']) ?></td>
                    <td><?= date('d-m-Y', strtotime($rr['return_date'])) ?></td>
                    <td><?= htmlspecialchars($rr['customer_name']) ?></td>
                    <td>
                        <a href="#" onclick="openDocPopup('master',<?= $rr['master_id'] ?>,'<?= htmlspecialchars($rr['master_no'], ENT_QUOTES) ?>');return false;"
                           style="color:#0891b2;text-decoration:none;">
                            <?= htmlspecialchars($rr['master_no']) ?>
                        </a>
                    </td>
                    <td><?= htmlspecialchars($rr['product_name']) ?></td>
                    <td style="color:var(--text-secondary);font-size:12px;"><?= htmlspecialchars($rr['batch_no']) ?></td>
                    <td style="text-align:center;"><?= number_format($rr['return_qty'], 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($rr['ret_sale_excl'], 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($rr['ret_cost'], 2) ?></td>
                    <td style="text-align:right;color:var(--danger);">
                        <strong>−&#8377; <?= number_format($impact, 2) ?></strong>
                    </td>
                <?php else: ?>
                    <td style="font-weight:700;color:#f59e0b;"><?= htmlspecialchars($rr['return_no']) ?></td>
                    <td><?= date('d-m-Y', strtotime($rr['return_date'])) ?></td>
                    <td><?= htmlspecialchars($rr['customer_name']) ?></td>
                    <td>
                        <a href="#" onclick="openDocPopup('master',<?= $rr['master_id'] ?>,'<?= htmlspecialchars($rr['master_no'], ENT_QUOTES) ?>');return false;"
                           style="color:#0891b2;text-decoration:none;">
                            <?= htmlspecialchars($rr['master_no']) ?>
                        </a>
                    </td>
                    <td style="text-align:right;">&#8377; <?= number_format($rr['ret_sale_excl'], 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($rr['ret_cost'], 2) ?></td>
                    <td style="text-align:right;color:var(--danger);">
                        <strong>−&#8377; <?= number_format($impact, 2) ?></strong>
                    </td>
                <?php endif; ?>
                </tr>
                <?php endforeach; ?>
                <tr style="background:var(--bg-light);font-weight:700;">
                    <td colspan="<?= $dateWise ? 1 : ($itemWise ? 7 : 4) ?>" style="text-align:right;padding-right:12px;">Total</td>
                    <td style="text-align:right;">&#8377; <?= number_format($totalReturns, 2) ?></td>
                    <td style="text-align:right;">&#8377; <?= number_format($totalRetCost, 2) ?></td>
                    <td style="text-align:right;color:var(--danger);">
                        −&#8377; <?= number_format($totalReturns - $totalRetCost, 2) ?>
                    </td>
                </tr>
            <?php else: ?>
                <tr>
                    <td colspan="<?= $retColspan ?>" style="text-align:center;padding:24px;color:var(--text-muted);">
                        No returns in this period.
                    </td>
                </tr>
            <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require_once '../includes/doc_popup_modal.php'; ?>
<?php require_once '../includes/footer.php'; ?>
