« Back to History
parcel_send_simple.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ====================================================================== FILE: /erp/parcel_send_simple.php PURPOSE: Parcel Send Entry with Persistent Fields (Vehicle & Date) ====================================================================== */ 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 send entry add (insert), add: // activity_create('parcel', 'parcel_send_simple', 0, 'Parcel send simple entry'); $ctx = page_require_access('parcel_send_simple'); $pdo = $ctx['pdo']; $COMPANY_ID = (int)$ctx['company_id']; $USER_ID = (int)$ctx['user']['id']; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function buildParcelSendRemark($sendFromLocation, $remark) { $parts = []; if ($sendFromLocation !== '') { $parts[] = 'Send From: ' . preg_replace('/\s+/', ' ', $sendFromLocation); } if ($remark !== '') { $parts[] = preg_replace('/\s+/', ' ', $remark); } return $parts ? implode(' | ', $parts) : null; } /* ---------- 2. STATE & FILTERS ---------- */ $msg = $err = ''; // Persistent Fields Logic: Form submit ke baad yahi values wapas dikhayenge $selected_vehicle = $_POST['vehicle_no'] ?? ''; $selected_date = $_POST['send_date'] ?? date('Y-m-d'); $selected_send_from = trim($_POST['send_from_location'] ?? ''); // Filter Dates (For Bill List) $from_date = $_GET['from_date'] ?? date('Y-m-d'); $to_date = $_GET['to_date'] ?? date('Y-m-d'); /* ---------- 3. SAVE LOGIC ---------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_entry'])) { $bill_no = trim($_POST['bill_no'] ?? ''); $vehicle_no = trim($_POST['vehicle_no'] ?? ''); // Yahan se value fetch ho rahi hai $send_date = $_POST['send_date'] ?? ''; $send_from_location = trim($_POST['send_from_location'] ?? ''); $remark = trim($_POST['remark'] ?? ''); $stored_remark = buildParcelSendRemark($send_from_location, $remark); if ($bill_no === '' || $vehicle_no === '' || $send_date === '') { $err = 'Bill No, Vehicle and Send Date are required.'; } else { try { $pdo->beginTransaction(); // Delete-then-Insert $stDel = $pdo->prepare("DELETE FROM parcel_send_entry WHERE bill_no = ? AND company_id = ?"); $stDel->execute([$bill_no, $COMPANY_ID]); $sqlIns = "INSERT INTO parcel_send_entry (company_id, bill_no, vehicle_no, send_date, remark, created_at, created_by) VALUES (?, ?, ?, ?, ?, NOW(), ?)"; $pdo->prepare($sqlIns)->execute([ $COMPANY_ID, $bill_no, $vehicle_no, $send_date, $stored_remark, $USER_ID ]); $upd = $pdo->prepare("UPDATE parcel_stock_in SET status = 'out' WHERE company_id = ? AND bill_no = ? AND status = 'in'"); $upd->execute([$COMPANY_ID, $bill_no]); $pdo->commit(); require_once __DIR__ . '/helpers/activity_helper.php'; log_activity([ 'activity_type' => 'entry', 'module_name' => 'parcel_send_simple', 'action_name' => 'add_save', 'activity_note' => "Bill: $bill_no, Vehicle: $vehicle_no, Send Date: $send_date, Remark: $stored_remark", 'reference_id' => null, ]); $msg = "Parcel sent successfully for Bill #$bill_no."; // Success ke baad hum $selected_vehicle aur $selected_date ko reset NAHI kar rahe hain. // Lekin agar remark reset karna ho toh yahan kar sakte hain: unset($_POST['remark']); } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $err = "Database Error: " . $e->getMessage(); } } } /* ---------- 4. FETCH DATA ---------- */ // detect if parcel_stock_in has a transport column (try 'transport' then 'transporter') $hasTransport = $pdo->query("SHOW COLUMNS FROM parcel_stock_in LIKE 'transport'")->rowCount() > 0; $hasTransport2 = !$hasTransport && $pdo->query("SHOW COLUMNS FROM parcel_stock_in LIKE 'transporter'")->rowCount() > 0; if ($hasTransport) { $sqlBills = "SELECT DISTINCT bill_no, transport FROM parcel_stock_in WHERE company_id = ? AND status = 'in' AND (bill_date BETWEEN ? AND ?) ORDER BY CAST(bill_no AS UNSIGNED) ASC"; $stB = $pdo->prepare($sqlBills); $stB->execute([$COMPANY_ID, $from_date, $to_date]); $bills = $stB->fetchAll(PDO::FETCH_ASSOC); } elseif ($hasTransport2) { $sqlBills = "SELECT DISTINCT bill_no, transporter FROM parcel_stock_in WHERE company_id = ? AND status = 'in' AND (bill_date BETWEEN ? AND ?) ORDER BY CAST(bill_no AS UNSIGNED) ASC"; $stB = $pdo->prepare($sqlBills); $stB->execute([$COMPANY_ID, $from_date, $to_date]); $bills = $stB->fetchAll(PDO::FETCH_ASSOC); } else { $sqlBills = "SELECT DISTINCT bill_no FROM parcel_stock_in WHERE company_id = ? AND status = 'in' AND (bill_date BETWEEN ? AND ?) ORDER BY CAST(bill_no AS UNSIGNED) ASC"; $stB = $pdo->prepare($sqlBills); $stB->execute([$COMPANY_ID, $from_date, $to_date]); $bills = $stB->fetchAll(PDO::FETCH_ASSOC); } $stV = $pdo->prepare("SELECT vehicle_no FROM vehicle_data WHERE company_id = ? AND is_active = 1 ORDER BY vehicle_no"); $stV->execute([$COMPANY_ID]); $vehicles = $stV->fetchAll(PDO::FETCH_ASSOC); require_once __DIR__ . '/partials/header.php'; ?> <div class="container mt-4"> <div class="d-flex justify-content-between align-items-center mb-4"> <h3 class="fw-bold"><i class="bi bi-box-seam me-2"></i>Parcel Send (Simple)</h3> <a href="parcel_send_entry_add.php" class="btn btn-sm btn-outline-primary">Switch to Searchable</a> </div> <div class="card mb-3 bg-light border-0 shadow-sm"> <div class="card-body"> <form method="get" class="row g-2 align-items-end"> <div class="col-md-3"> <label class="small fw-bold text-secondary">From Bill Date</label> <input type="date" name="from_date" class="form-control" value="<?= h($from_date) ?>"> </div> <div class="col-md-3"> <label class="small fw-bold text-secondary">To Bill Date</label> <input type="date" name="to_date" class="form-control" value="<?= h($to_date) ?>"> </div> <div class="col-md-2"> <button type="submit" class="btn btn-dark w-100">Filter Bills</button> </div> <div class="col-md-4 text-end"> <span class="small text-muted">Filtered: <?= count($bills) ?> Bills</span> </div> </form> </div> </div> <?php if ($msg): ?> <div class="alert alert-success alert-dismissible fade show"><?= $msg ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div> <?php endif; ?> <?php if ($err): ?> <div class="alert alert-danger alert-dismissible fade show"><?= h($err) ?><button type="button" class="btn-close" data-bs-dismiss="alert"></button></div> <?php endif; ?> <div class="card shadow-sm border-0"> <div class="card-body p-4"> <form method="post" class="row g-3"> <div class="col-md-3"> <label class="form-label fw-bold text-danger">Select Bill No</label> <select name="bill_no" class="form-select border-danger" required> <option value="">-- Choose Bill --</option> <?php foreach ($bills as $b): $display = h($b['bill_no']); if (!empty($b['transport'])) $display .= ' — ' . h($b['transport']); elseif (!empty($b['transporter'])) $display .= ' — ' . h($b['transporter']); ?> <option value="<?= h($b['bill_no']) ?>"><?= $display ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label fw-bold">Select Vehicle</label> <select name="vehicle_no" class="form-select" required> <option value="">-- Choose Vehicle --</option> <?php foreach ($vehicles as $v): ?> <option value="<?= h($v['vehicle_no']) ?>" <?= ($selected_vehicle == $v['vehicle_no']) ? 'selected' : '' ?>> <?= h($v['vehicle_no']) ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label fw-bold">Dispatch Date</label> <input type="date" name="send_date" class="form-control" value="<?= h($selected_date) ?>" required> </div> <div class="col-md-3"> <label class="form-label fw-bold">Remark</label> <input type="text" name="remark" class="form-control" placeholder="Optional" value="<?= h($_POST['remark'] ?? '') ?>"> </div> <div class="col-md-6"> <label class="form-label fw-bold">Send From Branch / Godown</label> <input type="text" name="send_from_location" class="form-control" placeholder="e.g. Surat Branch / Main Godown" value="<?= h($selected_send_from) ?>"> </div> <div class="col-12 mt-4"> <button type="submit" name="save_entry" class="btn btn-primary w-100 fw-bold py-2">SAVE DISPATCH ENTRY</button> </div> </form> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>