Cart work for payments and Paypal work to test
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
* @copyright (c) 2010 Open Source Billing
|
||||
* @license http://dev.osbill.net/license.html
|
||||
*/
|
||||
class Model_Invoice extends ORM_OSB {
|
||||
class Model_Invoice extends ORM_OSB implements Cartable {
|
||||
protected $_belongs_to = array(
|
||||
'account'=>array()
|
||||
);
|
||||
@@ -47,74 +47,68 @@ class Model_Invoice extends ORM_OSB {
|
||||
),
|
||||
);
|
||||
|
||||
// Items belonging to an invoice
|
||||
private $invoice_items = array();
|
||||
private $subitems_load = FALSE;
|
||||
|
||||
public function __construct($id = NULL) {
|
||||
// Load our model.
|
||||
parent::__construct($id);
|
||||
|
||||
return $this->load_sub_items();
|
||||
/** INTERFACE REQUIREMENTS **/
|
||||
public function cart_item() {
|
||||
return new Cart_Item(1,sprintf('Invoice: %s',$this->refnum()),$this->due());
|
||||
}
|
||||
|
||||
private function load_sub_items() {
|
||||
// Load our sub items
|
||||
if (! $this->subitems_load AND $this->loaded()) {
|
||||
$this->invoice_items = $this->invoice_item->find_all()->as_array();
|
||||
$this->subitems_load = TRUE;
|
||||
}
|
||||
/**
|
||||
* Return if this invoice is already in the cart
|
||||
*/
|
||||
public function cart_exists() {
|
||||
return count(Cart::instance()->get($this->mid(),$this->id));
|
||||
}
|
||||
|
||||
// Items belonging to an invoice
|
||||
private $invoice_items = array();
|
||||
|
||||
public function __construct($id = NULL) {
|
||||
// Load our Model
|
||||
parent::__construct($id);
|
||||
|
||||
// Autoload our Sub Items
|
||||
if ($this->loaded())
|
||||
$this->_load_sub_items();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of invoice items for this payment.
|
||||
* Load our invoice items
|
||||
* We need these so that we can calculate totals, etc
|
||||
*/
|
||||
public function items() {
|
||||
$this->load_sub_items();
|
||||
|
||||
return $this->invoice_items;
|
||||
private function _load_sub_items() {
|
||||
// Load our sub items
|
||||
$this->invoice_items = $this->invoice_item->find_all()->as_array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the Invoice Number
|
||||
* Add an item to an invoice
|
||||
*/
|
||||
public function id() {
|
||||
return sprintf('%06s',$this->id);
|
||||
public function add_item() {
|
||||
if ($this->loaded() and ! $this->invoice_items)
|
||||
throw new Kohana_Exception('Need to load invoice_items?');
|
||||
|
||||
$c = count($this->invoice_items);
|
||||
|
||||
$this->invoice_items[$c] = ORM::factory('Invoice_Item');
|
||||
|
||||
return $this->invoice_items[$c];
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the Invoice Reference Number
|
||||
* Return a list of valid checkout options for this invoice
|
||||
*/
|
||||
public function refnum() {
|
||||
return sprintf('%s-%06s',$this->account->accnum(),$this->id);
|
||||
}
|
||||
public function checkout() {
|
||||
$due = $this->due();
|
||||
|
||||
/**
|
||||
* Display the amount due
|
||||
*/
|
||||
public function due($format=FALSE) {
|
||||
// If the invoice is active calculate the due amount
|
||||
$result = $this->status ? $this->total()-$this->payments_total() : 0;
|
||||
|
||||
// @todo This should not be required.
|
||||
if ((Currency::round($result) == .01) or Currency::round($result) == .02)
|
||||
$result = 0;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the subtotal of all items
|
||||
*/
|
||||
public function subtotal($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
$result += $ito->subtotal();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
return ORM::factory('Checkout')
|
||||
->where_active()
|
||||
->where('amount_min','<=',$due)
|
||||
->where_open()
|
||||
->and_where('amount_max','>=',$due)
|
||||
->or_where('amount_max','is',null)
|
||||
->where_close()->find_all();
|
||||
}
|
||||
|
||||
public function credits() {
|
||||
@@ -139,41 +133,32 @@ class Model_Invoice extends ORM_OSB {
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
public function tax($format=FALSE) {
|
||||
$result = 0;
|
||||
/**
|
||||
* Display the amount due
|
||||
*/
|
||||
public function due($format=FALSE) {
|
||||
// If the invoice is active calculate the due amount
|
||||
$result = $this->status ? $this->total()-$this->payments_total() : 0;
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
$result += $ito->tax();
|
||||
// @todo This should not be required.
|
||||
if ((Currency::round($result) == .01) or Currency::round($result) == .02)
|
||||
$result = 0;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total of all items
|
||||
* Display the Invoice Number
|
||||
*/
|
||||
public function total($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
$result += $ito->total();
|
||||
|
||||
// Reduce by any credits
|
||||
$result -= $this->credit_amt;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
public function id() {
|
||||
return sprintf('%06s',$this->id);
|
||||
}
|
||||
|
||||
public function payments() {
|
||||
return $this->payment_item->find_all();
|
||||
}
|
||||
|
||||
public function payments_total($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->payments() as $po)
|
||||
$result += $po->alloc_amt;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
/**
|
||||
* Return a list of invoice items for this payment.
|
||||
*/
|
||||
public function items() {
|
||||
return $this->invoice_items;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -345,79 +330,28 @@ class Model_Invoice extends ORM_OSB {
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of taxes used on this invoice
|
||||
* @todo Move some of this to invoice_item_tax.
|
||||
*/
|
||||
public function tax_summary() {
|
||||
$summary = array();
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
foreach ($ito->invoice_item_tax->find_all() as $item_tax) {
|
||||
if (! isset($summary[$item_tax->tax_id]))
|
||||
$summary[$item_tax->tax_id] = $item_tax->amount;
|
||||
else
|
||||
$summary[$item_tax->tax_id] += $item_tax->amount;
|
||||
}
|
||||
}
|
||||
|
||||
// @todo This should be removed eventually
|
||||
if (! $summary)
|
||||
$summary[1] = $this->tax();
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an item to an invoice
|
||||
*/
|
||||
public function add_item() {
|
||||
if ($this->loaded() and ! $this->invoice_items)
|
||||
throw new Kohana_Exception('Need to load invoice_items?');
|
||||
|
||||
$c = count($this->invoice_items);
|
||||
|
||||
$this->invoice_items[$c] = ORM::factory('Invoice_Item');
|
||||
|
||||
return $this->invoice_items[$c];
|
||||
}
|
||||
|
||||
public function min_due($date) {
|
||||
return strtotime(date('Y-M-d',($date < time()) ? time()+ORM::factory('Invoice')->config('DUE_DAYS_MIN')*86400 : $date));
|
||||
}
|
||||
|
||||
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();
|
||||
/**
|
||||
* Display the Invoice Reference Number
|
||||
*/
|
||||
public function refnum() {
|
||||
return sprintf('%s-%06s',$this->account->accnum(),$this->id);
|
||||
}
|
||||
|
||||
// Save the invoice
|
||||
parent::save($validation);
|
||||
public function payments() {
|
||||
return $this->payment_item->find_all();
|
||||
}
|
||||
|
||||
// Need to save the associated items and their taxes
|
||||
if ($this->saved()) {
|
||||
foreach ($items as $iio) {
|
||||
$iio->invoice_id = $this->id;
|
||||
public function payments_total($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
if (! $iio->check()) {
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed check()',array(':invoice'=>$invoice->id));
|
||||
}
|
||||
foreach ($this->payments() as $po)
|
||||
$result += $po->alloc_amt;
|
||||
|
||||
$iio->save();
|
||||
|
||||
if (! $iio->saved()) {
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed save()',array(':invoice'=>$invoice->id));
|
||||
}
|
||||
|
||||
// @todo Need to save discount information
|
||||
}
|
||||
|
||||
|
||||
} else
|
||||
throw new Kohana_Exception('Couldnt save invoice for some reason?');
|
||||
|
||||
return TRUE;
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -441,6 +375,44 @@ class Model_Invoice extends ORM_OSB {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
// Save the invoice
|
||||
if ($this->changed())
|
||||
parent::save($validation);
|
||||
|
||||
// Need to save the associated items and their taxes
|
||||
if ($this->loaded()) {
|
||||
foreach ($items as $iio) {
|
||||
$iio->invoice_id = $this->id;
|
||||
|
||||
if (! $iio->changed())
|
||||
continue;
|
||||
|
||||
if (! $iio->check()) {
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed check()',array(':invoice'=>$this->id));
|
||||
}
|
||||
|
||||
$iio->save();
|
||||
|
||||
if (! $iio->saved()) {
|
||||
// @todo Mark invoice as cancelled and write a memo, then...
|
||||
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed save()',array(':invoice'=>$this->id));
|
||||
}
|
||||
|
||||
// @todo Need to save discount information
|
||||
}
|
||||
|
||||
|
||||
} else
|
||||
throw new Kohana_Exception('Couldnt save invoice for some reason?');
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
public function set_remind($key,$value,$add=FALSE) {
|
||||
if (! $this->loaded())
|
||||
throw new Kohana_Exception('Cant call :method when a record not loaded.',array(':method',__METHOD__));
|
||||
@@ -484,6 +456,69 @@ class Model_Invoice extends ORM_OSB {
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the subtotal of all items
|
||||
*/
|
||||
public function subtotal($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
$result += $ito->subtotal();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
public function tax($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
$result += $ito->tax();
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of taxes used on this invoice
|
||||
* @todo Move some of this to invoice_item_tax.
|
||||
*/
|
||||
public function tax_summary() {
|
||||
$summary = array();
|
||||
|
||||
foreach ($this->items() as $ito) {
|
||||
foreach ($ito->invoice_item_tax->find_all() as $item_tax) {
|
||||
if (! isset($summary[$item_tax->tax_id]))
|
||||
$summary[$item_tax->tax_id] = $item_tax->amount;
|
||||
else
|
||||
$summary[$item_tax->tax_id] += $item_tax->amount;
|
||||
}
|
||||
}
|
||||
|
||||
// @todo This should be removed eventually
|
||||
if (! $summary)
|
||||
$summary[1] = $this->tax();
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total of all items
|
||||
*/
|
||||
public function total($format=FALSE) {
|
||||
$result = 0;
|
||||
|
||||
// @todo - This should be required, but during checkout payment processing $pio->invoice->total() showed no invoice items?
|
||||
if ($this->loaded() AND ! count($this->items()))
|
||||
$this->_load_sub_items();
|
||||
|
||||
foreach ($this->items() as $ito)
|
||||
$result += $ito->total();
|
||||
|
||||
// Reduce by any credits
|
||||
$result -= $this->credit_amt;
|
||||
|
||||
return $format ? Currency::display($result) : Currency::round($result);
|
||||
}
|
||||
|
||||
/** LIST FUNCTIONS **/
|
||||
|
||||
/**
|
||||
|
@@ -142,6 +142,8 @@ class Model_Invoice_Item extends ORM_OSB {
|
||||
|
||||
case 6: return _('Service Excess Fee');
|
||||
|
||||
case 125: return _('Payment Fee');
|
||||
|
||||
case 126: return _('Rounding');
|
||||
|
||||
case 127: return _('Late Payment Fee');
|
||||
@@ -164,6 +166,9 @@ class Model_Invoice_Item extends ORM_OSB {
|
||||
}
|
||||
|
||||
public function save(Validation $validation = NULL) {
|
||||
if (! $this->changed())
|
||||
return;
|
||||
|
||||
// Save the invoice item
|
||||
parent::save($validation);
|
||||
|
||||
|
Reference in New Issue
Block a user