2011-08-16 02:27:19 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides OSB invoice task capabilities.
|
|
|
|
*
|
2013-03-19 22:35:19 +00:00
|
|
|
* @package Invoice
|
|
|
|
* @category Controllers
|
2011-08-16 02:27:19 +00:00
|
|
|
* @author Deon George
|
2013-03-19 22:35:19 +00:00
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
2011-08-16 02:27:19 +00:00
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
|
|
|
class Controller_Task_Invoice extends Controller_Task {
|
2013-05-02 09:52:19 +00:00
|
|
|
public $auto_render = FALSE;
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
/**
|
|
|
|
* Email a customers a reminder of their upcoming invoices that are due.
|
|
|
|
*/
|
2011-09-26 10:12:54 +00:00
|
|
|
public function action_remind_due() {
|
2011-10-11 08:52:31 +00:00
|
|
|
$action = array();
|
2011-09-27 11:22:13 +00:00
|
|
|
$key = 'remind_due';
|
2012-11-09 23:13:57 +00:00
|
|
|
$days = ORM::factory('Invoice')->config('REMIND_DUE');
|
2011-09-26 10:12:54 +00:00
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
foreach (ORM::factory('Invoice')->list_due(time()+86400*$days) as $io) {
|
2012-03-30 04:13:01 +00:00
|
|
|
// @todo Use another option to supress reminders
|
2011-09-26 10:12:54 +00:00
|
|
|
// If we have already sent a reminder, we'll skip to the next one.
|
2012-03-30 04:13:01 +00:00
|
|
|
if (($io->remind($key) AND (is_null($x=$this->request->param('id')) OR $x != 'again')) OR ($io->account->invoice_delivery != 1))
|
2011-09-26 10:12:54 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Send our email
|
2011-09-27 11:22:13 +00:00
|
|
|
$et = Email_Template::instance('task_invoice_'.$key);
|
2011-09-26 10:12:54 +00:00
|
|
|
|
|
|
|
$et->to = array('account'=>array($io->account_id));
|
|
|
|
$et->variables = array(
|
|
|
|
'DUE'=>$io->due(TRUE),
|
2011-09-27 11:22:13 +00:00
|
|
|
'DUE_DATE'=>$io->display('due_date'),
|
|
|
|
'FIRST_NAME'=>$io->account->first_name,
|
2011-09-26 10:12:54 +00:00
|
|
|
'INV_NUM'=>$io->refnum(),
|
2013-02-12 11:14:59 +00:00
|
|
|
'INV_URL'=>URL::site(URL::link('user','invoice/view/'.$io->id),'http'),
|
|
|
|
'SITE_NAME'=>Company::instance()->name(),
|
2011-09-27 11:22:13 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// @todo Record email log id if possible.
|
2011-10-11 08:52:31 +00:00
|
|
|
if ($et->send()) {
|
2011-09-27 11:22:13 +00:00
|
|
|
$io->set_remind($key,time());
|
2011-10-11 08:52:31 +00:00
|
|
|
array_push($action,(string)$io);
|
|
|
|
}
|
2011-09-27 11:22:13 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
$this->response->body(_('Due Reminders Sent: ').join('|',$action));
|
2011-09-27 11:22:13 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
/**
|
|
|
|
* Generate our services invoices, based on the service next invoice date
|
|
|
|
*
|
2012-03-30 04:13:01 +00:00
|
|
|
* @param int ID Service ID to generate invoice for (optional) - multiple services colon separated
|
2011-10-11 08:52:31 +00:00
|
|
|
*/
|
2011-10-14 05:44:12 +00:00
|
|
|
public function action_services() {
|
|
|
|
// Used to only process X invoices in a row.
|
2012-11-09 23:13:57 +00:00
|
|
|
$max = ($x=Kohana::$config->load('debug')->invoice) ? $x : ORM::factory('Invoice')->config('GEN_INV_MAX');
|
2012-03-30 04:13:01 +00:00
|
|
|
// Our service next billing dates that need to be updated if this is successful.
|
|
|
|
$snd = array();
|
|
|
|
// Our charges that need to be updated if this is successful.
|
|
|
|
$chgs = array();
|
|
|
|
// If we are invoicing a specific service
|
2011-10-14 05:44:12 +00:00
|
|
|
$sid = is_null($this->request->param('id')) ? NULL : explode(':',$this->request->param('id'));
|
2011-10-11 08:52:31 +00:00
|
|
|
// Sort our service by account_id, then we can generate 1 invoice.
|
2012-11-09 23:13:57 +00:00
|
|
|
$svs = ORM::factory('Service')->list_invoicesoon()->as_array();
|
2011-10-11 08:52:31 +00:00
|
|
|
Sort::MAsort($svs,'account_id,date_next_invoice');
|
|
|
|
|
|
|
|
$aid = $due = $io = NULL;
|
2011-10-14 05:44:12 +00:00
|
|
|
$max_count = 0;
|
2011-10-11 08:52:31 +00:00
|
|
|
foreach ($svs as $so) {
|
2011-10-14 05:44:12 +00:00
|
|
|
// If we are generating an invoice for a service, skip to that service.
|
|
|
|
if (! is_null($sid) AND ! in_array($so->id,$sid))
|
2011-10-11 08:52:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Close off invoice, and start a new one.
|
2011-10-14 05:44:12 +00:00
|
|
|
if (is_null($io) OR (! is_null($aid) AND $aid != $so->account_id) OR (! is_null($due) AND $due != $io->min_due($so->date_next_invoice))) {
|
2011-10-11 08:52:31 +00:00
|
|
|
// Close this invoice.
|
2011-10-14 05:44:12 +00:00
|
|
|
if (is_object($io)) {
|
2011-10-11 08:52:31 +00:00
|
|
|
// Save our invoice.
|
|
|
|
if (! $io->save())
|
|
|
|
throw new Kohana_Exception('Failed to save invoice :invoice for service :service',array(':invoice'=>$io->id,':service'=>$so->id));
|
|
|
|
}
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
// If we have issued the max number of invoices this round, finish.
|
2012-01-11 23:23:35 +00:00
|
|
|
if ($max AND (++$max_count > $max))
|
2011-10-14 05:44:12 +00:00
|
|
|
break;
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
// Start a new invoice.
|
2012-11-09 23:13:57 +00:00
|
|
|
$io = ORM::factory('Invoice');
|
2011-10-11 08:52:31 +00:00
|
|
|
$io->due_date = $due = $io->min_due($so->date_next_invoice);
|
|
|
|
$io->account_id = $aid = $so->account_id;
|
|
|
|
$io->status = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pdata = Period::details($so->recur_schedule,$so->product->price_recurr_weekday,$so->date_next_invoice,TRUE);
|
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
$iio = $io->add_item();
|
2011-10-11 08:52:31 +00:00
|
|
|
$iio->service_id = $so->id;
|
|
|
|
$iio->product_id = $so->product_id;
|
2011-10-14 05:44:12 +00:00
|
|
|
$iio->quantity = $pdata['prorata'];
|
2012-03-30 04:13:01 +00:00
|
|
|
$iio->item_type = 0; // Service Billing
|
2012-07-05 03:06:17 +00:00
|
|
|
$iio->discount_amt = NULL; // @todo
|
2012-03-30 04:13:01 +00:00
|
|
|
$iio->price_base = $so->price();
|
2011-10-11 08:52:31 +00:00
|
|
|
$iio->recurring_schedule = $so->recur_schedule;
|
2012-03-30 04:13:01 +00:00
|
|
|
$iio->date_start = $pdata['start_time'];
|
|
|
|
$iio->date_stop = $pdata['end_time'];
|
2011-10-11 08:52:31 +00:00
|
|
|
|
|
|
|
// Our service next billing date, if this invoice generation is successful.
|
|
|
|
$snd[$so->id] = $pdata['end_time']+86400;
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
// Check if there are any charges
|
2012-11-09 23:13:57 +00:00
|
|
|
$c = ORM::factory('Charge')
|
2011-10-14 05:44:12 +00:00
|
|
|
->where('service_id','=',$so->id)
|
|
|
|
->where('status','=',0)
|
|
|
|
->where('sweep_type','=',6); // @todo This needs to be dynamic, not "6"
|
|
|
|
|
|
|
|
foreach ($c->find_all() as $co) {
|
|
|
|
$iio = $io->add_item();
|
|
|
|
$iio->service_id = $co->service_id;
|
|
|
|
$iio->product_id = $co->product_id;
|
|
|
|
$iio->charge_id = $co->id;
|
|
|
|
$iio->quantity = $co->quantity;
|
|
|
|
$iio->item_type = 5; // @todo This probably should not be hard coded as "5".
|
2012-07-05 03:06:17 +00:00
|
|
|
$iio->discount_amt = NULL; // @todo
|
2011-10-14 05:44:12 +00:00
|
|
|
$iio->price_base = $co->amount;
|
|
|
|
$iio->date_start = $co->date_orig;
|
|
|
|
$iio->date_stop = $co->date_orig; // @todo
|
|
|
|
|
|
|
|
// @todo Temp
|
2012-03-30 04:13:01 +00:00
|
|
|
// We'll mark any charges as temporarily processed, although they should be set to status=1 later.
|
2011-10-14 05:44:12 +00:00
|
|
|
$co->status=2;
|
|
|
|
$co->save();
|
2012-03-30 04:13:01 +00:00
|
|
|
array_push($chgs,$co->id);
|
2011-10-14 05:44:12 +00:00
|
|
|
}
|
2011-10-11 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save our invoice.
|
2012-01-11 23:23:35 +00:00
|
|
|
if ($io AND ! $io->saved() AND ! $io->save()) {
|
2011-10-14 05:44:12 +00:00
|
|
|
print_r($io->items());
|
2011-10-11 08:52:31 +00:00
|
|
|
throw new Kohana_Exception('Failed to save invoice :invoice for service :service',array(':invoice'=>$io->id,':service'=>$so->id));
|
2011-10-14 05:44:12 +00:00
|
|
|
}
|
2011-10-11 08:52:31 +00:00
|
|
|
|
|
|
|
// Update our service next billing dates.
|
2012-03-30 04:13:01 +00:00
|
|
|
// @todo Catch any update errors
|
2011-10-11 08:52:31 +00:00
|
|
|
foreach ($snd as $sid=>$date) {
|
2012-11-09 23:13:57 +00:00
|
|
|
$so = ORM::factory('Service',$sid);
|
2011-10-11 08:52:31 +00:00
|
|
|
$so->date_next_invoice = $date;
|
|
|
|
$so->save();
|
|
|
|
}
|
|
|
|
|
2012-03-30 04:13:01 +00:00
|
|
|
// Update any processed charges as such
|
|
|
|
// @todo Catch any update errors
|
|
|
|
foreach ($chgs as $cid) {
|
2012-11-09 23:13:57 +00:00
|
|
|
$co = ORM::factory('Charge',$cid);
|
2012-03-30 04:13:01 +00:00
|
|
|
$co->status=1;
|
|
|
|
$co->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response->body(_('Services Invoiced: ').join('|',array_keys($snd)));
|
2011-10-11 08:52:31 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
public function action_send() {
|
|
|
|
// Used to only process X invoices in a row.
|
2012-11-09 23:13:57 +00:00
|
|
|
$max = ORM::factory('Invoice')->config('EMAIL_INV_MAX');
|
2012-01-11 23:23:35 +00:00
|
|
|
|
2011-10-12 22:20:08 +00:00
|
|
|
$action = array();
|
|
|
|
$iid = $this->request->param('id');
|
|
|
|
$x = NULL;
|
|
|
|
|
|
|
|
if (preg_match('/:/',$iid))
|
|
|
|
list($iid,$x) = explode(':',$iid);
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
// Get our list of invoices to send
|
2012-11-09 23:13:57 +00:00
|
|
|
$i = $iid ? ORM::factory('Invoice')->where('id','=',$iid) : ORM::factory('Invoice')->list_tosend();
|
2011-10-12 22:20:08 +00:00
|
|
|
|
|
|
|
$key = 'send';
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
$max_count = 0;
|
2011-10-12 22:20:08 +00:00
|
|
|
foreach ($i->find_all() as $io) {
|
2012-03-30 04:13:01 +00:00
|
|
|
// If we have already sent a reminder or we dont email invoices we'll skip to the next one.
|
|
|
|
if (($io->remind($key) AND (is_null($x) OR $x != 'again')) OR ($io->account->invoice_delivery != 1))
|
2011-10-12 22:20:08 +00:00
|
|
|
continue;
|
|
|
|
|
2011-10-14 05:44:12 +00:00
|
|
|
// If we have issued the max number of invoices this round, finish.
|
|
|
|
if (++$max_count > $max)
|
|
|
|
break;
|
|
|
|
|
2011-10-12 22:20:08 +00:00
|
|
|
// Send our email
|
|
|
|
$et = Email_Template::instance('task_invoice_'.$key);
|
2012-11-09 23:13:57 +00:00
|
|
|
$token = ORM::factory('Module_Method_Token')
|
2011-10-12 22:20:08 +00:00
|
|
|
->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'),
|
2013-02-26 03:19:32 +00:00
|
|
|
'EMAIL'=>Company::instance()->email(),
|
2011-10-12 22:20:08 +00:00
|
|
|
'FIRST_NAME'=>$io->account->first_name,
|
|
|
|
'HTML_INVOICE'=>$io->html(),
|
|
|
|
'INV_NUM'=>$io->refnum(),
|
2013-02-12 11:14:59 +00:00
|
|
|
'INV_URL'=>URL::site(URL::link('user','invoice/view/'.$io->id),'http'),
|
|
|
|
'INV_URL_DOWNLOAD'=>URL::site(URL::link('user',sprintf('invoice/download/%s?token=%s',$io->id,$token)),'http'),
|
|
|
|
'SITE_NAME'=>Company::instance()->name(),
|
2011-10-12 22:20:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// @todo Record email log id if possible.
|
|
|
|
if ($et->send()) {
|
2011-10-14 05:44:12 +00:00
|
|
|
$io->print_status = 1;
|
2012-03-30 04:13:01 +00:00
|
|
|
$io->set_remind($key,time(),($x=='again' ? TRUE : FALSE));
|
2011-10-12 22:20:08 +00:00
|
|
|
array_push($action,(string)$io);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response->body(_('Invoices Sent: ').join('|',$action));
|
|
|
|
}
|
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
/** END **/
|
|
|
|
|
|
|
|
public function action_audit_invoice_items() {
|
|
|
|
$output = '';
|
|
|
|
|
2012-11-09 23:13:57 +00:00
|
|
|
foreach (ORM::factory('Invoice_Item')->find_all() as $iio) {
|
2011-10-11 08:52:31 +00:00
|
|
|
if ($iio->product_name AND $iio->product_id) {
|
2013-04-26 01:42:09 +00:00
|
|
|
if (md5(strtoupper($iio->product_name)) == md5(strtoupper($iio->product->title()))) {
|
2012-07-05 03:06:17 +00:00
|
|
|
$iio->product_name = NULL;
|
2011-10-11 08:52:31 +00:00
|
|
|
$iio->save();
|
|
|
|
} else {
|
2013-04-26 01:42:09 +00:00
|
|
|
print_r(array("DIFF",'id'=>$iio->id,'pn'=>serialize($iio->product_name),'ppn'=>serialize($iio->product->title()),'pid'=>$iio->product_id,'test'=>strcasecmp($iio->product_name,$iio->product->title())));
|
2011-10-11 08:52:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response->body($output);
|
2011-09-26 10:12:54 +00:00
|
|
|
}
|
2011-08-16 02:27:19 +00:00
|
|
|
}
|
|
|
|
?>
|