<?php

function BackendClearEarnings($coinid = NULL)
{
//	debuglog(__FUNCTION__);

	if(defined('VEXON_EARNINGS_CLEAR_DELAY'))
		$delay = time() - max(0, intval(VEXON_EARNINGS_CLEAR_DELAY));
	else if (VEXON_ALLOW_EXCHANGE)
		$delay = time() - (int) VEXON_PAYMENTS_FREQ;
	else
		$delay = time() - (VEXON_PAYMENTS_FREQ / 2);
	$total_cleared = 0.0;

	$sqlFilter = $coinid ? " AND coinid=".intval($coinid) : '';

	$list = getdbolist('db_earnings', "status=1 AND mature_time<$delay $sqlFilter");
	foreach($list as $earning)
	{
		$user = getdbo('db_accounts', $earning->userid);
		if(!$user)
		{
			$earning->delete();
			continue;
		}

		$coin = getdbo('db_coins', $earning->coinid);
		if(!$coin)
		{
			$earning->delete();
			continue;
		}

		$earning->status = 2;		// cleared
		$earning->price = $coin->price;
		$earning->save();

// 		$refcoin = getdbo('db_coins', $user->coinid);
// 		if($refcoin && $refcoin->price<=0) continue;
// 		$value = $earning->amount * $coin->price / ($refcoin? $refcoin->price: 1);

		$value = vexon_convert_amount_user($coin, $earning->amount, $user);

		if($user->coinid == 6 && !VEXON_ALLOW_EXCHANGE)
			continue;

		$user->balance += $value;
		$user->save();

		if($user->coinid == 6)
			$total_cleared += $value;
	}

	if($total_cleared>0)
	 	debuglog("total cleared from mining $total_cleared BTC");
}
