Improvements to payment and other misc items

This commit is contained in:
Deon George
2013-06-19 19:26:07 +10:00
parent 1caeed6606
commit 3499776ddc
33 changed files with 569 additions and 530 deletions

View File

@@ -11,14 +11,14 @@
*/
class Model_Payment extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'account'=>array(),
'checkout'=>array('foreign_key'=>'checkout_id'),
);
protected $_has_many = array(
'payment_item'=>array('far_key'=>'id'),
'invoice'=>array('through'=>'payment_item'),
);
protected $_belongs_to = array(
'account'=>array(),
'checkout'=>array('foreign_key'=>'checkout_plugin_id'),
);
protected $_sorting = array(
'date_payment'=>'DESC'
@@ -31,6 +31,9 @@ class Model_Payment extends ORM_OSB {
'date_payment'=>array(
array('Config::date',array(':value')),
),
'fees_amt'=>array(
array('Currency::display',array(':value')),
),
'total_amt'=>array(
array('Currency::display',array(':value')),
),
@@ -38,33 +41,34 @@ class Model_Payment extends ORM_OSB {
// Items belonging to an invoice
private $payment_items = array();
private $payment_items_unsorted = TRUE;
public function __construct($id = NULL) {
// Load our model.
parent::__construct($id);
/**
* Intercept our object load, so that we can load our subitems
*/
protected function _load_values(array $values) {
parent::_load_values($values);
// Load our sub items
if ($this->loaded())
if ($this->_loaded)
$this->payment_items = $this->payment_item->find_all()->as_array();
return $this;
}
/**
* Add an item to this payment
*
* @param $inv number, to allocate payment to an invoice
* Add an item to an payment
*/
public function add_item($invnum) {
// Find our id, if it exists
public function add_item(Model_Payment_Item $p=NULL) {
$this->payment_items_unsorted = TRUE;
// Make sure this payment item for an invoice is not already in the list
foreach ($this->payment_items as $pio)
if ($pio->invoice_id == $invnum)
if ($pio->invoice_id == $p->invoice_id)
return $pio;
// New Item
$c = count($this->payment_items);
$this->payment_items[$c] = ORM::factory('Payment_Item');
$this->payment_items[$c]->invoice_id = $invnum;
array_push($this->payment_items,$p);
return $this->payment_items[$c];
return $p;
}
/**
@@ -80,14 +84,15 @@ class Model_Payment extends ORM_OSB {
}
/**
* Find all items that are exportable.
*
* @param int $start List payments that were modified this many days ago
* Calculate there refund amount
*/
public function export($start) {
return ORM::factory('Payment')
->where('date_payment','>=',time()-$start*86400)
->find_all();
public function credit($format=FALSE) {
$result = 0;
foreach ($this->items('CREDIT') as $pio)
$result += $pio->alloc_amt*-1;
return $format ? Currency::display($result) : $result;
}
/**
@@ -112,6 +117,11 @@ class Model_Payment extends ORM_OSB {
* @see payment_items
*/
public function items($type='ALL') {
if ($this->payment_items_unsorted) {
Sort::MAsort($this->payment_items,'invoice_id');
$this->payment_items_unsorted = FALSE;
}
$result = array();
foreach ($this->payment_items as $pio) {
@@ -119,7 +129,7 @@ class Model_Payment extends ORM_OSB {
switch ($type) {
case 'ALLOC':
if ($pio->alloc_amt > 0)
if ($pio->alloc_amt >= 0)
$return = TRUE;
break;
@@ -142,13 +152,73 @@ class Model_Payment extends ORM_OSB {
}
/**
* Show the total amount of a payment.
* Save a record and payment_items
*/
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();
// If a user modified this payment, we'll update the source to their ID.
if ($ao = Auth::instance()->get_user())
$this->source_id = $ao->id;
$this->ip = Request::$client_ip;
// Make sure we dont over allocate
$msg = '';
foreach ($items as $pio)
// Only need to check items that ave actually changed.
if ($pio->changed() AND ($x=$pio->original_values()))
if (($x = $pio->alloc_amt-$pio->invoice->due()-$x['alloc_amt']) > 0)
$msg .= ($msg ? ' ' : '').sprintf('Invoice %s over allocated by %3.2f.',$pio->invoice_id,$x);
if (($x=$this->balance()) < 0)
$msg .= ($msg ? ' ' : '').sprintf('Payment over allocated by %3.2f.',-$x);
if ($msg) {
SystemMessage::factory()
->title('Record NOT updated')
->type('warning')
->body($msg);
return FALSE;
}
// Save the payment
parent::save($validation);
// Need to save the associated items and their taxes
if (! $this->changed() OR $this->saved()) {
foreach ($items as $pio) {
// Skip applying 0 payments to invoices.
if (! $pio->changed() OR (Currency::round($pio->alloc_amt) == 0 AND ! $pio->loaded()))
continue;
$pio->payment_id = $this->id;
// @todo Mark payment as cancelled and write a memo, then...
if (! $pio->check())
throw HTTP_Exception::factory(501,'Problem saving payment_item for invoice :invoice - Failed check()',array(':invoice'=>$pio->invoice_id));
$pio->save();
// @todo Mark payment as cancelled and write a memo, then...
if (! $pio->saved())
throw HTTP_Exception::factory(501,'Problem saving payment_item for invoice :invoice - Failed save()',array(':invoice'=>$pio->invoice_id));
}
} else {
throw HTTP_Exception::factory(501,'Problem saving payment :id - Failed save()',array(':id'=>$this->id));
}
return TRUE;
}
/**
* Reduce the total by the amount of credits returned.
*/
public function total($format=FALSE) {
$result = $this->total_amt;
foreach ($this->items('CREDIT') as $pio)
$result += $pio->alloc_amt;
$result = $this->total_amt - $this->credit();
return $format ? Currency::display($result) : Currency::round($result);
}
@@ -156,18 +226,22 @@ class Model_Payment extends ORM_OSB {
/** LIST FUNCTIONS **/
public function list_unapplied() {
return array();
$pi = array();
$pid = array();
// @todo database suffix needs to be dynamically calculated
foreach (DB::Query(Database::SELECT,
sprintf('SELECT A.id AS id,A.total_amt as total_amt FROM ab_%s A LEFT JOIN ab_%s B ON (A.site_id=B.site_id AND A.id=B.payment_id) WHERE (A.refund_status=0 OR A.refund_status IS NULL) GROUP BY A.id HAVING ROUND(SUM(IFNULL(B.alloc_amt,0)),2)!=A.total_amt ORDER BY account_id,payment_id','payment','payment_item'))
->execute() as $values) {
// We cant use ORM for this complex SQL
$db = Database::instance();
array_push($pi,$values['id']);
}
$sql = 'SELECT A.id as id, A.total_amt, ROUND(SUM(IF(IFNULL(B.alloc_amt,0)<0,IFNULL(B.alloc_amt,0)*-1,IFNULL(B.alloc_amt,0))),2) as ALLOC';
$sql .= ' FROM :prefix_payment A ';
$sql .= ' RIGHT JOIN :prefix_payment_item B ON (A.site_id=B.site_id AND A.id=B.payment_id)';
$sql .= ' WHERE A.site_id=:site_id';
$sql .= ' GROUP BY A.id';
$sql .= ' HAVING round(A.total_amt-ALLOC,2) <> 0';
return $this->where('id','IN',$pi)->order_by('account_id')->find_all();
foreach ($db->query(Database::SELECT,__($sql,array(':prefix_'=>$db->table_prefix(),':site_id'=>Config::siteid()))) as $values)
array_push($pid,$values['id']);
return $this->where('id','IN',$pid)->order_by('account_id')->find_all();
}
public function list_recent_table() {
@@ -196,73 +270,5 @@ class Model_Payment extends ORM_OSB {
'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();
// @todo This should not be mandatory - or there should be a source for non-users (automatic postings)
$this->source_id = Auth::instance()->get_user() ? Auth::instance()->get_user()->id : 1;
$this->ip = Request::$client_ip;
// 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);
// Need to save the associated items and their taxes
if (! $this->changed() OR $this->saved()) {
foreach ($items as $pio) {
// Skip applying 0 payments to invoices.
if (Currency::round($pio->alloc_amt) == 0 AND ! $pio->loaded())
continue;
$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;
}
}
?>

View File

@@ -11,12 +11,17 @@
*/
class Model_Payment_Item extends ORM_OSB {
// Relationships
protected $_belongs_to = array(
'payment'=>array(),
);
protected $_has_one = array(
'invoice'=>array('far_key'=>'invoice_id','foreign_key'=>'id'),
);
protected $_belongs_to = array(
'payment'=>array(),
protected $_display_filters = array(
'alloc_amt'=>array(
array('Currency::display',array(':value')),
),
);
}
?>