230 lines
7.0 KiB
PHP
230 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace common\models;
|
|
|
|
use Yii;
|
|
use yii\db\QueryBuilder;
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
/**
|
|
* This is the model class for table "account_state".
|
|
*
|
|
* @property integer $id_account_state
|
|
* @property integer $id_account
|
|
* @property integer $type
|
|
* @property integer $money
|
|
* @property integer $banknote_5_ft
|
|
* @property integer $banknote_10_ft
|
|
* @property integer $banknote_20_ft
|
|
* @property integer $banknote_50_ft
|
|
* @property integer $banknote_100_ft
|
|
* @property integer $banknote_200_ft
|
|
* @property integer $banknote_500_ft
|
|
* @property integer $banknote_1000_ft
|
|
* @property integer $banknote_2000_ft
|
|
* @property integer $banknote_5000_ft
|
|
* @property integer $banknote_10000_ft
|
|
* @property integer $banknote_20000_ft
|
|
* @property integer $id_user
|
|
* @property string $created_at
|
|
* @property string $updated_at
|
|
*/
|
|
class AccountState extends \common\models\BaseFitnessActiveRecord
|
|
{
|
|
|
|
const TYPE_OPEN = 10;
|
|
const TYPE_CLOSE = 20;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public static function tableName()
|
|
{
|
|
return 'account_state';
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['id_account','money' ], 'required'],
|
|
[['id_account', 'type', 'money', 'banknote_5_ft', 'banknote_10_ft', 'banknote_20_ft', 'banknote_50_ft', 'banknote_100_ft', 'banknote_200_ft', 'banknote_500_ft', 'banknote_1000_ft', 'banknote_2000_ft', 'banknote_5000_ft', 'banknote_10000_ft', 'banknote_20000_ft' ], 'integer'],
|
|
[['comment' ], 'string' ,'max' => 255],
|
|
[['prev_state' ], 'integer'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'id_account_state' => Yii::t('common/account_state', 'Id Account State'),
|
|
'id_account' => Yii::t('common/account_state', 'Id Account'),
|
|
'accountName' => Yii::t('common/account_state', 'Id Account'),
|
|
'type' => Yii::t('common/account_state', 'Type'),
|
|
'typeName' => Yii::t('common/account_state', 'Type'),
|
|
'money' => Yii::t('common/account_state', 'Money'),
|
|
'banknote_5_ft' => Yii::t('common/account_state', 'Banknote 5 Ft'),
|
|
'banknote_10_ft' => Yii::t('common/account_state', 'Banknote 10 Ft'),
|
|
'banknote_20_ft' => Yii::t('common/account_state', 'Banknote 20 Ft'),
|
|
'banknote_50_ft' => Yii::t('common/account_state', 'Banknote 50 Ft'),
|
|
'banknote_100_ft' => Yii::t('common/account_state', 'Banknote 100 Ft'),
|
|
'banknote_200_ft' => Yii::t('common/account_state', 'Banknote 200 Ft'),
|
|
'banknote_500_ft' => Yii::t('common/account_state', 'Banknote 500 Ft'),
|
|
'banknote_1000_ft' => Yii::t('common/account_state', 'Banknote 1000 Ft'),
|
|
'banknote_2000_ft' => Yii::t('common/account_state', 'Banknote 2000 Ft'),
|
|
'banknote_5000_ft' => Yii::t('common/account_state', 'Banknote 5000 Ft'),
|
|
'banknote_10000_ft' => Yii::t('common/account_state', 'Banknote 10000 Ft'),
|
|
'banknote_20000_ft' => Yii::t('common/account_state', 'Banknote 20000 Ft'),
|
|
'id_user' => Yii::t('common/account_state', 'Id User'),
|
|
'created_at' => Yii::t('common/account_state', 'Created At'),
|
|
'updated_at' => Yii::t('common/account_state', 'Updated At'),
|
|
'comment' => Yii::t('common/account_state', 'Comment'),
|
|
];
|
|
}
|
|
|
|
public static function banknoteValues()
|
|
{
|
|
return [
|
|
'banknote_5_ft' => 5,
|
|
'banknote_10_ft' => 10,
|
|
'banknote_20_ft' => 20,
|
|
'banknote_50_ft' => 50,
|
|
'banknote_100_ft' => 100,
|
|
'banknote_200_ft' => 200,
|
|
'banknote_500_ft' => 500,
|
|
'banknote_1000_ft' => 1000,
|
|
'banknote_2000_ft' => 2000,
|
|
'banknote_5000_ft' => 5000,
|
|
'banknote_10000_ft' => 10000,
|
|
'banknote_20000_ft' => 20000,
|
|
];
|
|
}
|
|
|
|
|
|
public function getAccount(){
|
|
return $this->hasOne( Account::className(), ["id_account" =>"id_account" ] ) ;
|
|
}
|
|
|
|
public function getAccountName(){
|
|
$result = "";
|
|
$account = $this->account;
|
|
if (isset($account)){
|
|
$result = $account->name;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function getUser(){
|
|
return $this->hasOne( User::className(), ["id" =>"id_user" ] );
|
|
}
|
|
|
|
public function getUserName(){
|
|
$result = "";
|
|
$user = $this->user;
|
|
if (isset($this->user)){
|
|
$result = $user->username;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
public function getTypeName(){
|
|
$result = "";
|
|
$type = AccountState::findType($this->type);
|
|
if (isset($type)){
|
|
$result = $type;
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function types(){
|
|
return [
|
|
self::TYPE_OPEN => Yii::t('common/account-state','Open'),
|
|
self::TYPE_CLOSE => Yii::t('common/account-state','Close'),
|
|
];
|
|
}
|
|
|
|
public static function findType($type){
|
|
$result = null;
|
|
$types = self::types();
|
|
if ( array_key_exists($type, $types)){
|
|
$result = $types[ $type ];
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Read last accountstates
|
|
* @param $type int the type of accountstate to load
|
|
* @param $user $id of user to load the account
|
|
* */
|
|
public static function readLast($type, $user = null){
|
|
$result = null;
|
|
$query = AccountState::find();
|
|
//filter user
|
|
if ( isset($user)){
|
|
$query->innerJoinWith("account");
|
|
$query->innerJoinWith("account.userAccountAssignments");
|
|
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
|
|
}
|
|
//filter type
|
|
if ( isset($type)){
|
|
$query->andWhere(['account_state.type' => $type]);
|
|
}
|
|
//filter last
|
|
$query->join("INNER JOIN ",
|
|
"(select max(created_at) as cdate, id_account from account_state group by id_account) as sq1"
|
|
,"sq1.cdate = account_state.created_at and sq1.id_account = account_state.id_account");
|
|
$query->orderBy(['created_at' => SORT_DESC]);
|
|
$result = $query->all();
|
|
return $result;
|
|
}
|
|
|
|
function beforeSave($insert){
|
|
$result = parent::beforeSave($insert);
|
|
if ( $result ){
|
|
$prev = null;
|
|
|
|
if ( isset($this->prev_state) ){
|
|
$prev = AccountState::findOne($this->prev_state);
|
|
}
|
|
|
|
if ( $prev != null){
|
|
$this->prev_money = $prev->money;
|
|
}
|
|
}
|
|
return $result;
|
|
|
|
}
|
|
|
|
public function hasDifferenceToPrevState(){
|
|
$result = false;
|
|
if ( isset( $this->prev_state ) ){
|
|
$result = $this->prev_money != $this->money;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public static function readLastForUser($type){
|
|
return static::readLast($type, Yii::$app->user->id);
|
|
}
|
|
|
|
|
|
public static function modelsToArray($models){
|
|
return ArrayHelper::toArray($models, [
|
|
'common\models\AccountState' => [
|
|
'id_account_state',
|
|
'id_account',
|
|
'money',
|
|
],
|
|
]);
|
|
}
|
|
|
|
}
|