Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
8.8 kB
13
Indexable
Never
###################################################################################################
/app/Http/Controllers/ExchangeController.php
###################################################################################################

public function checkAmountValidate($userId, $fromCurrencyId, $toCurrencyId, $amount)
{
    // Lấy đồng tiền mặc định
    $defaultCurrency = Currency::where('id', basicControl()->base_currency)->first();
    if (!$defaultCurrency) {
        return ['status' => false, 'message' => 'Default currency not found'];
    }
    $defaultCurrencyRate = $defaultCurrency->exchange_rate;

    // Lấy ví từ và ví đến
    $fromWallet = Wallet::with('currency')->where(['user_id' => $userId, 'currency_id' => $fromCurrencyId])->first();
    $toWallet = Wallet::with('currency')->where(['user_id' => $userId, 'currency_id' => $toCurrencyId])->first();
    
    // Kiểm tra ví và đồng tiền
    if (!$fromWallet || !$fromWallet->currency) {
        return ['status' => false, 'message' => 'From wallet or its currency not found'];
    }
    if (!$toWallet || !$toWallet->currency) {
        return ['status' => false, 'message' => 'To wallet or its currency not found'];
    }

    $toLimit = $toWallet->currency->currency_type == 0 ? 8 : 4;
    $chargesLimit = ChargesLimit::with('currency')->where([
        'currency_id' => $fromCurrencyId, 
        'transaction_type_id' => config('transactionType.exchange'), 
        'is_active' => 1
    ])->first();

    // Kiểm tra giới hạn phí
    if (!$chargesLimit || !$chargesLimit->currency) {
        $limit = 4; // Giá trị mặc định nếu không có phí
    } else {
        $limit = $chargesLimit->currency->currency_type == 0 ? 8 : 4;
    }

    $amount = getAmount($amount, $limit);
    $status = false;
    $percentage = 0;
    $chargeFixed = 0;
    $chargePercentage = 0;
    $charge = 0;
    $minLimit = 0;
    $maxLimit = 0;

    if ($chargesLimit) {
        $percentage = getAmount($chargesLimit->percentage_charge, $limit);
        $chargeFixed = getAmount($chargesLimit->fixed_charge, $limit);
        $chargePercentage = getAmount(($amount * $percentage) / 100, $limit);
        $charge = getAmount($chargePercentage + $chargeFixed, $limit);
        $minLimit = getAmount($chargesLimit->min_limit, $limit);
        $maxLimit = getAmount($chargesLimit->max_limit, $limit);
    }

    $fromExchangeRate = getAmount($fromWallet->currency->exchange_rate, $limit);
    $toExchangeRate = getAmount($toWallet->currency->exchange_rate, $toLimit);
    $exchangeRate = getAmount(($defaultCurrencyRate / $fromExchangeRate) * $toExchangeRate, $toLimit);

    $transferAmount = getAmount($amount + $charge, $limit);
    $receivedAmount = getAmount($amount * $exchangeRate, $toLimit);

    $fromWalletBalance = getAmount($fromWallet->balance, $limit);
    $fromWalletUpdateBalance = getAmount($fromWalletBalance - $transferAmount, $limit);
    $toWalletUpdateBalance = getAmount($toWallet->balance + $receivedAmount, $toLimit);

    if ($amount < $minLimit || $amount > $maxLimit) {
        $message = "Minimum transfer $minLimit and maximum transfer limit $maxLimit";
    } elseif ($transferAmount > $fromWalletBalance) {
        $message = 'Does not have enough money to cover transfer';
    } else {
        $status = true;
        $message = "Remaining balance : $fromWalletUpdateBalance " . optional($fromWallet->currency)->code;
    }

    $data = [
        'balance' => $fromWalletBalance,
        'user_id' => $userId,
        'from_wallet' => $fromWallet->id,
        'to_wallet' => $toWallet->id,
        'percentage' => $percentage,
        'charge_percentage' => $chargePercentage,
        'charge_fixed' => $chargeFixed,
        'charge' => $charge,
        'exchange_rate' => $exchangeRate,
        'amount' => $amount,
        'transfer_amount' => $transferAmount,
        'received_amount' => $receivedAmount,
        'status' => $status,
        'message' => $message,
        'fromWalletUpdateBalance' => $fromWalletUpdateBalance,
        'toWalletUpdateBalance' => $toWalletUpdateBalance,
        'min_limit' => $minLimit,
        'max_limit' => $maxLimit,
        'currency_limit' => $limit,
    ];

    return $data;
}


