mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
This commit is contained in:
parent
b2c3ee9779
commit
46b780758e
@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Http\Controllers\Transaction;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Events\UpdatedTransactionGroup;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
@ -65,7 +64,7 @@ class ConvertController extends Controller
|
||||
$this->middleware(
|
||||
function ($request, $next) {
|
||||
$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');
|
||||
|
||||
return $next($request);
|
||||
@ -87,16 +86,16 @@ class ConvertController extends Controller
|
||||
}
|
||||
|
||||
/** @var TransactionGroupTransformer $transformer */
|
||||
$transformer = app(TransactionGroupTransformer::class);
|
||||
$transformer = app(TransactionGroupTransformer::class);
|
||||
|
||||
/** @var TransactionJournal $first */
|
||||
$first = $group->transactionJournals()->first();
|
||||
$sourceType = $first->transactionType;
|
||||
$first = $group->transactionJournals()->first();
|
||||
$sourceType = $first->transactionType;
|
||||
|
||||
$groupTitle = $group->title ?? $first->description;
|
||||
$groupArray = $transformer->transformObject($group);
|
||||
$subTitle = (string)trans('firefly.convert_to_'.$destinationType->type, ['description' => $groupTitle]);
|
||||
$subTitleIcon = 'fa-exchange';
|
||||
$groupTitle = $group->title ?? $first->description;
|
||||
$groupArray = $transformer->transformObject($group);
|
||||
$subTitle = (string) trans('firefly.convert_to_' . $destinationType->type, ['description' => $groupTitle]);
|
||||
$subTitleIcon = 'fa-exchange';
|
||||
|
||||
// get a list of asset accounts and liabilities and stuff, in various combinations:
|
||||
$validDepositSources = $this->getValidDepositSources();
|
||||
@ -105,13 +104,13 @@ class ConvertController extends Controller
|
||||
$assets = $this->getAssetAccounts();
|
||||
|
||||
// old input variables:
|
||||
$preFilled = [
|
||||
$preFilled = [
|
||||
'source_name' => old('source_name'),
|
||||
];
|
||||
|
||||
if ($sourceType->type === $destinationType->type) { // cannot convert to its own 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]));
|
||||
}
|
||||
@ -140,22 +139,21 @@ class ConvertController extends Controller
|
||||
// make repositories
|
||||
$liabilityTypes = [AccountType::MORTGAGE, AccountType::DEBT, AccountType::CREDITCARD, AccountType::LOAN];
|
||||
$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 = [];
|
||||
|
||||
// group accounts:
|
||||
/** @var Account $account */
|
||||
foreach ($accountList as $account) {
|
||||
$role = (string)$this->accountRepository->getMetaValue($account, 'account_role');
|
||||
$name = $account->name;
|
||||
$role = (string) $this->accountRepository->getMetaValue($account, 'account_role');
|
||||
$name = $account->name;
|
||||
if ('' === $role) {
|
||||
$role = 'no_account_type';
|
||||
}
|
||||
|
||||
// maybe it's a liability thing:
|
||||
if (in_array($account->accountType->type, $liabilityTypes, true)) {
|
||||
$role = 'l_'.$account->accountType->type;
|
||||
$role = 'l_' . $account->accountType->type;
|
||||
}
|
||||
if (AccountType::CASH === $account->accountType->type) {
|
||||
$role = 'cash_account';
|
||||
@ -165,7 +163,7 @@ class ConvertController extends Controller
|
||||
$role = 'revenue_account';
|
||||
}
|
||||
|
||||
$key = (string)trans('firefly.opt_group_'.$role);
|
||||
$key = (string) trans('firefly.opt_group_' . $role);
|
||||
$grouped[$key][$account->id] = $name;
|
||||
}
|
||||
|
||||
@ -184,15 +182,15 @@ class ConvertController extends Controller
|
||||
// group accounts:
|
||||
/** @var Account $account */
|
||||
foreach ($accountList as $account) {
|
||||
$role = (string)$this->accountRepository->getMetaValue($account, 'account_role');
|
||||
$name = $account->name;
|
||||
$role = (string) $this->accountRepository->getMetaValue($account, 'account_role');
|
||||
$name = $account->name;
|
||||
if ('' === $role) {
|
||||
$role = 'no_account_type';
|
||||
}
|
||||
|
||||
// maybe it's a liability thing:
|
||||
if (in_array($account->accountType->type, $liabilityTypes, true)) {
|
||||
$role = 'l_'.$account->accountType->type;
|
||||
$role = 'l_' . $account->accountType->type;
|
||||
}
|
||||
if (AccountType::CASH === $account->accountType->type) {
|
||||
$role = 'cash_account';
|
||||
@ -202,7 +200,7 @@ class ConvertController extends Controller
|
||||
$role = 'expense_account';
|
||||
}
|
||||
|
||||
$key = (string)trans('firefly.opt_group_'.$role);
|
||||
$key = (string) trans('firefly.opt_group_' . $role);
|
||||
$grouped[$key][$account->id] = $name;
|
||||
}
|
||||
|
||||
@ -224,9 +222,9 @@ class ConvertController extends Controller
|
||||
foreach ($accountList as $account) {
|
||||
$balance = app('steam')->balance($account, today());
|
||||
$currency = $this->accountRepository->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$role = 'l_'.$account->accountType->type;
|
||||
$key = (string)trans('firefly.opt_group_'.$role);
|
||||
$grouped[$key][$account->id] = $account->name.' ('.app('amount')->formatAnything($currency, $balance, false).')';
|
||||
$role = 'l_' . $account->accountType->type;
|
||||
$key = (string) trans('firefly.opt_group_' . $role);
|
||||
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
@ -245,15 +243,15 @@ class ConvertController extends Controller
|
||||
// group accounts:
|
||||
/** @var Account $account */
|
||||
foreach ($accountList as $account) {
|
||||
$balance = app('steam')->balance($account, today());
|
||||
$currency = $this->accountRepository->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$role = (string)$this->accountRepository->getMetaValue($account, 'account_role');
|
||||
$balance = app('steam')->balance($account, today());
|
||||
$currency = $this->accountRepository->getAccountCurrency($account) ?? $defaultCurrency;
|
||||
$role = (string) $this->accountRepository->getMetaValue($account, 'account_role');
|
||||
if ('' === $role) {
|
||||
$role = 'no_account_type';
|
||||
}
|
||||
|
||||
$key = (string)trans('firefly.opt_group_'.$role);
|
||||
$grouped[$key][$account->id] = $account->name.' ('.app('amount')->formatAnything($currency, $balance, false).')';
|
||||
$key = (string) trans('firefly.opt_group_' . $role);
|
||||
$grouped[$key][$account->id] = $account->name . ' (' . app('amount')->formatAnything($currency, $balance, false) . ')';
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
@ -285,7 +283,7 @@ class ConvertController extends Controller
|
||||
// correct transfers:
|
||||
$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));
|
||||
|
||||
return redirect(route('transactions.show', [$group->id]));
|
||||
@ -297,20 +295,20 @@ class ConvertController extends Controller
|
||||
private function convertJournal(TransactionJournal $journal, TransactionType $transactionType, array $data): TransactionJournal
|
||||
{
|
||||
/** @var AccountValidator $validator */
|
||||
$validator = app(AccountValidator::class);
|
||||
$validator = app(AccountValidator::class);
|
||||
$validator->setUser(auth()->user());
|
||||
$validator->setTransactionType($transactionType->type);
|
||||
|
||||
$sourceId = $data['source_id'][$journal->id] ?? null;
|
||||
$sourceName = $data['source_name'][$journal->id] ?? null;
|
||||
$destinationId = $data['destination_id'][$journal->id] ?? null;
|
||||
$destinationName = $data['destination_name'][$journal->id] ?? null;
|
||||
$sourceId = $data['source_id'][$journal->id] ?? null;
|
||||
$sourceName = $data['source_name'][$journal->id] ?? null;
|
||||
$destinationId = $data['destination_id'][$journal->id] ?? null;
|
||||
$destinationName = $data['destination_name'][$journal->id] ?? null;
|
||||
|
||||
// double check its not an empty string.
|
||||
$sourceId = '' === $sourceId || null === $sourceId ? null : (int)$sourceId;
|
||||
$sourceName = '' === $sourceName ? null : (string)$sourceName;
|
||||
$destinationId = '' === $destinationId || null === $destinationId ? null : (int)$destinationId;
|
||||
$destinationName = '' === $destinationName ? null : (string)$destinationName;
|
||||
// double check it's not an empty string.
|
||||
$sourceId = '' === $sourceId || null === $sourceId ? null : (int) $sourceId;
|
||||
$sourceName = '' === $sourceName ? null : (string) $sourceName;
|
||||
$destinationId = '' === $destinationId || null === $destinationId ? null : (int) $destinationId;
|
||||
$destinationName = '' === $destinationName ? null : (string) $destinationName;
|
||||
$validSource = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
|
||||
$validDestination = $validator->validateDestination(['id' => $destinationId, 'name' => $destinationName]);
|
||||
|
||||
@ -323,7 +321,7 @@ class ConvertController extends Controller
|
||||
|
||||
// TODO typeOverrule: the account validator may have another opinion on the transaction type.
|
||||
|
||||
$update = [
|
||||
$update = [
|
||||
'source_id' => $sourceId,
|
||||
'source_name' => $sourceName,
|
||||
'destination_id' => $destinationId,
|
||||
@ -331,8 +329,22 @@ class ConvertController extends Controller
|
||||
'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 */
|
||||
$service = app(JournalUpdateService::class);
|
||||
$service = app(JournalUpdateService::class);
|
||||
$service->setTransactionJournal($journal);
|
||||
$service->setData($update);
|
||||
$service->update();
|
||||
|
Loading…
Reference in New Issue
Block a user