Moved Invoice reminders into Minion Tasks

This commit is contained in:
Deon George 2013-07-05 23:37:06 +10:00
parent f3d2c1fe8d
commit 0a0e149e56
10 changed files with 124 additions and 71 deletions

View File

@ -160,7 +160,7 @@ class Config extends Kohana_Config {
* @return mixed|array - Email to send test emails to * @return mixed|array - Email to send test emails to
*/ */
public static function testmail($template) { 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])) if (is_null($config) OR ! is_array($config) OR empty($config[$template]))
return FALSE; return FALSE;

View File

@ -14,9 +14,6 @@
return array( return array(
'appname' => 'OS Billing', // Our application name, as shown in the title bar of pages 'appname' => 'OS Billing', // Our application name, as shown in the title bar of pages
'email_from' => array('noreply@graytech.net.au'=>'Graytech Hosting'), '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 '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 'session_change_trigger'=>array( // Updates to tables to make when our session ID is changed
'Cart'=>'session_id', 'Cart'=>'session_id',

View File

@ -16,6 +16,9 @@ return array
'ajax'=>FALSE, // AJAX actions can only be run by ajax calls if set to FALSE 'ajax'=>FALSE, // AJAX actions can only be run by ajax calls if set to FALSE
'etag'=>FALSE, // Force generating ETAGS 'etag'=>FALSE, // Force generating ETAGS
'checkout_notify'=>FALSE, // Test mode to test a particular checkout_notify item '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 'invoice'=>0, // Number of invoices to generate in a pass
'site'=>FALSE, // Glogal site debug 'site'=>FALSE, // Glogal site debug
'show_errors'=>FALSE, // Show errors instead of logging in the DB. 'show_errors'=>FALSE, // Show errors instead of logging in the DB.

View File

@ -150,11 +150,11 @@ class Email_Template {
// @todo - Setup queue mode // @todo - Setup queue mode
$result = $e->send($sm); $result = $e->send($sm);
if ($result) {
// Store our email log. // Store our email log.
$elo = ORM::factory('Email_Log'); $elo = ORM::factory('Email_Log');
if ($result) {
foreach ($sa as $id) { foreach ($sa as $id) {
$elo->clear(); $elo->clear();
@ -166,7 +166,7 @@ class Email_Template {
} }
} }
return $result; return ($result AND $elo->saved()) ? $elo->id : $result;
} }
private function to_accounts() { private function to_accounts() {

View File

@ -49,63 +49,6 @@ class Controller_Task_Invoice extends Controller_Task {
$this->response->body(_('Due Reminders Sent: ').join('|',$action)); $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 * Generate our services invoices, based on the service next invoice date
* *

View File

@ -464,19 +464,22 @@ class Model_Invoice extends ORM_OSB implements Cartable {
} }
public function set_remind($key,$value,$add=FALSE) { public function set_remind($key,$value,$add=FALSE) {
$x = $this->reminders;
// If our value is null, we'll remove it. // If our value is null, we'll remove it.
if (is_null($value) AND isset($this->reminders[$key])) if (is_null($value) AND isset($x[$key]))
unset($this->reminders[$key]); unset($x[$key]);
elseif ($add) { elseif ($add) {
if (! is_array($a=$this->reminders[$key])) if (! is_array($a=$x[$key]))
$this->reminders[$key] = array($a); $x[$key] = array($a);
$this->reminders[$key][] = $value; $x[$key][] = $value;
} else } else
$this->reminders[$key] = $value; $x[$key] = $value;
$this->reminders = $x;
$this->save(); $this->save();
return $this->saved(); return $this->saved();

View File

@ -0,0 +1,67 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Send out first reminder for over due invoices.
*
* @package Invoice
* @category Tasks
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Invoice_Remindoverdue1 extends Minion_Task {
protected function remind_overdue($notice=1) {
$action = array();
$key = 'remind_overdue_'.$notice;
switch ($notice) {
case 1:
case 2:
case 3:
$days = ORM::factory('Invoice')->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);
}
}
?>

View File

@ -0,0 +1,19 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Send out second reminder for over due invoices.
*
* @package Invoice
* @category Tasks
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Invoice_Remindoverdue2 extends Task_Invoice_Remindoverdue1 {
protected function _execute(array $params) {
$action = $this->remind_overdue(2);
return _('OverDue Notice #2 Reminders Sent: ').join('|',$action);
}
}
?>

View File

@ -0,0 +1,19 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Send out third reminder for over due invoices.
*
* @package Invoice
* @category Tasks
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Invoice_Remindoverdue3 extends Task_Invoice_Remindoverdue1 {
protected function _execute(array $params) {
$action = $this->remind_overdue(3);
return _('OverDue Notice #3 Reminders Sent: ').join('|',$action);
}
}
?>

View File

@ -58,7 +58,9 @@ class Task_Task_Run extends Minion_Task {
switch ($to->type) { switch ($to->type) {
case 1: 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 = 0;
$to->running_host = NULL; $to->running_host = NULL;