This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/account/classes/model/account.php
2011-07-18 16:20:55 +10:00

115 lines
2.5 KiB
PHP

<?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'),
'group' => array('through' => 'account_group'),
'invoice' => array('far_key'=>'id'),
'payment'=>array(),
'service' => array(),
);
/**
* 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 date_last() {
return Config::date($this->date_last);
}
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;
}
/**
* Get a list of all invoices for this account
*/
public function invoices() {
$return = array();
foreach ($this->invoice->distinct('id')->find_all() as $invoice)
$return[$invoice->id] = $invoice;
return $return;
}
/**
* Get a list of due invoices for this account
*/
public function invoices_due() {
$return = array();
foreach ($this->invoices() as $invoice)
if ($invoice->due())
$return[$invoice->id] = $invoice;
return $return;
}
/**
* Calculate the total of invoices due for this account
*/
public function invoices_due_total($format=FALSE) {
$result = 0;
foreach ($this->invoices_due() as $invoice)
$result += $invoice->due();
if ($format)
return Currency::display($result);
else
return $result;
}
}
?>