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( 'affiliate' => 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( array('StaticList_YesNo::display',array(':value')), ), ); /** * 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() { return sprintf('%02s-%04s',Config::siteid(),$this->id); } 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(); } public function isAdmin() { // @todo Define admins in the config file or DB $admins = array(ORM::factory('group',array('name'=>'Root'))); return $this->has('group',$admins); } /** * Get a list of all invoices for this account */ public function invoices() { return $this->invoice->distinct('id')->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) { $return = array(); foreach ($this->invoices() as $io) if ((is_null($date) OR $io->date_orig < $date) AND $io->due()) $return[$io->id] = $io; return $return; } /** * 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; } } ?>