Tax amount override in free text invoices

Introduction

One of the common issues we encounter when importing free text invoices into Dynamics AX is overriding the tax. Usually AX calculates the tax when sales invoice lines are inserted into the free text invoice. On some occasions, such as when integrating AX with external systems, users will want the tax calculated by AX to be overridden with the value form the external system so that the tax in  both system matches to the penny.

Override the tax manually

To override tax manually in free text invoices, go to Sales ledger/Common/Free text invoices/All free text invoices. Choose one of the free text invoices that has not been posted yet (e.g. no invoice Id was assigned). Then from the action pane, click on VAT under Details group. This will open a form that will look like this…

B1

Click on the Adjustment tab and you will see the following:

B2

In the form above, you can tick the “Override calculated VAT” field and then set the new tax value in the “Actual VAT amount” text box. Before you close the form, click apply.

Override the tax in x++

We will do the same exercise but from x++ code. There are many blogs and articles on how to do this but in order not to jump into different websites to get the code, I copied the code from a random website here below and then added my code afterwards.

So, the original code will look something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static void FreeTextTaxAdjust(Args _args)  
{  
  CustInvoiceTable    custInvoiceTable;  
  CustInvoiceCalcTax  custInvoiceCalcTax;  
  TaxFreeInvoice      taxFreeInvoice;  
  TaxRegulation       taxRegulation;  
  ;  
 
  ttsbegin;  
 
  select firstonly custInvoiceTable  
  where custInvoiceTable.RecId == 54371652769;  
 
  custInvoiceCalcTax = new CustInvoiceCalcTax_Table(custInvoiceTable);  
  taxFreeInvoice     = new TaxFreeInvoice(custInvoiceCalcTax);  
  taxFreeInvoice.calc();  
  taxRegulation = TaxRegulation::newTaxRegulation(taxFreeInvoice);  
  taxRegulation.allocateAmount(2.01);  
  taxRegulation.saveTaxRegulation();  
 
  ttscommit;  
}

As shown in the code above, this will override the total tax amount of the free text invoice from the automatically calculated £2 to £2.01. The issue with this is that AX will merge the taxes by the VAT code. So, if in the form above we had two tax codes (STD and RED) instead of only one tax code (STD), then the code above will not handle it. The code above will only allow inserting the total tax amount for both tax codes. It will not allow splitting the values for each tax code.

I managed to fix the issue by splitting the above into two steps:

  1. A method to get the tax code. In my case, I cross reference the Tax Group with the Tax Item Group to get the tax code. This can be done by using the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static TaxCode CWFGetCrossRefTaxCode(TaxGroup _taxGroup, 
                                            TaxItemGroup _taxItemGroup)
{
    TaxOnItem           taxOnItem;
    TaxGroupData        taxGroupData;
    TaxTable            taxTable;
    ;
 
    select firstonly TaxCode from taxTable
    join firstonly TaxGroup, TaxCode from taxGroupData
    where taxGroupData.TaxCode == taxTable.TaxCode
       && taxGroupData.TaxGroup == _taxGroup
    join firstonly TaxItemGroup, TaxCode from taxOnItem
    where taxOnItem.TaxCode == taxTable.TaxCode
       && taxOnItem.TaxItemGroup == _taxItemGroup;
 
    return taxTable.TaxCode;
}
  1. Use the tax code from the method above and pass it to the method below along with the new tax amount:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public static void CWFCalculateTax(TaxCode _taxCode, 
                                   TaxAmount _taxAmount, 
                                   CustInvoiceTable _custInvoiceTable)
{
    CustInvoiceCalcTax          custInvoiceCalcTax;
    TaxFreeInvoice              taxFreeInvoice;
    TaxRegulation               taxRegulation;
    TmpTaxWorkTrans             tmpTaxWorkTrans;
    TmpTaxRegulation            tmpTaxRegulation;
    ;
 
    ttsbegin;
 
    custInvoiceCalcTax = new CustInvoiceCalcTax_Table(_custInvoiceTable);
    taxFreeInvoice     = new TaxFreeInvoice(custInvoiceCalcTax, true, true, true);
    taxFreeInvoice.calcTax();
 
    taxRegulation = TaxRegulation::newTaxRegulation(taxFreeInvoice);
    tmpTaxRegulation = taxRegulation.tmpTaxRegulation();
    tmpTaxWorkTrans = taxRegulation.tmpTaxWorkTrans();
 
    while select tmpTaxRegulation
    where tmpTaxRegulation.TaxCode == _taxCode
    {
        tmpTaxRegulation.SourceRegulateAmountCur = _taxAmount;
        tmpTaxRegulation.OverrideCalculatedTax = NoYes::Yes;
        tmpTaxRegulation.update();
    }
 
    while select tmpTaxWorkTrans
        where tmpTaxWorkTrans.TaxCode == _taxCode
    {
        tmpTaxWorkTrans.SourceRegulateAmountCur = _taxAmount;
        tmpTaxWorkTrans.update();
    }
 
    taxRegulation.setTmpTaxWorkTransTmpData(tmpTaxWorkTrans);
    taxRegulation.saveTaxRegulation();
    ttscommit;
}

