<?php

defined('HOSTCMS') || exit('HostCMS: access denied.');

/**
 * Online shop.
 *
 * @package HostCMS 6\Shop
 * @version 6.x
 * @author Hostmake LLC
 * @copyright © 2005-2013 ООО "Хостмэйк" (Hostmake LLC), http://www.hostcms.ru
 */
class Shop_Price_Model extends Core_Entity
{
	/**
	 * One-to-many or many-to-many relations
	 * @var array
	 */
	protected $_hasMany = array(
		'shop_item' => array('through' => 'shop_item_price'),
		'shop_item_price' => array(),
	);

	/**
	 * Belongs to relations
	 * @var array
	 */
	protected $_belongsTo = array(
		'shop' => array(),
		'siteuser_group' => array()
	);

	/**
	 * List of preloaded values
	 * @var array
	 */
	protected $_preloadValues = array(
		'percent' => 0
	);

	/**
	 * Constructor.
	 * @param int $id entity ID
	 */
	public function __construct($id = NULL)
	{
		parent::__construct($id);

		if (is_null($id))
		{
			$oUserCurrent = Core_Entity::factory('User', 0)->getCurrent();
			$this->_preloadValues['user_id'] = is_null($oUserCurrent) ? 0 : $oUserCurrent->id;
			$this->_preloadValues['guid'] = Core_Guid::get();
		}
	}

	/**
	 * Get price by user group and shop
	 * @param int $siteuser_group_id user group id
	 * @param int $shop_id shop id
	 * @return Shop_Price_Model|NULL
	 */
	public function getBySiteuserGroupAndShop($siteuser_group_id, $shop_id)
	{
		$this->queryBuilder()
			//->clear()
			->where('siteuser_group_id', '=', $siteuser_group_id)
			->where('shop_id', '=', $shop_id)
			->limit(1);

		$aShop_Prices = $this->findAll();

		if (isset($aShop_Prices[0]))
		{
			return $aShop_Prices[0];
		}

		return NULL;
	}

	/**
	 * Delete object from database
	 * @param mixed $primaryKey primary key for deleting object
	 * @return Core_Entity
	 */
	public function delete($primaryKey = NULL)
	{
		if (is_null($primaryKey))
		{
			$primaryKey = $this->getPrimaryKey();
		}

		$this->id = $primaryKey;

		$aShop_Item_Prices = $this->Shop_Item_Prices->findAll(FALSE);
		foreach($aShop_Item_Prices as $oShop_Item_Price)
		{
			$oShop_Item_Price->delete();
		}

		return parent::delete($primaryKey);
	}

	/**
	 * Get price by guid
	 * @param string $guid guid
	 * @return Shop_Price_Model|NULL
	 */
	public function getByGuid($guid)
	{
		$this->queryBuilder()
			//->clear()
			->where('guid', '=', $guid)
			->limit(1);

		$aObjects = $this->findAll(FALSE);

		return isset($aObjects[0]) ? $aObjects[0] : NULL;
	}
}