Move invoice config items into the DB

This commit is contained in:
Deon George 2012-01-12 10:23:35 +11:00
parent 8d53924988
commit 14d5f1939e
5 changed files with 60 additions and 42 deletions

View File

@ -30,7 +30,7 @@ class Controller_TemplateDefault_Admin extends Controller_TemplateDefault_User {
->set('mc',$mc) ->set('mc',$mc)
->set('key',$k) ->set('key',$k)
->set('info',$v) ->set('info',$v)
->set('val',empty($mc[$k]) ? '' : $mc[$k]); ->set('val',isset($mc[$k]) ? $mc[$k] : '');
$output .= View::factory('setup/admin/module/foot'); $output .= View::factory('setup/admin/module/foot');

View File

@ -22,8 +22,15 @@ class Controller_Admin_Invoice extends Controller_TemplateDefault_Admin {
public function action_setup() { public function action_setup() {
$this->setup(array( $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_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'), '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'=>_('TAX ID shown on invoices'),
'TAX_ID_NAME'=>_('TAX ID name shown on invoices'), 'TAX_ID_NAME'=>_('TAX ID name shown on invoices'),
)); ));

View File

@ -58,9 +58,8 @@ class Controller_Task_Invoice extends Controller_Task {
*/ */
public function action_remind_due() { public function action_remind_due() {
$action = array(); $action = array();
// @todo This should go in a config somewhere
$days = 5;
$key = 'remind_due'; $key = 'remind_due';
$days = ORM::factory('invoice')->config('REMIND_DUE');
foreach (ORM::factory('invoice')->list_due(time()+86400*$days) as $io) { 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. // 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) { switch ($notice) {
case 1: case 1:
// @todo This should go in a config somewhere
$days = 2;
break;
case 2: case 2:
// @todo This should go in a config somewhere
$days = 7;
break;
case 3: case 3:
// @todo This should go in a config somewhere $days = ORM::factory('invoice')->config('REMIND_OVERDUE_'.$notice);
$days = 13;
break; break;
default: default:
@ -162,8 +152,8 @@ class Controller_Task_Invoice extends Controller_Task {
*/ */
public function action_services() { public function action_services() {
// Used to only process X invoices in a row. // Used to only process X invoices in a row.
// @todo This should be a configuration item. $max = ORM::factory('invoice')->config('GEN_INV_MAX');
$max = 10;
$action = array(); $action = array();
$snd = array(); // Our service next billing dates that need to be updated if this is successful. $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')); $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 we have issued the max number of invoices this round, finish.
if (++$max_count > $max) if ($max AND (++$max_count > $max))
break; break;
// Start a new invoice. // Start a new invoice.
@ -247,7 +237,7 @@ class Controller_Task_Invoice extends Controller_Task {
} }
// Save our invoice. // Save our invoice.
if (! $io->saved() AND ! $io->save()) { if ($io AND ! $io->saved() AND ! $io->save()) {
print_r($io->items()); print_r($io->items());
throw new Kohana_Exception('Failed to save invoice :invoice for service :service',array(':invoice'=>$io->id,':service'=>$so->id)); 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() { public function action_send() {
// Used to only process X invoices in a row. // Used to only process X invoices in a row.
// @todo This should be a configuration item. $max = ORM::factory('invoice')->config('EMAIL_INV_MAX');
$max = 10;
$action = array(); $action = array();
$iid = $this->request->param('id'); $iid = $this->request->param('id');
$x = NULL; $x = NULL;

View File

@ -370,18 +370,13 @@ class Model_Invoice extends ORMOSB {
} }
public function min_due($date) { public function min_due($date) {
// @todo This should be a DB confirm item return ($date < time()) ? time()+ORM::factory('invoice')->config('DUE_DAYS_MIN')*86400 : $date;
return ($date < time()) ? time()+Kohana::config('config.invoice.min_due_days')*86400 : $date;
} }
public function save(Validation $validation = NULL) { public function save(Validation $validation = NULL) {
// Our items will be clobbered once we save the object, so we need to save it here. // Our items will be clobbered once we save the object, so we need to save it here.
$items = $this->items(); $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 // Save the invoice
parent::save($validation); parent::save($validation);

View File

@ -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 // Display a transaction number
public function trannum() { public function trannum() {
return sprintf('%03s-%06s',$this->item_type,$this->id); return sprintf('%03s-%06s',$this->item_type,$this->id);
@ -146,29 +167,34 @@ class Model_Invoice_Item extends ORMOSB {
// Save the invoice item // Save the invoice item
parent::save($validation); parent::save($validation);
// Need to save the taxes and discounts associated with the invoice_item // Need to save the discounts associated with the invoice_item
// @todo This needs to only check if the records have previously been saved, and update them.
if ($this->saved()) { if ($this->saved()) {
//@todo When updating a record, we shouldnt create a new tax item.
$iito = ORM::factory('invoice_item_tax'); $iito = ORM::factory('invoice_item_tax');
// Save TAX details if ($this->subitems_loaded) {
// @todo tax parameters should come from user session foreach (array('tax') as $i)
foreach (Tax::detail(61,NULL,$this->subtotal()) as $tax) { foreach ($this->subitems[$i] as $io)
$iito->clear(); if ($io->changed())
$iito->invoice_item_id = $this->id; $io->save();
$iito->tax_id = $tax['id'];
// @todo Rounding here should come from a global config
$iito->amount = round($tax['amount'],2);
if (! $iito->check()) // Add TAX details
throw new Kohana_Exception('Couldnt save tax for some reason - failed check()?'); } 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()) $iito->save();
throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?');
} if (! $iito->saved())
throw new Kohana_Exception('Couldnt save tax for some reason - failed save()?');
}
} else } else
throw new Kohana_Exception('Couldnt save invoice_item for some reason?'); throw new Kohana_Exception('Couldnt save invoice_item for some reason?');
} }