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/application/classes/Model/Account.php

185 lines
4.4 KiB
PHP
Raw Normal View History

2013-10-10 02:44:53 +00:00
<?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
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Account extends lnApp_Model_Account {
2013-10-10 02:44:53 +00:00
// Relationships
protected $_has_many = array(
2013-12-05 05:22:23 +00:00
'user_tokens'=>array('model'=>'user_token'),
'email_log'=>array('far_key'=>'id'),
'group'=>array('through'=>'account_group'),
'invoice'=>array('far_key'=>'id'),
2013-10-10 02:44:53 +00:00
'payment'=>array('far_key'=>'id'),
2013-12-05 05:22:23 +00:00
'service'=>array('far_key'=>'id'),
2013-10-10 02:44:53 +00:00
);
protected $_has_one = array(
'RTM'=>array('far_key'=>'id'),
);
2016-08-03 04:00:51 +00:00
protected $_belongs_to = array(
'country'=>array(),
'currency'=>array(),
'language'=>array(),
);
2013-10-10 02:44:53 +00:00
protected $_display_filters = array(
2016-08-31 05:03:09 +00:00
'active'=>array(
array('StaticList_YesNo::get',array(':value',TRUE)),
),
2013-10-10 02:44:53 +00:00
'date_orig'=>array(
2014-08-25 04:41:07 +00:00
array('Site::Date',array(':value')),
2013-10-10 02:44:53 +00:00
),
'date_last'=>array(
2014-08-25 04:41:07 +00:00
array('Site::Date',array(':value')),
2013-10-10 02:44:53 +00:00
),
);
2016-08-03 04:00:51 +00:00
/** REQUIRED ABSTRACT METHODS **/
/** LOCAL METHODS **/
2013-10-10 02:44:53 +00:00
public function activated() {
return $this->has('group');
}
2013-10-10 02:44:53 +00:00
/**
* Get a list of all invoices for this account
*/
public function invoices($processed=FALSE) {
$o = $this->invoice;
return $processed ? $o->find_all() : $o->where_unprocessed()->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) {
$result = array();
foreach ($this->invoices() as $io)
if ((is_null($date) OR $io->date_orig < $date) AND $io->due())
$result[$io->id] = $io;
return $result;
}
/**
* 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;
}
public function isAdmin() {
return ($this->RTM->loaded() AND is_null($this->RTM->parent_id));
2013-10-10 02:44:53 +00:00
}
/**
2016-08-03 04:00:51 +00:00
* Is this account a company account
2013-10-10 02:44:53 +00:00
*/
2016-08-03 04:00:51 +00:00
public function isCompany() {
return strlen($this->company) > 0;
}
public function isReseller() {
return $this->RTM->loaded();
2013-10-10 02:44:53 +00:00
}
/**
* Returns the company name if it exists, otherwise the persons name
*/
public function name($variable=NULL) {
return $this->isCompany() ? $this->company : $this->namesub();
}
/*
* Returns the persons name
*/
public function namesub($variable=NULL) {
return trim(sprintf('%s %s',$this->first_name,$this->last_name));
}
2013-10-10 02:44:53 +00:00
/**
* 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);
}
/**
* Generate a token for non-login authorised functinos
*/
public function token($token_expire,$module,$method,$uses) {
return ORM::factory('Module_Method_Token')
->method(array($module,$method))
->account($this)
->uses($uses)
->expire(time()+$token_expire*60)
->generate();
}
2013-10-10 02:44:53 +00:00
/**
* Search for accounts matching a term
*/
2016-08-03 04:00:51 +00:00
public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=array()) {
2013-10-10 02:44:53 +00:00
$ao = Auth::instance()->get_user();
$this->clear();
$this->where_active();
// Build our where clause
// First Name, Last name
if (preg_match('/\ /',$term)) {
list($fn,$ln) = explode(' ',$term,2);
$this->where_open()
->where_open()
2013-10-10 02:44:53 +00:00
->where('first_name','like','%'.$fn.'%')
->and_where('last_name','like','%'.$ln.'%')
->where_close()
->or_where('company','like','%'.$term.'%')
->where_close();
2013-10-10 02:44:53 +00:00
} elseif (is_numeric($term)) {
$this->where('id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$this->where('email','like','%'.$term.'%');
} else {
$this->where_open()
->where('company','like','%'.$term.'%')
->or_where('first_name','like','%'.$term.'%')
->or_where('last_name','like','%'.$term.'%')
->where_close();
}
// Restrict results to authorised accounts
array_push($limit,array('id','IN',$ao->RTM->customers($ao->RTM)));
2013-10-10 02:44:53 +00:00
2016-08-03 04:00:51 +00:00
return parent::list_autocomplete($term,$index,$value,$label,$limit,array_merge($options,array('parentbypass'=>TRUE)));
2013-10-10 02:44:53 +00:00
}
}
?>