|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\core\models; |
|
4
|
|
|
|
|
5
|
|
|
use app\core\exceptions\InvalidArgumentException; |
|
6
|
|
|
use app\core\services\RecurrenceService; |
|
7
|
|
|
use app\core\services\TransactionService; |
|
8
|
|
|
use app\core\types\RecurrenceFrequency; |
|
9
|
|
|
use app\core\types\RecurrenceStatus; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\InvalidConfigException; |
|
12
|
|
|
use yii\behaviors\TimestampBehavior; |
|
13
|
|
|
use yiier\helpers\DateHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This is the model class for table "{{%recurrence}}". |
|
17
|
|
|
* |
|
18
|
|
|
* @property int $id |
|
19
|
|
|
* @property int $user_id |
|
20
|
|
|
* @property string $name |
|
21
|
|
|
* @property int $frequency |
|
22
|
|
|
* @property int|null $interval |
|
23
|
|
|
* @property string|null $schedule |
|
24
|
|
|
* @property int $transaction_id |
|
25
|
|
|
* @property string|null $started_at |
|
26
|
|
|
* @property string|null $execution_date |
|
27
|
|
|
* @property int|null $status |
|
28
|
|
|
* @property string|null $created_at |
|
29
|
|
|
* @property string|null $updated_at |
|
30
|
|
|
* |
|
31
|
|
|
* @property-read Transaction $transaction |
|
32
|
|
|
*/ |
|
33
|
|
|
class Recurrence extends \yii\db\ActiveRecord |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function tableName() |
|
39
|
|
|
{ |
|
40
|
|
|
return '{{%recurrence}}'; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
* @throws InvalidConfigException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function behaviors() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
[ |
|
51
|
|
|
'class' => TimestampBehavior::class, |
|
52
|
|
|
'value' => Yii::$app->formatter->asDatetime('now') |
|
53
|
|
|
], |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return bool |
|
59
|
|
|
* @throws InvalidConfigException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function beforeValidate() |
|
62
|
|
|
{ |
|
63
|
|
|
if (parent::beforeValidate()) { |
|
64
|
|
|
$this->execution_date = $this->execution_date ? |
|
65
|
|
|
Yii::$app->formatter->asDatetime($this->execution_date, 'php:Y-m-d') : null; |
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
|
|
public function rules() |
|
75
|
|
|
{ |
|
76
|
|
|
return [ |
|
77
|
|
|
[['name', 'frequency', 'transaction_id'], 'required'], |
|
78
|
|
|
[['user_id', 'interval', 'transaction_id'], 'integer'], |
|
79
|
|
|
['frequency', 'in', 'range' => RecurrenceFrequency::names()], |
|
80
|
|
|
['status', 'in', 'range' => RecurrenceStatus::names()], |
|
81
|
|
|
[['started_at', 'execution_date'], 'datetime', 'format' => 'php:Y-m-d'], |
|
82
|
|
|
[['name'], 'string', 'max' => 255], |
|
83
|
|
|
[ |
|
84
|
|
|
'transaction_id', |
|
85
|
|
|
function ($attribute, $params, $validator) { |
|
|
|
|
|
|
86
|
|
|
try { |
|
87
|
|
|
TransactionService::findCurrentOne($this->$attribute); |
|
88
|
|
|
} catch (\Exception $e) { |
|
89
|
|
|
$this->addError( |
|
90
|
|
|
$attribute, |
|
91
|
|
|
Yii::t('app', 'The {attribute} not found.', ['attribute' => $attribute]) |
|
92
|
|
|
); |
|
93
|
|
|
return null; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
], |
|
97
|
|
|
[ |
|
98
|
|
|
'schedule', |
|
99
|
|
|
'required', |
|
100
|
|
|
'when' => function (self $model) { |
|
101
|
|
|
return in_array( |
|
102
|
|
|
RecurrenceFrequency::toEnumValue($model->frequency), |
|
103
|
|
|
[RecurrenceFrequency::WEEK, RecurrenceFrequency::MONTH, RecurrenceFrequency::YEAR] |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
], |
|
107
|
|
|
[ |
|
108
|
|
|
'schedule', |
|
109
|
|
|
'integer', |
|
110
|
|
|
'min' => 1, |
|
111
|
|
|
'max' => 7, |
|
112
|
|
|
'when' => function (self $model) { |
|
113
|
|
|
return RecurrenceFrequency::toEnumValue($model->frequency) === RecurrenceFrequency::WEEK; |
|
114
|
|
|
} |
|
115
|
|
|
], |
|
116
|
|
|
[ |
|
117
|
|
|
'schedule', |
|
118
|
|
|
'datetime', |
|
119
|
|
|
'format' => 'd', |
|
120
|
|
|
'when' => function (self $model) { |
|
121
|
|
|
return RecurrenceFrequency::toEnumValue($model->frequency) === RecurrenceFrequency::MONTH; |
|
122
|
|
|
} |
|
123
|
|
|
], |
|
124
|
|
|
[ |
|
125
|
|
|
'schedule', |
|
126
|
|
|
'datetime', |
|
127
|
|
|
'format' => 'M-d', |
|
128
|
|
|
'when' => function (self $model) { |
|
129
|
|
|
return RecurrenceFrequency::toEnumValue($model->frequency) === RecurrenceFrequency::YEAR; |
|
130
|
|
|
} |
|
131
|
|
|
], |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
public function attributeLabels() |
|
139
|
|
|
{ |
|
140
|
|
|
return [ |
|
141
|
|
|
'id' => Yii::t('app', 'ID'), |
|
142
|
|
|
'user_id' => Yii::t('app', 'User ID'), |
|
143
|
|
|
'name' => Yii::t('app', 'Name'), |
|
144
|
|
|
'frequency' => Yii::t('app', 'Frequency'), |
|
145
|
|
|
'interval' => Yii::t('app', 'Interval'), |
|
146
|
|
|
'schedule' => Yii::t('app', 'Schedule'), |
|
147
|
|
|
'transaction_id' => Yii::t('app', 'Transaction ID'), |
|
148
|
|
|
'started_at' => Yii::t('app', 'Started At'), |
|
149
|
|
|
'execution_date' => Yii::t('app', 'Execution Date'), |
|
150
|
|
|
'status' => Yii::t('app', 'Status'), |
|
151
|
|
|
'created_at' => Yii::t('app', 'Created At'), |
|
152
|
|
|
'updated_at' => Yii::t('app', 'Updated At'), |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param bool $insert |
|
158
|
|
|
* @return bool |
|
159
|
|
|
* @throws InvalidArgumentException|InvalidConfigException |
|
160
|
|
|
*/ |
|
161
|
|
|
public function beforeSave($insert) |
|
162
|
|
|
{ |
|
163
|
|
|
if (parent::beforeSave($insert)) { |
|
164
|
|
|
if ($insert) { |
|
165
|
|
|
$this->user_id = Yii::$app->user->id; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
$this->started_at = Yii::$app->formatter->asDatetime($this->started_at ?: 'now', 'php:Y-m-d'); |
|
168
|
|
|
$this->frequency = RecurrenceFrequency::toEnumValue($this->frequency); |
|
169
|
|
|
$this->status = is_null($this->status) ? |
|
170
|
|
|
RecurrenceStatus::ACTIVE : RecurrenceStatus::toEnumValue($this->status); |
|
171
|
|
|
$this->execution_date = RecurrenceService::getExecutionDate($this); |
|
172
|
|
|
return true; |
|
173
|
|
|
} else { |
|
174
|
|
|
return false; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function getTransaction() |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->hasOne(Transaction::class, ['id' => 'transaction_id']); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
|
public function fields() |
|
188
|
|
|
{ |
|
189
|
|
|
$fields = parent::fields(); |
|
190
|
|
|
unset($fields['user_id']); |
|
191
|
|
|
|
|
192
|
|
|
$fields['status'] = function (self $model) { |
|
193
|
|
|
return RecurrenceStatus::getName($model->status); |
|
|
|
|
|
|
194
|
|
|
}; |
|
195
|
|
|
|
|
196
|
|
|
$fields['frequency'] = function (self $model) { |
|
197
|
|
|
return RecurrenceFrequency::getName($model->frequency); |
|
198
|
|
|
}; |
|
199
|
|
|
|
|
200
|
|
|
$fields['frequency_text'] = function (self $model) { |
|
201
|
|
|
return data_get(RecurrenceFrequency::texts(), $model->frequency); |
|
202
|
|
|
}; |
|
203
|
|
|
|
|
204
|
|
|
$fields['started_at'] = function (self $model) { |
|
205
|
|
|
return DateHelper::datetimeToIso8601($model->started_at); |
|
|
|
|
|
|
206
|
|
|
}; |
|
207
|
|
|
|
|
208
|
|
|
$fields['execution_date'] = function (self $model) { |
|
209
|
|
|
return DateHelper::datetimeToIso8601($model->execution_date); |
|
|
|
|
|
|
210
|
|
|
}; |
|
211
|
|
|
|
|
212
|
|
|
$fields['created_at'] = function (self $model) { |
|
213
|
|
|
return DateHelper::datetimeToIso8601($model->created_at); |
|
|
|
|
|
|
214
|
|
|
}; |
|
215
|
|
|
|
|
216
|
|
|
$fields['updated_at'] = function (self $model) { |
|
217
|
|
|
return DateHelper::datetimeToIso8601($model->updated_at); |
|
|
|
|
|
|
218
|
|
|
}; |
|
219
|
|
|
|
|
220
|
|
|
return $fields; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.