:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/crm/quatation/**
:: Editing File: save_inward.php
<?php // Include necessary files include_once("session.php"); include("db.php"); // Assumed to contain $conn for mysqli connection // Check if the form was submitted via POST if ($_SERVER["REQUEST_METHOD"] != "POST") { $_SESSION['error'] = "Invalid request method."; header("Location: add_inward.php"); exit(); } // ----------------------------------------------------------- // 1. DATA VALIDATION AND SANITIZATION // ----------------------------------------------------------- // Basic required fields $required_fields = ['inward_date', 'vendor_id', 'brand_desc', 'total_box_count', 'cold_storage_id']; foreach ($required_fields as $field) { if (!isset($_POST[$field]) || empty($_POST[$field])) { $_SESSION['error'] = "Missing required field: " . str_replace('_', ' ', $field); header("Location: add_inward.php"); exit(); } } // Sanitize and assign variables for the 'inward' table $inward_date = mysqli_real_escape_string($conn, $_POST['inward_date']); $inward_no = mysqli_real_escape_string($conn, $_POST['inward_no'] ?? ''); $billno = mysqli_real_escape_string($conn, $_POST['billno'] ?? ''); $product_name = mysqli_real_escape_string($conn, $_POST['product_name']); $brand_desc = mysqli_real_escape_string($conn, $_POST['brand_desc']); $total_box_count = (int) $_POST['total_box_count']; $vendor_id = (int) $_POST['vendor_id']; $cold_storage_id = (int) $_POST['cold_storage_id']; $user_id = (int) $_SESSION['user_id']; // Logistical/Driver details (NEW FIELDS) $supervisior_name = mysqli_real_escape_string($conn, $_POST['supervisior_name'] ?? ''); $vehicle_number = mysqli_real_escape_string($conn, $_POST['vehicle_number'] ?? ''); $driver_number = mysqli_real_escape_string($conn, $_POST['driver_number'] ?? ''); $dispatch_time = mysqli_real_escape_string($conn, $_POST['dispatch_time'] ?? ''); // DATETIME or NULL // ----------------------------------------------------------- // 2. FILE UPLOAD HANDLING // ----------------------------------------------------------- $target_dir = "uploads/inward_receipts/"; $inward_receipt_path = null; if (isset($_FILES["inward_receipt"]) && $_FILES["inward_receipt"]["error"] == 0) { $file_name = basename($_FILES["inward_receipt"]["name"]); $target_file = $target_dir . time() . "_" . $file_name; $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); if ($_FILES["inward_receipt"]["size"] > 5000000) { $uploadOk = 0; } if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "pdf") { $uploadOk = 0; } if ($uploadOk == 0) { $_SESSION['error'] = $_SESSION['error'] ?? "File upload failed due to size/type."; header("Location: add_inward.php"); exit(); } else { if (move_uploaded_file($_FILES["inward_receipt"]["tmp_name"], $target_file)) { $inward_receipt_path = $target_file; } else { $_SESSION['error'] = "Sorry, there was an error uploading your file."; header("Location: add_inward.php"); exit(); } } } // ----------------------------------------------------------- // 3. DATABASE TRANSACTION // ----------------------------------------------------------- mysqli_begin_transaction($conn); $success = true; try { // --- STEP 3A: INSERT INTO `inward` (Master Record) --- $sql_inward = "INSERT INTO inward (vendor_id, inward_date, inward_no, billno, brand_desc, total_box_count, cold_storage_id, inward_receipt, user_id, supervisior_name, vehicle_number, driver_number, dispatch_time, product_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $stmt_inward = mysqli_prepare($conn, $sql_inward); if (!$stmt_inward) throw new Exception("Inward statement preparation failed: " . mysqli_error($conn)); // Bind parameters: i s s s s i s s s s s s mysqli_stmt_bind_param($stmt_inward, "issssissssssss", $vendor_id, $inward_date, $inward_no, $billno, $brand_desc, $total_box_count, $cold_storage_id, $inward_receipt_path, $user_id, $supervisior_name, $vehicle_number, $driver_number, $dispatch_time,$product_name ); if (!mysqli_stmt_execute($stmt_inward)) throw new Exception("Inward execution failed: " . mysqli_stmt_error($stmt_inward)); $inward_id = mysqli_insert_id($conn); mysqli_stmt_close($stmt_inward); // --- STEP 3B: INSERT INTO `inward_box` (Category Breakdown) --- // (Logic from previous step remains the same for dynamic box category handling) $box_categories = []; $query = "SELECT category_name FROM box_category WHERE status = 1 ORDER BY id ASC"; $result = mysqli_query($conn, $query); if ($result) { while ($row = mysqli_fetch_assoc($result)) { $category_name = htmlspecialchars($row['category_name']); $input_key = 'box_' . str_replace([' ', '/', '-'], '', $category_name); $box_categories[$input_key] = (int) ($_POST[$input_key] ?? 0); } } $cols = implode(", ", array_keys($box_categories)); $values = implode(", ", array_fill(0, count($box_categories), '?')); $types = str_repeat('i', count($box_categories)); $sql_box = "INSERT INTO inward_box (inward_id, total_box, $cols) VALUES (?, ?, $values)"; $stmt_box = mysqli_prepare($conn, $sql_box); if (!$stmt_box) throw new Exception("Box statement preparation failed: " . mysqli_error($conn)); $bind_params = array_merge([$inward_id, $total_box_count], array_values($box_categories)); $bind_types = 'ii' . $types; $refs = []; foreach ($bind_params as $key => $value) { $refs[$key] = &$bind_params[$key]; } array_unshift($refs, $bind_types); if (!call_user_func_array('mysqli_stmt_bind_param', array_merge([$stmt_box], $refs))) { throw new Exception("Box binding failed."); } if (!mysqli_stmt_execute($stmt_box)) throw new Exception("Box execution failed: " . mysqli_stmt_error($stmt_box)); mysqli_stmt_close($stmt_box); // --- STEP 3C: INSERT INTO `inward_items` (Detailed Line Items) --- if (isset($_POST['item_desc']) && is_array($_POST['item_desc'])) { $sql_items = "INSERT INTO inward_items (inward_id, description, type_of_good, total_box, wastage_kg, actual_wastage_kg) VALUES (?, ?, ?, ?, ?, ?)"; $stmt_items = mysqli_prepare($conn, $sql_items); if (!$stmt_items) throw new Exception("Items statement preparation failed: " . mysqli_error($conn)); foreach ($_POST['item_desc'] as $key => $description) { $description = mysqli_real_escape_string($conn, $description); $type_of_good = mysqli_real_escape_string($conn, $_POST['item_good'][$key] ?? ''); $item_total_box = (float) ($_POST['item_total_box'][$key] ?? 0.00); $wastage_kg = (float) ($_POST['item_wastage_kg'][$key] ?? 0.00); $actual_wastage_kg= (float) ($_POST['item_actual_wastage_kg'][$key] ?? 0.00); mysqli_stmt_bind_param($stmt_items, "issddd", $inward_id, $description, $type_of_good, $item_total_box, $wastage_kg, $actual_wastage_kg); if (!mysqli_stmt_execute($stmt_items)) throw new Exception("Item execution failed for row $key: " . mysqli_stmt_error($stmt_items)); } mysqli_stmt_close($stmt_items); } // Commit Transaction mysqli_commit($conn); $_SESSION['success'] = "Inward Bill (No: $inward_no, Bill No: $billno) added successfully!"; } catch (Exception $e) { // Rollback Transaction on error mysqli_rollback($conn); if ($inward_receipt_path && file_exists($inward_receipt_path)) { unlink($inward_receipt_path); } $success = false; $_SESSION['error'] = "Failed to add Inward Bill. Error: " . $e->getMessage(); } // ----------------------------------------------------------- // 4. REDIRECT // ----------------------------------------------------------- if ($success) { header("Location: inward_list.php"); } else { header("Location: add_inward.php"); } exit(); ?>