Rework service, change module_method configuration

This commit is contained in:
Deon George
2013-06-04 21:50:41 +10:00
parent 1a40f95b30
commit 951845f6c6
35 changed files with 780 additions and 1022 deletions

View File

@@ -18,6 +18,7 @@ class Model_Service extends ORM_OSB {
'service_billing'=>array('far_key'=>'account_billing_id','foreign_key'=>'id'),
);
protected $_has_many = array(
'charge'=>array('far_key'=>'id'),
'invoice_item'=>array('far_key'=>'id'),
'invoice'=>array('through'=>'invoice_item'),
'service_change'=>array('far_key'=>'id'),
@@ -46,33 +47,27 @@ class Model_Service extends ORM_OSB {
);
/**
* Return the object of the product plugin
* Get the additional charges associated with this service
*/
public function plugin($type='') {
if (! $this->product->prod_plugin_file)
return NULL;
public function charges($unprocessed=TRUE,$format=FALSE) {
$result = 0;
$o = ORM::factory(Kohana::classname(sprintf('Service_Plugin_%s',$this->product->prod_plugin_file)),array('service_id'=>$this->id));
foreach ($this->charge_list($unprocessed) as $co)
$result += $co->total();
return $type ? $o->$type : $o;
return $format ? Currency::display($result) : $result;
}
/**
* Display the service number
*/
public function id() {
return sprintf('%05s',$this->id);
}
public function charge_list($unprocessed=FALSE) {
$x = $this->charge;
/**
* Display the service product name
*/
public function name() {
return is_null($plugin=$this->plugin()) ? $this->product->title() : $plugin->name();
}
if ($unprocessed)
$x->where_open()
->where('processed','IS',NULL)
->or_where('processed','=',FALSE)
->where_close();
public function pending_change() {
return count($this->service_change->where_active()->where_open()->and_where('complete','!=',1)->or_where('complete','IS',null)->where_close()->find_all()->as_array());
return $x->find_all();
}
/**
@@ -81,7 +76,7 @@ class Model_Service extends ORM_OSB {
public function due($format=FALSE) {
$total = 0;
foreach ($this->list_invoices(TRUE) as $io)
foreach ($this->invoice_list(TRUE) as $io)
$total += $io->due();
return $format ? Currency::display($total) : $total;
@@ -103,10 +98,80 @@ class Model_Service extends ORM_OSB {
/**
* Determine if a service expires in the next $days.
*/
public function expiring($days) {
public function expiring($days=0) {
return time()+$days*86400 > $this->expire();
}
/**
* Display the service number
*/
public function id() {
return sprintf('%05s',$this->id);
}
/**
* List invoices for this service
*/
public function invoice_list($due=FALSE) {
$result = array();
$x = $this->invoice->distinct('id');
// If we only want due invoices, we can speed things up by only looking for unprocessed
if ($due)
$x->where_unprocessed();
foreach ($x->find_all() as $io)
if (! $due OR $io->due())
array_push($result,$io);
return $result;
}
/**
* Display the service product name
*/
public function name() {
return is_null($plugin=$this->plugin()) ? $this->product->title() : $plugin->name();
}
/**
* Returns TRUE of this service has a planend change
*/
public function pending_change() {
return $this->service_change()->loaded() ? TRUE : FALSE;
}
/**
* Show the product that this service will be changed to
*/
public function pending_product() {
return $this->service_change()->product;
}
/**
* Return the object of the product plugin
*/
public function plugin($type='') {
if (! $this->product->prod_plugin_file)
return NULL;
$o = ORM::factory(Kohana::classname(sprintf('Service_Plugin_%s',$this->product->prod_plugin_file)),array('service_id'=>$this->id));
return $type ? $o->$type : $o;
}
public function service_change() {
return $this->service_change->where_active()->where_open()->and_where('complete','!=',1)->or_where('complete','IS',null)->where_close()->find();
}
/**
* Return a descriptive name for this service
*/
public function service_name() {
return is_null($plugin=$this->plugin()) ? $this->name() : $plugin->service_name();
}
/**
* Return the service charge
*/
@@ -122,32 +187,18 @@ class Model_Service extends ORM_OSB {
return $format ? Currency::display($p) : $p;
}
public function service_name() {
return is_null($plugin=$this->plugin()) ? $this->name() : $plugin->service_name();
}
public function service_view() {
return is_null($plugin=$this->plugin()) ? HTML::nbsp('') : $plugin->service_view();
}
/**
* Display the product feature summary
*/
public function product_feature_summary() {
return $this->product->feature_summary();
}
/**
* Render some details for specific calls, eg: invoice
*/
public function details($type) {
$plugin = $this->plugin();
switch ($type) {
case 'invoice_detail_items':
if (is_null($plugin = $this->plugin()))
return array();
else
return $plugin->_details($type);
break;
return is_null($plugin) ? array() : $plugin->_details($type);
case 'service_view':
return is_null($plugin) ? HTML::nbsp('') : $plugin->service_view();
default:
throw new Kohana_Exception('Unkown detail request :type',array(':type'=>$type));
@@ -170,7 +221,7 @@ class Model_Service extends ORM_OSB {
// @todo To implement
public function charges_new() {
return 0;
return $this->charges();
}
/** LIST FUNCTIONS **/
@@ -198,7 +249,20 @@ class Model_Service extends ORM_OSB {
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
}
/**
* List all products by their plugin type
*/
public function list_byplugin($plugin) {
return $this
->join('product')
->on($this->table_name().'.site_id','=','product.site_id') // @todo This should be automatic
->on($this->table_name().'.product_id','=','product.id')
->where('prod_plugin_file','=',$plugin)
->and_where('service.status','=',TRUE)
->find_all();
}
public function list_bylistgroup($cat) {
$result = array();
@@ -223,19 +287,6 @@ class Model_Service extends ORM_OSB {
return $result;
}
/**
* List invoices for this service
*/
public function list_invoices($due=FALSE) {
$result = array();
foreach ($this->invoice->find_all() as $io)
if (! $due OR $io->due())
array_push($result,$io);
return $result;
}
/**
* List services that need to be billed.
*