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

@ -9,6 +9,9 @@
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*
* This file contains enhancements for Kohana, that should be considered upstream and maybe havent been yet.
* It also contains some functionality for OSB, which cannot be covered in ORM_OSB.
*/
abstract class ORM extends Kohana_ORM {
protected $_table_names_plural = FALSE;
@ -18,15 +21,6 @@ abstract class ORM extends Kohana_ORM {
// Our filters used to display values in a friendly format
protected $_display_filters = array();
// Add our OSB site_id to each SELECT query
final protected function _build($type) {
// Exclude tables without site ID's
if (! in_array($this->_table_name,Config::$no_site_id_tables))
$this->where($this->_object_name.'.site_id','=',Config::siteid());
return parent::_build($type);
}
/**
* Format fields for display purposes
*
@ -94,6 +88,23 @@ abstract class ORM extends Kohana_ORM {
return (int) $count;
}
/** OSB SPECIFIC ENHANCEMENTS **
/**
* Add our OSB site_id to each SELECT query
* @see parent::__build()
*/
final protected function _build($type) {
// Exclude tables without site ID's
if (! in_array($this->_table_name,Config::$no_site_id_tables))
$this->where($this->_object_name.'.site_id','=',Config::siteid());
return parent::_build($type);
}
/**
* Function help to find records that are active
*/
protected function _where_active() {
return $this->where('status','=',TRUE);
}

View File

@ -37,6 +37,10 @@ abstract class ORM_OSB extends ORM {
);
}
/**
* Auto process some data as it comes from the database
* @see parent::__get()
*/
public function __get($column) {
if (array_key_exists($column,$this->_table_columns)) {
// If the column is a blob, we'll decode it automatically
@ -216,14 +220,17 @@ abstract class ORM_OSB extends ORM {
return parent::save($validation);
}
/**
* Function help to find records that are active
*/
public function list_active() {
return $this->_where_active()->find_all();
}
public function list_count($active=TRUE) {
$a=($active ? $this->_where_active() : $this);
$x=($active ? $this->_where_active() : $this);
return $a->find_all()->count();
return $x->find_all()->count();
}
}
?>

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 } ?>