🤖 Auto commit for release 'develop' on 2025-05-31

This commit is contained in:
JC5
2025-05-31 07:56:20 +02:00
parent d465b51da8
commit 8bdfdc39cb
14 changed files with 202 additions and 197 deletions

View File

@@ -54,7 +54,7 @@ class UpdateController extends Controller
$this->middleware(
function ($request, $next) {
/** @var User $admin */
$admin = auth()->user();
$admin = auth()->user();
$this->groupRepository = app(TransactionGroupRepositoryInterface::class);
$this->groupRepository->setUser($admin);
@@ -73,47 +73,48 @@ class UpdateController extends Controller
public function update(UpdateRequest $request, TransactionGroup $transactionGroup): JsonResponse
{
app('log')->debug('Now in update routine for transaction group');
$data = $request->getAll();
$oldAmount = $this->groupRepository->getTotalAmount($transactionGroup);
$data = $request->getAll();
$oldAmount = $this->groupRepository->getTotalAmount($transactionGroup);
$transactionGroup = $this->groupRepository->update($transactionGroup, $data);
$newAmount = $this->groupRepository->getTotalAmount($transactionGroup);
$manager = $this->getManager();
$newAmount = $this->groupRepository->getTotalAmount($transactionGroup);
$manager = $this->getManager();
Log::debug(sprintf('Old amount: %s, new amount: %s', $oldAmount, $newAmount));
app('preferences')->mark();
$applyRules = $data['apply_rules'] ?? true;
$fireWebhooks = $data['fire_webhooks'] ?? true;
$amountChanged = 0 !== bccomp($oldAmount, $newAmount);
$applyRules = $data['apply_rules'] ?? true;
$fireWebhooks = $data['fire_webhooks'] ?? true;
$amountChanged = 0 !== bccomp($oldAmount, $newAmount);
event(new UpdatedTransactionGroup($transactionGroup, $applyRules, $fireWebhooks, $amountChanged));
/** @var User $admin */
$admin = auth()->user();
$admin = auth()->user();
// use new group collector:
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$collector = app(GroupCollectorInterface::class);
$collector
->setUser($admin)
// filter on transaction group.
->setTransactionGroup($transactionGroup)
// all info needed for the API:
->withAPIInformation();
->withAPIInformation()
;
$selectedGroup = $collector->getGroups()->first();
$selectedGroup = $collector->getGroups()->first();
if (null === $selectedGroup) {
throw new NotFoundHttpException();
}
// enrich
$enrichment = new TransactionGroupEnrichment();
$enrichment = new TransactionGroupEnrichment();
$enrichment->setUser($admin);
$selectedGroup = $enrichment->enrichSingle($selectedGroup);
$selectedGroup = $enrichment->enrichSingle($selectedGroup);
/** @var TransactionGroupTransformer $transformer */
$transformer = app(TransactionGroupTransformer::class);
$transformer = app(TransactionGroupTransformer::class);
$transformer->setParameters($this->parameters);
$resource = new Item($selectedGroup, $transformer, 'transactions');
$resource = new Item($selectedGroup, $transformer, 'transactions');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE);
}

View File

@@ -69,7 +69,7 @@ class UpdateController extends Controller
$transactionGroup = $this->groupRepository->update($transactionGroup, $data);
$applyRules = $data['apply_rules'] ?? true;
$fireWebhooks = $data['fire_webhooks'] ?? true;
$amountChanged = true;
$amountChanged = true;
event(new UpdatedTransactionGroup($transactionGroup, $applyRules, $fireWebhooks, $amountChanged));
app('preferences')->mark();

View File

@@ -54,8 +54,8 @@ class CorrectsIbans extends Command
{
/** @var Account $account */
foreach ($accounts as $account) {
$iban = (string) $account->iban;
$newIban = app('steam')->filterSpaces($iban);
$iban = (string) $account->iban;
$newIban = app('steam')->filterSpaces($iban);
if ('' !== $iban && $iban !== $newIban) {
$account->iban = $newIban;
$account->save();
@@ -64,8 +64,8 @@ class CorrectsIbans extends Command
}
// same for account number:
$accountNumber = $account->accountMeta->where('name', 'account_number')->first();
if(null !== $accountNumber) {
$number = (string) $accountNumber->value;
if (null !== $accountNumber) {
$number = (string) $accountNumber->value;
$newNumber = app('steam')->filterSpaces($number);
if ('' !== $number && $number !== $newNumber) {
$accountNumber->value = $newNumber;

View File

@@ -42,9 +42,9 @@ class AccountMetaFactory
$entry = $account->accountMeta()->where('name', $field)->first();
// must not be an empty string:
if ('' !== $value) {
if('account_number' === $field) {
if ('account_number' === $field) {
$value = Steam::filterSpaces($value);
$value = trim(str_replace([' ',"\t", "\n", "\r"], '', $value));
$value = trim(str_replace([' ', "\t", "\n", "\r"], '', $value));
}
// if $data has field and $entry is null, create new one:
if (null === $entry) {

View File

@@ -60,19 +60,20 @@ class UpdatedGroupEventHandler
*/
public function unifyAccounts(UpdatedTransactionGroup $updatedGroupEvent): void
{
$group = $updatedGroupEvent->transactionGroup;
$group = $updatedGroupEvent->transactionGroup;
if (1 === $group->transactionJournals->count()) {
return;
}
// first journal:
/** @var null|TransactionJournal $first */
$first = $group->transactionJournals()
$first = $group->transactionJournals()
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->orderBy('transaction_journals.description', 'DESC')
->first();
->first()
;
if (null === $first) {
Log::warning(sprintf('Group #%d has no transaction journals.', $group->id));
@@ -80,24 +81,26 @@ class UpdatedGroupEventHandler
return;
}
$all = $group->transactionJournals()->get()->pluck('id')->toArray();
$all = $group->transactionJournals()->get()->pluck('id')->toArray();
/** @var Account $sourceAccount */
$sourceAccount = $first->transactions()->where('amount', '<', '0')->first()->account;
/** @var Account $destAccount */
$destAccount = $first->transactions()->where('amount', '>', '0')->first()->account;
$destAccount = $first->transactions()->where('amount', '>', '0')->first()->account;
$type = $first->transactionType->type;
$type = $first->transactionType->type;
if (TransactionTypeEnum::TRANSFER->value === $type || TransactionTypeEnum::WITHDRAWAL->value === $type) {
// set all source transactions to source account:
Transaction::whereIn('transaction_journal_id', $all)
->where('amount', '<', 0)->update(['account_id' => $sourceAccount->id]);
->where('amount', '<', 0)->update(['account_id' => $sourceAccount->id])
;
}
if (TransactionTypeEnum::TRANSFER->value === $type || TransactionTypeEnum::DEPOSIT->value === $type) {
// set all destination transactions to destination account:
Transaction::whereIn('transaction_journal_id', $all)
->where('amount', '>', 0)->update(['account_id' => $destAccount->id]);
->where('amount', '>', 0)->update(['account_id' => $destAccount->id])
;
}
}
@@ -112,24 +115,24 @@ class UpdatedGroupEventHandler
return;
}
$journals = $updatedGroupEvent->transactionGroup->transactionJournals;
$array = [];
$journals = $updatedGroupEvent->transactionGroup->transactionJournals;
$array = [];
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$array[] = $journal->id;
}
$journalIds = implode(',', $array);
$journalIds = implode(',', $array);
Log::debug(sprintf('Add local operator for journal(s): %s', $journalIds));
// collect rules:
$ruleGroupRepository = app(RuleGroupRepositoryInterface::class);
$ruleGroupRepository->setUser($updatedGroupEvent->transactionGroup->user);
$groups = $ruleGroupRepository->getRuleGroupsWithRules('update-journal');
$groups = $ruleGroupRepository->getRuleGroupsWithRules('update-journal');
// file rule engine.
$newRuleEngine = app(RuleEngineInterface::class);
$newRuleEngine = app(RuleEngineInterface::class);
$newRuleEngine->setUser($updatedGroupEvent->transactionGroup->user);
$newRuleEngine->addOperator(['type' => 'journal_id', 'value' => $journalIds]);
$newRuleEngine->setRuleGroups($groups);
@@ -138,7 +141,7 @@ class UpdatedGroupEventHandler
private function recalculateCredit(UpdatedTransactionGroup $event): void
{
$group = $event->transactionGroup;
$group = $event->transactionGroup;
/** @var CreditRecalculateService $object */
$object = app(CreditRecalculateService::class);
@@ -149,13 +152,13 @@ class UpdatedGroupEventHandler
private function triggerWebhooks(UpdatedTransactionGroup $updatedGroupEvent): void
{
Log::debug(__METHOD__);
$group = $updatedGroupEvent->transactionGroup;
$group = $updatedGroupEvent->transactionGroup;
if (false === $updatedGroupEvent->fireWebhooks) {
Log::info(sprintf('Will not fire webhooks for transaction group #%d', $group->id));
return;
}
$user = $group->user;
$user = $group->user;
/** @var MessageGeneratorInterface $engine */
$engine = app(MessageGeneratorInterface::class);

View File

@@ -86,7 +86,7 @@ class GroupCollector implements GroupCollectorInterface
$this->hasJoinedAttTables = false;
$this->expandGroupSearch = false;
$this->hasJoinedMetaTables = false;
$this->booleanFields = ['source_balance_dirty','destination_balance_dirty'];
$this->booleanFields = ['source_balance_dirty', 'destination_balance_dirty'];
$this->integerFields = [
'transaction_group_id',
'user_id',

View File

@@ -191,15 +191,15 @@ class MassController extends Controller
*/
private function updateJournal(int $journalId, MassEditJournalRequest $request): void
{
$journal = $this->repository->find($journalId);
$journal = $this->repository->find($journalId);
if (!$journal instanceof TransactionJournal) {
throw new FireflyException(sprintf('Trying to edit non-existent or deleted journal #%d', $journalId));
}
$service = app(JournalUpdateService::class);
$service = app(JournalUpdateService::class);
// for each field, call the update service.
$service->setTransactionJournal($journal);
$data = [
$data = [
'date' => $this->getDateFromRequest($request, $journal->id, 'date'),
'description' => $this->getStringFromRequest($request, $journal->id, 'description'),
'source_id' => $this->getIntFromRequest($request, $journal->id, 'source_id'),

View File

@@ -436,15 +436,17 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface,
public function getTotalAmount(TransactionGroup $group): string
{
$sum = '0';
/** @var TransactionJournal $journal */
foreach($group->transactionJournals as $journal) {
foreach ($group->transactionJournals as $journal) {
/** @var Transaction $transaction */
foreach($journal->transactions as $transaction) {
if(-1 === bccomp('0', (string) $transaction->amount)) {
foreach ($journal->transactions as $transaction) {
if (-1 === bccomp('0', (string) $transaction->amount)) {
$sum = bcadd($sum, $transaction->amount);
}
}
}
return $sum;
}
}

View File

@@ -76,19 +76,19 @@ class JournalUpdateService
*/
public function __construct()
{
$this->destinationAccount = null;
$this->destinationAccount = null;
$this->destinationTransaction = null;
$this->sourceAccount = null;
$this->sourceTransaction = null;
$this->transactionGroup = null;
$this->transactionJournal = null;
$this->billRepository = app(BillRepositoryInterface::class);
$this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->budgetRepository = app(BudgetRepositoryInterface::class);
$this->tagFactory = app(TagFactory::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
$this->metaString = [
$this->sourceAccount = null;
$this->sourceTransaction = null;
$this->transactionGroup = null;
$this->transactionJournal = null;
$this->billRepository = app(BillRepositoryInterface::class);
$this->categoryRepository = app(CategoryRepositoryInterface::class);
$this->budgetRepository = app(BudgetRepositoryInterface::class);
$this->tagFactory = app(TagFactory::class);
$this->accountRepository = app(AccountRepositoryInterface::class);
$this->currencyRepository = app(CurrencyRepositoryInterface::class);
$this->metaString = [
'sepa_cc',
'sepa_ct_op',
'sepa_ct_id',
@@ -103,8 +103,8 @@ class JournalUpdateService
'external_id',
'external_url',
];
$this->metaDate = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date',
'invoice_date',];
$this->metaDate = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date',
'invoice_date', ];
}
public function setData(array $data): void
@@ -114,16 +114,16 @@ class JournalUpdateService
public function setTransactionGroup(TransactionGroup $transactionGroup): void
{
$this->transactionGroup = $transactionGroup;
$this->transactionGroup = $transactionGroup;
$this->billRepository->setUser($transactionGroup->user);
$this->categoryRepository->setUser($transactionGroup->user);
$this->budgetRepository->setUser($transactionGroup->user);
$this->tagFactory->setUser($transactionGroup->user);
$this->accountRepository->setUser($transactionGroup->user);
$this->destinationAccount = null;
$this->destinationAccount = null;
$this->destinationTransaction = null;
$this->sourceAccount = null;
$this->sourceTransaction = null;
$this->sourceAccount = null;
$this->sourceTransaction = null;
}
public function setTransactionJournal(TransactionJournal $transactionJournal): void
@@ -183,14 +183,14 @@ class JournalUpdateService
private function hasValidSourceAccount(): bool
{
$sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null;
$sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null;
Log::debug(sprintf('Now in hasValidSourceAccount("%s","%s").', $sourceId, $sourceName));
if (!$this->hasFields(['source_id', 'source_name'])) {
$origSourceAccount = $this->getOriginalSourceAccount();
$sourceId = $origSourceAccount->id;
$sourceName = $origSourceAccount->name;
$sourceId = $origSourceAccount->id;
$sourceName = $origSourceAccount->name;
}
// make new account validator.
@@ -199,11 +199,11 @@ class JournalUpdateService
// make a new validator.
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$result = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
$result = $validator->validateSource(['id' => $sourceId, 'name' => $sourceName]);
Log::debug(
sprintf('hasValidSourceAccount(%d, "%s") will return %s', $sourceId, $sourceName, var_export($result, true))
);
@@ -228,7 +228,7 @@ class JournalUpdateService
private function getOriginalSourceAccount(): Account
{
if (!$this->sourceAccount instanceof Account) {
$source = $this->getSourceTransaction();
$source = $this->getSourceTransaction();
$this->sourceAccount = $source->account;
}
@@ -239,7 +239,7 @@ class JournalUpdateService
{
if (!$this->sourceTransaction instanceof Transaction) {
/** @var null|Transaction $result */
$result = $this->transactionJournal->transactions()->with(['account'])->where('amount', '<', 0)->first();
$result = $this->transactionJournal->transactions()->with(['account'])->where('amount', '<', 0)->first();
$this->sourceTransaction = $result;
}
Log::debug(sprintf('getSourceTransaction: %s', $this->sourceTransaction->amount));
@@ -267,27 +267,27 @@ class JournalUpdateService
private function hasValidDestinationAccount(): bool
{
Log::debug('Now in hasValidDestinationAccount().');
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
if (!$this->hasFields(['destination_id', 'destination_name'])) {
Log::debug('No destination info submitted, grab the original data.');
$destination = $this->getOriginalDestinationAccount();
$destId = $destination->id;
$destName = $destination->name;
$destId = $destination->id;
$destName = $destination->name;
}
// make new account validator.
$expectedType = $this->getExpectedType();
$expectedType = $this->getExpectedType();
Log::debug(sprintf('(b) Expected type (new or unchanged) is %s', $expectedType));
// make a new validator.
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$validator->source = $this->getValidSourceAccount();
$result = $validator->validateDestination(['id' => $destId, 'name' => $destName]);
$result = $validator->validateDestination(['id' => $destId, 'name' => $destName]);
Log::debug(
sprintf(
'hasValidDestinationAccount(%d, "%s") will return %s',
@@ -306,7 +306,7 @@ class JournalUpdateService
private function getOriginalDestinationAccount(): Account
{
if (!$this->destinationAccount instanceof Account) {
$destination = $this->getDestinationTransaction();
$destination = $this->getDestinationTransaction();
$this->destinationAccount = $destination->account;
}
@@ -320,7 +320,7 @@ class JournalUpdateService
{
if (!$this->destinationTransaction instanceof Transaction) {
/** @var null|Transaction $result */
$result = $this->transactionJournal->transactions()->where('amount', '>', 0)->first();
$result = $this->transactionJournal->transactions()->where('amount', '>', 0)->first();
$this->destinationTransaction = $result;
}
@@ -338,12 +338,12 @@ class JournalUpdateService
return $this->getOriginalSourceAccount();
}
$sourceInfo = [
'id' => (int)($this->data['source_id'] ?? null),
'name' => $this->data['source_name'] ?? null,
'iban' => $this->data['source_iban'] ?? null,
$sourceInfo = [
'id' => (int)($this->data['source_id'] ?? null),
'name' => $this->data['source_name'] ?? null,
'iban' => $this->data['source_iban'] ?? null,
'number' => $this->data['source_number'] ?? null,
'bic' => $this->data['source_bic'] ?? null,
'bic' => $this->data['source_bic'] ?? null,
];
$expectedType = $this->getExpectedType();
@@ -366,8 +366,8 @@ class JournalUpdateService
*/
private function updateAccounts(): void
{
$source = $this->getValidSourceAccount();
$destination = $this->getValidDestinationAccount();
$source = $this->getValidSourceAccount();
$destination = $this->getValidDestinationAccount();
// cowardly refuse to update if both accounts are the same.
if ($source->id === $destination->id) {
@@ -380,7 +380,7 @@ class JournalUpdateService
$origSourceTransaction->account()->associate($source);
$origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction();
$destTransaction = $this->getDestinationTransaction();
$destTransaction->account()->associate($destination);
$destTransaction->save();
@@ -402,12 +402,12 @@ class JournalUpdateService
return $this->getOriginalDestinationAccount();
}
$destInfo = [
'id' => (int)($this->data['destination_id'] ?? null),
'name' => $this->data['destination_name'] ?? null,
'iban' => $this->data['destination_iban'] ?? null,
$destInfo = [
'id' => (int)($this->data['destination_id'] ?? null),
'name' => $this->data['destination_name'] ?? null,
'iban' => $this->data['destination_iban'] ?? null,
'number' => $this->data['destination_number'] ?? null,
'bic' => $this->data['destination_bic'] ?? null,
'bic' => $this->data['destination_bic'] ?? null,
];
// make new account validator.
@@ -431,7 +431,7 @@ class JournalUpdateService
{
Log::debug('Now in updateType()');
if ($this->hasFields(['type'])) {
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
Log::debug(
sprintf(
'Trying to change journal #%d from a %s to a %s.',
@@ -443,7 +443,7 @@ class JournalUpdateService
/** @var TransactionTypeFactory $typeFactory */
$typeFactory = app(TransactionTypeFactory::class);
$result = $typeFactory->find($this->data['type']);
$result = $typeFactory->find($this->data['type']);
if (null !== $result) {
Log::debug('Changed transaction type!');
$this->transactionJournal->transaction_type_id = $result->id;
@@ -464,14 +464,14 @@ class JournalUpdateService
{
$type = $this->transactionJournal->transactionType->type;
if ((
array_key_exists('bill_id', $this->data)
array_key_exists('bill_id', $this->data)
|| array_key_exists('bill_name', $this->data)
)
)
&& TransactionTypeEnum::WITHDRAWAL->value === $type
) {
$billId = (int)($this->data['bill_id'] ?? 0);
$billName = (string)($this->data['bill_name'] ?? '');
$bill = $this->billRepository->findBill($billId, $billName);
$billId = (int)($this->data['bill_id'] ?? 0);
$billName = (string)($this->data['bill_name'] ?? '');
$bill = $this->billRepository->findBill($billId, $billName);
$this->transactionJournal->bill_id = $bill?->id;
Log::debug('Updated bill ID');
}
@@ -483,7 +483,7 @@ class JournalUpdateService
private function updateField(string $fieldName): void
{
if (array_key_exists($fieldName, $this->data) && '' !== (string)$this->data[$fieldName]) {
$value = $this->data[$fieldName];
$value = $this->data[$fieldName];
if ('date' === $fieldName) {
if (!$value instanceof Carbon) {
@@ -588,10 +588,10 @@ class JournalUpdateService
if ($this->hasFields([$field])) {
$value = '' === $this->data[$field] ? null : $this->data[$field];
Log::debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value));
$set = [
$set = [
'journal' => $this->transactionJournal,
'name' => $field,
'data' => $value,
'name' => $field,
'data' => $value,
];
$factory->updateOrCreate($set);
}
@@ -615,15 +615,15 @@ class JournalUpdateService
Log::debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value));
$set = [
'journal' => $this->transactionJournal,
'name' => $field,
'data' => $value,
'name' => $field,
'data' => $value,
];
$factory->updateOrCreate($set);
// also set date with timezone.
$set = [
'journal' => $this->transactionJournal,
'name' => sprintf('%s_tz', $field),
'data' => $value?->format('e'),
'name' => sprintf('%s_tz', $field),
'data' => $value?->format('e'),
];
$factory->updateOrCreate($set);
}
@@ -636,19 +636,19 @@ class JournalUpdateService
if (!$this->hasFields(['currency_id', 'currency_code'])) {
return;
}
$currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
$currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
// update currency everywhere.
$this->transactionJournal->transaction_currency_id = $currency->id;
$this->transactionJournal->save();
$source = $this->getSourceTransaction();
$source->transaction_currency_id = $currency->id;
$source = $this->getSourceTransaction();
$source->transaction_currency_id = $currency->id;
$source->save();
$dest = $this->getDestinationTransaction();
$dest->transaction_currency_id = $currency->id;
$dest = $this->getDestinationTransaction();
$dest->transaction_currency_id = $currency->id;
$dest->save();
// refresh transactions.
@@ -664,7 +664,7 @@ class JournalUpdateService
return;
}
$value = $this->data['amount'] ?? '';
$value = $this->data['amount'] ?? '';
Log::debug(sprintf('Amount is now "%s"', $value));
try {
@@ -674,14 +674,14 @@ class JournalUpdateService
return;
}
$origSourceTransaction = $this->getSourceTransaction();
$this->amountChanged = 0 !== bccomp($origSourceTransaction->amount, app('steam')->negative($amount));
$origSourceTransaction->amount = app('steam')->negative($amount);
$origSourceTransaction = $this->getSourceTransaction();
$this->amountChanged = 0 !== bccomp($origSourceTransaction->amount, app('steam')->negative($amount));
$origSourceTransaction->amount = app('steam')->negative($amount);
$origSourceTransaction->balance_dirty = true;
$origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction();
$destTransaction->amount = app('steam')->positive($amount);
$destTransaction->balance_dirty = true;
$destTransaction = $this->getDestinationTransaction();
$destTransaction->amount = app('steam')->positive($amount);
$destTransaction->balance_dirty = true;
$destTransaction->save();
// refresh transactions.
$this->sourceTransaction->refresh();
@@ -696,15 +696,15 @@ class JournalUpdateService
return;
}
$amount = $this->data['foreign_amount'] ?? null;
$foreignAmount = $this->getForeignAmount($amount);
$source = $this->getSourceTransaction();
$dest = $this->getDestinationTransaction();
$amount = $this->data['foreign_amount'] ?? null;
$foreignAmount = $this->getForeignAmount($amount);
$source = $this->getSourceTransaction();
$dest = $this->getDestinationTransaction();
$foreignCurrency = $source->foreignCurrency;
// find currency in data array
$newForeignId = $this->data['foreign_currency_id'] ?? null;
$newForeignCode = $this->data['foreign_currency_code'] ?? null;
$newForeignId = $this->data['foreign_currency_id'] ?? null;
$newForeignCode = $this->data['foreign_currency_code'] ?? null;
$foreignCurrency = $this->currencyRepository->findCurrencyNull($newForeignId, $newForeignCode)
?? $foreignCurrency;
@@ -718,26 +718,26 @@ class JournalUpdateService
// add foreign currency info to source and destination if possible.
if (null !== $foreignCurrency && null !== $foreignAmount) {
$source->foreign_currency_id = $foreignCurrency->id;
$source->foreign_amount = app('steam')->negative($foreignAmount);
$source->foreign_amount = app('steam')->negative($foreignAmount);
$source->save();
// if the transaction is a TRANSFER, and the foreign amount and currency are set (like they seem to be)
// the correct fields to update in the destination transaction are NOT the foreign amount and currency
// but rather the normal amount and currency. This is new behavior.
$isTransfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;
$isTransfer = TransactionTypeEnum::TRANSFER->value === $this->transactionJournal->transactionType->type;
// also check if it is not between an asset account and a liability, because then the same rule applies.
$isBetween = $this->isBetweenAssetAndLiability();
$isBetween = $this->isBetweenAssetAndLiability();
if ($isTransfer || $isBetween) {
Log::debug('Switch amounts, store in amount and not foreign_amount');
$dest->transaction_currency_id = $foreignCurrency->id;
$dest->amount = app('steam')->positive($foreignAmount);
$dest->foreign_amount = app('steam')->positive($source->amount);
$dest->foreign_currency_id = $source->transaction_currency_id;
$dest->amount = app('steam')->positive($foreignAmount);
$dest->foreign_amount = app('steam')->positive($source->amount);
$dest->foreign_currency_id = $source->transaction_currency_id;
}
if (!$isTransfer && !$isBetween) {
$dest->foreign_currency_id = $foreignCurrency->id;
$dest->foreign_amount = app('steam')->positive($foreignAmount);
$dest->foreign_amount = app('steam')->positive($foreignAmount);
}
$dest->save();
@@ -759,11 +759,11 @@ class JournalUpdateService
}
if ('0' === $amount) {
$source->foreign_currency_id = null;
$source->foreign_amount = null;
$source->foreign_amount = null;
$source->save();
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$dest->save();
Log::debug(sprintf('Foreign amount is "%s" so remove foreign amount info.', $amount));
}
@@ -777,7 +777,7 @@ class JournalUpdateService
private function isBetweenAssetAndLiability(): bool
{
/** @var Transaction $sourceTransaction */
$sourceTransaction = $this->transactionJournal->transactions()->where('amount', '<', 0)->first();
$sourceTransaction = $this->transactionJournal->transactions()->where('amount', '<', 0)->first();
/** @var Transaction $destinationTransaction */
$destinationTransaction = $this->transactionJournal->transactions()->where('amount', '>', 0)->first();
@@ -792,15 +792,15 @@ class JournalUpdateService
return false;
}
$source = $sourceTransaction->account;
$destination = $destinationTransaction->account;
$source = $sourceTransaction->account;
$destination = $destinationTransaction->account;
if (null === $source || null === $destination) {
Log::warning('Either is false, stop.');
return false;
}
$sourceTypes = [AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
$sourceTypes = [AccountTypeEnum::LOAN->value, AccountTypeEnum::DEBT->value, AccountTypeEnum::MORTGAGE->value];
// source is liability, destination is asset
if (in_array($source->accountType->type, $sourceTypes, true) && AccountTypeEnum::ASSET->value === $destination->accountType->type) {
@@ -822,5 +822,4 @@ class JournalUpdateService
{
return $this->amountChanged;
}
}

View File

@@ -66,7 +66,7 @@ class TransactionGroupEnrichment implements EnrichmentInterface
$this->locations = [];
$this->attachmentCount = [];
$this->dateFields = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date'];
$this->nativeCurrency = Amount::getNativeCurrency();
$this->nativeCurrency = Amount::getNativeCurrency();
}
#[Override]
@@ -196,7 +196,7 @@ class TransactionGroupEnrichment implements EnrichmentInterface
$metaData = $this->metaData;
$locations = $this->locations;
$attachmentCount = $this->attachmentCount;
$nativeCurrency = $this->nativeCurrency;
$nativeCurrency = $this->nativeCurrency;
$this->collection = $this->collection->map(function (array $item) use ($nativeCurrency, $notes, $tags, $metaData, $locations, $attachmentCount) {
foreach ($item['transactions'] as $index => $transaction) {

View File

@@ -155,24 +155,24 @@ class TransactionGroupTransformer extends AbstractTransformer
'native_amount' => $transaction['native_amount'] ?? null,
// native currency, always present.
'native_currency_id' => $transaction['native_currency']['id'] ?? null,
'native_currency_code' => $transaction['native_currency']['code'] ?? null,
'native_currency_name' => $transaction['native_currency']['name'] ?? null,
'native_currency_symbol' => $transaction['native_currency']['symbol'] ?? null,
'native_currency_decimal_places' => $transaction['native_currency']['decimal_places'] ?? null,
'native_currency_id' => $transaction['native_currency']['id'] ?? null,
'native_currency_code' => $transaction['native_currency']['code'] ?? null,
'native_currency_name' => $transaction['native_currency']['name'] ?? null,
'native_currency_symbol' => $transaction['native_currency']['symbol'] ?? null,
'native_currency_decimal_places' => $transaction['native_currency']['decimal_places'] ?? null,
// source balance after
'source_balance_after' => $transaction['source_balance_after'] ?? null,
'source_balance_dirty' => $transaction['source_balance_dirty'],
'source_balance_after' => $transaction['source_balance_after'] ?? null,
'source_balance_dirty' => $transaction['source_balance_dirty'],
// destination balance after
'destination_balance_after' => $transaction['destination_balance_after'] ?? null,
'destination_balance_dirty' => $transaction['destination_balance_dirty'],
'destination_balance_after' => $transaction['destination_balance_after'] ?? null,
'destination_balance_dirty' => $transaction['destination_balance_dirty'],
// balance before and after, if not dirty.
//'running_balance_dirty' => $transaction['balance_dirty'] ?? false,
//'running_balance_before' => $transaction['balance_before'] ?? null,
//'running_balance_after' => $transaction['balance_after'] ?? null,
// 'running_balance_dirty' => $transaction['balance_dirty'] ?? false,
// 'running_balance_before' => $transaction['balance_before'] ?? null,
// 'running_balance_after' => $transaction['balance_after'] ?? null,