User Tools

Site Tools


taxengine:integration:code_samples:c_sample_code

Sample Console App in C#

Please see Getting Started for help with using the KampData TaxEngine.

using System;
using System.Runtime.InteropServices;
using System.Collections;
//Add the KDTaxEngine.dll as a reference.
using KampData.TaxEngine.API;
using KampData.TaxEngine.API.Enums;

namespace TaxEngineConsoleApp
{
    /// <summary>
    /// The CSharpClient object is a simple example of how the KampData
    /// TaxEngine can be utilized from a 3rd party application. This sample
    /// is not intended to be comprehensive. It simply illustrates the access
    /// methods.
    /// </summary>
    /// <remarks>To use the KampData TaxEngine in this sample, or in any other 
    /// project, you must first setup the SOP module from the KDControlCenter.exe's 
    /// Transaction Source Module Setup window. Please see "Getting Started"in the 
    /// KDTaxEngine.chm help file for more information.</remarks>
    class CSharpClient
    {
        [STAThread]
        static void Main(string[] args)
        {
            string zip;
            bool abort = false;
            ArrayList cities = new ArrayList();
            ArrayList counties = new ArrayList();
            ArrayList states = new ArrayList();
            string server = @"Microsoft SQL Server";
            string userId = "User";
            string password = "p@ssw0rd";
            string companyDatabase = "TWO";
            string systemDatabase = "DYNAMICS";
            string itemCategory = "FREIGHT";
            Int16 timeOutSeconds = 60;
            bool displayProgessBar = true;
            string loginMessage = "";
            string message = "";
            /// KampData.Portal object must be instantiated.
            Portal txEngn = new Portal();
            try
            {
                try
                {
                    /// The Portal.Login method makes the connection to SQL Server and
                    /// prepares the TaxEngine for use.
                    loginMessage = txEngn.Login(server, userId, password, systemDatabase, companyDatabase, timeOutSeconds, displayProgessBar);
                }
                catch (Exception e)
                {
                    loginMessage = e.Message;
                }
                if (loginMessage != "")
                {
                    abort = true;
                    Console.WriteLine(loginMessage);
                    Console.WriteLine("Press enter to continue");
                    Console.ReadLine(); // to hold window open displaying error message
                }
                if (!abort)
                {
                    /// Set the docType and module using the values you have setup
                    /// in the KDWindowClient.exe's Transaction Source Module Setup 
                    /// and Document Type Setup windows.  In the SOP module, invoices are 
                    /// docType 3.
                    /// (The SOP module can be setup automatically using 
                    /// File > Create SOP in the TE Transaction Source Modules window.)
                    Int16 docType = 3;
                    string module = "SOP";

                    /// You must assign a document number at this point. You can use a
                    /// document number generated by your order entry software
                    /// that will call the TaxEngine, or you can use the following code
                    /// to generate a document number.
                    string docNumber = txEngn.NextDocumentNumber(module);

                    /// Initialize the document.
                    txEngn.Document(docNumber, docType, module);

                    /// DocSaleOrPurchase defaults to Sales, so the following line
                    /// is unnecessary. However, you can set it to Purchases if desired.
                    txEngn.DocSaleOrPurchase = SalesOrPurchases.Sales;

                    /// You would normally set your customer ID at this point along with
                    /// any other document defaults. For purposes of this demonstration,
                    /// we will skip that step. 
                    //txEngn.DocCustomerID = "ADAMPARK0001";

                    /// We now add a line to the order. The addressing and taxation 
                    /// features occur on the line level.  This step is required to 
                    /// instantiate a line object. We recommend using the lineNumber
                    /// as it exists in your order entry system. Doing so ensures that
                    /// the TaxEngine line and the order entry line can be matched and 
                    /// verified.
                    int lineNumber = 1;
                    txEngn.LineAdd(lineNumber);
                    do
                    {
                        Console.WriteLine("Enter an item category. Press enter for {0}", itemCategory);
                        string ic = Console.ReadLine();
                        if (ic != "")
                            itemCategory = ic;
                        if (itemCategory != "")
                        {
                            try
                            {
                                txEngn.LineItemCategory = itemCategory;
                                Console.WriteLine("Item category has been set to {0}", txEngn.LineItemCategory);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                                if (txEngn.LineItemCategory == "")
                                    Console.WriteLine("Item category is blank");
                                else
                                    Console.WriteLine("Item category is {0}", txEngn.LineItemCategory);
                                Console.WriteLine("");
                            }
                        }
                        /// Select a ZIP code.
                        Console.WriteLine("Enter a ZIP code");
                        zip = Console.ReadLine();
                        if (zip == "" || zip.ToUpper() == "EXIT")
                            break;
                        try
                        {
                            txEngn.LineZip = zip;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                            Console.WriteLine("");
                            continue;
                        }
                        // Fetch the default tax schedule for this zip.
                        txEngn.LineTaxScheduleSelect();
                        /*
                        * At this point, the Portal object will have identified and made
                        * available the default city, county and state for the ZIP provided.
                        * Also, the default tax schedule will be identified, and made available.
                        * The following call prints these defaults:
                        */
                        Console.WriteLine("Count found: {0} Default: {1}, [{2}] {3} {4}",
                        txEngn.LineAddressesInZip(), txEngn.LineCity, txEngn.LineCounty, txEngn.LineState, txEngn.LineZip);
                        /*
                        * Some ZIP codes are valid for more than one city, county and or state.
                        * The Portal object can provide ArrayLists of the available choices.
                        * The ArrayLists include:
                        * All States in the current ZIP.
                        * All Cities in the current state and ZIP.
                        * All Counties in the current city, state and ZIP.
                        * 
                        * For example, if you change the city, then the list of possible counties
                        * may changes.
                        * 
                        * You select the state, city and county in the same manner that you select
                        * the ZIP. (ie txEngn.LineCity = "New Orleans";)
                        */
                        if (txEngn.LineAddressesInZip() > 1)
                        {
                            states = txEngn.LineStatesInZip();
                            Console.WriteLine("States in ZIP:");
                            foreach (string state in states)
                            {
                                Console.WriteLine(" State of {0}", state);
                            }
                            if (states.Count > 1)
                            {
                                Console.WriteLine("Enter a state. Press enter for {0}", txEngn.LineState);
                                string state = Console.ReadLine();
                                if (state != "")
                                    txEngn.LineState = state;
                            }
                            // Fetch the default tax schedule for this state and zip.
                            txEngn.LineTaxScheduleSelect();
                            cities = txEngn.LineCitiesInStateAndZip();
                            Console.WriteLine("Cities in {0} state and {1} ZIP:",
                            txEngn.LineState, txEngn.LineZip);
                            foreach (string city in cities)
                            {
                                Console.WriteLine(" City of: {0}", city);
                            }
                            if (cities.Count > 1)
                            {
                                bool cityChosen = false;
                                while (!cityChosen)
                                {
                                    Console.WriteLine("Enter a city. Press enter for {0}", txEngn.LineCity);
                                    string city = Console.ReadLine();
                                    if (city == "")
                                    {
                                        cityChosen = true; //Accepting the current value
                                    }
                                    else
                                    {
                                        try
                                        {
                                            txEngn.LineCity = city;
                                            cityChosen = true;
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                        }
                                    }
                                }
                            }
                            // Fetch the default tax schedule for this city state and zip.
                            txEngn.LineTaxScheduleSelect();
                            counties = txEngn.LineCountiesInState();
                            Console.WriteLine("Counties in {0}:", txEngn.LineState);
                            foreach (string county in counties)
                            {
                                Console.WriteLine(" County of {0}", county);
                            }
                            if (counties.Count > 1)
                            {
                                bool countyChosen = false;
                                while (!countyChosen)
                                {
                                    Console.WriteLine("Enter a county. Press enter for {0}", txEngn.LineCounty);
                                    string county = Console.ReadLine();
                                    if (county == "")
                                    {
                                        countyChosen = true; //Accepting the current value
                                    }
                                    else
                                    {
                                        try
                                        {
                                            txEngn.LineCounty = county;
                                            countyChosen = true;
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                        }
                                    }
                                }
                            }
                        }
                        /// In city limits is assumed. This line exists here only to 
                        /// illustrate that this option can be set after the city has
                        /// been specified.
                        txEngn.LineInCityLimits = true;
                        txEngn.LineTaxScheduleSelect();
                        Console.WriteLine("Tax schedule [{0}] {1} Rate: {2}",
                            txEngn.LineTaxScheduleID, txEngn.LineTaxScheduleDescription,
                            txEngn.LineInitialTaxPercent);
                        txEngn.LineItemUnitPrice = 100;
                        txEngn.LineItemQuantity = 1;
                        txEngn.DocCalculateTax();
                        Console.WriteLine();
                        message = "ID";
                        message = message.PadRight(80);
                        message = message.Insert(17, "Description");
                        message = message.Insert(48, "Total");
                        message = message.Insert(58, "Taxable");
                        message = message.Insert(68, "Tax");
                        message = message.Trim();
                        Console.WriteLine(message);
                        // This code displays the Microsoft Dynamics GP tax details
                        // that were used.
                        foreach (TrxTaxDetail trxDtl in txEngn.DocTaxDetails(TaxDetails.Transaction))
                        {
                            message = trxDtl.ID;
                            message = message.PadRight(80);
                            message = message.Insert(17, trxDtl.Description);
                            message = message.Insert(48, trxDtl.TotalAmount.ToString());
                            message = message.Insert(58, trxDtl.TaxedAmount.ToString());
                            message = message.Insert(68, trxDtl.TaxAmount.ToString());
                            message = message.Trim();
                            Console.WriteLine(message);
                        }
                        Console.WriteLine();
                    }
                    while (zip != "" && zip.ToUpper() != "EXIT");
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
                Console.WriteLine("Press enter to continue");
                Console.ReadLine(); // to hold window open displaying error message
            }
        }
    }
}

Sample Code
Sample Code in Dexterity

taxengine/integration/code_samples/c_sample_code.txt · Last modified: 2015/08/06 20:00 by chuck

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki