From 0bbda5352e1938800d1d9ea28d2d8d822fd53885 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 13 Oct 2011 09:20:08 +1100 Subject: [PATCH] Email invoices to users --- application/classes/auth/osb.php | 51 +++--- .../classes/controller/admin/welcome.php | 4 + .../classes/controller/lnapp/login.php | 1 + .../classes/controller/user/account.php | 6 - application/classes/lnapp/config.php | 2 +- .../classes/model/auth/userdefault.php | 2 +- .../classes/controller/admin/invoice.php | 4 +- .../classes/controller/task/invoice.php | 71 +++++++-- modules/invoice/classes/model/invoice.php | 18 +++ modules/invoice/views/invoice/user/email.php | 145 ++++++++++++++++++ modules/invoice/views/invoice/user/view.php | 10 +- .../classes/controller/admin/service.php | 2 + .../classes/controller/affiliate/service.php | 2 + 13 files changed, 264 insertions(+), 54 deletions(-) create mode 100644 modules/invoice/views/invoice/user/email.php diff --git a/application/classes/auth/osb.php b/application/classes/auth/osb.php index b525f288..9666e01b 100644 --- a/application/classes/auth/osb.php +++ b/application/classes/auth/osb.php @@ -155,37 +155,38 @@ class Auth_OSB extends Auth_ORM { $mmto = ORM::factory('module_method_token',array('token'=>$token)); $user = FALSE; + // Ignore the token if it doesnt exist. if ($mmto->loaded()) { - if (! is_null($mmto->date_expire) AND $mmto->date_expire < time()) { - SystemMessage::add(array( - 'title'=>_('Token Not Valid'), - 'type'=>'warning', - 'body'=>_('Token expired'))); + // 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() + )); - // @todo Log the token deletion - Session::instance()->delete('token'); - $mmto->delete(); + // 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()) { + SystemMessage::add(array( + 'title'=>_('Token Not Valid'), + 'type'=>'warning', + 'body'=>_('Token expired'))); - } elseif (! is_null($mmto->uses) AND $mmto->uses < 1) { - SystemMessage::add(array( - 'title'=>_('Token Not Valid'), - 'type'=>'warning', - 'body'=>_('Token expired'))); + // @todo Log the token deletion + Session::instance()->delete('token'); + $mmto->delete(); - // @todo Log the token deletion - Session::instance()->delete('token'); - $mmto->delete(); + } elseif (! is_null($mmto->uses) AND $mmto->uses < 1) { + SystemMessage::add(array( + 'title'=>_('Token Not Valid'), + 'type'=>'warning', + 'body'=>_('Token expired'))); - } 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() - )); + // @todo Log the token deletion + Session::instance()->delete('token'); + $mmto->delete(); - // Ignore the token if this is not the right method. - if ($mmo->id == $mmto->method_id) { + } else { // If this is a usage count token, reduce the count. if (! is_null($mmto->uses)) $mmto->uses -= 1; diff --git a/application/classes/controller/admin/welcome.php b/application/classes/controller/admin/welcome.php index 4d418292..cc0d747e 100644 --- a/application/classes/controller/admin/welcome.php +++ b/application/classes/controller/admin/welcome.php @@ -30,6 +30,7 @@ class Controller_Admin_Welcome extends Controller_TemplateDefault { 25, array( 'due_date'=>array('label'=>'Due Date'), + 'account->accnum()'=>array('label'=>'Num'), 'account->name()'=>array('label'=>'Account'), 'account->display("status")'=>array('label'=>'Active'), 'id'=>array('label'=>'ID','url'=>'user/invoice/view/'), @@ -48,6 +49,7 @@ class Controller_Admin_Welcome extends Controller_TemplateDefault { 25, array( 'due_date'=>array('label'=>'Due Date'), + 'account->accnum()'=>array('label'=>'Num'), 'account->name()'=>array('label'=>'Account'), 'account->display("status")'=>array('label'=>'Active'), 'id'=>array('label'=>'ID','url'=>'user/invoice/view/'), @@ -66,6 +68,7 @@ class Controller_Admin_Welcome extends Controller_TemplateDefault { 25, array( 'due_date'=>array('label'=>'Due Date'), + 'account->accnum()'=>array('label'=>'Num'), 'account->name()'=>array('label'), 'account->display("status")'=>array('label'=>'Active'), 'id'=>array('label'=>'ID','url'=>'user/invoice/view/'), @@ -85,6 +88,7 @@ class Controller_Admin_Welcome extends Controller_TemplateDefault { 25, array( 'date_payment'=>array('label'=>'Pay Date'), + 'account->accnum()'=>array('label'=>'Num'), 'account->name()'=>array('label'=>'Account'), 'account->display("status")'=>array('label'=>'Active'), 'id'=>array('label'=>'ID','url'=>'user/payment/view/'), diff --git a/application/classes/controller/lnapp/login.php b/application/classes/controller/lnapp/login.php index d91cfc53..aa87f1cf 100644 --- a/application/classes/controller/lnapp/login.php +++ b/application/classes/controller/lnapp/login.php @@ -135,6 +135,7 @@ class Controller_lnApp_Login extends Controller_TemplateDefault { $mmto = ORM::factory('module_method_token') ->method(array('account','user_resetpassword')) ->account($ao) + ->uses(2) ->expire(time()+$token_expire*60); if ($mmto->generate()) { diff --git a/application/classes/controller/user/account.php b/application/classes/controller/user/account.php index 6db2be58..5b18ffbb 100644 --- a/application/classes/controller/user/account.php +++ b/application/classes/controller/user/account.php @@ -54,12 +54,6 @@ class Controller_User_Account extends Controller_TemplateDefault_User { 'body'=>_('Your updates didnt pass validation.').'
'.$output, )); } - else - SystemMessage::add(array( - 'title'=>_('Record NOT updated'), - 'type'=>'info', - 'body'=>_('Your account has NOT been updated.') - )); Block::add(array( 'title'=>_('Password Reset'), diff --git a/application/classes/lnapp/config.php b/application/classes/lnapp/config.php index f9b23b2c..e91d7bff 100644 --- a/application/classes/lnapp/config.php +++ b/application/classes/lnapp/config.php @@ -74,7 +74,7 @@ abstract class lnApp_Config extends Kohana_Config { public static function logo_uri() { 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() { diff --git a/application/classes/model/auth/userdefault.php b/application/classes/model/auth/userdefault.php index 50944557..a5e6ee26 100644 --- a/application/classes/model/auth/userdefault.php +++ b/application/classes/model/auth/userdefault.php @@ -63,7 +63,7 @@ class Model_Auth_UserDefault extends Model_Auth_User { * Test to see if a record has been changed */ public function changed() { - return ! (empty($this->_changed)); + return (count($this->_changed)); } /** diff --git a/modules/invoice/classes/controller/admin/invoice.php b/modules/invoice/classes/controller/admin/invoice.php index e1342772..67b1abcf 100644 --- a/modules/invoice/classes/controller/admin/invoice.php +++ b/modules/invoice/classes/controller/admin/invoice.php @@ -27,9 +27,9 @@ class Controller_Admin_Invoice extends Controller_TemplateDefault_Admin { array( 'id'=>array('label'=>'ID','url'=>'user/invoice/view/'), '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'), - 'billed_amt'=>array('label'=>'Payments','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'), diff --git a/modules/invoice/classes/controller/task/invoice.php b/modules/invoice/classes/controller/task/invoice.php index 03138772..3ff0e68c 100644 --- a/modules/invoice/classes/controller/task/invoice.php +++ b/modules/invoice/classes/controller/task/invoice.php @@ -60,10 +60,10 @@ class Controller_Task_Invoice extends Controller_Task { $action = array(); // @todo This should go in a config somewhere $days = 5; - $io = ORM::factory('invoice'); + $i = ORM::factory('invoice'); $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 ($io->remind($key) AND (is_null($x=$this->request->param('id')) OR $x != 'again')) continue; @@ -96,7 +96,7 @@ class Controller_Task_Invoice extends Controller_Task { */ public function action_remind_overdue() { $action = array(); - $io = ORM::factory('invoice'); + $i = ORM::factory('invoice'); $notice = $this->request->param('id'); $x = NULL; @@ -125,9 +125,8 @@ class Controller_Task_Invoice extends Controller_Task { } $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 ($io->remind($key) AND (is_null($x) OR $x != 'again')) continue; @@ -230,6 +229,56 @@ class Controller_Task_Invoice extends Controller_Task { $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 **/ public function action_audit_invoice_items() { @@ -241,19 +290,9 @@ class Controller_Task_Invoice extends Controller_Task { $iio->product_name = null; $iio->save(); } else { - 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); diff --git a/modules/invoice/classes/model/invoice.php b/modules/invoice/classes/model/invoice.php index b8d6c1e8..b6f972ff 100644 --- a/modules/invoice/classes/model/invoice.php +++ b/modules/invoice/classes/model/invoice.php @@ -450,5 +450,23 @@ class Model_Invoice extends ORMOSB { return $result; } + + public function html() { + // @todo This should be in a config file. + $css = ''; + + $output = View::factory('invoice/user/email') + ->set('mediapath',Route::get('default/media')) + ->set('io',$this); + + return $css.$output; + } } ?> diff --git a/modules/invoice/views/invoice/user/email.php b/modules/invoice/views/invoice/user/email.php new file mode 100644 index 00000000..007b0880 --- /dev/null +++ b/modules/invoice/views/invoice/user/email.php @@ -0,0 +1,145 @@ + + + + + + + + + + + +
+ + + + + +


+
  + + + + + + + + + + + + + + + + + + + + + + + + + +
TAX INVOICEid(); ?>
Issue Datedisplay('date_orig'); ?>
Due Datedisplay('due_date'); ?>
Current Chargestotal(TRUE); ?>
Payments Received to Datepayments_total(TRUE); ?>
Total Charges Due This Invoicedue(TRUE); ?>
+
 
+ + + + + items_service_periods() as $rs => $items) { ?> + + + + + + + + + + + + + + + + + + + + discount()) { ?> + + + + + + + + + tax_summary() as $tid => $amount) { + $m = ORM::factory('tax',$tid); + ?> + + + + + + + + + + + + + + + +
Charges Detail:
+Other Items 
  +
+ + + items_services($items) as $sid) { ?> + + + + + + + + + + items_service($sid) as $ito) { ?> + + + + + + + + + + + + + discount_amt) { ?> + + + + + + + + + + + + + + + +
id(); ?>service_name(); ?> (product_id; ?>)items_service_total($so->id));?>
 trannum();?>name();?>detail();?>period();?>subtotal());?> 
 (items_service_discount($so->id));?>)
 items_service_tax($so->id));?> 
