Added login_log and overdue_reminders

This commit is contained in:
Deon George
2011-09-27 21:22:13 +10:00
parent f38acfe403
commit b6802e4b5d
12 changed files with 302 additions and 21 deletions

View File

@@ -32,7 +32,7 @@ class Controller_Task_Invoice extends Controller_Task {
$duelist .= View::factory('invoice/task/'.$tm.'_foot');
// Send our email
$et = Email_Template::instance('task_list_invoice_overdue');
$et = Email_Template::instance('task_invoice_list_overdue');
// @todo Update this to be dynamic
$et->to = array('account'=>array(1,68));
@@ -51,32 +51,93 @@ class Controller_Task_Invoice extends Controller_Task {
// @todo This should go in a config somewhere
$days = 5;
$io = ORM::factory('invoice');
$key = 'remind_due';
foreach ($io->list_due(time()-86400*$days) as $io) {
// If we have already sent a reminder, we'll skip to the next one.
if ($io->remind('due_reminder') AND (is_null($x=$this->request->param('id')) OR $x != 'again'))
if ($io->remind($key) AND (is_null($x=$this->request->param('id')) OR $x != 'again'))
continue;
// Send our email
$et = Email_Template::instance('task_invoice_due_reminder');
$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'),
'FIRST_NAME'=>$io->account->first_name,
'INV_NUM'=>$io->refnum(),
'INV_URL'=>URL::site('user/invoice/view/'.$io->id,'http'),
'DUE_DATE'=>$io->display('due_date'),
'FIRSTNAME'=>$io->account->first_name,
'SITE_NAME'=>Config::sitename(),
);
// @todo Record email log id if possible.
if ($et->send())
$io->set_remind('due_reminder',time());
$io->set_remind($key,time());
}
$output = _('Overdue Reminders Sent.');
$this->response->body($output);
$this->response->body(_('Due Reminders Sent.'));
}
public function action_remind_overdue() {
$io = ORM::factory('invoice');
$notice = $this->request->param('id');
$x = NULL;
if (preg_match('/:/',$notice))
list($notice,$x) = explode(':',$notice);
switch ($notice) {
case 1:
// @todo This should go in a config somewhere
$days = 2;
break;
case 2:
// @todo This should go in a config somewhere
$days = 7;
break;
case 3:
// @todo This should go in a config somewhere
$days = 21;
break;
default:
$this->response->body(_('Unknown Remind Period: ').$notice);
return;
}
$key = 'remind_overdue_'.$notice;
$template = 'task_invoice_'.$key;
foreach ($io->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) AND (is_null($x) OR $x != 'again'))
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'=>'accounts@graytech.net.au', // @todo This should come from a config.
'FIRST_NAME'=>$io->account->first_name,
'INV_NUM'=>$io->refnum(),
'INV_URL'=>URL::site('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'=>Config::sitename(),
);
// @todo Record email log id if possible.
if ($et->send())
$io->set_remind($key,time());
}
$this->response->body(_('Overdue Reminders Sent: ').$notice);
}
}
?>

View File

@@ -337,7 +337,11 @@ class Model_Invoice extends ORMOSB {
throw new Kohana_Exception('Reminder is not an array? (:reminder)',array(':remind',$this->reminders));
$remind = unserialize($this->reminders);
return isset($remind[$key]) ? $remind[$key] : FALSE;
if (isset($remind[$key]))
return (is_array($remind[$key])) ? end($remind[$key]) : $remind[$key];
else
return FALSE;
}
public function set_remind($key,$value,$add=FALSE) {
@@ -346,20 +350,22 @@ class Model_Invoice extends ORMOSB {
if (! trim($this->reminders)) {
$remind = array();
$remind[$key][] = $value;
} else {
if (! preg_match('/^a:/',$this->reminders))
throw new Kohana_Exception('Reminder is not an array? (:reminder)',array(':remind',$this->reminders));
$remind = unserialize($this->reminders);
if ($add)
$remind[$key][] = $value;
else
$remind[$key] = $value;
}
// If our value is null, we'll remove it.
if (is_null($value) AND isset($remind[$key]))
unset($remind[$key]);
elseif ($add)
$remind[$key][] = $value;
else
$remind[$key] = $value;
$this->reminders = serialize($remind);
$this->save();
return $this->saved();
@@ -390,6 +396,25 @@ class Model_Invoice extends ORMOSB {
return $this->_list_due($time,'<=');
}
/**
* Return a list of invoices that are over their due date with/without auto billing
*/
public function list_overdue_billing($time=NULL,$billing=FALSE) {
$return = array();
foreach ($this->list_overdue($time) as $io) {
$i = FALSE;
foreach ($io->service->find_all() as $so)
if (($billing AND $so->account_billing_id) OR (! $billing AND ! $so->account_billing_id)) {
array_push($return,$io);
break;
}
}
return $return;
}
/**
* Return a list of invoices that are due, excluding overdue.
*/