<?php
// Store item ids
$itemids = array();
foreach ((array)$shoppingcart['items'] as $shopid => $items)
{
	foreach ($items as $itemid => $quantity)
	{
		// Store the itemid
		$itemids[] = $itemid;
	}
}

$allowsubmit = false;
if (count($itemids))
{
	$purchases = array();
	$purchases_q = $db->query_read_slave("
		SELECT *		
		FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase
		WHERE feature = 'item'
			AND userid = " . intval($vbulletin->userinfo['userid']) . "
	");
	while ($purchases_r = $db->fetch_array($purchases_q))
	{
		$purchases[$purchases_r['featureid']] = true;
	}
	
	// Store item info
	$iteminfo = array();
	$iteminfo_q = $db->query_read_slave("
		SELECT *		
		FROM " . TABLE_PREFIX . "dbtech_vbshop_shopinventory
		WHERE itemid IN(" . implode(',', $itemids) . ")
			AND active = 1
	");
	while ($iteminfo_r = $db->fetch_array($iteminfo_q))
	{
		// Index by shopid and itemid
		$iteminfo[$iteminfo_r['shopid']][$iteminfo_r['itemid']] = $iteminfo_r;
	}
	$db->free_result($iteminfo_q);
	unset($iteminfo_r);
	
	$userinfo = $vbulletin->userinfo;
	$cartitems = '';
	$totals = '';
	$totalprices = array();
	$allowsubmit = true;
	foreach ((array)$shoppingcart['items'] as $shopid => $items)
	{
		foreach ($items as $itemid => $quantity)
		{
			if ($purchases[$itemid] AND VBSHOP::$cache['item'][$itemid]['uniqueitem'])
			{
				// This item is unique, cannot re-buy
				continue;
			}
			
			if ($quantity > 1 AND VBSHOP::$cache['item'][$itemid]['uniqueitem'])
			{
				// This item is unique
				$quantity = 1;
			}
			
			if (!$currency = VBSHOP::$cache['currency'][$iteminfo[$shopid][$itemid]['currencyid']])
			{
				// This currency doesn't exist anymore
				continue;
			}
			
			// Store total prices
			$totalprices[$currency['currencyid']] += ($iteminfo[$shopid][$itemid]['price'] * $quantity);
			
			 // Ensure this is set
			$iteminfo[$shopid][$itemid]['currency'] = $currency['title'];
			$iteminfo[$shopid][$itemid]['quantity'] = $quantity;
			
			if ($iteminfo[$shopid][$itemid]['stock'] > -1 AND $iteminfo[$shopid][$itemid]['stock'] < $quantity)
			{
				// Not enough of these items
				continue;
			}				
							
			// Decrement our available points
			$userinfo[$currency['pointscolumn']] -= ($iteminfo[$shopid][$itemid]['price'] * $quantity);
			
			if ($userinfo[$currency['pointscolumn']] < 0)
			{
				// We can't afford this
				$allowsubmit = false;
			}
			
			$templater = vB_Template::create('dbtech_vbshop_checkout_itembit');
				$templater->register('item', array_merge(VBSHOP::$cache['item'][$itemid], $iteminfo[$shopid][$itemid]));
				$templater->register('price', ($iteminfo[$shopid][$itemid]['price'] * $quantity));
			$cartitems .= $templater->render();
		}
	}
	
	foreach ($totalprices as $currencyid => $price)
	{
		$templater = vB_Template::create('dbtech_vbshop_checkout_totalbit');
			$templater->register('currency', VBSHOP::$cache['currency'][$currencyid]['title']);
			$templater->register('price', $price);
		$totals .= $templater->render();
	}
}
else
{
	// Just create a plain array
	$shoppingcart['items'] = array();
}

$templater = vB_Template::create('dbtech_vbshop_checkout_cartcontents');
	$templater->register('cartitems', 	$cartitems);
	$templater->register('totals', 		$totals);
	$templater->register('allowsubmit', $allowsubmit);
$cartcontents = $templater->render();
?>