<commit_before><?php defined('SYSPATH') OR die('No direct script access.');

/**
 * @package    Openbuildings\Purchases
 * @author     <NAME> <<EMAIL>>
 * @copyright  (c) 2013 OpenBuildings Ltd.
 * @license    http://spdx.org/licenses/BSD-3-Clause
 */
class Kohana_Model_Store_Purchase extends Jam_Model implements Purchasable {

	/**
	 * @codeCoverageIgnore
	 */
	public static function initialize(Jam_Meta $meta)
	{
		$meta
			->name_key('number')
			->behaviors(array(
				'tokenable' => Jam::behavior('tokenable', array('uppercase' => TRUE, 'field' => 'number')),
				'paranoid' => Jam::behavior('paranoid'),
				'freezable' => Jam::behavior('freezable', array('associations' => 'items', 'parent' => 'purchase')),
			))
			->associations(array(
				'purchase' => Jam::association('belongsto', array(
					'inverse_of' => 'store_purchases'
				)),
				'items' => Jam::association('hasmany', array(
					'inverse_of' => 'store_purchase',
					'foreign_model' => 'purchase_item',
					'delete_on_remove' => Jam_Association::DELETE,
					'dependent' => Jam_Association::DELETE,
				)),
				'refunds' => Jam::association('hasmany', array(
					'inverse_of' => 'store_purchase',
					'foreign_model' => 'store_refund',
					'delete_on_remove' => Jam_Association::DELETE,
					'dependent' => Jam_Association::DELETE,
				)),
				'store' => Jam::association('belongsto', array(
					'inverse_of' => 'store_purchases',
				)),
			))
			->fields(array(
				'id' => Jam::field('primary'),
				'created_at'      => Jam::field('timestamp', array('auto_now_create' => TRUE, 'format' => 'Y-m-d H:i:s')),
				'updated_at'      => Jam::field('timestamp', array('auto_now_update' => TRUE, 'format' => 'Y-m-d H:i:s')),
			))
			->validator('purchase', 'store', array('present' => TRUE));
	}

	/**
	 * Search for the same item in items, (using "is_same()" method, and return its index, or NULL if not found)
	 * @param  Model_Purchase_Item $new_item
	 * @return integer
	 */
	public function search_same_item(Model_Purchase_Item $new_item)
	{
		foreach ($this->items as $index => $item)
		{
			if ($item->is_same($new_item))
			{
				return $index;
			}
		}
	}

	/**
	 * Add the item to items or update an existing one (checked using "search_same_item()")
	 * @param Model_Purchase_Item $new_item
	 * @return Model_Store_Purchase self
	 */
	public function add_or_update_item(Model_Purchase_Item $new_item)
	{
		if (($index = $this->search_same_item($new_item)) !== NULL)
		{
			$this->items[$index]->quantity += $new_item->quantity;
		}
		else
		{
			$this->items->add($new_item);
		}
		return $this;
	}<commit_msg>fix weird __set/__get bug with +=<commit_after><?php defined('SYSPATH') OR die('No direct script access.');

/**
 * @package    Openbuildings\Purchases
 * @author     <NAME> <<EMAIL>>
 * @copyright  (c) 2013 OpenBuildings Ltd.
 * @license    http://spdx.org/licenses/BSD-3-Clause
 */
class Kohana_Model_Store_Purchase extends Jam_Model implements Purchasable {

	/**
	 * @codeCoverageIgnore
	 */
	public static function initialize(Jam_Meta $meta)
	{
		$meta
			->name_key('number')
			->behaviors(array(
				'tokenable' => Jam::behavior('tokenable', array('uppercase' => TRUE, 'field' => 'number')),
				'paranoid' => Jam::behavior('paranoid'),
				'freezable' => Jam::behavior('freezable', array('associations' => 'items', 'parent' => 'purchase')),
			))
			->associations(array(
				'purchase' => Jam::association('belongsto', array(
					'inverse_of' => 'store_purchases'
				)),
				'items' => Jam::association('hasmany', array(
					'inverse_of' => 'store_purchase',
					'foreign_model' => 'purchase_item',
					'delete_on_remove' => Jam_Association::DELETE,
					'dependent' => Jam_Association::DELETE,
				)),
				'refunds' => Jam::association('hasmany', array(
					'inverse_of' => 'store_purchase',
					'foreign_model' => 'store_refund',
					'delete_on_remove' => Jam_Association::DELETE,
					'dependent' => Jam_Association::DELETE,
				)),
				'store' => Jam::association('belongsto', array(
					'inverse_of' => 'store_purchases',
				)),
			))
			->fields(array(
				'id' => Jam::field('primary'),
				'created_at'      => Jam::field('timestamp', array('auto_now_create' => TRUE, 'format' => 'Y-m-d H:i:s')),
				'updated_at'      => Jam::field('timestamp', array('auto_now_update' => TRUE, 'format' => 'Y-m-d H:i:s')),
			))
			->validator('purchase', 'store', array('present' => TRUE));
	}

	/**
	 * Search for the same item in items, (using "is_same()" method, and return its index, or NULL if not found)
	 * @param  Model_Purchase_Item $new_item
	 * @return integer
	 */
	public function search_same_item(Model_Purchase_Item $new_item)
	{
		foreach ($this->items as $index => $item)
		{
			if ($item->is_same($new_item))
			{
				return $index;
			}
		}
	}

	/**
	 * Add the item to items or update an existing one (checked using "search_same_item()")
	 * @param Model_Purchase_Item $new_item
	 * @return Model_Store_Purchase self
	 */
	public function add_or_update_item(Model_Purchase_Item $new_item)
	{
		if (($index = $this->search_same_item($new_item)) !== NULL)
		{
			$this->items[$index]->quantity = $this->items[$index]->quantity + 1;
		}
		else
		{
			$this->items->add($new_item);
		}
		return $this;
	}
{'fraction_non_alphanumeric': 0.1818876576232327, 'fraction_numerical': 0.0021016431027894534, 'mean_word_length': 4.043352601156069, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 96, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6068991', 'n_tokens_mistral': 2107, 'n_tokens_neox': 1802, 'n_words': 350}