Added Payment, other minor fixes

This commit is contained in:
Deon George
2011-12-20 16:46:10 +11:00
parent c8fd44f844
commit a40ce27a16
31 changed files with 452 additions and 1280 deletions

View File

@@ -1,10 +1,10 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports OSB exporting by rending the exportable items.
* This class supports OSB payments.
*
* @package OSB
* @subpackage Export
* @subpackage Payment
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
@@ -34,6 +34,34 @@ class Model_Payment extends ORMOSB {
),
);
// Items belonging to an invoice
private $payment_items = array();
/**
* Return a list of invoice items for this payment.
*/
public function items() {
// If we havent been changed, we'll load the records from the DB.
if ($this->loaded() AND ! $this->_changed)
return $this->payment_item->order_by('invoice_id')->find_all()->as_array();
else
return $this->payment_items;
}
/**
* Add an item to an invoice
*/
public function add_item() {
if ($this->loaded() and ! $this->payment_items)
throw new Kohana_Exception('Need to load payment_items?');
$c = count($this->payment_items);
$this->payment_items[$c] = ORM::factory('payment_item');
return $this->payment_items[$c];
}
/**
* Find all items that are exportable.
*
@@ -103,7 +131,7 @@ class Model_Payment extends ORMOSB {
return $css.Table::display(
$this->limit(10)->find_all(),
25,
array(
array(
'id'=>array('label'=>'ID'),
'date_payment'=>array('label'=>'Date'),
'checkout->display("name")'=>array('label'=>'Method'),
@@ -111,9 +139,45 @@ class Model_Payment extends ORMOSB {
'balance(TRUE)'=>array('label'=>'Balance','class'=>'right'),
'invoicelist()'=>array('label'=>'Invoices'),
),
array(
array(
'type'=>'list',
));
}
public function save(Validation $validation = NULL) {
// Our items will be clobbered once we save the object, so we need to save it here.
$items = $this->items();
$this->source_id = Auth::instance()->get_user()->id;
$this->ip = Request::$client_ip;
// @todo Need validation, if there is an overbalance in payment_items or if an invoice is overpaid.
// Save the payment
parent::save($validation);
// Need to save the associated items and their taxes
if ($this->saved()) {
foreach ($items as $pio) {
$pio->payment_id = $this->id;
if (! $pio->check()) {
// @todo Mark payment as cancelled and write a memo, then...
throw new Kohana_Exception('Problem saving payment_item for invoice :invoice - Failed check()',array(':invoice'=>$invoice->id));
}
$pio->save();
if (! $pio->saved()) {
// @todo Mark payment as cancelled and write a memo, then...
throw new Kohana_Exception('Problem saving payment_item for invoice :invoice - Failed save()',array(':invoice'=>$invoice->id));
}
}
} else
throw new Kohana_Exception('Couldnt save payment for some reason?');
return TRUE;
}
}
?>