|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Transfer |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
7
|
|
|
* @link http://inji.ru/ |
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Money; |
|
13
|
|
|
|
|
14
|
|
|
class Transfer extends \Model |
|
15
|
|
|
{ |
|
16
|
|
|
public static $cols = [ |
|
17
|
|
|
'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
|
18
|
|
|
'to_user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'toUser'], |
|
19
|
|
|
'currency_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'currency'], |
|
20
|
|
|
'amount' => ['type' => 'decimal'], |
|
21
|
|
|
'code' => ['type' => 'text'], |
|
22
|
|
|
'comment' => ['type' => 'textarea', 'validator' => 'commentClean'], |
|
23
|
|
|
'complete' => ['type' => 'bool'], |
|
24
|
|
|
'canceled' => ['type' => 'bool'], |
|
25
|
|
|
'date_create' => ['type' => 'dateTime'], |
|
26
|
|
|
]; |
|
27
|
|
|
public static $labels = [ |
|
28
|
|
|
'user_id' => 'От кого', |
|
29
|
|
|
'to_user_id' => 'Кому', |
|
30
|
|
|
'currency_id' => 'Валюта', |
|
31
|
|
|
'amount' => 'Сумма', |
|
32
|
|
|
'code' => 'Код подтверждения', |
|
33
|
|
|
'comment' => 'Комментарий', |
|
34
|
|
|
'complete' => 'Завершен', |
|
35
|
|
|
'canceled' => 'Отменен', |
|
36
|
|
|
'date_create' => 'Дата создания', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
public static function itemName($item) |
|
40
|
|
|
{ |
|
41
|
|
|
return $item->pk() . '. ' . $item->name(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static $dataManagers = [ |
|
45
|
|
|
'manager' => [ |
|
46
|
|
|
'preSort' => [ |
|
47
|
|
|
'date_create' => 'desc' |
|
48
|
|
|
], |
|
49
|
|
|
'name' => 'Переводы', |
|
50
|
|
|
'cols' => [ |
|
51
|
|
|
'user_id', 'to_user_id', 'currency_id', 'amount', 'comment', 'complete', 'canceled','date_create' |
|
52
|
|
|
], |
|
53
|
|
|
'actions' => [ |
|
54
|
|
|
'Money\CancelTransfer', 'Money\CompleteTransfer' |
|
55
|
|
|
], |
|
56
|
|
|
'sortable' => [ |
|
57
|
|
|
'user_id', 'to_user_id', 'currency_id', 'amount', 'comment', 'complete', 'canceled','date_create' |
|
58
|
|
|
], |
|
59
|
|
|
'filters' => [ |
|
60
|
|
|
'user_id', 'to_user_id', 'currency_id', 'amount', 'comment', 'complete', 'canceled','date_create' |
|
61
|
|
|
] |
|
62
|
|
|
] |
|
63
|
|
|
]; |
|
64
|
|
|
public static $forms = [ |
|
65
|
|
|
'transfer' => [ |
|
66
|
|
|
'name' => 'Перевод средств', |
|
67
|
|
|
'successText' => 'Операция перевода средств была успешно начата', |
|
68
|
|
|
'inputs' => [ |
|
69
|
|
|
'userSearch' => [ |
|
70
|
|
|
'type' => 'search', |
|
71
|
|
|
'source' => 'relation', |
|
72
|
|
|
'relation' => 'toUser', |
|
73
|
|
|
'showCol' => [ |
|
74
|
|
|
'type' => 'staticMethod', |
|
75
|
|
|
'class' => 'Money\Transfer', |
|
76
|
|
|
'method' => 'itemName', |
|
77
|
|
|
], |
|
78
|
|
|
'label' => 'Получатель', |
|
79
|
|
|
'cols' => [ |
|
80
|
|
|
'info:first_name', |
|
81
|
|
|
'info:last_name', |
|
82
|
|
|
'info:middle_name', |
|
83
|
|
|
'mail' |
|
84
|
|
|
], |
|
85
|
|
|
'col' => 'to_user_id', |
|
86
|
|
|
'required' => true, |
|
87
|
|
|
'validator' => 'userSearch' |
|
88
|
|
|
], |
|
89
|
|
|
'wallets' => [ |
|
90
|
|
|
'type' => 'select', |
|
91
|
|
|
'source' => 'method', |
|
92
|
|
|
'module' => 'Money', |
|
93
|
|
|
'method' => 'getUserWallets', |
|
94
|
|
|
'params' => [null, false, true, true], |
|
95
|
|
|
'label' => 'Кошелек', |
|
96
|
|
|
'col' => 'currency_id', |
|
97
|
|
|
'required' => true |
|
98
|
|
|
], |
|
99
|
|
|
'amount' => [ |
|
100
|
|
|
'type' => 'number', |
|
101
|
|
|
'validator' => 'amount', |
|
102
|
|
|
'required' => true |
|
103
|
|
|
], |
|
104
|
|
|
], |
|
105
|
|
|
'map' => [ |
|
106
|
|
|
['userSearch'], |
|
107
|
|
|
['wallets', 'amount'], |
|
108
|
|
|
['comment'] |
|
109
|
|
|
] |
|
110
|
|
|
] |
|
111
|
|
|
]; |
|
112
|
|
|
|
|
113
|
|
|
public static function validators() |
|
114
|
|
|
{ |
|
115
|
|
|
return [ |
|
116
|
|
|
'userSearch' => function($activeForm, $request) { |
|
117
|
|
|
if (empty($request['userSearch'])) { |
|
118
|
|
|
throw new \Exception('Не указан получатель'); |
|
119
|
|
|
} |
|
120
|
|
|
if (!((int) $request['userSearch'])) { |
|
121
|
|
|
throw new \Exception('Не указан получатель'); |
|
122
|
|
|
} |
|
123
|
|
|
$user = \Users\User::get((int) $request['userSearch']); |
|
124
|
|
|
if (!$user) { |
|
125
|
|
|
throw new \Exception('Такой пользователь не найден'); |
|
126
|
|
|
} |
|
127
|
|
|
if ($user->id == \Users\User::$cur->id) { |
|
128
|
|
|
throw new \Exception('Нельзя выбрать себя в качестве получателя'); |
|
129
|
|
|
} |
|
130
|
|
|
return true; |
|
131
|
|
|
}, |
|
132
|
|
|
'amount' => function($activeForm, $request) { |
|
133
|
|
|
if (empty($request['amount'])) { |
|
134
|
|
|
throw new \Exception('Не указана сумма'); |
|
135
|
|
|
} |
|
136
|
|
|
if (!((float) $request['amount'])) { |
|
137
|
|
|
throw new \Exception('Не указана сумма'); |
|
138
|
|
|
} |
|
139
|
|
|
$amount = (float) $request['amount']; |
|
140
|
|
|
if (empty($request['wallets'])) { |
|
141
|
|
|
throw new \Exception('Не указан кошелек'); |
|
142
|
|
|
} |
|
143
|
|
|
if (!((int) $request['wallets'])) { |
|
144
|
|
|
throw new \Exception('Не указан кошелек'); |
|
145
|
|
|
} |
|
146
|
|
|
$wallets = \App::$cur->money->getUserWallets(); |
|
147
|
|
|
if (empty($wallets[(int) $request['wallets']])) { |
|
148
|
|
|
throw new \Exception('У вас нет такого кошелька'); |
|
149
|
|
|
} |
|
150
|
|
|
$wallet = $wallets[(int) $request['wallets']]; |
|
151
|
|
|
if (!$wallet->currency->transfer) { |
|
152
|
|
|
throw new \Exception('Вы не можете переводить эту валюту'); |
|
153
|
|
|
} |
|
154
|
|
|
if ($wallet->amount < $amount) { |
|
155
|
|
|
throw new \Exception('У вас недостаточно средств на кошельке'); |
|
156
|
|
|
} |
|
157
|
|
|
return true; |
|
158
|
|
|
}, |
|
159
|
|
|
'commentClean' => function($activeForm, &$request) { |
|
160
|
|
|
$request['comment'] = trim(htmlspecialchars(urldecode($request['comment']))); |
|
161
|
|
|
} |
|
162
|
|
|
]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
View Code Duplication |
public static function relations() |
|
166
|
|
|
{ |
|
167
|
|
|
return [ |
|
168
|
|
|
'user' => [ |
|
169
|
|
|
'model' => 'Users\User', |
|
170
|
|
|
'col' => 'user_id' |
|
171
|
|
|
], |
|
172
|
|
|
'toUser' => [ |
|
173
|
|
|
'model' => 'Users\User', |
|
174
|
|
|
'col' => 'to_user_id' |
|
175
|
|
|
], |
|
176
|
|
|
'currency' => [ |
|
177
|
|
|
'model' => 'Money\Currency', |
|
178
|
|
|
'col' => 'currency_id' |
|
179
|
|
|
] |
|
180
|
|
|
]; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function name() |
|
184
|
|
|
{ |
|
185
|
|
|
return 'Перевод на сумму ' . $this->amount . ' ' . $this->currency->name . ' от ' . $this->user->name() . ' для ' . $this->toUser->name(); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function cancel() |
|
189
|
|
|
{ |
|
190
|
|
|
if ($this->canceled || $this->complete) { |
|
191
|
|
|
return false; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$this->canceled = 1; |
|
195
|
|
|
$block = \Money\Wallet\Block::get('Money\Transfer:' . $this->id, 'data'); |
|
196
|
|
|
if ($block) { |
|
197
|
|
|
$block->delete(); |
|
198
|
|
|
} |
|
199
|
|
|
$wallets = \App::$cur->money->getUserWallets($this->user_id); |
|
200
|
|
|
$text = 'Отмена перевода средств'; |
|
201
|
|
|
$wallets[$this->currency_id]->diff($this->amount, $text); |
|
202
|
|
|
\App::$cur->users->AddUserActivity($this->user_id, 4, $text . '<br />' . (float) $this->amount . ' ' . $wallets[$this->currency_id]->currency->acronym()); |
|
203
|
|
|
$this->save(); |
|
204
|
|
|
return true; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function complete() |
|
208
|
|
|
{ |
|
209
|
|
|
if ($this->canceled || $this->complete) { |
|
210
|
|
|
return false; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$this->complete = 1; |
|
214
|
|
|
$block = \Money\Wallet\Block::get('Money\Transfer:' . $this->id, 'data'); |
|
215
|
|
|
if ($block) { |
|
216
|
|
|
$block->delete(); |
|
217
|
|
|
} |
|
218
|
|
|
$wallets = \App::$cur->money->getUserWallets($this->to_user_id); |
|
219
|
|
|
$text = 'Перевод средств от ' . $this->user->name() . '.' . ($this->comment ? ' Комментарий:' . $this->comment : ''); |
|
220
|
|
|
$wallets[$this->currency_id]->diff($this->amount, $text); |
|
221
|
|
|
\App::$cur->users->AddUserActivity($this->to_user_id, 4, $text . '<br />' . (float) $this->amount . ' ' . $wallets[$this->currency_id]->currency->acronym()); |
|
222
|
|
|
$this->save(); |
|
223
|
|
|
return true; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
|