<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class supports OSB payments.
 *
 * @package    Payment
 * @category   Models
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Model_Payment extends ORM_OSB {
	// Relationships
	protected $_belongs_to = array(
		'account'=>array(),
		'checkout'=>array('foreign_key'=>'checkout_id'),
	);
	protected $_has_many = array(
		'payment_item'=>array('far_key'=>'id'),
		'invoice'=>array('through'=>'payment_item'),
	);

	protected $_sorting = array(
		'date_payment'=>'DESC'
	);

	protected $_display_filters = array(
		'date_orig'=>array(
			array('Config::date',array(':value')),
		),
		'date_payment'=>array(
			array('Config::date',array(':value')),
		),
		'fees_amt'=>array(
			array('Currency::display',array(':value')),
		),
		'total_amt'=>array(
			array('Currency::display',array(':value')),
		),
	);

	// Items belonging to a payment
	protected $_sub_items_load = array(
		'payment_item'=>FALSE,
	);

	/**
	 * Add an item to an payment
	 */
	public function add_item(Model_Payment_Item $p=NULL) {
		$this->_sub_items_sorted = FALSE;

		// Make sure this payment item for an invoice is not already in the list
		foreach ($this->_sub_items as $pio)
			if ($pio->invoice_id == $p->invoice_id)
				return $pio;

		array_push($this->_sub_items,$p);

		return $p;
	}

	/**
	 * Calculate the remaining balance available for this payment
	 */
	public function balance($format=FALSE) {
		$result = $this->total();

		foreach ($this->subitems('ALLOC') as $pio)
			$result -= $pio->alloc_amt;

		return $format ? Currency::display($result) : Currency::round($result);
	}

	/**
	 * Calculate there refund amount
	 */
	public function credit($format=FALSE) {
		$result = 0;

		foreach ($this->subitems('CREDIT') as $pio)
			$result += $pio->alloc_amt*-1;

		return $format ? Currency::display($result) : Currency::round($result);
	}

	/**
	 * Return a list of invoices that this payment is applied to
	 */
	public function invoices() {
		$result = array();

		foreach ($this->_sub_items as $pio)
			array_push($result,$pio->invoice);

		return $result;
	}

	public function invoicelist() {
		return join(',',$this->invoices());
	}

	/**
	 * Return a list of payment items for this payment.
	 * @param type [ALLOC|CREDIT|ALL]
	 * @see payment_items
	 */
	public function subitems($type='ALL') {
		if (! $this->_sub_items_sorted) {
			Sort::MAsort($this->_sub_items,'invoice_id');
			$this->_sub_items_sorted = TRUE;
		}

		$result = array();

		foreach ($this->_sub_items as $pio) {
			$return = FALSE;

			switch ($type) {
				case 'ALLOC':
					if ($pio->alloc_amt >= 0)
						$return = TRUE;
					break;

				case 'CREDIT':
					if ($pio->alloc_amt < 0)
						$return = TRUE;
					break;

				case 'ALL':
				default:
					$return = TRUE;
					break;
			}

			if ($return)
				array_push($result,$pio);
		}

		return $result;
	}

	/**
	 * Save a record and payment_items
	 */
	public function save(Validation $validation = NULL) {
		// Our items will be clobbered once we save the object, so we need to save it here.
		$items = $this->subitems();

		// If a user modified this payment, we'll update the source to their ID.
		if ($ao = Auth::instance()->get_user())
			$this->source_id = $ao->id;

		$this->ip = Request::$client_ip;

		// Make sure we dont over allocate
		$msg = '';
		foreach ($items as $pio)
			// Only need to check items that ave actually changed.
			if ($pio->changed() AND ($x=$pio->original_values()))
				if (($x = $pio->alloc_amt-$pio->invoice->due()-$x['alloc_amt']) > 0)
					$msg .= ($msg ? ' ' : '').sprintf('Invoice %s over allocated by %3.2f.',$pio->invoice_id,$x);

		if (($x=$this->balance()) < 0)
			$msg .= ($msg ? ' ' : '').sprintf('Payment over allocated by %3.2f.',-$x);

		if ($msg) {
			SystemMessage::factory()
				->title('Record NOT updated')
				->type('warning')
				->body($msg);

			return FALSE;
		}

		// Save the payment
		parent::save($validation);

		// Need to save the associated items and their taxes
		if (! $this->changed() OR $this->saved()) {
			foreach ($items as $pio) {
				// Skip applying 0 payments to invoices.
				if (! $pio->changed() OR (Currency::round($pio->alloc_amt) == 0 AND ! $pio->loaded()))
					continue;

				$pio->payment_id = $this->id;

				// @todo Mark payment as cancelled and write a memo, then...
				if (! $pio->check())
					throw HTTP_Exception::factory(501,'Problem saving payment_item for invoice :invoice - Failed check()',array(':invoice'=>$pio->invoice_id));

				$pio->save();

				// @todo Mark payment as cancelled and write a memo, then...
				if (! $pio->saved())
					throw HTTP_Exception::factory(501,'Problem saving payment_item for invoice :invoice - Failed save()',array(':invoice'=>$pio->invoice_id));
			}

		} else {
			throw HTTP_Exception::factory(501,'Problem saving payment :id - Failed save()',array(':id'=>$this->id));
		}

		return $this;
	}

	/**
	 * Reduce the total by the amount of credits returned.
	 */
	public function total($format=FALSE) {
		$result = $this->total_amt - $this->credit();

		return $format ? Currency::display($result) : Currency::round($result);
	}

	/** LIST FUNCTIONS **/

	/**
	 * Show recent payments for this account
	 */
	public function list_recent_table() {
		return Table::factory()
			->data($this->limit(10)->find_all())
			->columns(array(
				'id'=>'ID',
				'date_payment'=>'Date',
				'checkout->display("name")'=>'Method',
				'total_amt'=>'Total',
				'balance(TRUE)'=>'Balance',
				'invoicelist()'=>'Invoices'
			));
	}

	public function list_unapplied() {
		$pid = array();

		// We cant use ORM for this complex SQL
		$db = Database::instance();

		$sql = 'SELECT A.id as id, A.total_amt, ROUND(SUM(IF(IFNULL(B.alloc_amt,0)<0,IFNULL(B.alloc_amt,0)*-1,IFNULL(B.alloc_amt,0))),2) as ALLOC';
		$sql .= ' FROM :prefix_payment A ';
		$sql .= ' LEFT JOIN :prefix_payment_item B ON (A.site_id=B.site_id AND A.id=B.payment_id)';
		$sql .= ' WHERE A.site_id=:site_id';
		$sql .= ' GROUP BY A.id';
		$sql .= ' HAVING round(A.total_amt-ALLOC,2) <> 0';

		foreach ($db->query(Database::SELECT,__($sql,array(':prefix_'=>$db->table_prefix(),':site_id'=>Config::siteid()))) as $values)
			array_push($pid,$values['id']);

		return $this->where('id','IN',$pid)->order_by('account_id')->find_all();
	}
}
?>
