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

@@ -9,23 +9,16 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
// @todo This "module" menu items should belong in the module dir.
class Controller_Admin_Service extends Controller_Service {
protected $secure_actions = array(
'ajaxlist'=>TRUE,
'ajaxjson_traffic'=>TRUE,
'adslstat'=>TRUE,
'list'=>TRUE,
'listbycheckout'=>TRUE,
'listadslbilling'=>TRUE,
'listadslservices'=>TRUE,
'listexpiring'=>TRUE,
'listdomainservices'=>TRUE,
'listdomainservicesbysupplier'=>TRUE,
'listdomainservicesbydnshost'=>TRUE,
'listhostservices'=>TRUE,
'listhostservicesbysupplier'=>TRUE,
'listhspaservices'=>TRUE,
'listwebservices'=>TRUE,
'listinvoicesoon'=>TRUE,
'update'=>TRUE,
@@ -63,111 +56,6 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
$this->response->body($google->json());
}
/**
* Show a list of services
*/
public function action_list() {
$id = $this->request->param('id');
$svs = ORM::factory('Service');
if ($id)
$svs->where_active()->and_where('account_id','=',$id);
Block::add(array(
'title'=>_('Customer Services'),
'body'=>Table::display(
$svs->find_all(),
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'status'=>array('label'=>'Active'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
/**
* List all services by their default checkout method
*/
public function action_listbycheckout() {
$svs = array();
// @todo This needs to be configurable
$go = ORM::factory('Group',array('name'=>'Personal'));
foreach (ORM::factory('Account')->list_active() as $ao)
if ($ao->has_any('group',array($go)))
foreach ($ao->service->list_active() as $so)
if (! $so->service_billing->checkout_plugin_id)
array_push($svs,$so);
if ($svs)
Block::add(array(
'title'=>'Services that should be auto-billed',
'body'=>Table::display(
$svs,
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'status'=>array('label'=>'Active'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
'account->invoices_due_total(NULL,TRUE)'=>array('label'=>'Due Invoices'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
foreach (ORM::factory('Checkout')->list_active() as $co) {
$svs = array();
foreach ($co->account->find_all() as $ao)
foreach ($ao->service->list_active() as $so)
if ($so->service_billing->checkout_plugin_id == $co->id)
array_push($svs,$so);
if ($svs)
Block::add(array(
'title'=>$co->name,
'body'=>Table::display(
$svs,
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'status'=>array('label'=>'Active'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
'account->invoices_due_total(NULL,TRUE)'=>array('label'=>'Due Invoices'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
}
private function consoltraffic($svs,$date) {
$data = array();
@@ -200,51 +88,6 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
return $data;
}
public function action_listadslservices() {
$id = $this->request->param('id');
$svs = $list = ORM::factory('Service')->list_bylistgroup('ADSL');
if ($id) {
$svs = array();
foreach ($list as $so)
if ($so->account_id == $id)
array_push($svs,$so);
}
$google = GoogleChart::factory('ComboChart')
->dataurl(URL::link('admin','service/ajaxjson_traffic',TRUE))
->title(sprintf('ADSL traffic as at %s',date('Y-m-d',strtotime('yesterday'))));
Block::add(array('body'=>(string)$google));
Block::add(array(
'title'=>_('ADSL Services'),
'body'=>Table::display(
$svs,
NULL,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'name()'=>array('label'=>'Service'),
'plugin()->ipaddress()'=>array('label'=>'IP Address'),
'product->plugin()->speed'=>array('label'=>'Speed'),
'product->plugin()->allowance()'=>array('label'=>'Allowance'),
'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
'product->display("status")'=>array('label'=>'Current'),
),
array(
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
/**
* Show a list of services that are expiring or have expired
*/
@@ -271,74 +114,6 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
));
}
public function action_listhspaservices() {
$svs = ORM::factory('Service')->list_bylistgroup('HSPA');
$data = $this->consoltraffic($svs,time());
$google = GoogleChart::factory('Legacy')
->type('vertical_bar')
->title(sprintf('HSPA traffic as at %s',date('Y-m-d',strtotime('yesterday'))));
foreach ($data['data'] as $key => $values)
$google->data(array('yl'=>$key),array($key=>$values));
$google->data(array('yr'=>'services'),array('services'=>$data['svs']));
Block::add(array('body'=>(string)$google));
Block::add(array(
'title'=>_('HSPA Services'),
'body'=>Table::display(
$svs,
NULL,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'name()'=>array('label'=>'Service'),
'plugin()->ipaddress()'=>array('label'=>'IP Address'),
'product->plugin()->speed'=>array('label'=>'Speed'),
'product->plugin()->allowance()'=>array('label'=>'Allowance'),
'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
),
array(
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
public function action_listdomainservices() {
$svs = ORM::factory('Service')->list_bylistgroup('DOMAIN');
Sort::MAsort($svs,'name()');
Block::add(array(
'title'=>_('Domain Names'),
'body'=>Table::display(
$svs,
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'plugin()->display("domain_expire")'=>array('label'=>'Expire'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'display("date_next_invoice")'=>array('label'=>'Next Invoice'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
public function action_listdomainservicesbysupplier() {
$svs = ORM::factory('Service')->list_bylistgroup('DOMAIN');
Sort::MAsort($svs,'plugin()->domain_registrar_id,name()');
@@ -402,33 +177,6 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
));
}
public function action_listhostservices() {
$svs = ORM::factory('Service')->list_bylistgroup('HOST');
Sort::MAsort($svs,'name()');
Block::add(array(
'title'=>_('Hosting Services'),
'body'=>Table::display(
$svs,
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'plugin()->display("host_expire")'=>array('label'=>'Expire'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'display("date_next_invoice")'=>array('label'=>'Next Invoice'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
public function action_listhostservicesbysupplier() {
$svs = ORM::factory('Service')->list_bylistgroup('HOST');
Sort::MAsort($svs,'plugin()->host_server,name()');
@@ -691,7 +439,7 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
'recur_schedule'=>array('label'=>'Billing'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'charges_new()'=>array('label'=>'Charges','class'=>'right'),
'charges()'=>array('label'=>'Charges','class'=>'right'),
'status'=>array('label'=>'Active'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
@@ -787,8 +535,6 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin {
if (is_null($bt))
$bt = $iio->date_start;
// $doutput .= sprintf('%s: BT is: %s [%s]<br/>',$iio->id,Config::date($bt),$bt);
$pdata = Period::details($iio->recurring_schedule,$a ? NULL : $iio->product->price_recurr_weekday,$bt,TRUE);
switch ($iio->recurring_schedule) {

View File

@@ -1,287 +0,0 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Affiliate Service functions
*
* @package Service
* @category Controllers/Affiliate
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Affiliate_Service extends Controller_TemplateDefault_Affiliate {
protected $secure_actions = array(
'list'=>TRUE,
'listbycheckout'=>TRUE,
'listadslservices'=>TRUE,
'listdomainservices'=>TRUE,
'listhostservices'=>TRUE,
'listhspaservices'=>TRUE,
);
/**
* Show a list of services
*/
public function action_list() {
Block::add(array(
'title'=>_('Customer Services'),
'body'=>Table::display(
$this->filter(ORM::factory('Service')->list_active(),$this->ao->RTM->customers($this->ao->RTM),NULL,'account_id'),
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'status'=>array('label'=>'Active'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
/**
* List all services by their default checkout method
*/
public function action_listbycheckout() {
$svs = array();
// @todo This needs to be configurable
$go = ORM::factory('Group',array('name'=>'Personal'));
foreach (ORM::factory('Account')->list_active() as $ao)
if ($ao->has_any('group',array($go)))
foreach ($this->filter($ao->service->list_active(),$this->ao->RTM->customers($this->ao->RTM),NULL,'account_id') as $so)
if (! $so->service_billing->checkout_plugin_id)
array_push($svs,$so);
if ($svs)
Block::add(array(
'title'=>'Services that should be auto-billed',
'body'=>Table::display(
$svs,
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'status'=>array('label'=>'Active'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
'account->invoices_due_total(NULL,TRUE)'=>array('label'=>'Due Invoices'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
else
Block::add(array(
'title'=>'Services that should be auto-billed',
'body'=>_('None found'),
));
foreach (ORM::factory('Checkout')->list_active() as $co) {
$svs = array();
foreach ($co->account->find_all() as $ao)
foreach ($this->filter($ao->service->list_active(),$this->ao->RTM->customers($this->ao->RTM),NULL,'account_id') as $so)
if ($so->service_billing->checkout_plugin_id == $co->id)
array_push($svs,$so);
if ($svs)
Block::add(array(
'title'=>$co->name,
'body'=>Table::display(
$svs,
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'status'=>array('label'=>'Active'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
'account->invoices_due_total(NULL,TRUE)'=>array('label'=>'Due Invoices'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
}
private function consoltraffic($svs,$date) {
$data = array();
foreach ($svs as $so) {
$c = array();
foreach ($so->plugin()->get_traffic_data_monthly($date) as $metric => $ma) {
foreach ($ma as $month => $traffic) {
// Only count the service once, not for each metric.
if (! isset($c[$month])) {
if (isset($data['svs'][$month]))
$data['svs'][$month] += 1;
else
$data['svs'][$month] = 1;
$c[$month] = 1;
}
if (isset($data['data'][$metric][$month]))
$data['data'][$metric][$month] += (int)$traffic;
else
$data['data'][$metric][$month] = (int)$traffic;
}
}
}
ksort($data['svs']);
foreach ($data['data'] as $metric => $details)
ksort($data['data'][$metric]);
return $data;
}
public function action_listadslservices() {
$svs = $this->filter(ORM::factory('Service')->list_bylistgroup('ADSL'),$this->ao->RTM->customers($this->ao->RTM),'name()','account_id');
$data = $this->consoltraffic($svs,time());
$google = GoogleChart::factory('Legacy')
->type('vertical_bar')
->title(sprintf('ADSL traffic as at %s',date('Y-m-d',strtotime('yesterday'))));
foreach ($data['data'] as $key => $values)
$google->sdata(array('yl'=>$key),array($key=>$values));
$google->sdata(array('yr'=>'services'),array('services'=>$data['svs']));
Block::add(array('body'=>(string)$google));
Block::add(array(
'title'=>_('ADSL Services'),
'body'=>Table::display(
$svs,
50,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'name()'=>array('label'=>'Service'),
'plugin()->ipaddress()'=>array('label'=>'IP Address'),
'product->plugin()->speed'=>array('label'=>'Speed'),
'product->plugin()->allowance()'=>array('label'=>'Allowance'),
'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
),
array(
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
public function action_listhspaservices() {
$svs = $this->filter(ORM::factory('Service')->list_bylistgroup('HSPA'),$this->ao->RTM->customers($this->ao->RTM),'name()','account_id');
$data = $this->consoltraffic($svs,time());
$google = GoogleChart::factory('Legacy')
->type('vertical_bar')
->title(sprintf('HSPA traffic as at %s',date('Y-m-d',strtotime('yesterday'))));
foreach ($data['data'] as $key => $values)
$google->sdata(array('yl'=>$key),array($key=>$values));
$google->sdata(array('yr'=>'services'),array('services'=>$data['svs']));
Block::add(array('body'=>(string)$google));
Block::add(array(
'title'=>_('HSPA Services'),
'body'=>Table::display(
$svs,
NULL,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'name()'=>array('label'=>'Service'),
'plugin()->ipaddress()'=>array('label'=>'IP Address'),
'product->plugin()->speed'=>array('label'=>'Speed'),
'product->plugin()->allowance()'=>array('label'=>'Allowance'),
'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'date_next_invoice'=>array('label'=>'Next Invoice'),
),
array(
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
public function action_listdomainservices() {
Block::add(array(
'title'=>_('Domain Names'),
'body'=>Table::display(
$this->filter(ORM::factory('Service')->list_bylistgroup('DOMAIN'),$this->ao->RTM->customers($this->ao->RTM),'name()','account_id'),
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'plugin()->display("domain_expire")'=>array('label'=>'Expire'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'display("date_next_invoice")'=>array('label'=>'Next Invoice'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
public function action_listhostservices() {
Block::add(array(
'title'=>_('Hosting Services'),
'body'=>Table::display(
$this->filter(ORM::factory('Service')->list_bylistgroup('HOST'),$this->ao->RTM->customers($this->ao->RTM),'name()','account_id'),
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'plugin()->display("host_expire")'=>array('label'=>'Expire'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'),
'display("date_next_invoice")'=>array('label'=>'Next Invoice'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
}
}
?>

View File

@@ -0,0 +1,112 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Reseller Service functions
*
* @package Service
* @category Controllers/Reseller
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Reseller_Service extends Controller_Service {
protected $secure_actions = array(
'list'=>TRUE,
'listbycheckout'=>TRUE,
'listhostservices'=>TRUE,
);
/**
* Show a list of services
*/
public function action_list() {
Block::factory()
->title('Customer Services')
->title_icon('icon-th-list')
->body(Table::factory()
->jssort('services')
->data($this->filter(ORM::factory('Service')->find_all(),$this->ao->RTM->customers($this->ao->RTM),'account_id'))
->columns(array(
'id'=>'ID',
'service_name()'=>'Service',
'recur_schedule'=>'Billing',
'price(TRUE,TRUE)'=>'Price',
'status(TRUE)'=>'Active',
'account->accnum()'=>'Cust ID',
'account->name()'=>'Customer',
))
->prepend(array(
'id'=>array('url'=>URL::link('user','service/view/')),
))
);
}
/**
* List all services by their default checkout method
*/
public function action_listbycheckout() {
$svs = array();
// @todo This needs to be configurable
$go = ORM::factory('Group',array('name'=>'Personal'));
foreach (ORM::factory('Account')->list_active() as $ao)
if ($ao->has_any('group',array($go)))
foreach ($this->filter($ao->service->list_active(),$this->ao->RTM->customers($this->ao->RTM),'account_id') as $so)
if (! $so->service_billing->checkout_plugin_id)
array_push($svs,$so);
if ($svs)
Block::factory()
->title('Services that should be auto-billed')
->title_icon('icon-pencil')
->body(Table::factory()
->jssort('services')
->data($svs)
->columns(array(
'id'=>'ID',
'service_name()'=>'Service',
'recur_schedule'=>'Billing',
'price(TRUE,TRUE)'=>'Price',
'account->accnum()'=>'Cust ID',
'account->name()'=>'Customer',
'date_next_invoice'=>'Next Invoice',
'due(TRUE)'=>'Due Invoices',
))
->prepend(array(
'id'=>array('url'=>URL::link('user','service/view/')),
))
);
foreach (ORM::factory('Checkout')->find_all() as $co) {
$svs = array();
foreach ($co->account->find_all() as $ao)
foreach ($this->filter($ao->service->list_active(),$this->ao->RTM->customers($this->ao->RTM),'account_id') as $so)
if ($so->service_billing->checkout_plugin_id == $co->id)
array_push($svs,$so);
if ($svs)
Block::factory()
->title($co->name)
->title_icon('icon-repeat')
->body(Table::factory()
->jssort($co->id)
->data($svs)
->columns(array(
'id'=>'ID',
'service_name()'=>'Service',
'recur_schedule'=>'Billing',
'price(TRUE,TRUE)'=>'Price',
'account->accnum()'=>'Cust ID',
'account->name()'=>'Customer',
'date_next_invoice'=>'Next Invoice',
'due(TRUE)'=>'Due Invoices',
))
->prepend(array(
'id'=>array('url'=>URL::link('user','service/view/')),
))
);
}
}
}
?>

View File

@@ -9,13 +9,16 @@
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_User_Service extends Controller_TemplateDefault_User {
class Controller_User_Service extends Controller_Service {
protected $secure_actions = array(
'ajaxmanage'=>TRUE,
'list'=>TRUE,
'view'=>TRUE,
);
/**
* This ajax functions obtains the manage button login/password for this service
*/
public function action_ajaxmanage() {
$this->auto_render = FALSE;
@@ -36,24 +39,23 @@ class Controller_User_Service extends Controller_TemplateDefault_User {
* Show a list of services
*/
public function action_list() {
Block::add(array(
'title'=>sprintf('%s: %s - %s',_('Services For'),$this->ao->accnum(),$this->ao->name(TRUE)),
'body'=>Table::display(
$this->ao->service->find_all(),
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('user','service/view/')),
'service_name()'=>array('label'=>'Details'),
'recur_schedule'=>array('label'=>'Billing'),
'price(TRUE,TRUE)'=>array('label'=>'Price','class'=>'right'),
'status'=>array('label'=>'Active'),
),
array(
'page'=>TRUE,
'type'=>'select',
'form'=>URL::link('user','service/view'),
)),
));
Block::factory()
->title(sprintf('Services for Account: %s',$this->ao->accnum()))
->title_icon('icon-th-list')
->body(Table::factory()
->jssort('services')
->data($this->ao->service->find_all())
->columns(array(
'id'=>'ID',
'service_name()'=>'Service',
'recur_schedule'=>'Billing',
'price(TRUE,TRUE)'=>'Price',
'status(TRUE)'=>'Active',
))
->prepend(array(
'id'=>array('url'=>URL::link('user','service/view/')),
))
);
}
public function action_view() {
@@ -61,18 +63,16 @@ class Controller_User_Service extends Controller_TemplateDefault_User {
$so = ORM::factory('Service',$id);
if (! $so->loaded() OR ! Auth::instance()->authorised($so->account)) {
$this->template->content = 'Unauthorised or doesnt exist?';
return FALSE;
}
if (! $so->loaded() OR ! Auth::instance()->authorised($so->account))
throw HTTP_Exception::factory(403,'Service either doesnt exist, or you are not authorised to see it');
$output .= View::factory($this->viewpath())
->set('so',$so);
$output .= View::factory('service/user/view')
->set('o',$so);
Block::add(array(
'title'=>sprintf('%s: %s',$so->id(),$so->service_name()),
'body'=>$output,
));
Block::factory()
->title(sprintf('%s: %s',$so->id(),$so->service_name()))
->title_icon('icon-list-alt')
->body($output);
}
}
?>

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.
*