Add "From/To Account Rate" fields in transfer create/edit page

This commit is contained in:
Cüneyt Şentürk
2021-03-18 20:00:55 +03:00
parent 84e0e51620
commit a9ab37d4dd
7 changed files with 149 additions and 15 deletions

View File

@ -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'));
}
/**

View File

@ -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;
}
}

View File

@ -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;
}
}