<?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);
}

$expired_q = $vbulletin->db->query_read_slave("
	SELECT
		user.*,
		purchase.*,
		shopinventory.*
	FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase AS purchase
	LEFT JOIN " . TABLE_PREFIX . "dbtech_vbshop_item AS item ON (item.itemid = purchase.featureid)
	LEFT JOIN " . TABLE_PREFIX . "dbtech_vbshop_shopinventory AS shopinventory ON (
		shopinventory.itemid = item.itemid AND
		shopinventory.shopid = purchase.shopid
	)
	LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = purchase.userid)
	WHERE purchase.feature = 'item'
		AND item.duration > 0
		AND (UNIX_TIMESTAMP() >= (purchase.dateline + (item.duration * 86400)))
		AND purchase.active = '1'
");
while ($expired_r = $vbulletin->db->fetch_array($expired_q))
{
	// Construct the array proper
	$expired_r = array_merge(VBSHOP::$cache['item'][$expired_r['itemid']], $expired_r);
	
	// Init the item
	$itemobj = VBSHOP::init_item($expired_r);
	
	// Deactivate the item
	$itemobj->deactivate($expired_r['purchaseid'], $expired_r);

	log_cron_action('Expired item: ' . $expired_r['title'] . ' from user: ' . $expired_r['username'], $nextitem);	
}

log_cron_action('Items Deactivated', $nextitem);