/** * Create a transaction with mirror entry for the other user if mobile number matches a user * @param int $user_id Current user ID * @param int $person_id Person ID * @param string $type Transaction type ('lena' or 'dena') * @param float $amount Transaction amount * @param string $date_time Transaction date and time * @param string $remark Transaction remark * @return int|bool Returns the transaction ID if successful, false otherwise */ function create_transaction_with_mirror($user_id, $person_id, $type, $amount, $date_time, $remark) { global $conn; // Check if required parameters are valid if (!$user_id || !$person_id || !in_array($type, ['lena', 'dena']) || !is_numeric($amount) || $amount <= 0) { error_log("Invalid parameters for create_transaction_with_mirror: user_id=$user_id, person_id=$person_id, type=$type, amount=$amount"); return false; } // Start a transaction to ensure data consistency $conn->begin_transaction(); try { // 1. Get the person's mobile number $query = "SELECT mobile, name FROM persons WHERE id = ?"; $stmt = $conn->prepare($query); $stmt->bind_param("i", $person_id); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows == 0) { throw new Exception("Person not found with ID: $person_id"); } $person_data = $result->fetch_assoc(); $person_mobile = $person_data['mobile']; $person_name = $person_data['name']; // 2. Create the original transaction // Simpler version that works with all database schemas $query = "INSERT INTO transactions (user_id, person_id, type, amount, date_time, remark) VALUES (?, ?, ?, ?, ?, ?)"; $stmt = $conn->prepare($query); $stmt->bind_param("iisdss", $user_id, $person_id, $type, $amount, $date_time, $remark); if (!$stmt->execute()) { throw new Exception("Failed to create transaction: " . $conn->error); } $transaction_id = $conn->insert_id; // 3. Check if there's a user with this mobile number $query = "SELECT id, name FROM users WHERE mobile = ?"; $stmt = $conn->prepare($query); $stmt->bind_param("s", $person_mobile); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { // We found a user with this mobile number $other_user = $result->fetch_assoc(); $other_user_id = $other_user['id']; // 4. Get current user's details $current_user_query = "SELECT mobile, name FROM users WHERE id = ?"; $current_user_stmt = $conn->prepare($current_user_query); $current_user_stmt->bind_param("i", $user_id); $current_user_stmt->execute(); $current_user_result = $current_user_stmt->get_result(); if ($current_user_result->num_rows == 0) { throw new Exception("Current user not found: $user_id"); } $current_user = $current_user_result->fetch_assoc(); $current_user_mobile = $current_user['mobile']; $current_user_name = $current_user['name']; // 5. Find if current user exists in other user's contacts $other_person_query = "SELECT id FROM persons WHERE user_id = ? AND mobile = ?"; $other_person_stmt = $conn->prepare($other_person_query); $other_person_stmt->bind_param("is", $other_user_id, $current_user_mobile); $other_person_stmt->execute(); $other_person_result = $other_person_stmt->get_result(); if ($other_person_result->num_rows > 0) { // Current user exists in other user's contacts $other_person_id = $other_person_result->fetch_assoc()['id']; } else { // Add current user to other user's contacts $insert_person_query = "INSERT INTO persons (user_id, name, mobile) VALUES (?, ?, ?)"; $insert_person_stmt = $conn->prepare($insert_person_query); $insert_person_stmt->bind_param("iss", $other_user_id, $current_user_name, $current_user_mobile); if (!$insert_person_stmt->execute()) { throw new Exception("Failed to add person to contacts: " . $conn->error); } $other_person_id = $conn->insert_id; } // 6. Create mirror transaction for other user // If original user has a "lena", other user gets a "dena" and vice versa $other_type = ($type == 'lena') ? 'dena' : 'lena'; $mirror_query = "INSERT INTO transactions (user_id, person_id, type, amount, date_time, remark) VALUES (?, ?, ?, ?, ?, ?)"; $mirror_stmt = $conn->prepare($mirror_query); $mirror_stmt->bind_param("iisdss", $other_user_id, $other_person_id, $other_type, $amount, $date_time, $remark); if (!$mirror_stmt->execute()) { throw new Exception("Failed to create mirror transaction: " . $conn->error); } } // Commit transaction $conn->commit(); return $transaction_id; } catch (Exception $e) { // Rollback transaction on error $conn->rollback(); error_log("Transaction error: " . $e->getMessage()); return false; } } Sign Up - Lena Dena

Lena Dena

Create a new account

Already have an account? Login