With the code above, I managed to override the tax amount for multiple tax codes only when I’ve updated the table TmpTaxWorkTrans.

Happy DAXing!

Debugging XSLT in Visual Studio

I was tasked with modifying an XSLT file to allow a bank statement from a UK bank to be imported into Dynamics AX 2013 R3.  I setup the standard import within Dynamics to be able to do this, but found that the sample I was given would not import.  I needed to do some debugging on the Transform in Visual Studio and here is how I went about it.

The import of a bank statement usually goes through a two stage transform, with the first used to convert the banks typical CSV format to an XML file that can be easily transformed by XSLT and the second to convert the XML into XML formatted for AX. The CSV to XML transform is fairly simplistic with most of the complexity of transform reserved for the second transform and its here that I needed to do some debugging.

I found that In order to debug BAI2 CSV files in Visual Studio there are 2 things initially that were required:

  1. The XSLT Document
  2. The XML of the BAI2 CSV that is created by Dynamics

The creation XML of the CSV file from Dynamics happens upon import of the bank statement during the first CSV to XML transform and the file gets created in:

  • <Your Machines Program Files Folder>\Microsoft Dynamics AX\60\Server\<Your Dynamics Application Name>\bin\Application\Appl\Standard\Tmp

All that happens in this process is that the CSV detail gets put into a CDATA section for processing, so this could be done manually by adding:

<?xml version=”1.0″ encoding=”us-ascii”?>

<Batch><![CDATA[<CSV CONTENT HERE> </Batch>

And saving the file as .xml

Once inside Visual Studio, open the XSLT file first by going to File > open > open file

Once the file is open, open the properties for the file by going to view > properties or pressing f4 when the XSLT file tab is selected

Grab

 

Inside the property window, select an input which will be the CSVXML file described above, and an output file where the results of the transform will be created (This file will be created for you)

Once this is setup, you can now begin the debug by creating a breakpoint within the XSLT, and stepping through will show the output being created in the file you selected as the output file.

In my case there were two steps to the Statement import, so a second XSLT (BAI2XML-to-Reconciliation) had to be loaded and the input property was set to the output from the first transform, setting a new output file for the result.

Troubleshooting Full IL build in AX 2012 R3

I have recently came across a problem when I tried to activate an AIF port. The deployment failed and I got the following error..

ActivationError

So I started the incremental IL build, couple of minutes after I got an info log message saying all is well…

2014-12-05_0919

…but when I tried to re-activate the port I got the same error and incremental CIL build has actually made no difference.

I had no option left but to start full IL build, assuming that this will fix the problem, but surprisingly AX finishes the IL build process in less than a minute (usually takes between 30-60 minutes) and there is no indication of the progress, nor any info log or warning message. Something is clearly amiss!

Solution:

I performed the following steps to resolve the above problem

  1. Compile the application (I used AxBuild, much faster than the traditional compile from development work space), Resolve any errors after the compile
  2. Stop the AOS service
  3. Delete all the files and folders within the XppIL folder on the AOS server folder
  4. Run the following statement against the model store database
    TRUNCATE TABLE SysXppAssembly
  5. Start the AOS service
  6. Run the Full IL build

Once I had done this I was able to deploy the AIF port without any problems.

There is a nice article on msdn blogs explaining the IL build process

http://blogs.msdn.com/b/axsupport/archive/2011/11/07/il-compiles-explained.aspx