function __toString() { $string = ''; $first = true; foreach ($this->sums as $currency_id => $sum) { if ($first) { $first = false; } else { $string .= '<br />'; } $string .= '<span style="white-space:nowrap;">'; $string .= number_format($sum, 2, '.', ' '); if (\App::$cur->money) { $currency = \Money\Currency::get($currency_id); if ($currency) { $string .= ' ' . $currency->acronym(); } else { $string .= ' руб.'; } } else { $string .= ' руб.'; } $string .= '</span>'; } return $string; }
public function exchangeAction() { $wallets = $this->module->getUserWallets(); $currency = !empty($_GET['currency_id']) ? \Money\Currency::get((int) $_GET['currency_id']) : null; if ($currency && empty($wallets[$currency->id])) { $currency = null; } $targetCurrency = !empty($_GET['target_currency_id']) ? \Money\Currency::get((int) $_GET['target_currency_id']) : null; if ($targetCurrency && empty($wallets[$targetCurrency->id])) { $targetCurrency = null; } $where = []; if ($currency) { $where[] = ['currency_id', $currency->id]; } else { $where[] = ['currency_id', implode(',', array_keys($wallets)), 'IN']; } if ($targetCurrency) { $where[] = ['target_currency_id', $targetCurrency->id]; } else { $where[] = ['target_currency_id', implode(',', array_keys($wallets)), 'IN']; } if ($where) { $rates = Money\Currency\ExchangeRate::getList(['where' => $where]); } else { $rates = []; } if (!empty($_GET['exchange']) && $currency && $targetCurrency && !empty($rates[$_GET['exchange']['rate_id']])) { $error = false; $rate = $rates[$_GET['exchange']['rate_id']]; if (empty($_GET['exchange']['give']['amount']) || !(double) $_GET['exchange']['give']['amount']) { Msg::add('Укажите сумму которую вы хотите отдать'); $error = true; } else { $amount = (double) $_GET['exchange']['give']['amount']; } if (!empty($amount) && $amount > $wallets[$currency->id]->amount) { Msg::add('Вы указали сумму большую чем вам доступно'); $error = true; } if (!$error) { $wallets[$currency->id]->diff(-$amount, 'Обмен валюты на ' . $targetCurrency->name()); $wallets[$targetCurrency->id]->diff($amount * $rate->rate, 'Обмне валюты с ' . $currency->name()); Tools::redirect('/users/cabinet', 'Обмен был успешно проведен'); } } $this->view->setTitle('Обмен валюты'); $this->view->page(['data' => compact('rates', 'currency', 'targetCurrency', 'wallets')]); }
<?php return ['name' => 'Валюта', 'params' => ['currency_id' => ['type' => 'select'], 'type' => ['type' => 'select', 'source' => 'array', 'sourceArray' => ['procent' => 'Процент', 'amount' => 'Кол-во']], 'amount' => ['type' => 'decimal']], 'viewer' => function ($level) { $levelTypes = ['procent' => 'Процент', 'amount' => 'Сумма']; return $levelTypes[$level->params['type']->value] . ': ' . $level->params['amount']->value . ' ' . ($level->params['type']->value == 'procent' ? '%' : ($level->params['currency_id']->value ? \Money\Currency::get($level->params['currency_id']->value)->acronym() : '')); }, 'rewarder' => function ($reward, $sums, $user, $rootUser, $level, $rewardGet) { $wallets = \App::$cur->money->getUserWallets($user->id); $amount = 0; if (!empty($wallets[$level->params['currency_id']->value])) { switch ($level->params['type']->value) { case 'procent': $finalSum = 0; foreach ($sums as $currency_id => $sum) { if ($currency_id != $level->params['currency_id']->value) { $rate = \Money\Currency\ExchangeRate::get([['currency_id', $currency_id], ['target_currency_id', $level->params['currency_id']->value]]); if ($rate) { $finalSum += $sum * $rate->rate; } } else { $finalSum += $sum; } } switch ($reward->round_type) { case 'round': $finalSum = round($finalSum, $reward->round_precision); $amount = $finalSum / 100 * (double) $level->params['amount']->value; break; case 'floor': $finalSum = floor($finalSum); $amount = $finalSum / 100 * (double) $level->params['amount']->value; break;
public function bonusTrigger($event) { $cart = $event['eventObject']; foreach ($cart->cartItems as $cartItem) { foreach ($cartItem->price->offer->bonuses as $bonus) { if ($bonus->limited && $bonus->left <= 0) { continue; } elseif ($bonus->limited && $bonus->left > 0) { $bonus->left -= 1; $bonus->save(); } switch ($bonus->type) { case 'currency': $currency = \Money\Currency::get($bonus->value); $wallets = App::$cur->money->getUserWallets($cart->user->id); $wallets[$currency->id]->diff($bonus->count, 'Бонус за покупку'); break; } } } return $cart; }