mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-20 11:19:16 +00:00
Only trigger running balance when amount actually changes.
This commit is contained in:
@@ -34,6 +34,7 @@ use FireflyIII\Support\JsonApi\Enrichments\TransactionGroupEnrichment;
|
|||||||
use FireflyIII\Transformers\TransactionGroupTransformer;
|
use FireflyIII\Transformers\TransactionGroupTransformer;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use League\Fractal\Resource\Item;
|
use League\Fractal\Resource\Item;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
@@ -73,13 +74,18 @@ class UpdateController extends Controller
|
|||||||
{
|
{
|
||||||
app('log')->debug('Now in update routine for transaction group');
|
app('log')->debug('Now in update routine for transaction group');
|
||||||
$data = $request->getAll();
|
$data = $request->getAll();
|
||||||
|
$oldAmount = $this->groupRepository->getTotalAmount($transactionGroup);
|
||||||
$transactionGroup = $this->groupRepository->update($transactionGroup, $data);
|
$transactionGroup = $this->groupRepository->update($transactionGroup, $data);
|
||||||
|
$newAmount = $this->groupRepository->getTotalAmount($transactionGroup);
|
||||||
$manager = $this->getManager();
|
$manager = $this->getManager();
|
||||||
|
|
||||||
|
Log::debug(sprintf('Old amount: %s, new amount: %s', $oldAmount, $newAmount));
|
||||||
|
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
$applyRules = $data['apply_rules'] ?? true;
|
$applyRules = $data['apply_rules'] ?? true;
|
||||||
$fireWebhooks = $data['fire_webhooks'] ?? true;
|
$fireWebhooks = $data['fire_webhooks'] ?? true;
|
||||||
event(new UpdatedTransactionGroup($transactionGroup, $applyRules, $fireWebhooks));
|
$amountChanged = 0 !== bccomp($oldAmount, $newAmount);
|
||||||
|
event(new UpdatedTransactionGroup($transactionGroup, $applyRules, $fireWebhooks, $amountChanged));
|
||||||
|
|
||||||
/** @var User $admin */
|
/** @var User $admin */
|
||||||
$admin = auth()->user();
|
$admin = auth()->user();
|
||||||
@@ -92,8 +98,7 @@ class UpdateController extends Controller
|
|||||||
// filter on transaction group.
|
// filter on transaction group.
|
||||||
->setTransactionGroup($transactionGroup)
|
->setTransactionGroup($transactionGroup)
|
||||||
// all info needed for the API:
|
// all info needed for the API:
|
||||||
->withAPIInformation()
|
->withAPIInformation();
|
||||||
;
|
|
||||||
|
|
||||||
$selectedGroup = $collector->getGroups()->first();
|
$selectedGroup = $collector->getGroups()->first();
|
||||||
if (null === $selectedGroup) {
|
if (null === $selectedGroup) {
|
||||||
|
@@ -69,8 +69,9 @@ class UpdateController extends Controller
|
|||||||
$transactionGroup = $this->groupRepository->update($transactionGroup, $data);
|
$transactionGroup = $this->groupRepository->update($transactionGroup, $data);
|
||||||
$applyRules = $data['apply_rules'] ?? true;
|
$applyRules = $data['apply_rules'] ?? true;
|
||||||
$fireWebhooks = $data['fire_webhooks'] ?? true;
|
$fireWebhooks = $data['fire_webhooks'] ?? true;
|
||||||
|
$amountChanged = true;
|
||||||
|
|
||||||
event(new UpdatedTransactionGroup($transactionGroup, $applyRules, $fireWebhooks));
|
event(new UpdatedTransactionGroup($transactionGroup, $applyRules, $fireWebhooks, $amountChanged));
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
|
|
||||||
/** @var User $admin */
|
/** @var User $admin */
|
||||||
|
@@ -58,7 +58,7 @@ class CorrectsGroupAccounts extends Command
|
|||||||
$handler = new UpdatedGroupEventHandler();
|
$handler = new UpdatedGroupEventHandler();
|
||||||
foreach ($groups as $groupId) {
|
foreach ($groups as $groupId) {
|
||||||
$group = TransactionGroup::find($groupId);
|
$group = TransactionGroup::find($groupId);
|
||||||
$event = new UpdatedTransactionGroup($group, true, true);
|
$event = new UpdatedTransactionGroup($group, true, true, false);
|
||||||
$handler->unifyAccounts($event);
|
$handler->unifyAccounts($event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,5 +37,5 @@ class UpdatedTransactionGroup extends Event
|
|||||||
/**
|
/**
|
||||||
* Create a new event instance.
|
* Create a new event instance.
|
||||||
*/
|
*/
|
||||||
public function __construct(public TransactionGroup $transactionGroup, public bool $applyRules, public bool $fireWebhooks) {}
|
public function __construct(public TransactionGroup $transactionGroup, public bool $applyRules, public bool $fireWebhooks, public bool $amountChanged) {}
|
||||||
}
|
}
|
||||||
|
@@ -49,7 +49,9 @@ class UpdatedGroupEventHandler
|
|||||||
$this->processRules($event);
|
$this->processRules($event);
|
||||||
$this->recalculateCredit($event);
|
$this->recalculateCredit($event);
|
||||||
$this->triggerWebhooks($event);
|
$this->triggerWebhooks($event);
|
||||||
|
if ($event->amountChanged) {
|
||||||
$this->updateRunningBalance($event);
|
$this->updateRunningBalance($event);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,8 +72,7 @@ class UpdatedGroupEventHandler
|
|||||||
->orderBy('transaction_journals.order', 'ASC')
|
->orderBy('transaction_journals.order', 'ASC')
|
||||||
->orderBy('transaction_journals.id', 'DESC')
|
->orderBy('transaction_journals.id', 'DESC')
|
||||||
->orderBy('transaction_journals.description', 'DESC')
|
->orderBy('transaction_journals.description', 'DESC')
|
||||||
->first()
|
->first();
|
||||||
;
|
|
||||||
|
|
||||||
if (null === $first) {
|
if (null === $first) {
|
||||||
Log::warning(sprintf('Group #%d has no transaction journals.', $group->id));
|
Log::warning(sprintf('Group #%d has no transaction journals.', $group->id));
|
||||||
@@ -91,14 +92,12 @@ class UpdatedGroupEventHandler
|
|||||||
if (TransactionTypeEnum::TRANSFER->value === $type || TransactionTypeEnum::WITHDRAWAL->value === $type) {
|
if (TransactionTypeEnum::TRANSFER->value === $type || TransactionTypeEnum::WITHDRAWAL->value === $type) {
|
||||||
// set all source transactions to source account:
|
// set all source transactions to source account:
|
||||||
Transaction::whereIn('transaction_journal_id', $all)
|
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) {
|
if (TransactionTypeEnum::TRANSFER->value === $type || TransactionTypeEnum::DEPOSIT->value === $type) {
|
||||||
// set all destination transactions to destination account:
|
// set all destination transactions to destination account:
|
||||||
Transaction::whereIn('transaction_journal_id', $all)
|
Transaction::whereIn('transaction_journal_id', $all)
|
||||||
->where('amount', '>', 0)->update(['account_id' => $destAccount->id])
|
->where('amount', '>', 0)->update(['account_id' => $destAccount->id]);
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -118,7 +118,7 @@ class BulkController extends Controller
|
|||||||
// run rules on changed journals:
|
// run rules on changed journals:
|
||||||
/** @var TransactionJournal $journal */
|
/** @var TransactionJournal $journal */
|
||||||
foreach ($collection as $journal) {
|
foreach ($collection as $journal) {
|
||||||
event(new UpdatedTransactionGroup($journal->transactionGroup, true, true));
|
event(new UpdatedTransactionGroup($journal->transactionGroup, true, true, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
|
@@ -292,7 +292,7 @@ class ConvertController extends Controller
|
|||||||
$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, false));
|
||||||
|
|
||||||
return redirect(route('transactions.show', [$group->id]));
|
return redirect(route('transactions.show', [$group->id]));
|
||||||
}
|
}
|
||||||
|
@@ -217,7 +217,8 @@ class MassController extends Controller
|
|||||||
$service->setData($data);
|
$service->setData($data);
|
||||||
$service->update();
|
$service->update();
|
||||||
// trigger rules
|
// trigger rules
|
||||||
event(new UpdatedTransactionGroup($journal->transactionGroup, true, true));
|
$amountChanged = $service->isAmountChanged();
|
||||||
|
event(new UpdatedTransactionGroup($journal->transactionGroup, true, true, $amountChanged));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getDateFromRequest(MassEditJournalRequest $request, int $journalId, string $key): ?Carbon
|
private function getDateFromRequest(MassEditJournalRequest $request, int $journalId, string $key): ?Carbon
|
||||||
|
@@ -432,4 +432,19 @@ class TransactionGroupRepository implements TransactionGroupRepositoryInterface,
|
|||||||
|
|
||||||
return $service->update($transactionGroup, $data);
|
return $service->update($transactionGroup, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getTotalAmount(TransactionGroup $group): string
|
||||||
|
{
|
||||||
|
$sum = '0';
|
||||||
|
/** @var TransactionJournal $journal */
|
||||||
|
foreach($group->transactionJournals as $journal) {
|
||||||
|
/** @var Transaction $transaction */
|
||||||
|
foreach($journal->transactions as $transaction) {
|
||||||
|
if(-1 === bccomp('0', (string) $transaction->amount)) {
|
||||||
|
$sum = bcadd($sum, $transaction->amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $sum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -49,6 +49,8 @@ interface TransactionGroupRepositoryInterface
|
|||||||
{
|
{
|
||||||
public function countAttachments(int $journalId): int;
|
public function countAttachments(int $journalId): int;
|
||||||
|
|
||||||
|
public function getTotalAmount(TransactionGroup $group): string;
|
||||||
|
|
||||||
public function destroy(TransactionGroup $group): void;
|
public function destroy(TransactionGroup $group): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -69,6 +69,7 @@ class JournalUpdateService
|
|||||||
private ?Transaction $sourceTransaction;
|
private ?Transaction $sourceTransaction;
|
||||||
private ?TransactionGroup $transactionGroup;
|
private ?TransactionGroup $transactionGroup;
|
||||||
private ?TransactionJournal $transactionJournal;
|
private ?TransactionJournal $transactionJournal;
|
||||||
|
private bool $amountChanged = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JournalUpdateService constructor.
|
* JournalUpdateService constructor.
|
||||||
@@ -103,7 +104,7 @@ class JournalUpdateService
|
|||||||
'external_url',
|
'external_url',
|
||||||
];
|
];
|
||||||
$this->metaDate = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date',
|
$this->metaDate = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date',
|
||||||
'invoice_date', ];
|
'invoice_date',];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setData(array $data): void
|
public function setData(array $data): void
|
||||||
@@ -338,7 +339,7 @@ class JournalUpdateService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$sourceInfo = [
|
$sourceInfo = [
|
||||||
'id' => (int) ($this->data['source_id'] ?? null),
|
'id' => (int)($this->data['source_id'] ?? null),
|
||||||
'name' => $this->data['source_name'] ?? null,
|
'name' => $this->data['source_name'] ?? null,
|
||||||
'iban' => $this->data['source_iban'] ?? null,
|
'iban' => $this->data['source_iban'] ?? null,
|
||||||
'number' => $this->data['source_number'] ?? null,
|
'number' => $this->data['source_number'] ?? null,
|
||||||
@@ -402,7 +403,7 @@ class JournalUpdateService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$destInfo = [
|
$destInfo = [
|
||||||
'id' => (int) ($this->data['destination_id'] ?? null),
|
'id' => (int)($this->data['destination_id'] ?? null),
|
||||||
'name' => $this->data['destination_name'] ?? null,
|
'name' => $this->data['destination_name'] ?? null,
|
||||||
'iban' => $this->data['destination_iban'] ?? null,
|
'iban' => $this->data['destination_iban'] ?? null,
|
||||||
'number' => $this->data['destination_number'] ?? null,
|
'number' => $this->data['destination_number'] ?? null,
|
||||||
@@ -468,8 +469,8 @@ class JournalUpdateService
|
|||||||
)
|
)
|
||||||
&& TransactionTypeEnum::WITHDRAWAL->value === $type
|
&& TransactionTypeEnum::WITHDRAWAL->value === $type
|
||||||
) {
|
) {
|
||||||
$billId = (int) ($this->data['bill_id'] ?? 0);
|
$billId = (int)($this->data['bill_id'] ?? 0);
|
||||||
$billName = (string) ($this->data['bill_name'] ?? '');
|
$billName = (string)($this->data['bill_name'] ?? '');
|
||||||
$bill = $this->billRepository->findBill($billId, $billName);
|
$bill = $this->billRepository->findBill($billId, $billName);
|
||||||
$this->transactionJournal->bill_id = $bill?->id;
|
$this->transactionJournal->bill_id = $bill?->id;
|
||||||
Log::debug('Updated bill ID');
|
Log::debug('Updated bill ID');
|
||||||
@@ -481,7 +482,7 @@ class JournalUpdateService
|
|||||||
*/
|
*/
|
||||||
private function updateField(string $fieldName): void
|
private function updateField(string $fieldName): void
|
||||||
{
|
{
|
||||||
if (array_key_exists($fieldName, $this->data) && '' !== (string) $this->data[$fieldName]) {
|
if (array_key_exists($fieldName, $this->data) && '' !== (string)$this->data[$fieldName]) {
|
||||||
$value = $this->data[$fieldName];
|
$value = $this->data[$fieldName];
|
||||||
|
|
||||||
if ('date' === $fieldName) {
|
if ('date' === $fieldName) {
|
||||||
@@ -557,7 +558,7 @@ class JournalUpdateService
|
|||||||
{
|
{
|
||||||
// update notes.
|
// update notes.
|
||||||
if ($this->hasFields(['notes'])) {
|
if ($this->hasFields(['notes'])) {
|
||||||
$notes = '' === (string) $this->data['notes'] ? null : $this->data['notes'];
|
$notes = '' === (string)$this->data['notes'] ? null : $this->data['notes'];
|
||||||
$this->storeNotes($this->transactionJournal, $notes);
|
$this->storeNotes($this->transactionJournal, $notes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -605,7 +606,7 @@ class JournalUpdateService
|
|||||||
foreach ($this->metaDate as $field) {
|
foreach ($this->metaDate as $field) {
|
||||||
if ($this->hasFields([$field])) {
|
if ($this->hasFields([$field])) {
|
||||||
try {
|
try {
|
||||||
$value = '' === (string) $this->data[$field] ? null : new Carbon($this->data[$field]);
|
$value = '' === (string)$this->data[$field] ? null : new Carbon($this->data[$field]);
|
||||||
} catch (InvalidDateException|InvalidFormatException $e) { // @phpstan-ignore-line
|
} catch (InvalidDateException|InvalidFormatException $e) { // @phpstan-ignore-line
|
||||||
Log::debug(sprintf('%s is not a valid date value: %s', $this->data[$field], $e->getMessage()));
|
Log::debug(sprintf('%s is not a valid date value: %s', $this->data[$field], $e->getMessage()));
|
||||||
|
|
||||||
@@ -673,8 +674,8 @@ class JournalUpdateService
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$origSourceTransaction = $this->getSourceTransaction();
|
$origSourceTransaction = $this->getSourceTransaction();
|
||||||
|
$this->amountChanged = 0 !== bccomp($origSourceTransaction->amount, app('steam')->negative($amount));
|
||||||
$origSourceTransaction->amount = app('steam')->negative($amount);
|
$origSourceTransaction->amount = app('steam')->negative($amount);
|
||||||
$origSourceTransaction->balance_dirty = true;
|
$origSourceTransaction->balance_dirty = true;
|
||||||
$origSourceTransaction->save();
|
$origSourceTransaction->save();
|
||||||
@@ -816,4 +817,10 @@ class JournalUpdateService
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isAmountChanged(): bool
|
||||||
|
{
|
||||||
|
return $this->amountChanged;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -29,7 +29,7 @@ export default class Get {
|
|||||||
* @returns {Promise<AxiosResponse<any>>}
|
* @returns {Promise<AxiosResponse<any>>}
|
||||||
*/
|
*/
|
||||||
list(params) {
|
list(params) {
|
||||||
return api.get('/api/v2/budgets', {params: params});
|
return api.get('/api/v1/budgets', {params: params});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -490,8 +490,10 @@ let transactions = function () {
|
|||||||
// addedSplit, is called from the HTML
|
// addedSplit, is called from the HTML
|
||||||
// for source account
|
// for source account
|
||||||
const renderAccount = function (item, b, c) {
|
const renderAccount = function (item, b, c) {
|
||||||
return item.title + '<br><small class="text-muted">' + i18next.t('firefly.account_type_' + item.meta.type) + '</small>';
|
console.log('render account');
|
||||||
|
return item.name_with_balance + '<br><small class="text-muted">' + i18next.t('firefly.account_type_' + item.type) + '</small>';
|
||||||
};
|
};
|
||||||
|
console.log('here we are in');
|
||||||
addAutocomplete({
|
addAutocomplete({
|
||||||
selector: 'input.ac-source',
|
selector: 'input.ac-source',
|
||||||
serverUrl: urls.account,
|
serverUrl: urls.account,
|
||||||
|
@@ -38,7 +38,7 @@ export function addAutocomplete(options) {
|
|||||||
'X-CSRF-TOKEN': document.head.querySelector('meta[name="csrf-token"]').content
|
'X-CSRF-TOKEN': document.head.querySelector('meta[name="csrf-token"]').content
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
queryParam: 'filter[query]',
|
queryParam: 'query',
|
||||||
hiddenInput: true,
|
hiddenInput: true,
|
||||||
// preventBrowserAutocomplete: true,
|
// preventBrowserAutocomplete: true,
|
||||||
highlightTyped: true,
|
highlightTyped: true,
|
||||||
@@ -48,6 +48,7 @@ export function addAutocomplete(options) {
|
|||||||
params.serverParams['filter[account_types]'] = options.account_types;
|
params.serverParams['filter[account_types]'] = options.account_types;
|
||||||
}
|
}
|
||||||
if (typeof options.onRenderItem !== 'undefined' && null !== options.onRenderItem) {
|
if (typeof options.onRenderItem !== 'undefined' && null !== options.onRenderItem) {
|
||||||
|
console.log('overrule onRenderItem.');
|
||||||
params.onRenderItem = options.onRenderItem;
|
params.onRenderItem = options.onRenderItem;
|
||||||
}
|
}
|
||||||
if (options.valueField) {
|
if (options.valueField) {
|
||||||
|
Reference in New Issue
Block a user