From 609a72425b197737c15e92c26c69c98787597c63 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 16 Jun 2013 23:36:47 +1000 Subject: [PATCH] Some invoice cleanup, and Task updating conversion --- application/classes/Model/RTM.php | 41 ++++++++++-- .../classes/Controller/Admin/Invoice.php | 41 +----------- .../classes/Controller/Task/Invoice.php | 42 ------------ .../invoice/classes/Model/Invoice/Item.php | 3 + .../invoice/classes/Task/Invoice/Complete.php | 2 +- .../classes/Task/Invoice/Listoverdue.php | 64 +++++++++++++++++++ .../views/invoice/task/list_overdue_body.php | 6 -- .../views/invoice/task/list_overdue_foot.php | 1 - .../views/invoice/task/list_overdue_head.php | 7 -- .../views/invoice/task/listoverdue.php | 20 ++++++ 10 files changed, 126 insertions(+), 101 deletions(-) create mode 100644 modules/invoice/classes/Task/Invoice/Listoverdue.php delete mode 100644 modules/invoice/views/invoice/task/list_overdue_body.php delete mode 100644 modules/invoice/views/invoice/task/list_overdue_foot.php delete mode 100644 modules/invoice/views/invoice/task/list_overdue_head.php create mode 100644 modules/invoice/views/invoice/task/listoverdue.php diff --git a/application/classes/Model/RTM.php b/application/classes/Model/RTM.php index 56578d9d..fee28708 100644 --- a/application/classes/Model/RTM.php +++ b/application/classes/Model/RTM.php @@ -16,8 +16,22 @@ class Model_RTM extends ORM_OSB { protected $_has_many = array( 'customer' => array('model'=>'account','far_key'=>'id','foreign_key'=>'rtm_id'), 'agent' => array('model'=>'rtm','far_key'=>'id','foreign_key'=>'parent_id'), + 'supplier' => array('model'=>'rtm','far_key'=>'parent_id','foreign_key'=>'id'), ); + public function agents(Model_RTM $rtmo) { + $result = array($rtmo); + + foreach ($rtmo->agents_direct() as $artmo) + $result = Arr::merge($result,$rtmo->agents($artmo)); + + return $result; + } + + public function agents_direct() { + return $this->agent->find_all(); + } + public function customers(Model_RTM $rtmo) { // If our RTM is NULL, then we are our only customer. if (is_null($rtmo->id)) @@ -34,12 +48,31 @@ class Model_RTM extends ORM_OSB { return $result; } - public function agents_direct() { - return $this->agent->find_all(); - } - public function customers_direct() { return $this->customer->find_all(); } + + public function peers() { + $result = array(); + + foreach (ORM::factory('RTM')->where('id','=',$this->id)->find_all() as $rtmo) + array_push($result,$rtmo->account); + + return $result; + } + + public function suppliers(Model_RTM $rtmo) { + $result = array($rtmo); + + foreach ($rtmo->suppliers_direct() as $srtmo) + $result = Arr::merge($result,$rtmo->suppliers($srtmo)); + + return $result; + } + + public function suppliers_direct() { + return $this->supplier->find_all(); + } + } ?> diff --git a/modules/invoice/classes/Controller/Admin/Invoice.php b/modules/invoice/classes/Controller/Admin/Invoice.php index 4dbcfef5..2884df82 100644 --- a/modules/invoice/classes/Controller/Admin/Invoice.php +++ b/modules/invoice/classes/Controller/Admin/Invoice.php @@ -8,14 +8,9 @@ * @author Deon George * @copyright (c) 2009-2013 Open Source Billing * @license http://dev.osbill.net/license.html - * - * Column Definitions: - * + price_type: 0=One Time, 1=Recurring, 2=Trial, 3=Extra Item - * + item_type: 0=MAIN Service Item,2=?,3=?,4=Connection/Setup,5=Excess Service Item,6=Change Service,126=Payment Fee,127=Late Fee */ -class Controller_Admin_Invoice extends Controller_TemplateDefault_Admin { +class Controller_Admin_Invoice extends Controller_Invoice { protected $secure_actions = array( - 'list'=>TRUE, 'setup'=>TRUE, ); @@ -34,39 +29,5 @@ class Controller_Admin_Invoice extends Controller_TemplateDefault_Admin { 'TAX_ID_NAME'=>_('TAX ID name shown on invoices'), )); } - - /** - * Show a list of invoices - */ - public function action_list() { - $id = $this->request->param('id'); - - $invs = ORM::factory('Invoice'); - - if ($id) - $invs->where('account_id','=',$id); - - Block::add(array( - 'title'=>_('System Customer Invoices'), - 'body'=>Table::display( - $invs->find_all(), - 25, - array( - 'id'=>array('label'=>'ID','url'=>URL::link('user','invoice/view/')), - 'date_orig'=>array('label'=>'Date'), - 'total(TRUE)'=>array('label'=>'Total','class'=>'right'), - 'total_credits(TRUE)'=>array('label'=>'Credits','class'=>'right'), - 'payments_total(TRUE)'=>array('label'=>'Payments','class'=>'right'), - 'due(TRUE)'=>array('label'=>'Still Due','class'=>'right'), - 'account->accnum()'=>array('label'=>'Cust ID'), - 'account->name()'=>array('label'=>'Customer'), - ), - array( - 'page'=>TRUE, - 'type'=>'select', - 'form'=>URL::link('user','invoice/view'), - )), - )); - } } ?> diff --git a/modules/invoice/classes/Controller/Task/Invoice.php b/modules/invoice/classes/Controller/Task/Invoice.php index e238ed56..c8d0f7ea 100644 --- a/modules/invoice/classes/Controller/Task/Invoice.php +++ b/modules/invoice/classes/Controller/Task/Invoice.php @@ -12,48 +12,6 @@ class Controller_Task_Invoice extends Controller_Task { public $auto_render = FALSE; - /** - * Email a list of invoice balances - * - * This function is typically used to list the overdue invoices to the admins - * @param string mode The callback method to use as the data list eg: overdue - */ - public function action_list() { - $mode = $this->request->param('id'); - - $i = ORM::factory('Invoice'); - $tm = 'list_'.$mode; - - if (! method_exists($i,$tm)) - throw new Kohana_Exception('Unknown Task List command :command',array(':command'=>$mode)); - - $total = $numinv = 0; - $duelist = View::factory('invoice/task/'.$tm.'_head'); - foreach ($i->$tm() as $t) { - $duelist .= View::factory('invoice/task/'.$tm.'_body') - ->set('io',$t); - - $numinv++; - $total += $t->due(); - } - $duelist .= View::factory('invoice/task/'.$tm.'_foot'); - - // Send our email - $et = Email_Template::instance('task_invoice_list_overdue'); - - // @todo Update this to be dynamic - $et->to = array('account'=>array(1,68)); - $et->variables = array( - 'TABLE'=>$duelist, - 'NUM_INV'=>$numinv, - 'TOTAL'=>$total, - ); - $et->send(); - - $output = sprintf('List (%s) sent to: %s',$mode,implode(',',array_keys($et->to))); - $this->response->body($output); - } - /** * Email a customers a reminder of their upcoming invoices that are due. */ diff --git a/modules/invoice/classes/Model/Invoice/Item.php b/modules/invoice/classes/Model/Invoice/Item.php index 3b3ff6e4..04d133d9 100644 --- a/modules/invoice/classes/Model/Invoice/Item.php +++ b/modules/invoice/classes/Model/Invoice/Item.php @@ -8,6 +8,9 @@ * @author Deon George * @copyright (c) 2009-2013 Open Source Billing * @license http://dev.osbill.net/license.html + * + * Column Definitions: + * + item_type: 0=MAIN Service Item,2=?,3=?,4=Connection/Setup,5=Excess Service Item,6=Change Service,126=Payment Fee,127=Late Fee */ class Model_Invoice_Item extends ORM_OSB implements Invoicable { // Relationships diff --git a/modules/invoice/classes/Task/Invoice/Complete.php b/modules/invoice/classes/Task/Invoice/Complete.php index c5fd705b..d983156f 100644 --- a/modules/invoice/classes/Task/Invoice/Complete.php +++ b/modules/invoice/classes/Task/Invoice/Complete.php @@ -9,7 +9,7 @@ * @copyright (c) 2009-2013 Open Source Billing * @license http://dev.osbill.net/license.html */ -class Task_Invoice_Complete extends Task { +class Task_Invoice_Complete extends Minion_Task { protected function _execute(array $params) { $c = 0; diff --git a/modules/invoice/classes/Task/Invoice/Listoverdue.php b/modules/invoice/classes/Task/Invoice/Listoverdue.php new file mode 100644 index 00000000..5c403e76 --- /dev/null +++ b/modules/invoice/classes/Task/Invoice/Listoverdue.php @@ -0,0 +1,64 @@ +list_overdue(); + + $rtms = $invoices = array(); + foreach ($overdue as $io) + $invoices[$io->account->rtm_id][] = $io; + + // Work out which suppliers should be notified. + foreach (array_keys($invoices) as $rtm_id) + foreach (ORM::factory('RTM')->suppliers(ORM::factory('RTM',$rtm_id)) as $rtmo) + if (empty($rtms[$rtmo->id])) + $rtms[$rtmo->id] = $rtmo; + + // Send an email to each supplier, with their list of overdue invoices. + foreach ($rtms as $rtmo) { + $output = ''; + + $output .= sprintf('Hi, %s, here are the outstanding invoices of your customers.',$rtmo->name)."\n\n"; + + $rtmp = array(); + foreach ($rtmo->agents($rtmo) as $artmo) { + // If we have included this reseller ID or there are no invoices + if (in_array($artmo->id,$rtmp) OR empty($invoices[$artmo->id])) + continue; + + array_push($rtmp,$artmo->id); + + $rtm_invoices = array(); + $total = 0; + + foreach ($invoices[$artmo->id] as $o) { + array_push($rtm_invoices,$o); + $total += $o->due(); + } + + $output .= View::factory('invoice/task/listoverdue')->set('rtmo',$artmo)->set('o',$rtm_invoices); + } + + // Send our email + $et = Email_Template::instance('task_invoice_list_overdue'); + $et->to = array('account'=>$rtmo->peers()); + $et->variables = array( + 'TABLE'=>$output, + 'NUM_INV'=>count($rtm_invoices), + 'TOTAL'=>$total, + ); + + $et->send(); + } + } +} +?> diff --git a/modules/invoice/views/invoice/task/list_overdue_body.php b/modules/invoice/views/invoice/task/list_overdue_body.php deleted file mode 100644 index 4a4d5084..00000000 --- a/modules/invoice/views/invoice/task/list_overdue_body.php +++ /dev/null @@ -1,6 +0,0 @@ - - account->name(); ?> - display('due_date'); ?> - id(); ?> - due(TRUE); ?> - diff --git a/modules/invoice/views/invoice/task/list_overdue_foot.php b/modules/invoice/views/invoice/task/list_overdue_foot.php deleted file mode 100644 index 000ca4b0..00000000 --- a/modules/invoice/views/invoice/task/list_overdue_foot.php +++ /dev/null @@ -1 +0,0 @@ - diff --git a/modules/invoice/views/invoice/task/list_overdue_head.php b/modules/invoice/views/invoice/task/list_overdue_head.php deleted file mode 100644 index b0e1d550..00000000 --- a/modules/invoice/views/invoice/task/list_overdue_head.php +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/modules/invoice/views/invoice/task/listoverdue.php b/modules/invoice/views/invoice/task/listoverdue.php new file mode 100644 index 00000000..9dd56f21 --- /dev/null +++ b/modules/invoice/views/invoice/task/listoverdue.php @@ -0,0 +1,20 @@ +

name; ?>

+
AccountDue DateInv IDAmount Due
+ + + + + + + + + + + + + + + + + +
AccountDue DateInv IDAmount Due
account->name(); ?>display('due_date'); ?>id(); ?>due(TRUE); ?>