/
Account.php
126 lines (115 loc) · 3.57 KB
/
Account.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "Account".
*
* @property int $id
* @property string $name
* @property string $description
* @property string $type
* @property string $currency
* @property string $initialBalance
* @property string $initialBalanceDefaultCurrency
* @property string $percent
* @property string $limit
*
* @property string $typeName
* @property string $currentBalance
*
* @property Currency $accountCurrency
*/
class Account extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'Account';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['initialBalance', 'initialBalanceDefaultCurrency', 'percent', 'limit'], 'number'],
[['name'], 'string', 'max' => 255],
[['description'], 'string', 'max' => 1024],
[['type'], 'string', 'max' => 10],
[['currency'], 'string', 'max' => 5],
[['currency'], 'exist', 'skipOnError' => true, 'targetClass' => Currency::className(), 'targetAttribute' => ['currency' => 'code']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Name',
'description' => 'Description',
'type' => 'Type',
'currency' => 'Currency',
'initialBalance' => 'Initial Balance',
'initialBalanceDefaultCurrency' => 'Initial Balance Default Currency',
'percent' => 'Percent',
'limit' => 'Limit',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAccountCurrency()
{
return $this->hasOne(Currency::className(), ['code' => 'currency']);
}
public function getCurrentBalance() {
$sql = "select account_id, sum(amount) as spend
from (
(
select source_account_id as account_id, -1 * source_account_sum as amount
from Transfer
where source_account_id is not null
)
UNION ALL
(
select destination_account_id as account_id, destination_account_sum as amount
from Transfer
where destination_account_id is not null
)
) t
group by account_id";
$connection = Yii::$app->getDb();
$command = $connection->createCommand($sql);
$spends =[];
foreach ($command->queryAll() as $row) {
$spends[$row['account_id']] = floatval($row['spend']);
}
return floatval($this->initialBalance) + (isset($spends[$this->id]) ? $spends[$this->id] : 0);
}
public function getTypeName()
{
switch ($this->type) {
case "cash": return "Наличные";
case "card": return "Карта";
case "account": return "Счет";
case "credit": return "Кредит";
case "creditcard": return "Кредитная карта";
case "deposit": return "Вклад";
case "debt": return "Долг";
case "crypto": return "Криптовалюта";
}
return "Unknown";
}
/**
* @inheritdoc
* @return AccountQuery the active query used by this AR class.
*/
public static function find()
{
return new AccountQuery(get_called_class());
}
}