This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/payment/classes/Model/Payment.php
2013-06-20 15:57:07 +10:00

275 lines
7.0 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports OSB payments.
*
* @package Payment
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
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 $_sorting = array(
'date_payment'=>'DESC'
);
protected $_display_filters = array(
'date_orig'=>array(
array('Config::date',array(':value')),
),
'date_payment'=>array(
array('Config::date',array(':value')),
),
'fees_amt'=>array(
array('Currency::display',array(':value')),
),
'total_amt'=>array(
array('Currency::display',array(':value')),
),
);
// Items belonging to an invoice
private $payment_items = array();
private $payment_items_unsorted = TRUE;
/**
* Intercept our object load, so that we can load our subitems
*/
protected function _load_values(array $values) {
parent::_load_values($values);
if ($this->_loaded)
$this->payment_items = $this->payment_item->find_all()->as_array();
return $this;
}
/**
* Add an item to an payment
*/
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 == $p->invoice_id)
return $pio;
array_push($this->payment_items,$p);
return $p;
}
/**
* Calculate the remaining balance available for this payment
*/
public function balance($format=FALSE) {
$result = $this->total();
foreach ($this->items('ALLOC') as $pio)
$result -= $pio->alloc_amt;
return $format ? Currency::display($result) : $result;
}
/**
* Calculate there refund amount
*/
public function credit($format=FALSE) {
$result = 0;
foreach ($this->items('CREDIT') as $pio)
$result += $pio->alloc_amt*-1;
return $format ? Currency::display($result) : $result;
}
/**
* Return a list of invoices that this payment is applied to
*/
public function invoices() {
$result = array();
foreach ($this->payment_items as $pio)
array_push($result,$pio->invoice);
return $result;
}
public function invoicelist() {
return join(',',$this->invoices());
}
/**
* Return a list of payment items for this payment.
* @param type [ALLOC|CREDIT|ALL]
* @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) {
$return = FALSE;
switch ($type) {
case 'ALLOC':
if ($pio->alloc_amt >= 0)
$return = TRUE;
break;
case 'CREDIT':
if ($pio->alloc_amt < 0)
$return = TRUE;
break;
case 'ALL':
default:
$return = TRUE;
break;
}
if ($return)
array_push($result,$pio);
}
return $result;
}
/**
* 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 - $this->credit();
return $format ? Currency::display($result) : Currency::round($result);
}
/** LIST FUNCTIONS **/
public function list_unapplied() {
$pid = array();
// We cant use ORM for this complex SQL
$db = Database::instance();
$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';
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() {
// @todo This should be in a config file.
$css = '<style type="text/css">';
$css .= 'table.box-left { border: 1px solid #AAAACC; margin-right: auto; }';
$css .= 'tr.head { font-weight: bold; }';
$css .= 'td.head { font-weight: bold; }';
$css .= 'td.right { text-align: right; }';
$css .= 'tr.odd { background-color: #FCFCFE; }';
$css .= 'tr.even { background-color: #F6F6F8; }';
$css .= '</style>';
return $css.Table::display(
$this->limit(10)->find_all(),
25,
array(
'id'=>array('label'=>'ID'),
'date_payment'=>array('label'=>'Date'),
'checkout->display("name")'=>array('label'=>'Method'),
'total_amt'=>array('label'=>'Total','class'=>'right'),
'balance(TRUE)'=>array('label'=>'Balance','class'=>'right'),
'invoicelist()'=>array('label'=>'Invoices'),
),
array(
'type'=>'list',
));
}
}
?>