:: **Zploit** v1.0 | Current Path: **/home/kreativepixelz/www/crm/quatation/**
:: Editing File: save_packing_material.php
<?php include_once("session.php"); include("db.php"); // Only accept POST if ($_SERVER['REQUEST_METHOD'] !== 'POST') { header('Location: add_packing_material.php'); exit; } $new_file_db_path = null; // Store the new file's path for the DB $new_file_full_path = null; // Store full server path for cleanup on error try { // ---------- 1) Collect & Sanitize Master Invoice Data ---------- $user_id = (int)($_POST['user_id'] ?? 0); $party_id = (int)($_POST['party_id'] ?? 0); $invoice_number = trim($_POST['invoice_number'] ?? ''); $invoice_date = trim($_POST['invoice_date'] ?? ''); $mode_of_payment = trim($_POST['mode_of_payment'] ?? 'CREDIT'); // These values are calculated on the client-side, but we re-verify and use them $total_taxable_amount = (float)($_POST['total_taxable_amount'] ?? 0.0); $total_sgst_amount = (float)($_POST['total_sgst_amount'] ?? 0.0); $total_cgst_amount = (float)($_POST['total_cgst_amount'] ?? 0.0); $total_igst_amount = (float)($_POST['total_igst_amount'] ?? 0.0); // Assuming 0 for now $total_tax_amount = (float)($_POST['total_tax_amount'] ?? 0.0); $round_off = (float)($_POST['round_off'] ?? 0.0); $total_amount_after_tax = (float)($_POST['total_amount_after_tax'] ?? 0.0); $seller_name = trim($_POST['seller_name'] ?? ''); $seller_gstin = trim($_POST['seller_gstin'] ?? ''); $seller_contact = trim($_POST['seller_contact'] ?? ''); $seller_address = trim($_POST['seller_address'] ?? ''); // Get item array $items = $_POST['item'] ?? []; // Basic Validation if (empty($invoice_number)) throw new Exception("Invoice Number is required."); if (empty($invoice_date)) throw new Exception("Invoice Date is required."); if ($party_id <= 0) throw new Exception("A Receiver Party must be selected."); if (empty($items) || count($items) == 0) throw new Exception("At least one item is required."); // ---------- 2) File Upload Logic ---------- $upload_dir = __DIR__ . '/uploads/packing_matrials_recepit/'; // Your path from the list page if (isset($_FILES['packing_material_receipt']) && $_FILES['packing_material_receipt']['error'] === UPLOAD_ERR_OK) { if (!is_dir($upload_dir) && !mkdir($upload_dir, 0777, true) && !is_dir($upload_dir)) { throw new Exception("Failed to create upload directory."); } $orig_name = basename($_FILES['packing_material_receipt']['name']); $ext = strtolower(pathinfo($orig_name, PATHINFO_EXTENSION)); $allowed = ['pdf', 'jpg', 'jpeg', 'png']; if (!in_array($ext, $allowed)) { throw new Exception("Invalid file type. Allowed: PDF, JPG, PNG."); } // Create a safe, unique filename $safe_invoice_no = preg_replace('/[^A-Za-z0-9_\-]/', '_', $invoice_number); $new_filename = "pm_receipt_" . $safe_invoice_no . "_" . time() . "." . $ext; $new_file_full_path = $upload_dir . $new_filename; $new_file_db_path = $new_filename; // Only store the filename in the DB if (!move_uploaded_file($_FILES['packing_material_receipt']['tmp_name'], $new_file_full_path)) { throw new Exception("Failed to move uploaded file."); } } // ---------- 3) Database Transaction ---------- mysqli_begin_transaction($conn); // Insert into master table 'packing_materials' // ** NOTE: This assumes default values for seller_*, bank_* fields as they are not in your form $sql_master = "INSERT INTO packing_materials (invoice_number, invoice_date, mode_of_payment, party_id, total_taxable_amount, total_sgst_amount, total_cgst_amount, total_igst_amount, total_tax_amount, round_off, total_amount_after_tax, packing_material_receipt, seller_name,seller_gstin,seller_contact,seller_address, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())"; $stmt_master = mysqli_prepare($conn, $sql_master); if (!$stmt_master) throw new Exception("Master prepare failed: " . mysqli_error($conn)); mysqli_stmt_bind_param( $stmt_master, "sssidddddddssssss", $invoice_number, $invoice_date, $mode_of_payment, $party_id, $total_taxable_amount, $total_sgst_amount, $total_cgst_amount, $total_igst_amount, $total_tax_amount, $round_off, $total_amount_after_tax, $new_file_db_path, // This will be NULL if no file was uploaded $seller_name, $seller_gstin, $seller_contact, $seller_address ); if (!mysqli_stmt_execute($stmt_master)) { throw new Exception("Master execute failed: " . mysqli_stmt_error($stmt_master)); } // Get the ID of the invoice we just inserted $invoice_id = mysqli_insert_id($conn); mysqli_stmt_close($stmt_master); if ($invoice_id <= 0) { throw new Exception("Failed to get new invoice ID."); } // ---------- 4) Loop and Insert Items ---------- $sql_item = "INSERT INTO packing_material_items (invoice_id, item_description, hsn_code, unit_price, quantity, taxable_amount, sgst_rate, sgst_amount, cgst_rate, cgst_amount, total_amount) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $stmt_item = mysqli_prepare($conn, $sql_item); if (!$stmt_item) throw new Exception("Item prepare failed: " . mysqli_error($conn)); foreach ($items as $item) { $desc = trim($item['description'] ?? ''); if (empty($desc)) continue; // Skip empty rows $hsn = trim($item['hsn_code'] ?? ''); $price = (float)($item['unit_price'] ?? 0.0); $qty = (float)($item['quantity'] ?? 0.0); $taxable_amt = (float)($item['taxable_amount'] ?? 0.0); $sgst_rate = (float)($item['sgst_rate'] ?? 0.0); $sgst_amt = (float)($item['sgst_amount'] ?? 0.0); $cgst_rate = (float)($item['cgst_rate'] ?? 0.0); $cgst_amt = (float)($item['cgst_amount'] ?? 0.0); $total_amt = (float)($item['total_amount'] ?? 0.0); mysqli_stmt_bind_param( $stmt_item, "issdddddddd", $invoice_id, $desc, $hsn, $price, $qty, $taxable_amt, $sgst_rate, $sgst_amt, $cgst_rate, $cgst_amt, $total_amt ); if (!mysqli_stmt_execute($stmt_item)) { throw new Exception("Item execute failed: " . mysqli_stmt_error($stmt_item)); } } mysqli_stmt_close($stmt_item); // If all successful, commit the transaction mysqli_commit($conn); $_SESSION['success'] = "Packing Material Invoice (ID: $invoice_id) saved successfully."; header("Location: packing_material_list.php"); exit; } catch (Exception $e) { // Something went wrong, rollback the transaction mysqli_rollback($conn); // If a new file was uploaded, delete it if ($new_file_full_path && file_exists($new_file_full_path)) { @unlink($new_file_full_path); } // Send error message back to the add form $_SESSION['error'] = "Error: " . $e->getMessage(); header("Location: add_packing_material.php"); exit; } ?>