###################################################################################################
/resources/views/user/exchange/index.blade.php
###################################################################################################
@extends('user.layouts.master')
@section('page_title',__('Exchange Money'))

@section('content')
<div class="main-content">
	<section class="section">
		<div class="section-header">
			<h1>@lang('Exchange Money')</h1>
			<div class="section-header-breadcrumb">
				<div class="breadcrumb-item active">
					<a href="{{ route('user.dashboard') }}">@lang('Dashboard')</a>
				</div>
				<div class="breadcrumb-item">@lang('Exchange Money')</div>
			</div>
		</div>

		<div class="row mb-3">
			<div class="container-fluid" id="container-wrapper">
				<div class="row">
					<div class="col-lg-12">
						<div class="card mb-4 card-primary shadow-sm">
							<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
								<h6 class="m-0 font-weight-bold text-primary">@lang('Search')</h6>
							</div>
							<div class="card-body">
								<form action="{{ route('exchange.search') }}" method="get">
									@include('user.exchange.searchForm')
								</form>
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-lg-12">
						<div class="card mb-4 card-primary shadow">
							<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
								<h6 class="m-0 font-weight-bold text-primary">@lang('Exchange Money')</h6>
							</div>
							<div class="card-body">
								<div class="table-responsive">
									<table class="table table-striped table-hover align-items-center table-borderless">
										<thead class="thead-light">
										<tr>
											<th>@lang('Exchange')</th>
											<th>@lang('Amount')</th>
											<th>@lang('Charge')</th>
											<th>@lang('Exchange Rate')</th>
											<th>@lang('Exchange Amount')</th>
											<th>@lang('Transaction ID')</th>
											<th>@lang('Status')</th>
											<th>@lang('Created time')</th>
											<th>@lang('Action')</th>
										</tr>
										</thead>
										<tbody>
										@forelse($exchanges as $key => $value)
											<tr>
												<td data-label="@lang('Exchange')">
													{{ __(optional($value->fromWallet)->currency ? $value->fromWallet->currency->code : 'N/A') }}
													<i class="fa fa-exchange-alt text-indigo"></i>
													{{ __(optional($value->toWallet)->currency ? $value->toWallet->currency->code : 'N/A') }}
												</td>
												<td data-label="@lang('Amount')">
													{{ getAmount($value->amount) .' '. __(optional($value->fromWallet)->currency ? $value->fromWallet->currency->code : 'N/A') }}
												</td>
												<td data-label="@lang('Charge')">
													{{ getAmount($value->charge) .' '. __(optional($value->fromWallet)->currency ? $value->fromWallet->currency->code : 'N/A') }}
												</td>
												<td data-label="@lang('Exchange Rate')">
													{{ getAmount($value->exchange_rate) .' '. __(optional($value->toWallet)->currency ? $value->toWallet->currency->code : 'N/A') }}
												</td>
												<td data-label="@lang('Exchange Amount')">
													{{ getAmount($value->received_amount) .' '. __(optional($value->toWallet)->currency ? $value->toWallet->currency->code : 'N/A') }}
												</td>
												<td data-label="@lang('Transaction ID')">{{ __($value->utr) }}</td>
												<td data-label="@lang('Status')">
													@if($value->status)
														<span class="badge badge-info">@lang('Completed')</span>
													@else
														<span class="badge badge-warning">@lang('Pending')</span>
													@endif
												</td>
												<td data-label="@lang('Created time')"> {{ dateTime($value->created_at) }} </td>
												<td data-label="@lang('Action')">
													@if(!$value->status)
														<a href="{{ route('exchange.confirm', $value->utr) }}" target="_blank" class="btn btn-sm btn-primary">@lang('Confirm')</a>
													@endif
												</td>
											</tr>
										@empty
											<tr>
												<th colspan="100%" class="text-center">@lang('No data found')</th>
											</tr>
										@endforelse
										</tbody>
									</table>
								</div>
								<div class="card-footer">
									{{ $exchanges->links() }}
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>

	</section>
</div>
@endsection
Leave a Comment