From 1b83a5a0b1bf883f83b343ac99bb7319c130f2c8 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 5 Jul 2013 23:37:06 +1000 Subject: [PATCH] Moved Invoice reminders into Minion Tasks --- application/classes/Config.php | 2 +- application/config/config.php | 3 - application/config/debug.php | 3 + modules/email/classes/Email/Template.php | 6 +- .../classes/Controller/Task/Invoice.php | 57 ---------------- modules/invoice/classes/Model/Invoice.php | 15 +++-- .../classes/Task/Invoice/Remindoverdue1.php | 67 +++++++++++++++++++ .../classes/Task/Invoice/Remindoverdue2.php | 19 ++++++ .../classes/Task/Invoice/Remindoverdue3.php | 19 ++++++ modules/task/classes/Task/Task/Run.php | 4 +- 10 files changed, 124 insertions(+), 71 deletions(-) create mode 100644 modules/invoice/classes/Task/Invoice/Remindoverdue1.php create mode 100644 modules/invoice/classes/Task/Invoice/Remindoverdue2.php create mode 100644 modules/invoice/classes/Task/Invoice/Remindoverdue3.php diff --git a/application/classes/Config.php b/application/classes/Config.php index 2a9ef5dc..7cb7a8d5 100644 --- a/application/classes/Config.php +++ b/application/classes/Config.php @@ -160,7 +160,7 @@ class Config extends Kohana_Config { * @return mixed|array - Email to send test emails to */ public static function testmail($template) { - $config = Kohana::$config->load('config')->email_admin_only; + $config = Kohana::$config->load('debug')->email_admin_only; if (is_null($config) OR ! is_array($config) OR empty($config[$template])) return FALSE; diff --git a/application/config/config.php b/application/config/config.php index 0c4da93e..615c1da6 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -14,9 +14,6 @@ return array( 'appname' => 'OS Billing', // Our application name, as shown in the title bar of pages 'email_from' => array('noreply@graytech.net.au'=>'Graytech Hosting'), - 'email_admin_only'=> array( -// 'adsl_traffic_notice'=>array('deon@leenooks.vpn'=>'Deon George'), - ), 'method_security' => TRUE, // Enables Method Security. Setting to false means any method can be run without authentication 'session_change_trigger'=>array( // Updates to tables to make when our session ID is changed 'Cart'=>'session_id', diff --git a/application/config/debug.php b/application/config/debug.php index 9fedb4e4..29c54770 100644 --- a/application/config/debug.php +++ b/application/config/debug.php @@ -16,6 +16,9 @@ return array 'ajax'=>FALSE, // AJAX actions can only be run by ajax calls if set to FALSE 'etag'=>FALSE, // Force generating ETAGS 'checkout_notify'=>FALSE, // Test mode to test a particular checkout_notify item + 'email_admin_only'=> array( // Override emails and send them to an admin instead +// 'task_invoice_list_overdue'=>array('deon@leenooks.net'=>'Deon George'), + ), 'invoice'=>0, // Number of invoices to generate in a pass 'site'=>FALSE, // Glogal site debug 'show_errors'=>FALSE, // Show errors instead of logging in the DB. diff --git a/modules/email/classes/Email/Template.php b/modules/email/classes/Email/Template.php index 6ba04026..282a8cd8 100644 --- a/modules/email/classes/Email/Template.php +++ b/modules/email/classes/Email/Template.php @@ -150,10 +150,10 @@ class Email_Template { // @todo - Setup queue mode $result = $e->send($sm); + // Store our email log. + $elo = ORM::factory('Email_Log'); if ($result) { - // Store our email log. - $elo = ORM::factory('Email_Log'); foreach ($sa as $id) { $elo->clear(); @@ -166,7 +166,7 @@ class Email_Template { } } - return $result; + return ($result AND $elo->saved()) ? $elo->id : $result; } private function to_accounts() { diff --git a/modules/invoice/classes/Controller/Task/Invoice.php b/modules/invoice/classes/Controller/Task/Invoice.php index c8d0f7ea..69da9959 100644 --- a/modules/invoice/classes/Controller/Task/Invoice.php +++ b/modules/invoice/classes/Controller/Task/Invoice.php @@ -49,63 +49,6 @@ class Controller_Task_Invoice extends Controller_Task { $this->response->body(_('Due Reminders Sent: ').join('|',$action)); } - /** - * Email a customers when their invoices are now overdue. - */ - public function action_remind_overdue() { - $action = array(); - $notice = $this->request->param('id'); - $x = NULL; - - if (preg_match('/:/',$notice)) - list($notice,$x) = explode(':',$notice); - - switch ($notice) { - case 1: - case 2: - case 3: - $days = ORM::factory('Invoice')->config('REMIND_OVERDUE_'.$notice); - break; - - default: - $this->response->body(_('Unknown Remind Period: ').$notice); - return; - } - - $key = 'remind_overdue_'.$notice; - - foreach (ORM::factory('Invoice')->list_overdue_billing(time()-86400*$days,FALSE) as $io) { - // @todo Use another option to supress reminders - // If we have already sent a reminder, we'll skip to the next one. - if (($io->remind($key) AND (is_null($x=$this->request->param('id')) OR $x != 'again')) OR ($io->account->invoice_delivery != 1)) - continue; - - // Send our email - $et = Email_Template::instance('task_invoice_'.$key); - - $et->to = array('account'=>array($io->account_id)); - $et->variables = array( - 'DUE'=>$io->due(TRUE), - 'DUE_DATE'=>$io->display('due_date'), - 'EMAIL'=>Company::instance()->email(), - 'FIRST_NAME'=>$io->account->first_name, - 'INV_NUM'=>$io->refnum(), - 'INV_URL'=>URL::site(URL::link('user','invoice/view/'.$io->id),'http'), - 'LATE_FEE'=>'5.50', // @todo This should come from a config file. - 'PAYMENTS_TABLE'=>$io->account->payment->list_recent_table(), - 'SITE_NAME'=>Company::instance()->name(), - ); - - // @todo Record email log id if possible. - if ($et->send()) { - $io->set_remind($key,time()); - array_push($action,(string)$io); - } - } - - $this->response->body(_('Overdue Reminders Sent: ').join('|',$action)); - } - /** * Generate our services invoices, based on the service next invoice date * diff --git a/modules/invoice/classes/Model/Invoice.php b/modules/invoice/classes/Model/Invoice.php index d010dfff..903f4de4 100644 --- a/modules/invoice/classes/Model/Invoice.php +++ b/modules/invoice/classes/Model/Invoice.php @@ -464,19 +464,22 @@ class Model_Invoice extends ORM_OSB implements Cartable { } public function set_remind($key,$value,$add=FALSE) { + $x = $this->reminders; + // If our value is null, we'll remove it. - if (is_null($value) AND isset($this->reminders[$key])) - unset($this->reminders[$key]); + if (is_null($value) AND isset($x[$key])) + unset($x[$key]); elseif ($add) { - if (! is_array($a=$this->reminders[$key])) - $this->reminders[$key] = array($a); + if (! is_array($a=$x[$key])) + $x[$key] = array($a); - $this->reminders[$key][] = $value; + $x[$key][] = $value; } else - $this->reminders[$key] = $value; + $x[$key] = $value; + $this->reminders = $x; $this->save(); return $this->saved(); diff --git a/modules/invoice/classes/Task/Invoice/Remindoverdue1.php b/modules/invoice/classes/Task/Invoice/Remindoverdue1.php new file mode 100644 index 00000000..d89c1a87 --- /dev/null +++ b/modules/invoice/classes/Task/Invoice/Remindoverdue1.php @@ -0,0 +1,67 @@ +config(strtoupper($key)); + break; + + default: + $this->response->body(_('Unknown Remind Period: ').$notice); + return; + } + + foreach (ORM::factory('Invoice')->list_overdue_billing(time()-86400*$days,FALSE) as $io) { + // If we have already sent a reminder, we'll skip to the next one. + if ($io->remind($key) OR ($io->account->invoice_delivery != 1)) + continue; + + // Send our email + $et = Email_Template::instance('task_invoice_'.$key); + + $et->to = array('account'=>array($io->account_id)); + $et->variables = array( + 'DUE'=>$io->due(TRUE), + 'DUE_DATE'=>$io->display('due_date'), + 'EMAIL'=>Company::instance()->email(), + 'FIRST_NAME'=>$io->account->first_name, + 'INV_NUM'=>$io->refnum(), + 'INV_URL'=>URL::site(URL::link('user','invoice/view/'.$io->id),'http'), + 'LATE_FEE'=>'5.50', // @todo This should come from a config file. + 'PAYMENTS_TABLE'=>$io->account->payment->list_recent_table(), + 'SITE_NAME'=>Company::instance()->name(), + ); + + // @todo Record email log id if possible. + if ($eloid = $et->send()) { + $io->set_remind($key,time(),FALSE); + array_push($action,(string)$io); + } + } + + return $action; + } + + protected function _execute(array $params) { + $action = $this->remind_overdue(1); + + return _('OverDue Notice #1 Reminders Sent: ').join('|',$action); + } +} +?> diff --git a/modules/invoice/classes/Task/Invoice/Remindoverdue2.php b/modules/invoice/classes/Task/Invoice/Remindoverdue2.php new file mode 100644 index 00000000..9f46dc34 --- /dev/null +++ b/modules/invoice/classes/Task/Invoice/Remindoverdue2.php @@ -0,0 +1,19 @@ +remind_overdue(2); + + return _('OverDue Notice #2 Reminders Sent: ').join('|',$action); + } +} +?> diff --git a/modules/invoice/classes/Task/Invoice/Remindoverdue3.php b/modules/invoice/classes/Task/Invoice/Remindoverdue3.php new file mode 100644 index 00000000..a4daa2f8 --- /dev/null +++ b/modules/invoice/classes/Task/Invoice/Remindoverdue3.php @@ -0,0 +1,19 @@ +remind_overdue(3); + + return _('OverDue Notice #3 Reminders Sent: ').join('|',$action); + } +} +?> diff --git a/modules/task/classes/Task/Task/Run.php b/modules/task/classes/Task/Task/Run.php index 5e382e10..fc798dc8 100644 --- a/modules/task/classes/Task/Task/Run.php +++ b/modules/task/classes/Task/Task/Run.php @@ -58,7 +58,9 @@ class Task_Task_Run extends Minion_Task { switch ($to->type) { case 1: - $r = Minion_Task::factory(array('site'=>Minion_CLI::options('site'),'task'=>$to->command))->execute(); + ob_start(); + Minion_Task::factory(array('site'=>Minion_CLI::options('site'),'task'=>$to->command))->execute(); + $r = ob_get_clean(); $to->running = 0; $to->running_host = NULL;