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/fetch_product_invoice_edit.php
<?php
error_reporting(0);
ini_set('display_errors', 0);

include 'db.php';

$response = array('success' => false);

try {
    $pid = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
    $cid = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
    $paid = isset($_GET['paid']) ? intval($_GET['paid']) : 0;
    
    if ($pid <= 0) {
        throw new Exception("Invalid product ID");
    }

    // First, check if the product exists
    $productCheck = "SELECT COUNT(*) as count FROM product WHERE pid = ?";
    $productCheckStmt = $con->prepare($productCheck);
    $productCheckStmt->bind_param("i", $pid);
    $productCheckStmt->execute();
    $productCheckResult = $productCheckStmt->get_result();
    $productExists = ($productCheckResult->fetch_assoc()['count'] > 0);
    $productCheckStmt->close();
    
    if (!$productExists) {
        throw new Exception("Product not found");
    }
    
    $useCustomerPricing = false;
    
    // Check if customer exists when cid is provided
    if ($cid > 0) {
        $customerCheck = "SELECT COUNT(*) as count FROM customers WHERE id = ?";
        $checkStmt = $con->prepare($customerCheck);
        $checkStmt->bind_param("i", $cid);
        $checkStmt->execute();
        $checkResult = $checkStmt->get_result();
        $customerExists = ($checkResult->fetch_assoc()['count'] > 0);
        $checkStmt->close();
        
        if (!$customerExists) {
            $cid = 0;
            $response['customer_not_found'] = true;
        } else {
            $checkCustomerProduct = "SELECT COUNT(*) as count FROM cus_product WHERE cid = ? AND pid = ?";
            $cpCheckStmt = $con->prepare($checkCustomerProduct);
            $cpCheckStmt->bind_param("ii", $cid, $pid);
            $cpCheckStmt->execute();
            $cpCheckResult = $cpCheckStmt->get_result();
            $customerHasProduct = ($cpCheckResult->fetch_assoc()['count'] > 0);
            $cpCheckStmt->close();
            
            $useCustomerPricing = $customerHasProduct;
            
            if (!$customerHasProduct) {
                $response['customer_product_not_found'] = true;
            }
        }
    }
    
    if ($useCustomerPricing && $paid > 0) {
        $query = "SELECT p.pid, p.pname, p.bsprice as product_price, p.paid, p.pform, p.nprice as default_new_price, p.bsprice as default_old_price,
                    cp.newprice as customer_price, cp.paid as selected_paid, cp.pform as selected_pform, cp.oldprice as customer_oprice
                FROM product p
                INNER JOIN cus_product cp ON p.pid = cp.pid AND cp.cid = ?
                INNER JOIN package pp ON pp.paid = ? AND FIND_IN_SET(pp.paid, p.paid)
                WHERE p.pid = ?";
        
        $stmt = $con->prepare($query);
        $stmt->bind_param("iii", $cid, $paid, $pid);
    } else if ($useCustomerPricing) {
        $query = "SELECT p.pid, p.pname, p.bsprice as product_price, p.paid, p.pform, p.nprice as default_new_price, p.bsprice as default_old_price,
                    cp.newprice as customer_price, cp.paid as selected_paid, cp.pform as selected_pform, cp.oldprice as customer_oprice
                FROM product p
                INNER JOIN cus_product cp ON p.pid = cp.pid AND cp.cid = ?
                WHERE p.pid = ?";
        
        $stmt = $con->prepare($query);
        $stmt->bind_param("ii", $cid, $pid);
    } else if ($paid > 0) {
        $query = "SELECT p.pid, p.pname, p.bsprice as product_price, p.paid, p.pform, p.nprice as default_new_price, p.bsprice as default_old_price
                FROM product p
                INNER JOIN package pp ON pp.paid = ? AND FIND_IN_SET(pp.paid, p.paid)
                WHERE p.pid = ?";
        
        $stmt = $con->prepare($query);
        $stmt->bind_param("ii", $paid, $pid);
    } else {
        $query = "SELECT p.pid, p.pname, p.bsprice as product_price, p.nprice as default_new_price, p.paid, p.pform, p.bsprice as default_old_price
                FROM product p 
                WHERE p.pid = ?";
        
        $stmt = $con->prepare($query);
        $stmt->bind_param("i", $pid);
    }
    
    $stmt->execute();
    $result = $stmt->get_result();
    
    if (!$result) {
        throw new Exception("Database query error: " . $con->error);
    }
    
    if ($row = $result->fetch_assoc()) {
        $response['success'] = true;
        if ($paid > 0) {
            $response['price'] = $row['default_new_price'];
            $response['oldprice'] = $row['default_old_price'];
        } else if ($useCustomerPricing && isset($row['customer_price']) && $row['customer_price'] !== null) {
            $response['price'] = $row['customer_price'];
            $response['oldprice'] = $row['customer_oprice'] ?? $row['default_old_price'];
        } else {
            $response['price'] = $row['default_new_price'];
            $response['oldprice'] = $row['default_old_price'];
        }
        $pformIds = $row['pform'];
        $response['pform'] = array();
        if ($useCustomerPricing && isset($row['selected_paid']) && $row['selected_paid'] !== null) {
            $response['selected_paid'] = $row['selected_paid'];
        }
        if ($useCustomerPricing && isset($row['selected_pform']) && $row['selected_pform'] !== null) {
            $response['selected_pform'] = $row['selected_pform'];
        }
        if (!empty($pformIds)) {
            $formQuery = "SELECT fid, name, measurement FROM forms_value WHERE FIND_IN_SET(fid, ?)";
            $formStmt = $con->prepare($formQuery);
            $formStmt->bind_param("s", $pformIds);
            $formStmt->execute();
            $formResult = $formStmt->get_result();
            if (!$formResult) {
                throw new Exception("Error fetching form values: " . $con->error);
            }
            while ($formRow = $formResult->fetch_assoc()) {
                $response['pform'][] = $formRow;
            }
            $formStmt->close();
        }
        $packageIds = $row['paid'];
        $response['paid'] = array();
        if (!empty($packageIds)) {
            $packageQuery = "SELECT paid, name FROM package WHERE FIND_IN_SET(paid, ?)";
            $packageStmt = $con->prepare($packageQuery);
            $packageStmt->bind_param("s", $packageIds);
            $packageStmt->execute();
            $packageResult = $packageStmt->get_result();
            if (!$packageResult) {
                throw new Exception("Error fetching packages: " . $con->error);
            }
            while ($packageRow = $packageResult->fetch_assoc()) {
                $response['paid'][] = $packageRow;
            }
            $packageStmt->close();
        }
    } else {
        throw new Exception("Product not found");
    }
    
    $stmt->close();
    
} catch (Exception $e) {
    error_log("Product fetch error: " . $e->getMessage());
    $response = array('success' => false, 'error' => $e->getMessage());
}

header('Content-Type: application/json');
echo json_encode($response);
exit;
?>

MMCT - 2023