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

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