[PR #5925] Adds balance variable to rule templates #13268

Closed
opened 2026-04-10 21:50:37 -05:00 by GiteaMirror · 0 comments
Owner

Original Pull Request: https://github.com/actualbudget/actual/pull/5925

State: closed
Merged: Yes


Enables access to the account balance within rule templates. This allows for more complex rule creation based on the current account balance.

Calculates the account balance up to the transaction being processed, including transactions on the same date with lower sort order.

Handles cases where the balance is undefined gracefully, defaulting to 0 to prevent errors.

Copilot Summary

This pull request adds support for using the account balance in transaction rule actions, enabling more advanced rule logic and calculations. It introduces the balance property to transactions processed by rules, ensures correct calculation of the balance up to each transaction, and updates both the rule execution logic and tests to handle balance-related scenarios robustly.

Balance Calculation and Integration:

  • Added calculation of the account balance up to (but not including) each transaction in prepareTransactionForRules, making it available for rule actions and templates.
  • Extended the transaction type (TransactionForRules) to include the optional balance property for use in rules.
  • Ensured the balance property is removed from transactions after rule processing to avoid side effects.

Rule Engine and Validation Improvements:

  • Updated the Action class to default number fields to 0 when template evaluation results in NaN, preventing database errors when balance is undefined.

Testing and Validation:

  • Added comprehensive tests to verify that balance is accessible in rule templates, supports math operations, and is handled gracefully when undefined, including scenarios for both string and number fields. [1] [2]

Reference

Attempts to address point 1 in the first comment: https://github.com/actualbudget/actual/issues/3606#issuecomment-2400673544

AI disclaimer

This PR contains code that was partially or fully generated by AI and may contain errors. All suggestions for improvement are welcome.

**Original Pull Request:** https://github.com/actualbudget/actual/pull/5925 **State:** closed **Merged:** Yes --- Enables access to the account balance within rule templates. This allows for more complex rule creation based on the current account balance. Calculates the account balance up to the transaction being processed, including transactions on the same date with lower sort order. Handles cases where the balance is undefined gracefully, defaulting to 0 to prevent errors. # Copilot Summary This pull request adds support for using the account balance in transaction rule actions, enabling more advanced rule logic and calculations. It introduces the `balance` property to transactions processed by rules, ensures correct calculation of the balance up to each transaction, and updates both the rule execution logic and tests to handle balance-related scenarios robustly. **Balance Calculation and Integration:** * Added calculation of the account `balance` up to (but not including) each transaction in `prepareTransactionForRules`, making it available for rule actions and templates. * Extended the transaction type (`TransactionForRules`) to include the optional `balance` property for use in rules. * Ensured the `balance` property is removed from transactions after rule processing to avoid side effects. **Rule Engine and Validation Improvements:** * Updated the `Action` class to default number fields to `0` when template evaluation results in `NaN`, preventing database errors when `balance` is undefined. **Testing and Validation:** * Added comprehensive tests to verify that `balance` is accessible in rule templates, supports math operations, and is handled gracefully when undefined, including scenarios for both string and number fields. [[1]](diffhunk://#diff-03472a29ce63ef88a786032d62a9d9448077800db24356db421b1435a30d6488R333-R341) [[2]](diffhunk://#diff-03472a29ce63ef88a786032d62a9d9448077800db24356db421b1435a30d6488R479-R516) ## Reference Attempts to address point 1 in the first comment: https://github.com/actualbudget/actual/issues/3606#issuecomment-2400673544 ### AI disclaimer This PR contains code that was partially or fully generated by AI and may contain errors. All suggestions for improvement are welcome. <!-- Thank you for submitting a pull request! Make sure to follow the instructions to write release notes for your PR — it should only take a minute or two: https://github.com/actualbudget/docs#writing-good-release-notes. Try running yarn generate:release-notes *before* pushing your PR for an interactive experience. -->
GiteaMirror added the pull-request label 2026-04-10 21:50:37 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/actual#13268