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
*
2013-03-19 22:35:19 +00:00
* @ package Payment
2010-11-29 22:41:08 +00:00
* @ category Models
* @ author Deon George
2013-03-19 22:35:19 +00:00
* @ copyright ( c ) 2009 - 2013 Open Source Billing
2010-11-29 22:41:08 +00:00
* @ license http :// dev . osbill . net / license . html
*/
2012-12-10 21:48:30 +00:00
class Model_Payment extends ORM_OSB {
2010-11-29 22:41:08 +00:00
// Relationships
protected $_has_many = array (
2011-07-22 01:04:20 +00:00
'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
2011-07-22 01:04:20 +00:00
protected $_display_filters = array (
2013-05-14 13:53:04 +00:00
'date_orig' => array (
array ( 'Config::date' , array ( ':value' )),
),
2011-07-22 01:04:20 +00:00
'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 ();
2011-12-29 02:52:24 +00:00
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 ();
}
2011-12-20 05:46:10 +00:00
/**
2013-04-05 12:50:08 +00:00
* Add an item to this payment
*
* @ param $inv number , to allocate payment to an invoice
2011-12-20 05:46:10 +00:00
*/
2013-04-05 12:50:08 +00:00
public function add_item ( $invnum ) {
2011-12-29 02:52:24 +00:00
// Find our id, if it exists
foreach ( $this -> payment_items as $pio )
2013-04-05 12:50:08 +00:00
if ( $pio -> invoice_id == $invnum )
2011-12-29 02:52:24 +00:00
return $pio ;
2011-12-20 05:46:10 +00:00
2011-12-29 02:52:24 +00:00
// New Item
2011-12-20 05:46:10 +00:00
$c = count ( $this -> payment_items );
2012-11-09 23:13:57 +00:00
$this -> payment_items [ $c ] = ORM :: factory ( 'Payment_Item' );
2013-04-05 12:50:08 +00:00
$this -> payment_items [ $c ] -> invoice_id = $invnum ;
2011-12-20 05:46:10 +00:00
return $this -> payment_items [ $c ];
}
2013-04-05 12:50:08 +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 ;
}
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 ) {
2012-11-09 23:13:57 +00:00
return ORM :: factory ( 'Payment' )
2010-11-29 22:41:08 +00:00
-> where ( 'date_payment' , '>=' , time () - $start * 86400 )
-> find_all ();
}
2011-08-02 06:20:11 +00:00
/**
2013-04-05 12:50:08 +00:00
* Return a list of invoices that this payment is applied to
2011-08-02 06:20:11 +00:00
*/
2013-04-05 12:50:08 +00:00
public function invoices () {
$result = array ();
2011-08-02 06:20:11 +00:00
2013-04-05 12:50:08 +00:00
foreach ( $this -> payment_items as $pio )
array_push ( $result , $pio -> invoice );
return $result ;
}
2011-08-02 06:20:11 +00:00
2013-04-05 12:50:08 +00:00
public function invoicelist () {
return join ( ',' , $this -> invoices ());
2011-08-02 06:20:11 +00:00
}
/**
2013-04-05 12:50:08 +00:00
* Return a list of payment items for this payment .
* @ param type [ ALLOC | CREDIT | ALL ]
* @ see payment_items
2011-08-02 06:20:11 +00:00
*/
2013-04-05 12:50:08 +00:00
public function items ( $type = 'ALL' ) {
$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 );
}
2011-08-02 06:20:11 +00:00
2013-04-05 12:50:08 +00:00
return $result ;
2011-08-02 06:20:11 +00:00
}
2013-04-05 12:50:08 +00:00
/**
* Show the total amount of a payment .
*/
public function total ( $format = FALSE ) {
$result = $this -> total_amt ;
foreach ( $this -> items ( 'CREDIT' ) as $pio )
$result += $pio -> alloc_amt ;
return $format ? Currency :: display ( $result ) : Currency :: round ( $result );
2011-08-02 06:20:11 +00:00
}
2011-08-16 02:27:19 +00:00
/** LIST FUNCTIONS **/
public function list_unapplied () {
2013-04-20 01:40:44 +00:00
return array ();
2011-08-16 02:27:19 +00:00
$pi = array ();
// @todo database suffix needs to be dynamically calculated
foreach ( DB :: Query ( Database :: SELECT ,
2011-10-14 05:44:12 +00:00
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 ();
2013-01-15 06:07:54 +00:00
// @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 ;
2011-12-20 05:46:10 +00:00
$this -> ip = Request :: $client_ip ;
2011-12-29 02:52:24 +00:00
// 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 ()) {
2012-11-09 23:13:57 +00:00
$old_pio = ORM :: factory ( 'Payment_Item' , $pio -> id );
2011-12-29 02:52:24 +00:00
2012-11-09 23:13:57 +00:00
if (( $it = $pio -> invoice -> due () + ORM :: factory ( 'Payment_Item' , $pio -> id ) -> alloc_amt - $pio -> alloc_amt ) < 0 )
2011-12-29 02:52:24 +00:00
$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 ;
}
2011-12-20 05:46:10 +00:00
// Save the payment
parent :: save ( $validation );
// Need to save the associated items and their taxes
2013-02-02 06:35:21 +00:00
if ( ! $this -> changed () OR $this -> saved ()) {
2011-12-20 05:46:10 +00:00
foreach ( $items as $pio ) {
2013-02-02 06:35:21 +00:00
// Skip applying 0 payments to invoices.
if ( Currency :: round ( $pio -> alloc_amt ) == 0 AND ! $pio -> loaded ())
continue ;
2011-12-20 05:46:10 +00:00
$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 ));
}
}
2013-02-02 06:35:21 +00:00
} else {
2011-12-20 05:46:10 +00:00
throw new Kohana_Exception ( 'Couldnt save payment for some reason?' );
2013-02-02 06:35:21 +00:00
}
2011-12-20 05:46:10 +00:00
return TRUE ;
}
2010-11-29 22:41:08 +00:00
}
2011-08-16 02:27:19 +00:00
?>