:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/crm/quatation/**
:: Editing File: save_outward.php
<?php error_reporting(E_ALL); ini_set('display_errors', '1'); // Includes are assumed to exist based on the inward file include_once("session.php"); include("db.php"); // Database connection $conn is assumed here // 1. Check if the request method is POST, otherwise redirect. if ($_SERVER['REQUEST_METHOD'] !== 'POST') { header('Location: add_outward.php'); exit; } // ---------- 2. Collect & Sanitize Form Data for 'outward' table ---------- $user_id = (int)$_POST['user_id']; $challan_date = mysqli_real_escape_string($conn, $_POST['challan_date']); $challan_no = mysqli_real_escape_string($conn, $_POST['challan_no']); // BARJEEL Challan No //$sales_billno = mysqli_real_escape_string($conn, $_POST['sales_billno']); // PAN EXPORTS Bill No $vendor_id = (int)$_POST['vendor_id']; $vendor_code = mysqli_real_escape_string($conn, $_POST['vendor_code']); $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']; $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 = $_POST['dispatch_time']; // ---------- Time Format ---------- $date_obj = new DateTime($dispatch_time); $dispatch_time_for_db = $date_obj->format('Y-m-d H:i:s'); $dispatch_time_for_db = ($dispatch_time_for_db === null) ? '' : $dispatch_time_for_db; $cold_storage_id = (int)$_POST['cold_storage_id']; // ---------- 3. Handle File Upload ---------- $receipt_filename = null; if (isset($_FILES['outward_receipt']) && $_FILES['outward_receipt']['error'] == UPLOAD_ERR_OK) { $target_dir = "uploads/outward_receipts/"; // Ensure this folder exists and is writable // Create directory if it doesn't exist if (!is_dir($target_dir)) { mkdir($target_dir, 0755, true); } // Sanitize filename and create a unique name to prevent overwrites $safe_basename = preg_replace("/[^a-zA-Z0-9\._-]/", "", basename($_FILES["outward_receipt"]["name"])); $filename = time() . "_" . $safe_basename; $target_filepath = $target_dir . $filename; // Move the file to the target directory if (move_uploaded_file($_FILES["outward_receipt"]["tmp_name"], $target_filepath)) { $receipt_filename = $filename; } else { $_SESSION['error'] = "Sorry, there was an error uploading your file."; header("Location: add_outward.php"); exit; } } // ---------- 4. Start Database Transaction ---------- mysqli_begin_transaction($conn); try { // --- Generate a new outward_no (e.g., max + 1) --- /*$result_max_no = mysqli_query($conn, "SELECT MAX(outward_no) as max_no FROM outward"); $row_max = mysqli_fetch_assoc($result_max_no); $new_outward_no = ($row_max['max_no'] ?? 0) + 1;*/ // ---------- 5. Insert into the main 'outward' table ---------- // NOTE: Added challan_no and sales_billno columns to the query as they are in the form. // You may need to add these columns to your `outward` table structure. // ALTER TABLE `outward` ADD `challan_no` VARCHAR(100) NULL AFTER `outward_no`, ADD `sales_billno` VARCHAR(100) NULL AFTER `challan_no`; $sql_outward = " INSERT INTO outward (outward_date, outward_no, vendor_id, vendor_code, product_name, brand_desc, cold_storage_id, vehicle_number, driver_number, dispatch_time, total_box_count, supervisior_name, outward_receipt, user_id, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) "; $stmt_outward = mysqli_prepare($conn, $sql_outward); mysqli_stmt_bind_param( $stmt_outward, 'ssississsssssi', $challan_date, $challan_no, $vendor_id, $vendor_code, $product_name, $brand_desc, $cold_storage_id, $vehicle_number, $driver_number, $dispatch_time, $total_box_count, $supervisior_name, $target_filepath, $user_id ); if (!mysqli_stmt_execute($stmt_outward)) { throw new Exception("Error saving outward record: " . mysqli_stmt_error($stmt_outward)); } // Get the ID of the newly inserted outward record $outward_id = mysqli_insert_id($conn); // ---------- 6. Collect & Insert into 'outward_box' table ---------- // These keys come from the dynamically generated inputs in your form $box_4H = isset($_POST['box_4H']) ? (int)$_POST['box_4H'] : 0; $box_5H = isset($_POST['box_5H']) ? (int)$_POST['box_5H'] : 0; $box_6H = isset($_POST['box_6H']) ? (int)$_POST['box_6H'] : 0; $box_7H = isset($_POST['box_7H']) ? (int)$_POST['box_7H'] : 0; $box_8H = isset($_POST['box_8H']) ? (int)$_POST['box_8H'] : 0; $box_9H = isset($_POST['box_9H']) ? (int)$_POST['box_9H'] : 0; $box_10H = isset($_POST['box_10H']) ? (int)$_POST['box_10H'] : 0; $box_CL = isset($_POST['box_CL']) ? (int)$_POST['box_CL'] : 0; $sql_box = " INSERT INTO outward_box (challan_id, box_4H, box_5H, box_6H, box_7H, box_8H, box_9H, box_10H, box_CL, total_box) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "; $stmt_box = mysqli_prepare($conn, $sql_box); mysqli_stmt_bind_param( $stmt_box, 'iiiiiiiiii', $outward_id, $box_4H, $box_5H, $box_6H, $box_7H, $box_8H, $box_9H, $box_10H, $box_CL, $total_box_count ); if (!mysqli_stmt_execute($stmt_box)) { throw new Exception("Error saving box details: " . mysqli_stmt_error($stmt_box)); } // ---------- 7. Commit the Transaction ---------- mysqli_commit($conn); $_SESSION['success'] = "Outward Bill #{$new_outward_no} has been saved successfully!"; header("Location: outward_list.php"); // Redirect to the listing page exit; } catch (Exception $e) { // ---------- 8. Rollback in case of any error ---------- mysqli_rollback($conn); // Also, delete the uploaded file if the transaction failed if ($receipt_filename && file_exists("uploads/outward_receipts/" . $receipt_filename)) { unlink("uploads/outward_receipts/" . $receipt_filename); } $_SESSION['error'] = "An error occurred: " . $e->getMessage(); header("Location: add_outward.php"); // Redirect back to the form exit; } ?>