2013-10-10 02:44:53 +00:00
|
|
|
<?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
|
2013-06-19 09:26:07 +00:00
|
|
|
protected $_belongs_to = array(
|
|
|
|
'account'=>array(),
|
|
|
|
'checkout'=>array('foreign_key'=>'checkout_id'),
|
|
|
|
);
|
2013-10-10 02:44:53 +00:00
|
|
|
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(
|
2013-05-14 13:53:04 +00:00
|
|
|
'date_orig'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
2013-10-10 02:44:53 +00:00
|
|
|
'date_payment'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
2013-06-19 09:26:07 +00:00
|
|
|
'fees_amt'=>array(
|
|
|
|
array('Currency::display',array(':value')),
|
|
|
|
),
|
2013-10-10 02:44:53 +00:00
|
|
|
'total_amt'=>array(
|
|
|
|
array('Currency::display',array(':value')),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Items belonging to an invoice
|
|
|
|
private $payment_items = array();
|
2013-06-19 09:26:07 +00:00
|
|
|
private $payment_items_unsorted = TRUE;
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
/**
|
|
|
|
* Intercept our object load, so that we can load our subitems
|
|
|
|
*/
|
|
|
|
protected function _load_values(array $values) {
|
|
|
|
parent::_load_values($values);
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
if ($this->_loaded)
|
2013-10-10 02:44:53 +00:00
|
|
|
$this->payment_items = $this->payment_item->find_all()->as_array();
|
2013-06-19 09:26:07 +00:00
|
|
|
|
|
|
|
return $this;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-19 09:26:07 +00:00
|
|
|
* Add an item to an payment
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-06-19 09:26:07 +00:00
|
|
|
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
|
2013-10-10 02:44:53 +00:00
|
|
|
foreach ($this->payment_items as $pio)
|
2013-06-19 09:26:07 +00:00
|
|
|
if ($pio->invoice_id == $p->invoice_id)
|
2013-10-10 02:44:53 +00:00
|
|
|
return $pio;
|
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
array_push($this->payment_items,$p);
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
return $p;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-19 09:26:07 +00:00
|
|
|
* Calculate there refund amount
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-06-19 09:26:07 +00:00
|
|
|
public function credit($format=FALSE) {
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
foreach ($this->items('CREDIT') as $pio)
|
|
|
|
$result += $pio->alloc_amt*-1;
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : $result;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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') {
|
2013-06-19 09:26:07 +00:00
|
|
|
if ($this->payment_items_unsorted) {
|
|
|
|
Sort::MAsort($this->payment_items,'invoice_id');
|
|
|
|
$this->payment_items_unsorted = FALSE;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->payment_items as $pio) {
|
|
|
|
$return = FALSE;
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case 'ALLOC':
|
2013-06-19 09:26:07 +00:00
|
|
|
if ($pio->alloc_amt >= 0)
|
2013-10-10 02:44:53 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-19 09:26:07 +00:00
|
|
|
* Save a record and payment_items
|
2013-10-10 02:44:53 +00:00
|
|
|
*/
|
2013-06-19 09:26:07 +00:00
|
|
|
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();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
// 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();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
|
|
|
return $format ? Currency::display($result) : Currency::round($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** LIST FUNCTIONS **/
|
|
|
|
|
|
|
|
public function list_unapplied() {
|
2013-06-19 09:26:07 +00:00
|
|
|
$pid = array();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
// We cant use ORM for this complex SQL
|
|
|
|
$db = Database::instance();
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
$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';
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-06-19 09:26:07 +00:00
|
|
|
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();
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|