<?php
class vBShop_Item_Core
{
	/**
	* The vBulletin registry
	*
	* @protected	vBulletin
	*/	
	protected $registry = NULL;
	
	/**
	* The configuration array
	*
	* @public		array
	*/	
	public $config = array();
	
	/**
	* The purchase info array
	*
	* @protected	array
	*/	
	protected $purchaseinfo = array();
	
	
	/**
	* The constructor
	*
	* @param	vBulletin	vBulletin registry
	* @param	array		Item info
	*/	
	public function __construct(&$registry, &$item)
	{
		$this->registry =& $registry;
		$this->config =& $item;

		$this->config['active'] = (VBSHOP::$cache[$item['feature']][$item['featureid']]['active'] ? $this->config['active'] : 0);
			
		if (!is_array($this->config['code']))
		{
			// Ensure this is an array
			$this->config['code'] = array();
		}
		
		if (!is_array($this->config['permissions']))
		{
			// Ensure this is an array
			$this->config['permissions'] = array();
		}
		
		if (!is_array($this->config['configuration']))
		{
			// Ensure this is an array
			$this->config['configuration'] = @unserialize($this->config['configuration']);
			
			if (!is_array($this->config['configuration']))
			{
				// Ensure this is an array
				$this->config['configuration'] = array();
			}
		}
		
		// Set shop type
		$this->config['quantity'] = ($this->config['quantity'] ? $this->config['quantity'] : 1);
	}
	
	/**
	* Function to call before every action
	*/	
	public function action()
	{
		if (!$this->config['active'])
		{
			// This type is inactive
			//return false;
		}
		
		// We made it!
		return true;
	}
	
	/**
	* Form to purchase an item
	*/	
	public function render()
	{
		global $vbphrase;
		
		if (!$this->config['purchasetemplate'])
		{
			// Didn't have configuration
			return '';
		}
		
		// Begin the purchase template
		$templater = vB_Template::create($this->config['purchasetemplate']);
			$templater->register('item', $this->config);
		return $templater->render();
	}
	
	/**
	* Form to purchase an item
	*/	
	public function renderConfig()
	{
		global $vbphrase;

		$values = array();
		foreach ($this->config['code'] as $style => &$value)
		{
			$output = $value;
			if (is_numeric($value))
			{
				if ($value == 0)
				{
					$output = $vbphrase['no'];
				}
				else if ($value == 1)
				{
					$output = $vbphrase['yes'];
				}				
			}
			else if (!$value)
			{
				$output = 'N/A';
			}
			else
			{
				$output = $value = preg_replace('/[^A-Za-z0-9 #(),]/', '', $value);
			}
			
			// Set the values
			$values[$style] = $vbphrase['dbtech_vbshop_' . $style] . ': ' . $output;
		}

		foreach ($this->config['configuration'] as $style => &$value)
		{
			$output = $value;
			if (is_numeric($value))
			{
				if ($value == 0)
				{
					$output = $vbphrase['no'];
				}
				else if ($value == 1)
				{
					$output = $vbphrase['yes'];
				}				
			}
			else if (!$value)
			{
				$output = 'N/A';
			}
			else
			{
				$output = $value = preg_replace('/[^A-Za-z0-9 #(),]/', '', $value);
			}
			
			// Set the values
			$values[$style] = $vbphrase['dbtech_vbshop_' . $style] . ': ' . $output;
		}

		return implode("\n", $values);
	}
	
