Fixed payment updates, admin update and minor SSL items
This commit is contained in:
@@ -45,18 +45,25 @@ class Controller_Admin_Payment extends Controller_TemplateDefault_Admin {
|
||||
$this->auto_render = FALSE;
|
||||
|
||||
$i = 0;
|
||||
$list = array();
|
||||
if (isset($_REQUEST['pid']))
|
||||
foreach (ORM::factory('payment_item')->where('payment_id','=',$_REQUEST['pid'])->find_all() as $pio)
|
||||
foreach (ORM::factory('payment',$_REQUEST['pid'])->items() as $pio) {
|
||||
$output .= View::factory($this->viewpath().'/body')
|
||||
->set('trc',$i++%2 ? 'odd' : 'even')
|
||||
->set('pio',$pio)
|
||||
->set('io',$pio->invoice);
|
||||
|
||||
// Remember the invoices we have listed
|
||||
array_push($list,$pio->invoice_id);
|
||||
}
|
||||
|
||||
foreach (ORM::factory('account',$_REQUEST['key'])->invoices_due() as $io)
|
||||
$output .= View::factory($this->viewpath().'/body')
|
||||
->set('trc',$i++%2 ? 'odd' : 'even')
|
||||
->set('pio',ORM::factory('payment_item'))
|
||||
->set('io',$io);
|
||||
// Only list invoices not yet listed
|
||||
if (! in_array($io->id,$list))
|
||||
$output .= View::factory($this->viewpath().'/body')
|
||||
->set('trc',$i++%2 ? 'odd' : 'even')
|
||||
->set('pio',ORM::factory('payment_item'))
|
||||
->set('io',$io);
|
||||
|
||||
// @todo Need the JS to add up the payment allocation before submission
|
||||
$output .= View::factory($this->viewpath().'/foot')
|
||||
@@ -96,20 +103,13 @@ class Controller_Admin_Payment extends Controller_TemplateDefault_Admin {
|
||||
$po = ORM::factory('payment',$id);
|
||||
|
||||
if ($_POST) {
|
||||
if (isset($_POST['payment_item']) AND count($_POST['payment_item'])) {
|
||||
foreach ($_POST['payment_item'] as $k=>$v) {
|
||||
if ($v) {
|
||||
$pio = $po->add_item();
|
||||
$pio->invoice_id = $k;
|
||||
$pio->alloc_amt = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Update our invoice payment items
|
||||
if (isset($_POST['payment_item']) AND count($_POST['payment_item']))
|
||||
foreach ($_POST['payment_item'] as $k=>$v)
|
||||
$po->add_item($k)->alloc_amt = is_numeric($v) ? $v : 0;
|
||||
|
||||
// Entry updated
|
||||
if (! $po->values($_POST)->check() OR ! $po->save())
|
||||
throw new Kohana_Exception('Unable to save payment');
|
||||
else
|
||||
if ($po->values($_POST)->check() AND $po->save())
|
||||
SystemMessage::add(array(
|
||||
'title'=>'Payment Recorded',
|
||||
'type'=>'info',
|
||||
|
@@ -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