From 96a13548f1ae5a0679d90f62e95d21678e3186d7 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 8 Feb 2013 22:41:29 +1100 Subject: [PATCH] Work on invoice --- application/classes/ORM.php | 29 +++-- application/classes/ORM/OSB.php | 11 +- modules/invoice/classes/Model/Invoice.php | 105 ++++++++++++------ .../invoice/classes/Task/Invoice/Complete.php | 34 ++++++ modules/invoice/views/invoice/user/view.php | 8 +- 5 files changed, 140 insertions(+), 47 deletions(-) create mode 100644 modules/invoice/classes/Task/Invoice/Complete.php diff --git a/application/classes/ORM.php b/application/classes/ORM.php index e5b23d1b..8a0c8862 100644 --- a/application/classes/ORM.php +++ b/application/classes/ORM.php @@ -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); } diff --git a/application/classes/ORM/OSB.php b/application/classes/ORM/OSB.php index ce02122c..e4f2c93c 100644 --- a/application/classes/ORM/OSB.php +++ b/application/classes/ORM/OSB.php @@ -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(); } } ?> diff --git a/modules/invoice/classes/Model/Invoice.php b/modules/invoice/classes/Model/Invoice.php index ecd73b96..7f64b462 100644 --- a/modules/invoice/classes/Model/Invoice.php +++ b/modules/invoice/classes/Model/Invoice.php @@ -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 */ diff --git a/modules/invoice/classes/Task/Invoice/Complete.php b/modules/invoice/classes/Task/Invoice/Complete.php new file mode 100644 index 00000000..e2b2e929 --- /dev/null +++ b/modules/invoice/classes/Task/Invoice/Complete.php @@ -0,0 +1,34 @@ +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); + } +} +?> diff --git a/modules/invoice/views/invoice/user/view.php b/modules/invoice/views/invoice/user/view.php index d2b309a9..fc3f13fe 100644 --- a/modules/invoice/views/invoice/user/view.php +++ b/modules/invoice/views/invoice/user/view.php @@ -33,7 +33,7 @@ Current Charges - total(TRUE); ?> + total_charges(TRUE); ?> Payments Received to Date @@ -41,7 +41,7 @@ Credits Applied to Date - credits_total(TRUE); ?> + total_credits(TRUE); ?> Total Charges Due This Invoice @@ -176,11 +176,11 @@ - discount()) { ?> + total_discount()) { ?> Discounts: - (discount(TRUE); ?>) + (total_discount(TRUE); ?>)