Skip to content

Bugs/625412 Wrong posted G/L Entries and VAT Entries using Cash Basis Unrealized VAT from a partial Credit Memo if we mix VAT types and use only part of the amount in the Mexican version. - #10840

Open
Maria Jose Jofre (v-mjofre) wants to merge 10 commits into
mainfrom
bugs/625412-master-mx-wrongposted-glentry-cashbasis

Conversation

@v-mjofre

@v-mjofre Maria Jose Jofre (v-mjofre) commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Error Reported
When posting sales credit memos with cash-basis VAT, partially applying a credit memo to invoices containing multiple VAT posting groups could calculate VAT realization using the total settled amount instead of the amount associated with each VAT group. This resulted in incorrect amounts and invalid G/L entries between the realized and unrealized VAT accounts.

Solution
A dedicated calculation was added to determine the settled credit memo amount for each VAT posting group combination. This amount is adjusted by the currency factor before the VAT amount is calculated.

A condition was also introduced to control when G/L entries should be posted, preventing incorrect entries when the credit memo application does not represent an actual VAT realization.

Files Modified

  • GenJnlPostLine.Codeunit.al: uses the settled amount per VAT group and controls G/L entry creation.
  • CustLedgerEntry.Table.al: adds the calculation of the credit memo portion associated with each VAT group.
  • App/Layers/NA/Tests/Local/ERMCashBasis.codeunit.al: adds and updates automated tests for cash-basis VAT credit memo scenarios.

Result
Cash-basis VAT credit memos now realize VAT correctly for each VAT posting group. Partial and full applications, subsequent payments, and unapplied credit memos produce the expected amounts without creating invalid entries between the realized and unrealized VAT accounts.

Fixes
AB#648956

@v-mjofre
Maria Jose Jofre (v-mjofre) requested a review from a team August 31, 2026 17:21
@github-actions github-actions Bot added this to the Version 30.0 milestone Aug 31, 2026
@github-actions github-actions Bot added the Team: Finance GitHub request for Finance area label Aug 31, 2026
Comment thread src/Layers/NA/BaseApp/Sales/Receivables/CustLedgerEntry.Table.al Outdated
Comment thread src/Layers/NA/BaseApp/Finance/GeneralLedger/Posting/GenJnlPostLine.Codeunit.al Outdated
Comment thread src/Layers/NA/BaseApp/Sales/Receivables/CustLedgerEntry.Table.al Outdated
Comment thread src/Layers/NA/BaseApp/Sales/Receivables/CustLedgerEntry.Table.al
Comment thread src/Layers/NA/Tests/Local/ERMCashBasis.codeunit.al
Comment thread src/Layers/NA/Tests/Local/ERMCashBasis.codeunit.al
@alexei-dobriansky

Copy link
Copy Markdown
Contributor

Good Sense Reviewer - Round 1

Recommendation: Request Changes

What this PR does

This changes customer unrealized VAT posting for sales credit memos so the VAT part is calculated from the settled credit memo amount for each VAT posting group. It also skips the unrealized-to-realized G/L transfer when the credit memo application only reduces the invoice's remaining unrealized VAT.

The main logic is targeted: the grouped path filters VAT entries by posting group, splits the settled credit memo amount from the posted credit memo lines, and still updates the source VAT entry through PostUnrealVATEntry. No new event subscriber or external publisher dependency is introduced. The remaining concern is that the exact high-risk posting path that produced wrong G/L entries is not asserted.

Problem-solution fit

Fit: Strong

The reported scenario is a partial sales credit memo applied to an invoice with multiple VAT posting groups. The code change targets that calculation and the related G/L transfer, and the scope stays within the customer cash-basis VAT posting path.

Suggestions

S1 (🔴 High): Add exact G/L regression assertion
Please store the posted credit memo number in PartialCrMemoAppliesToDocMultiVATCorrectRealization and call VerifyInvoiceUnrealizedVATGLEntriesDoNotExist for the credited VAT group. This is the exact partial-credit-memo on multi-VAT invoice path. Without this check, the new ShouldPostGLEntries guard can regress while the test still passes.

Risk assessment and necessity

Risk: This is a sensitive customer posting path in codeunit 12 that changes VAT Entry updates and G/L Entry creation for cash-basis VAT. A wrong result can post incorrect VAT realization or leave invalid transfers between realized and unrealized VAT accounts. The purchase/vendor paths are not changed, and no public event contract is changed.

Necessity: The change is needed because the current behavior can create wrong VAT and G/L amounts when a partial credit memo is applied against a mixed-VAT invoice. There is no safe functional workaround other than correcting posted accounting data, so the scope is justified, but the exact regression path should be pinned before merge.


