JezK
Edit File: functions.php
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); class UserFunctions { public $conn; public $db; function __construct() { require_once dirname(__FILE__) . '/dbconnect.php'; date_default_timezone_set('Etc/UTC'); $this->db = new DbConnect(); $this->conn = $this->db->connect(); } function userLogin($post) { $phone = strtolower(trim($post['phone'])); $password = base64_encode(trim($post['password'])); $phone = intval($phone); $query = $this->conn->prepare("SELECT * FROM user WHERE phone = :phone AND delete_status = 0"); $query->bindParam(':phone', $phone, PDO::PARAM_STR); if (!$query->execute()) { print_r($query->errorInfo()); // Display error information exit; } $query->execute(); $count = $query->rowcount(); //print_r($count); exit; if ($count == 1) { $data = $query->fetch(PDO::FETCH_ASSOC); if ($password != $data['password']) { // Wrong Password $result['status'] = 4; return $result; } if ($data['login_status'] == 0) { // User Not Verified $result['status'] = 2; return $result; } if ($data['delete_status'] == 1) { // User Deleted $result['status'] = 3; return $result; } $result['data'] = $data; $result['status'] = 1; $_SESSION['user_name'] = $result['data']['user_name']; $_SESSION['user_id'] = $result['data']['user_id']; $_SESSION['id'] = $result['data']['id']; } else { $result['status'] = 0; // User Doesn't Exist } return $result; } function forgotPassword($post) { $phone = strtolower(trim($post['phone'])); $query = $this->conn->prepare('SELECT * FROM user WHERE phone=:phone AND delete_status!=1'); $query->bindParam(':phone', $phone, PDO::PARAM_STR); $query->execute(); $count = $query->rowcount(); $result = array(); if ($count > 0) { $data = $query->fetch(PDO::FETCH_ASSOC); $userId = $data['id']; $otp = rand(1000, 9999); $this->sendSms($phone, $otp); $updateQuery = $this->conn->prepare('UPDATE user SET forgot_otp = :forgot_otp WHERE id = :id AND delete_status!=1'); $updateQuery->bindParam(':forgot_otp', $otp, PDO::PARAM_STR); $updateQuery->bindParam(':id', $userId, PDO::PARAM_INT); $updateQuery->execute(); $update = $updateQuery->rowCount(); $result['status'] = 1; $result['phone'] = $data['phone']; $result['token'] = base64_encode($data['phone']); $result['otp'] = $otp; } else { $result['status'] = 0; } return $result; } function otpVerify($post) { $phoneNumber = $post['token']; $otp = $post['otpNumber']; $verifyStatus = $post['verifyStatus']; $query = $this->conn->prepare('SELECT * FROM user WHERE phone = :phone AND delete_status!=1'); $query->bindParam(':phone', $phoneNumber, PDO::PARAM_STR); $query->execute(); $data = $query->fetch(PDO::FETCH_ASSOC); // print_r($data); // $result['data'] = $data; // $result['status'] = 1; // $_SESSION['status']=1; // $_SESSION['user_name'] = $result['data']['user_name']; // $_SESSION['user_id'] = $result['data']['user_id']; // $_SESSION['id'] = $result['data']['id']; // print_r($_SESSION); // exit; $result = array(); $result['userId'] = $data['id']; if ($otp == $data['forgot_otp']) { if ($verifyStatus == 1) { $query = $this->conn->prepare('UPDATE user SET login_status=1 WHERE phone=:phone AND delete_status!=1'); $query->bindParam(':phone', $phoneNumber, PDO::PARAM_STR); $query->execute(); } else { $query = $this->conn->prepare('UPDATE user SET forgot_status=1 WHERE phone=:phone AND delete_status!=1'); $query->bindParam(':phone', $phoneNumber, PDO::PARAM_STR); $query->execute(); } $result['data'] = $data; $result['status'] = 1; $_SESSION['status'] = 1; $_SESSION['user_name'] = $result['data']['user_name']; $_SESSION['user_id'] = $result['data']['user_id']; $_SESSION['id'] = $result['data']['id']; } else { $result['status'] = 0; } return $result; } function updatePassword($post) { $userId = $post['token']; $password = base64_encode($post['newPassword']); $sql = 'UPDATE user SET `password` = "' . $password . '" WHERE id = "' . $userId . '"'; $updateQuery = $this->conn->prepare($sql); $updateQuery->execute(); $update = $updateQuery->rowCount(); echo $update; } function sendSms($mobile, $otp, $from = '') { // Replace these values with your actual data $api_key = 'a51944sa7u24U24A5SA71944'; // Your API key $mobile_number = $mobile; // The recipient's mobile number $sender_id = 'KRYSOF'; // The sender ID (e.g., ABC123) $message = 'OTP for login on GOAMBEE App is ' . $otp . ', Please do not share with anyone. GOAMBEE Krify'; // The text message you want to send // Build the URL $url = 'http://SMS.VRINFOSOFT.CO.IN/unified.php'; $data = array( 'key' => $api_key, 'ph' => $mobile_number, 'sndr' => $sender_id, 'text' => $message, ); // Initialize cURL session $ch = curl_init(); // Set cURL options curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Execute cURL session and get the response $response = curl_exec($ch); // Check for cURL errors if (curl_errno($ch)) { echo 'cURL error: ' . curl_error($ch); } // Close cURL session curl_close($ch); // Process the response if ($response === false) { // return $response; return 2; } else { if ($from == 'login') { $updateQuery = $this->conn->prepare('UPDATE user SET forgot_otp = :forgot_otp WHERE phone = :phone'); // echo $otp; // echo $mobile; $updateQuery->bindParam(':forgot_otp', $otp, PDO::PARAM_STR); $updateQuery->bindParam(':phone', $mobile, PDO::PARAM_STR); // print_r($updateQuery); // exit; $updateQuery->execute(); } return 1; // echo 'Response: ' . $response; } } function resendOtp($mobile) { $otp = rand(1000, 9999); $this->sendSms($mobile, $otp); $updateQuery = $this->conn->prepare('UPDATE user SET forgot_otp = :forgot_otp WHERE phone =:phone AND delete_status!=1'); $updateQuery->bindParam(':forgot_otp', $otp, PDO::PARAM_STR); $updateQuery->bindParam(':phone', $mobile, PDO::PARAM_STR); $updateQuery->execute(); return $otp; } function getServices($post) { $query = $this->conn->prepare('SELECT `service_id`,`name`, `description`,`sedan_amount`,`muv_amount`,`suv_amount`,`hb_amount`,`image_name` FROM services WHERE service_status = 1 ORDER BY `service_id` DESC LIMIT :limit OFFSET :offset'); $query->bindValue(':limit', $post['limit'], PDO::PARAM_INT); $query->bindValue(':offset', $post['offset'], PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); $query1 = $this->conn->prepare('SELECT `service_id` FROM services WHERE service_status = 1'); $query1->execute(); $count = $query1->rowcount(); $data['services'] = $result; $data['count'] = $count; return $data; } function getProducts($post) { $query = $this->conn->prepare('SELECT * FROM products WHERE product_status = 1 ORDER BY `product_id` DESC LIMIT :limit OFFSET :offset'); $query->bindValue(':limit', $post['limit'], PDO::PARAM_INT); $query->bindValue(':offset', $post['offset'], PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); $query1 = $this->conn->prepare('SELECT * FROM products WHERE product_status = 1'); $query1->execute(); $count = $query1->rowcount(); $data['products'] = $result; $data['count'] = $count; return $data; } function getServiceDetails($post) { $id = base64_decode($post['id']); $query = $this->conn->prepare('SELECT p.package_id, p.name, p.total_points, p.service_points, p.month, a.created_date, a.details, a.service_id, a.id, a.vechile_details FROM packages AS p JOIN allotment AS a ON p.package_id = a.package_id WHERE a.type = 1 AND a.id = :id ORDER BY `package_id`'); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); $data['mypackages'] = $result; return $data; } function getTotalProducts() { $query1 = $this->conn->prepare('SELECT `name`,`amount`,`image_name`,`description` FROM products WHERE product_status = 1'); $query1->execute(); $count = $query1->rowcount(); return $count; } function getMyProducts($post) { if (!empty($post['fromDate'])) { $query = $this->conn->prepare('SELECT id,created_date, details, product_id FROM allotment WHERE (type = 3 or type = 2) AND user_id = :id AND ( approve_status = 1 OR approve_status = 4) AND created_date LIKE CONCAT("%", :date, "%") ORDER BY created_date DESC'); $query->bindValue(':id', $post['id'], PDO::PARAM_INT); $query->bindValue(':date', '%' . $post['fromDate'] . '%', PDO::PARAM_STR); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); } else { $query = $this->conn->prepare('SELECT id,created_date, details, product_id FROM allotment WHERE (type = 3 or type = 2) AND ( approve_status = 1 OR approve_status = 4) AND user_id = :id ORDER BY created_date DESC'); $query->bindValue(':id', $post['id'], PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); } $combinedArray = []; foreach ($result as $rows) { $row = unserialize($rows['details']); $productNames = isset($row['productNames']) ? json_decode($row['productNames'], true) : []; $productQuantity = isset($row['productQuantity']) ? json_decode($row['productQuantity'], true) : []; $productCosts = isset($row['productCosts']) ? json_decode($row['productCosts'], true) : []; $productExpiry = isset($row['productExpiry']) ? json_decode($row['productExpiry'], true) : []; $productDuration = isset($row['productDuration']) ? json_decode($row['productDuration'], true) : []; $productWarrenty = isset($row['productWarrenty']) ? json_decode($row['productWarrenty'], true) : []; $productWarrentyDuration = isset($row['productWarrentyDuration']) ? json_decode($row['productWarrentyDuration'], true) : []; $created_date = $rows['created_date']; $product_id = json_decode($rows['product_id']); for ($i = 0; $i < count($productNames); $i++) { $query1 = $this->conn->prepare('SELECT image_name FROM products WHERE product_id = :product_id'); $query1->bindValue(':product_id', $product_id[$i], PDO::PARAM_INT); $query1->execute(); $result1 = $query1->fetch(PDO::FETCH_ASSOC); $combinedArray[] = [ 'id' => $rows['id'], 'productName' => $productNames[$i], 'quantity' => $productQuantity[$i], 'cost' => $productCosts[$i], 'productExpiry' => $productExpiry[$i], 'productDuration' => $productDuration[$i], 'productWarrenty' => $productWarrenty[$i], 'productWarrentyDuration' => $productWarrentyDuration[$i], 'created_date' => $created_date, 'image' => $result1['image_name'] ]; } } $data['myproducts'] = $combinedArray; $data['count'] = count($combinedArray); return $data; } function getTotalMyProducts($id) { $query = $this->conn->prepare("SELECT created_date, details, product_id FROM allotment WHERE (type = 3 or type = 2 ) AND user_id = $id AND ( approve_status = 1 OR approve_status = 4) "); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); $combinedArray = []; foreach ($result as $rows) { $row = isset($rows['details']) ? unserialize($rows['details']) : []; $productNames = isset($row['productNames']) ? json_decode($row['productNames']) : []; $productQuantity = isset($row['productQuantity']) ? $row['productQuantity'] : []; $productCosts = isset($row['productCosts']) ? $row['productCosts'] : []; $created_date = isset($rows['created_date']) ? $rows['created_date'] : ''; $product_id = isset($rows['product_id']) ? json_decode($rows['product_id']) : []; $productsCount = count($productNames); for ($i = 0; $i < $productsCount; $i++) { $query1 = $this->conn->prepare('SELECT image_name FROM products WHERE product_id = :product_id'); $query1->bindValue(':product_id', $product_id[$i], PDO::PARAM_INT); $query1->execute(); $result1 = $query1->fetch(PDO::FETCH_ASSOC); $combinedArray[] = [ 'productName' => $productNames[$i], 'quantity' => $productQuantity[$i], 'cost' => $productCosts[$i], 'created_date' => $created_date, 'image' => $result1['image_name'] ]; } } $data = count($combinedArray); return $data; } function getTotalMyProducts1($post) { $query = $this->conn->prepare('SELECT created_date, details, product_id FROM allotment WHERE (type = 3 or type = 2 )AND user_id = :id AND created_date LIKE CONCAT("%", :date, "%")'); $query->bindValue(':id', $post['id'], PDO::PARAM_INT); $query->bindValue(':date', '%' . $post['fromDate'] . '%', PDO::PARAM_STR); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $rows) { $row = unserialize($rows['details']); // print_r($row);exit; // foreach($data2 as $row){ // print_r($row['productNames']);exit; $productNames = isset($row['productNames']) ? json_decode($row['productNames'], true) : []; $productQuantity = isset($row['productQuantity']) ? json_decode($row['productQuantity'], true) : []; $productCosts = isset($row['productCosts']) ? json_decode($row['productCosts'], true) : []; $created_date = $rows['created_date']; $product_id = isset($rows['product_id']) ? json_decode($rows['product_id']) : null; $productsCount = count($productNames) ? count($productNames) : 0; for ($i = 0; $i < $productsCount; $i++) { $query1 = $this->conn->prepare('SELECT image_name FROM products WHERE product_id = :product_id'); $query1->bindValue(':product_id', $product_id[$i], PDO::PARAM_INT); $query1->execute(); $result1 = $query1->fetch(PDO::FETCH_ASSOC); $combinedArray[] = [ 'productName' => $productNames[$i], 'quantity' => $productQuantity[$i], 'cost' => $productCosts[$i], 'created_date' => $created_date, 'image' => $result1['image_name'] ]; } // } } // print_r($combinedArray);exit; $data = count($combinedArray); return $data; } function getmyservices($post) { if (!empty($post['fromDate'])) { $query = $this->conn->prepare('SELECT * FROM allotment WHERE (type = 2 or type = 3) AND user_id = :id AND ( approve_status = 1 OR approve_status = 4) AND created_date LIKE CONCAT("%", :date, "%") ORDER BY created_date DESC'); $query->bindValue(':id', $post['id'], PDO::PARAM_INT); $query->bindValue(':date', '%' . $post['fromDate'] . '%', PDO::PARAM_STR); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); } else { $query = $this->conn->prepare('SELECT * FROM allotment WHERE (type = 2 or type = 3) AND ( approve_status = 1 OR approve_status = 4) AND user_id = :id ORDER BY created_date DESC'); $query->bindValue(':id', $post['id'], PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); } $combinedArray = []; foreach ($result as $rows) { $row = unserialize($rows['details']); $completedServiceDetails = unserialize($rows['vechile_details']); $servicesNames = json_decode($row['servicesNames']); $servicesCosts = json_decode($row['servicesCosts']); $created_date = $rows['created_date']; $service_id = json_decode($rows['service_id']); for ($i = 0; $i < count($servicesNames); $i++) { $query1 = $this->conn->prepare('SELECT image_name FROM services WHERE service_id = :service_id'); $query1->bindValue(':service_id', $service_id[$i], PDO::PARAM_INT); $query1->execute(); $result1 = $query1->fetch(PDO::FETCH_ASSOC); // Check if service status is complete or pending $status = 'Pending'; $serviceDate = '0'; foreach ($completedServiceDetails as $serviceDetail) { if (isset($serviceDetail['date'])) { $serviceDate = $serviceDetail['date']; } if ($serviceDetail['serviceName'] === $servicesNames[$i]) { if ($serviceDetail['status'] == 1) { $status = 'Complete'; } break; } } $combinedArray[] = [ 'allotmentId' => $rows['id'], 'servicesName' => $servicesNames[$i], 'cost' => $servicesCosts[$i], 'created_date' => $created_date, 'serviceDate' => $serviceDate, 'image' => $result1['image_name'], 'status' => $status // Append the status to the combined array ]; } } $data['myservices'] = $combinedArray; $data['count'] = count($combinedArray); return $data; } function getServicesinMyservices($service) { $services = json_decode($service['selectedServicesIds']); $placeholders = implode(',', array_fill(0, count($services), '?')); $query = "SELECT name,amount FROM services WHERE service_id IN ($placeholders)"; // Assuming $pdo is your PDO instance $stmt = $this->conn->prepare($query); // Bind the values from the $services array to the placeholders foreach ($services as $index => $service) { $stmt->bindValue($index + 1, $service); } // Execute the prepared statement $stmt->execute(); // Fetch the results $results = $stmt->fetchAll(PDO::FETCH_ASSOC); return $results; } // function added just as where condtions delete_status = 0 function getCoupons($post) { $delete_status = 0; $query = $this->conn->prepare('SELECT * FROM coupons WHERE usercount > usedusers AND delete_status = :delete_status AND cpn_type = 1 LIMIT :limit OFFSET :offset'); $query->bindValue(':delete_status', $delete_status, PDO::PARAM_INT); $query->bindValue(':limit', $post['limit'], PDO::PARAM_INT); $query->bindValue(':offset', $post['offset'], PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); $data['coupons'] = $result; return $data; } function getTotalCoupons() { $query = $this->conn->prepare('SELECT * FROM coupons WHERE usercount > usedusers AND delete_status = 0'); $query->execute(); $count = $query->rowcount(); return $count; } function getSingleserviceDetails($id) { $serviceId = $id; $query = 'SELECT * From services WHERE service_id =:service_id'; $selectQuery = $this->conn->prepare($query); $selectQuery->bindParam(':service_id', $serviceId, PDO::PARAM_INT); $selectQuery->execute(); $servicesData = $selectQuery->fetch(PDO::FETCH_ASSOC); $servicesData['image_name'] = $_ENV['base_url'] . 'uploads/services/' . $servicesData['image_name']; return $servicesData; } function getServicesByCareClub($id) { $careClubId = $id; $query = 'SELECT services_1_year,services_2_year,services_3_year,services_4_year,services_5_year From care_club_brach WHERE care_club_id =:care_club_id'; $selectQuery = $this->conn->prepare($query); $selectQuery->bindParam(':care_club_id', $careClubId, PDO::PARAM_INT); $selectQuery->execute(); $queryData = $selectQuery->fetch(PDO::FETCH_ASSOC); $services1stYr = json_decode($queryData['services_1_year'], true) ?? []; $services2ndYr = json_decode($queryData['services_2_year'], true) ?? []; $services3rdYr = json_decode($queryData['services_3_year'], true) ?? []; $services4thYr = json_decode($queryData['services_4_year'], true) ?? []; $services5thYr = json_decode($queryData['services_5_year'], true) ?? []; $allServiceIds = array_values(array_unique(array_merge( array_keys($services1stYr), array_keys($services2ndYr), array_keys($services3rdYr), array_keys($services4thYr), array_keys($services5thYr) ))); /* ----------------------------- BUILD SERVICE ID => TOTAL COUNT MAP ------------------------------*/ $serviceCountMap = []; foreach ([$services1stYr, $services2ndYr, $services3rdYr, $services4thYr, $services5thYr] as $yearServices) { foreach ($yearServices as $serviceId => $count) { if (!isset($serviceCountMap[$serviceId])) { $serviceCountMap[$serviceId] = 0; } $serviceCountMap[$serviceId] += (int)$count; } } /* ----------------------------- FETCH SERVICE NAMES + BUILD FINAL ARRAY ------------------------------*/ $serviceNames = []; $i = 0; foreach ($allServiceIds as $serviceId) { $service = $this->getSingleserviceDetails($serviceId); if (!$service || !isset($service['name'])) { continue; } $count = $serviceCountMap[$serviceId] ?? 0; $serviceNames[$i] = [ 'service_id' => $serviceId, 'name' => $service['name'], 'count' => $count ]; $i++; } return $serviceNames; } function getTotalServices() { $query = $this->conn->prepare('SELECT service_id,created_date,sedan_amount,muv_amount,suv_amount,hb_amount FROM services WHERE service_status = 1'); $query->execute(); $count = $query->rowcount(); return $count; } function getTotalMyServices($id) { $query = $this->conn->prepare('SELECT * FROM allotment WHERE user_id =:id AND (type= 2 or type=3)'); $query->bindValue(':id', $id, PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); $combinedArray = []; // echo "<pre>"; // print_r($result);exit; foreach ($result as $rows) { $row = unserialize($rows['details']); // print_r($row);exit; // foreach($data2 as $row){ // print_r(json_decode($row['servicesNames']));exit; $servicesNames = json_decode($row['servicesNames']); $servicesCosts = json_decode($row['servicesCosts']); $created_date = $rows['created_date']; $service_id = json_decode($rows['service_id']); for ($i = 0; $i < count($servicesNames); $i++) { $query1 = $this->conn->prepare('SELECT image_name FROM services WHERE service_id = :service_id'); $query1->bindValue(':service_id', $service_id[$i], PDO::PARAM_INT); $query1->execute(); $result1 = $query1->fetch(PDO::FETCH_ASSOC); $combinedArray[] = [ 'servicesName' => $servicesNames[$i], 'cost' => $servicesCosts[$i], 'created_date' => $created_date, 'image' => $result1['image_name'] ]; } // } } // print_r($combinedArray);exit; $data = count($combinedArray); return $data; } function getTotalMyServices1($post) { $query = $this->conn->prepare('SELECT created_date, details, service_id FROM allotment WHERE (type = 2 or type= 3) AND user_id = :id AND created_date LIKE CONCAT("%", :date, "%")'); $query->bindValue(':id', $post['id'], PDO::PARAM_INT); $query->bindValue(':date', '%' . $post['fromDate'] . '%', PDO::PARAM_STR); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); $combinedArray = []; foreach ($result as $rows) { $row = unserialize($rows['details']); // print_r($row);exit; // foreach($data2 as $row){ // print_r(json_decode($row['servicesNames']));exit; $servicesNames = json_decode($row['servicesNames']); $servicesCosts = json_decode($row['servicesCosts']); $created_date = $rows['created_date']; $service_id = json_decode($rows['service_id']); for ($i = 0; $i < count($servicesNames); $i++) { $query1 = $this->conn->prepare('SELECT image_name FROM services WHERE service_id = :service_id'); $query1->bindValue(':service_id', $service_id[$i], PDO::PARAM_INT); $query1->execute(); $result1 = $query1->fetch(PDO::FETCH_ASSOC); $combinedArray[] = [ 'servicesName' => $servicesNames[$i], 'cost' => $servicesCosts[$i], 'created_date' => $created_date, 'image' => $result1['image_name'] ]; } // } } // print_r($combinedArray);exit; $data = count($combinedArray); return $data; } //User Registration // function userRegister($post) // { // $check = $this->conn->prepare("SELECT * FROM user WHERE phone =:phone AND delete_status!=1"); // $check->bindParam(':phone', $post['phone'], PDO::PARAM_INT); // $check->execute(); // $count = $check->rowcount(); // $result = array(); // if ($count > 0) { // $result['status'] = 2; // return $result; // } else { // $referred_by = isset($post['referred_by']) ? trim($post['referred_by']) : null; // $referral_code = strtoupper(bin2hex(random_bytes(4))); // 8 chars // $name = $post['name']; // $phone = $post['phone']; // $email = $post['email']; // $address = $post['address']; // $password = base64_encode($post['password']); // $blockstatus = 1; // $created_date = date('Y-m-d H:i:s'); // if ($referred_by) { // $checkRef = $this->conn->prepare("SELECT user_id FROM user WHERE referral_code = :referral"); // $checkRef->bindParam(':referral', $referred_by); // $checkRef->execute(); // if ($checkRef->rowCount() == 0) { // $referred_by = null; // ignore invalid code // } // } // $query = $this->conn->prepare("SELECT MAX(id) + 1 AS last_id FROM user"); // $query->execute(); // $result = $query->fetch(PDO::FETCH_ASSOC); // $user_id = "DE" . $result['last_id']; // $otp = rand(1000, 9999); // $this->sendSms($phone, $otp); // $insertQuery = "INSERT INTO user (user_id,user_name,phone,email,address,password,blockstatus,created_date,modified_date,forgot_otp,login_status,forgot_status, referral_code, referred_by) VALUES ('$user_id','$name','$phone','$email','$address','$password','$blockstatus','$created_date','',$otp,0,0,'$referral_code','$referred_by')"; // $insert = $this->conn->exec($insertQuery); // if ($insert) { // $result['token'] = base64_encode($phone); // $result['status'] = 1; // return $result; // } else { // $result['status'] = 3; // return $result; // } // } // } public function userRegister($post) { $result = []; // 1. Check if phone already registered $stmt = $this->conn->prepare("SELECT * FROM user WHERE phone = :phone AND delete_status != 1"); $stmt->bindParam(':phone', $post['phone'], PDO::PARAM_STR); $stmt->execute(); if ($stmt->rowCount() > 0) { return ['status' => 2]; // Phone already exists } // 2. Setup all fields $name = trim($post['name'] ?? ''); $phone = trim($post['phone'] ?? ''); $email = trim($post['email'] ?? ''); $address = trim($post['address'] ?? ''); $password = base64_encode($post['password'] ?? ''); // Prefer password_hash! $blockstatus = 1; $created_date = date('Y-m-d H:i:s'); $modified_date = null; $otp = rand(1000, 9999); $referral_code = strtoupper(bin2hex(random_bytes(4))); // 8-char code $referred_by = isset($post['referred_by']) && trim($post['referred_by']) !== '' ? trim($post['referred_by']) : null; // 3. Validate referred_by if ($referred_by) { $refCheck = $this->conn->prepare("SELECT id FROM user WHERE referral_code = :referral"); $refCheck->bindParam(':referral', $referred_by); $refCheck->execute(); if ($refCheck->rowCount() == 0) { $referred_by = null; // ignore invalid code } else { $referred_by = $refCheck->fetch(PDO::FETCH_ASSOC)['id']; } } // 4. Generate user ID $idQuery = $this->conn->query("SELECT MAX(id) + 1 AS last_id FROM user"); $idResult = $idQuery->fetch(PDO::FETCH_ASSOC); $user_id = "DE" . ($idResult['last_id'] ?? 1); // 5. Send OTP (assumes sendSms is defined) $this->sendSms($phone, $otp); // 6. Insert user $insertQuery = " INSERT INTO user ( user_id, user_name, phone, email, address, password, blockstatus, created_date, modified_date, forgot_otp, login_status, forgot_status, referral_code, referred_by ) VALUES ( :user_id, :name, :phone, :email, :address, :password, :blockstatus, :created_date, :modified_date, :forgot_otp, 0, 0, :referral_code, :referred_by ) "; $insertStmt = $this->conn->prepare($insertQuery); $insertStmt->execute([ ':user_id' => $user_id, ':name' => $name, ':phone' => $phone, ':email' => $email, ':address' => $address, ':password' => $password, ':blockstatus' => $blockstatus, ':created_date' => $created_date, ':modified_date' => $modified_date, ':forgot_otp' => $otp, ':referral_code' => $referral_code, ':referred_by' => $referred_by, ]); if ($insertStmt->rowCount() > 0) { return [ 'status' => 1, 'token' => base64_encode($phone), ]; } return ['status' => 3]; // Insert failed } function getHomeData() { $query = $this->conn->prepare('SELECT * FROM home WHERE id = 1'); $query->execute(); $result = $query->fetch(PDO::FETCH_ASSOC); return $result; } function allotPurchases($post) { $userId = $post['userId']; $cartItemId = $post['cartItemId']; $type = $post['type']; // 1-Care Club, 2-Products, 3-Service $branchId = isset($post['branchId']) ? $post['branchId'] : 0; $planType = isset($post['planType']) ? $post['planType'] : 0; $loyaltyPoints = isset($post['loyalty']) ? floatval($post['loyalty']) : 0; $referralPoints = isset($post['referral']) ? floatval($post['referral']) : 0; $result = array(); //check if same user has already requested for same service which is pending switch ($type) { case 1: $column = 'care_club_id'; $condition = "$column = :cartItemId"; // Direct comparison for INT $itemId = $cartItemId; $purchaseType = 4; break; case 2: case 3: $column = ($type == 2) ? 'service_id' : 'product_id'; $condition = "JSON_CONTAINS($column, :cartItemId)"; // JSON search $itemId = json_encode((string)$cartItemId); // Convert to JSON string $purchaseType = $type; break; } $query = "SELECT * FROM allotment WHERE user_id = :userId AND $condition AND type = :type AND approve_status = 6"; $stmt = $this->conn->prepare($query); $stmt->bindParam(':userId', $userId, PDO::PARAM_INT); $stmt->bindParam(':cartItemId', $itemId, ($type == 1) ? PDO::PARAM_INT : PDO::PARAM_STR); // Adjust binding type $stmt->bindParam(':type', $purchaseType, PDO::PARAM_INT); $stmt->execute(); $response = $stmt->fetch(PDO::FETCH_ASSOC); if ($response) { $result['status'] = 0; //already allocated for same service return $result; } $approveStatus = 6; // for user purchased ones $notificationStatus = 3; $vehicletype = ''; $vehicleNumber = ''; $model = ''; $paymentDetails = json_encode([]); // Initialize as empty JSON array $paymentId = ''; // Initialize variables for insertion $invoiceId = 0; $amount = 0; $purchaseDetails = ''; $vehicleDetailsString = ''; $couponId = isset($post['couponId']) && $post['couponId'] != "" ? $post['couponId'] : 0; $couponAmount = isset($post['couponAmount']) && $post['couponAmount'] != "" ? $post['couponAmount'] : 0; $carType = isset($post['carType']) ? $post['carType'] : 0; if ($couponId > 0) { $couponSql = "SELECT * FROM coupons where coupon_id=:couponId"; $couponQuery = $this->conn->prepare($couponSql); $couponQuery->bindParam(':couponId', $couponId, PDO::PARAM_INT); $couponQuery->execute(); $couponData = $couponQuery->fetch(PDO::FETCH_ASSOC); $usercount = $couponData['usercount'] - $couponData['usedusers']; if ($usercount > 0) { $couponUsedUsers = $couponData['usedusers'] + 1; $updateCouponQuery = $this->conn->prepare('UPDATE coupons SET usedusers = :usedusers WHERE coupon_id = :coupon_id'); $updateCouponQuery->bindParam(':usedusers', $couponUsedUsers, PDO::PARAM_INT); $updateCouponQuery->bindParam(':coupon_id', $couponId, PDO::PARAM_INT); $updateCouponQuery->execute(); } } // Care Club if ($type == 1) { // Check and update alloted_count in care_club_branch table $sql = "SELECT count, alloted_count FROM care_club_brach WHERE care_club_id = :cartItemId AND branch_id = :branchId and delete_status=0"; $query = $this->conn->prepare($sql); $query->bindParam(':cartItemId', $cartItemId, PDO::PARAM_INT); $query->bindParam(':branchId', $branchId, PDO::PARAM_INT); $query->execute(); $countData = $query->fetch(PDO::FETCH_ASSOC); if (!$countData) { $result['status'] = 1; $result['message'] = "Care club not found or invalid branch"; return $result; } $alloted_count = $countData['alloted_count']; $count = $countData['count']; if ($alloted_count >= $count) { $result['status'] = 1; $result['message'] = "Care club purchase limit exceeded"; return $result; } else { $newCount = $alloted_count + 1; $sql = "UPDATE care_club_brach SET alloted_count = :newCount WHERE care_club_id = :cartItemId AND branch_id = :branchId"; $updateQuery = $this->conn->prepare($sql); $updateQuery->bindParam(':newCount', $newCount, PDO::PARAM_INT); $updateQuery->bindParam(':cartItemId', $cartItemId, PDO::PARAM_INT); $updateQuery->bindParam(':branchId', $branchId, PDO::PARAM_INT); $updateQuery->execute(); } $approveStatus = 6; // for user purchased ones $notificationStatus = 3; $careClubId = $cartItemId; // Assuming only one care club is processed $careClubBranchId = $branchId; // Assuming only one care club branch ID is processed $vehicletype = ''; $vehicleNumber = ''; $model = ''; $paymentDetails = ''; $paymentId = ''; // Retrieve details for the care club $careClubDetails = $this->getSingleCareClub(['id' => $careClubId, 'branchId' => $careClubBranchId]); switch ($carType) { case 1: $carName = "Sedan"; $careClubDetails['amount'] = $careClubDetails['sedan_price_'.$planType.'_year']; break; case 2: $carName = "SUV"; $careClubDetails['amount'] = $careClubDetails['suv_price_'.$planType.'_year']; break; case 3: $carName = "MUV"; $careClubDetails['amount'] = $careClubDetails['muv_price_'.$planType.'_year']; break; case 4: $carName = "Hatchback"; $careClubDetails['amount'] = $careClubDetails['hb_price_'.$planType.'_year']; break; } switch ($planType) { case 1: $careClubServiceDetails = json_decode($careClubDetails['services_1_year'],true); break; case 2: $careClubServiceDetails = json_decode($careClubDetails['services_2_year'],true); break; case 3: $careClubServiceDetails = json_decode($careClubDetails['services_3_year'],true); break; case 4: $careClubServiceDetails = json_decode($careClubDetails['services_4_year'],true); break; case 5: $careClubServiceDetails = json_decode($careClubDetails['services_5_year'],true); break; } // Get service details for the care club $serviceIds = array_keys($careClubServiceDetails); $serviceCounts = array_values($careClubServiceDetails); // Generate JSON strings for service IDs and counts $serviceIdsString = json_encode($serviceIds); $serviceCountsString = json_encode($serviceCounts); // Generate JSON string for service names $serviceNamesArray = []; foreach ($serviceIds as $id) { $serviceDeatils = $this->getSingleserviceDetails($id); $serviceNamesArray[] = $serviceDeatils['name']; } // Here we are adding all service names with their count in the care club $serviceNameWithCountArray = []; foreach ($serviceCounts as $index => $count) { $name = $serviceNamesArray[$index]; $serviceNameWithCountArray = array_merge($serviceNameWithCountArray, array_fill(0, $count, $name)); } // Here we are adding all service names with their plan years in the care club $serviceNameWithPlanCountArray = []; foreach ($serviceNameWithCountArray as $name) { $serviceNameWithPlanCountArray = array_merge( $serviceNameWithPlanCountArray, array_fill(0, $planType, $name) ); } $serviceNames = json_encode($serviceNameWithPlanCountArray); // Calculate amount for the care club $amount = (isset($careClubDetails['subAdminAmount']) && $careClubDetails['subAdminAmount'] != '') ? $careClubDetails['subAdminAmount'] : $careClubDetails['amount']; if ($couponId != 0) { if ($couponData['cpn_type'] == 1) { $couponAmount = $couponData['amount']; } else { $couponAmount = $amount * ($couponData['percent'] / 100); } } if ($couponAmount > 0) $amount = $amount - $couponAmount; $amount = ($amount * 0.18) + $amount; // add tax // Prepare details string $details = [ 'careClubName' => $careClubDetails['name'], 'serviceIds' => $serviceIdsString, 'serviceNames' => $serviceNames, 'serviceCount' => $serviceCountsString, 'vehicletype' => $vehicletype, 'vehicleNumber' => $vehicleNumber, 'model' => $model, 'careClubCost' => $careClubDetails['amount'], 'totalServices' => count($serviceNameWithPlanCountArray), 'planType' => $planType, 'couponId' => $couponId, ]; $detailsString = serialize($details); // Generate vechile details array and string $vechileDetailsArray = []; for ($i = 0; $i < $details['totalServices']; $i++) { $vechileDetailsArray[$i]['status'] = 0; } $vechileDetailsString = serialize($vechileDetailsArray); $insertQuery = "INSERT INTO allotment SET invoice_id = :invoiceId, care_club_id = :careClubId, user_id = :userId, type = 4, amount = :amount, details = :details, vechile_details = :vechileDetails, care_club_details = :careClubDetails, care_club_vechile_details = :vechileDetails, approve_status = :approveStatus, notification_status = :notificationStatus, remarks = '', payment_details = :paymentDetails, payment_id = :paymentId, coupon_id = :couponId, branch_id = :branchId, car_type = :carType"; $insertStmt = $this->conn->prepare($insertQuery); $insertStmt->bindParam(':invoiceId', $invoiceId, PDO::PARAM_INT); $insertStmt->bindParam(':careClubId', $careClubId, PDO::PARAM_INT); $insertStmt->bindParam(':userId', $userId, PDO::PARAM_INT); $insertStmt->bindParam(':amount', $amount, PDO::PARAM_STR); $insertStmt->bindParam(':details', $detailsString, PDO::PARAM_STR); $insertStmt->bindParam(':vechileDetails', $vechileDetailsString, PDO::PARAM_STR); $insertStmt->bindParam(':careClubDetails', $detailsString, PDO::PARAM_STR); $insertStmt->bindParam(':approveStatus', $approveStatus, PDO::PARAM_INT); $insertStmt->bindParam(':notificationStatus', $notificationStatus, PDO::PARAM_INT); $paymentDetailsJson = json_encode($paymentDetails); $insertStmt->bindParam(':paymentDetails', $paymentDetailsJson, PDO::PARAM_STR); $insertStmt->bindParam(':paymentId', $paymentId, PDO::PARAM_STR); $insertStmt->bindParam(':couponId', $couponId, PDO::PARAM_INT); $insertStmt->bindParam(':branchId', $careClubBranchId, PDO::PARAM_STR); $insertStmt->bindParam(':carType', $carType, PDO::PARAM_INT); $insertStmt->execute(); // Store result for the current care club $results = [ 'status' => 1, 'insert' => $insertStmt->rowCount(), 'insertedId' => $this->conn->lastInsertId() ]; return $results; } else if ($type == 3) { // Products $approveStatus = 6; // for user purchased ones $notificationStatus = 3; $productIds = [$cartItemId]; $productQuantity = isset($post['productQuantity']) ? $post['productQuantity'][0] : 1; $paymentDetails = isset($post['paymentDetails']) ? json_encode($post['paymentDetails']) : []; $productDetails = $this->getSingleProductDetails($cartItemId); $encodedProductId = json_encode($productIds); if (!$productDetails) { return "Product details not found"; } $selectedProductName = $productDetails['name']; $selectedProductCost = $productDetails['amount'] * $productQuantity; $selectedProductExpiry = $productDetails['expiry']; $selectedProductDuration = $productDetails['duration']; $selectedProductWarranty = $productDetails['warranty']; $selectedProductWarrantyDuration = $productDetails['warranty_duration_type']; $subamount = $selectedProductCost; if ($couponId != "" && $couponId != 0) { if ($couponData['cpn_type'] == 1) { $couponAmount = $couponData['amount']; } else { $couponAmount = $subamount * ($couponData['percent'] / 100); } } if ($couponAmount > 0) $subamount = $subamount - $couponAmount; if ($loyaltyPoints > 0) { $subamount = $subamount - $loyaltyPoints; } if ($referralPoints > 0) { $subamount = $subamount - $referralPoints; } $amount = ($subamount * 0.18) + $subamount; $jsonData = array(); $jsonData['selectedServicesIds'] = json_encode([]); $jsonData['servicesNames'] = json_encode([]); $jsonData['servicesCosts'] = json_encode([]); $jsonData['selectedProductIds'] = json_encode($productIds); $jsonData['productNames'] = json_encode([$selectedProductName]); $jsonData['productQuantity'] = json_encode([$productQuantity]); $jsonData['productCosts'] = json_encode([$selectedProductCost]); $jsonData['productExpiry'] = json_encode([$selectedProductExpiry]); $jsonData['productDuration'] = json_encode([$selectedProductDuration]); $jsonData['productWarrenty'] = json_encode([$selectedProductWarranty]); $jsonData['productWarrentyDuration'] = json_encode([$selectedProductWarrantyDuration]); $jsonData['couponId'] = $couponId; $jsonData['couponAmount'] = $couponAmount; $jsonData['loyaltyPoints'] = $loyaltyPoints; $jsonData['referrralPoints'] = $referralPoints; $purchaseDetails = serialize($jsonData); $vehicleDetailsArray = []; $vehicleDetailsString = serialize($vehicleDetailsArray); // Fetching the next invoice ID $invoiceQuery = 'SELECT MAX(invoice_id) as invoiceId FROM allotment WHERE approve_status = 1 OR approve_status = 4'; $invoiceQuery = $this->conn->prepare($invoiceQuery); $invoiceQuery->execute(); $invoiceData = $invoiceQuery->fetch(PDO::FETCH_ASSOC); $invoiceId = ($invoiceData['invoiceId']) ? $invoiceData['invoiceId'] + 1 : 1; // Inserting the new allotment record $insertQuery = "INSERT INTO allotment (invoice_id, package_id, service_id, product_id, user_id, type, amount, details, vechile_details, care_club_details, care_club_vechile_details, approve_status, notification_status, remarks, payment_details, payment_id, coupon_id, loyalty_points, referral_points) VALUES (:invoiceId, '0', '', :productId, :userId, '3', :amount, :details, :vehicleDetails, '', '', :approveStatus, :notificationStatus, '', :paymentDetails, :paymentId, :couponId, :loyaltyPoints, :referralPoints)"; $insertStmt = $this->conn->prepare($insertQuery); $insertStmt->bindParam(':invoiceId', $invoiceId, PDO::PARAM_INT); $insertStmt->bindParam(':productId', $encodedProductId, PDO::PARAM_STR); $insertStmt->bindParam(':userId', $userId, PDO::PARAM_INT); $insertStmt->bindParam(':amount', $amount, PDO::PARAM_STR); $insertStmt->bindParam(':details', $purchaseDetails, PDO::PARAM_STR); $insertStmt->bindParam(':vehicleDetails', $vehicleDetailsString, PDO::PARAM_STR); $insertStmt->bindParam(':approveStatus', $approveStatus, PDO::PARAM_INT); $insertStmt->bindParam(':notificationStatus', $notificationStatus, PDO::PARAM_INT); $paymentDetailsJson = json_encode($paymentDetails); $insertStmt->bindParam(':paymentDetails', $paymentDetailsJson, PDO::PARAM_STR); $insertStmt->bindParam(':paymentId', $paymentId, PDO::PARAM_STR); $insertStmt->bindParam(':couponId', $couponId, PDO::PARAM_INT); $insertStmt->bindParam(':loyaltyPoints', $loyaltyPoints, PDO::PARAM_INT); $insertStmt->bindParam(':referralPoints', $referralPoints, PDO::PARAM_INT); $insert = $insertStmt->execute(); $insertedId = $this->conn->lastInsertId(); //update loyalty points in user table if ($loyaltyPoints > 0) { $query = "UPDATE user SET loyalty_points = loyalty_points - :loyaltyPoints WHERE id = :userId"; $stmt = $this->conn->prepare($query); $stmt->bindParam(':loyaltyPoints', $loyaltyPoints, PDO::PARAM_INT); $stmt->bindParam(':userId', $userId, PDO::PARAM_INT); $stmt->execute(); } //update referral points in user table if ($referralPoints > 0) { $query = "UPDATE user SET referral_points = referral_points - :referralPoints WHERE id = :userId"; $stmt = $this->conn->prepare($query); $stmt->bindParam(':referralPoints', $referralPoints, PDO::PARAM_INT); $stmt->bindParam(':userId', $userId, PDO::PARAM_INT); $stmt->execute(); } $result['status'] = 1; $result['insert'] = $insert; $result['insertedId'] = $insertedId; $result['encryptedUserId'] = base64_encode($userId); return $result; } else if ($type == 2) { // Service $approveStatus = 6; // for user purchased ones $notificationStatus = 3; $serviceId = [$cartItemId]; $encodedServiceId = json_encode($serviceId); $paymentDetails = isset($post['paymentDetails']) ? json_encode($post['paymentDetails']) : []; // Getting Service Data of the selected item $serviceDetails = $this->getSingleServiceDetails($cartItemId); $selectedServiceName = $serviceDetails['name']; if ($post['carType'] == 1) { $selectedServiceCost = $serviceDetails['sedan_amount']; } else if ($post['carType'] == 2) { $selectedServiceCost = $serviceDetails['suv_amount']; } else if ($post['carType'] == 3) { $selectedServiceCost = $serviceDetails['muv_amount']; } else if ($post['carType'] == 4) { $selectedServiceCost = $serviceDetails['hb_amount']; } $vechileDetailsString = serialize(array_map(function ($serviceName) { return ['status' => 0, 'serviceName' => $serviceName]; }, [$selectedServiceName])); if (!$serviceDetails) { return "Service details not found"; } $jsonData = array(); $jsonData['selectedServiceId'] = json_encode($serviceId); $jsonData['servicesNames'] = json_encode([$selectedServiceName]); $jsonData['servicesCosts'] = json_encode([$selectedServiceCost]); $jsonData['carType'] = $post['carType']; $jsonData['planType'] = $planType; $jsonData['couponId'] = $couponId; $jsonData['loyaltyPoints'] = $loyaltyPoints; $jsonData['referralPoints'] = $referralPoints; $subamount = $selectedServiceCost; if ($couponId != "" && $couponId != 0) { if ($couponData['cpn_type'] == 1) { $couponAmount = $couponData['amount']; } else { $couponAmount = $subamount * ($couponData['percent'] / 100); } } $jsonData['couponAmount'] = $couponAmount; $subamount = $selectedServiceCost; if ($couponAmount > 0) $subamount = $subamount - $couponAmount; if ($loyaltyPoints > 0) { $subamount = $subamount - $loyaltyPoints; } if ($referralPoints > 0) { $subamount = $subamount - $referralPoints; } $amount = ($subamount * 0.18) + $subamount; $purchaseDetails = serialize($jsonData); // Fetching the next invoice ID $invoiceQuery = 'SELECT MAX(invoice_id) as invoiceId FROM allotment WHERE approve_status = 1 OR approve_status = 4'; $invoiceQuery = $this->conn->prepare($invoiceQuery); $invoiceQuery->execute(); $invoiceData = $invoiceQuery->fetch(PDO::FETCH_ASSOC); $invoiceId = ($invoiceData['invoiceId']) ? $invoiceData['invoiceId'] + 1 : 1; $carType = $post['carType']; // Inserting the new allotment record $insertQuery = "INSERT INTO allotment (invoice_id, package_id, service_id, product_id, user_id, type, amount, details, vechile_details, care_club_details, care_club_vechile_details, approve_status, notification_status, remarks, payment_details, payment_id, coupon_id, car_type,loyalty_points,referral_points) VALUES (:invoiceId, '0', :serviceId, '', :userId, '2', :amount, :details, :vehicleDetails, '', '', :approveStatus, :notificationStatus, '', :paymentDetails, :paymentId, :couponId, :carType,:loyaltyPoints,:referralPoints)"; $insertStmt = $this->conn->prepare($insertQuery); $insertStmt->bindParam(':invoiceId', $invoiceId, PDO::PARAM_INT); $insertStmt->bindParam(':serviceId', $encodedServiceId, PDO::PARAM_STR); $insertStmt->bindParam(':userId', $userId, PDO::PARAM_INT); $insertStmt->bindParam(':amount', $amount, PDO::PARAM_STR); $insertStmt->bindParam(':details', $purchaseDetails, PDO::PARAM_STR); $insertStmt->bindParam(':vehicleDetails', $vechileDetailsString, PDO::PARAM_STR); $insertStmt->bindParam(':approveStatus', $approveStatus, PDO::PARAM_INT); $insertStmt->bindParam(':notificationStatus', $notificationStatus, PDO::PARAM_INT); $paymentDetailsJson = json_encode($paymentDetails); $insertStmt->bindParam(':paymentDetails', $paymentDetailsJson, PDO::PARAM_STR); $insertStmt->bindParam(':paymentId', $paymentId, PDO::PARAM_STR); $insertStmt->bindParam(':couponId', $couponId, PDO::PARAM_INT); $insertStmt->bindParam(':carType', $carType, PDO::PARAM_INT); $insertStmt->bindParam(':loyaltyPoints', $loyaltyPoints, PDO::PARAM_INT); $insertStmt->bindParam(':referralPoints', $referralPoints, PDO::PARAM_INT); $insert = $insertStmt->execute(); $insertedId = $this->conn->lastInsertId(); //update loyalty points in user table if ($loyaltyPoints > 0) { $query = "UPDATE user SET loyalty_points = loyalty_points - :loyaltyPoints WHERE id = :userId"; $stmt = $this->conn->prepare($query); $stmt->bindParam(':loyaltyPoints', $loyaltyPoints, PDO::PARAM_INT); $stmt->bindParam(':userId', $userId, PDO::PARAM_INT); $stmt->execute(); } //update referral points in user table if ($referralPoints > 0) { $query = "UPDATE user SET referral_points = referral_points - :referralPoints WHERE id = :userId"; $stmt = $this->conn->prepare($query); $stmt->bindParam(':referralPoints', $referralPoints, PDO::PARAM_INT); $stmt->bindParam(':userId', $userId, PDO::PARAM_INT); $stmt->execute(); } $result['status'] = 1; $result['insert'] = $insert; $result['insertedId'] = $insertedId; $result['encryptedUserId'] = base64_encode($userId); return $result; } } function addCartItem($post) { $userId = $post['userId']; $cartItemId = $post['cartItemId']; $type = $post['type']; // 1-Care Club, 2-Products, 3-Service $branchId = isset($post['branchId']) ? $post['branchId'] : 0; if ($type == 1) { $sql = "SELECT count,alloted_count FROM care_club_brach WHERE care_club_id = $cartItemId AND branch_id = $branchId"; $query = $this->conn->prepare($sql); $query->execute(); $countData = $query->fetch(PDO::FETCH_ASSOC); $alloted_count = $countData['alloted_count']; $count = $countData['count']; if ($alloted_count >= $count) { return "Care club purchase limit excedded"; } else { $newCount = $alloted_count + 1; $sql = "UPDATE care_club_brach SET alloted_count = $newCount WHERE care_club_id = $cartItemId AND branch_id = $branchId"; $updateQuery = $this->conn->prepare($sql); $updateQuery->execute(); } $insertSql = "INSERT INTO cart_details (user_id,care_club_id,branch_id) VALUES ($userId,$cartItemId,$branchId)"; $selectSql = "SELECT * FROM cart_details WHERE user_id=$userId AND care_club_id=$cartItemId"; } elseif ($type == 2) { $insertSql = "INSERT INTO cart_details (user_id,product_id) VALUES ($userId,$cartItemId)"; $selectSql = "SELECT * FROM cart_details WHERE user_id=$userId AND product_id=$cartItemId"; } elseif ($type == 3) { $insertSql = "INSERT INTO cart_details (user_id,service_id) VALUES ($userId,$cartItemId)"; $selectSql = "SELECT * FROM cart_details WHERE user_id=$userId AND service_id =$cartItemId"; } $selectQuery = $this->conn->prepare($selectSql); $selectQuery->execute(); $cartData = $selectQuery->fetch(PDO::FETCH_ASSOC); if ($cartData) { return 0; } $this->conn->exec($insertSql); $insertStatus = $this->conn->lastInsertId(); return $insertStatus; } function getUserCartProducts($post) { $userId = $post['userId']; $sql = "SELECT cart.id AS cartId,cart.product_id,cart.product_count,prod.name AS productName,prod.amount,prod.image_name,prod.description,prod.warranty,prod.warranty_duration_type, prod.expiry, prod.duration FROM cart_details AS cart LEFT JOIN products AS prod ON cart.product_id = prod.product_id WHERE cart.product_id != 0 AND cart.user_id = $userId AND prod.product_status = 1"; $query = $this->conn->prepare($sql); $query->execute(); $data = $query->fetchAll(PDO::FETCH_ASSOC); return $data; } function getUserCartIds($userId) { $sql = "SELECT GROUP_CONCAT(DISTINCT IF(service_id != 0, service_id, NULL)) AS serviceIds, GROUP_CONCAT(DISTINCT IF(product_id != 0, product_id, NULL)) AS productIds, GROUP_CONCAT(DISTINCT IF(product_id != 0, product_count, NULL)) AS productQuantity FROM cart_details WHERE user_id=$userId GROUP BY user_id"; $query = $this->conn->prepare($sql); $query->execute(); $data = $query->fetch(PDO::FETCH_ASSOC); return $data; } function changeProductCount($post) { $cartId = $post['cartId']; $type = $post['type']; // 1- Increase $getCountSql = "SELECT product_count FROM cart_details WHERE id=$cartId"; $getCountQuery = $this->conn->prepare($getCountSql); $getCountQuery->execute(); $data = $getCountQuery->fetch(PDO::FETCH_ASSOC); if ($data['product_count'] == 1 && $type != 1) { return 2; // User Need Have Alteast 1 Product Count } if ($type == 1) { // Increase $sql = "UPDATE cart_details SET product_count = product_count + 1 WHERE id=$cartId"; } else { $sql = "UPDATE cart_details SET product_count = product_count - 1 WHERE id=$cartId"; } $query = $this->conn->prepare($sql); $query->execute(); $update = $query->rowCount(); return $update; } function getUserCartServices($post) { $userId = $post['userId']; $sql = "SELECT cart.id AS cartId,cart.service_id,serv.name AS serviceName,serv.amount,serv.image_name FROM cart_details AS cart LEFT JOIN services AS serv ON cart.service_id = serv.service_id WHERE cart.service_id != 0 AND cart.user_id = $userId AND serv.service_status = 1"; $query = $this->conn->prepare($sql); $query->execute(); $data = $query->fetchAll(PDO::FETCH_ASSOC); return $data; } function getUserCartCareClubs($post) { $userId = $post['userId']; $sql = "SELECT cart.id AS cartId,cart.branch_id,cart.care_club_id,care.name AS careClubName,IF(cb.id IS NOT NULL AND cb.branch_id > 0,cb.price,care.amount) AS amount FROM cart_details AS cart LEFT JOIN care_club AS care ON cart.care_club_id = care.care_club_id LEFT JOIN care_club_brach AS cb ON cart.care_club_id = cb.care_club_id AND cb.branch_id = cart.branch_id WHERE cart.care_club_id > 0 AND cart.user_id = $userId AND care.care_club_status = 1"; $query = $this->conn->prepare($sql); $query->execute(); $data = $query->fetchAll(PDO::FETCH_ASSOC); return $data; } function deleteCartItem($post) { $cartId = $post['cartId']; $sql = "SELECT care_club_id, branch_id FROM cart_details WHERE id=$cartId"; $query = $this->conn->prepare($sql); $query->execute(); $cartData = $query->fetch(PDO::FETCH_ASSOC); $careClubId = $cartData['care_club_id']; $branchId = $cartData['branch_id']; if ($careClubId > 0) { $sql = "SELECT count,alloted_count FROM care_club_brach WHERE care_club_id = $careClubId AND branch_id = $branchId"; $query = $this->conn->prepare($sql); $query->execute(); $countData = $query->fetch(PDO::FETCH_ASSOC); $alloted_count = $countData['alloted_count']; $count = $countData['count']; $newCount = $alloted_count - 1; $sql = "UPDATE care_club_brach SET alloted_count = $newCount WHERE care_club_id = $careClubId AND branch_id = $branchId"; $updateQuery = $this->conn->prepare($sql); $updateQuery->execute(); } $sql = "DELETE FROM cart_details WHERE id=$cartId"; $query = $this->conn->prepare($sql); $query->execute(); $deleteStatus = $query->rowCount(); return $deleteStatus; } function getCareClubs($branchId) { if ($branchId > 0) { $query = " SELECT c.care_club_id, c.name, c.total_services, cb.branch_id, cb.count, cb.services_1_year, cb.services_2_year, cb.services_3_year, cb.services_4_year, cb.services_5_year, cb.sedan_price_1_year, cb.sedan_price_2_year, cb.sedan_price_3_year, cb.sedan_price_4_year, cb.sedan_price_5_year, cb.muv_price_1_year, cb.muv_price_2_year, cb.muv_price_3_year, cb.muv_price_4_year, cb.muv_price_5_year, cb.suv_price_1_year, cb.suv_price_2_year, cb.suv_price_3_year, cb.suv_price_4_year, cb.suv_price_5_year, cb.hb_price_1_year, cb.hb_price_2_year, cb.hb_price_3_year, cb.hb_price_4_year, cb.hb_price_5_year FROM care_club c INNER JOIN care_club_brach cb ON c.care_club_id = cb.care_club_id WHERE cb.branch_id = :branchId AND c.delete_status = 0 AND cb.delete_status = 0 AND c.care_club_status = 1 "; $stmt = $this->conn->prepare($query); $stmt->bindValue(':branchId', $branchId, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } return []; } function checkCoupon($post) { $response = []; $couponValue = $post['couponValue']; $serviceAmount = $post['amount']; $selectSql = "SELECT coupon_id,usercount,usedusers,min_bill,amount,percent,cpn_type FROM coupons WHERE name = '$couponValue' AND delete_status=0"; $query = $this->conn->prepare($selectSql); $query->execute(); $couponData = $query->fetch(PDO::FETCH_ASSOC); if (empty($couponData)) { $response['status'] = 2; // Invalid Coupon return $response; } if ($couponData['usedusers'] == $couponData['usercount']) { $response['status'] = 0; // No Users Left return $response; } if ($serviceAmount < $couponData['min_bill']) { $response['status'] = 1; // Bill Amount is Less return $response; } if ($couponData['cpn_type'] == 1) { $response['couponAmount'] = $couponData['amount']; } else { $response['couponAmount'] = $serviceAmount * ($couponData['percent'] / 100); } $response['status'] = 3; $response['couponId'] = $couponData['coupon_id']; return $response; } function userCartAmount($post) { $userId = $post['userId']; $sql = "SELECT COALESCE(SUM(ser.amount),0) AS serviceAmount , COALESCE(SUM(pro.amount * cart.product_count), 0) AS productAmount, COALESCE(SUM(care.amount),0) AS careClubAmount, COALESCE(SUM(ser.amount), 0) + COALESCE(SUM(pro.amount * cart.product_count), 0) + COALESCE(SUM(care.amount), 0) AS totalAmount from cart_details AS cart LEFT JOIN services AS ser ON cart.service_id = ser.service_id LEFT JOIN products AS pro ON cart.product_id = pro.product_id LEFT JOIN care_club AS care ON cart.care_club_id=care.care_club_id WHERE cart.user_id = $userId"; $query = $this->conn->prepare($sql); $query->execute(); $data = $query->fetch(PDO::FETCH_ASSOC); return $data; } function getUserCareClubPurchagesData($post) { $user_id = $post['id']; $type = 4; $approveStatus = 1; $approveStatus1 = 4; $limit = $post['limit']; $offset = $post['offset']; $query = 'SELECT * From allotment WHERE user_id =:user_id AND type =:type AND (approve_status=:approve_status OR approve_status=:approve_status1) ORDER BY `id` DESC LIMIT :limit OFFSET :offset'; $selectQuery = $this->conn->prepare($query); $selectQuery->bindParam(':type', $type, PDO::PARAM_INT); $selectQuery->bindParam(':user_id', $user_id, PDO::PARAM_INT); $selectQuery->bindParam(':approve_status', $approveStatus, PDO::PARAM_INT); $selectQuery->bindParam(':approve_status1', $approveStatus1, PDO::PARAM_INT); $selectQuery->bindValue(':limit', $limit, PDO::PARAM_INT); $selectQuery->bindValue(':offset', $offset, PDO::PARAM_INT); $selectQuery->execute(); $userPacakgePurchagesData = $selectQuery->fetchAll(PDO::FETCH_ASSOC); return $userPacakgePurchagesData; } function getAllotmentDetails($id) { $allotmentId = $id; $query = 'SELECT * From allotment WHERE id =:id'; $selectQuery = $this->conn->prepare($query); $selectQuery->bindParam(':id', $allotmentId, PDO::PARAM_INT); $selectQuery->execute(); $allotmentDeatils = $selectQuery->fetch(PDO::FETCH_ASSOC); return $allotmentDeatils; } function getSingleCareClub($post) { $id = (int)$post['id']; $branchId = (int)$post['branchId']; $query = " SELECT c.name, c.total_services, cb.branch_id, cb.count, cb.alloted_count, cb.services_1_year, cb.services_2_year, cb.services_3_year, cb.services_4_year, cb.services_5_year, cb.sedan_price_1_year, cb.sedan_price_2_year, cb.sedan_price_3_year, cb.sedan_price_4_year, cb.sedan_price_5_year, cb.muv_price_1_year, cb.muv_price_2_year, cb.muv_price_3_year, cb.muv_price_4_year, cb.muv_price_5_year, cb.suv_price_1_year, cb.suv_price_2_year, cb.suv_price_3_year, cb.suv_price_4_year, cb.suv_price_5_year, cb.hb_price_1_year, cb.hb_price_2_year, cb.hb_price_3_year, cb.hb_price_4_year, cb.hb_price_5_year FROM care_club c INNER JOIN care_club_brach cb ON c.care_club_id = cb.care_club_id WHERE c.care_club_id = :id AND cb.branch_id = :branchId AND c.delete_status = 0 AND cb.delete_status = 0"; $stmt = $this->conn->prepare($query); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->bindParam(':branchId', $branchId, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetch(PDO::FETCH_ASSOC); } function getMyPackages($post) { $query = $this->conn->prepare('SELECT p.package_id, p.name, p.total_points, p.service_points, p.month, a.created_date, a.details, a.service_id, a.id, a.vechile_details FROM packages AS p JOIN allotment AS a ON p.package_id = a.package_id WHERE a.type = 1 AND a.user_id = :id ORDER BY `package_id` DESC LIMIT :limit OFFSET :offset'); $query->bindValue(':id', $post['id'], PDO::PARAM_INT); $query->bindValue(':limit', $post['limit'], PDO::PARAM_INT); $query->bindValue(':offset', $post['offset'], PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); $data['mypackages'] = $result; return $data; } function insertContactUs($post) { $name = $post['name']; $email = $post['email']; $phone = $post['phone']; $message = $post['message']; $sql = "INSERT INTO contact_us (`name`, `email`, `number`, `message`) VALUES (:name, :email, :phone, :message)"; $stmt = $this->conn->prepare($sql); $stmt->bindParam(':name', $name); $stmt->bindParam(':email', $email); $stmt->bindParam(':phone', $phone); $stmt->bindParam(':message', $message); $insertStatus = $stmt->execute(); return $insertStatus ? $stmt->rowCount() : 0; } function getSingleProductDetails($id) { $productId = $id; $query = 'SELECT * From products WHERE product_id =:product_id'; $selectQuery = $this->conn->prepare($query); $selectQuery->bindParam(':product_id', $productId, PDO::PARAM_INT); $selectQuery->execute(); $servicesData = $selectQuery->fetch(PDO::FETCH_ASSOC); $servicesData['image_name'] = $_ENV['base_url'] . 'uploads/products/' . $servicesData['image_name']; return $servicesData; } function addProductPayment($post) { $approveStatus = 0; $notificationStatus = 3; $selectedServicesIdsArray = isset($post['ServicesIds']) ? $post['ServicesIds'] : []; $selectedProductIds = isset($post['productIds']) ? $post['productIds'] : []; $productQuantity = isset($post['productQuantity']) ? $post['productQuantity'] : []; $paymentDetails = isset($post['paymentDetails']) ? json_encode($post['paymentDetails']) : []; $userId = $post['userId']; $couponId = $post['couponId']; $couponAmount = 0; $servicesLength = count($selectedServicesIdsArray); $productsLength = count($selectedProductIds); $paymentId = ''; // Getting Services Data of user seleced items $servicesNamesArray = array(); $servicesCostsArray = array(); // Initialize arrays for product names and costs $selectedProductNames = []; $selectedProductCosts = []; $selectedProductExpiry = []; $selectedProductDuration = []; $selectedProductWarrenty = []; $selectedProductWarrentyDuration = []; $servicesAmount = 0; for ($i = 0; $i < $servicesLength; $i++) { if (!empty($selectedServicesIdsArray[$i])) { $servicesDetails = $this->getSingleserviceDetails($selectedServicesIdsArray[$i]); $servicesNamesArray[] = $servicesDetails['name']; $servicesCostsArray[] = $servicesDetails['amount']; $servicesAmount = $servicesAmount + $servicesDetails['amount']; } } $productAmount = 0; for ($i = 0; $i < $productsLength; $i++) { if (!empty($selectedProductIds[$i])) { $productDetails = $this->getSingleProductDetails($selectedProductIds[$i]); $selectedProductNames[$i] = $productDetails['name']; $selectedProductCosts[$i] = $productDetails['amount'] * $productQuantity[$i]; $selectedProductExpiry[$i] = $productDetails['expiry']; $selectedProductDuration[$i] = $productDetails['duration']; $selectedProductWarrenty[$i] = $productDetails['warranty']; $selectedProductWarrentyDuration[$i] = $productDetails['warranty_duration_type']; $productAmount = $productAmount + $selectedProductCosts[$i]; } } $subamount = $servicesAmount + $productAmount; $amount = ($subamount * 0.18) + $subamount; $selectedServicesIds = json_encode($selectedServicesIdsArray); $productIds = json_encode($selectedProductIds); $jsonData = array(); $jsonData['selectedServicesIds'] = json_encode($selectedServicesIdsArray); $jsonData['servicesNames'] = json_encode($servicesNamesArray); $jsonData['servicesCosts'] = json_encode($servicesCostsArray); $jsonData['selectedProductIds'] = json_encode($selectedProductIds); $jsonData['productNames'] = json_encode($selectedProductNames); $jsonData['productQuantity'] = json_encode($productQuantity); $jsonData['productCosts'] = json_encode($selectedProductCosts); $jsonData['productExpiry'] = json_encode($selectedProductExpiry); $jsonData['productDuration'] = json_encode($selectedProductDuration); $jsonData['productWarrenty'] = json_encode($selectedProductWarrenty); $jsonData['productWarrentyDuration'] = json_encode($selectedProductWarrentyDuration); $jsonData['couponId'] = $couponId; $jsonData['couponAmount'] = $couponAmount; $purchaseDetails = serialize($jsonData); $totalServicesCount = count($servicesNamesArray); $vechileDetailsArray = []; for ($i = 0; $i < $totalServicesCount; $i++) { $vechileDetailsArray[$i]['status'] = 0; $vechileDetailsArray[$i]['serviceName'] = $servicesNamesArray[$i]; } $vechileDetailsString = serialize($vechileDetailsArray); // Admin Adding // if ($type == 1) { $invoiceQuery = 'SELECT MAX(invoice_id) as invoiceId From allotment WHERE approve_status = 1 OR approve_status = 4'; $invoiceQuery = $this->conn->prepare($invoiceQuery); $invoiceQuery->execute(); $invoiceData = $invoiceQuery->fetch(PDO::FETCH_ASSOC); $count = $invoiceQuery->rowCount(); if ($count > 0) { $invoiceId = $invoiceData['invoiceId'] + 1; } else { $invoiceId = 1; } $insertQuery = "INSERT INTO allotment (invoice_id,package_id,service_id,product_id,user_id,type,amount,details,vechile_details, care_club_details,care_club_vechile_details,approve_status,notification_status,remarks,payment_details,payment_id,coupon_id) VALUES ('$invoiceId','0','$selectedServicesIds','$productIds','$userId','3','$amount','$purchaseDetails', '$vechileDetailsString','','','$approveStatus','$notificationStatus','','$paymentDetails','$paymentId',$couponId)"; $insert = $this->conn->exec($insertQuery); $insertedId = $this->conn->lastInsertId(); $result = array(); $result['insert'] = $insert; $result['insertedId'] = $insertedId; $result['encryptedUserId'] = base64_encode($userId); return $result; } function addCareClubPayment($post, $type) { $approveStatus = 0; $notificationStatus = 3; $couponId = $post['couponId']; $userId = $post['userId']; $careClubIds = $post['careClubIds']; $careClubBranchIds = $post['careClubBranchIds']; $vehicletype = $post['vehicletype']; $vehicleNumber = $post['vehiclenumber']; $model = $post['model']; $paymentDetails = json_encode($post['paymentDetails']); $paymentId = ''; // Array to store results for each care club $results = []; // Loop through each care club ID and branch ID foreach ($careClubIds as $i => $careClubId) { $careClubBranchId = $careClubBranchIds[$i]; // Retrieve details for the current care club $careClubDetails = $this->getSingleCareClub(['id' => $careClubId, 'branchId' => $careClubBranchId]); // Get service details for the care club $careClubServiceDetails = unserialize($careClubDetails['service_details']); $serviceIds = array_keys($careClubServiceDetails); $serviceCounts = array_values($careClubServiceDetails); // Generate JSON strings for service IDs and counts $serviceIdsString = json_encode($serviceIds); $serviceCountsString = json_encode($serviceCounts); // Generate JSON string for service names $serviceNamesArray = []; $i = 0; foreach ($serviceIds as $id) { $serviceDeatils = $this->getSingleserviceDetails($id); $serviceNamesArray[$i] = $serviceDeatils['name']; $i = $i + 1; } // Here we are adding all service names with their count in the care club $serviceNameWithCountArray = []; foreach ($serviceCounts as $index => $count) { $name = $serviceNamesArray[$index]; $serviceNameWithCountArray = array_merge($serviceNameWithCountArray, array_fill(0, $count, $name)); } $serviceNames = json_encode($serviceNameWithCountArray); // Calculate amount for the care club $amount = (isset($careClubDetails['subAdminAmount']) && $careClubDetails['subAdminAmount'] != '') ? $careClubDetails['subAdminAmount'] : $careClubDetails['amount']; // Prepare details string $details = [ 'careClubName' => $careClubDetails['name'], 'serviceIds' => $serviceIdsString, 'serviceNames' => $serviceNames, 'serviceCount' => $serviceCountsString, 'vehicletype' => $vehicletype, 'vehicleNumber' => $vehicleNumber, 'model' => $model, 'careClubCost' => $amount, 'totalServices' => $careClubDetails['total_services'] ]; $amount += $amount * 0.18; // add tax $detailsString = serialize($details); // Generate vechile details array and string $vechileDetailsArray = []; for ($i = 0; $i < $details['totalServices']; $i++) { $vechileDetailsArray[$i]['status'] = 0; } $vechileDetailsString = serialize($vechileDetailsArray); // Insert data into database $invoiceQuery = 'SELECT MAX(invoice_id) as invoiceId From allotment WHERE approve_status = 1 OR approve_status = 4'; $invoiceQuery = $this->conn->prepare($invoiceQuery); $invoiceQuery->execute(); $invoiceData = $invoiceQuery->fetch(PDO::FETCH_ASSOC); $invoiceId = ($invoiceData['invoiceId']) ? $invoiceData['invoiceId'] + 1 : 1; $insertQuery = "INSERT INTO allotment SET invoice_id = :invoiceId, care_club_id = :careClubId, user_id = :userId, type = 4, amount = :amount, details = :details, vechile_details = :vechileDetails, care_club_details = :careClubDetails, care_club_vechile_details = :vechileDetails, approve_status = :approveStatus, notification_status = :notificationStatus, remarks = '',payment_details = :paymentDetails,payment_id = :paymentId,coupon_id = :couponId ,branch_id = :branchId"; $insertStmt = $this->conn->prepare($insertQuery); $insertStmt->bindParam(':invoiceId', $invoiceId, PDO::PARAM_INT); $insertStmt->bindParam(':careClubId', $careClubId, PDO::PARAM_INT); $insertStmt->bindParam(':userId', $userId, PDO::PARAM_INT); $insertStmt->bindParam(':amount', $amount, PDO::PARAM_INT); $insertStmt->bindParam(':details', $detailsString, PDO::PARAM_STR); $insertStmt->bindParam(':vechileDetails', $vechileDetailsString, PDO::PARAM_STR); $insertStmt->bindParam(':careClubDetails', $detailsString, PDO::PARAM_STR); $insertStmt->bindParam(':approveStatus', $approveStatus, PDO::PARAM_INT); $insertStmt->bindParam(':notificationStatus', $notificationStatus, PDO::PARAM_INT); $insertStmt->bindParam(':paymentDetails', $paymentDetails, PDO::PARAM_STR); $insertStmt->bindParam(':paymentId', $paymentId, PDO::PARAM_STR); $insertStmt->bindParam(':couponId', $couponId, PDO::PARAM_STR); $insertStmt->bindParam(':branchId', $careClubBranchId, PDO::PARAM_STR); $insertStmt->execute(); // Store result for the current care club $results[] = [ 'insert' => $insertStmt->rowCount(), 'insertedId' => $this->conn->lastInsertId() ]; } return $results; } public function clearCart($userId) { // Delete rows from cart_details table $deleteQuery = 'DELETE FROM cart_details WHERE user_id = :userId'; $deleteStmt = $this->conn->prepare($deleteQuery); $deleteStmt->bindParam(':userId', $userId, PDO::PARAM_INT); $deleteStmt->execute(); return $deleteStmt->rowCount(); } public function checkCart($userId) { $selectQuery = 'SELECT COUNT(*) as count FROM cart_details WHERE user_id = :userId'; $selectStmt = $this->conn->prepare($selectQuery); $selectStmt->bindParam(':userId', $userId, PDO::PARAM_INT); $selectStmt->execute(); $result = $selectStmt->fetch(PDO::FETCH_ASSOC); return $result['count']; } public function updateServiceDate($serviceName, $serviceDate, $allotmentId) { $query = $this->conn->prepare('SELECT * FROM allotment WHERE (type = 2 or type = 3) AND id = :id'); $query->bindValue(':id', $allotmentId, PDO::PARAM_INT); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); // Update the service date in the vehicle details foreach ($result as $rows) { $row = unserialize($rows['details']); $completedServiceDetails = unserialize($rows['vechile_details']); foreach ($completedServiceDetails as $key => $service) { if ($service['serviceName'] == $serviceName) { // $service['status'] = 1; $service['date'] = $serviceDate; $completedServiceDetails[$key] = $service; break; } } $vehicleDetailsString = serialize($completedServiceDetails); $updateQuery = 'UPDATE allotment SET vechile_details = :vehicleDetails WHERE id = :id'; $updateStmt = $this->conn->prepare($updateQuery); $updateStmt->bindParam(':vehicleDetails', $vehicleDetailsString, PDO::PARAM_STR); $updateStmt->bindParam(':id', $allotmentId, PDO::PARAM_INT); $updateStmt->execute(); if ($updateStmt->rowCount() > 0) { return true; } else { return false; } } } function getBranches() { $sql = "SELECT * FROM braches WHERE delete_status = 0"; $query = $this->conn->prepare($sql); $query->execute(); $data = $query->fetchAll(PDO::FETCH_ASSOC); return $data; } function allotmentDetails($id) { // Fetch the records that have the same created_at date with time $selectAllotmentsQuery = $this->conn->prepare('SELECT * FROM allotment WHERE DATE_FORMAT(created_date, "%Y-%m-%d %H:%i:%s") = (SELECT DATE_FORMAT(created_date, "%Y-%m-%d %H:%i:%s") FROM allotment WHERE id = :id)'); $selectAllotmentsQuery->bindParam(':id', $id, PDO::PARAM_INT); $selectAllotmentsQuery->execute(); $allotments = $selectAllotmentsQuery->fetchAll(PDO::FETCH_ASSOC); return $allotments; } function userDeatils($userId, $type = 1) { if ($type == 1) { $query = $this->conn->prepare('SELECT * FROM user WHERE id=:id '); $query->bindParam(':id', $userId, PDO::PARAM_INT); } else if ($type == 2) { $query = $this->conn->prepare('SELECT * FROM user WHERE user_id=:user_id '); $query->bindParam(':user_id', $userId, PDO::PARAM_STR); } $query->execute(); $count = $query->rowCount(); $queryData = array(); if ($count > 0) { $queryData = $query->fetch(PDO::FETCH_ASSOC); $queryData['status'] = 1; } else { $queryData['status'] = 0; } return $queryData; } function getCouponDetails($couponCode) { $query = $this->conn->prepare('SELECT * FROM coupons WHERE coupon_id = :coupon_code'); $query->bindParam(':coupon_code', $couponCode, PDO::PARAM_STR); $query->execute(); $queryData = $query->fetch(PDO::FETCH_ASSOC); return $queryData; } function branchDetails($branchId) { $query = $this->conn->prepare('SELECT * FROM braches WHERE brach_id = :branch_id'); $query->bindParam(':branch_id', $branchId, PDO::PARAM_INT); $query->execute(); $queryData = $query->fetch(PDO::FETCH_ASSOC); return $queryData; } //get service details based on id function getServiceById($serviceId) { $query = $this->conn->prepare('SELECT service_id,sedan_amount,muv_amount,suv_amount,hb_amount FROM services WHERE service_id = :service_id'); $query->bindParam(':service_id', $serviceId, PDO::PARAM_INT); $query->execute(); $queryData = $query->fetch(PDO::FETCH_ASSOC); return $queryData; } //get care club details based on branch and id function getCareClubDataById($careClubId, $branchId) { $query = $this->conn->prepare('SELECT * FROM care_club_brach WHERE branch_id = :branch_id AND care_club_id = :care_club_id and delete_status = 0'); $query->bindParam(':branch_id', $branchId, PDO::PARAM_INT); $query->bindParam(':care_club_id', $careClubId, PDO::PARAM_INT); $query->execute(); $queryData = $query->fetch(PDO::FETCH_ASSOC); return $queryData; } function checkSecretCoupon($post) { $response = []; $couponValue = $post['couponValue']; $serviceAmount = $post['amount']; $selectSql = "SELECT coupon_id,usercount,usedusers,min_bill,amount,percent,cpn_type FROM coupons WHERE name = '$couponValue' AND delete_status=0 and cpn_type = 2"; $query = $this->conn->prepare($selectSql); $query->execute(); $couponData = $query->fetch(PDO::FETCH_ASSOC); if (empty($couponData)) { $response['status'] = 2; // Invalid Coupon return $response; } if ($couponData['usedusers'] == $couponData['usercount']) { $response['status'] = 0; // No Users Left return $response; } if ($serviceAmount < $couponData['min_bill']) { $response['status'] = 1; // Bill Amount is Less return $response; } $response['status'] = 3; $response['couponId'] = $couponData['coupon_id']; $response['couponAmount'] = $serviceAmount * ($couponData['percent'] / 100); return $response; } /** * Retrieves the loyalty status and points of a given user */ function getLoyalUserDetails($userId) { $query = $this->conn->prepare('SELECT loyal_user,loyalty_points, referral_points FROM user WHERE user_id = :user_id'); $query->bindParam(':user_id', $userId, PDO::PARAM_STR); $query->execute(); $queryData = $query->fetch(PDO::FETCH_ASSOC); return $queryData; } }