-
Notifications
You must be signed in to change notification settings - Fork 10
Repeat mapping CTEs per UPDATE in lease posting rule category sync #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghacupha
wants to merge
1
commit into
master
Choose a base branch
from
codex/update-lease-posting-rules-in-postgresql-wioyak
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
erp-system/man_pages/lease-posting-rules/lease-posting-rule-category-sync.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Lease Posting Rule Category Sync (Technical Note) | ||
|
|
||
| ## Why this update is needed | ||
| Lease posting rules can be created from legacy TA rule screens or from the unified posting rule configuration UI. When the debit or credit transaction accounts in the templates change, the posting rule header must also reflect the account categories of those accounts. This ensures the posting rule header and templates stay consistent, and it keeps downstream rule evaluation aligned with the ledger categories derived from the transaction accounts. | ||
|
|
||
| ## Workflow summary | ||
| 1. Collect TA rule data for every lease contract (interest accrual, interest paid transfer, repayment, and recognition). | ||
| 2. Match the TA rules to lease posting rules using the `leaseContractId` posting rule condition and event type. | ||
| 3. Update posting rule templates so debit/credit accounts mirror the TA rules. | ||
| 4. Update the posting rule header debit/credit account category fields based on the selected transaction accounts. | ||
| 5. Normalize the `leaseContractId` condition value so it matches the lease contract in the TA rule. | ||
|
|
||
| ## Implementation notes | ||
| - The synchronization relies on the posting rule condition key `leaseContractId` with the `EQUALS` operator to associate a posting rule with a lease contract. | ||
| - Only rules with `module = 'LEASE'` are updated. | ||
| - Each posting rule is updated with the account categories derived from `transaction_account.account_category_id`. | ||
|
|
||
| ## Operational script | ||
| Use the SQL script in `erp-system/queries/lease-posting-rule-category-sync.sql` to apply the update in PostgreSQL once TA rules or posting templates have been modified for existing lease contracts. The script repeats its CTEs for each `UPDATE`, so it can be executed as-is in tools that run each statement independently. |
149 changes: 149 additions & 0 deletions
149
erp-system/queries/lease-posting-rule-category-sync.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| -- Sync lease posting rules with TA rule debit/credit accounts and account categories. | ||
| -- | ||
| -- This script aligns lease-specific posting rules with legacy TA rules by: | ||
| -- 1) Updating templates to use the TA rule debit/credit accounts. | ||
| -- 2) Updating posting rule debit/credit account categories to match the | ||
| -- selected transaction accounts. | ||
| -- 3) Normalizing leaseContractId conditions for the affected posting rules. | ||
|
|
||
| WITH ta_rules AS ( | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_INTEREST_ACCRUAL'::varchar AS event_type | ||
| FROM talease_interest_accrual_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_INTEREST_PAID_TRANSFER'::varchar AS event_type | ||
| FROM tainterest_paid_transfer_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_REPAYMENT'::varchar AS event_type | ||
| FROM talease_repayment_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_LIABILITY_RECOGNITION'::varchar AS event_type | ||
| FROM talease_recognition_rule | ||
| ), | ||
| lease_rule_map AS ( | ||
| SELECT pr.id AS posting_rule_id, | ||
| ta.lease_contract_id, | ||
| ta.debit_id, | ||
| ta.credit_id | ||
| FROM trx_account_posting_rule pr | ||
| JOIN trx_account_posting_rule_condition prc | ||
| ON prc.posting_rule_id = pr.id | ||
| AND prc.condition_key = 'leaseContractId' | ||
| AND prc.condition_operator = 'EQUALS' | ||
| JOIN ta_rules ta | ||
| ON ta.lease_contract_id = prc.condition_value::bigint | ||
| AND pr.event_type = ta.event_type | ||
| WHERE pr.module = 'LEASE' | ||
| ) | ||
| UPDATE trx_account_posting_rule_template template | ||
| SET debit_account_id = mapping.debit_id, | ||
| credit_account_id = mapping.credit_id | ||
| FROM lease_rule_map mapping | ||
| WHERE template.posting_rule_id = mapping.posting_rule_id; | ||
|
|
||
| WITH ta_rules AS ( | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_INTEREST_ACCRUAL'::varchar AS event_type | ||
| FROM talease_interest_accrual_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_INTEREST_PAID_TRANSFER'::varchar AS event_type | ||
| FROM tainterest_paid_transfer_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_REPAYMENT'::varchar AS event_type | ||
| FROM talease_repayment_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_LIABILITY_RECOGNITION'::varchar AS event_type | ||
| FROM talease_recognition_rule | ||
| ), | ||
| lease_rule_map AS ( | ||
| SELECT pr.id AS posting_rule_id, | ||
| ta.lease_contract_id, | ||
| ta.debit_id, | ||
| ta.credit_id | ||
| FROM trx_account_posting_rule pr | ||
| JOIN trx_account_posting_rule_condition prc | ||
| ON prc.posting_rule_id = pr.id | ||
| AND prc.condition_key = 'leaseContractId' | ||
| AND prc.condition_operator = 'EQUALS' | ||
| JOIN ta_rules ta | ||
| ON ta.lease_contract_id = prc.condition_value::bigint | ||
| AND pr.event_type = ta.event_type | ||
| WHERE pr.module = 'LEASE' | ||
| ) | ||
| UPDATE trx_account_posting_rule pr | ||
| SET debit_account_type_id = debit_account.account_category_id, | ||
| credit_account_type_id = credit_account.account_category_id | ||
| FROM lease_rule_map mapping | ||
| JOIN transaction_account debit_account | ||
| ON debit_account.id = mapping.debit_id | ||
| JOIN transaction_account credit_account | ||
| ON credit_account.id = mapping.credit_id | ||
| WHERE pr.id = mapping.posting_rule_id; | ||
|
|
||
| WITH ta_rules AS ( | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_INTEREST_ACCRUAL'::varchar AS event_type | ||
| FROM talease_interest_accrual_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_INTEREST_PAID_TRANSFER'::varchar AS event_type | ||
| FROM tainterest_paid_transfer_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_REPAYMENT'::varchar AS event_type | ||
| FROM talease_repayment_rule | ||
| UNION ALL | ||
| SELECT lease_contract_id, | ||
| debit_id, | ||
| credit_id, | ||
| 'LEASE_LIABILITY_RECOGNITION'::varchar AS event_type | ||
| FROM talease_recognition_rule | ||
| ), | ||
| lease_rule_map AS ( | ||
| SELECT pr.id AS posting_rule_id, | ||
| ta.lease_contract_id, | ||
| ta.debit_id, | ||
| ta.credit_id | ||
| FROM trx_account_posting_rule pr | ||
| JOIN trx_account_posting_rule_condition prc | ||
| ON prc.posting_rule_id = pr.id | ||
| AND prc.condition_key = 'leaseContractId' | ||
| AND prc.condition_operator = 'EQUALS' | ||
| JOIN ta_rules ta | ||
| ON ta.lease_contract_id = prc.condition_value::bigint | ||
| AND pr.event_type = ta.event_type | ||
| WHERE pr.module = 'LEASE' | ||
| ) | ||
| UPDATE trx_account_posting_rule_condition condition | ||
| SET condition_value = mapping.lease_contract_id::text | ||
| FROM lease_rule_map mapping | ||
| WHERE condition.posting_rule_id = mapping.posting_rule_id | ||
| AND condition.condition_key = 'leaseContractId'; | ||
16 changes: 16 additions & 0 deletions
16
erp-system/user-stories/lease-posting-rules/lease-posting-rule-category-sync.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # User Story: Sync Lease Posting Rule Categories | ||
|
|
||
| ## Persona | ||
| **Lease Accounting Administrator** who maintains posting rules and ensures the account categories match the configured debit and credit accounts. | ||
|
|
||
| ## Scenario | ||
| Lease templates are updated for existing contracts, but the posting rule header categories are out of sync. The administrator needs to align the posting rule header and templates with the TA rule configuration so that postings continue to use the correct categories. | ||
|
|
||
| ### Steps | ||
| 1. Review the lease contract and confirm the TA rules (interest accrual, interest paid transfer, repayment, recognition) are correct. | ||
| 2. Ensure each lease posting rule has a `leaseContractId` condition that targets the lease contract. | ||
| 3. Run the lease posting rule category sync SQL script. | ||
| 4. Refresh the lease posting rule configuration list. | ||
|
|
||
| ## Expected outcome | ||
| The posting rule templates use the correct debit/credit transaction accounts, and the posting rule header debit/credit categories match the account categories of those accounts. |
19 changes: 19 additions & 0 deletions
19
man_pages/lease-posting-rules/lease-posting-rule-category-sync.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Lease Posting Rule Category Sync (Technical Note) | ||
|
|
||
| ## Why this update is needed | ||
| Lease posting rules can be created from legacy TA rule screens or from the unified posting rule configuration UI. When the debit or credit transaction accounts in the templates change, the posting rule header must also reflect the account categories of those accounts. This ensures the posting rule header and templates stay consistent, and it keeps downstream rule evaluation aligned with the ledger categories derived from the transaction accounts. | ||
|
|
||
| ## Workflow summary | ||
| 1. Collect TA rule data for every lease contract (interest accrual, interest paid transfer, repayment, and recognition). | ||
| 2. Match the TA rules to lease posting rules using the `leaseContractId` posting rule condition and event type. | ||
| 3. Update posting rule templates so debit/credit accounts mirror the TA rules. | ||
| 4. Update the posting rule header debit/credit account category fields based on the selected transaction accounts. | ||
| 5. Normalize the `leaseContractId` condition value so it matches the lease contract in the TA rule. | ||
|
|
||
| ## Implementation notes | ||
| - The synchronization relies on the posting rule condition key `leaseContractId` with the `EQUALS` operator to associate a posting rule with a lease contract. | ||
| - Only rules with `module = 'LEASE'` are updated. | ||
| - Each posting rule is updated with the account categories derived from `transaction_account.account_category_id`. | ||
|
|
||
| ## Operational script | ||
| Use the SQL script in `erp-system/queries/lease-posting-rule-category-sync.sql` to apply the update in PostgreSQL once TA rules or posting templates have been modified for existing lease contracts. The script repeats its CTEs for each `UPDATE`, so it can be executed as-is in tools that run each statement independently. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
user-stories/lease-posting-rules/lease-posting-rule-category-sync.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # User Story: Sync Lease Posting Rule Categories | ||
|
|
||
| ## Persona | ||
| **Lease Accounting Administrator** who maintains posting rules and ensures the account categories match the configured debit and credit accounts. | ||
|
|
||
| ## Scenario | ||
| Lease templates are updated for existing contracts, but the posting rule header categories are out of sync. The administrator needs to align the posting rule header and templates with the TA rule configuration so that postings continue to use the correct categories. | ||
|
|
||
| ### Steps | ||
| 1. Review the lease contract and confirm the TA rules (interest accrual, interest paid transfer, repayment, recognition) are correct. | ||
| 2. Ensure each lease posting rule has a `leaseContractId` condition that targets the lease contract. | ||
| 3. Run the lease posting rule category sync SQL script. | ||
| 4. Refresh the lease posting rule configuration list. | ||
|
|
||
| ## Expected outcome | ||
| The posting rule templates use the correct debit/credit transaction accounts, and the posting rule header debit/credit categories match the account categories of those accounts. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CTE map is built only from
trx_account_posting_rule_conditionrows wherecondition_operator = 'EQUALS', but the finalUPDATEnormalizes allleaseContractIdconditions for those posting rules regardless of operator. If a rule has multipleleaseContractIdconditions (e.g., an additionalNOT_EQUALSorCONTAINSoperator), this statement will overwrite those values with the single mapped contract id, changing the rule’s matching semantics. Restrict the update tocondition_operator = 'EQUALS'or to the specific condition rows used in the mapping to avoid corrupting other operators.Useful? React with 👍 / 👎.