[AI-PR-REVIEW] version=1 promptVersion=4 system=github pr=10840 round=1 by=alexei-dobriansky at=2026-08-31T19:15:04Z lastSha=7f376bd0b63c1fd15a4fbf84b1070f0f1866b058 reviewKey=b502d024d42349fa9b5b400a6d64f7a603fc0fd38f273c72abdc27e52d69005f suggestions=S1@0ca27496

@alexei-dobriansky

Copy link
Copy Markdown
Contributor

Good Sense Reviewer - Round 2

Recommendation: Request Changes

What this PR does

Since round 1, the tests now store the posted credit memo number and assert that the credited VAT group does not post the invoice-side unrealized-to-realized G/L transfer. A new test also covers a partially settled credit memo with more than one VAT group.

That addresses the previous high-risk test gap. The posting logic is still targeted to the customer cash-basis VAT path, but the current head does not pass the required AL analyzer build because one changed branch now uses begin/end around a single statement.

Status of previous suggestions
ID Title Status Author response
S1 Add exact G/L regression assertion Addressed The test now stores CreditMemoNo and checks that the credited VAT group has no invoice-side unrealized VAT G/L transfer.
New observations (commits since round 1)

S2 (🔴 High): AL analyzer failure blocks build
Remove the begin/end around the single VATPart := assignment, or add another statement if the branch really needs a block. The AL analyzer reports AA0005 at this line, and the Apps CA/MX/US builds fail. This must be fixed before merge.

Risk assessment and necessity

Risk: This remains a sensitive customer VAT posting path in codeunit 12. A wrong result can post incorrect VAT realization or leave invalid transfers between realized and unrealized VAT accounts. No public event contract is changed, but the current analyzer failure blocks a clean app build.

Necessity: The change is needed because the reported partial credit memo scenario can create wrong VAT and G/L amounts. The added assertion now pins the exact G/L regression path, so the remaining work is to make the changed branch comply with the required AL ruleset.


[AI-PR-REVIEW] version=1 promptVersion=4 system=github pr=10840 round=2 by=alexei-dobriansky at=2026-09-02T01:15:55Z lastSha=291726e33743f8011f2c7b1b330cfe8906f1557e reviewKey=e3edb3c159ed69250e2227aa20888b7ad74c974ba81f989061e3cd13edb771fc suggestions=S1@0ca27496:addressed,S2@8687c764:new parentRound=1

@alexei-dobriansky

Copy link
Copy Markdown
Contributor

Good Sense Reviewer - Round 3

Recommendation: Request Changes

What this PR does

Since round 2, the single-statement begin/end block was removed, so the previous AA0005 analyzer issue is fixed. The PR still changes customer cash-basis VAT posting so credit memo applications are split by VAT posting group and invoice-side G/L transfers can be skipped.

The direct credit-memo posting path is better covered now, but the current head still has build-blocking AA0210 warnings in the new tests. The posting logic also still misses an already-posted credit memo application path and can over-realize a group when the calculated group amount is higher than that group's remaining unrealized VAT.

Status of previous suggestions
ID Title Status Author response
S1 Add exact G/L regression assertion Addressed The tests now store the posted credit memo number and check the missing invoice-side G/L transfer.
S2 AL analyzer failure blocks build Addressed The single-statement block was removed, and AA0005 is no longer present in the current build log checked.
New observations (current head)

S3 (🔴 High): Cover posted credit memo applications
Also run the VAT-group path when an already posted credit memo is applied from the invoice entry. In that flow GenJnlLine is copied from the invoice and CustLedgEntry2 can be the credit memo, so this new per-group amount is bypassed. The old total-amount calculation can still realize the wrong VAT group.

S4 (🔴 High): Cap VAT realization per group
Limit the group settled amount to the amount still available for that VAT entry before calculating VATPart. Cash Basis uses Unrealized Amount * VATPart, not Remaining Unrealized Amount, so a prior payment can make this subtract more than the group still has left.

S5 (🔴 High): Remove new VAT Entry key warnings
The current build fails because the new tests add four VATEntry.SetRange("Document Type", ...) filters, and VAT Entry has no key with that field. Rework those lookups, for example by using a supported key or an existing helper, so no new AA0210 warnings are introduced.

Risk assessment and necessity

Risk: This is a sensitive cash-basis VAT posting path in codeunit 12. A wrong result can persist incorrect VAT realization or invalid G/L/VAT balances, and the current CI build is still blocked by new analyzer warnings.

