<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\PosService; use App\Core\Database;
PosService::requireAccess();
$vendorId = PosService::currentVendorId();
$register = PosService::defaultRegister($vendorId);
$shift = $register ? PosService::currentShift((int)$register['id'], (int)(current_user()['id'] ?? 0)) : null;
$recent = Database::fetchAll('SELECT pr.*,ps.sale_no FROM pos_refunds pr LEFT JOIN pos_sales ps ON ps.id=pr.sale_id WHERE ps.vendor_id=? ORDER BY pr.id DESC LIMIT 30', [$vendorId]);
$pageTitle = 'Returns / Refunds | Pro POS';
require_once __DIR__ . '/partials/header.php';
?>
<div class="row g-3"><div class="col-lg-5"><div class="card pos-card"><div class="card-header bg-white"><strong>Create Refund / Exchange</strong></div><div class="card-body"><form id="posRefundForm"><input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>"><label class="form-label">Sale No or Receipt Barcode</label><input class="form-control" name="sale_no" placeholder="POS-20260509-..." required><label class="form-label mt-2">Refund amount</label><input class="form-control" name="amount" type="number" step="0.01" required><label class="form-label mt-2">Method</label><select class="form-select" name="method"><option value="cash">Cash</option><option value="bkash">bKash</option><option value="nagad">Nagad</option><option value="wallet">Wallet</option></select><label class="form-label mt-2">Reason</label><textarea class="form-control" name="reason" rows="3"></textarea><button class="btn btn-danger mt-3" type="submit">Submit Refund</button></form><div id="refundMessage" class="small mt-2"></div></div></div></div><div class="col-lg-7"><div class="card pos-card"><div class="card-header bg-white"><strong>Recent Refunds</strong></div><div class="table-responsive"><table class="table mb-0"><thead><tr><th>Sale</th><th>Amount</th><th>Method</th><th>Status</th><th>Date</th></tr></thead><tbody><?php if(!$recent): ?><?= table_empty(5,'No refunds yet.') ?><?php else: foreach($recent as $row): ?><tr><td><?= e($row['sale_no'] ?? '') ?></td><td><?= money($row['amount'] ?? 0) ?></td><td><?= e($row['method'] ?? '') ?></td><td><?= badge($row['status'] ?? 'pending') ?></td><td><?= e($row['created_at'] ?? '') ?></td></tr><?php endforeach; endif; ?></tbody></table></div></div></div></div>
<?php require_once __DIR__ . '/partials/footer.php'; ?>
