Email invoices to users

This commit is contained in:
Deon George 2011-10-13 09:20:08 +11:00
parent c419b50bac
commit 0bbda5352e
13 changed files with 264 additions and 54 deletions

View File

@ -155,7 +155,17 @@ class Auth_OSB extends Auth_ORM {
$mmto = ORM::factory('module_method_token',array('token'=>$token)); $mmto = ORM::factory('module_method_token',array('token'=>$token));
$user = FALSE; $user = FALSE;
// Ignore the token if it doesnt exist.
if ($mmto->loaded()) { if ($mmto->loaded()) {
// Check that the token is for this URI
$mo = ORM::factory('module',array('name'=>Request::current()->controller()));
$mmo = ORM::factory('module_method',array(
'module_id'=>$mo->id,
'name'=>Request::current()->directory() ? sprintf('%s_%s',Request::current()->directory(),Request::current()->action()) : Request::current()->action()
));
// Ignore the token if this is not the right method.
if ($mmo->id == $mmto->method_id) {
if (! is_null($mmto->date_expire) AND $mmto->date_expire < time()) { if (! is_null($mmto->date_expire) AND $mmto->date_expire < time()) {
SystemMessage::add(array( SystemMessage::add(array(
'title'=>_('Token Not Valid'), 'title'=>_('Token Not Valid'),
@ -177,15 +187,6 @@ class Auth_OSB extends Auth_ORM {
$mmto->delete(); $mmto->delete();
} else { } else {
// Check that the token is for this URI
$mo = ORM::factory('module',array('name'=>Request::current()->controller()));
$mmo = ORM::factory('module_method',array(
'module_id'=>$mo->id,
'name'=>Request::current()->directory() ? sprintf('%s_%s',Request::current()->directory(),Request::current()->action()) : Request::current()->action()
));
// Ignore the token if this is not the right method.
if ($mmo->id == $mmto->method_id) {
// If this is a usage count token, reduce the count. // If this is a usage count token, reduce the count.
if (! is_null($mmto->uses)) if (! is_null($mmto->uses))
$mmto->uses -= 1; $mmto->uses -= 1;

View File

@ -30,6 +30,7 @@ class Controller_Admin_Welcome extends Controller_TemplateDefault {
25, 25,
array( array(
'due_date'=>array('label'=>'Due Date'), 'due_date'=>array('label'=>'Due Date'),
'account->accnum()'=>array('label'=>'Num'),
'account->name()'=>array('label'=>'Account'), 'account->name()'=>array('label'=>'Account'),
'account->display("status")'=>array('label'=>'Active'), 'account->display("status")'=>array('label'=>'Active'),
'id'=>array('label'=>'ID','url'=>'user/invoice/view/'), 'id'=>array('label'=>'ID','url'=>'user/invoice/view/'),
@ -48,6 +49,7 @@ class Controller_Admin_Welcome extends Controller_TemplateDefault {
25, 25,
array( array(
'due_date'=>array('label'=>'Due Date'), 'due_date'=>array('label'=>'Due Date'),
'account->accnum()'=>array('label'=>'Num'),
'account->name()'=>array('label'=>'Account'), 'account->name()'=>array('label'=>'Account'),
'account->display("status")'=>array('label'=>'Active'), 'account->display("status")'=>array('label'=>'Active'),
'id'=>array('label'=>'ID','url'=>'user/invoice/view/'), 'id'=>array('label'=>'ID','url'=>'user/invoice/view/'),
@ -66,6 +68,7 @@ class Controller_Admin_Welcome extends Controller_TemplateDefault {
25, 25,
array( array(
'due_date'=>array('label'=>'Due Date'), 'due_date'=>array('label'=>'Due Date'),
'account->accnum()'=>array('label'=>'Num'),
'account->name()'=>array('label'), 'account->name()'=>array('label'),
'account->display("status")'=>array('label'=>'Active'), 'account->display("status")'=>array('label'=>'Active'),
'id'=>array('label'=>'ID','url'=>'user/invoice/view/'), 'id'=>array('label'=>'ID','url'=>'user/invoice/view/'),
@ -85,6 +88,7 @@ class Controller_Admin_Welcome extends Controller_TemplateDefault {
25, 25,
array( array(
'date_payment'=>array('label'=>'Pay Date'), 'date_payment'=>array('label'=>'Pay Date'),
'account->accnum()'=>array('label'=>'Num'),
'account->name()'=>array('label'=>'Account'), 'account->name()'=>array('label'=>'Account'),
'account->display("status")'=>array('label'=>'Active'), 'account->display("status")'=>array('label'=>'Active'),
'id'=>array('label'=>'ID','url'=>'user/payment/view/'), 'id'=>array('label'=>'ID','url'=>'user/payment/view/'),

View File

@ -135,6 +135,7 @@ class Controller_lnApp_Login extends Controller_TemplateDefault {
$mmto = ORM::factory('module_method_token') $mmto = ORM::factory('module_method_token')
->method(array('account','user_resetpassword')) ->method(array('account','user_resetpassword'))
->account($ao) ->account($ao)
->uses(2)
->expire(time()+$token_expire*60); ->expire(time()+$token_expire*60);
if ($mmto->generate()) { if ($mmto->generate()) {

View File

@ -54,12 +54,6 @@ class Controller_User_Account extends Controller_TemplateDefault_User {
'body'=>_('Your updates didnt pass validation.').'<br/>'.$output, 'body'=>_('Your updates didnt pass validation.').'<br/>'.$output,
)); ));
} }
else
SystemMessage::add(array(
'title'=>_('Record NOT updated'),
'type'=>'info',
'body'=>_('Your account has NOT been updated.')
));
Block::add(array( Block::add(array(
'title'=>_('Password Reset'), 'title'=>_('Password Reset'),

View File

@ -74,7 +74,7 @@ abstract class lnApp_Config extends Kohana_Config {
public static function logo_uri() { public static function logo_uri() {
list ($path,$suffix) = explode('.',static::$logo); list ($path,$suffix) = explode('.',static::$logo);
return Route::get('default/media')->uri(array('file'=>$path.'.'.$suffix),array('alt'=>static::sitename())); return URL::site(Route::get('default/media')->uri(array('file'=>$path.'.'.$suffix),array('alt'=>static::sitename())),'http');
} }
public static function logo() { public static function logo() {

View File

@ -63,7 +63,7 @@ class Model_Auth_UserDefault extends Model_Auth_User {
* Test to see if a record has been changed * Test to see if a record has been changed
*/ */
public function changed() { public function changed() {
return ! (empty($this->_changed)); return (count($this->_changed));
} }
/** /**

View File

@ -27,9 +27,9 @@ class Controller_Admin_Invoice extends Controller_TemplateDefault_Admin {
array( array(
'id'=>array('label'=>'ID','url'=>'user/invoice/view/'), 'id'=>array('label'=>'ID','url'=>'user/invoice/view/'),
'date_orig'=>array('label'=>'Date'), 'date_orig'=>array('label'=>'Date'),
'total_amt'=>array('label'=>'Total','class'=>'right'), 'total(TRUE)'=>array('label'=>'Total','class'=>'right'),
'credit_amt'=>array('label'=>'Credits','class'=>'right'), 'credit_amt'=>array('label'=>'Credits','class'=>'right'),
'billed_amt'=>array('label'=>'Payments','class'=>'right'), 'payments_total(TRUE)'=>array('label'=>'Payments','class'=>'right'),
'due(TRUE)'=>array('label'=>'Still Due','class'=>'right'), 'due(TRUE)'=>array('label'=>'Still Due','class'=>'right'),
'account->accnum()'=>array('label'=>'Cust ID'), 'account->accnum()'=>array('label'=>'Cust ID'),
'account->name()'=>array('label'=>'Customer'), 'account->name()'=>array('label'=>'Customer'),

View File

@ -60,10 +60,10 @@ class Controller_Task_Invoice extends Controller_Task {
$action = array(); $action = array();
// @todo This should go in a config somewhere // @todo This should go in a config somewhere
$days = 5; $days = 5;
$io = ORM::factory('invoice'); $i = ORM::factory('invoice');
$key = 'remind_due'; $key = 'remind_due';
foreach ($io->list_due(time()+86400*$days) as $io) { foreach ($i->list_due(time()+86400*$days) as $io) {
// If we have already sent a reminder, we'll skip to the next one. // 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')) if ($io->remind($key) AND (is_null($x=$this->request->param('id')) OR $x != 'again'))
continue; continue;
@ -96,7 +96,7 @@ class Controller_Task_Invoice extends Controller_Task {
*/ */
public function action_remind_overdue() { public function action_remind_overdue() {
$action = array(); $action = array();
$io = ORM::factory('invoice'); $i = ORM::factory('invoice');
$notice = $this->request->param('id'); $notice = $this->request->param('id');
$x = NULL; $x = NULL;
@ -125,9 +125,8 @@ class Controller_Task_Invoice extends Controller_Task {
} }
$key = 'remind_overdue_'.$notice; $key = 'remind_overdue_'.$notice;
$template = 'task_invoice_'.$key;
foreach ($io->list_overdue_billing(time()-86400*$days,FALSE) as $io) { foreach ($i->list_overdue_billing(time()-86400*$days,FALSE) as $io) {
// If we have already sent a reminder, we'll skip to the next one. // 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')) if ($io->remind($key) AND (is_null($x) OR $x != 'again'))
continue; continue;
@ -230,6 +229,56 @@ class Controller_Task_Invoice extends Controller_Task {
$this->response->body(_('Services Invoiced: ').join('|',$action)); $this->response->body(_('Services Invoiced: ').join('|',$action));
} }
public function action_invoice_send() {
$action = array();
$iid = $this->request->param('id');
$x = NULL;
if (preg_match('/:/',$iid))
list($iid,$x) = explode(':',$iid);
// @todo The parameter for email should be a system CONSTANT?
$i = $iid ? ORM::factory('invoice')->where('id','=',$iid) : ORM::factory('invoice')->tosend(1);
$key = 'send';
foreach ($i->find_all() 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);
$token = ORM::factory('module_method_token')
->method(array('invoice','user_download'))
->account($io->account)
->expire(time()+86400*21)
->uses(3)
->generate();
$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,
'HTML_INVOICE'=>$io->html(),
'INV_NUM'=>$io->refnum(),
'INV_URL'=>URL::site('user/invoice/view/'.$io->id,'http'),
'INV_URL_DOWNLOAD'=>URL::site(sprintf('user/invoice/download/%s?token=%s',$io->id,$token),'http'),
'SITE_NAME'=>Config::sitename(),
);
// @todo Record email log id if possible.
if ($et->send()) {
$io->set_remind($key,time());
array_push($action,(string)$io);
}
}
$this->response->body(_('Invoices Sent: ').join('|',$action));
}
/** END **/ /** END **/
public function action_audit_invoice_items() { public function action_audit_invoice_items() {
@ -244,16 +293,6 @@ class Controller_Task_Invoice extends Controller_Task {
print_r(array("DIFF",'id'=>$iio->id,'pn'=>serialize($iio->product_name),'ppn'=>serialize($iio->product->name()),'pid'=>$iio->product_id,'test'=>strcasecmp($iio->product_name,$iio->product->name()))); print_r(array("DIFF",'id'=>$iio->id,'pn'=>serialize($iio->product_name),'ppn'=>serialize($iio->product->name()),'pid'=>$iio->product_id,'test'=>strcasecmp($iio->product_name,$iio->product->name())));
} }
} }
#if ($iio->product->prod_plugin_file == 'HOST') {
# if ($iio->service->name() == strtoupper($iio->domain_name))
# $iio->domain_name=null;
#}
#if ($iio->product->prod_plugin_file == 'ADSL') {
# if ($iio->service->name() == strtoupper($iio->domain_name))
# $iio->domain_name=null;
# #print_r(array('pid'=>$iio->domain_name,'iio-service-name'=>$iio->service->name(),'iii-domain_name'=>$iio->domain_name));
#}
} }
$this->response->body($output); $this->response->body($output);

View File

@ -450,5 +450,23 @@ class Model_Invoice extends ORMOSB {
return $result; return $result;
} }
public function html() {
// @todo This should be in a config file.
$css = '<style type="text/css">';
$css .= 'table.box-left { border: 1px solid #AAAACC; margin-right: auto; }';
$css .= 'tr.head { font-weight: bold; }';
$css .= 'td.head { font-weight: bold; }';
$css .= 'td.right { text-align: right; }';
$css .= 'tr.odd { background-color: #FCFCFE; }';
$css .= 'tr.even { background-color: #F6F6F8; }';
$css .= '</style>';
$output = View::factory('invoice/user/email')
->set('mediapath',Route::get('default/media'))
->set('io',$this);
return $css.$output;
}
} }
?> ?>

View File

@ -0,0 +1,145 @@
<!-- @todo NEED to translate this -->
<table class="box-left" border="0">
<tr>
<td style="vertical-align: top">
<table class="company_details" border="0">
<tr>
<td class="logo"><?php echo Config::logo(); ?></td>
<td class="address"><span class="company_name"><?php echo Config::sitename(); ?></span><br/><?php echo Company::address(); ?><br/><?php echo Company::contacts(); ?></td>
</tr>
</table>
</td>
<td>&nbsp;</td>
<td>
<table class="invoice_summary" border="0">
<tr>
<td>TAX INVOICE</td>
<td class="bold-right"><?php echo $io->id(); ?></td>
</tr>
<tr>
<td>Issue Date</td>
<td class="bold-right"><?php echo $io->display('date_orig'); ?></td>
</tr>
<tr>
<td>Due Date</td>
<td class="bold-right"><?php echo $io->display('due_date'); ?></td>
</tr>
<tr>
<td>Current Charges</td>
<td class="bold-right"><?php echo $io->total(TRUE); ?></td>
</tr>
<tr>
<td>Payments Received to Date</td>
<td class="bold-right"><?php echo $io->payments_total(TRUE); ?></td>
</tr>
<tr>
<td>Total Charges Due This Invoice</td>
<td class="bold-right"><?php echo $io->due(TRUE); ?></td>
</tr>
</table>
</td>
</tr>
<tr><td class="spacer" colspan="3">&nbsp;</td></tr>
<tr>
<td colspan="3">
<table class="box-full" border="0">
<tr>
<td class="head" colspan="4">Charges Detail:</td>
</tr>
<?php foreach ($io->items_service_periods() as $rs => $items) { ?>
<tr>
<td>+</td>
<?php if ($rs) { ?>
<td><?php echo StaticList_RecurSchedule::display($rs); ?></td>
<td colspan="1"><?php printf('%s Service(s)',count($items)); ?></td>
<?php } else { ?>
<td colspan="2">Other Items</td>
<?php } ?>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2">
<div id="detail_toggle_<?php echo $rs; ?>">
<table class="box-full" border="0">
<?php if ($items) { ?>
<?php foreach ($io->items_services($items) as $sid) { ?>
<?php $so = ORM::factory('service',$sid); ?>
<!-- Product Information -->
<tr class="head">
<td><?php echo $so->id(); ?></td>
<td colspan="5"><?php echo $so->service_name(); ?> (<?php echo $so->product_id; ?>)</td>
<td class="right"><?php echo Currency::display($io->items_service_total($so->id));?></td>
</tr>
<!-- End Product Information -->
<?php foreach ($io->items_service($sid) as $ito) { ?>
<!-- Product Sub Information -->
<tr>
<td>&nbsp;</td>
<td><?php echo $ito->trannum();?></td>
<td><?php echo $ito->name();?></td>
<td><?php echo $ito->detail();?></td>
<td><?php echo $ito->period();?></td>
<td class="right"><?php echo Currency::display($ito->subtotal());?>&nbsp;</td>
</tr>
<!-- End Product Sub Information -->
<?php } ?>
<?php if ($ito->discount_amt) { ?>
<tr>
<td colspan="4">&nbsp;</td>
<td><?php echo _('Discounts'); ?></td>
<td class="right">(<?php echo Currency::display($io->items_service_discount($so->id));?>)</td>
</tr>
<?php } ?>
<!-- Product Sub Items Tax -->
<tr>
<td colspan="4">&nbsp;</td>
<td><?php echo _('Taxes'); ?></td>
<td class="right"><?php echo Currency::display($io->items_service_tax($so->id));?>&nbsp;</td>
</tr>
<!-- Product End Sub Items Tax -->
<?php } ?>
<?php } ?>
</table>
</div>
</td>
</tr>
<?php } ?>
<tr>
<td class="head" colspan="3">Sub Total of Items:</td>
<td class="bold-right"><?php echo $io->subtotal(TRUE); ?>&nbsp;</td>
</tr>
<?php if ($io->discount()) { ?>
<tr>
<td class="head" colspan="3">Discounts:</td>
<td class="bold-right">(<?php echo $io->discount(TRUE); ?>)</td>
</tr>
<?php } ?>
<tr>
<td class="head" colspan="4">Taxes Included:</td>
</tr>
<?php
foreach ($io->tax_summary() as $tid => $amount) {
$m = ORM::factory('tax',$tid);
?>
<tr>
<td>&nbsp;</td>
<td colspan="2"><?php echo $m->description; ?></td>
<td class="bold-right"><?php echo Currency::display($amount); ?>&nbsp;</td>
</tr>
<?php }?>
<!-- @todo Add discounts -->
<tr>
<td class="head" colspan="3">Total Invoice:</td>
<td class="bold-right"><?php echo $io->total(TRUE); ?>&nbsp;</td>
</tr>
<tr>
<td class="head" colspan="3">Total Outstanding:</td>
<td class="bold-right"><?php echo $io->account->invoices_due_total(NULL,TRUE); ?>&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>

View File

@ -25,7 +25,7 @@
<td class="bold-right"><?php echo $io->display('due_date'); ?></td> <td class="bold-right"><?php echo $io->display('due_date'); ?></td>
</tr> </tr>
<tr> <tr>
<td>Current Charges Due</td> <td>Current Charges</td>
<td class="bold-right"><?php echo $io->total(TRUE); ?></td> <td class="bold-right"><?php echo $io->total(TRUE); ?></td>
</tr> </tr>
<tr> <tr>
@ -33,7 +33,7 @@
<td class="bold-right"><?php echo $io->payments_total(TRUE); ?></td> <td class="bold-right"><?php echo $io->payments_total(TRUE); ?></td>
</tr> </tr>
<tr> <tr>
<td>Total Charges Due</td> <td>Total Charges Due This Invoice</td>
<td class="bold-right"><?php echo $io->due(TRUE); ?></td> <td class="bold-right"><?php echo $io->due(TRUE); ?></td>
</tr> </tr>
</table> </table>
@ -132,9 +132,13 @@
<?php }?> <?php }?>
<!-- @todo Add discounts --> <!-- @todo Add discounts -->
<tr> <tr>
<td class="head" colspan="3">Total:</td> <td class="head" colspan="3">Total Invoice:</td>
<td class="bold-right"><?php echo $io->total(TRUE); ?>&nbsp;</td> <td class="bold-right"><?php echo $io->total(TRUE); ?>&nbsp;</td>
</tr> </tr>
<tr>
<td class="head" colspan="3">Total Outstanding:</td>
<td class="bold-right"><?php echo $io->account->invoices_due_total(NULL,TRUE); ?>&nbsp;</td>
</tr>
</table> </table>
</td> </td>
</tr> </tr>

View File

@ -174,6 +174,7 @@ ORDER BY c.id,s.recur_schedule,c.name,a.company,a.last_name,a.first_name
'id'=>array('label'=>'ID','url'=>'user/service/view/'), 'id'=>array('label'=>'ID','url'=>'user/service/view/'),
'name()'=>array('label'=>'Service'), 'name()'=>array('label'=>'Service'),
'plugin()->ipaddress()'=>array('label'=>'IP Address'), 'plugin()->ipaddress()'=>array('label'=>'IP Address'),
'product->plugin()->speed'=>array('label'=>'Speed'),
'product->plugin()->allowance()'=>array('label'=>'Allowance'), 'product->plugin()->allowance()'=>array('label'=>'Allowance'),
'plugin()->traffic_thismonth()'=>array('label'=>'This Month'), 'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'), 'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),
@ -210,6 +211,7 @@ ORDER BY c.id,s.recur_schedule,c.name,a.company,a.last_name,a.first_name
'id'=>array('label'=>'ID','url'=>'user/service/view/'), 'id'=>array('label'=>'ID','url'=>'user/service/view/'),
'name()'=>array('label'=>'Service'), 'name()'=>array('label'=>'Service'),
'plugin()->ipaddress()'=>array('label'=>'IP Address'), 'plugin()->ipaddress()'=>array('label'=>'IP Address'),
'product->plugin()->speed'=>array('label'=>'Speed'),
'product->plugin()->allowance()'=>array('label'=>'Allowance'), 'product->plugin()->allowance()'=>array('label'=>'Allowance'),
'plugin()->traffic_thismonth()'=>array('label'=>'This Month'), 'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'), 'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),

View File

@ -172,6 +172,7 @@ ORDER BY c.id,s.recur_schedule,c.name,a.company,a.last_name,a.first_name
'id'=>array('label'=>'ID','url'=>'user/service/view/'), 'id'=>array('label'=>'ID','url'=>'user/service/view/'),
'name()'=>array('label'=>'Service'), 'name()'=>array('label'=>'Service'),
'plugin()->ipaddress()'=>array('label'=>'IP Address'), 'plugin()->ipaddress()'=>array('label'=>'IP Address'),
'product->plugin()->speed'=>array('label'=>'Speed'),
'product->plugin()->allowance()'=>array('label'=>'Allowance'), 'product->plugin()->allowance()'=>array('label'=>'Allowance'),
'plugin()->traffic_thismonth()'=>array('label'=>'This Month'), 'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'), 'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),
@ -208,6 +209,7 @@ ORDER BY c.id,s.recur_schedule,c.name,a.company,a.last_name,a.first_name
'id'=>array('label'=>'ID','url'=>'user/service/view/'), 'id'=>array('label'=>'ID','url'=>'user/service/view/'),
'name()'=>array('label'=>'Service'), 'name()'=>array('label'=>'Service'),
'plugin()->ipaddress()'=>array('label'=>'IP Address'), 'plugin()->ipaddress()'=>array('label'=>'IP Address'),
'product->plugin()->speed'=>array('label'=>'Speed'),
'product->plugin()->allowance()'=>array('label'=>'Allowance'), 'product->plugin()->allowance()'=>array('label'=>'Allowance'),
'plugin()->traffic_thismonth()'=>array('label'=>'This Month'), 'plugin()->traffic_thismonth()'=>array('label'=>'This Month'),
'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'), 'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),