Necessity: The change is needed because the reported partial credit memo scenario can post wrong VAT and G/L amounts. The scope is justified, but the remaining posting gaps and build failure must be fixed before merge.


[AI-PR-REVIEW] version=1 promptVersion=4 system=github pr=10840 round=3 by=alexei-dobriansky at=2026-09-04T10:39:14Z lastSha=522de7402488d02b4aed26245ee542a3aa88c842 reviewKey=9f9ef1e6c978325857f770624f927b945cd8939716e84addfa4c500076e2a72e suggestions=S1@0ca27496:addressed,S2@8687c764:addressed,S3@e45349d5:new,S4@1dfd09b4:new,S5@a3d49d44:new parentRound=2

Comment thread src/Layers/NA/Tests/Local/ERMCashBasis.codeunit.al Outdated
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

$\textbf{🟡\ Medium\ Severity\ —\ Testing}$

VerifyVATEntryAfterApplicationOfInvoiceWithCrMemo is the only coverage for the new invoice-to-credit-memo VAT-grouping path, but VerifyVATEntryForPostApplication only compares the first two VAT entries in the last G/L register to VATAmount and -VATAmount. It does not verify the VAT posting group, the number of VAT entries created, or that the untouched invoice VAT group stayed unchanged, so a regression in GetCreditMemoPartAmountByVAT or the new grouping branch can still pass. Assert the affected VAT posting group explicitly and verify that no extra VAT entries were produced for the other group.

Agent judgement — not directly backed by a BCQuality knowledge article.

Line mapping was unavailable, so this was posted as an issue comment.

👍 useful · ❤️ especially valuable · 👎 wrong - reply with why · AL review agent v1.41.6

Comment thread src/Layers/NA/Tests/Local/ERMCashBasis.codeunit.al
@alexei-dobriansky

Copy link
Copy Markdown
Contributor

Good Sense Reviewer - Round 7

Recommendation: Accept

What this PR does

Since round 6, the only code change is in the cash-basis VAT test code. It adds the posting-date confirm handler to one existing purchase unrealized VAT test and removes extra assertions from an older application test.

The production posting logic is unchanged in this update. The full PR still splits credit memo settlement by VAT posting group, avoids the invoice-side unrealized-to-realized G/L transfer for credit memo applications, caps realized amounts to the remaining unrealized VAT, and keeps regression coverage for the sensitive sales credit memo posting scenarios.

Status of previous suggestions

No active suggestions were carried from round 6. The live marker for round 6 had an empty suggestions list.

New observations (commits since round 6)

None - the latest commit only adjusts test coverage and does not introduce a new blocking issue.

Risk assessment and necessity

Risk: The touched production area remains high risk because it posts customer unrealized VAT, VAT entries, and G/L entries. The latest update is test-only, and the removed checks are covered by the dedicated multi-VAT credit memo tests that assert remaining unrealized VAT and absence of invalid G/L transfers.

Necessity: The change remains needed because the bug can persist wrong VAT realization and wrong G/L amounts. The current scope is still targeted to the reported cash-basis credit memo scenario and its follow-up posting paths.


[AI-PR-REVIEW] version=1 promptVersion=4 system=github pr=10840 round=7 by=alexei-dobriansky at=2026-09-08T19:19:32Z lastSha=09ea6a23cacc5efcdc194da6da1e5f9142a4e3f6 reviewKey=26b2a5a8a883472f59e7fd68f66164b2d5240cbdc85d5cdac583056320566e8a suggestions= parentRound=6

Comment thread src/Layers/MX/Tests/Local/ERMSalesPurchaseVAT.Codeunit.al Outdated
Comment thread src/Layers/NA/Tests/Local/ERMCashBasis.codeunit.al Outdated
Comment thread src/Layers/NA/Tests/Local/ERMCashBasis.codeunit.al
@alexei-dobriansky

Copy link
Copy Markdown
Contributor

Good Sense Reviewer - Round 8

Recommendation: Request Changes

What this PR does

The full PR updates cash-basis unrealized VAT posting for partial credit memos with mixed VAT posting groups. It splits the settled credit memo amount by VAT group, avoids the invalid invoice-side G/L transfer for credit memo applications, and keeps VAT entry updates tied to the affected group.

The latest round is test-only. It strengthens one VAT-entry assertion and changes two MX credit memo tests, but the MX test change now calls a helper that is not declared in that codeunit.

Problem-solution fit

Fit: Strong

The bug is clear: a partial credit memo must realize VAT only for the affected VAT group and must not create invalid G/L entries between realized and unrealized VAT accounts. The production fix and the main regression tests match that scenario, but the latest MX test edit must compile before the PR can be accepted.

