Minor fixes to statement, services and internal things

Many misc updates
This commit is contained in:
Deon George
2011-10-14 16:44:12 +11:00
parent 7876a16413
commit 56c11507f4
71 changed files with 2192 additions and 677 deletions

View File

@@ -148,7 +148,7 @@ class Model_Invoice extends ORMOSB {
}
/**
* Get a list of services on an invoice
* Get a list of invoice_items for a service_id on an invoice
*
* We use this to list details by service on an invoice.
*/
@@ -158,15 +158,15 @@ class Model_Invoice extends ORMOSB {
$items = $this->items();
foreach ($items as $ito)
if ($ito->service_id AND empty($result[$ito->service_id]))
$result[$ito->service_id] = $ito->service;
if ($ito->service_id AND empty($result[$ito->service_id]))
$result[$ito->service_id] = $ito;
return $result;
}
/**
* Return all invoice items for a service optionally by recurring schedule
*/
*/
public function items_service($sid,$rs=NULL) {
$result = array();
$items = $this->items();
@@ -211,11 +211,11 @@ class Model_Invoice extends ORMOSB {
$result = array();
foreach ($this->items() as $ito) {
// We conly summaries item_type=0
// We only summarise item_type=0
if (! $ito->item_type == 0)
continue;
$t = $ito->product_id;
$t = $ito->product->name();
if (! isset($result[$t])) {
$result[$t]['quantity'] = 0;
@@ -303,14 +303,17 @@ class Model_Invoice extends ORMOSB {
}
public function min_due($date) {
// @todo This should be configurable;
return ($date < time()) ? time() : $date;
return ($date < time()) ? time()+Kohana::config('config.invoice.min_due_days')*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);
@@ -331,12 +334,14 @@ class Model_Invoice extends ORMOSB {
throw new Kohana_Exception('Problem saving invoice_item for invoice :invoice - Failed save()',array(':invoice'=>$invoice->id));
}
// @todo Need to save tax information
// @todo Need to save discount information
}
} else
throw new Kohana_Exception('Couldnt save invoice for some reason?');
return TRUE;
}
/**
@@ -387,13 +392,29 @@ class Model_Invoice extends ORMOSB {
return $this->saved();
}
/**
* Return a list of affiliates associated with this invoice (via the service)
*/
public function service_affiliates() {
$return = array();
foreach ($this->items() as $io)
array_push($return,$io->service->affiliate_id);
return $return;
}
/** LIST FUNCTIONS **/
private function _list_active() {
return ORM::factory('invoice')->where('status','=',1);
}
private function _list_due() {
static $result = array();
if (! $result)
foreach (ORM::factory('invoice')->where('status','=',1)->find_all() as $io)
foreach ($this->_list_active()->find_all() as $io)
if ($io->due())
array_push($result,$io);
@@ -445,12 +466,20 @@ class Model_Invoice extends ORMOSB {
if ($io->due_date > time())
if (is_null($time))
array_push($result,$io);
elseif ($this->due_date <= $time)
elseif ($io->due_date <= $time)
array_push($result,$io);
return $result;
}
/**
* Return a list of invoices that need to be sent.
* @todo This should be optimised a little to return only invoices to send, instead of looking for them.
*/
public function list_tosend() {
return ORM::factory('invoice')->where('status','=',1)->where_open()->where('print_status','is',NULL)->or_where('print_status','!=',1)->where_close();
}
public function html() {
// @todo This should be in a config file.
$css = '<style type="text/css">';

View File

@@ -19,11 +19,17 @@ class Model_Invoice_Item extends ORMOSB {
'invoice'=>array(),
'service'=>array()
);
protected $_has_one = array(
'charge'=>array('far_key'=>'charge_id','foreign_key'=>'id')
);
protected $_has_many = array(
'invoice_item_tax'=>array('far_key'=>'id')
);
protected $_display_filters = array(
'date_orig'=>array(
array('Config::date',array(':value')),
),
'date_start'=>array(
array('Config::date',array(':value')),
),
@@ -83,8 +89,11 @@ class Model_Invoice_Item extends ORMOSB {
return ($this->item_type == 0 OR $this->quantity == 1) ? HTML::nbsp('') : sprintf('%s@%3.2f',$this->quantity,$this->price_base);
}
// @todo This might not be required.
public function invoice_detail_items() {
// @todo To fix up this function - needs to be better formed.
if ($this->item_type == 5)
return $this->charge->details('invoice_detail_items');
if ($this->item_type != 0)
return;
@@ -96,6 +105,7 @@ class Model_Invoice_Item extends ORMOSB {
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.
if ($this->saved()) {
$iito = ORM::factory('invoice_item_tax');