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-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')),
|
|
|
|
),
|
|
|
|
'status'=>array(
|
|
|
|
array('StaticList_YesNo::display',array(':value')),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2010-11-29 22:41:08 +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);
|
|
|
|
else
|
|
|
|
return sprintf('%s %s',$this->first_name,$this->last_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function accnum() {
|
2011-05-02 12:28:17 +00:00
|
|
|
return sprintf('%02s-%04s',Config::siteid(),$this->id);
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function title($name) {
|
|
|
|
return StaticList_Title::form($name,$this->title);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function currency($name) {
|
|
|
|
return StaticListModule::form($name,'currency',$this->currency_id,'id','name',array());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function country($name) {
|
|
|
|
return StaticListModule::form($name,'country',$this->country_id,'id','name',array());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function language($name) {
|
|
|
|
// @todo To setup
|
|
|
|
return 'en';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the groups that an account belongs to
|
|
|
|
*/
|
|
|
|
public function groups() {
|
|
|
|
return $this->group->find_all()->as_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function admin() {
|
|
|
|
// @todo Define admins in the config file or DB
|
|
|
|
$admins = array('Root');
|
|
|
|
|
|
|
|
foreach ($this->groups() as $go)
|
|
|
|
if (in_array($go->name,$admins))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
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-07-14 09:09:03 +00:00
|
|
|
return $format ? Currency::display($result) : $result;
|
2011-05-02 12:28:17 +00:00
|
|
|
}
|
2010-11-29 22:41:08 +00:00
|
|
|
}
|
2011-05-02 12:28:17 +00:00
|
|
|
?>
|