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

/**
 * This class provides checkout capabilities.
 *
 * @package    OSB
 * @subpackage Checkout
 * @category   Models
 * @author     Deon George
 * @copyright  (c) 2010 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Model_Checkout extends ORMOSB {
	protected $_has_many = array(
		'account'=>array('through'=>'account_billing','foreign_key'=>'checkout_plugin_id'),
		'payment'=>array(),
	);

	/**
	 * Give a cart, this will present the available checkout options
	 *
	 * Trial Products are NEW products
	 * Cart items are NEW products
	 * Invoice items are RE-OCCURING items (ie: carts are not re-occuring)
	 *
	 */
	public function payment_options_cart() {
		$cart = Cart::instance();

		$available_payments = array();

		if ($cart->has_trial())
			$this->and_where('allow_trial','=',TRUE);

		$this->and_where('allow_new','=',TRUE);

		foreach ($this->where('active','=',TRUE)->find_all() as $item) {
			// Check that the cart total meets the minimum requirement
			if ($item->total_minimum AND $cart->total() < $item->total_minimum)
				continue;

			// Check the cart total meets the maximum requirement
			if (($item->total_maximum AND $cart->total() > $item->total_maximum) OR ($item->total_maximum == '0' AND $cart->total()))
				continue;

			// Check that the payment option is available to this client based on groups
			// @todo Enable this test

			// Check that the payment option is not prohibited by an SKU item
			// @todo Enable this test

			// Check if this payment method is a default payment method
			// @todo Enable this test
			// By Amount
			// By Currency
			// By Group
			// By Country

			// This payment option is valid
			array_push($available_payments,$item);
			// Sort the checkout options
			// @todo Is this required?
		}

		return $available_payments;
	}
}
?>
