OSB enhancements to date

This commit is contained in:
Deon George
2010-11-30 09:41:08 +11:00
parent 8715a2059b
commit ec6a542bc3
478 changed files with 23423 additions and 9309 deletions

View File

@@ -0,0 +1,105 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides a order cart
*
* @package OSB
* @subpackage Cart
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Cart {
public static function instance() {
return new Cart;
}
/**
* Return a list of items in the cart
*/
public function contents() {
return ORM::factory('cart')
->where('session_id','=',Session::instance()->id());
}
/**
* Print an HTML cart list
*
* @param bool $detail List a detailed cart or a summary cart
*/
public function cart_block() {
// If the cart is empty, we'll return here.
if (! $this->contents()->count_all())
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()->find_all() 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&nbsp;%s</td>',
Form::button('checkout','Checkout',array('type' => 'submit')),
Form::button('empty','Empty',array('type' => 'submit')));
$output .= '</tr>';
$output .= '</table>';
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,116 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides a order cart
*
* @package OSB
* @subpackage Cart
* @category Controllers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Controller_Cart extends Controller_TemplateDefault {
/**
* Default action when called
*/
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');
$block = new block;
// If the cart is empty, we'll return here.
if (! Cart::instance()->contents()->count_all())
$block->add(array(
'title'=>_('Empty Cart'),
'body'=>_('The cart is empty')
));
else {
Style::add(array(
'type'=>'file',
'data'=>'css/cart_contents.css',
));
$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);
$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);
$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);
// If we are a plugin product, we might need more information
if ($item->product->prod_plugin AND method_exists($item->product->prod_plugin_file,'product_cart')) {
$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 .= Form::close();
$block->add(array(
'title'=>_('Your Items'),
'body'=>$output,
));
}
$this->template->content = $block;
// Suppress our right hand tab
$this->template->right = ' ';
}
/**
* Add an item to the cart
*/
public function action_add() {
$cart = ORM::factory('cart');
$cart->session_id = Session::instance()->id();
if (Auth::instance()->logged_in())
$cart->account_id = Auth::instance()->get_user()->id;
if ($cart->values($_POST)->check())
$cart->save();
else
echo Kohana::debug($cart->validate()->errors());
if ($cart->saved())
Request::instance()->redirect('cart/index');
else
throw new Kohana_Exception(_('There was a problem adding the item to the cart.'));
}
public function action_empty() {
$cart = ORM::factory('cart')
->where('session_id','=',session_id());
$cart->delete_all();
$this->template->content = _('Cart Emptied');
}
}
?>

View File

@@ -0,0 +1,25 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports a product cart
*
* @package OSB
* @subpackage Cart
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Cart extends ORMOSB {
protected $_belongs_to = array(
'product'=>array(),
);
protected $_formats = array(
'recurr_schedule'=>array('StaticList_RecurSchedule::display'=>array()),
);
// Cart doesnt use the update column
protected $_updated_column = FALSE;
}
?>