+
+
Sub Total of Items:subtotal(TRUE); ?> 
Discounts:(discount(TRUE); ?>)
Taxes Included:
 description; ?> 
Total Invoice:total(TRUE); ?> 
Total Outstanding:account->invoices_due_total(NULL,TRUE); ?> 
+
diff --git a/modules/invoice/views/invoice/user/view.php b/modules/invoice/views/invoice/user/view.php index ac9eeff2..a1c52cb2 100644 --- a/modules/invoice/views/invoice/user/view.php +++ b/modules/invoice/views/invoice/user/view.php @@ -25,7 +25,7 @@ display('due_date'); ?> - Current Charges Due + Current Charges total(TRUE); ?> @@ -33,7 +33,7 @@ payments_total(TRUE); ?> - Total Charges Due + Total Charges Due This Invoice due(TRUE); ?> @@ -132,9 +132,13 @@ - Total: + Total Invoice: total(TRUE); ?>  + + Total Outstanding: + account->invoices_due_total(NULL,TRUE); ?>  + diff --git a/modules/service/classes/controller/admin/service.php b/modules/service/classes/controller/admin/service.php index fe996b83..f11c790a 100644 --- a/modules/service/classes/controller/admin/service.php +++ b/modules/service/classes/controller/admin/service.php @@ -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/'), 'name()'=>array('label'=>'Service'), 'plugin()->ipaddress()'=>array('label'=>'IP Address'), + 'product->plugin()->speed'=>array('label'=>'Speed'), 'product->plugin()->allowance()'=>array('label'=>'Allowance'), 'plugin()->traffic_thismonth()'=>array('label'=>'This 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/'), 'name()'=>array('label'=>'Service'), 'plugin()->ipaddress()'=>array('label'=>'IP Address'), + 'product->plugin()->speed'=>array('label'=>'Speed'), 'product->plugin()->allowance()'=>array('label'=>'Allowance'), 'plugin()->traffic_thismonth()'=>array('label'=>'This Month'), 'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'), diff --git a/modules/service/classes/controller/affiliate/service.php b/modules/service/classes/controller/affiliate/service.php index d28cae19..9d9fc289 100644 --- a/modules/service/classes/controller/affiliate/service.php +++ b/modules/service/classes/controller/affiliate/service.php @@ -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/'), 'name()'=>array('label'=>'Service'), 'plugin()->ipaddress()'=>array('label'=>'IP Address'), + 'product->plugin()->speed'=>array('label'=>'Speed'), 'product->plugin()->allowance()'=>array('label'=>'Allowance'), 'plugin()->traffic_thismonth()'=>array('label'=>'This 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/'), 'name()'=>array('label'=>'Service'), 'plugin()->ipaddress()'=>array('label'=>'IP Address'), + 'product->plugin()->speed'=>array('label'=>'Speed'), 'product->plugin()->allowance()'=>array('label'=>'Allowance'), 'plugin()->traffic_thismonth()'=>array('label'=>'This Month'), 'plugin()->traffic_lastmonth()'=>array('label'=>'Last Month'),