James Cole 2024-06-15 09:38:19 +02:00
parent b2c3ee9779
commit 46b780758e
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Transaction; namespace FireflyIII\Http\Controllers\Transaction;
use Exception;
use FireflyIII\Events\UpdatedTransactionGroup; use FireflyIII\Events\UpdatedTransactionGroup;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Controllers\Controller;
@ -65,7 +64,7 @@ class ConvertController extends Controller
$this->middleware( $this->middleware(
function ($request, $next) { function ($request, $next) {
$this->accountRepository = app(AccountRepositoryInterface::class); $this->accountRepository = app(AccountRepositoryInterface::class);
app('view')->share('title', (string)trans('firefly.transactions')); app('view')->share('title', (string) trans('firefly.transactions'));
app('view')->share('mainTitleIcon', 'fa-exchange'); app('view')->share('mainTitleIcon', 'fa-exchange');
return $next($request); return $next($request);
@ -95,7 +94,7 @@ class ConvertController extends Controller
$groupTitle = $group->title ?? $first->description; $groupTitle = $group->title ?? $first->description;
$groupArray = $transformer->transformObject($group); $groupArray = $transformer->transformObject($group);
$subTitle = (string)trans('firefly.convert_to_'.$destinationType->type, ['description' => $groupTitle]); $subTitle = (string) trans('firefly.convert_to_' . $destinationType->type, ['description' => $groupTitle]);
$subTitleIcon = 'fa-exchange'; $subTitleIcon = 'fa-exchange';
// get a list of asset accounts and liabilities and stuff, in various combinations: // get a list of asset accounts and liabilities and stuff, in various combinations:
@ -111,7 +110,7 @@ class ConvertController extends Controller
if ($sourceType->type === $destinationType->type) { // cannot convert to its own type. if ($sourceType->type === $destinationType->type) { // cannot convert to its own type.
app('log')->debug('This is already a transaction of the expected type..'); app('log')->debug('This is already a transaction of the expected type..');
session()->flash('info', (string)trans('firefly.convert_is_already_type_'.$destinationType->type)); session()->flash('info', (string) trans('firefly.convert_is_already_type_' . $destinationType->type));
return redirect(route('transactions.show', [$group->id])); return redirect(route('transactions.show', [$group->id]));
} }
@ -140,14 +139,13 @@ class ConvertController extends Controller
// make repositories // make repositories
$liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN]; $liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
$accountList = $this->accountRepository $accountList = $this->accountRepository
->getActiveAccountsByType([AccountType::REVENUE, AccountType::CASH, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]) ->getActiveAccountsByType([AccountType::REVENUE, AccountType::CASH, AccountType::LOAN, AccountType::DEBT, AccountType::MORTGAGE]);
;
$grouped = []; $grouped = [];
// group accounts: // group accounts:
/** @var Account $account */ /** @var Account $account */
foreach ($accountList as $account) { foreach ($accountList as $account) {
$role = (string)$this->accountRepository->getMetaValue($account, 'account_role'); $role = (string) $this->accountRepository->getMetaValue($account, 'account_role');
$name = $account->name; $name = $account->name;
if ('' === $role) { if ('' === $role) {
$role = 'no_account_type'; $role = 'no_account_type';
@ -155,7 +153,7 @@ class ConvertController extends Controller
// maybe it's a liability thing: // maybe it's a liability thing:
if (in_array($account->accountType->type, $liabilityTypes, true)) { if (in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'l_'.$account->accountType->type; $role = 'l_' . $account->accountType->type;
} }
if (AccountType::CASH === $account->accountType->type) { if (AccountType::CASH === $account->accountType->type) {
$role = 'cash_account'; $role = 'cash_account';
@ -165,7 +163,7 @@ class ConvertController extends Controller
$role = 'revenue_account'; $role = 'revenue_account';
} }
$key = (string)trans('firefly.opt_group_'.$role); $key = (string) trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $name; $grouped[$key][$account->id] = $name;
} }
@ -184,7 +182,7 @@ class ConvertController extends Controller
// group accounts: // group accounts:
/** @var Account $account */ /** @var Account $account */
foreach ($accountList as $account) { foreach ($accountList as $account) {
$role = (string)$this->accountRepository->getMetaValue($account, 'account_role'); $role = (string) $this->accountRepository->getMetaValue($account, 'account_role');
$name = $account->name; $name = $account->name;
if ('' === $role) { if ('' === $role) {
$role = 'no_account_type'; $role = 'no_account_type';
@ -192,7 +190,7 @@ class ConvertController extends Controller
// maybe it's a liability thing: // maybe it's a liability thing:
if (in_array($account->accountType->type, $liabilityTypes, true)) { if (in_array($account->accountType->type, $liabilityTypes, true)) {
$role = 'l_'.$account->accountType->type; $role = 'l_' . $account->accountType->type;
} }
if (AccountType::CASH === $account->accountType->type) { if (AccountType::CASH === $account->accountType->type) {
$role = 'cash_account'; $role = 'cash_account';
@ -202,7 +200,7 @@ class ConvertController extends Controller
$role = 'expense_account'; $role = 'expense_account';
} }
$key = (string)trans('firefly.opt_group_'.$role); $key = (string) trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $name; $grouped[$key][$account->id] = $name;
} }
@ -224,9 +222,9 @@ class ConvertController extends Controller
foreach ($accountList as $account) { foreach ($accountList as $account) {
$balance = app('steam')->balance($account, today()); $balance = app('steam')->balance($account, today());
$currency = $this->accountRepository->getAccountCurrency($account) ?? $defaultCurrency; $currency = $this->accountRepository->getAccountCurrency($account) ?? $defaultCurrency;
$role = 'l_'.$account->accountType->type; $role = 'l_' . $account->accountType->type;
$key = (string)trans('firefly.opt_group_'.$role); $key = (string) trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $account->name.' ('.app('amount')->formatAnything($currency, $balance, false).')'; $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
} }
return $grouped; return $grouped;
@ -247,13 +245,13 @@ class ConvertController extends Controller
foreach ($accountList as $account) { foreach ($accountList as $account) {
$balance = app('steam')->balance($account, today()); $balance = app('steam')->balance($account, today());
$currency = $this->accountRepository->getAccountCurrency($account) ?? $defaultCurrency; $currency = $this->accountRepository->getAccountCurrency($account) ?? $defaultCurrency;
$role = (string)$this->accountRepository->getMetaValue($account, 'account_role'); $role = (string) $this->accountRepository->getMetaValue($account, 'account_role');
if ('' === $role) { if ('' === $role) {
$role = 'no_account_type'; $role = 'no_account_type';
} }
$key = (string)trans('firefly.opt_group_'.$role); $key = (string) trans('firefly.opt_group_' . $role);
$grouped[$key][$account->id] = $account->name.' ('.app('amount')->formatAnything($currency, $balance, false).')'; $grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
} }
return $grouped; return $grouped;
@ -285,7 +283,7 @@ class ConvertController extends Controller
// correct transfers: // correct transfers:
$group->refresh(); $group->refresh();
session()->flash('success', (string)trans('firefly.converted_to_'.$destinationType->type)); session()->flash('success', (string) trans('firefly.converted_to_' . $destinationType->type));
event(new UpdatedTransactionGroup($group, true, true)); event(new UpdatedTransactionGroup($group, true, true));
return redirect(route('transactions.show', [$group->id])); return redirect(route('transactions.show', [$group->id]));
@ -306,11 +304,11 @@ class ConvertController extends Controller
$destinationId = $data['destination_id'][$journal->id] ?? null; $destinationId = $data['destination_id'][$journal->id] ?? null;
$destinationName = $data['destination_name'][$journal->id] ?? null; $destinationName = $data['destination_name'][$journal->id] ?? null;
// double check its not an empty string. // double check it's not an empty string.
$sourceId = '' === $sourceId || null === $sourceId ? null : (int)$sourceId; $sourceId = '' === $sourceId || null === $sourceId ? null : (int) $sourceId;
$sourceName = '' === $sourceName ? null : (string)$sourceName; $sourceName = '' === $sourceName ? null : (string) $sourceName;
$destinationId = '' === $destinationId || null === $destinationId ? null : (int)$destinationId; $destinationId = '' === $destinationId || null === $destinationId ? null : (int) $destinationId;
$destinationName = '' === $destinationName ? null : (string)$destinationName; $destinationName = '' === $destinationName ? null : (string) $destinationName;
$validSource = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]); $validSource = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
$validDestination = $validator->validateDestination(['id' => $destinationId, 'name' => $destinationName]); $validDestination = $validator->validateDestination(['id' => $destinationId, 'name' => $destinationName]);
@ -331,6 +329,20 @@ class ConvertController extends Controller
'type' => $transactionType->type, 'type' => $transactionType->type,
]; ];
// also set the currency to the currency of the source account, in case you're converting a deposit into a transfer.
if (TransactionType::TRANSFER === $transactionType->type && TransactionType::DEPOSIT === $journal->transactionType->type) {
$source = $this->accountRepository->find((int) $sourceId);
$sourceCurrency = $this->accountRepository->getAccountCurrency($source);
$dest = $this->accountRepository->find((int) $destinationId);
$destCurrency = $this->accountRepository->getAccountCurrency($dest);
if (null !== $sourceCurrency && null !== $destCurrency && $sourceCurrency->code !== $destCurrency->code) {
$update['currency_id'] = $sourceCurrency->id;
$update['foreign_currency_id'] = $destCurrency->id;
$update['foreign_amount'] = '1'; // not the best solution but at this point the amount is hard to get.
}
}
/** @var JournalUpdateService $service */ /** @var JournalUpdateService $service */
$service = app(JournalUpdateService::class); $service = app(JournalUpdateService::class);
$service->setTransactionJournal($journal); $service->setTransactionJournal($journal);