Cart work for payments and Paypal work to test

This commit is contained in:
Deon George
2013-01-15 17:07:54 +11:00
parent 133ae4d5c6
commit 69645c4eea
42 changed files with 968 additions and 801 deletions

View File

@@ -11,8 +11,14 @@
* @license http://dev.leenooks.net/license.html
*/
class Cart {
public static function instance() {
return new Cart;
private $id = NULL;
public function __construct($id=NULL) {
$this->id = is_null($id) ? Session::instance()->id() : $id;
}
public static function instance($id=NULL) {
return new Cart($id);
}
/**
@@ -20,7 +26,34 @@ class Cart {
*/
public function contents() {
return ORM::factory('Cart')
->where('session_id','=',Session::instance()->id());
->where('session_id','=',$this->id)
->find_all();
}
public function delete() {
foreach (ORM::factory('Cart')->where('session_id','=',$this->id)->find_all() as $co)
$co->delete();
}
public function get($mid,$item) {
return ORM::factory('Cart')
->where('session_id','=',$this->id)
->and_where('module_id','=',$mid)
->and_where('module_item','=',$item)
->find_all();
}
public function id() {
return $this->id;
}
public function total($format=FALSE) {
$total = 0;
foreach ($this->contents() as $cio)
$total += $cio->item()->t;
return $format ? Currency::display($total) : $total;
}
/**
@@ -29,8 +62,10 @@ class Cart {
* @param bool $detail List a detailed cart or a summary cart
*/
public function cart_block() {
// @todo To implement.
return '';
// If the cart is empty, we'll return here.
if (! $this->contents()->count_all())
if (! count($this->contents()))
return 'The cart is empty.';
Style::add(array(
@@ -39,7 +74,7 @@ class Cart {
));
$output = '<table class="cart_blocklist" border="0">';
foreach ($this->contents()->find_all() as $item) {
foreach ($this->contents() as $item) {
$ppa = $item->product->get_price_array();
$pdata = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);
@@ -58,48 +93,5 @@ class Cart {
return $output;
}
/**
* Test to see if the cart has some trial options
*
* @return boolean
*/
public function has_trial() {
foreach ($this->contents()->find_all() as $item)
if ($item->product->is_trial())
return TRUE;
return FALSE;
}
public function subtotal() {
$total = 0;
foreach ($this->contents()->find_all() as $item) {
$ppa = $item->product->get_price_array();
$period = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);
$total += $item->quantity*$ppa[$item->recurr_schedule]['price_base']*$period['prorata'];
$total += $item->quantity*$ppa[$item->recurr_schedule]['price_setup'];
}
return $total;
}
/**
* Calculate Tax for the cart items
*
* @return unknown_type
* @uses Tax
*/
public function tax() {
// @todo Tax zone should come from somewhere else
return Tax::detail(61,NULL,$this->subtotal());
}
public function total() {
// @todo Tax zone should come from somewhere else
return $this->subtotal()+Tax::total(61,NULL,$this->subtotal());
}
}
?>

View File

@@ -0,0 +1,37 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides a Cart Item
*
* @package OSB
* @subpackage Cart
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Cart_Item {
// Quantity
private $q = 0;
// Item
private $i = 0;
// Total
private $t = 0;
public function __construct($q,$i,$t) {
$this->q = $q;
$this->i = $i;
$this->t = $t;
}
public function __get($key) {
switch($key) {
case 'i':
case 'q': return $this->{$key};
case 't': return Currency::display($this->{$key});
default: throw new Kohana_Exception('Unknown Key :key',array(':key',$key));
}
}
}
?>

View File

@@ -0,0 +1,19 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This interface ensures that all cart processing objects have the right methods
*
* @package OSB
* @subpackage Cart
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
interface Cartable {
// Render a cart line item.
public function cart_item();
// Return if this invoice is already in the cart
public function cart_exists();
}
?>

View File

@@ -12,71 +12,56 @@
*/
class Controller_Cart extends Controller_TemplateDefault {
/**
* Default action when called
* List the cart contents
*/
public function action_index() {
return $this->action_list();
}
/**
* List items in the cart
*/
public function action_list() {
// @todo - this should be a global config item
$mediapath = Route::get('default/media');
$output = '';
$co = Cart::instance();
// If the cart is empty, we'll return here.
if (! Cart::instance()->contents()->count_all())
if (! count($co->contents()))
Block::add(array(
'title'=>_('Empty Cart'),
'body'=>_('The cart is empty')
));
else {
Style::add(array(
'type'=>'file',
'data'=>'css/cart_contents.css',
));
Block::add(array(
'title'=>_('Cart Items'),
'body'=>Table::display(
$co->contents(),
NULL,
array(
'item()->q'=>array('label'=>'Quantity'),
'item()->i'=>array('label'=>'Item'),
'item()->t'=>array('label'=>'Total','class'=>'right'),
),
array(
'type'=>'list',
)
),
));
$output = Form::open('checkout/noready');
foreach (Cart::instance()->contents()->find_all() as $item) {
$ppa = $item->product->get_price_array();
$pdata = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);
$checkout = ORM::factory('Checkout')->where_active()->find_all()->as_array();
$price_box = View::factory('cart/list_pricebox')
->set('price_recurring',Currency::display($item->quantity*$ppa[$item->recurr_schedule]['price_base']))
->set('price_firstinvoice',Currency::display($item->quantity*$ppa[$item->recurr_schedule]['price_base']*$pdata['prorata']))
->set('price_setup',Currency::display($item->quantity*$ppa[$item->recurr_schedule]['price_setup']))
->set('item',$item)
->set('mediapath',$mediapath);
foreach ($co->contents() as $cio)
$checkout = array_intersect($checkout,$cio->checkout()->as_array());
$output .= View::factory('cart/list_item')
->set('price_box',$price_box)
->set('service_start',$pdata['date'])
->set('service_end',$pdata['end'])
->set('price_recurring',Currency::display($item->quantity*$ppa[$item->recurr_schedule]['price_base']))
->set('item',$item)
->set('mediapath',$mediapath);
$payopt = array();
foreach ($checkout as $cko)
$payopt[$cko->id] = $cko->name;
// If we are a plugin product, we might need more information
// @todo If an admin, show a system message if cart_info doesnt exist.
if ($item->product->prod_plugin AND method_exists($item->product->prod_plugin_file,'product_cart') AND Kohana::find_file('views',sprintf('%s/cart_info',strtolower($item->product->prod_plugin_file)))) {
$output .= View::factory(sprintf('%s/cart_info',strtolower($item->product->prod_plugin_file)));
// @todo JS validation will need to verify data before submission
}
}
$output .= '<div>'.Form::submit('submit',_('Checkout')).'</div>';
$output .= _('Total amount due for payment').' '.$co->total(TRUE);
$output .= Form::open('checkout/before');
$output .= Form::select('checkout_id',$payopt);
$output .= Form::submit('submit',_('Checkout'));
$output .= Form::close();
Block::add(array(
'title'=>_('Your Items'),
'title'=>_('Payment'),
'body'=>$output,
));
}
// Suppress our right hand tab
$this->template->right = ' ';
}
/**
@@ -87,13 +72,10 @@ class Controller_Cart extends Controller_TemplateDefault {
$cart->session_id = Session::instance()->id();
if (Auth::instance()->logged_in())
$cart->account_id = Auth::instance()->get_user()->id;
if ($cart->values($_POST)->check())
if ($cart->values(Request::current()->post())->check())
$cart->save();
else
echo Kohana::debug($cart->validate()->errors());
throw new Kohana_Exception('Unable to add to cart');
if ($cart->saved())
HTTP::redirect('cart/index');
@@ -102,10 +84,8 @@ class Controller_Cart extends Controller_TemplateDefault {
}
public function action_empty() {
$cart = ORM::factory('Cart')
->where('session_id','=',session_id());
$cart->delete_all();
foreach (ORM::factory('Cart')->where('session_id','=',Session::instance()->id())->find_all() as $co)
$co->delete();
$this->template->content = _('Cart Emptied');
}

View File

@@ -18,6 +18,10 @@ class Model_Cart extends ORM_OSB {
// Cart doesnt use the update column
protected $_updated_column = FALSE;
protected $_serialize_column = array(
'module_data',
);
/**
* Filters used to format the display of values into friendlier values
*/
@@ -26,5 +30,47 @@ class Model_Cart extends ORM_OSB {
array('StaticList_RecurSchedule::display',array(':value')),
),
);
private $mo;
public function __construct($id = NULL) {
// Load our Model
parent::__construct($id);
// Autoload our Sub Items
if ($this->loaded())
$this->_load_sub_items();
return $this;
}
private function _load_sub_items() {
$this->mo = ORM::factory('Module',$this->module_id)->instance($this->module_item);
if (! $this->mo->loaded())
throw new Kohana_Exception('Item :item not loaded?',array(':item'=>$this->module_item));
}
public function checkout() {
if (! method_exists($this->mo,'checkout'))
throw new Kohana_Exception('Module :module doesnt implement checkout?',array(':module'=>get_class($this->mo)));
return $this->mo->checkout();
}
public function item() {
if (! method_exists($this->mo,'cart_item'))
throw new Kohana_Exception('Module :module doesnt implement cart_item?',array(':module'=>get_class($this->mo)));
return $this->mo->cart_item();
}
public function mo() {
return $this->mo;
}
public function motype() {
return strtolower(preg_replace('/^Model_/','',get_class($this->mo)));
}
}
?>