<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_Refund extends Jam_Model {

	const REFUNDED = 'refunded';

	/**
	 * @codeCoverageIgnore
	 */
	public static function initialize(Jam_Meta $meta)
	{
		$meta
			->behaviors(array(
				'paranoid' => Jam::behavior('paranoid'),
			))
			->associations(array(
				'store_purchase' => Jam::association('belongsto', array('inverse_of' => 'refunds')),
				'items' => Jam::association('hasmany', array('inverse_of' => 'store_refund', 'foreign_model' => 'store_refund_item')),
			))
			->fields(array(
				'id' => Jam::field('primary'),
				'status' => Jam::field('string'),
				'raw_response' => Jam::field('serialized', array('method' => 'json')),
				'reason' => Jam::field('string'),
				'created_at' => Jam::field('timestamp', array('auto_now_create' => TRUE, 'format' => 'Y-m-d H:i:s')),
			))
			->validator('store_purchase', array('present' => TRUE));
	}

	/**
	 * Do not allow refunding more than the given price
	 */
	public function validate()
	{
		if ($this->total_amount() > $this->store_purchase->total_price(array('is_payable' => TRUE))) 
		{
			$this->errors()->add('items', 'amount_more_than_store_purchase_price');
		}
	}

	public function has_purchase_item(Model_Purchase_Item $purchase_item)
	{
		foreach ($this->items->as_array() as $item) 
		{
			if ($item->purchase_item_id == $purchase_item->id() AND $item->is_full_amount()) 
			{
				return TRUE;
			}
		}

		return FALSE;
	}

	/**
	 * Total amount to be refunded
	 * @return Jam_Price
	 */
	public function total_amount()
	{
		if ( ! count($this->items))
		{
			return $this->store_purchase->total_price(array('is_payable' => TRUE));
		}
		else
		{
			$amounts = array_map(function($item) { return $item->amount(); }, $this->items->as_array());
			
			return Jam_Price::sum($amounts, $this->currency(), $this->monetary(), $this->display_currency());
		}
	}

	public function currency()
	{
		return $this->get_insist('store_purchase')->currency();
	}

	public function display_currency()
	{
		return $this->get_insist('store_purchase')->display_currency();	
	}

	public function monetary()
	{
		return $this->get_insist('store_purchase')->monetary();
	}

	/**
	 * Call payment->refund
	 * @throws Kohana_Exception If payment is not "paid"
	 * @return Model_Store_refund self
	 */
	public function execute()
	{
		$this->check_insist();

		$payment = $this->payment_insist();

		if ($payment->status !== Model_Payment::PAID)
			throw new Kohana_Exception('Payment must be payed in order to perform a refund');

		$payment->refund($this);

		return $this;
	}

	public function purchase_insist()
	{
		return $this
			->get_insist('store_purchase')
				->get_insist('purchase');
	}

	public function payment_insist()
	{
		return $this
			->purchase_insist()
				->get_insist('payment');
	}
}<commit_msg>Improve indentation of closure function<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_Refund extends Jam_Model {

	const REFUNDED = 'refunded';

	/**
	 * @codeCoverageIgnore
	 */
	public static function initialize(Jam_Meta $meta)
	{
		$meta
			->behaviors(array(
				'paranoid' => Jam::behavior('paranoid'),
			))
			->associations(array(
				'store_purchase' => Jam::association('belongsto', array('inverse_of' => 'refunds')),
				'items' => Jam::association('hasmany', array('inverse_of' => 'store_refund', 'foreign_model' => 'store_refund_item')),
			))
			->fields(array(
				'id' => Jam::field('primary'),
				'status' => Jam::field('string'),
				'raw_response' => Jam::field('serialized', array('method' => 'json')),
				'reason' => Jam::field('string'),
				'created_at' => Jam::field('timestamp', array('auto_now_create' => TRUE, 'format' => 'Y-m-d H:i:s')),
			))
			->validator('store_purchase', array('present' => TRUE));
	}

	/**
	 * Do not allow refunding more than the given price
	 */
	public function validate()
	{
		if ($this->total_amount() > $this->store_purchase->total_price(array('is_payable' => TRUE))) 
		{
			$this->errors()->add('items', 'amount_more_than_store_purchase_price');
		}
	}

	public function has_purchase_item(Model_Purchase_Item $purchase_item)
	{
		foreach ($this->items->as_array() as $item) 
		{
			if ($item->purchase_item_id == $purchase_item->id() AND $item->is_full_amount()) 
			{
				return TRUE;
			}
		}

		return FALSE;
	}

	/**
	 * Total amount to be refunded
	 * @return Jam_Price
	 */
	public function total_amount()
	{
		if ( ! count($this->items))
		{
			return $this->store_purchase->total_price(array('is_payable' => TRUE));
		}
		else
		{
			$amounts = array_map(function($item) {
				return $item->amount();
			}, $this->items->as_array());
			
			return Jam_Price::sum($amounts, $this->currency(), $this->monetary(), $this->display_currency());
		}
	}

	public function currency()
	{
		return $this->get_insist('store_purchase')->currency();
	}

	public function display_currency()
	{
		return $this->get_insist('store_purchase')->display_currency();	
	}

	public function monetary()
	{
		return $this->get_insist('store_purchase')->monetary();
	}

	/**
	 * Call payment->refund
	 * @throws Kohana_Exception If payment is not "paid"
	 * @return Model_Store_refund self
	 */
	public function execute()
	{
		$this->check_insist();

		$payment = $this->payment_insist();

		if ($payment->status !== Model_Payment::PAID)
			throw new Kohana_Exception('Payment must be payed in order to perform a refund');

		$payment->refund($this);

		return $this;
	}

	public function purchase_insist()
	{
		return $this
			->get_insist('store_purchase')
				->get_insist('purchase');
	}

	public function payment_insist()
	{
		return $this
			->purchase_insist()
				->get_insist('payment');
	}
}
{'fraction_non_alphanumeric': 0.17902143675339552, 'fraction_numerical': 0.0016363933889707086, 'mean_word_length': 4.241852487135506, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 121, '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': '805672', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2091, 'n_words': 392}