2010-11-29 22:41:08 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @package lnApp
|
|
|
|
* @subpackage Auth
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2010 Deon George
|
|
|
|
* @license http://dev.leenooks.net/license.html
|
|
|
|
*/
|
|
|
|
class Model_Account extends Model_Auth_UserDefault {
|
|
|
|
// Relationships
|
|
|
|
protected $_has_many = array(
|
|
|
|
'user_tokens' => array('model' => 'user_token'),
|
2011-08-27 06:33:46 +00:00
|
|
|
'email_log' => array('far_key'=>'id'),
|
2010-11-29 22:41:08 +00:00
|
|
|
'group' => array('through' => 'account_group'),
|
2011-07-13 22:59:32 +00:00
|
|
|
'invoice' => array('far_key'=>'id'),
|
2011-08-02 06:20:11 +00:00
|
|
|
'payment'=>array('far_key'=>'id'),
|
2011-07-22 01:04:20 +00:00
|
|
|
'service' => array('far_key'=>'id'),
|
2010-11-29 22:41:08 +00:00
|
|
|
);
|
2011-09-26 10:12:54 +00:00
|
|
|
protected $_has_one = array(
|
|
|
|
'affiliate' => array('far_key'=>'id'),
|
2012-07-30 05:10:58 +00:00
|
|
|
'language'=>array('foreign_key'=>'id','far_key'=>'language_id'),
|
2011-09-26 10:12:54 +00:00
|
|
|
);
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2011-08-16 02:27:19 +00:00
|
|
|
protected $_display_filters = array(
|
|
|
|
'date_orig'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
|
|
|
'date_last'=>array(
|
|
|
|
array('Config::date',array(':value')),
|
|
|
|
),
|
2012-08-01 12:43:33 +00:00
|
|
|
'status'=>array(
|
|
|
|
array('StaticList_YesNo::display',array(':value')),
|
|
|
|
),
|
2011-08-16 02:27:19 +00:00
|
|
|
);
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
/**
|
|
|
|
* Return an account name
|
|
|
|
*/
|
|
|
|
public function name($withcompany=FALSE) {
|
|
|
|
if ($withcompany)
|
2011-12-20 05:46:10 +00:00
|
|
|
return sprintf('%s %s%s',$this->first_name,$this->last_name,$this->company ? sprintf(' (%s)',$this->company) : '');
|
2010-11-29 22:41:08 +00:00
|
|
|
else
|
|
|
|
return sprintf('%s %s',$this->first_name,$this->last_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function accnum() {
|
2012-01-11 08:59:20 +00:00
|
|
|
return sprintf('%s-%04s',Config::siteid(TRUE),$this->id);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:47:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-11-29 22:41:08 +00:00
|
|
|
public function title($name) {
|
|
|
|
return StaticList_Title::form($name,$this->title);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function currency($name) {
|
2011-12-21 01:39:21 +00:00
|
|
|
return StaticList_Module::form($name,'currency',$this->currency_id,'id','name',array('status'=>'=:1'),FALSE,array('class'=>'form_button'));
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function country($name) {
|
2012-08-01 12:43:33 +00:00
|
|
|
return StaticList_Module::form($name,'country',$this->country_id,'id','name',array('status'=>'=:1'),FALSE,array('class'=>'form_button'));
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the groups that an account belongs to
|
|
|
|
*/
|
|
|
|
public function groups() {
|
2011-09-24 13:13:38 +00:00
|
|
|
return $this->group->find_all();
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-26 10:12:54 +00:00
|
|
|
public function isAdmin() {
|
2010-11-29 22:41:08 +00:00
|
|
|
// @todo Define admins in the config file or DB
|
2012-11-09 23:13:57 +00:00
|
|
|
$admins = array(ORM::factory('Group',array('name'=>'Root')));
|
2010-11-29 22:41:08 +00:00
|
|
|
|
2011-09-26 10:12:54 +00:00
|
|
|
return $this->has('group',$admins);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
2011-05-02 12:28:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of all invoices for this account
|
|
|
|
*/
|
|
|
|
public function invoices() {
|
2011-07-14 09:09:03 +00:00
|
|
|
return $this->invoice->distinct('id')->find_all();
|
2011-05-02 12:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of due invoices for this account
|
2011-07-14 09:09:03 +00:00
|
|
|
*
|
|
|
|
* @param int Date (in secs) to only retrieve invoices prior to this date
|
2011-05-02 12:28:17 +00:00
|
|
|
*/
|
2011-07-14 09:09:03 +00:00
|
|
|
public function invoices_due($date=NULL) {
|
2011-05-02 12:28:17 +00:00
|
|
|
$return = array();
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
foreach ($this->invoices() as $io)
|
|
|
|
if ((is_null($date) OR $io->date_orig < $date) AND $io->due())
|
|
|
|
$return[$io->id] = $io;
|
2011-05-02 12:28:17 +00:00
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the total of invoices due for this account
|
|
|
|
*/
|
2011-07-14 09:09:03 +00:00
|
|
|
public function invoices_due_total($date=NULL,$format=FALSE) {
|
2011-05-02 12:28:17 +00:00
|
|
|
$result = 0;
|
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
foreach ($this->invoices_due($date) as $io)
|
|
|
|
$result += $io->due();
|
2011-05-02 12:28:17 +00:00
|
|
|
|
2011-10-11 08:52:31 +00:00
|
|
|
// @todo This shouldnt really be required
|
|
|
|
if ($result < 0)
|
2012-10-07 04:15:34 +00:00
|
|
|
throw new Kohana_Exception($result);
|
2011-10-11 08:52:31 +00:00
|
|
|
|
2011-07-14 09:09:03 +00:00
|
|
|
return $format ? Currency::display($result) : $result;
|
2011-05-02 12:28:17 +00:00
|
|
|
}
|
2011-09-27 11:22:13 +00:00
|
|
|
|
|
|
|
public function log($message) {
|
|
|
|
// Log the logout
|
2012-11-09 23:13:57 +00:00
|
|
|
$alo = ORM::factory('Account_Log');
|
2011-09-27 11:22:13 +00:00
|
|
|
$alo->account_id = $this->id;
|
2011-12-20 05:46:10 +00:00
|
|
|
$alo->ip = Request::$client_ip;
|
2011-09-27 11:22:13 +00:00
|
|
|
$alo->details = $message;
|
|
|
|
$alo->save();
|
|
|
|
|
|
|
|
return $alo->saved();
|
|
|
|
}
|
2011-12-20 05:46:10 +00:00
|
|
|
|
2012-07-30 07:47:28 +00:00
|
|
|
public function list_active() {
|
2012-08-01 12:43:33 +00:00
|
|
|
return $this->_where_active()->order_by('company,last_name,first_name')->find_all();
|
2012-07-30 07:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function list_affiliates() {
|
|
|
|
$return = array();
|
|
|
|
|
|
|
|
foreach ($this->list_services() as $so)
|
|
|
|
if (! isset($return[$so->affiliate_id]))
|
|
|
|
$return[$so->affiliate_id] = $so->affiliate;
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function count_services($active=TRUE,$afid=NULL) {
|
2012-08-01 12:43:33 +00:00
|
|
|
return $this->list_services($active,$afid)->count();
|
2012-07-30 07:47:28 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 05:46:10 +00:00
|
|
|
/**
|
|
|
|
* Search for accounts matching a term
|
|
|
|
*/
|
2012-10-07 04:15:34 +00:00
|
|
|
public function list_autocomplete($term,$index='id',array $limit=array()) {
|
2011-12-20 05:46:10 +00:00
|
|
|
$return = array();
|
|
|
|
|
|
|
|
$this->clear();
|
2012-10-07 04:15:34 +00:00
|
|
|
$this->where_active();
|
2011-12-20 05:46:10 +00:00
|
|
|
$value = 'name(TRUE)';
|
|
|
|
|
|
|
|
// 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.'%');
|
|
|
|
$value = 'email';
|
|
|
|
|
|
|
|
} else {
|
2012-10-07 04:15:34 +00:00
|
|
|
$this->where_open()
|
|
|
|
->where('company','like','%'.$term.'%')
|
2011-12-20 05:46:10 +00:00
|
|
|
->or_where('first_name','like','%'.$term.'%')
|
|
|
|
->or_where('last_name','like','%'.$term.'%')
|
2012-10-07 04:15:34 +00:00
|
|
|
->or_where('email','like','%'.$term.'%')
|
|
|
|
->where_close();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($limit as $w) {
|
|
|
|
list($k,$s,$v) = $w;
|
|
|
|
|
|
|
|
$this->and_where($k,$s,$v);
|
2011-12-20 05:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->find_all() as $o)
|
|
|
|
$return[$o->$index] = array(
|
|
|
|
'value'=>$o->$index,
|
|
|
|
'label'=>sprintf('ACC %s: %s',$o->id,Table::resolve($o,$value)),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2012-07-30 07:47:28 +00:00
|
|
|
|
|
|
|
public function list_services($active=TRUE,$afid=NULL) {
|
2012-08-01 12:43:33 +00:00
|
|
|
$svs = $this->service->where_active();
|
2012-07-30 07:47:28 +00:00
|
|
|
|
|
|
|
if ($afid)
|
|
|
|
$svs->where('affiliate_id','=',$afid);
|
|
|
|
|
|
|
|
return $svs->find_all();
|
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
2011-05-02 12:28:17 +00:00
|
|
|
?>
|