fitness-web/common/models/Account.php

227 lines
6.2 KiB
PHP

<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
/**
* This is the model class for table "account".
*
* @property integer $id_account
* @property string $name
* @property integer $status
* @property integer $type
* @property string $created_at
* @property string $updated_at
* @property integer $log_card_read_in_reception
*/
class Account extends \yii\db\ActiveRecord
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const TYPE_ALL = 0;
const TYPE_VALUE_HIDDEN = 10;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'account';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
[ 'class' => TimestampBehavior::className(),
'value' => function(){ return date('Y-m-d H:i:s' ); }
]
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'type'], 'required'],
[['name', ], 'unique'],
[['status', 'type','log_card_read_in_reception'], 'integer'],
[['name'], 'string', 'max' => 64]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id_account' => Yii::t('common/account', 'Id Account'),
'name' => Yii::t('common/account', 'Name'),
'status' => Yii::t('common/account', 'Status'),
'type' => Yii::t('common/account', 'Type'),
'created_at' => Yii::t('common/account', 'Created At'),
'updated_at' => Yii::t('common/account', 'Updated At'),
'log_card_read_in_reception' => Yii::t('common/account', 'Log Card Read in Reception'),
];
}
public function getUserAccountAssignments(){
return $this->hasMany(UserAccountAssignment::className(), ['id_account' => 'id_account']);
}
static function statuses() {
return [
self::STATUS_ACTIVE => Yii::t('common/account', 'Active'),
self::STATUS_DELETED => Yii::t('common/account', 'Inactive'),
];
}
public function getStatusHuman(){
$result = null;
$s = self::statuses();
if ( array_key_exists($this->status, $s)){
$result = $s[$this->status];
}
return $result;
}
static function types() {
return [
self::TYPE_ALL => Yii::t('common/account', 'Account'),
self::TYPE_VALUE_HIDDEN => Yii::t('common/account', 'Only the name is visible'),
];
}
public function getTypeHuman(){
$result = null;
$s = self::types();
if ( array_key_exists($this->type, $s)){
$result = $s[$this->type];
}
return $result;
}
public function isInactive(){
return $this->status == self::STATUS_DELETED;
}
/**
* When this account is the current default account,
* and this flag is turned on, then if a card will be
* read in the reception masks , than we will care a doorlog
* into the database.
* Use case is example "Botond", where there is no
* three arm gate, but we want to track the customers.
* @return bool
*/
public function isLogCardReadInReceptionOn(){
return $this->log_card_read_in_reception == 1;
}
/**
* $param int $forceIncludeAccount id account, that should be included in list, even if it is inactive
* @param null $forceIncludeAccount the next account should be included too, even if it is not
* allowed for user
* @return array|null|\yii\db\ActiveRecord[]
*/
public static function readAccounts($forceIncludeAccount = null){
$accounts = null;
if ( $forceIncludeAccount == null) {
$accounts = Account::find()->andWhere(['status' => Account::STATUS_ACTIVE])->all();
}else{
$accounts = Account::find()->andWhere( ['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $forceIncludeAccount ] ])->all();
}
return $accounts;
}
public static function read($forceIncludeAccount = null){
$accounts = null;
$query = Account::find();
$query->innerJoinWith('userAccountAssignments');
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
if ( $forceIncludeAccount == null){
$query->andWhere(['status' => Account::STATUS_ACTIVE])->all();
}else{
$query->andWhere( ['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $forceIncludeAccount ] ])->all();
}
$query->orderBy( ['name' => SORT_ASC]);
$accounts = $query->all();
return $accounts;
}
public static function writeDefault($account){
$session = Yii::$app->session;
$session->set('id_account', $account->id_account);
}
/** read id_transfer from session (default account )
*
* @return int id_transfer
* */
public static function readDefault( ){
$session = Yii::$app->session;
$result = $session->get('id_account');
return $result;
}
/**
* read default transfer object
* return the default account or null, if not found
* @return \common\models\Account
* */
public static function readDefaultObject( ){
$account = null;
$id_account = self::readDefault();
if ( isset($id_account)){
$account = Account::findOne($id_account);
}
return $account;
}
/**
* read assigned account.
* @param $idAccount integer The id of the account to read
* @return array|null|\yii\db\ActiveRecord
*/
public static function readOne($idAccount){
$accounts = null;
$query = Account::find();
$query->innerJoinWith('userAccountAssignments');
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
$query->andWhere(['status' => Account::STATUS_ACTIVE]);
$query->andWhere(['account.id_account' => $idAccount]);
$accounts = $query->one();
return $accounts;
}
public static function toAccaountMap($accounts){
return ArrayHelper::map( $accounts,'id_account','name' );
}
}