	/**
	* Purchasing an item
	*
	* @param	array		User info
	*/	
	public function purchase(&$buyer)
	{
		global $vbphrase;
		
		if (!$buyer['userid'])
		{
			// We not doin dis
			return false;
		}
		
		if (!$currency = VBSHOP::$cache['currency'][$this->config['currencyid']])
		{
			// This currency doesn't exist anymore
			return false;
		}
				
		if (!$this->check_permissions($buyer))
		{
			// Check permissions failed
			return false;
		}
		
		if ($this->config['stock'] > -1 AND $this->config['stock'] < $this->config['quantity'])
		{
			// Not enough of these items
			return false;
		}

		if (!$shop = VBSHOP::$cache['shop'][$this->config['shopid']])
		{
			// Wrong shop
			return false;
		}
		
		// Run pre-purchase checks that other actions may need to override
		$this->pre_purchase($buyer);
		
		for ($i = 0; $i < $this->config['quantity']; $i++)
		{
			if ($this->config['price'] > 0)
			{
				$remaining = $this->registry->db->query_first_slave("
					SELECT " . $currency['pointscolumn'] . " AS points 
					FROM " . TABLE_PREFIX . $currency['pointstable'] . "
					WHERE userid = " . intval($buyer['userid'])
				);
				
				if ($remaining['points'] < $this->config['price'])
				{
					// Can't afford this
					return false;
				}

				// We're done with the error checking
				$this->registry->db->query_write("
					UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
					SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " - " . doubleval($this->config['price']) . "
					WHERE userid = " . intval($buyer['userid'])
				);
				$buyer[$currency['pointscolumn']] -= doubleval($this->config['price']);
				
				if ($shop['beneficiary'] > 0 OR ($shop['beneficiary'] == -1 AND $shop['ownerid']))
				{
					$price = $remaining = $this->config['price'];
					if ($shop['beneficiary'] > 0)
					{
						// This means we have a beneficiary
						$price = ($price / 100) * $shop['beneficiary_split'];
						$remaining -= $price;

						if ($price)
						{
							// Shop beneficiary gains a sale
							$this->registry->db->query_write("
								UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
								SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " + " . doubleval($price) . "
								WHERE userid = " . intval($shop['beneficiary'])
							);

							if ($shop['beneficiary'] == $buyer['userid'])
							{
								$buyer[$currency['pointscolumn']] += doubleval($price);
							}
							
							$info = array(
								'feature' 		=> 'item',
								'featureid' 	=> $this->config['itemid'],
								'shopid' 		=> $this->config['shopid'],
								'currencyid' 	=> $this->config['currencyid'],
								'price' 		=> $price
							);
							
							// init data manager
							$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
								$dm->set('userid', 		$buyer['userid']);
								$dm->set('recipient', 	$shop['beneficiary']);
								$dm->set('ipaddress', 	IPADDRESS);
								$dm->set('action', 		'sale');
								$dm->set('info', 		$info);
							$dm->save();
							unset($dm);
						}

						if ($shop['ownerid'] AND $remaining > 0)
						{
							// Shop owner gains a sale
							$this->registry->db->query_write("
								UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
								SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " + " . doubleval($remaining) . "
								WHERE userid = " . intval($shop['ownerid'])
							);

							if ($shop['ownerid'] == $buyer['userid'])
							{
								$buyer[$currency['pointscolumn']] += doubleval($price);
							}

							$info = array(
								'feature' 		=> 'item',
								'featureid' 	=> $this->config['itemid'],
								'shopid' 		=> $this->config['shopid'],
								'currencyid' 	=> $this->config['currencyid'],
								'price' 		=> $remaining
							);
							
							// init data manager
							$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
								$dm->set('userid', 		$buyer['userid']);
								$dm->set('recipient', 	$shop['ownerid']);
								$dm->set('ipaddress', 	IPADDRESS);
								$dm->set('action', 		'sale');
								$dm->set('info', 		$info);
							$dm->save();
							unset($dm);
						}
					}
					else
					{
						// Beneficiary is Shop Owner
						$price = ($price / 100) * $shop['beneficiary_split'];
						if ($shop['ownerid'] AND $price > 0)
						{
							// Shop owner gains a sale
							$this->registry->db->query_write("
								UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
								SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " + " . doubleval($price) . "
								WHERE userid = " . intval($shop['ownerid'])
							);

							if ($shop['ownerid'] == $buyer['userid'])
							{
								$buyer[$currency['pointscolumn']] += doubleval($price);
							}
							
							$info = array(
								'feature' 		=> 'item',
								'featureid' 	=> $this->config['itemid'],
								'shopid' 		=> $this->config['shopid'],
								'currencyid' 	=> $this->config['currencyid'],
								'price' 		=> $price
							);
							
							// init data manager
							$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
								$dm->set('userid', 		$buyer['userid']);
								$dm->set('recipient', 	$shop['ownerid']);
								$dm->set('ipaddress', 	IPADDRESS);
								$dm->set('action', 		'sale');
								$dm->set('info', 		$info);
							$dm->save();
							unset($dm);
						}
					}
				}
			}
			
			// Grant the purchase to the user
			$this->registry->db->query_write("
				INSERT INTO " . TABLE_PREFIX . "dbtech_vbshop_purchase
					(userid, buyer, feature, featureid, shopid, dateline, active)
				VALUES (
					" . $buyer['userid'] . ",
					" . $buyer['userid'] . ",
					'item',
					" . $this->config['itemid'] . ",
					" . $this->config['shopid'] . ",
					" . TIMENOW . ",
					'0'
				)
			");
			$purchaseid = $this->registry->db->insert_id();			
			
			$info = array(
				'feature' 		=> 'item',
				'featureid' 	=> $this->config['itemid'],
				'shopid' 		=> $this->config['shopid'],
				'currencyid' 	=> $this->config['currencyid'],
				'price' 		=> $this->config['price']
			);
			
			// init data manager
			$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
				$dm->set('userid', 		$buyer['userid']);
				$dm->set('recipient', 	$buyer['userid']);
				$dm->set('ipaddress', 	IPADDRESS);
				$dm->set('action', 		'purchase');
				$dm->set('info', 		$info);
			$dm->save();
			unset($dm);	
			
			// Grab this
			$salesamounts = VBSHOP::$cache['shop'][$this->config['shopid']]['salesamounts'];
			$salesamounts[$this->config['currencyid']] += $this->config['price'];
			
			// init data manager
			$dm =& VBSHOP::initDataManager('Item', $this->registry, ERRTYPE_ARRAY);
				$dm->set_existing(VBSHOP::$cache['item'][$this->config['itemid']]);
				$dm->set('purchases', 'purchases + 1', false);
			$dm->save();
			unset($dm);	
			
			// init data manager
			$dm =& VBSHOP::initDataManager('Shop', $this->registry, ERRTYPE_ARRAY);
				$dm->set_existing(VBSHOP::$cache['shop'][$this->config['shopid']]);
				$dm->set('sales', 'sales + 1', false);
				$dm->set('latestsale', $this->config['itemid']);
				$dm->set('latestcustomer', $buyer['userid']);
				$dm->set('salesamounts', $salesamounts);
			$dm->save();
			unset($dm);	
			
			if ($existing = $this->registry->db->query_first_slave("
				SELECT * 
				FROM " . TABLE_PREFIX . "dbtech_vbshop_shopinventory
				WHERE itemid = " . $this->config['itemid'] . "
					AND shopid = " . $this->config['shopid'] . "
					AND stock > 0
			"))
			{
				// init data manager
				$dm =& VBSHOP::initDataManager('Shopinventory', $this->registry, ERRTYPE_ARRAY);
					$dm->set_existing($existing);
					$dm->set('stock', 'stock - 1', false);
				$dm->save();
				unset($dm);	
			}
		}
		
		// Rebuild the cache
		VBSHOP::build_purchases($buyer);
		
		if ($this->config['notifications'])
		{
			$userids = explode(',', $this->config['notifications']);
			foreach ((array)$userids as $userid)
			{
				// Send PM
				VBSHOP::sendPM(
					$userid,
					construct_phrase($vbphrase['dbtech_vbshop_new_sale_x'],
						$this->config['title']
					),
					construct_phrase($vbphrase['dbtech_vbshop_x_just_bought_y'],
						$buyer['username'],
						$this->config['title'],
						$this->config['quantity']
					),
					$buyer['username'],
					$buyer['userid']
				);
			}
		}
		
		// Activate the item
		$this->config['_activateoverride'] = true;
		$this->activate($purchaseid);
		
		// And we're done.
		return $purchaseid;
	}
	
	/**
	* Gifting an item
	*
	* @param	integer		Purchase ID
	* @param	array		Buyer info
	* @param	array		Recipient info
	* @param	string		The message from the buyer
	*/	
	public function gift($purchaseid, &$buyer, &$recipient, $message = '')
	{
		global $vbphrase;
		
		if (!$this->can_gift($purchaseid))
		{
			// Can't gift this item
			return false;
		}
		
		$purchases = array();
		$purchases_q = $this->registry->db->query_read_slave("
			SELECT *		
			FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase
			WHERE feature = 'item'
				AND userid = " . intval($recipient['userid']) . "
		");
		while ($purchases_r = $this->registry->db->fetch_array($purchases_q))
		{
			$purchases["$purchases_r[featureid]"] = true;
		}
		
		if ($this->config['uniqueitem'] AND $purchases[$this->config['itemid']])
		{
			// Already bought it
			eval(standard_error(fetch_error('dbtech_vbshop_error_x', $vbphrase['dbtech_vbshop_unique_item_gifterror'])));
		}		
		
		// Send a PM about the gift
		VBSHOP::sendPM(
			$recipient['userid'],
			construct_phrase($vbphrase['dbtech_vbshop_new_gift_from_x'],
				$buyer['username']
			),
			construct_phrase($vbphrase['dbtech_vbshop_new_gift_from_x_body'],
				$buyer['username'],
				$this->config['title'],
				$this->config['description'],
				$message
			),
			$buyer['username'],
			$buyer['userid']
		);
		
		// When transferring the purchase, ensure the item is set to inactive
		$this->registry->db->query_write("
			UPDATE " . TABLE_PREFIX . "dbtech_vbshop_purchase
			SET 
				hidden = '0',
				userid = " . intval($recipient['userid']) . ",
				buyer = " . intval($buyer['userid']) . ",
				configuration = NULL,
				message = " . $this->registry->db->sql_prepare($message) . ",
				feedbackstatus = 2
			WHERE purchaseid = " . intval($purchaseid)
		);
		
		$info = array(
			'feature' 		=> 'item',
			'featureid' 	=> $this->config['itemid'],
			'message' 		=> $message
		);
		
		// init data manager
		$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
			$dm->set('userid', 		$buyer['userid']);
			$dm->set('recipient', 	$recipient['userid']);
			$dm->set('ipaddress', 	IPADDRESS);
			$dm->set('action', 		'gift');
			$dm->set('info', 		$info);
		$dm->save();
		unset($dm);	
		
		// Activate item on gift
		$this->config['_isgift'] = true;
		$this->activate($purchaseid, $recipient);	
		
		if (method_exists($this, 'configure'))
		{
			// Configure the item
			$this->config['_overrideconfigerror'] = true;
			$this->configure($purchaseid, $recipient);
		}
		
		// Rebuild the cache
		VBSHOP::build_purchases($buyer);
		VBSHOP::build_purchases($recipient);
		
		return true;
	}
	
	/**
	* Activate an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function activate($purchaseid, $recipient = NULL, $dorebuild = true)
	{
		if (!$this->can_activate($purchaseid))
		{
			// We can't activate this item
			return false;
		}
		
		// Ensure this is set
		$recipient = ($recipient === NULL ? $this->registry->userinfo : $recipient);
		
		// Update flag
		$this->registry->db->query_write("
			UPDATE " . TABLE_PREFIX . "dbtech_vbshop_purchase
			SET active = '1'
			WHERE purchaseid = " . intval($purchaseid) . "
		");
		
		if ($dorebuild)
		{
			// Build purchase cache
			VBSHOP::build_purchases($recipient);
		}
		
		return true;
	}
	
	/**
	* Deactivate an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function deactivate($purchaseid, $recipient = null, $dorebuild = true)
	{
		if (!$this->can_deactivate($purchaseid))
		{
			// Cannot deactivate
			return false;
		}
		
		// Ensure this is set
		$recipient = ($recipient === NULL ? $this->registry->userinfo : $recipient);
		
		// Update flag
		$this->registry->db->query_write("
			UPDATE " . TABLE_PREFIX . "dbtech_vbshop_purchase
			SET active = '0'
			WHERE purchaseid = " . intval($purchaseid) . "
		");
		
		if ($dorebuild)
		{
			// Build purchase cache
			VBSHOP::build_purchases($recipient);
		}
		
		return true;
	}
		
	/**
	* Sell back an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function sell($purchaseid, $recipient = null)
	{
		if (!$this->can_sell($purchaseid))
		{
			// Can't sell this item
			return false;
		}
		
		if (!$currency = VBSHOP::$cache['currency'][$this->config['currencyid']])
		{
			// This currency doesn't exist anymore
			return false;
		}

		if (!$shop = VBSHOP::$cache['shop'][$this->config['shopid']])
		{
			// Shop doesn't exist
			return false;
		}
		
		// Ensure this is set
		$recipient = ($recipient === NULL ? $this->registry->userinfo : $recipient);
		
		// Ensure this can be deactivated
		$this->config['_deactivateoverride'] = true;
		
		// Deactivate the item
		$this->deactivate($purchaseid, $recipient);
		
		if ($this->config['buybackreplenish'])
		{
			if ($existing = $this->registry->db->query_first_slave("
				SELECT * 
				FROM " . TABLE_PREFIX . "dbtech_vbshop_shopinventory
				WHERE itemid = " . $this->config['itemid'] . "
					AND shopid = " . $this->config['shopid'] . "
					AND stock > -1
			"))
			{
				// init data manager
				$dm =& VBSHOP::initDataManager('Shopinventory', $this->registry, ERRTYPE_ARRAY);
					$dm->set_existing($existing);
					$dm->set('stock', 'stock + 1', false);
				$dm->save();
				unset($dm);	
			}
		}
		
		// We're done with the error checking
		$this->registry->db->query_write("
			UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
			SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " + " . doubleval($this->config['buybackprice']) . "
			WHERE userid = " . intval($this->purchaseinfo['userid'])
		);

		if ($shop['beneficiary'] > 0 OR ($shop['beneficiary'] == -1 AND $shop['ownerid']))
		{
			$price = $remaining = $this->config['buybackprice'];
			if ($shop['beneficiary'] > 0)
			{
				// This means we have a beneficiary
				$price = ($price / 100) * $shop['beneficiary_split'];
				$remaining -= $price;

				if ($price)
				{
					$pointsRemaining = $this->registry->db->query_first_slave("
						SELECT " . $currency['pointscolumn'] . " AS points 
						FROM " . TABLE_PREFIX . $currency['pointstable'] . "
						WHERE userid = " . intval($shop['beneficiary'])
					);
					
					if ($pointsRemaining['points'] < $price)
					{
						// Can't afford to pay back
						$difference = $price - $pointsRemaining['points'];
						
						// Blank out our points
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
							SET " . $currency['pointscolumn'] . " = 0
							WHERE userid = " . intval($shop['beneficiary'])
						);

						// Take the difference from bank as we KNOW this can go into minus
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . "dbtech_vbshop_bank
							SET points = points - " . doubleval($difference) . "
							WHERE userid = " . intval($shop['beneficiary']) . "
								AND currencyid = " . intval($currency['currencyid'])
						);
					}
					else
					{
						// Beneficiary could afford to pay back
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
							SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " - " . doubleval($price) . "
							WHERE userid = " . intval($shop['beneficiary'])
						);				
					}
					
					$info = array(
						'feature' 		=> 'item',
						'featureid' 	=> $this->config['itemid'],
						'shopid' 		=> $this->config['shopid'],
						'currencyid' 	=> $this->config['currencyid'],
						'price' 		=> $price
					);
					
					// init data manager
					$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
						$dm->set('userid', 		$this->purchaseinfo['userid']);
						$dm->set('recipient', 	$shop['beneficiary']);
						$dm->set('ipaddress', 	IPADDRESS);
						$dm->set('action', 		'sellback');
						$dm->set('info', 		$info);
					$dm->save();
					unset($dm);
				}

				if ($shop['ownerid'] AND $remaining > 0)
				{
					$pointsRemaining = $this->registry->db->query_first_slave("
						SELECT " . $currency['pointscolumn'] . " AS points 
						FROM " . TABLE_PREFIX . $currency['pointstable'] . "
						WHERE userid = " . intval($shop['ownerid'])
					);
					
					if ($pointsRemaining['points'] < $remaining)
					{
						// Can't afford to pay back
						$difference = $remaining - $pointsRemaining['points'];
						
						// Blank out our points
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
							SET " . $currency['pointscolumn'] . " = 0
							WHERE userid = " . intval($shop['ownerid'])
						);

						// Take the difference from bank as we KNOW this can go into minus
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . "dbtech_vbshop_bank
							SET points = points - " . doubleval($difference) . "
							WHERE userid = " . intval($shop['ownerid']) . "
								AND currencyid = " . intval($currency['currencyid'])
						);
					}
					else
					{
						// Beneficiary could afford to pay back
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
							SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " - " . doubleval($remaining) . "
							WHERE userid = " . intval($shop['ownerid'])
						);				
					}
					
					$info = array(
						'feature' 		=> 'item',
						'featureid' 	=> $this->config['itemid'],
						'shopid' 		=> $this->config['shopid'],
						'currencyid' 	=> $this->config['currencyid'],
						'price' 		=> $remaining
					);
					
					// init data manager
					$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
						$dm->set('userid', 		$this->purchaseinfo['userid']);
						$dm->set('recipient', 	$shop['ownerid']);
						$dm->set('ipaddress', 	IPADDRESS);
						$dm->set('action', 		'sellback');
						$dm->set('info', 		$info);
					$dm->save();
					unset($dm);
				}
			}
			else
			{
				// Beneficiary is Shop Owner
				$price = ($price / 100) * $shop['beneficiary_split'];
				if ($shop['ownerid'] AND $price > 0)
				{
					$pointsRemaining = $this->registry->db->query_first_slave("
						SELECT " . $currency['pointscolumn'] . " AS points 
						FROM " . TABLE_PREFIX . $currency['pointstable'] . "
						WHERE userid = " . intval($shop['ownerid'])
					);
					
					if ($pointsRemaining['points'] < $price)
					{
						// Can't afford to pay back
						$difference = $price - $pointsRemaining['points'];
						
						// Blank out our points
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
							SET " . $currency['pointscolumn'] . " = 0
							WHERE userid = " . intval($shop['ownerid'])
						);

						// Take the difference from bank as we KNOW this can go into minus
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . "dbtech_vbshop_bank
							SET points = points - " . doubleval($difference) . "
							WHERE userid = " . intval($shop['ownerid']) . "
								AND currencyid = " . intval($currency['currencyid'])
						);
					}
					else
					{
						// Beneficiary could afford to pay back
						$this->registry->db->query_write("
							UPDATE " . TABLE_PREFIX . $currency['pointstable'] . "
							SET " . $currency['pointscolumn'] . " = " . $currency['pointscolumn'] . " - " . doubleval($price) . "
							WHERE userid = " . intval($shop['ownerid'])
						);				
					}
					
					$info = array(
						'feature' 		=> 'item',
						'featureid' 	=> $this->config['itemid'],
						'shopid' 		=> $this->config['shopid'],
						'currencyid' 	=> $this->config['currencyid'],
						'price' 		=> $price
					);
					
					// init data manager
					$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
						$dm->set('userid', 		$this->purchaseinfo['userid']);
						$dm->set('recipient', 	$shop['ownerid']);
						$dm->set('ipaddress', 	IPADDRESS);
						$dm->set('action', 		'sellback');
						$dm->set('info', 		$info);
					$dm->save();
					unset($dm);
				}
			}
		}
		
		$this->registry->db->query_write("
			DELETE FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase
			WHERE purchaseid = " . intval($purchaseid) . "
		");
		
		// Build purchase cache
		VBSHOP::build_purchases($recipient);		
		
		return true;
	}
	
	/**
	* Discard an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Recipient
	*/	
	public function discard($purchaseid, $recipient = null)
	{
		if (!$this->can_discard($purchaseid))
		{
			// We can't discard this item
			return false;
		}
		
		// Ensure this is set
		$recipient = ($recipient === NULL ? $this->registry->userinfo : $recipient);		
		
		// Ensure this can be deactivated
		$this->config['_deactivateoverride'] = true;
		
		// Deactivate the item
		$this->deactivate($purchaseid);
		
		$info = array(
			'feature' 		=> 'item',
			'featureid' 	=> $this->config['itemid'],
			'shopid' 		=> $this->config['shopid'],
		);
		
		// init data manager
		$dm =& VBSHOP::initDataManager('Transactionlog', $this->registry, ERRTYPE_ARRAY);
			$dm->set('userid', 		$this->purchaseinfo['userid']);
			$dm->set('recipient', 	$this->purchaseinfo['userid']);
			$dm->set('ipaddress', 	IPADDRESS);
			$dm->set('action', 		'discard');
			$dm->set('info', 		$info);
		$dm->save();
		unset($dm);		
		
		$this->registry->db->query_write("
			DELETE FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase
			WHERE purchaseid = " . intval($purchaseid) . "
		");
		
		// Build purchase cache
		VBSHOP::build_purchases($recipient);		
		
		return true;
	}
	
	
	
	
	
	/**
	* Whether we can configure an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Purchase info
	*/		
	public function can_configure($purchaseid, $purchase = null)
	{
		if (!$this->config['_overrideconfigerror'])
		{
			if (!$this->config['reconfigure'] AND ($this->config['configuration'] !== NULL AND count($this->config['configuration'])))
			{
				// Cannot configure this item
				return false;
			}	
			
			if ($this->config['onlygiftable'] AND !$this->config['_configureoverride'])
			{
				// Cannot configure this item
				return false;
			}
			
			if (($this->config['duration'] > 0 AND TIMENOW >= ($this->config['dateline'] + ($this->config['duration'] * 86400))) AND !$this->config['_configureoverride'])
			{
				// Cannot activate this item
				return false;
			}
		}
		
		return true;		
	}
	
	/**
	* Whether we can activate an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Purchase info
	*/		
	public function can_activate($purchaseid, $purchase = null)
	{
		if ($this->config['onlygiftable'] AND !$this->config['_isgift'])
		{
			// Cannot activate this item
			return false;
		}
		
		if (($this->config['duration'] > 0 AND TIMENOW >= ($this->config['dateline'] + ($this->config['duration'] * 86400))) AND !$this->config['_activateoverride'])
		{
			// Cannot activate this item
			return false;
		}
		
		return true;		
	}
	
	/**
	* Whether we can deactivate an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Purchase info
	*/		
	public function can_deactivate($purchaseid, $purchase = null)
	{
		if ($this->config['onlygiftable'] AND !$this->config['_isgift'])
		{
			// Cannot deactivate this item
			return false;
		}

		if ($this->config['_nodeactivate'] AND !$this->config['_deactivateoverride'])
		{
			// Cannot deactivate
			return false;
		}
		
		return true;		
	}
	
	/**
	* Whether we can gift an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Purchase info
	*/	
	public function can_gift($purchaseid, $purchase = null)
	{
		if (!$this->config['giftable'])
		{
			// Not giftable
			return false;
		}
		
		if (!is_array($purchase))
		{
			if (!$this->purchaseinfo = $this->registry->db->query_first_slave("
				SELECT *
				FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase
				WHERE purchaseid = " . intval($purchaseid)
			))
			{
				// Purchase didn't exist
				return false;
			}
		}
		else
		{
			// Set purchase info
			$this->purchaseinfo = $purchase;
		}
		
		if (($this->purchaseinfo['userid'] != $this->purchaseinfo['buyer']) AND !$this->config['regift'])
		{
			// Re-gifting gifts? You horrible person.
			return false;
		}
		
		return true;
	}
	
	/**
	* Whether we can sell back an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Purchase info
	*/	
	public function can_sell($purchaseid, $purchase = null)
	{
		if ($this->config['_nosellback'])
		{
			// Cannot sellback
			return false;
		}
		
		if (!$currency = VBSHOP::$cache['currency'][$this->config['currencyid']])
		{
			// This currency doesn't exist anymore
			return false;
		}		
		
		if (!is_array($purchase))
		{
			if (!$this->purchaseinfo = $this->registry->db->query_first_slave("
				SELECT *
				FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase
				WHERE purchaseid = " . intval($purchaseid)
			))
			{
				// Purchase didn't exist
				return false;
			}
		}
		else
		{
			// Set purchase info
			$this->purchaseinfo = $purchase;
		}
		
		if ($this->purchaseinfo['userid'] != $this->purchaseinfo['buyer'])
		{
			// Selling gifts? You horrible person.
			return false;
		}
		
		if (!$this->config['buybackprice'])
		{
			// Store isn't buying back this item
			return false;
		}
		
		if ($this->config['buybacktime'] AND (TIMENOW - $this->purchaseinfo['dateline']) > ($this->config['buybacktime'] * 3600))
		{
			// Too late
			return false;
		}

		if (!$this->config['shopid'])
		{
			// Wrong shop
			return false;
		}
		
		return true;		
	}
	
	/**
	* Whether we can discard an item
	*
	* @param	integer		Purchase ID
	* @param	mixed		Purchase info
	*/	
	public function can_discard($purchaseid, $purchase = null)
	{
		if (!is_array($purchase))
		{
			if (!$this->purchaseinfo = $this->registry->db->query_first_slave("
				SELECT *
				FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase
				WHERE purchaseid = " . intval($purchaseid)
			))
			{
				// Purchase didn't exist
				return false;
			}
		}
		else
		{
			// Set purchase info
			$this->purchaseinfo = $purchase;
		}
		
		return true;		
	}
	
	/**
	* Check if we have permissions to buy an item
	*
	* @param	array		User info
	*/	
	public function check_permissions(&$user)
	{
		// Wrapper
		return VBSHOP::checkPermissions($user, $this->config['permissions']);
	}
	
	/**
	* Purchasing an item
	*/	
	protected function pre_purchase() {}	
}
?>