« Back to History
parcel_send_back_in_stock.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php /* ====================================================================== FILE: /erp/parcel_send_back_in_stock.php PURPOSE: Reverse Parcel Send (Delete Entry & Update Stock back to 'in') ====================================================================== */ error_reporting(E_ALL); ini_set('display_errors', 1); /* ---------- 1. AUTH + DB ---------- */ require_once __DIR__ . '/modules/auth/page_acl.php'; require_once __DIR__ . '/helpers/activity_helper.php'; // After successful revert/back in stock, add: // activity_create('parcel', 'parcel_send_back_in_stock', 0, 'Parcel send back in stock'); $ctx = page_require_access('parcel_send_simple'); $pdo = $ctx['pdo']; $COMPANY_ID = (int)$ctx['company_id']; $USER_ID = (int)($ctx['user']['id'] ?? 0); function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } $msg = $err = ''; $bill_to_search = trim($_GET['search_bill'] ?? ''); $found_parcel = null; /* ---------- 2. SEARCH LOGIC ---------- */ if ($bill_to_search !== '') { $stSearch = $pdo->prepare("SELECT * FROM parcel_send_entry WHERE bill_no = ? AND company_id = ? LIMIT 1"); $stSearch->execute([$bill_to_search, $COMPANY_ID]); $found_parcel = $stSearch->fetch(PDO::FETCH_ASSOC); if (!$found_parcel) { $err = "No record found for Bill #$bill_to_search in Sent history."; } } /* ---------- 3. REVERSE / DELETE LOGIC ---------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm_revert'])) { $target_bill = $_POST['target_bill']; try { $pdo->beginTransaction(); // 1. Delete from parcel_send_entry $stDel = $pdo->prepare("DELETE FROM parcel_send_entry WHERE bill_no = ? AND company_id = ?"); $stDel->execute([$target_bill, $COMPANY_ID]); if ($stDel->rowCount() > 0) { // 2. Update stock back to 'in' $upd = $pdo->prepare("UPDATE parcel_stock_in SET status = 'in' WHERE company_id = ? AND bill_no = ? AND status = 'out'"); $upd->execute([$COMPANY_ID, $target_bill]); $pdo->commit(); // Standardized activity log require_once __DIR__ . '/helpers/activity_helper.php'; log_activity([ 'activity_type' => 'entry', 'module_name' => 'parcel_send_back_in_stock', 'action_name' => 'delete', 'activity_note' => "Bill: $target_bill moved back to stock", 'reference_id' => $target_bill, ]); $msg = "Success! Bill #$target_bill has been moved back to stock."; $found_parcel = null; // Clear view after success } else { throw new Exception("Record already removed or not found."); } } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $err = "Revert Failed: " . $e->getMessage(); } } require_once __DIR__ . '/partials/header.php'; ?> <div class="container mt-4"> <div class="row justify-content-center"> <div class="col-md-6"> <div class="d-flex justify-content-between align-items-center mb-4"> <h3 class="fw-bold text-danger"><i class="bi bi-arrow-counterclockwise me-2"></i>Back to Stock</h3> <a href="parcel_send_list.php" class="btn btn-sm btn-outline-secondary">View Sent List</a> </div> <!-- Search Card --> <div class="card shadow-sm border-0 mb-4"> <div class="card-body p-4"> <form method="get" class="row g-2"> <label class="form-label fw-bold">Enter Bill Number to Revert</label> <div class="col-9"> <input type="text" name="search_bill" class="form-control" placeholder="Ex: 397" value="<?= h($bill_to_search) ?>" required> </div> <div class="col-3"> <button type="submit" class="btn btn-dark w-100">Search</button> </div> </form> </div> </div> <?php if ($msg): ?> <div class="alert alert-success border-0 shadow-sm"><?= $msg ?></div> <?php endif; ?> <?php if ($err): ?> <div class="alert alert-danger border-0 shadow-sm"><?= h($err) ?></div> <?php endif; ?> <!-- Result & Action Card --> <?php if ($found_parcel): ?> <div class="card border-danger shadow-sm"> <div class="card-header bg-danger text-white fw-bold"> Confirm Reversal </div> <div class="card-body"> <table class="table table-sm borderless mb-3"> <tr><th width="40%">Bill No:</th><td><?= h($found_parcel['bill_no']) ?></td></tr> <tr><th>Vehicle No:</th><td><?= h($found_parcel['vehicle_no']) ?></td></tr> <tr><th>Send Date:</th><td><?= date('d-M-Y', strtotime($found_parcel['send_date'])) ?></td></tr> <tr><th>Remark:</th><td><?= h($found_parcel['remark'] ?: '-') ?></td></tr> </table> <div class="alert alert-warning small"> <i class="bi bi-exclamation-triangle me-2"></i> Warning: This will delete the dispatch entry and make this bill available in "Send" list again. </div> <form method="post" onsubmit="return confirm('Are you sure you want to revert this parcel to stock?');"> <input type="hidden" name="target_bill" value="<?= h($found_parcel['bill_no']) ?>"> <button type="submit" name="confirm_revert" class="btn btn-danger w-100 py-2 fw-bold"> YES, DELETE & REVERT TO STOCK </button> </form> </div> </div> <?php endif; ?> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>