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

195 lines
4.7 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +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.
2013-03-19 22:35:19 +00:00
*
* @package OSB
2010-11-29 22:41:08 +00:00
* @category Models
* @author Deon George
2013-03-19 22:35:19 +00:00
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
2010-11-29 22:41:08 +00:00
*/
class Model_Account extends Model_Auth_UserDefault {
// Relationships
protected $_has_many = array(
'user_tokens' => array('model' => 'user_token'),
'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'),
'service' => array('far_key'=>'id'),
2010-11-29 22:41:08 +00:00
);
protected $_has_one = array(
'country'=>array('foreign_key'=>'id'),
'currency'=>array('foreign_key'=>'id'),
'language'=>array('foreign_key'=>'id'),
'RTM'=>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')),
),
2012-08-01 12:43:33 +00:00
'status'=>array(
array('StaticList_YesNo::display',array(':value')),
),
2011-08-16 02:27:19 +00:00
);
2010-11-29 22:41:08 +00:00
/**
* Our account number format
2010-11-29 22:41:08 +00:00
*/
public function accnum() {
return sprintf('%s-%04s',Company::instance()->site(TRUE),$this->id);
2010-11-29 22:41:08 +00:00
}
/**
* Get the groups that an account belongs to
*/
public function groups() {
return $this->group->where_active()->find_all();
2010-11-29 22:41:08 +00:00
}
2011-05-02 12:28:17 +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();
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) {
$result = array();
2011-05-02 12:28:17 +00:00
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())
$result[$io->id] = $io;
2011-05-02 12:28:17 +00:00
return $result;
2011-05-02 12:28:17 +00:00
}
/**
* 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
}
2011-09-27 11:22:13 +00:00
public function log($message) {
// Log the logout
2012-11-09 23:13:57 +00:00
$alo = ORM::factory('Account_Log');
2011-09-27 11:22:13 +00:00
$alo->account_id = $this->id;
2011-12-20 05:46:10 +00:00
$alo->ip = Request::$client_ip;
2011-09-27 11:22:13 +00:00
$alo->details = $message;
$alo->save();
return $alo->saved();
}
2011-12-20 05:46:10 +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 ? sprintf(' (%s)',$this->company) : '');
else
return sprintf('%s %s',$this->first_name,$this->last_name);
}
/**
* List all the services for this account
*/
public function services($active=TRUE) {
$o = $this->service;
return $active ? $o->where_active()->find_all() : $o->find_all();
}
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);
}
2011-12-20 05:46:10 +00:00
/**
* Search for accounts matching a term
*/
public function list_autocomplete($term,$index='id',array $limit=array()) {
$result = array();
$ao = Auth::instance()->get_user();
2011-12-20 05:46:10 +00:00
$this->clear();
$this->where_active();
2011-12-20 05:46:10 +00:00
$value = 'name(TRUE)';
// Build our where clause
// First Name, Last name
if (preg_match('/\ /',$term)) {
list($fn,$ln) = explode(' ',$term,2);
$this->where_open()
->where('first_name','like','%'.$fn.'%')
->and_where('last_name','like','%'.$ln.'%')
->where_close()
->or_where('company','like','%'.$term.'%');
} elseif (is_numeric($term)) {
$this->where('id','like','%'.$term.'%');
} elseif (preg_match('/\@/',$term)) {
$this->where('email','like','%'.$term.'%');
$value = 'email';
} else {
$this->where_open()
->where('company','like','%'.$term.'%')
2011-12-20 05:46:10 +00:00
->or_where('first_name','like','%'.$term.'%')
->or_where('last_name','like','%'.$term.'%')
->or_where('email','like','%'.$term.'%')
->where_close();
}
foreach ($limit as $w) {
list($k,$s,$v) = $w;
$this->and_where($k,$s,$v);
2011-12-20 05:46:10 +00:00
}
// Restrict results to authorised accounts
$this->and_where('id','IN',$ao->RTM->customers($ao->RTM));
2011-12-20 05:46:10 +00:00
foreach ($this->find_all() as $o)
$result[$o->$index] = array(
2011-12-20 05:46:10 +00:00
'value'=>$o->$index,
'label'=>sprintf('ACC %s: %s',$o->id,Table::resolve($o,$value)),
);
return $result;
2011-12-20 05:46:10 +00:00
}
2010-11-29 22:41:08 +00:00
}
2011-05-02 12:28:17 +00:00
?>