Calculate Preposted tax document on Purchases order confirmation in D365FO using Electronic reporting





What's the Problem:



Some of the customers demand printing the tax amount in the Purchases order print at line level. As we know, there is no table in the standard D365 to save the line-level tax amount in any posted tables. To get the tax amount at line level, the tax document needs to be calculated at run-time with available information on the Purchases Order line.

Presently we use the customized class to calculate the tax amount at line level using the below code:

static void GSTTotal(Args _args)

{
    vendPurchOrderJour vendPurchOrderJour;
    ITaxDocument taxDocument;
    ITaxDocumentComponentLineEnumerator componentLineEnumerator;
    ITaxDocumentComponentLine componentLineObject;
    ITaxDocumentMeasureEnumerator measureEnumerator;
    TaxAmount taxAmount,taxValue;
    TaxComponent_IN taxComponent;
    ;
    vendPurchOrderJour = vendPurchOrderJour::findRecId(5637291565); 
    /Need to pass Confirmation journal recid
    taxDocument = TaxBusinessService::getTaxDocumentBySource(vendPurchOrderJour.TableId, 
    vendPurchOrderJour.RecId);

    componentLineEnumerator = taxDocument.componentLines();

    while(componentLineEnumerator.moveNext())
    {
        componentLineObject = componentLineEnumerator.current();

        taxComponent = componentLineObject.metaData().taxComponent();
        taxValue = componentLineObject.getMeasure("Rate").value().value() * 100;
        taxAmount = componentLineObject.getMeasure("Tax Amount").value().value();

        info(strFmt("Component %1 ,Rate %2, Amount%3",taxComponent,taxValue,taxAmount));
    }
}

To create the Purchases order print document through Electronic reporting, we need to calculate the tax amount in a similar manner as mentioned in the above code.

Here is the solution:

After analyzing the X++ code, we can conclude the below points:
1. First identify the current transactions for which we are calculating the tax amount. We will take the Recid from VendPurchOrderJour (represents the Purchases order confirmation) table.

2. Initialize the Tax business service class. Use the method "GetTaxDocumentbySouce" method by passing the TableID and RecID of current transactions in VendPurchOrderJour.

3. Get the purchases order lines and Enumerate all the lines in the purchases order to get serial numbers starting from 1 to n.

4. Here comes a tricky and interesting method available in Tax document named "ToUnzipJSON". this method provides a detailed JSON file representing a complete tax document containing the header and lines at the component level.

Below is the sample JSON file representing TaxAmount, tax Component, and Tax rates at different nodes.


5. To fetch the values from JSON file, we will use the JSONVALUE function
Syntax for JSONVALUE function
JSONVALUE(_string, _path). Here _string represents the JSON string and _path represents the node from where data is to be fetched. In the above screenshots, path is mentioned as per requirement.

To use the above conclusions in the Electronic reporting. We need to modify the model mapping. You can download the configuration file for practice and better understanding.

Logic for the report: 



1. Let's start with creating a data model for our testing.


2. Add the data source type "class". Select the class "Tax business service". Then add one calculated field "$TaxDocumentJSONstring" with the below formula:

This method provides the JSON shown in previous screenshots.

2. Add calculate field "EnumeratePurchlines".


3. Now add calculated fields under the above-calculated field.
First, we are creating one function "#TaxMeasureFunction" with parameter "path": string. This function will be used in other calculated fields by passing the values to the "path" parameter and getting the required values of the Tax rate, tax amount, and tax component.


Here we added Counter0 and Counter1 assuming only two components at max, but if it is a case of "Cess", then one more counter will be required.

Tax component representing the RECID of TaxRuntimeDocComponent table which contains the tax component name - IGST, SGST, and CGST.

To get the tax component name, we can create one more function "#TaxComponentIdentifier".



We need to identify the CGST Rate and tax amount from above Counter0 and Counter1 calculated fields, to achieve this we are using the single calculated field for Rate and Tax amount named as:
$$CGSTRate|TaxAmount 

Similarly, the calculated fields of "$$SGSTRate|TaxAmount" and "$$IGSTRate|TaxAmount" can be added.

Bind the data model with the data sources.


Final Output:

Now it's time to run the data model mapping. Save the above configuration and press on run. Select the desired Purchases order that is to be tested. For instance, we selected purchases order number = PO-0103


This is our final output. We got the SGST/CGST rate along with the tax amount. This model mapping can be used in any format mapping to fetch and show the data in reports/print documents.



That's it for this blog. See you in the next blog. In case of any query, please comment, I will be happy to answer the query.

Happy Learning! 
By Neeraj Kumar

Comments

You might love these

Run ER reports using Electronic Messaging Functionality in D365

Bulk Posting of Journals in Dynamics 365 Finance and Operations