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_delete_invoice.php
<?php
require_once 'db.php';

header('Content-Type: application/json');

if ($con->connect_error) {
    echo json_encode(['success' => false, 'message' => "Connection failed: " . $con->connect_error]);
    exit;
}

if (isset($_POST['action']) && $_POST['action'] === "delete" && isset($_POST['invoice_number'])) {
    $invoice_number = intval($_POST['invoice_number']);

    if ($invoice_number <= 0) {
        echo json_encode(['success' => false, 'message' => 'Invalid invoice number.']);
        exit;
    }

    $response = ["success" => false, "message" => ""];

    // Start transaction
    $con->begin_transaction();

    try {
        // Get "inid" from invoices table using the invoice_number
        $fetchInidQuery = "SELECT inid FROM invoices WHERE invoice_number = $invoice_number";
        $result = $con->query($fetchInidQuery);

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

            // Delete from invoice_products using inid
            $deleteInvoiceProducts = "DELETE FROM invoice_products WHERE invoice_id = $inid";
            if (!$con->query($deleteInvoiceProducts)) {
                throw new Exception("Error deleting invoice products: " . $con->error);
            }
            // Delete Customer details 
            $deleteCustomers = "DELETE FROM invoice_customerdetails WHERE invoice_cid = $inid";
            if (!$con->query($deleteCustomers)) {
                throw new Exception("Error deleting invoice Customers: " . $con->error);
            }

            // Delete from invoices
            $deleteInvoice = "DELETE FROM invoices WHERE invoice_number = $invoice_number";
            if (!$con->query($deleteInvoice)) {
                throw new Exception("Error deleting invoice: " . $con->error);
            }

            // Commit transaction
            $con->commit();
            $response["success"] = true;
            $response["message"] = "Invoice and related products deleted successfully.";
        } else {
            throw new Exception("Invoice not found.");
        }
    } catch (Exception $e) {
        // Rollback transaction if an error occurs
        $con->rollback();
        $response["message"] = $e->getMessage();
    }

    $con->close();
    echo json_encode($response);
    exit;
}

echo json_encode(['success' => false, 'message' => 'Invalid request.']);
exit;
?>

MMCT - 2023