« Back to History
packing_opening_stock.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('packing_opening_stock'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $selectedMonth = month_value_for_input($_GET['month'] ?? date('Y-m')); function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function normalize_month_for_storage(string $monthValue): string { $monthValue = trim($monthValue); if ($monthValue === '') { return ''; } if (preg_match('/^\d{4}-\d{2}$/', $monthValue)) { return $monthValue . '-01'; } $ts = strtotime($monthValue); if ($ts === false) { return ''; } return date('Y-m-01', $ts); } function month_value_for_input(?string $monthValue): string { $monthValue = trim((string)$monthValue); if ($monthValue === '' || $monthValue === '0000-00-00') { return ''; } if (preg_match('/^\d{4}-\d{2}$/', $monthValue)) { return $monthValue; } $ts = strtotime($monthValue); if ($ts === false) { return ''; } return date('Y-m', $ts); } function month_value_for_display(?string $monthValue): string { $monthValue = trim((string)$monthValue); if ($monthValue === '' || $monthValue === '0000-00-00') { return '-'; } $ts = strtotime($monthValue); if ($ts === false) { return h($monthValue); } return date('M Y', $ts); } function get_financial_year_label(string $monthValue): string { $monthValue = normalize_month_for_storage($monthValue); if ($monthValue === '') { return ''; } $ts = strtotime($monthValue); if ($ts === false) { return ''; } $year = (int)date('Y', $ts); $month = (int)date('n', $ts); if ($month >= 4) { return $year . '-' . substr((string)($year + 1), -2); } return ($year - 1) . '-' . substr((string)$year, -2); } // Fetch material master for dropdown $stmt = $pdo->prepare("SELECT id, material_type, quality, size, ref_pcs, ref_weight, uom FROM packing_material_type WHERE company_id = ? ORDER BY material_type ASC, quality ASC, size ASC"); $stmt->execute([$company_id]); $materials = $stmt->fetchAll(PDO::FETCH_ASSOC); // CSRF if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } // ================= SAVE / UPDATE (RAW DATA ONLY) ================= if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) { die('Invalid CSRF token'); } $id = (int)($_POST['id'] ?? 0); $monthInput = $_POST['month'] ?? ''; $month = normalize_month_for_storage($monthInput); $financial_year = get_financial_year_label($month); $material_type_id = (int)($_POST['material_type_id'] ?? 0); $size = $_POST['size'] ?? ''; $opening_pcs = (float)($_POST['opening_pcs'] ?? 0); $opening_kg = (float)($_POST['opening_kg'] ?? 0); if ($month !== '' && $material_type_id > 0) { if ($id > 0) { $stmt = $pdo->prepare("UPDATE packing_material_opening_stock SET financial_year=?, month=?, material_type_id=?, size=?, opening_kg=?, opening_pcs=?, updated_at=NOW() WHERE id=? AND company_id=?"); $stmt->execute([$financial_year, $month, $material_type_id, $size, $opening_kg, $opening_pcs, $id, $company_id]); } else { $stmt = $pdo->prepare("INSERT INTO packing_material_opening_stock (company_id, financial_year, month, material_type_id, size, opening_kg, opening_pcs, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW())"); $stmt->execute([$company_id, $financial_year, $month, $material_type_id, $size, $opening_kg, $opening_pcs]); } header("Location: " . $_SERVER['PHP_SELF'] . '?month=' . rawurlencode(month_value_for_input($month))); exit; } } // ================= DELETE ================= if (isset($_GET['delete'])) { $stmt = $pdo->prepare("DELETE FROM packing_material_opening_stock WHERE id = ? AND company_id = ?"); $stmt->execute([(int)$_GET['delete'], $company_id]); header("Location: " . $_SERVER['PHP_SELF']); exit; } // ================= EDIT FETCH ================= $edit = null; if (isset($_GET['edit'])) { $stmt = $pdo->prepare("SELECT * FROM packing_material_opening_stock WHERE id = ? AND company_id = ?"); $stmt->execute([(int)$_GET['edit'], $company_id]); $edit = $stmt->fetch(PDO::FETCH_ASSOC); } // ================= LIST FETCH ================= $stmt = $pdo->prepare("SELECT s.*, m.material_type, m.simple_name, m.quality, m.ref_pcs, m.ref_weight, m.uom FROM packing_material_opening_stock s LEFT JOIN packing_material_type m ON m.id = s.material_type_id AND m.company_id = s.company_id WHERE s.company_id = ? ORDER BY s.id DESC"); $stmt->execute([$company_id]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); require_once __DIR__ . '/partials/header.php'; ?> <div class="wrap container-fluid mt-4"> <div class="card shadow-sm border-0 mb-4"> <div class="card-header bg-white py-3"> <h5 class="mb-0 fw-bold"><?= $edit ? 'Edit' : 'Add' ?> Opening Stock (Raw Entry)</h5> </div> <div class="card-body"> <form method="post" class="row g-3"> <input type="hidden" name="csrf_token" value="<?=h($_SESSION['csrf_token'])?>"> <input type="hidden" name="id" value="<?=h($edit['id'] ?? 0)?>"> <div class="col-md-2"> <label class="form-label small fw-bold">Month</label> <input type="month" name="month" class="form-control" value="<?=h($edit ? month_value_for_input($edit['month'] ?? '') : $selectedMonth)?>" required> </div> <div class="col-md-3"> <label class="form-label small fw-bold">Material</label> <select name="material_type_id" id="materialSelect" class="form-select" required> <option value="">Select Material</option> <?php foreach ($materials as $m): ?> <?php $sel = ($edit && (int)($edit['material_type_id'] ?? 0) === (int)$m['id']) ? 'selected' : ''; ?> <option data-quality="<?=h($m['quality'])?>" data-size="<?=h($m['size'])?>" data-refpcs="<?=h($m['ref_pcs'])?>" data-refweight="<?=h($m['ref_weight'])?>" data-uom="<?=h($m['uom'])?>" value="<?=h($m['id'])?>" <?= $sel ?>> <?=h($m['material_type'].' - '.$m['quality'].' - '.$m['size'])?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-2"> <label class="form-label small fw-bold text-muted">Quality</label> <input type="text" id="quality" class="form-control bg-light" value="<?=h($edit['quality'] ?? '')?>" readonly> </div> <div class="col-md-1"> <label class="form-label small fw-bold text-muted">Size</label> <input type="text" name="size" id="size" class="form-control bg-light" value="<?=h($edit['size'] ?? '')?>" readonly> </div> <div class="col-md-1"> <label class="form-label small fw-bold text-muted">Ref PCS</label> <input type="number" id="ref_pcs" class="form-control bg-light" value="<?=h($edit['ref_pcs'] ?? '')?>" readonly> </div> <div class="col-md-1"> <label class="form-label small fw-bold text-muted">Ref WT</label> <input type="number" step="0.001" id="ref_weight" class="form-control bg-light" value="<?=h($edit['ref_weight'] ?? '')?>" readonly> </div> <div class="col-md-1"> <label class="form-label small fw-bold text-muted">UOM</label> <input type="text" id="uom" class="form-control bg-light" value="<?=h($edit['uom'] ?? '')?>" readonly> </div> <div class="col-md-1"> <label class="form-label small fw-bold text-primary">Input PCS</label> <input type="number" step="0.01" name="opening_pcs" class="form-control border-primary" value="<?=h($edit['opening_pcs'] ?? '')?>"> </div> <div class="col-md-1"> <label class="form-label small fw-bold text-primary">Input KG</label> <input type="number" step="0.001" name="opening_kg" class="form-control border-primary" value="<?=h($edit['opening_kg'] ?? '')?>"> </div> <div class="col-12 mt-4 text-end"> <button type="submit" class="btn btn-primary px-5"><?= $edit ? 'Update' : 'Save Entry' ?></button> <?php if($edit): ?> <a href="<?=h($_SERVER['PHP_SELF'])?>" class="btn btn-secondary">Cancel</a> <?php endif; ?> </div> </form> </div> </div> <div class="card shadow-sm border-0"> <div class="card-body p-0"> <div class="table-responsive"> <table class="table table-hover align-middle mb-0"> <thead class="table-light"> <tr> <th class="ps-3">Month</th> <th>Financial Year</th> <th>Material / Quality</th> <th>UOM</th> <th class="text-end">Raw PCS</th> <th class="text-end">Raw KG</th> <th class="text-center">Action</th> </tr> </thead> <tbody> <?php foreach ($rows as $r): ?> <tr> <td class="ps-3"><?= month_value_for_display($r['month'] ?? '') ?></td> <td><?= h($r['financial_year']) ?></td> <td> <div class="fw-bold"><?= h($r['material_type'] ?: ('Material #' . $r['material_type_id'])) ?></div> <small class="text-muted"><?= h($r['quality']) ?> (<?= h($r['size']) ?>)</small> </td> <td><?= h($r['uom'] ?? '') ?></td> <td class="text-end"><?= $r['opening_pcs'] ?></td> <td class="text-end"><?= $r['opening_kg'] ?></td> <td class="text-center"> <a href="?edit=<?=$r['id']?>" class="btn btn-sm btn-outline-warning me-1">Edit</a> <a href="?delete=<?=$r['id']?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete?')">Delete</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> </div> <script> const materialSelect = document.getElementById('materialSelect'); if (materialSelect) { const syncMaterialMeta = () => { const opt = materialSelect.selectedOptions[0]; if (!opt) { return; } document.getElementById('quality').value = opt.dataset.quality || ''; document.getElementById('size').value = opt.dataset.size || ''; document.getElementById('ref_pcs').value = opt.dataset.refpcs || ''; document.getElementById('ref_weight').value = opt.dataset.refweight || ''; document.getElementById('uom').value = opt.dataset.uom || ''; }; materialSelect.addEventListener('change', syncMaterialMeta); syncMaterialMeta(); } </script> <?php require_once __DIR__ . '/partials/footer.php'; ?>