<?php
/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2013 Fillip Hannisdal AKA Revan/NeoRevan/Belazor 	  # ||
|| # All Rights Reserved. 											  # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------------------------------------------------------- # ||
|| # You are not allowed to use this on your server unless the files  # ||
|| # you downloaded were done so with permission.					  # ||
|| # ---------------------------------------------------------------- # ||
|| #################################################################### ||
\*======================================================================*/

if (!is_object($vbulletin->db))
{
	exit;
}

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

if (defined('IN_CONTROL_PANEL'))
{
	define('ECHO_CRON_LOG', true);
}

if (!$vbulletin->options['dbtech_vbshop_manualinterest'])
{
	$users = $vbulletin->db->query_read_slave("
		SELECT *
		FROM " . TABLE_PREFIX . "dbtech_vbshop_bank
		WHERE points > 0
	");
	while ($user = $vbulletin->db->fetch_array($users))
	{
		$interest = doubleval(VBSHOP::$cache['currency'][$user['currencyid']]['interest']);
		if ($interest == 0)
		{
			// Not yielding any interest
			continue;
		}
		
		// Convert to multiplicable values
		$interest = ($interest / 100);
		
		$info = array(
			'currencyid' 	=> $user['currencyid'],
			'amount' 		=> round($user['points'] * $interest, 2),
		);
		
		// init data manager
		$dm =& VBSHOP::initDataManager('Transactionlog', $vbulletin, ERRTYPE_ARRAY);
			$dm->set('userid', 		$user['userid']);
			$dm->set('recipient', 	$user['userid']);
			$dm->set('ipaddress', 	'');
			$dm->set('action', 		'interest');
			$dm->set('info', 		$info);
		$dm->save();
		unset($dm);	
	}
	
	foreach (VBSHOP::$cache['currency'] as $currencyid => $currency)
	{
		$interest = doubleval($currency['interest']);
		if ($interest == 0)
		{
			// Not yielding any interest
			continue;
		}
		
		// Convert to multiplicable values
		$interest = 1 + ($interest / 100);
	
		// Update database
		$vbulletin->db->query_write("
			UPDATE " . TABLE_PREFIX . "dbtech_vbshop_bank
			SET 
				points = ROUND(points * $interest, 2),
				lastinterest = " . intval(TIMENOW) . "
			WHERE points > 0
				AND currencyid = $currencyid
		");
	}
}
	
log_cron_action('Bank Interest Processed', $nextitem);