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

184 lines
5.0 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
2011-12-20 05:46:10 +00:00
* This class supports OSB payments.
2010-11-29 22:41:08 +00:00
*
* @package OSB
2011-12-20 05:46:10 +00:00
* @subpackage Payment
2010-11-29 22:41:08 +00:00
* @category Models
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Payment extends ORMOSB {
// Relationships
protected $_has_many = array(
'payment_item'=>array('far_key'=>'id'),
2010-11-29 22:41:08 +00:00
'invoice'=>array('through'=>'payment_item'),
);
protected $_belongs_to = array(
'account'=>array(),
'checkout'=>array('foreign_key'=>'checkout_plugin_id'),
);
2011-08-02 06:20:11 +00:00
protected $_sorting = array(
'date_payment'=>'DESC'
);
2010-11-29 22:41:08 +00:00
protected $_display_filters = array(
'date_payment'=>array(
array('Config::date',array(':value')),
),
'total_amt'=>array(
array('Currency::display',array(':value')),
),
2010-11-29 22:41:08 +00:00
);
2011-12-20 05:46:10 +00:00
// Items belonging to an invoice
private $payment_items = 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;
}
/**
* 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?');
$c = count($this->payment_items);
$this->payment_items[$c] = ORM::factory('payment_item');
return $this->payment_items[$c];
}
2010-11-29 22:41:08 +00:00
/**
* Find all items that are exportable.
*
* @param int $start List payments that were modified this many days ago
*/
public function export($start) {
return ORM::factory('payment')
->where('date_payment','>=',time()-$start*86400)
->find_all();
}
2011-08-02 06:20:11 +00:00
/**
* Calculate the remaining balance available for this payment
*/
public function balance($format=FALSE) {
$t = 0;
foreach ($this->payment_item->find_all() as $pio)
$t += $pio->alloc_amt;
return $format ? Currency::display($this->total_amt-$t) : $this->total_amt-$t;
}
/**
* Return a list of invoices that this payment is applied to
*/
public function invoices() {
$invoices = array();
foreach ($this->payment_item->find_all() as $pio)
array_push($invoices,$pio->invoice);
return $invoices;
}
public function invoicelist() {
return join(',',$this->invoices());
}
2011-08-16 02:27:19 +00:00
/** LIST FUNCTIONS **/
public function list_unapplied() {
$pi = 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'))
2011-08-16 02:27:19 +00:00
->execute() as $values) {
array_push($pi,$values['id']);
}
return $this->where('id','IN',$pi)->order_by('account_id')->find_all();
}
2011-09-27 11:22:13 +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,
2011-12-20 05:46:10 +00:00
array(
2011-09-27 11:22:13 +00:00
'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'),
),
2011-12-20 05:46:10 +00:00
array(
2011-09-27 11:22:13 +00:00
'type'=>'list',
));
}
2011-12-20 05:46:10 +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();
$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.
// Save the payment
parent::save($validation);
// Need to save the associated items and their taxes
if ($this->saved()) {
foreach ($items as $pio) {
$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;
}
2010-11-29 22:41:08 +00:00
}
2011-08-16 02:27:19 +00:00
?>