« Back to History
stock_in.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php declare(strict_types=1); require_once __DIR__ . '/auth.php'; require_login(); require_once __DIR__ . '/partials/header.php'; $SHOP_ID = (int)$_SESSION['owner']['shop_id']; $error = ''; $success = ''; /* ====================== SYSTEM CONFIG ====================== */ $cfgStmt = $pdo->prepare(" SELECT profit_tracking_enabled FROM system_config WHERE shop_id = ? "); $cfgStmt->execute([$SHOP_ID]); $cfg = $cfgStmt->fetch(); $PROFIT_ON = $cfg && (int)$cfg['profit_tracking_enabled'] === 1; /* ====================== FETCH CATEGORY ====================== */ $catStmt = $pdo->prepare(" SELECT category_id, category_name, has_subtype FROM category WHERE shop_id=? AND is_active=1 ORDER BY category_name "); $catStmt->execute([$SHOP_ID]); $categories = $catStmt->fetchAll(); /* ====================== FETCH BRAND ====================== */ $brandStmt = $pdo->prepare(" SELECT brand_id, brand_name FROM brand WHERE shop_id=? AND is_active=1 ORDER BY brand_name "); $brandStmt->execute([$SHOP_ID]); $brands = $brandStmt->fetchAll(); /* ====================== FETCH MODEL ====================== */ $modelStmt = $pdo->prepare(" SELECT model_id, model_name, brand_id FROM model WHERE shop_id=? AND is_active=1 ORDER BY model_name "); $modelStmt->execute([$SHOP_ID]); $models = $modelStmt->fetchAll(); /* ====================== FETCH SUBTYPE ====================== */ $subtypes = $pdo->query(" SELECT subtype_id, subtype_name, category_id FROM subtype ")->fetchAll(); /* ====================== HANDLE SUBMIT ====================== */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $category_id = (int)($_POST['category_id'] ?? 0); $brand_id = (int)($_POST['brand_id'] ?? 0); $model_id = (int)($_POST['model_id'] ?? 0); $subtype_id = (int)($_POST['subtype_id'] ?? 0); $qty = (int)($_POST['quantity'] ?? 0); $buy_price = null; if ($PROFIT_ON) { $buy_price = (float)($_POST['buy_price'] ?? 0); } if ($category_id <= 0 || $brand_id <= 0 || $model_id <= 0 || $qty <= 0) { $error = 'All required fields must be filled.'; } if (!$error && $PROFIT_ON && $buy_price <= 0) { $error = 'Buy price is required when profit tracking is enabled.'; } if (!$error) { $catChk = $pdo->prepare(" SELECT has_subtype FROM category WHERE category_id=? AND shop_id=? "); $catChk->execute([$category_id, $SHOP_ID]); $cat = $catChk->fetch(); if ($cat && (int)$cat['has_subtype'] === 1 && $subtype_id <= 0) { $error = 'Subtype (Front / Back) is required for Camera.'; } } if (!$error) { try { $pdo->beginTransaction(); /* ---------- INVENTORY ---------- */ $chk = $pdo->prepare(" SELECT stock_id FROM inventory_stock WHERE shop_id=? AND category_id=? AND brand_id=? AND model_id=? AND (subtype_id <=> ?) "); $chk->execute([ $SHOP_ID, $category_id, $brand_id, $model_id, $subtype_id ?: null ]); $row = $chk->fetch(); if ($row) { $pdo->prepare(" UPDATE inventory_stock SET quantity = quantity + ? WHERE stock_id = ? ")->execute([$qty, $row['stock_id']]); } else { $pdo->prepare(" INSERT INTO inventory_stock (shop_id, category_id, brand_id, model_id, subtype_id, quantity) VALUES (?, ?, ?, ?, ?, ?) ")->execute([ $SHOP_ID, $category_id, $brand_id, $model_id, $subtype_id ?: null, $qty ]); } /* ---------- STOCK MOVEMENT ---------- */ $pdo->prepare(" INSERT INTO stock_movement (shop_id, category_id, brand_id, model_id, subtype_id, movement_type, quantity, price) VALUES (?, ?, ?, ?, ?, 'IN', ?, ?) ")->execute([ $SHOP_ID, $category_id, $brand_id, $model_id, $subtype_id ?: null, $qty, $PROFIT_ON ? $buy_price : null ]); $pdo->commit(); $success = 'Stock added successfully.'; } catch (Throwable $e) { $pdo->rollBack(); $error = 'Stock add failed.'; } } } ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Stock IN</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="assets/css/main.css"> </head> <body class="font-md"> <div class="header"> <h2>Stock IN</h2> </div> <div class="container"> <div class="card"> <?php if ($error): ?><div class="alert alert-danger"><?=h($error)?></div><?php endif; ?> <?php if ($success): ?><div class="alert alert-success"><?=h($success)?></div><?php endif; ?> <form method="post"> <div class="form-group"> <label>Category</label> <select name="category_id" id="category_id" required> <option value="">Select Category</option> <?php foreach ($categories as $c): ?> <option value="<?=$c['category_id']?>" data-has-subtype="<?=$c['has_subtype']?>"> <?=h($c['category_name'])?> </option> <?php endforeach; ?> </select> </div> <div class="form-group"> <label>Brand</label> <select name="brand_id" id="brand_id" required> <option value="">Select Brand</option> <?php foreach ($brands as $b): ?> <option value="<?=$b['brand_id']?>"><?=h($b['brand_name'])?></option> <?php endforeach; ?> </select> </div> <div class="form-group"> <label>Model</label> <select name="model_id" id="model_id" required> <option value="">Select Model</option> <?php foreach ($models as $m): ?> <option value="<?=$m['model_id']?>" data-brand="<?=$m['brand_id']?>"> <?=h($m['model_name'])?> </option> <?php endforeach; ?> </select> </div> <div class="form-group" id="subtype-group"> <label>Subtype (Camera)</label> <select name="subtype_id" id="subtype_id"> <option value="">Select Subtype</option> <?php foreach ($subtypes as $s): ?> <option value="<?=$s['subtype_id']?>" data-category="<?=$s['category_id']?>"> <?=h($s['subtype_name'])?> </option> <?php endforeach; ?> </select> </div> <div class="form-group"> <label>Quantity</label> <input type="number" name="quantity" min="1" required> </div> <?php if ($PROFIT_ON): ?> <div class="form-group"> <label>Buy Price (per piece)</label> <input type="number" name="buy_price" step="0.01" min="0.01" required> </div> <?php endif; ?> <button class="btn btn-primary">Add Stock</button> </form> </div> </div> <script> const brandSel = document.getElementById('brand_id'); const modelSel = document.getElementById('model_id'); const catSel = document.getElementById('category_id'); const subGrp = document.getElementById('subtype-group'); const subSel = document.getElementById('subtype_id'); function updateModels(){ const bid = brandSel.value; modelSel.value = ''; Array.from(modelSel.options).forEach(o=>{ if(!o.value) return; o.hidden = o.dataset.brand !== bid; }); } function updateSubtype(){ const opt = catSel.options[catSel.selectedIndex]; const has = opt && opt.dataset.hasSubtype === '1'; const cid = catSel.value; subGrp.style.display = has ? 'block' : 'none'; Array.from(subSel.options).forEach(o=>{ if(!o.value) return; o.hidden = o.dataset.category !== cid; if(o.hidden) o.selected = false; }); } brandSel.addEventListener('change', updateModels); catSel.addEventListener('change', updateSubtype); updateModels(); updateSubtype(); </script> </body> </html>