:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/kreative_crm/quatation/**
:: Editing File: packaging_stock_edit.php
<?php ob_start(); error_reporting(E_ALL); ini_set('display_errors', 1); include_once("session.php"); include("db.php"); include("header.php"); $user_id = $_SESSION['user_id']; /* ---------------- VALIDATE ID ---------------- */ $id = isset($_GET['id']) ? (int)$_GET['id'] : 0; if ($id <= 0) { header("Location: packaging_stock_list.php"); exit; } /* ---------------- FETCH CURRENT ROW ---------------- */ $stock = mysqli_fetch_assoc(mysqli_query($conn," SELECT * FROM packaging_stock WHERE id=$id ")); if (!$stock) { header("Location: packaging_stock_list.php"); exit; } /* ---------------- PRODUCTS ---------------- */ $product_res = mysqli_query($conn," SELECT id, product_name FROM packaging_materials WHERE status=1 AND category_id=1 ORDER BY product_name "); /* ---------------- VENDORS ---------------- */ $vendor_res = mysqli_query($conn," SELECT id, name FROM party ORDER BY name "); /* ---------------- UPDATE ---------------- */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $product_id = (int)$_POST['product_id']; $vendor_id = !empty($_POST['vendor_id']) ? (int)$_POST['vendor_id'] : NULL; $stock_type = $_POST['stock_type']; $quantity = (float)$_POST['quantity']; $stock_date = $_POST['stock_date']; $status = (int)$_POST['status']; $unit_type = mysqli_real_escape_string($conn,$_POST['unit_type']); $remark = mysqli_real_escape_string($conn,$_POST['remark']); /* ---------------- GET CURRENT AVAILABLE STOCK ---------------- */ $last = mysqli_fetch_assoc(mysqli_query($conn," SELECT available_stock FROM packaging_stock WHERE product_id=$product_id AND id < $id AND status=1 ORDER BY id DESC LIMIT 1 ")); $current_stock = $last ? (float)$last['available_stock'] : 0; /* ---------------- REVERT OLD EFFECT ---------------- */ if ($stock['stock_type'] === 'IN') { $current_stock -= (float)$stock['quantity']; } else { $current_stock += (float)$stock['quantity']; } /* ---------------- APPLY NEW EFFECT ---------------- */ if ($stock_type === 'IN') { $new_stock = $current_stock + $quantity; } else { if ($quantity > $current_stock) { $_SESSION['error'] = "❌ Insufficient stock! Available: $current_stock $unit_type"; header("Location: packaging_stock_edit.php?id=$id"); exit; } $new_stock = $current_stock - $quantity; } /* ---------------- UPDATE ---------------- */ $update = " UPDATE packaging_stock SET product_id='$product_id', vendor_id=".($vendor_id ? "'$vendor_id'" : "NULL").", stock_type='$stock_type', quantity='$quantity', available_stock='$new_stock', unit_type='$unit_type', stock_date='$stock_date', status='$status', remark='$remark', updated_by='$user_id' WHERE id='$id'"; if (mysqli_query($conn,$update)) { $_SESSION['success'] = "✅ Stock updated successfully!"; header("Location: packaging_stock_list.php"); exit; } else { $_SESSION['error'] = mysqli_error($conn); } } ?> <main> <div class="container-fluid"> <div class="row m-1"> <div class="col-12"> <h4 class="main-title">Edit Packaging Stock</h4> <ul class="app-line-breadcrumbs mb-3"> <li><a href="dashboard.php">Home</a></li> <li><a href="packaging_stock_list.php">Packaging Stock List</a></li> <li class="active"><a href="#">Edit Packaging Stock</a></li> </ul> </div> </div> <div class="card"> <div class="card-body"> <?php if (!empty($_SESSION['success'])) { echo "<div class='alert alert-success'>{$_SESSION['success']}</div>"; unset($_SESSION['success']); } if (!empty($_SESSION['error'])) { echo "<div class='alert alert-danger'>{$_SESSION['error']}</div>"; unset($_SESSION['error']); } ?> <form method="post" class="row g-3"> <div class="col-md-3"> <label>Vendor</label> <select name="vendor_id" class="form-select"> <option value="">-- Optional --</option> <?php while ($v=mysqli_fetch_assoc($vendor_res)) { ?> <option value="<?= $v['id']; ?>" <?= $stock['vendor_id']==$v['id']?'selected':''; ?> required> <?= htmlspecialchars($v['name']); ?> </option> <?php } ?> </select> </div> <div class="col-md-3"> <label>Product *</label> <select name="product_id" id="product_id" class="form-select" required> <?php while ($p=mysqli_fetch_assoc($product_res)) { ?> <option value="<?= $p['id']; ?>" <?= $stock['product_id']==$p['id']?'selected':''; ?> required> <?= htmlspecialchars($p['product_name']); ?> </option> <?php } ?> </select> </div> <div class="col-md-3"> <label>Unit</label> <input type="text" name="unit_type" id="unit_type" value="<?= $stock['unit_type']; ?>" class="form-control" readonly> </div> <div class="col-md-3"> <label>Available Stock</label> <input type="text" id="available_stock" class="form-control" readonly> </div> <div class="col-md-3"> <label>Quantity *</label> <input type="number" step="0.001" name="quantity" value="<?= $stock['quantity']; ?>" class="form-control" required> </div> <div class="col-md-3"> <label>Stock Type</label> <select name="stock_type" class="form-select"> <option value="IN" <?= $stock['stock_type']=='IN'?'selected':''; ?>>IN</option> <option value="OUT" <?= $stock['stock_type']=='OUT'?'selected':''; ?>>OUT</option> </select> </div> <div class="col-md-3"> <label>Date</label> <input type="date" name="stock_date" value="<?= $stock['stock_date']; ?>" class="form-control"> </div> <div class="col-md-3"> <label>Status</label> <select name="status" class="form-select"> <option value="1" <?= $stock['status']==1?'selected':''; ?>>Active</option> <option value="0" <?= $stock['status']==0?'selected':''; ?>>Inactive</option> </select> </div> <div class="col-12"> <label>Remark</label> <textarea name="remark" class="form-control"><?= htmlspecialchars($stock['remark']); ?></textarea> </div> <div class="col-12"> <button class="btn btn-primary">Update</button> <a href="packaging_stock_list.php" class="btn btn-secondary">Back</a> </div> </form> </div> </main> <?php include("footer.php"); ?> <script> document.addEventListener('DOMContentLoaded', function () { fetch('ajax_get_product_stock.php?product_id=<?= $stock['product_id']; ?>') .then(res => res.json()) .then(data => { if (data.status === 'success') { document.getElementById('available_stock').value = data.stock + ' ' + data.unit; } }); }); </script> <?php ob_end_flush(); ?>