web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

x++ code to calculate the cgst and sgst amount for a particular invoice in a sales order

(0) ShareShare
ReportReport
Posted on by

x++ code to calculate the cgst and sgst amount for a particular invoice in a sales order 

The code below is calculating for all invoices in a sales order. But not for a single invoice . Please help.

private void calc_GST(SalesId _invoiceId)
{
TmpTaxDocument tmpTax;
SalesCalcTax PurchCalcTax;
SalesTotals salesTotals;

ITaxableDocument taxableDocument;
ITaxDocumentComponentLineEnumerator lineEnumerator;
ITaxDocument taxDocumentObject;

ITaxDocumentMeasure taxMeasure;
ITaxDocumentMeasureEnumerator taxMeasureEnumerator;
ITaxDocumentMeasureValue partyTaxMeasureValue;
int i;

salesTotals = SalesTotals::construct(SalesTable::find(_invoiceId));
taxableDocument = TaxableDocumentObject::construct(salesTotals.parmTaxableDocumentDescriptor());
taxDocumentObject = TaxBusinessService::calculateTax(taxableDocument);

if (taxDocumentObject)
{
taxTotalGTE = taxDocumentObject.getTotalTax().amountTransactionCurrency();

// Calculation of Tax amount for Tax type GST and Tax component SGST
lineEnumerator = taxDocumentObject.componentLines("GST","SGST");
while (lineEnumerator.moveNext())
{
taxMeasureEnumerator = lineEnumerator.current().measures();
while (taxMeasureEnumerator.moveNext())
{
i++;
if (i == 3)
{
partyTaxMeasureValue = taxMeasureEnumerator.current().value();
SGST += partyTaxMeasureValue.amountTransactionCurrency();
i=0;
break;
}
}
}

// Calculation of Tax amount for Tax type GST and Tax component CGST
lineEnumerator = taxDocumentObject.componentLines("GST","CGST");
while (lineEnumerator.moveNext())
{
taxMeasureEnumerator = lineEnumerator.current().measures();
while (taxMeasureEnumerator.moveNext())
{
i++;
if (i == 3)
{
partyTaxMeasureValue = taxMeasureEnumerator.current().value();
CGST += partyTaxMeasureValue.amountTransactionCurrency();
i=0;
break;
}
}
}

// Calculation of Tax amount for Tax type GST and Tax component IGST
lineEnumerator = taxDocumentObject.componentLines("GST","IGST");
while (lineEnumerator.moveNext())
{
taxMeasureEnumerator = lineEnumerator.current().measures();
while (taxMeasureEnumerator.moveNext())
{
i++;
if (i == 3)
{
partyTaxMeasureValue = taxMeasureEnumerator.current().value();
IGST += partyTaxMeasureValue.amountTransactionCurrency();
i=0;
break;
}
}
}
}

info( strFmt("Total GST value of sales order : %1 " , taxTotalGTE));
if(IGST)
{
info( strFmt("Line IGST value of sales order : %1 " , IGST));
}
else
{
info( strFmt("Line SGST value of sales order : %1 " , SGST));
info( strFmt("Line CGST value of sales order : %1 " , CGST));
}
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Your parameter is a salesId called _invoiceId. Next you provide the construct of the salesTotals with this salesId. I changed your code and indicated the changes in red, but I couldn't test it because I don't have your customizations in my environment. So please try code below in a test/development environment.

    //private void calc_GST(SalesId _invoiceId)
    private void calc_GST(InvoiceId _invoiceId)
    {
    TmpTaxDocument tmpTax;
    SalesCalcTax PurchCalcTax;
    SalesTotals salesTotals;
    ITaxableDocument taxableDocument;
    ITaxDocumentComponentLineEnumerator lineEnumerator;
    ITaxDocument taxDocumentObject;
    ITaxDocumentMeasure taxMeasure;
    ITaxDocumentMeasureEnumerator taxMeasureEnumerator;
    ITaxDocumentMeasureValue partyTaxMeasureValue;
    int i;
    CustInvoiceJour custInvoiceJour;

    select firstonly firstfast parmId from custInvoiceJour
    where custInvoiceJour.InvoiceId == _invoiceId;

    //salesTotals = SalesTotals::construct(SalesTable::find(_invoiceId));
    salesTotals = SalesTotals::construct(SalesTable::find(_invoiceId),SalesUpdate::All,AccountOrder::None,custInvoiceJour.ParmId);
    taxableDocument = TaxableDocumentObject::construct(salesTotals.parmTaxableDocumentDescriptor());
    taxDocumentObject = TaxBusinessService::calculateTax(taxableDocument);

    if (taxDocumentObject)
    {
    taxTotalGTE = taxDocumentObject.getTotalTax().amountTransactionCurrency();

    // Calculation of Tax amount for Tax type GST and Tax component SGST
    lineEnumerator = taxDocumentObject.componentLines("GST","SGST");

    while (lineEnumerator.moveNext())
    {
    taxMeasureEnumerator = lineEnumerator.current().measures();
    while (taxMeasureEnumerator.moveNext())
    {
    i++;
    if (i == 3)
    {
    partyTaxMeasureValue = taxMeasureEnumerator.current().value();
    SGST += partyTaxMeasureValue.amountTransactionCurrency();
    i=0;
    break;
    }
    }
    }

    // Calculation of Tax amount for Tax type GST and Tax component CGST
    lineEnumerator = taxDocumentObject.componentLines("GST","CGST");

    while (lineEnumerator.moveNext())
    {
    taxMeasureEnumerator = lineEnumerator.current().measures();

    while (taxMeasureEnumerator.moveNext())
    {
    i++;
    if (i == 3)
    {
    partyTaxMeasureValue = taxMeasureEnumerator.current().value();
    CGST += partyTaxMeasureValue.amountTransactionCurrency();
    i=0;
    break;
    }
    }
    }

    // Calculation of Tax amount for Tax type GST and Tax component IGST
    lineEnumerator = taxDocumentObject.componentLines("GST","IGST");
    while (lineEnumerator.moveNext())
    {
    taxMeasureEnumerator = lineEnumerator.current().measures();

    while (taxMeasureEnumerator.moveNext())
    {
    i++;
    if (i == 3)
    {
    partyTaxMeasureValue = taxMeasureEnumerator.current().value();
    IGST += partyTaxMeasureValue.amountTransactionCurrency();
    i=0;
    break;
    }
    }
    }
    }
    info( strFmt("Total GST value of sales order : %1 " , taxTotalGTE));

    if(IGST)
    {
    info( strFmt("Line IGST value of sales order : %1 " , IGST));
    }
    else
    {
    info( strFmt("Line SGST value of sales order : %1 " , SGST));
    info( strFmt("Line CGST value of sales order : %1 " , CGST));
    }
    }

  • Community Member Profile Picture
    on at

    Hi  Walters


     I executed the above code in a job. I gave the invoiceid as input  . Sales id as argument for sales totals. Worknig fine.

    Thank you  so much.

     

    static void Jobtest(Args _args)
    {
    TmpTaxDocument tmpTax;
    SalesCalcTax PurchCalcTax;
    SalesTotals salesTotals;
    ITaxableDocument taxableDocument;
    ITaxDocumentComponentLineEnumerator lineEnumerator;
    ITaxDocument taxDocumentObject;
    ITaxDocumentMeasure taxMeasure;
    ITaxDocumentMeasureEnumerator taxMeasureEnumerator;
    ITaxDocumentMeasureValue partyTaxMeasureValue;
    int i;

    CustInvoiceJour custInvoiceJourtax;
    real taxTotalGTE,taxtotal,SGST,CGST,IGST;

    select * from custInvoiceJourtax
    where custInvoiceJourtax.InvoiceId == 'xyzInvoiceid';  //invoice id

    salesTotals = SalesTotals::construct(SalesTable::find('xyzsalesid'),SalesUpdate::All,AccountOrder::None,custInvoiceJour.ParmId);  //salesid
    taxableDocument = TaxableDocumentObject::construct(salesTotals.parmTaxableDocumentDescriptor());
    taxDocumentObject =TaxBusinessService::calculateTax(taxableDocument);
    if (taxDocumentObject)
    {
    taxTotalGTE = taxDocumentObject.getTotalTax().amountTransactionCurrency();
    // Calculation of Tax amount for Tax type GST and Tax component SGST
    lineEnumerator = taxDocumentObject.componentLines("GST","SGST");
    while (lineEnumerator.moveNext())
    {
    taxMeasureEnumerator = lineEnumerator.current().measures();
    while (taxMeasureEnumerator.moveNext())
    {
    i++;
    if (i == 3)
    {
    partyTaxMeasureValue = taxMeasureEnumerator.current().value();
    SGST += partyTaxMeasureValue.amountTransactionCurrency();
    i=0;
    break;
    }
    }
    }
    // Calculation of Tax amount for Tax type GST and Tax component CGST
    lineEnumerator = taxDocumentObject.componentLines("GST","CGST");
    while (lineEnumerator.moveNext())
    {
    taxMeasureEnumerator = lineEnumerator.current().measures();
    while (taxMeasureEnumerator.moveNext())
    {
    i++;
    if (i == 3)
    {
    partyTaxMeasureValue = taxMeasureEnumerator.current().value();
    CGST += partyTaxMeasureValue.amountTransactionCurrency();
    i=0;
    break;
    }
    }
    }
    // Calculation of Tax amount for Tax type GST and Tax component IGST
    lineEnumerator = taxDocumentObject.componentLines("GST","IGST");
    while (lineEnumerator.moveNext())
    {
    taxMeasureEnumerator = lineEnumerator.current().measures();
    while (taxMeasureEnumerator.moveNext())
    {
    i++;
    if (i == 3)
    {
    partyTaxMeasureValue = taxMeasureEnumerator.current().value();
    IGST += partyTaxMeasureValue.amountTransactionCurrency();
    i=0;
    break;
    }
    }
    }
    }
    info( strFmt("Total GST value of sales order : %1 " , taxTotalGTE));
    if(IGST)
    {
    info( strFmt("Line IGST value of sales order : %1 " , IGST));
    }
    else
    {
    info( strFmt("Line SGST value of sales order : %1 " , SGST));
    info( strFmt("Line CGST value of sales order : %1 " , CGST));
    }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 16

#2
Nagendra Varma K Profile Picture

Nagendra Varma K 4

#2
Harisgillani Profile Picture

Harisgillani 4

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans