1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Money module |
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
|
|
|
class Money extends Module |
12
|
|
|
{ |
13
|
|
|
public $currentMerchant = ''; |
14
|
|
|
|
15
|
|
|
public function init() |
16
|
|
|
{ |
17
|
|
|
if (!empty($this->config['defaultMerchant'])) { |
18
|
|
|
$this->currentMerchant = $this->config['defaultMerchant']; |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function refillPayRecive($data) |
23
|
|
|
{ |
24
|
|
|
$wallets = $this->getUserWallets($data['pay']->user_id); |
25
|
|
|
foreach ($wallets as $wallet) { |
26
|
|
|
if ($wallet->currency_id == $data['pay']->currency_id) { |
27
|
|
|
$wallet->diff($data['pay']->sum, 'Пополнение'); |
28
|
|
|
break; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function goToMerchant($pay, $merchant, $method, $merchantOptions) |
34
|
|
|
{ |
35
|
|
|
$objectName = $merchant->object_name; |
36
|
|
|
|
37
|
|
|
if (is_array($pay)) { |
38
|
|
|
$pay = new Money\Pay($pay); |
39
|
|
|
$pay->save(); |
40
|
|
|
} |
41
|
|
View Code Duplication |
switch ($method['type']) { |
|
|
|
|
42
|
|
|
case 'transfer': |
43
|
|
|
$sum = $pay->sum / $method['transfer']->rate; |
44
|
|
|
break; |
45
|
|
|
default: |
46
|
|
|
$sum = $pay->sum; |
47
|
|
|
} |
48
|
|
|
$className = 'Money\MerchantHelper\\' . $objectName; |
49
|
|
|
return $className::goToMerchant($pay->id, $sum, $method['currency'], $merchantOptions['description'], $merchantOptions['success'], $merchantOptions['false']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function reciver($data, $system, $status, $mr) |
53
|
|
|
{ |
54
|
|
|
if ($system) { |
55
|
|
|
$merchant = \Money\Merchant::get($system, 'object_name'); |
56
|
|
|
} else { |
57
|
|
|
$merchant = false; |
58
|
|
|
} |
59
|
|
|
if ($merchant) { |
60
|
|
|
$this->currentMerchant = $system; |
61
|
|
|
} |
62
|
|
|
$className = 'Money\MerchantHelper\\' . $this->currentMerchant; |
63
|
|
|
$result = $className::reciver($data, $status); |
64
|
|
|
$result['pay'] = null; |
65
|
|
|
if (!empty($result['payId'])) { |
66
|
|
|
$result['pay'] = Money\Pay::get($result['payId']); |
67
|
|
|
$mr->pay_id = $result['payId']; |
68
|
|
|
} |
69
|
|
|
if ($result['pay'] && $result['pay']->pay_status_id == 1) { |
70
|
|
|
$statuses = \Money\Pay\Status::getList(['key' => 'code']); |
71
|
|
|
if (!empty($statuses[$result['status']])) { |
72
|
|
|
$result['pay']->pay_status_id = $statuses[$result['status']]->id; |
73
|
|
|
} |
74
|
|
|
$result['pay']->date_recive = date('Y-m-d H:i:s'); |
75
|
|
|
$result['pay']->save(); |
76
|
|
|
if ($result['status'] == 'success' && $result['pay']->callback_module && $result['pay']->callback_method) { |
77
|
|
|
App::$cur->{$result['pay']->callback_module}->{$result['pay']->callback_method}($result); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
if (!empty($result['callback'])) { |
81
|
|
|
echo $result['callback']; |
82
|
|
|
$mr->result_callback = $result['callback']; |
83
|
|
|
} |
84
|
|
|
if (!empty($result['status'])) { |
85
|
|
|
$mr->status = $result['status']; |
86
|
|
|
} |
87
|
|
|
$mr->save(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getUserBlocks($userId = null) |
91
|
|
|
{ |
92
|
|
|
$userId = $userId ? $userId : \Users\User::$cur->id; |
93
|
|
|
$blocked = \Money\Wallet\Block::getList(['where' => [ |
94
|
|
|
['wallet:user_id', $userId], |
95
|
|
|
[ |
96
|
|
|
['date_expired', '0000-00-00 00:00:00'], |
97
|
|
|
['date_expired', date('Y-m-d H:i:s'), '>', 'OR'] |
98
|
|
|
] |
99
|
|
|
]]); |
100
|
|
|
$blocks = []; |
101
|
|
|
foreach ($blocked as $block) { |
102
|
|
|
if (empty($blocks[$block->wallet->currency_id])) { |
103
|
|
|
$blocks[$block->wallet->currency_id] = $block->amount; |
104
|
|
|
} else { |
105
|
|
|
$blocks[$block->wallet->currency_id]+= $block->amount; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
return $blocks; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getUserWallets($userId = null, $walletIdasKey = false, $forSelect = false, $transferOnly = false) |
112
|
|
|
{ |
113
|
|
|
$userId = $userId ? $userId : \Users\User::$cur->id; |
114
|
|
|
if (!$userId) { |
115
|
|
|
return []; |
116
|
|
|
} |
117
|
|
|
$this->getUserBlocks($userId); |
118
|
|
|
$where = [['wallet', 1]]; |
119
|
|
|
if ($transferOnly) { |
120
|
|
|
$where[] = ['transfer', 1]; |
121
|
|
|
} |
122
|
|
|
$currencies = Money\Currency::getList(['where' => $where]); |
123
|
|
|
$wallets = Money\Wallet::getList(['where' => ['user_id', $userId], 'key' => 'currency_id']); |
124
|
|
|
$result = []; |
125
|
|
|
foreach ($currencies as $currency) { |
126
|
|
|
if (empty($wallets[$currency->id])) { |
127
|
|
|
$wallet = new Money\Wallet(); |
128
|
|
|
$wallet->user_id = $userId; |
129
|
|
|
$wallet->currency_id = $currency->id; |
130
|
|
|
$wallet->save(); |
131
|
|
|
$result[$walletIdasKey ? $wallet->id : $currency->id] = $forSelect ? $wallet->name() : $wallet; |
132
|
|
|
} else { |
133
|
|
|
$result[$walletIdasKey ? $wallets[$currency->id]->id : $currency->id] = $forSelect ? $wallets[$currency->id]->name() : $wallets[$currency->id]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function rewardTrigger($event) |
140
|
|
|
{ |
141
|
|
|
$triggers = Money\Reward\Trigger::getList(['where' => [['type', 'event'], ['value', $event['eventName']]]]); |
142
|
|
|
foreach ($triggers as $trigger) { |
143
|
|
|
$handlers = $this->getSnippets('rewardTriggerHandler'); |
144
|
|
|
if (!empty($handlers[$trigger->handler])) { |
145
|
|
|
$handlers[$trigger->handler]['handler']($event['eventObject'], $trigger); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function rewardConditionTrigger($event) |
151
|
|
|
{ |
152
|
|
|
$items = Money\Reward\Condition\Item::getList(['where' => [['type', 'event'], ['value', $event['eventName']]]]); |
153
|
|
|
foreach ($items as $item) { |
154
|
|
|
$recivers = $this->getSnippets('rewardConditionItemReciver'); |
155
|
|
|
if (!empty($recivers[$item->reciver])) { |
156
|
|
|
$recivers[$item->reciver]['reciver']($event['eventObject'], $item); |
157
|
|
|
if ($item->condition->reward->block) { |
158
|
|
|
$item->condition->reward->checkBlocked(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function reward($reward_id, $sums = [], $rootUser = null) |
165
|
|
|
{ |
166
|
|
|
$rootUser = $rootUser ? $rootUser : \Users\User::$cur; |
167
|
|
|
$reward = \Money\Reward::get($reward_id); |
168
|
|
|
$reward->checkBlocked(); |
169
|
|
|
$reward_count = \Money\Reward\Recive::getCount([ 'where' => [ 'reward_id', $reward_id ]]); |
170
|
|
|
if ($reward_count >= $reward->quantity && $reward->quantity) |
171
|
|
|
return false; |
172
|
|
|
$types = $this->getSnippets('rewardType'); |
173
|
|
|
foreach ($reward->levels(['order' => ['level', 'asc']]) as $level) { |
174
|
|
|
$user = $rootUser; |
175
|
|
|
for ($i = 0; $i < $level->level; $i++) { |
176
|
|
|
$next = $user && $user->parent ? $user->parent : false; |
177
|
|
|
if (!$next && $reward->lasthaveall) { |
178
|
|
|
break; |
179
|
|
|
} |
180
|
|
|
$user = $next; |
181
|
|
|
} |
182
|
|
|
if (!$user) { |
183
|
|
|
break; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ($reward->peruser) { |
187
|
|
|
$recives = \Money\Reward\Recive::getList(['where' => [['user_id', $user->id], ['reward_id', $reward->id]]]); |
188
|
|
|
$amount = 0; |
189
|
|
|
foreach ($recives as $recive) { |
190
|
|
|
$amount+=$recive->amount; |
191
|
|
|
} |
192
|
|
|
if ($amount >= $reward->peruser) { |
193
|
|
|
continue; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
$rewardGet = true; |
197
|
|
|
foreach ($reward->conditions as $condition) { |
198
|
|
|
if (!$condition->checkComplete($user->id)) { |
199
|
|
|
$rewardGet = false; |
200
|
|
|
break; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
if (!$rewardGet && !$reward->block) { |
204
|
|
|
continue; |
205
|
|
|
} |
206
|
|
|
$recive = new \Money\Reward\Recive(); |
207
|
|
|
$recive->reward_id = $reward->id; |
208
|
|
|
$recive->user_id = $user->id; |
209
|
|
|
$recive->amount = 1; |
210
|
|
|
$recive->save(); |
211
|
|
|
$count = $types[$level->type]['rewarder']($reward, $sums, $user, $rootUser, $level, $rewardGet); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.