Published On: February 16, 2017Categories: Technical

Posting a purchase order related invoice for a non-stocked product in AX resulted in a price variance posting. Normally this shouldn’t happen. Investigating the problem we noticed that it happens in case of a partial invoice posting related to single receipt. This problem happens when the invoice is the last one for the line. The AX version is AX 2012 R2.
To make it simple, we have one line with quantity 100 and unit price 1. We invoice 25 on the first invoice. The posting goes well. On the second invoice, we invoice the rest e.g. 75. The expectation is that the no price variance will be calculated. However, if we post the invoice or view the accounting distribution we can see price variance calculated and the extended price calculated for the full amount of the line. In this case, the accounting distribution looks like this:
Net amount: 75.00
Extended price: 100.00
Price variance: 25.00
The accounting distributions are calculated in the Source Document Framework. Looking into the code we noticed that when the line needs to be closed, there is a different calculation for the remaining amount.
The calculation starts in VendorInvoiceLineSourceDocLineItem class in the calculateSourceDocumentAmountMap method. On the line 79, it switches to the calcRemainingAmount in the same class. The third parameter marked with the red box is part of the fix. We will come back later with this.

calculate SourceDocumentAmountMap method
The calcRemainingAmount method uses the SourceDocumentBalanceProvider class to calculate the balance. Here we propagate the same parameter to the parmDoRestrictByAccountingEvent. This is a new parm method created.

parmDoRestrictByAccountingEvent

SourceDocumentBalanceProvider calculates the amounts. We are interested in the calculateRelievedAmount method since this is the amount used in the price calculation. Again, we propagate the information in the SourceDocumentLineProvider::getDistributedAmountInCurrency method.

Purchase price variance in accounting distribution - service invoice
Here is the origin of the problem. The query that calculates the amount uses the Accounting Distributions as a source of data. But, the accounting distributions that belong to the previous invoices are not filtered out!

Here is the origin of the problem
The parameter that we propagated through out the classes is now used to implement the filter.
This is a classical AX 2012 R2 problem. In R3 version it is fixed and in this post, the same fix is applied in R2.
This post is written since the problem was noticed (and it was a huge problem for the accounting department at the client).
We hope that this could help someone and save some investigating time.