263 lines
8.1 KiB
PHP
263 lines
8.1 KiB
PHP
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
/**
|
|
* This class provides OSB invoice task capabilities.
|
|
*
|
|
* @package OSB
|
|
* @subpackage Invoice
|
|
* @category Controllers/Task
|
|
* @author Deon George
|
|
* @copyright (c) 2010 Open Source Billing
|
|
* @license http://dev.osbill.net/license.html
|
|
*/
|
|
class Controller_Task_Invoice extends Controller_Task {
|
|
/**
|
|
* Email a list of invoice balances
|
|
*
|
|
* This function is typically used to list the overdue invoices to the admins
|
|
* @param string mode The callback method to use as the data list eg: overdue
|
|
*/
|
|
public function action_list() {
|
|
$mode = $this->request->param('id');
|
|
|
|
$io = ORM::factory('invoice');
|
|
$tm = 'list_'.$mode;
|
|
|
|
if (! method_exists($io,$tm))
|
|
throw new Kohana_Exception('Unknown Task List command :command',array(':command'=>$mode));
|
|
|
|
$total = $numinv = 0;
|
|
$duelist = View::factory('invoice/task/'.$tm.'_head');
|
|
foreach ($io->$tm() as $t) {
|
|
$duelist .= View::factory('invoice/task/'.$tm.'_body')
|
|
->set('io',$t);
|
|
|
|
$numinv++;
|
|
$total += $t->due();
|
|
}
|
|
$duelist .= View::factory('invoice/task/'.$tm.'_foot');
|
|
|
|
// Send our email
|
|
$et = Email_Template::instance('task_invoice_list_overdue');
|
|
|
|
// @todo Update this to be dynamic
|
|
$et->to = array('account'=>array(1,68));
|
|
$et->variables = array(
|
|
'TABLE'=>$duelist,
|
|
'NUM_INV'=>$numinv,
|
|
'TOTAL'=>$total,
|
|
);
|
|
$et->send();
|
|
|
|
$output = sprintf('List (%s) sent to: %s',$mode,implode(',',array_keys($et->to)));
|
|
$this->response->body($output);
|
|
}
|
|
|
|
/**
|
|
* Email a customers a reminder of their upcoming invoices that are due.
|
|
*/
|
|
public function action_remind_due() {
|
|
$action = array();
|
|
// @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($key) AND (is_null($x=$this->request->param('id')) 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'),
|
|
'FIRST_NAME'=>$io->account->first_name,
|
|
'INV_NUM'=>$io->refnum(),
|
|
'INV_URL'=>URL::site('user/invoice/view/'.$io->id,'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(_('Due Reminders Sent: ').join('|',$action));
|
|
}
|
|
|
|
/**
|
|
* Email a customers when their invoices are now overdue.
|
|
*/
|
|
public function action_remind_overdue() {
|
|
$action = array();
|
|
$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());
|
|
array_push($action,(string)$io);
|
|
}
|
|
}
|
|
|
|
$this->response->body(_('Overdue Reminders Sent: ').join('|',$action));
|
|
}
|
|
|
|
/**
|
|
* Generate our services invoices, based on the service next invoice date
|
|
*
|
|
* @param int ID Service ID to generate invoice for (optional)
|
|
*/
|
|
public function action_serviceinvoices() {
|
|
$action = array();
|
|
$snd = array(); // Our service next billing dates that need to be updated if this is successful.
|
|
$sid = $this->request->param('id');
|
|
|
|
// Sort our service by account_id, then we can generate 1 invoice.
|
|
$svs = ORM::factory('service')->list_invoicesoon();
|
|
Sort::MAsort($svs,'account_id,date_next_invoice');
|
|
|
|
$aid = $due = $io = NULL;
|
|
foreach ($svs as $so) {
|
|
if (! is_null($sid) AND $sid != $so->id)
|
|
continue;
|
|
|
|
// Close off invoice, and start a new one.
|
|
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))) {
|
|
// Close this invoice.
|
|
if (! is_null($io)) {
|
|
// 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));
|
|
}
|
|
|
|
// Start a new invoice.
|
|
$io = ORM::factory('invoice');
|
|
$io->due_date = $due = $io->min_due($so->date_next_invoice);
|
|
$io->account_id = $aid = $so->account_id;
|
|
$io->status = TRUE;
|
|
}
|
|
|
|
$ppa = $so->product->get_price_array();
|
|
// @todo Need to check our recurr_weekday configuration for items that need to be pro-rated and items that are billed on absolute dates.
|
|
$pdata = Period::details($so->recur_schedule,$so->product->price_recurr_weekday,$so->date_next_invoice,TRUE);
|
|
$iio = $io->add_item();
|
|
|
|
$iio->service_id = $so->id;
|
|
$iio->product_id = $so->product_id;
|
|
$iio->quantity = 1;
|
|
$iio->item_type = 0;
|
|
$iio->discount_amt = null; // @todo
|
|
$iio->price_type = $so->price_type; // @todo Do we need this?
|
|
// @todo Might be a better way to do this
|
|
$iio->price_base = isset($ppa[$so->recur_schedule]['price_base']) ? $ppa[$so->recur_schedule]['price_base'] : 0;
|
|
$iio->recurring_schedule = $so->recur_schedule;
|
|
$iio->date_start = $pdata['start_time']; // @todo
|
|
$iio->date_stop = $pdata['end_time']; // @todo
|
|
|
|
// Our service next billing date, if this invoice generation is successful.
|
|
$snd[$so->id] = $pdata['end_time']+86400;
|
|
|
|
array_push($action,(string)$so->id);
|
|
}
|
|
|
|
// 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));
|
|
|
|
// Update our service next billing dates.
|
|
foreach ($snd as $sid=>$date) {
|
|
$so = ORM::factory('service',$sid);
|
|
$so->date_next_invoice = $date;
|
|
$so->save();
|
|
}
|
|
|
|
$this->response->body(_('Services Invoiced: ').join('|',$action));
|
|
}
|
|
|
|
/** END **/
|
|
|
|
public function action_audit_invoice_items() {
|
|
$output = '';
|
|
|
|
foreach (ORM::factory('invoice_item')->find_all() as $iio) {
|
|
if ($iio->product_name AND $iio->product_id) {
|
|
if (md5(strtoupper($iio->product_name)) == md5(strtoupper($iio->product->name()))) {
|
|
$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())));
|
|
}
|
|
}
|
|
|
|
#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);
|
|
}
|
|
}
|
|
?>
|