From a9ab37d4dd1c3411a7c01d08835d5797e7e4d687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eent=C3=BCrk?= Date: Thu, 18 Mar 2021 20:00:55 +0300 Subject: [PATCH] Add "From/To Account Rate" fields in transfer create/edit page --- app/Http/Controllers/Banking/Transfers.php | 12 +++- app/Jobs/Banking/CreateTransfer.php | 30 ++++++++-- app/Jobs/Banking/UpdateTransfer.php | 30 ++++++++-- .../assets/js/views/banking/transfers.js | 59 ++++++++++++++++++- resources/lang/en-GB/transfers.php | 2 + .../views/banking/transfers/create.blade.php | 17 +++++- .../views/banking/transfers/edit.blade.php | 14 ++++- 7 files changed, 149 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Banking/Transfers.php b/app/Http/Controllers/Banking/Transfers.php index ba537734c..fd4a77ea3 100644 --- a/app/Http/Controllers/Banking/Transfers.php +++ b/app/Http/Controllers/Banking/Transfers.php @@ -53,9 +53,11 @@ class Transfers extends Controller $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(); - 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) { $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_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['description'] = $transfer->expense_transaction->description; $transfer['amount'] = $transfer->expense_transaction->amount; @@ -137,9 +143,11 @@ class Transfers extends Controller $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(); - return view('banking.transfers.edit', compact('transfer', 'accounts', 'payment_methods', 'currency')); + return view('banking.transfers.edit', compact('transfer', 'accounts', 'payment_methods', 'currencies', 'currency')); } /** diff --git a/app/Jobs/Banking/CreateTransfer.php b/app/Jobs/Banking/CreateTransfer.php index 301b09b7e..481daad83 100644 --- a/app/Jobs/Banking/CreateTransfer.php +++ b/app/Jobs/Banking/CreateTransfer.php @@ -36,11 +36,11 @@ class CreateTransfer extends Job public function handle() { \DB::transaction(function () { - $expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first(); - $income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first(); + $expense_currency_code = $this->getCurrencyCode('from'); + $income_currency_code = $this->getCurrencyCode('to'); - $expense_currency_rate = config('money.' . $expense_currency_code . '.rate'); - $income_currency_rate = config('money.' . $income_currency_code . '.rate'); + $expense_currency_rate = $this->getCurrencyRate('from'); + $income_currency_rate = $this->getCurrencyRate('to'); $expense_transaction = Transaction::create([ 'company_id' => $this->request['company_id'], @@ -88,4 +88,26 @@ class CreateTransfer extends Job 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; + } } diff --git a/app/Jobs/Banking/UpdateTransfer.php b/app/Jobs/Banking/UpdateTransfer.php index fcbc0a61d..81a11b8ba 100644 --- a/app/Jobs/Banking/UpdateTransfer.php +++ b/app/Jobs/Banking/UpdateTransfer.php @@ -38,11 +38,11 @@ class UpdateTransfer extends Job public function handle() { \DB::transaction(function () { - $expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first(); - $income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first(); + $expense_currency_code = $this->getCurrencyCode('from'); + $income_currency_code = $this->getCurrencyCode('to'); - $expense_currency_rate = config('money.' . $expense_currency_code . '.rate'); - $income_currency_rate = config('money.' . $income_currency_code . '.rate'); + $expense_currency_rate = $this->getCurrencyRate('from'); + $income_currency_rate = $this->getCurrencyRate('to'); $expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id); $income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id); @@ -93,4 +93,26 @@ class UpdateTransfer extends Job 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; + } } diff --git a/resources/assets/js/views/banking/transfers.js b/resources/assets/js/views/banking/transfers.js index 2b101952f..65fb2cf87 100644 --- a/resources/assets/js/views/banking/transfers.js +++ b/resources/assets/js/views/banking/transfers.js @@ -28,7 +28,64 @@ const app = new Vue({ data: function () { return { 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; + } + }); + }, } }); diff --git a/resources/lang/en-GB/transfers.php b/resources/lang/en-GB/transfers.php index f79af31d4..551bcae00 100644 --- a/resources/lang/en-GB/transfers.php +++ b/resources/lang/en-GB/transfers.php @@ -3,7 +3,9 @@ return [ 'from_account' => 'From Account', + 'from_account_rate' => 'From Account Rate', 'to_account' => 'To Account', + 'to_account_rate' => 'To Account Rate', 'messages' => [ 'delete' => ':from to :to (:amount)', diff --git a/resources/views/banking/transfers/create.blade.php b/resources/views/banking/transfers/create.blade.php index bee9f75b2..7e2db5f79 100644 --- a/resources/views/banking/transfers/create.blade.php +++ b/resources/views/banking/transfers/create.blade.php @@ -17,9 +17,19 @@
- {{ 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']) }} + +
+ {!! 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', []) }} +
{{ Form::moneyGroup('amount', trans('general.amount'), 'money-bill-alt', ['required' => 'required', 'currency' => $currency, 'dynamic-currency' => 'currency'], 0) }} @@ -46,5 +56,8 @@ @endsection @push('scripts_start') + + @endpush diff --git a/resources/views/banking/transfers/edit.blade.php b/resources/views/banking/transfers/edit.blade.php index 96250b9a4..934c888da 100644 --- a/resources/views/banking/transfers/edit.blade.php +++ b/resources/views/banking/transfers/edit.blade.php @@ -18,9 +18,19 @@
- {{ 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']) }} + +
+ {!! 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) }} +
{{ Form::moneyGroup('amount', trans('general.amount'), 'money-bill-alt', ['required' => 'required', 'currency' => $currency, 'dynamic-currency' => 'currency'], $transfer->amount) }}