2013-10-10 02:44:53 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Model manages both the accounts that users use to login to the system, as well as the account where services are owned.
|
|
|
|
*
|
|
|
|
* @package OSB
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2009-2013 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
|
|
|
class Model_Account extends Model_Auth_UserDefault {
|
|
|
|
// Relationships
|
|
|
|
protected $_has_many = array(
|
|
|
|
'user_tokens' => array('model' => 'user_token'),
|
|
|
|
'email_log' => array('far_key'=>'id'),
|
|
|
|
'group' => array('through' => 'account_group'),
|
|
|
|
'invoice' => array('far_key'=>'id'),
|
|
|
|
'payment'=>array('far_key'=>'id'),
|
|
|
|
'service' => array('far_key'=>'id'),
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $_has_one = array(
|
|
|
|
'country'=>array('foreign_key'=>'id'),
|
|
|
|
'currency'=>array('foreign_key'=>'id'),
|
|
|
|
'language'=>array('foreign_key'=>'id'),
|
|
|
|
'RTM'=>array('far_key'=>'id'),
|
|
|
|
);
|
|
|
|
|
|
|
|
protected $_display_filters = array(
|
|
|
|
'date_orig'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
|
|
|
'date_last'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
|
|
|
'status'=>array(
|
2013-04-26 01:42:09 +00:00
|
|
|
array('StaticList_YesNo::get',array(':value')),
|
2013-10-10 02:44:53 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Our account number format
|
|
|
|
*/
|
|
|
|
public function accnum() {
|
|
|
|
return sprintf('%s-%04s',Company::instance()->site(TRUE),$this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the groups that an account belongs to
|
|
|
|
*/
|
|
|
|
public function groups() {
|
2013-04-26 01:42:09 +00:00
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->group->where_active()->find_all() as $go)
|
|
|
|
foreach ($go->list_parentgrps(TRUE) as $cgo)
|
|
|
|
if (empty($result[$cgo->id]))
|
|
|
|
$result[$cgo->id] = $cgo;
|
|
|
|
|
|
|
|
return $result;
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all invoices for this account
|
|
|
|
*/
|
|
|
|
public function invoices($processed=FALSE) {
|
|
|
|
$o = $this->invoice;
|
|
|
|
|
|
|
|
return $processed ? $o->find_all() : $o->where_unprocessed()->find_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of due invoices for this account
|
|
|
|
*
|
|
|
|
* @param int Date (in secs) to only retrieve invoices prior to this date
|
|
|
|
*/
|
|
|
|
public function invoices_due($date=NULL) {
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($this->invoices() as $io)
|
|
|
|
if ((is_null($date) OR $io->date_orig < $date) AND $io->due())
|
|
|
|
$result[$io->id] = $io;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the total of invoices due for this account
|
|
|
|
*/
|
|
|
|
public function invoices_due_total($date=NULL,$format=FALSE) {
|
|
|
|
$result = 0;
|
|
|
|
|
|
|
|
foreach ($this->invoices_due($date) as $io)
|
|
|
|
$result += $io->due();
|
|
|
|
|
|
|
|
return $format ? Currency::display($result) : $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function log($message) {
|
|
|
|
// Log the logout
|
|
|
|
$alo = ORM::factory('Account_Log');
|
|
|
|
$alo->account_id = $this->id;
|
|
|
|
$alo->ip = Request::$client_ip;
|
|
|
|
$alo->details = $message;
|
|
|
|
$alo->save();
|
|
|
|
|
|
|
|
return $alo->saved();
|
|
|
|
}
|
|
|
|
|
2013-04-26 01:42:09 +00:00
|
|
|
/**
|
|
|
|
* This function will extract the available methods for this account
|
|
|
|
* This is used both for menu options and method security
|
|
|
|
*/
|
|
|
|
public function methods() {
|
|
|
|
static $result = array();
|
|
|
|
|
|
|
|
// @todo We may want to optimise this with some session caching.
|
|
|
|
if ($result)
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
foreach ($this->groups() as $go)
|
|
|
|
foreach ($go->module_method->find_all() as $mmo)
|
|
|
|
if (empty($result[$mmo->id]))
|
|
|
|
$result[$mmo->id] = $mmo;
|
|
|
|
|
|
|
|
Sort::MAsort($result,'module->name,name');
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-10-10 02:44:53 +00:00
|
|
|
/**
|
|
|
|
* Return an account name
|
|
|
|
*/
|
|
|
|
public function name($withcompany=FALSE) {
|
|
|
|
if ($withcompany)
|
|
|
|
return sprintf('%s %s%s',$this->first_name,$this->last_name,$this->company ? sprintf(' (%s)',$this->company) : '');
|
|
|
|
else
|
|
|
|
return sprintf('%s %s',$this->first_name,$this->last_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List all the services for this account
|
|
|
|
*/
|
|
|
|
public function services($active=TRUE) {
|
|
|
|
$o = $this->service;
|
|
|
|
|
|
|
|
return $active ? $o->where_active()->find_all() : $o->find_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function services_count($active=TRUE,$afid=NULL) {
|
|
|
|
return $this->services($active)->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The key we use to sort entries of this model type
|
|
|
|
*/
|
|
|
|
public function sortkey($withcompany=FALSE) {
|
|
|
|
$sk = '';
|
|
|
|
|
|
|
|
if ($withcompany AND $this->company)
|
|
|
|
$sk .= $this->company.' ';
|
|
|
|
|
|
|
|
return $sk.sprintf('%s %s',$this->last_name,$this->first_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for accounts matching a term
|
|
|
|
*/
|
2013-05-08 09:00:47 +00:00
|
|
|
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=NULL) {
|
2013-10-10 02:44:53 +00:00
|
|
|
$ao = Auth::instance()->get_user();
|
|
|
|
|
|
|
|
$this->clear();
|
|
|
|
$this->where_active();
|
|
|
|
|
|
|
|
// Build our where clause
|
|
|
|
// First Name, Last name
|
|
|
|
if (preg_match('/\ /',$term)) {
|
|
|
|
list($fn,$ln) = explode(' ',$term,2);
|
|
|
|
|
|
|
|
$this->where_open()
|
|
|
|
->where('first_name','like','%'.$fn.'%')
|
|
|
|
->and_where('last_name','like','%'.$ln.'%')
|
|
|
|
->where_close()
|
|
|
|
->or_where('company','like','%'.$term.'%');
|
|
|
|
|
|
|
|
} elseif (is_numeric($term)) {
|
|
|
|
$this->where('id','like','%'.$term.'%');
|
|
|
|
|
|
|
|
} elseif (preg_match('/\@/',$term)) {
|
|
|
|
$this->where('email','like','%'.$term.'%');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->where_open()
|
|
|
|
->where('company','like','%'.$term.'%')
|
|
|
|
->or_where('first_name','like','%'.$term.'%')
|
|
|
|
->or_where('last_name','like','%'.$term.'%')
|
|
|
|
->or_where('email','like','%'.$term.'%')
|
|
|
|
->where_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restrict results to authorised accounts
|
2013-05-08 09:00:47 +00:00
|
|
|
array_push($limit,array('id','IN',$ao->RTM->customers($ao->RTM)));
|
2013-10-10 02:44:53 +00:00
|
|
|
|
2013-05-08 09:00:47 +00:00
|
|
|
return parent::list_autocomplete($term,$index,$value,$label,$limit,$options);
|
2013-10-10 02:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|