From 14d5f1939eb800dbcd55d22022273412bf3103e0 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 12 Jan 2012 10:23:35 +1100 Subject: [PATCH] Move invoice config items into the DB --- .../controller/templatedefault/admin.php | 2 +- .../classes/controller/admin/invoice.php | 7 +++ .../classes/controller/task/invoice.php | 26 +++----- modules/invoice/classes/model/invoice.php | 7 +-- .../invoice/classes/model/invoice/item.php | 60 +++++++++++++------ 5 files changed, 60 insertions(+), 42 deletions(-) diff --git a/application/classes/controller/templatedefault/admin.php b/application/classes/controller/templatedefault/admin.php index 8480b03f..f8731360 100644 --- a/application/classes/controller/templatedefault/admin.php +++ b/application/classes/controller/templatedefault/admin.php @@ -30,7 +30,7 @@ class Controller_TemplateDefault_Admin extends Controller_TemplateDefault_User { ->set('mc',$mc) ->set('key',$k) ->set('info',$v) - ->set('val',empty($mc[$k]) ? '' : $mc[$k]); + ->set('val',isset($mc[$k]) ? $mc[$k] : ''); $output .= View::factory('setup/admin/module/foot'); diff --git a/modules/invoice/classes/controller/admin/invoice.php b/modules/invoice/classes/controller/admin/invoice.php index 62a12f77..8f5d73b1 100644 --- a/modules/invoice/classes/controller/admin/invoice.php +++ b/modules/invoice/classes/controller/admin/invoice.php @@ -22,8 +22,15 @@ class Controller_Admin_Invoice extends Controller_TemplateDefault_Admin { public function action_setup() { $this->setup(array( + 'EMAIL_INV_MAX'=>_('Email this many invoices in a run (0=no limit)'), 'GEN_DAYS'=>_('Generate Invoices this many days in advance of the due date'), + 'GEN_INV_MAX'=>_('Generate this many invoices in a run (0=no limit)'), 'GEN_SOON_DAYS'=>_('Days before GEN_DAYS to list invoices that will be generated'), + 'DUE_DAYS_MIN'=>_('When invoices are generated, the minimum days in advance the due date should be set to'), + 'REMIND_DUE'=>_('Days before an invoice due to sent out a reminder'), + 'REMIND_OVERDUE_1'=>_('Days after an invoice is due to send first reminder'), + 'REMIND_OVERDUE_2'=>_('Days after an invoice is due to send second reminder'), + 'REMIND_OVERDUE_3'=>_('Days after an invoice is due to send third and final reminder'), 'TAX_ID'=>_('TAX ID shown on invoices'), 'TAX_ID_NAME'=>_('TAX ID name shown on invoices'), )); diff --git a/modules/invoice/classes/controller/task/invoice.php b/modules/invoice/classes/controller/task/invoice.php index 6758c924..649c988d 100644 --- a/modules/invoice/classes/controller/task/invoice.php +++ b/modules/invoice/classes/controller/task/invoice.php @@ -58,9 +58,8 @@ class Controller_Task_Invoice extends Controller_Task { */ public function action_remind_due() { $action = array(); - // @todo This should go in a config somewhere - $days = 5; $key = 'remind_due'; + $days = ORM::factory('invoice')->config('REMIND_DUE'); foreach (ORM::factory('invoice')->list_due(time()+86400*$days) as $io) { // If we have already sent a reminder, we'll skip to the next one. @@ -103,18 +102,9 @@ class Controller_Task_Invoice extends Controller_Task { switch ($notice) { case 1: - // @todo This should go in a config somewhere - $days = 2; - break; - case 2: - // @todo This should go in a config somewhere - $days = 7; - break; - case 3: - // @todo This should go in a config somewhere - $days = 13; + $days = ORM::factory('invoice')->config('REMIND_OVERDUE_'.$notice); break; default: @@ -162,8 +152,8 @@ class Controller_Task_Invoice extends Controller_Task { */ public function action_services() { // Used to only process X invoices in a row. - // @todo This should be a configuration item. - $max = 10; + $max = ORM::factory('invoice')->config('GEN_INV_MAX'); + $action = array(); $snd = array(); // Our service next billing dates that need to be updated if this is successful. $sid = is_null($this->request->param('id')) ? NULL : explode(':',$this->request->param('id')); @@ -189,7 +179,7 @@ class Controller_Task_Invoice extends Controller_Task { } // If we have issued the max number of invoices this round, finish. - if (++$max_count > $max) + if ($max AND (++$max_count > $max)) break; // Start a new invoice. @@ -247,7 +237,7 @@ class Controller_Task_Invoice extends Controller_Task { } // Save our invoice. - if (! $io->saved() AND ! $io->save()) { + if ($io AND ! $io->saved() AND ! $io->save()) { print_r($io->items()); throw new Kohana_Exception('Failed to save invoice :invoice for service :service',array(':invoice'=>$io->id,':service'=>$so->id)); } @@ -264,8 +254,8 @@ class Controller_Task_Invoice extends Controller_Task { public function action_send() { // Used to only process X invoices in a row. - // @todo This should be a configuration item. - $max = 10; + $max = ORM::factory('invoice')->config('EMAIL_INV_MAX'); + $action = array(); $iid = $this->request->param('id'); $x = NULL; diff --git a/modules/invoice/classes/model/invoice.php b/modules/invoice/classes/model/invoice.php index 4469667b..26dc2ad0 100644 --- a/modules/invoice/classes/model/invoice.php +++ b/modules/invoice/classes/model/invoice.php @@ -370,18 +370,13 @@ class Model_Invoice extends ORMOSB { } public function min_due($date) { - // @todo This should be a DB confirm item - return ($date < time()) ? time()+Kohana::config('config.invoice.min_due_days')*86400 : $date; + return ($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(); - // @todo This is added here so we can do payments - $this->total_amt = $this->total(); - $this->billed_amt = 0; - // Save the invoice parent::save($validation); diff --git a/modules/invoice/classes/model/invoice/item.php b/modules/invoice/classes/model/invoice/item.php index 9b6ff5eb..f68a94e0 100644 --- a/modules/invoice/classes/model/invoice/item.php +++ b/modules/invoice/classes/model/invoice/item.php @@ -38,6 +38,27 @@ class Model_Invoice_Item extends ORMOSB { ), ); + // Items belonging to an invoice + private $subitems = array(); + private $subitems_load = FALSE; + + public function __construct($id = NULL) { + // Load our model. + parent::__construct($id); + + return $this->load_sub_items(); + } + + private function load_sub_items() { + // Load our sub items + if (! $this->subitems_load AND $this->loaded()) { + $this->subitems['tax'] = $this->invoice_item_tax->find_all()->as_array(); + $this->subitems_load = TRUE; + } + + return $this; + } + // Display a transaction number public function trannum() { return sprintf('%03s-%06s',$this->item_type,$this->id); @@ -146,29 +167,34 @@ class Model_Invoice_Item extends ORMOSB { // Save the invoice item parent::save($validation); - // Need to save the taxes and discounts associated with the invoice_item - // @todo This needs to only check if the records have previously been saved, and update them. + // Need to save the discounts associated with the invoice_item if ($this->saved()) { -//@todo When updating a record, we shouldnt create a new tax item. $iito = ORM::factory('invoice_item_tax'); - // Save TAX details - // @todo tax parameters should come from user session - foreach (Tax::detail(61,NULL,$this->subtotal()) as $tax) { - $iito->clear(); - $iito->invoice_item_id = $this->id; - $iito->tax_id = $tax['id']; - // @todo Rounding here should come from a global config - $iito->amount = round($tax['amount'],2); + if ($this->subitems_loaded) { + foreach (array('tax') as $i) + foreach ($this->subitems[$i] as $io) + if ($io->changed()) + $io->save(); - if (! $iito->check()) - throw new Kohana_Exception('Couldnt save tax for some reason - failed check()?'); + // Add TAX details + } else + // @todo tax parameters should come from user session + foreach (Tax::detail(61,NULL,$this->subtotal()) as $tax) { + $iito->clear(); + $iito->invoice_item_id = $this->id; + $iito->tax_id = $tax['id']; + // @todo Rounding here should come from a global config + $iito->amount = round($tax['amount'],2); - $iito->save(); + if (! $iito->check()) + throw new Kohana_Exception('Couldnt save tax for some reason - failed check()?'); - if (! $iito->saved()) - throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?'); - } + $iito->save(); + + if (! $iito->saved()) + throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?'); + } } else throw new Kohana_Exception('Couldnt save invoice_item for some reason?'); }