Work on invoice

This commit is contained in:
Deon George
2013-02-08 22:41:29 +11:00
parent 69645c4eea
commit 96a13548f1
5 changed files with 140 additions and 47 deletions

View File

@@ -47,6 +47,9 @@ class Model_Invoice extends ORM_OSB implements Cartable {
),
);
// Items belonging to an invoice
private $invoice_items = array();
/** INTERFACE REQUIREMENTS **/
public function cart_item() {
return new Cart_Item(1,sprintf('Invoice: %s',$this->refnum()),$this->due());
@@ -59,9 +62,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
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);
@@ -111,28 +111,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
->where_close()->find_all();
}
public function credits() {
return array($this);
}
public function credits_total($format=FALSE) {
$result = 0;
foreach ($this->credits() as $po)
$result += $po->credit_amt;
return $format ? Currency::display($result) : Currency::round($result);
}
public function discount($format=FALSE) {
$result = 0;
foreach ($this->items() as $ito)
$result += $ito->discount_amt;
return $format ? Currency::display($result) : Currency::round($result);
}
/**
* Display the amount due
*/
@@ -140,10 +118,6 @@ class Model_Invoice extends ORM_OSB implements Cartable {
// 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);
}
@@ -156,9 +130,37 @@ class Model_Invoice extends ORM_OSB implements Cartable {
/**
* Return a list of invoice items for this payment.
* @param type [CHARGE|CREDIT|ALL]
* @see invoice_items
*/
public function items() {
return $this->invoice_items;
public function items($type='ALL') {
$result = array();
foreach ($this->invoice_items as $ito) {
$return = FALSE;
switch ($type) {
case 'CHARGE':
if ($ito->quantity > 0)
$return = TRUE;
break;
case 'CREDIT':
if ($ito->quantity < 0)
$return = TRUE;
break;
case 'ALL':
default:
$return = TRUE;
break;
}
if ($return)
array_push($result,$ito);
}
return $result;
}
/**
@@ -519,6 +521,37 @@ class Model_Invoice extends ORM_OSB implements Cartable {
return $format ? Currency::display($result) : Currency::round($result);
}
public function total_charges($format=FALSE) {
$result = 0;
foreach ($this->items('CHARGE') as $ito)
$result += $ito->subtotal()+$ito->tax();
return $format ? Currency::display($result) : Currency::round($result);
}
public function total_credits($format=FALSE) {
$result = 0;
// @todo Remove when credit_amt is dropped.
if ($this->credit_amt)
$result = $this->credit_amt;
foreach ($this->items('CREDIT') as $ito)
$result += ($ito->subtotal()+$ito->tax())*-1;
return $format ? Currency::display($result) : Currency::round($result);
}
public function total_discount($format=FALSE) {
$result = 0;
foreach ($this->items() as $ito)
$result += $ito->discount();
return $format ? Currency::display($result) : Currency::round($result);
}
/** LIST FUNCTIONS **/
/**
@@ -549,13 +582,21 @@ class Model_Invoice extends ORM_OSB implements Cartable {
static $result = array();
if (! $result)
foreach ($this->list_active() as $io)
foreach ($this->_where_active()->_where_unprocessed()->find_all() as $io)
if ($io->due())
array_push($result,$io);
return $result;
}
private function _where_unprocessed() {
return $this->where_open()->where('process_status','!=',1)->or_where('process_status','is',NULL)->where_close();
}
public function where_unprocessed() {
return $this->_where_unprocessed();
}
/**
* Identify all the invoices that are due
*/

View File

@@ -0,0 +1,34 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Mark all invoices that have been paid as complete, so that they are not processed again.
*
* @package OSB
* @subpackage Invoice
* @category Task/Invoice
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Invoice_Complete extends Task {
protected function _execute(array $params) {
$c = 0;
$o = ORM::factory('Invoice')
->where_active()
->where_unprocessed();
foreach ($o->find_all() as $io) {
if ($io->due() == 0)
$io->process_status = 1;
$io->save();
if ($io->saved())
$c++;
}
printf('%s invoices updated',$c);
}
}
?>

View File

@@ -33,7 +33,7 @@
</tr>
<tr>
<td>Current Charges</td>
<td class="bold-right"><?php echo $io->total(TRUE); ?></td>
<td class="bold-right"><?php echo $io->total_charges(TRUE); ?></td>
</tr>
<tr>
<td>Payments Received to Date</td>
@@ -41,7 +41,7 @@
</tr>
<tr>
<td>Credits Applied to Date</td>
<td class="bold-right"><?php echo $io->credits_total(TRUE); ?></td>
<td class="bold-right"><?php echo $io->total_credits(TRUE); ?></td>
</tr>
<tr>
<td>Total Charges Due This Invoice</td>
@@ -176,11 +176,11 @@
</tr>
<!-- END Invoice Credits -->
<?php } ?>
<?php if ($io->discount()) { ?>
<?php if ($io->total_discount()) { ?>
<!-- Invoice Discounts Total -->
<tr>
<td class="head" colspan="2">Discounts:</td>
<td class="bold-right" colspan="2">(<?php echo $io->discount(TRUE); ?>)</td>
<td class="bold-right" colspan="2">(<?php echo $io->total_discount(TRUE); ?>)</td>
</tr>
<!-- END Invoice Discounts Total -->
<?php } ?>