First start at retiring Affiliate in favour of RTM

This commit is contained in:
Deon George
2013-04-05 09:42:29 +11:00
parent 9d4b2f50ff
commit 43dfd88bce
49 changed files with 680 additions and 570 deletions

View File

@@ -1,6 +1,7 @@
<?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
@@ -18,9 +19,12 @@ class Model_Account extends Model_Auth_UserDefault {
'payment'=>array('far_key'=>'id'),
'service' => array('far_key'=>'id'),
);
protected $_has_one = array(
'affiliate' => array('far_key'=>'id'),
'language'=>array('foreign_key'=>'id','far_key'=>'language_id'),
'country'=>array('foreign_key'=>'id'),
'currency'=>array('foreign_key'=>'id'),
'language'=>array('foreign_key'=>'id'),
'RTM'=>array('far_key'=>'id'),
);
protected $_display_filters = array(
@@ -36,59 +40,26 @@ class Model_Account extends Model_Auth_UserDefault {
);
/**
* Return an account name
* Our account number format
*/
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);
}
public function accnum() {
return sprintf('%s-%04s',Company::instance()->site(TRUE),$this->id);
}
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);
}
public function title($name) {
return StaticList_Title::form($name,$this->title);
}
public function currency($name) {
return StaticList_Module::form($name,'currency',$this->currency_id,'id','name',array('status'=>'=:1'),FALSE,array('class'=>'form_button'));
}
public function country($name) {
return StaticList_Module::form($name,'country',$this->country_id,'id','name',array('status'=>'=:1'),FALSE,array('class'=>'form_button'));
}
/**
* 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);
return $this->group->where_active()->find_all();
}
/**
* Get a list of all invoices for this account
*/
public function invoices() {
return $this->invoice->distinct('id')->find_all();
public function invoices($processed=FALSE) {
$o = $this->invoice;
return $processed ? $o->find_all() : $o->where_unprocessed()->find_all();
}
/**
@@ -129,22 +100,39 @@ class Model_Account extends Model_Auth_UserDefault {
return $alo->saved();
}
public function list_active() {
return $this->_where_active()->order_by('company,last_name,first_name')->find_all();
/**
* 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);
}
public function list_affiliates() {
$return = array();
/**
* List all the services for this account
*/
public function services($active=TRUE) {
$o = $this->service;
foreach ($this->list_services() as $so)
if (! isset($return[$so->affiliate_id]))
$return[$so->affiliate_id] = $so->affiliate;
return $return;
return $active ? $o->where_active()->find_all() : $o->find_all();
}
public function count_services($active=TRUE,$afid=NULL) {
return $this->list_services($active,$afid)->count();
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);
}
/**
@@ -152,6 +140,7 @@ class Model_Account extends Model_Auth_UserDefault {
*/
public function list_autocomplete($term,$index='id',array $limit=array()) {
$return = array();
$ao = Auth::instance()->get_user();
$this->clear();
$this->where_active();
@@ -190,6 +179,9 @@ class Model_Account extends Model_Auth_UserDefault {
$this->and_where($k,$s,$v);
}
// Restrict results to authorised accounts
$this->and_where('id','IN',$ao->RTM->customers($ao->RTM));
foreach ($this->find_all() as $o)
$return[$o->$index] = array(
'value'=>$o->$index,
@@ -198,14 +190,5 @@ class Model_Account extends Model_Auth_UserDefault {
return $return;
}
public function list_services($active=TRUE,$afid=NULL) {
$svs = $this->service->where_active();
if ($afid)
$svs->where('affiliate_id','=',$afid);
return $svs->find_all();
}
}
?>