diff --git a/modules/adsl/classes/model/service/plugin/adsl.php b/modules/adsl/classes/model/service/plugin/adsl.php index 5a6f6881..57622432 100644 --- a/modules/adsl/classes/model/service/plugin/adsl.php +++ b/modules/adsl/classes/model/service/plugin/adsl.php @@ -36,6 +36,14 @@ class Model_Service_Plugin_ADSL extends Model_Service_Plugin { ->set('so',$this); } + /** + * When does this service expire + */ + public function expire() { + // @todo This should work out if the invoices are currently due, then the expire is the invoice date, otherwise the next invoice date. + return NULL; + } + public function name() { return $this->service_number; } diff --git a/modules/domain/classes/model/service/plugin/domain.php b/modules/domain/classes/model/service/plugin/domain.php index 57d2a03e..8a0d2077 100644 --- a/modules/domain/classes/model/service/plugin/domain.php +++ b/modules/domain/classes/model/service/plugin/domain.php @@ -44,6 +44,10 @@ class Model_Service_Plugin_Domain extends Model_Service_Plugin { return ''; } + public function expire() { + return $this->domain_expire; + } + public function name() { return sprintf('%s.%s',$this->display('domain_name'),$this->domain_tld->display('name')); } diff --git a/modules/host/classes/model/service/plugin/host.php b/modules/host/classes/model/service/plugin/host.php index cc076f4e..54efbeef 100644 --- a/modules/host/classes/model/service/plugin/host.php +++ b/modules/host/classes/model/service/plugin/host.php @@ -41,6 +41,10 @@ class Model_Service_Plugin_Host extends Model_Service_Plugin { return ''; } + public function expire() { + return $this->host_expire; + } + public function name() { return sprintf('%s.%s',$this->display('domain_name'),$this->domain_tld->display('name')); } diff --git a/modules/service/classes/controller/admin/service.php b/modules/service/classes/controller/admin/service.php index 3c5b7cad..9c79d8c5 100644 --- a/modules/service/classes/controller/admin/service.php +++ b/modules/service/classes/controller/admin/service.php @@ -19,6 +19,7 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin { 'listbycheckout'=>TRUE, 'listadslbilling'=>TRUE, 'listadslservices'=>TRUE, + 'listexpiring'=>TRUE, 'listdomainservices'=>TRUE, 'listdomainservicesbysupplier'=>TRUE, 'listdomainservicesbydnshost'=>TRUE, @@ -212,6 +213,32 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin { )); } + /** + * Show a list of services that are expiring or have expired + */ + public function action_listexpiring() { + $svs = ORM::factory('service')->list_expiring(); + + Sort::MAsort($svs,'expire()'); + + Block::add(array( + 'title'=>_('ADSL Services'), + 'body'=>Table::display( + $svs, + NULL, + array( + 'id'=>array('label'=>'ID','url'=>'user/service/view/'), + 'service_name()'=>array('label'=>'Service'), + 'expire(TRUE)'=>array('label'=>'Expires'), + 'due(TRUE)'=>array('label'=>'Due'), + ), + array( + 'type'=>'select', + 'form'=>'user/service/view', + )), + )); + } + public function action_listhspaservices() { $svs = ORM::factory('service')->list_bylistgroup('HSPA'); $data = $this->consoltraffic($svs,time()); @@ -560,7 +587,13 @@ class Controller_Admin_Service extends Controller_TemplateDefault_Admin { continue; } - list($id,$ref,$unknown,$linedata,$q,$cost,$total) = explode(',',$line); + // @todo This is to workaround SEP2012 CSV invoice which had extra columns. + if (count(explode(',',$line)) == 9) + list($id,$ref,$unknown,$unknown,$unknown,$linedata,$q,$cost,$total) = explode(',',$line); + elseif (count(explode(',',$line)) == 10) + list($id,$ref,$unknown,$unknown,$unknown,$unknown,$linedata,$q,$cost,$total) = explode(',',$line); + else + list($id,$ref,$unknown,$linedata,$q,$cost,$total) = explode(',',$line); // Extract the phone number from the $linedata @list($service,$description) = explode(':',(preg_replace('/([0-9]+)\s+-\s+(.*)$/',"$1:$2",$linedata))); diff --git a/modules/service/classes/model/service.php b/modules/service/classes/model/service.php index d047c739..d92255d5 100644 --- a/modules/service/classes/model/service.php +++ b/modules/service/classes/model/service.php @@ -75,6 +75,38 @@ class Model_Service extends ORMOSB { return is_null($plugin=$this->plugin()) ? $this->product->name() : $plugin->name(); } + /** + * Display how much is due on this service + */ + public function due($format=FALSE) { + $total = 0; + + foreach ($this->list_invoices(TRUE) as $io) + $total += $io->due(); + + return $format ? Currency::display($total) : $total; + } + + /** + * When does this service expire + */ + public function expire($format=FALSE) { + // For plugins the plugin determins expiry + $expire = (is_null($plugin=$this->plugin()) ? NULL : $plugin->expire()); + + // If $expire is NULL, we'll use the next invoice date + $expire = is_null($expire) ? $this->date_next_invoice-86400 : $expire; + + return $format ? Config::date($expire) : $expire; + } + + /** + * Determine if a service expires in the next $days. + */ + public function expiring($days) { + return time()+$days*86400 > $this->expire(); + } + /** * Return the service charge */ @@ -190,6 +222,32 @@ class Model_Service extends ORMOSB { return $result; } + /** + * List services expiring + */ + public function list_expiring($days=14) { + $result = array(); + + foreach ($this->list_active() as $so) + if ($so->expiring($days)) + array_push($result,$so); + + return $result; + } + + /** + * List invoices for this service + */ + public function list_invoices($due=FALSE) { + $return = array(); + + foreach ($this->invoice->find_all() as $io) + if (! $due OR $io->due()) + array_push($return,$io); + + return $return; + } + /** * List services that need to be billed. * diff --git a/modules/service/classes/model/service/plugin.php b/modules/service/classes/model/service/plugin.php index b005c59b..a2ca69d1 100644 --- a/modules/service/classes/model/service/plugin.php +++ b/modules/service/classes/model/service/plugin.php @@ -24,6 +24,11 @@ abstract class Model_Service_Plugin extends ORMOSB { */ abstract public function name(); + /** + * When does our service expire + */ + abstract public function expire(); + /** * Show our service name as defined in the DB with product suffix. */ diff --git a/modules/ssl/classes/model/service/plugin/ssl.php b/modules/ssl/classes/model/service/plugin/ssl.php index ddfb31e8..eae1ba94 100644 --- a/modules/ssl/classes/model/service/plugin/ssl.php +++ b/modules/ssl/classes/model/service/plugin/ssl.php @@ -40,6 +40,10 @@ class Model_Service_Plugin_SSL extends Model_Service_Plugin { ->set('so',$this); } + public function expire() { + return $this->valid_to(); + } + public function name() { if ($this->cert) { return sprintf('%s:%s',$this->ssl_ca->subject(),$this->display('cert'));