Merge pull request #1946 from cuneytsenturk/master

Add "From/To Account Rate" fields in transfer create/edit page
This commit is contained in:
Cüneyt Şentürk 2021-03-18 20:02:39 +03:00 committed by GitHub
commit 0b261964e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 149 additions and 15 deletions

View File

@ -53,9 +53,11 @@ class Transfers extends Controller
$payment_methods = Modules::getPaymentMethods(); $payment_methods = Modules::getPaymentMethods();
$currencies = Currency::enabled()->orderBy('name')->get()->makeHidden(['id', 'company_id', 'created_at', 'updated_at', 'deleted_at']);
$currency = Currency::where('code', setting('default.currency'))->first(); $currency = Currency::where('code', setting('default.currency'))->first();
return view('banking.transfers.create', compact('accounts', 'payment_methods', 'currency')); return view('banking.transfers.create', compact('accounts', 'payment_methods', 'currencies', 'currency'));
} }
/** /**
@ -124,7 +126,11 @@ class Transfers extends Controller
public function edit(Transfer $transfer) public function edit(Transfer $transfer)
{ {
$transfer['from_account_id'] = $transfer->expense_transaction->account_id; $transfer['from_account_id'] = $transfer->expense_transaction->account_id;
$transfer['from_currency_code'] = $transfer->expense_transaction->currency_code;
$transfer['from_account_rate'] = $transfer->expense_transaction->currency_rate;
$transfer['to_account_id'] = $transfer->income_transaction->account_id; $transfer['to_account_id'] = $transfer->income_transaction->account_id;
$transfer['to_currency_code'] = $transfer->income_transaction->currency_code;
$transfer['to_account_rate'] = $transfer->income_transaction->currency_rate;
$transfer['transferred_at'] = Date::parse($transfer->expense_transaction->paid_at)->format('Y-m-d'); $transfer['transferred_at'] = Date::parse($transfer->expense_transaction->paid_at)->format('Y-m-d');
$transfer['description'] = $transfer->expense_transaction->description; $transfer['description'] = $transfer->expense_transaction->description;
$transfer['amount'] = $transfer->expense_transaction->amount; $transfer['amount'] = $transfer->expense_transaction->amount;
@ -137,9 +143,11 @@ class Transfers extends Controller
$account = $transfer->expense_transaction->account; $account = $transfer->expense_transaction->account;
$currencies = Currency::enabled()->orderBy('name')->get()->makeHidden(['id', 'company_id', 'created_at', 'updated_at', 'deleted_at']);
$currency = Currency::where('code', $account->currency_code)->first(); $currency = Currency::where('code', $account->currency_code)->first();
return view('banking.transfers.edit', compact('transfer', 'accounts', 'payment_methods', 'currency')); return view('banking.transfers.edit', compact('transfer', 'accounts', 'payment_methods', 'currencies', 'currency'));
} }
/** /**

View File

@ -36,11 +36,11 @@ class CreateTransfer extends Job
public function handle() public function handle()
{ {
\DB::transaction(function () { \DB::transaction(function () {
$expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first(); $expense_currency_code = $this->getCurrencyCode('from');
$income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first(); $income_currency_code = $this->getCurrencyCode('to');
$expense_currency_rate = config('money.' . $expense_currency_code . '.rate'); $expense_currency_rate = $this->getCurrencyRate('from');
$income_currency_rate = config('money.' . $income_currency_code . '.rate'); $income_currency_rate = $this->getCurrencyRate('to');
$expense_transaction = Transaction::create([ $expense_transaction = Transaction::create([
'company_id' => $this->request['company_id'], 'company_id' => $this->request['company_id'],
@ -88,4 +88,26 @@ class CreateTransfer extends Job
return $this->transfer; return $this->transfer;
} }
protected function getCurrencyCode($type)
{
$currency_code = $this->request->get($type . '_account_currency_code');
if (empty($currency_code)) {
$currency_code = Account::where('id', $this->request->get($type . '_account_id'))->pluck('currency_code')->first();
}
return $currency_code;
}
protected function getCurrencyRate($type)
{
$currency_rate = $this->request->get($type . '_account_rate');
if (empty($currency_rate)) {
$currency_rate = config('money.' . $this->getCurrencyCode($type) . '.rate');
}
return $currency_rate;
}
} }

View File

@ -38,11 +38,11 @@ class UpdateTransfer extends Job
public function handle() public function handle()
{ {
\DB::transaction(function () { \DB::transaction(function () {
$expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first(); $expense_currency_code = $this->getCurrencyCode('from');
$income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first(); $income_currency_code = $this->getCurrencyCode('to');
$expense_currency_rate = config('money.' . $expense_currency_code . '.rate'); $expense_currency_rate = $this->getCurrencyRate('from');
$income_currency_rate = config('money.' . $income_currency_code . '.rate'); $income_currency_rate = $this->getCurrencyRate('to');
$expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id); $expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id);
$income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id); $income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id);
@ -93,4 +93,26 @@ class UpdateTransfer extends Job
return $this->transfer; return $this->transfer;
} }
protected function getCurrencyCode($type)
{
$currency_code = $this->request->get($type . '_account_currency_code');
if (empty($currency_code)) {
$currency_code = Account::where('id', $this->request->get($type . '_account_id'))->pluck('currency_code')->first();
}
return $currency_code;
}
protected function getCurrencyRate($type)
{
$currency_rate = $this->request->get($type . '_account_rate');
if (empty($currency_rate)) {
$currency_rate = config('money.' . $this->getCurrencyCode($type) . '.rate');
}
return $currency_rate;
}
} }

View File

@ -28,7 +28,64 @@ const app = new Vue({
data: function () { data: function () {
return { return {
form: new Form('transfer'), form: new Form('transfer'),
bulk_action: new BulkAction('transfers') bulk_action: new BulkAction('transfers'),
show_rate: false,
} }
},
methods: {
async onChangeFromAccount(from_account_id) {
if (!from_account_id) {
return;
}
let from_promise = Promise.resolve(window.axios.get(url + '/banking/accounts/currency', {
params: {
account_id: from_account_id
}
}));
from_promise.then(response => {
this.currency = response.data;
this.form.currency_code = response.data.currency_code;
this.form.currency_rate = response.data.currency_rate;
this.form.from_currency_code = response.data.currency_code;
this.form.from_account_rate = response.data.currency_rate;
})
.catch(error => {
})
.finally(() => {
this.show_rate = false;
if (this.form.to_currency_code && this.form.from_currency_code != this.form.to_currency_code) {
this.show_rate = true;
}
});
},
async onChangeToAccount(to_account_id) {
if (!to_account_id) {
return;
}
let to_promise = Promise.resolve(window.axios.get(url + '/banking/accounts/currency', {
params: {
account_id: to_account_id
}
}));
to_promise.then(response => {
this.form.to_currency_code = response.data.currency_code;
this.form.to_account_rate = response.data.currency_rate;
})
.catch(error => {
})
.finally(() => {
this.show_rate = false;
if (this.form.from_currency_code && this.form.from_currency_code != this.form.to_currency_code) {
this.show_rate = true;
}
});
},
} }
}); });

View File

@ -3,7 +3,9 @@
return [ return [
'from_account' => 'From Account', 'from_account' => 'From Account',
'from_account_rate' => 'From Account Rate',
'to_account' => 'To Account', 'to_account' => 'To Account',
'to_account_rate' => 'To Account Rate',
'messages' => [ 'messages' => [
'delete' => ':from to :to (:amount)', 'delete' => ':from to :to (:amount)',

View File

@ -17,9 +17,19 @@
<div class="card-body"> <div class="card-body">
<div class="row"> <div class="row">
{{ Form::selectGroup('from_account_id', trans('transfers.from_account'), 'university', $accounts, null, ['required' => 'required', 'change' => 'onChangeAccount']) }} {{ Form::selectGroup('from_account_id', trans('transfers.from_account'), 'university', $accounts, null, ['required' => 'required', 'change' => 'onChangeFromAccount']) }}
{{ Form::selectGroup('to_account_id', trans('transfers.to_account'), 'university', $accounts) }} {{ Form::selectGroup('to_account_id', trans('transfers.to_account'), 'university', $accounts, null, ['required' => 'required', 'change' => 'onChangeToAccount']) }}
<div class="d-none w-100" :class="[{'d-flex' : show_rate}]">
{!! Form::hidden('from_currency_code', null, ['id' => 'from_currency_code', 'v-model' => 'form.from_currency_code']) !!}
{{ Form::textGroup('from_account_rate', trans('transfers.from_account_rate'), 'sliders-h', []) }}
{!! Form::hidden('to_currency_code', null, ['id' => 'to_currency_code', 'v-model' => 'form.to_currency_code']) !!}
{{ Form::textGroup('to_account_rate', trans('transfers.to_account_rate'), 'sliders-h', []) }}
</div>
{{ Form::moneyGroup('amount', trans('general.amount'), 'money-bill-alt', ['required' => 'required', 'currency' => $currency, 'dynamic-currency' => 'currency'], 0) }} {{ Form::moneyGroup('amount', trans('general.amount'), 'money-bill-alt', ['required' => 'required', 'currency' => $currency, 'dynamic-currency' => 'currency'], 0) }}
@ -46,5 +56,8 @@
@endsection @endsection
@push('scripts_start') @push('scripts_start')
<script type="text/javascript">
</script>
<script src="{{ asset('public/js/banking/transfers.js?v=' . version('short')) }}"></script> <script src="{{ asset('public/js/banking/transfers.js?v=' . version('short')) }}"></script>
@endpush @endpush

View File

@ -18,9 +18,19 @@
<div class="card-body"> <div class="card-body">
<div class="row"> <div class="row">
{{ Form::selectGroup('from_account_id', trans('transfers.from_account'), 'university', $accounts, $transfer->from_account_id, ['required' => 'required', 'change' => 'onChangeAccount']) }} {{ Form::selectGroup('from_account_id', trans('transfers.from_account'), 'university', $accounts, $transfer->from_account_id, ['required' => 'required', 'change' => 'onChangeFromAccount']) }}
{{ Form::selectGroup('to_account_id', trans('transfers.to_account'), 'university', $accounts, $transfer->to_account_id) }} {{ Form::selectGroup('to_account_id', trans('transfers.to_account'), 'university', $accounts, $transfer->to_account_id, ['required' => 'required', 'change' => 'onChangeToAccount']) }}
<div class="d-none w-100" :class="[{'d-flex' : show_rate}]">
{!! Form::hidden('from_currency_code', $transfer->from_currency_code, ['id' => 'from_currency_code', 'v-model' => 'form.from_currency_code']) !!}
{{ Form::textGroup('from_account_rate', trans('transfers.from_account_rate'), 'sliders-h', [], $transfer->from_account_rate) }}
{!! Form::hidden('to_currency_code', $transfer->to_currency_code, ['id' => 'to_currency_code', 'v-model' => 'form.to_currency_code']) !!}
{{ Form::textGroup('to_account_rate', trans('transfers.to_account_rate'), 'sliders-h', [], $transfer->to_account_rate) }}
</div>
{{ Form::moneyGroup('amount', trans('general.amount'), 'money-bill-alt', ['required' => 'required', 'currency' => $currency, 'dynamic-currency' => 'currency'], $transfer->amount) }} {{ Form::moneyGroup('amount', trans('general.amount'), 'money-bill-alt', ['required' => 'required', 'currency' => $currency, 'dynamic-currency' => 'currency'], $transfer->amount) }}