Completed
Push — master ( c01525...9b654e )
by Alexey
05:15
created

Money::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
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']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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
                foreach ($item->condition->rewards as $reward) {
158
                    if ($reward->block) {
159
                        $reward->checkBlocked();
160
                    }
161
                }
162
            }
163
        }
164
    }
165
166
    public function reward($reward_id, $sums = [], $rootUser = null)
167
    {
168
        $rootUser = $rootUser ? $rootUser : \Users\User::$cur;
169
        $reward = \Money\Reward::get($reward_id);
170
        $reward->checkBlocked();
171
        $reward_count = \Money\Reward\Recive::getCount([ 'where' => [ 'reward_id', $reward_id]]);
172
        if ($reward_count >= $reward->quantity && $reward->quantity) {
173
            return false;
174
        }
175
        $types = $this->getSnippets('rewardType');
176
        $checkers = $this->getSnippets('userActivity');
177
        foreach ($reward->levels(['order' => ['level', 'asc']]) as $level) {
178
            $user = $rootUser;
179
            for ($i = 0; $i < $level->level; $i++) {
180
                $next = $user && $user->parent ? $user->parent : false;
181
                $noActive = $next->blocked;
182
                foreach ($checkers as $checker) {
183
                    if ($noActive) {
184
                        break;
185
                    }
186
                    $noActive = !$checker['checker']($next);
187
                }
188
                if ($next && $next->parent_id && $noActive) {
189
                    foreach ($next->users as $childUser) {
190
                        $childUser->parent_id = $next->parent_id;
191
                        $childUser->save();
192
                    }
193
                    $i--;
194
                    $user = Users\User::get($user->id);
195
                    $rootUser = Users\User::get($rootUser->id);
196
                    continue;
197
                }
198
                if (!$next && $reward->lasthaveall) {
199
                    break;
200
                }
201
                $user = $next;
202
            }
203
            if (!$user) {
204
                continue;
205
            }
206
207
            if ($reward->peruser) {
208
                $recives = \Money\Reward\Recive::getList(['where' => [['user_id', $user->id], ['reward_id', $reward->id]]]);
209
                $amount = 0;
210
                foreach ($recives as $recive) {
211
                    $amount+=$recive->amount;
212
                }
213
                if ($amount >= $reward->peruser) {
214
                    continue;
215
                }
216
            }
217
            $rewardGet = true;
218
            if (!$level->nocondition) {
219
                foreach ($reward->conditions as $condition) {
220
                    if (!$condition->checkComplete($user->id)) {
221
                        $rewardGet = false;
222
                        break;
223
                    }
224
                }
225
                if (!$rewardGet && !$reward->block) {
226
                    continue;
227
                }
228
            }
229
            $recive = new \Money\Reward\Recive();
230
            $recive->reward_id = $reward->id;
231
            $recive->user_id = $user->id;
232
            $recive->amount = 1;
233
            $recive->save();
234
            $count = $types[$level->type]['rewarder']($reward, $sums, $user, $rootUser, $level, $rewardGet);
0 ignored issues
show
Unused Code introduced by
$count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
235
        }
236
    }
237
238
}
239