AX – D365FO – Calculate Withholding tax on a Sales order – X++

This is an example job of how to calculate withholding tax for a Sales order

class WithholdingtaxJob
{
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {

        SalesLine               salesLine;
        Map                     mapOfTaxesToCalculate = new Map(Types::Container, Types::Real);
        TaxWithholdOnItem_TH    taxWithholdOnItem_TH;
        TaxWithholdGroupData    taxWithholdGroupData;
        TaxWithholdBase         taxWithholdBase;
        container               key;
        MapEnumerator           mapEnumerator;
        TaxWithholdCode         taxWithholdCode;
        TaxWithholdTable        taxWithholdTable;
        TaxWithholdData         taxWithholdData;
        TmpTaxWithhold          tmpTaxWithhold;
        AmountMST 		taxBaseAmount;
        AmountMST		excludedAmount;


        while select salesLine
            where salesLine.SalesId == 'test'
        {
            while select TaxWithholdCode
                from taxWithholdOnItem_TH
                where taxWithholdOnItem_TH.TaxWithholdItemGroupHeading_TH == salesLine.TaxWithholdItemGroupHeading_TH
                join TaxWithholdGroup from taxWithholdGroupData
                where taxWithholdGroupData.TaxWithholdGroup == salesLine.TaxWithholdGroup
                && taxWithholdOnItem_TH.TaxWithholdCode == taxWithholdGroupData.TaxWithholdCode
            {
                key = [taxWithholdGroupData.TaxWithholdGroup, taxWithholdOnItem_TH.TaxWithholdCode];

                if (!mapOfTaxesToCalculate.exists(key))
                {
                    mapOfTaxesToCalculate.insert(key, salesLine.LineAmount);
                }
                else
                {
                    taxWithholdBase = mapOfTaxesToCalculate.lookup(key);
                    taxWithholdBase += salesLine.LineAmount;

                    mapOfTaxesToCalculate.insert(key, taxWithholdBase);
                }
            }
        }

        mapEnumerator = mapOfTaxesToCalculate.getEnumerator();

        while (mapEnumerator.moveNext())
        {
            taxWithholdCode = conPeek(mapEnumerator.currentKey(), 2);
            taxBaseAmount = mapEnumerator.currentValue();

            taxWithholdTable = TaxWithholdTable::find(taxWithholdCode);

            taxWithholdData = TaxWithholdData::find(taxWithholdTable.TaxWithholdCode,
                today(),
                taxBaseAmount);
            if(taxWithholdData)
            {
                tmpTaxWithhold.TaxWithholdCode = taxWithholdTable.TaxWithholdCode;
                tmpTaxWithhold.TaxWithholdValue = taxWithholdData.TaxWithholdValue;
                tmpTaxWithhold.TaxWithholdBase = taxBaseAmount;

                tmpTaxWithhold.TaxWithholdRoundOff     = taxWithholdTable.TaxWithholdRoundOff;
                tmpTaxWithhold.TaxWithholdRoundOffType = taxWithholdTable.TaxWithholdRoundOffType;
                tmpTaxWithhold.TaxWithholdExclude      = taxWithholdData.TaxWithholdExclude;
                excludedAmount = taxBaseAmount * tmpTaxWithhold.TaxWithholdExclude/100;
                tmpTaxWithhold.TaxWithholdAmount = (taxBaseAmount - excludedAmount) * tmpTaxWithhold.TaxWithholdValue / 100;

                tmpTaxWithhold.insert();
            }
        }

        while select tmpTaxWithhold
        {
            info(strFmt('Code: %1, Aliquot: %1, Base amount: %3, Tax Amount: %4',
                tmpTaxWithhold.TaxWithholdCode,
                tmpTaxWithhold.TaxWithholdValue,
                tmpTaxWithhold.TaxWithholdBase,
                tmpTaxWithhold.TaxWithholdAmount));
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s