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('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');

View File

@ -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'),
));

View File

@ -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;

View File

@ -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);

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
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?');
}