2010-11-29 22:41:08 +00:00
< ? php defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
/**
* This class supports OSB exporting by rending the exportable items .
*
* @ package OSB
* @ subpackage Export
* @ 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 (
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 (
'date_payment' => array (
array ( 'Config::date' , array ( ':value' )),
),
'total_amt' => array (
array ( 'Currency::display' , array ( ':value' )),
),
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,ab_%s B WHERE A.site_id=B.site_id AND A.id=B.payment_id GROUP BY B.payment_id HAVING ROUND(SUM(B.alloc_amt),2)!=A.total_amt ORDER BY account_id,payment_id' , 'payment' , 'payment_item' ))
-> execute () as $values ) {
array_push ( $pi , $values [ 'id' ]);
}
return $this -> where ( 'id' , 'IN' , $pi ) -> order_by ( 'account_id' ) -> find_all ();
}
2010-11-29 22:41:08 +00:00
}
2011-08-16 02:27:19 +00:00
?>