<?php

	function reference($db, $user_id, $depostit_amount) {


		global $db;

		if(REFERENCE_STATUS) {

			$reference = $db->from('user_references')->where('user_id', $user_id)->first();
		
		
			if($reference) {

				$reference_user = $db->from('users')->where('user_id', $reference['reference_user_id'])->first();


				if($reference_user) {
					
					$amount = ($depostit_amount * REFERENCE_SHARE) / 100;
					$db->beginTransaction();
					$query = $db->prepare("UPDATE `users` SET user_balance = ? WHERE user_id = ?");
					$update_user = $query->execute([$reference_user["user_balance"] + $amount, $reference_user["user_id"]]);

					$query = $db->prepare("UPDATE `user_references` SET reference_earnings = ? WHERE reference_id = ?");
					$update_reference = $query->execute([$reference["reference_earnings"] + $amount, $reference["reference_id"]]);


					if($update_user && $update_reference) {
						$db->commit();
					} else {
						$db->rollBack();
					}
				}

			}

		}

	}