freenetis-github/application/models/bank_account.php @ 74a7dbca
8baed187 | Michal Kliment | <?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|||
* This file is part of open source system FreenetIS
|
|||
* and it is release under GPLv3 licence.
|
|||
74a7dbca | Michal Kliment | *
|
|
8baed187 | Michal Kliment | * More info about licence can be found:
|
|
* http://www.gnu.org/licenses/gpl-3.0.html
|
|||
74a7dbca | Michal Kliment | *
|
|
8baed187 | Michal Kliment | * More info about project can be found:
|
|
* http://www.freenetis.org/
|
|||
74a7dbca | Michal Kliment | *
|
|
8baed187 | Michal Kliment | */
|
|
/**
|
|||
* Bank accounts
|
|||
74a7dbca | Michal Kliment | *
|
|
8baed187 | Michal Kliment | * @package Model
|
|
74a7dbca | Michal Kliment | *
|
|
8baed187 | Michal Kliment | * @property integer $id
|
|
* @property string $name
|
|||
* @property integer $member_id
|
|||
* @property Member_Model $member
|
|||
* @property integer $account_nr
|
|||
* @property integer $bank_nr
|
|||
* @property string $IBAN
|
|||
* @property string $SWIFT
|
|||
c1bdc1c4 | Michal Kliment | * @property integer $type
|
|
* @property string $settings
|
|||
8baed187 | Michal Kliment | * @property ORM_Iterator $accounts
|
|
*/
|
|||
class Bank_account_Model extends ORM
|
|||
{
|
|||
c1bdc1c4 | Michal Kliment | // type of bank account (bank)
|
|
/** Other (unknown) bank account */
|
|||
const TYPE_OTHER = 0;
|
|||
/* FIO bank account */
|
|||
const TYPE_FIO = 1;
|
|||
/** Unicredit bank account */
|
|||
const TYPE_UNICREDIT = 2;
|
|||
/** Raiffeisenbank */
|
|||
const TYPE_RAIFFEISENBANK = 3;
|
|||
74a7dbca | Michal Kliment | ||
/**
|
|||
c1bdc1c4 | Michal Kliment | * Type message names
|
|
*
|
|||
* @var array
|
|||
*/
|
|||
private static $type_name = array
|
|||
(
|
|||
self::TYPE_OTHER => '',
|
|||
self::TYPE_FIO => 'FIO',
|
|||
self::TYPE_UNICREDIT => 'UniCredit',
|
|||
self::TYPE_RAIFFEISENBANK => 'Raiffeisebank'
|
|||
);
|
|||
74a7dbca | Michal Kliment | ||
c1bdc1c4 | Michal Kliment | // db relations
|
|
8baed187 | Michal Kliment | protected $belongs_to = array('member');
|
|
protected $has_and_belongs_to_many = array('accounts');
|
|||
74a7dbca | Michal Kliment | ||
c1bdc1c4 | Michal Kliment | /**
|
|
* Get settings driver for managing of bank account settings.
|
|||
74a7dbca | Michal Kliment | *
|
|
c1bdc1c4 | Michal Kliment | * @return BankAccountSettings
|
|
* @throws InvalidArgumentException On unsuported type of bank account
|
|||
*/
|
|||
public function get_settings_driver()
|
|||
{
|
|||
$driver = Bank_Account_Settings::factory($this->type);
|
|||
$driver->load_column_data($this->settings);
|
|||
return $driver;
|
|||
}
|
|||
8baed187 | Michal Kliment | ||
/**
|
|||
* Contruct of app, shutdown action logs by default
|
|||
74a7dbca | Michal Kliment | * @param type $id
|
|
8baed187 | Michal Kliment | */
|
|
public function __construct($id = NULL)
|
|||
{
|
|||
parent::__construct($id);
|
|||
// disable action log
|
|||
$this->set_logger(FALSE);
|
|||
}
|
|||
74a7dbca | Michal Kliment | ||
c1bdc1c4 | Michal Kliment | /**
|
|
* Types of bank account (key is type, value is name).
|
|||
74a7dbca | Michal Kliment | *
|
|
c1bdc1c4 | Michal Kliment | * @return array
|
|
*/
|
|||
public static function get_type_names()
|
|||
{
|
|||
return self::$type_name;
|
|||
}
|
|||
74a7dbca | Michal Kliment | ||
c1bdc1c4 | Michal Kliment | /**
|
|
* Type of bank account type name.
|
|||
74a7dbca | Michal Kliment | *
|
|
c1bdc1c4 | Michal Kliment | * @return string|null
|
|
*/
|
|||
public static function get_type_name($type)
|
|||
{
|
|||
if (isset(self::$type_name[$type]))
|
|||
{
|
|||
return self::$type_name[$type];
|
|||
}
|
|||
74a7dbca | Michal Kliment | ||
c1bdc1c4 | Michal Kliment | return NULL;
|
|
}
|
|||
8baed187 | Michal Kliment | ||
/**
|
|||
74a7dbca | Michal Kliment | * Creates bank account.
|
|
*
|
|||
* @author Tomas Dulik, Ondrej Fibich
|
|||
8baed187 | Michal Kliment | * @param string $name - name of the bank account
|
|
* @param integer $account_nr - bank account number
|
|||
74a7dbca | Michal Kliment | * @param integer $bank_nr - bank number
|
|
* @param integer $member_id - id of the owner [optional]
|
|||
8baed187 | Michal Kliment | * @return Bank_account_Model new object containing the new record model
|
|
*/
|
|||
74a7dbca | Michal Kliment | public static function create($name, $account_nr, $bank_nr, $member_id = NULL)
|
|
8baed187 | Michal Kliment | {
|
|
74a7dbca | Michal Kliment | $member_id_int = intval($member_id);
|
|
8baed187 | Michal Kliment | $bank_acc = new Bank_account_model();
|
|
74a7dbca | Michal Kliment | $bank_acc->member_id = ($member_id_int) ? $member_id_int : NULL;
|
|
8baed187 | Michal Kliment | $bank_acc->name = $name;
|
|
$bank_acc->account_nr = $account_nr;
|
|||
74a7dbca | Michal Kliment | $bank_acc->bank_nr = $bank_nr;
|
|
$bank_acc->save_throwable();
|
|||
8baed187 | Michal Kliment | return $bank_acc;
|
|
}
|
|||
74a7dbca | Michal Kliment | ||
8baed187 | Michal Kliment | /**
|
|
* It gets all bank accounts of association.
|
|||
74a7dbca | Michal Kliment | *
|
|
8baed187 | Michal Kliment | * @author Jiri Svitak
|
|
* @return Mysql_Result
|
|||
*/
|
|||
public function get_assoc_bank_accounts()
|
|||
74a7dbca | Michal Kliment | {
|
|
8baed187 | Michal Kliment | return $this->db->query("
|
|
SELECT ba.id, ba.name AS baname, m.name AS mname,
|
|||
c1bdc1c4 | Michal Kliment | CONCAT(ba.account_nr, '/', ba.bank_nr) AS account_number,
|
|
ba.type, ba.settings
|
|||
8baed187 | Michal Kliment | FROM bank_accounts ba
|
|
LEFT JOIN members m ON m.id = ba.member_id
|
|||
WHERE ba.member_id = 1
|
|||
");
|
|||
}
|
|||
/**
|
|||
* It gets all bank accounts except bank accounts of association.
|
|||
* @return Mysql_Result
|
|||
*/
|
|||
public function get_bank_accounts(
|
|||
$limit_from = 0, $limit_results = 20,
|
|||
$order_by = 'id', $order_by_direction = 'asc',
|
|||
c1bdc1c4 | Michal Kliment | $filter_sql = '')
|
|
8baed187 | Michal Kliment | {
|
|
$where = '';
|
|||
// order by direction check
|
|||
if (strtolower($order_by_direction) != 'desc')
|
|||
{
|
|||
$order_by_direction = 'asc';
|
|||
}
|
|||
// filter
|
|||
c1bdc1c4 | Michal Kliment | if (!empty($filter_sql))
|
|
8baed187 | Michal Kliment | {
|
|
7dafd607 | Michal Kliment | $where = "WHERE $filter_sql";
|
|
8baed187 | Michal Kliment | }
|
|
// query
|
|||
return $this->db->query("
|
|||
7dafd607 | Michal Kliment | SELECT ba.* FROM (
|
|
SELECT ba.id, ba.name AS baname, ba.account_nr, ba.bank_nr,
|
|||
m.name AS member_name, ba.member_id, ba.type, ba.settings
|
|||
FROM bank_accounts ba
|
|||
LEFT JOIN members m ON m.id = ba.member_id
|
|||
WHERE (ba.member_id <> 1 OR ba.member_id IS NULL)
|
|||
) ba
|
|||
$where
|
|||
8baed187 | Michal Kliment | ORDER BY ".$this->db->escape_column($order_by)." $order_by_direction
|
|
LIMIT " . intval($limit_from) . ", " . intval($limit_results) . "
|
|||
74a7dbca | Michal Kliment | ");
|
|
8baed187 | Michal Kliment | }
|
|
74a7dbca | Michal Kliment | ||
8baed187 | Michal Kliment | /**
|
|
* It counts bank accounts except bank accounts of association.
|
|||
* @return integer
|
|||
*/
|
|||
c1bdc1c4 | Michal Kliment | public function count_bank_accounts($filter_sql = '')
|
|
8baed187 | Michal Kliment | {
|
|
$where = '';
|
|||
// filter
|
|||
c1bdc1c4 | Michal Kliment | if (!empty($filter_sql))
|
|
8baed187 | Michal Kliment | {
|
|
7dafd607 | Michal Kliment | $where = "WHERE $filter_sql";
|
|
8baed187 | Michal Kliment | }
|
|
// query
|
|||
return $this->db->query("
|
|||
7dafd607 | Michal Kliment | SELECT COUNT(ba.id) AS total FROM (
|
|
SELECT ba.id, ba.name AS baname, ba.account_nr, ba.bank_nr,
|
|||
m.name AS member_name, ba.member_id
|
|||
FROM bank_accounts ba
|
|||
LEFT JOIN members m ON m.id = ba.member_id
|
|||
WHERE (ba.member_id <> 1 OR ba.member_id IS NULL)
|
|||
) ba
|
|||
$where"
|
|||
74a7dbca | Michal Kliment | )->current()->total;
|
|
}
|
|||
8baed187 | Michal Kliment | /**
|
|
* Function gets bank accounts except bank account with origin_id.
|
|||
* @param $origin_id
|
|||
* @return Mysql_Result
|
|||
*/
|
|||
public function get_destination_bank_accounts($origin_id)
|
|||
{
|
|||
return $this->db->query("
|
|||
SELECT *
|
|||
FROM bank_accounts
|
|||
74a7dbca | Michal Kliment | WHERE id <> ?
|
|
8baed187 | Michal Kliment | ", array($origin_id));
|
|
}
|
|||
74a7dbca | Michal Kliment | ||
8baed187 | Michal Kliment | /**
|
|
* @author Tomas Dulik
|
|||
* @param $attribute_id - a value from accounts.account_attribute_id
|
|||
74a7dbca | Michal Kliment | * @return object containing first related account from the account table having the $type
|
|
8baed187 | Michal Kliment | */
|
|
public function get_related_account_by_attribute_id($attribute_id)
|
|||
{
|
|||
if (!$this->id)
|
|||
{
|
|||
return FALSE;
|
|||
}
|
|||
74a7dbca | Michal Kliment | ||
8baed187 | Michal Kliment | $result = $this->db->query("
|
|
SELECT accounts.* FROM accounts
|
|||
74a7dbca | Michal Kliment | JOIN accounts_bank_accounts AS pivot
|
|
8baed187 | Michal Kliment | ON accounts.id=pivot.account_id AND pivot.bank_account_id=?
|
|
AND accounts.account_attribute_id=?
|
|||
", array($this->id, $attribute_id));
|
|||
74a7dbca | Michal Kliment | ||
8baed187 | Michal Kliment | return ($result && $result->count()) ? $result->current() : FALSE;
|
|
}
|
|||
74a7dbca | Michal Kliment | ||
8baed187 | Michal Kliment | }
|