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')]); }
<h3>Мои кошельки</h3> <div class="row"> <?php $blocked = App::$cur->money->getUserBlocks(); $wallets = App::$cur->money->getUserWallets(); $rates = Money\Currency\ExchangeRate::getList(); foreach ($wallets as $wallet) { ?> <div class="col-sm-4"> <h4><?php echo $wallet->currency->name(); ?> </h4> <b><?php echo $wallet->showAmount(); ?> </b> <?php echo $wallet->currency->acronym(); ?> <br /> <?php if (!empty($blocked[$wallet->currency_id])) { echo "Заблокировано: {$blocked[$wallet->currency_id]} " . $wallet->currency->acronym() . "<br />"; } if ($wallet->currency->refill) { echo "<a href = '/money/refill?currency_id={$wallet->currency_id}'>Пополнить</a> "; } foreach ($rates as $rate) { if ($rate->currency_id == $wallet->currency_id && !empty($wallets[$rate->target_currency_id])) { echo "<a href = '/money/exchange?currency_id={$wallet->currency_id}'>Обменять</a>"; break;