Open Source Billing
This commit is contained in:
96
modules/cart/classes/Cart.php
Normal file
96
modules/cart/classes/Cart.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides a order cart
|
||||
*
|
||||
* @package Cart
|
||||
* @category Helpers
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of items in the cart
|
||||
*/
|
||||
public function contents() {
|
||||
return ORM::factory('Cart')
|
||||
->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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an HTML cart list
|
||||
*
|
||||
* @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 (! count($this->contents()))
|
||||
return 'The cart is empty.';
|
||||
|
||||
Style::add(array(
|
||||
'type'=>'file',
|
||||
'data'=>'css/cart_blocklist.css',
|
||||
));
|
||||
|
||||
$output = '<table class="cart_blocklist" border="0">';
|
||||
foreach ($this->contents() as $item) {
|
||||
$ppa = $item->product->get_price_array();
|
||||
$pdata = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);
|
||||
|
||||
$output .= View::factory('cart/block_list')
|
||||
->set('item',$item)
|
||||
->set('price_setup',$item->quantity*$ppa[$item->recurr_schedule]['price_setup'])
|
||||
->set('price_firstinvoice',$item->quantity*$ppa[$item->recurr_schedule]['price_base']*$pdata['prorata']);
|
||||
}
|
||||
|
||||
$output .= '<tr class="submit">';
|
||||
$output .= sprintf('<td colspan="3">%s %s</td>',
|
||||
Form::button('checkout','Checkout',array('type' => 'submit')),
|
||||
Form::button('empty','Empty',array('type' => 'submit')));
|
||||
$output .= '</tr>';
|
||||
$output .= '</table>';
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
?>
|
36
modules/cart/classes/Cart/Item.php
Normal file
36
modules/cart/classes/Cart/Item.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides a Cart Item
|
||||
*
|
||||
* @package Cart
|
||||
* @category Helpers
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.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));
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
18
modules/cart/classes/Cartable.php
Normal file
18
modules/cart/classes/Cartable.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This interface ensures that all cart processing objects have the right methods
|
||||
*
|
||||
* @package Cart
|
||||
* @category Interface
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.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();
|
||||
}
|
||||
?>
|
94
modules/cart/classes/Controller/Cart.php
Normal file
94
modules/cart/classes/Controller/Cart.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class provides a order cart
|
||||
*
|
||||
* @package Cart
|
||||
* @category Controllers
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Controller_Cart extends Controller_TemplateDefault {
|
||||
protected $auth_required = FALSE;
|
||||
|
||||
/**
|
||||
* List the cart contents
|
||||
*/
|
||||
public function action_index() {
|
||||
$output = '';
|
||||
$co = Cart::instance();
|
||||
|
||||
// If the cart is empty, we'll return here.
|
||||
if (! count($co->contents()))
|
||||
Block::add(array(
|
||||
'title'=>_('Empty Cart'),
|
||||
'body'=>_('The cart is empty')
|
||||
));
|
||||
|
||||
else {
|
||||
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',
|
||||
)
|
||||
),
|
||||
));
|
||||
|
||||
$checkout = ORM::factory('Checkout')->where_active()->find_all()->as_array();
|
||||
|
||||
foreach ($co->contents() as $cio)
|
||||
$checkout = array_intersect($checkout,$cio->checkout()->as_array());
|
||||
|
||||
$payopt = array();
|
||||
foreach ($checkout as $cko)
|
||||
$payopt[$cko->id] = $cko->name;
|
||||
|
||||
$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'=>_('Payment'),
|
||||
'body'=>$output,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to the cart
|
||||
*/
|
||||
public function action_add() {
|
||||
$cart = ORM::factory('Cart');
|
||||
|
||||
$cart->session_id = Session::instance()->id();
|
||||
|
||||
if ($cart->values(Request::current()->post())->check())
|
||||
$cart->save();
|
||||
else
|
||||
throw new Kohana_Exception('Unable to add to cart');
|
||||
|
||||
if ($cart->saved())
|
||||
HTTP::redirect('cart/index');
|
||||
else
|
||||
throw new Kohana_Exception(_('There was a problem adding the item to the cart.'));
|
||||
}
|
||||
|
||||
public function action_empty() {
|
||||
foreach (ORM::factory('Cart')->where('session_id','=',Session::instance()->id())->find_all() as $co)
|
||||
$co->delete();
|
||||
|
||||
$this->template->content = _('Cart Emptied');
|
||||
}
|
||||
}
|
||||
?>
|
75
modules/cart/classes/Model/Cart.php
Normal file
75
modules/cart/classes/Model/Cart.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php defined('SYSPATH') or die('No direct access allowed.');
|
||||
|
||||
/**
|
||||
* This class supports a product cart
|
||||
*
|
||||
* @package Cart
|
||||
* @category Models
|
||||
* @author Deon George
|
||||
* @copyright (c) 2009-2013 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Cart extends ORM_OSB {
|
||||
protected $_belongs_to = array(
|
||||
'product'=>array(),
|
||||
);
|
||||
|
||||
// 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
|
||||
*/
|
||||
protected $_display_filters = array(
|
||||
'recurr_schedule'=>array(
|
||||
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)));
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user