<?php

require_once 'includes/config.php';
$error=false;
$errors=array();


    $users=mysqli_query($con,"SELECT * FROM users WHERE status=1 ");
	while($user=mysqli_fetch_array($users)){
		
		$credits=$user['credits'];
		//$setting=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM member_topup_settings WHERE id=1"));
		$setting=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM member_commission_settings WHERE id=1"));
		$points=0;
		$voucher=0.00;
		
		if($credits<=10000){ 
			$points=($setting['normal_points']/100)*$user['point_wallet'];
			$voucher=($setting['normal_voucher']/100)*$user['point_wallet'];
		
		}elseif($credits<=100000 && $credits>50000){
			$points=($setting['vip_points']/100)*$user['point_wallet'];
			$voucher=($setting['vip_voucher']/100)*$user['point_wallet'];
		}elseif($credits<=1000000 && $credits>100000){
			$points=($setting['platinum_points']/100)*$user['point_wallet'];
			$voucher=($setting['platinum_voucher']/100)*$user['point_wallet'];
		}
		//$points=$points-$voucher;
		$point_wallet=$user['point_wallet']-round($voucher,2);
		$voucher_wallet=$user['vouchers']+round($voucher,2);
		$createpoints=mysqli_query($con,"INSERT INTO points_logs (user_id,credits,points,voucher,difference,created_at) VALUES(".$user['id'].",'".$credits."','".$point_wallet."','".$voucher_wallet."',".round($voucher,2).",'".date('Y-m-d H:i:s')."')");
		$createvouchers=mysqli_query($con,"INSERT INTO vouchers_logs (user_id,credits,points,voucher,difference,created_at) VALUES(".$user['id'].",'".$credits."','".$point_wallet."','".$voucher_wallet."',".round($voucher,2).",'".date('Y-m-d H:i:s')."')");
		
		
		//$points=$user['point_wallet']-round($voucher,2);
		//$voucher=$user['vouchers']+round($voucher,2);
	
		$updateCustomer=mysqli_query($con,"UPDATE users SET credits='".$credits."',vouchers='".$voucher_wallet."',point_wallet='".$point_wallet."' WHERE id=".$user['id']);
		
	}
	
	echo "Earning cron Run Successfully";

?>
 