Fixed payment updates, admin update and minor SSL items
This commit is contained in:
@@ -37,27 +37,35 @@ class Model_Payment extends ORMOSB {
|
||||
// Items belonging to an invoice
|
||||
private $payment_items = array();
|
||||
|
||||
public function __construct($id = NULL) {
|
||||
// Load our model.
|
||||
parent::__construct($id);
|
||||
|
||||
// Load our sub items
|
||||
if ($this->loaded())
|
||||
$this->payment_items = $this->payment_item->find_all()->as_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;
|
||||
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?');
|
||||
public function add_item($iid) {
|
||||
// Find our id, if it exists
|
||||
foreach ($this->payment_items as $pio)
|
||||
if ($pio->invoice_id == $iid)
|
||||
return $pio;
|
||||
|
||||
// New Item
|
||||
$c = count($this->payment_items);
|
||||
|
||||
$this->payment_items[$c] = ORM::factory('payment_item');
|
||||
$this->payment_items[$c]->invoice_id = $iid;
|
||||
|
||||
return $this->payment_items[$c];
|
||||
}
|
||||
@@ -151,7 +159,33 @@ class Model_Payment extends ORMOSB {
|
||||
$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.
|
||||
// Make sure we dont over allocate
|
||||
$t = 0;
|
||||
$msg = '';
|
||||
foreach ($items as $pio) {
|
||||
// Only need to check items that ave actually changed.
|
||||
if ($pio->changed()) {
|
||||
$old_pio = ORM::factory('payment_item',$pio->id);
|
||||
|
||||
if ($it = $pio->invoice->due()+ORM::factory('payment_item',$pio->id)->alloc_amt-$pio->alloc_amt < 0)
|
||||
$msg .= ($msg ? ' ' : '').sprintf('Invoice %s over allocated by %3.2f.',$pio->invoice_id,$it);
|
||||
}
|
||||
|
||||
$t += $pio->alloc_amt;
|
||||
}
|
||||
|
||||
if ($t > (float)$this->total_amt)
|
||||
$msg .= ($msg ? ' ' : '').sprintf('Payment over allocated by %3.2f.',$t-$this->total_amt);
|
||||
|
||||
if ($msg) {
|
||||
SystemMessage::add(array(
|
||||
'title'=>'Payment NOT Recorded',
|
||||
'type'=>'warning',
|
||||
'body'=>$msg,
|
||||
));
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Save the payment
|
||||
parent::save($validation);
|
||||
|
Reference in New Issue
Block a user