checkout();
// @todo - this should be a global config item
$mediapath = Route::get('default/media');
// @todo Items in the cart dont have account_id if they were put in the cart when the user was not logged in
// 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/checkout_cartlist.css',
));
// Show a list of items in the cart
$output = '
';
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);
$output .= View::factory('cart/checkout_list')
->set('price_firstinvoice',$item->quantity*$ppa[$item->recurr_schedule]['price_base']*$pdata['prorata'])
->set('price_setup',$item->quantity*$ppa[$item->recurr_schedule]['price_setup'])
->set('service_start',$pdata['date'])
->set('service_end',$pdata['end'])
->set('price_recurring',$item->quantity*$ppa[$item->recurr_schedule]['price_base'])
->set('item',$item)
->set('mediapath',$mediapath);
}
$output .= '
';
Block::add(array(
'title'=>_('Your Items'),
'body'=>$output,
));
$po = ORM::factory('Checkout')
->payment_options_cart();
// @todo Country value should come from somewhere?
Block::add(array(
'title'=>_('Order Total'),
'body'=>View::factory('cart/checkout_total')
->set('cart',Cart::instance())
->set('country',61),
));
$output = Form::open();
$output .= '';
foreach ($po as $payment) {
$output .= View::factory('checkout/payment_option')
->set('payment',$payment);
}
// @todo Add Javascript to stop submission if something not selected
$output .= ' |
';
$output .= '';
$output .= sprintf('%s | ',Form::submit('submit',_('Submit Order')));
$output .= '
';
$output .= '
';
$output .= Form::close();
Block::add(array(
'title'=>_('Available Payment Methods'),
'body'=>$output,
));
}
// Suppress our right hand tab
$this->template->right = ' ';
}
/**
* Process checkout
*/
private function checkout() {
$invoice = ORM::factory('Invoice');
// Add our individual items to the invoice
foreach (Cart::instance()->contents()->find_all() as $item) {
$invoice_item = $invoice->add_item();
$invoice_item->product_id = $item->product_id;
$invoice_item->product_attr = $item->product_attr;
$invoice_item->product_attr_cart = $item->product_attr;
$invoice_item->quantity = $item->quantity;
$invoice_item->recurring_schedule = $item->recurr_schedule;
$ppa = $item->product->get_price_array();
$period = Period::details($item->recurr_schedule,$item->product->price_recurr_weekday,time(),TRUE);
// @todo rounding should be a global config
$invoice_item->price_base = round($item->quantity*$ppa[$item->recurr_schedule]['price_base']*$period['prorata'],2);
$invoice_item->price_setup = round($item->quantity*$ppa[$item->recurr_schedule]['price_setup'],2);
}
$invoice->account_id = Auth::instance()->get_user()->id;
$invoice->type = 2; // INVOICED VIA CHECKOUT
$invoice->status = 1; // INVOICE IS NOT CANCELLED
$invoice->due_date = time(); // DATE INVOICE MUST BE PAID
$invoice->billed_currency_id = 6; // @todo This should come from the site config or the currency selected
/*
$invoice->process_status = NULL; // TO BE PROCESSED
$invoice->billing_status = NULL; // UNPAID
$invoice->refund_status = NULL; // NOT REFUNDED
$invoice->print_status = NULL; // NOT YET PRINTED
$invoice->discount_amt = NULL; // @todo CALCULATE DISCOUNTS
$invoice->checkout_plugin_id = NULL; // @todo Update the selected checkout plugin
$invoice->checkout_plugin_data = NULL; // @todo Data required for the checkout plugin
*/
if ($invoice->check())
$invoice->save();
else
throw new Kohana_Exception('Problem saving invoice - Failed check()');
}
}
?>