:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/kreative_crm/quatation/**
:: Editing File: update_stock.php
<?php error_reporting(E_ALL); ini_set('display_errors', 1); include_once("session.php"); include("db.php"); if ($_SERVER['REQUEST_METHOD'] !== 'POST') { $_SESSION['error'] = "Invalid request."; header("Location: stock_list.php"); exit; } $movement_id = (int)$_POST['movement_id']; $movement_date = $_POST['movement_date']; $movement_type = $_POST['movement_type']; $vendor_id = !empty($_POST['vendor_id']) ? (int)$_POST['vendor_id'] : null; $cold_storage_id = !empty($_POST['cold_storage_id']) ? (int)$_POST['cold_storage_id'] : null; $product_name = $_POST['product_name'] ?? ''; $brand_desc = $_POST['brand_desc'] ?? ''; $remark = $_POST['remark'] ?? ''; // --- 1. GET BOX CATEGORIES --- $categories = []; $q = mysqli_query($conn, "SELECT category_name FROM box_category WHERE status = 1 ORDER BY id ASC"); while ($r = mysqli_fetch_assoc($q)) { $cat = $r['category_name']; $key = "box_" . preg_replace("/[^A-Za-z0-9]/", "", $cat); $box_count = isset($_POST[$key]) ? (int)$_POST[$key] : 0; $categories[] = [ 'category_name' => $cat, 'box_count' => $box_count ]; } // Calculate total $total_box = array_sum(array_column($categories, 'box_count')); // --- 2. START TRANSACTION --- mysqli_begin_transaction($conn); try { // --- 3. UPDATE MASTER RECORD --- $sql = "UPDATE stock_movements SET movement_date = ?, movement_type = ?, vendor_id = ?, cold_storage_id = ?, product_name = ?, brand_desc = ?, remark = ?, total_box = ?, updated_at = NOW() WHERE id = ? "; $stmt = mysqli_prepare($conn, $sql); mysqli_stmt_bind_param( $stmt, "ssiisssii", $movement_date, $movement_type, $vendor_id, $cold_storage_id, $product_name, $brand_desc, $remark, $total_box, $movement_id ); if (!mysqli_stmt_execute($stmt)) { throw new Exception("Master update failed: " . mysqli_error($conn)); } mysqli_stmt_close($stmt); // --- 4. DELETE OLD BOX DATA --- $del = mysqli_prepare($conn, "DELETE FROM stock_movement_boxes WHERE movement_id = ?"); mysqli_stmt_bind_param($del, "i", $movement_id); if (!mysqli_stmt_execute($del)) { throw new Exception("Delete old box data failed."); } mysqli_stmt_close($del); // --- 5. INSERT NEW BOX ROWS --- $ins = mysqli_prepare($conn, "INSERT INTO stock_movement_boxes (movement_id, category_name, box_count) VALUES (?, ?, ?)" ); foreach ($categories as $c) { mysqli_stmt_bind_param($ins, "isi", $movement_id, $c['category_name'], $c['box_count']); if (!mysqli_stmt_execute($ins)) { throw new Exception("Box insert failed: " . mysqli_error($conn)); } } mysqli_stmt_close($ins); // --- 6. COMMIT --- mysqli_commit($conn); $_SESSION['success'] = "Stock movement updated successfully."; header("Location: stock_list.php"); exit; } catch (Exception $e) { mysqli_rollback($conn); $_SESSION['error'] = "Update failed: " . $e->getMessage(); header("Location: edit_stock.php?id=" . $movement_id); exit; } ?>