Status of previous suggestions

No active suggestions were carried from round 7.

New observations (commits since round 7)

S1 (🔴 High): MX tests call an undefined helper
The changed MX tests now call VerifyRealizedVATGLEntriesDoNotExist, but that procedure is not declared in this codeunit. Add the helper here, or call a helper that exists in this codeunit, so the test app can compile.

Risk assessment and necessity

Risk: The production area is high risk because it posts customer unrealized VAT, VAT entries, and G/L entries. The latest code change is test-only, but the undefined helper prevents the MX test app from compiling, so CI cannot prove the posting fix.

Necessity: The change remains needed because the bug can persist wrong VAT realization and wrong G/L amounts. The scope remains targeted once the test compile issue is fixed.


[AI-PR-REVIEW] version=1 promptVersion=4 system=github pr=10840 round=8 by=alexei-dobriansky at=2026-09-09T07:28:37Z lastSha=289d215b60fe68692b5071016bd3391f42b03fcd reviewKey=fc80bcaa0bb8fff5de324d708fe6781eb928129fb1a663bb614c90b245b92782 suggestions=S1@2367fc9d:new parentRound=7

@alexei-dobriansky

Copy link
Copy Markdown
Contributor

Good Sense Reviewer - Round 9

Recommendation: Accept

What this PR does

The full PR updates cash-basis unrealized VAT posting for partial sales credit memos with mixed VAT posting groups. It now uses the settled credit memo amount for each VAT group, avoids invoice-side realized/unrealized G/L transfers for the credit memo application, and updates tests for the sales and MX paths.

The latest round is test-only. It adds the missing MX helper and keeps the additional-currency tests on the expected G/L-entry path, so the prior compile blocker is addressed without changing production code.

Problem-solution fit

Fit: Strong

The bug is clear: a partial credit memo must realize VAT only for the affected VAT group and must not create invalid G/L entries between realized and unrealized VAT accounts. The production calculation and regression tests match that scenario, including partial and remaining-payment flows.

Status of previous suggestions
ID Title Status Author response
S1 MX tests call an undefined helper Addressed The helper is now declared in the MX test codeunit.
New observations (commits since round 8)

None - the latest changes only address the prior test compile issue and adjust the affected assertions.

Risk assessment and necessity

Risk: The production area is high risk because it posts customer unrealized VAT, VAT entries, and G/L entries. The latest changes are limited to tests, and the helper used by the changed MX tests is now present.

Necessity: The change is needed because the bug can persist wrong VAT realization and wrong G/L amounts. The scope is targeted to sales credit memo application and the related regression coverage.


[AI-PR-REVIEW] version=1 promptVersion=4 system=github pr=10840 round=9 by=alexei-dobriansky at=2026-09-09T19:17:45Z lastSha=3a6a5cf2f46fe1bd79fc9ea93dee0eb4df278f0a reviewKey=551452af610a5e55e1abc1b3324214521e0c2605e378d3331396d8f18b90cf57 suggestions=S1@2367fc9d:addressed parentRound=8

@alexei-dobriansky

Copy link
Copy Markdown
Contributor

Good Sense Reviewer - Round 10

Recommendation: Accept

What this PR does

The latest changes adjust the cash-basis unrealized VAT fix and tests for partial credit memos with multiple VAT groups. The code limits realization to the affected VAT posting group, and the tests now check both affected and unaffected VAT groups.

Status of previous suggestions
ID Title Status Author response
S1 MX tests call an undefined helper Addressed The helper is now present and the latest test changes use the VAT posting group-specific checks.
New observations (commits since round 9)

None - the latest commit is test and scenario stabilization for the already-reviewed fix.

Risk assessment and necessity

Risk: High area but controlled. This affects VAT realization G/L and VAT entries in localized cash-basis posting, but the changed conditions are focused on credit memo application and are backed by multi-VAT-group tests.

Necessity: The fix is needed to avoid realizing or reversing VAT for the wrong VAT group when a partial credit memo is applied.


[AI-PR-REVIEW] version=1 promptVersion=4 system=github pr=10840 round=10 by=alexei-dobriansky at=2026-09-11T11:22:36.729Z lastSha=90e5dd1ada3397f90d5bd05aa975f10f3423826a reviewKey=ae18925b752a62c65f54b8347b5ab7982fc09793cf2df37731ce88c53bb8bf60 suggestions=S1@2367fc9d:addressed parentRound=9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Team: Finance GitHub request for Finance area

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants