User Tools

Site Tools


taxengine:integration:code_samples:dex_sample_code

Sample Code in Dexterity

The attached text file is a script designed to illustrate how to utilize the KampData TaxEngine API in Dexterity applications.

IMPORTANT: The KDTaxEngine.dll must be converted into a KDTaxEngine.tbl file for adding the KampData TaxEngine to your Base » Libraries as a COM Type Library. Please see Accessing the TaxEngine through COM for step by step instructions.

NOTE: DynamicZip has an external procedure you can call from your Dexterity code. Please see Calling DynamicZip from another 3rd party product for more information.

Procedure	"KampDataTaxEngineSampleCall"
{
	Core	"System"
	Script	"{
/// The SampleCallToDll 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.
}

local string zip;
local boolean terminate; 
local string city, cities[10];
local string county, counties[10];
local string state, states[10];
local text textMessage, loginMessage;
local integer iteration, stateCount, cityCount, countyCount, docType, lineNumber, 
timeOutSeconds;
local KDTaxEngine.Portal txEngn;
local string message, server, userId, password, companyDatabase,
	docNumber, module;
local boolean displayProgressBar, useSqlPassword;

server = \"SqlServer\\Instance\";
userId = \"sa\"; 
password = \"p@ssw0rd\"; 
companyDatabase = \"TWO\"; 
timeOutSeconds = 60;
displayProgressBar = true;
useSqlPassword = true;
{Retrieve a reference to the instance of an object if it exists in the running object table.
 As a general rule, each instance of the TaxEngine should be used by only one process as it
 points at only one document at a time. Therefore, COM_GetObject should probably not be used.
txEngn = COM_GetObject(\"KDTaxEngine.Portal\");}

{ KampData.EntryPoint object must be instantiated. }
{If a reference was not retrieved, then create a new instance}
if txEngn = null then
	try
		txEngn = new KDTaxEngine.Portal();
	else
		error \"An exception occurred creating the KDTaxEngine.Portal object. Class: \" + 
			str(Exception_Class()) + \" Subclass: \" + 
			str(Exception_SubClass()) + \" Message:\" + Exception_Message();		
	end try;
	{Verify that object exists and abort script if not.}
	if(txEngn = null) then
		warning \"Unable to create KDTaxEngine.Portal object\";
		abort script;
	end if;
end if;

try
	{ The TaxEngine.Login method connects to SQL Server and prepares the tax
	   engine for use. }
	loginMessage = txEngn.Login(server, userId, password, companyDatabase,
	timeOutSeconds, displayProgressBar, useSqlPassword);
	{ If there is any problem logging in, a message will be returned - or
	  an error thrown - depending on the problem. }
	if loginMessage = \"\" then
		terminate = false;
	else
		terminate = true;
		warning loginMessage;
	end if;
else
	terminate = true;
	warning(Exception_Message());
end try;

if (not terminate) then
	{ A document needs to be created (even if never saved) in the TaxEngine
	   to provide default information. If the docNumber and module are not
	   blank and the docType > -1 a record will be reserved in the KDTrxDoc table
	   on SQL Server for the document. For purposes of this sample, we will not
	   create a document that results in a reserved record. }
	docNumber = \"\";
	docType = -1;
	module = \"SOP\";
	{The DocSaleOrPurchase property defaults to the Transaction Type 
     used when creating the sourceModule in the KDControlCenter.exe.  
     Setting this property is only necessary when you are either 
     NOT using a sourceModule or when using a Transaction Type that is 
     different from the Transaction Type that the sourceModule was 
     created for.}
    txEngn.DocSaleOrPurchase = KDTaxEngine.SalesOrPurchases_Sales;
	txEngn.Document_2(docNumber, docType, module);
	{ At this point you would normally set the customer id and any other document
	  defaults that may be appropriate.\\
	  For example: }
	{txEngn.DocCustomerID = customerId;}
	{ An order line must be created at this point since the addressing and taxation
	  occur on a line by line basis. }
	lineNumber = 1;
	txEngn.LineAdd(lineNumber);
	repeat
		{ The first step is to provide the zip code. }
		if not getstring(\"Enter a zip code\",false , zip) then
			abort script; {user pressed Cancel}
		end if;
		if (zip = \"\") then
			exit repeat;
		end if;
		try
			txEngn.LineZip = zip;
		else
			warning(Exception_Message());
			continue repeat;
		end try;
		
		{
		/*
		 * At this point the TaxEngine 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:
		 */
		   The EntryPoing method AddressesInZip returns a count of addresses found
		}
		warning(\"Count found: \" + string(txEngn.LineAddressesInZip()) + 
			\" Default: \" + txEngn.LineCity + \" [\" + txEngn.LineCounty + \"] \" + 
			txEngn.LineState + \" \" +  txEngn.LineZip);

		{
		/*
		 * Some ZIP codes are valid for more than one city, county and or state.
		 * The TaxEngine 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, the list of possible counties
		 * may changes.
		 * 
		 * You select the state, city and county in the same way that you select
		 * the ZIP. (ie txEngn.City = \"New Orleans\";)
		 *
		 * Dexterity does not support ArrayLists, therefore the TaxEngine object
		 *  contains methods that provide access to the ArrayList elements. For
		 *  example, txEngn.StateInZip(0) will return the state code of the first
		 *  element of the ArrayList txEngn.StatesInZip. The code below contains
		 *  examples of creating a local Dexterity Array by stepping through the 
		 *  elements of the ArrayList. A .NET ArrayList is zero based so the
		 *  first element has an index of 0. The first element in a Dexterity 
		 *  Array has an index of 1. For this reason we fetch the first element
		 *  using (iteration - 1) and then store it in the Dexterity Array as
		 *  [iteration]. For this sample, we have limited the number of cities,
		 *  counties or states to 10. At the time of this writing, our current
		 *  Zip Address table has a maximum of 30 cities in a single zip. That
		 *  zip code being 41465 in Kentucky. The table has a maximum of 6 counties
		 *  in three different zips. Those zip codes being 39573, 68823 and 79720. 
		 *  There are a maximum of 2 states in three different zip codes. Those zip 
		 *  codes are 71749, 72761 and 85534.
		 */
		 }
		if (txEngn.LineAddressesInZip() > 1) then
			
			state = txEngn.LineState;
			
			cityCount = txEngn.LineCitiesInStateAndZipCount();
			for iteration equals 1 to cityCount do
				{ The TaxEngine method CityInStateAndZip returns a city name for the
				   zero based index specified. For example txEngn.CityInStateAndZip(0) will
				   return the city name of the first city that the zip code can
				   refer to. (The majority of zip codes do not cross city lines
				   and therefore there will only be one city name to retrieve.}
				city = txEngn.LineCityInStateAndZip(iteration - 1);
				if city = \"\" then
					exit for;
				end if;
				cities[iteration] = city;
				cityCount = iteration;
			end for;
			
			assert cityCount > 0;
			textMessage = \"Cities in \" + txEngn.LineState + \" state and \" + txEngn.LineZip + \" ZIP:\";
			for iteration equals 1 to cityCount do
				if iteration > 1 then
					textMessage = textMessage + \",\";
				end if;
				textMessage = textMessage + \" \" + cities[iteration];
			end for;

			if (cityCount > 1) then
				warning textMessage;
				city = txEngn.LineCity;
				message = \"Enter a city. Press enter for \" + city;
				if not getstring(message, false, city) then
					abort script; {user pressed Cancel}
				end if;
				if (city <> txEngn.LineCity) then
				
					try
					
						txEngn.LineCity = city;
					
					else
					
						warning(Exception_Message());
					
					end try;
				end if;
			end if;
			
			countyCount = txEngn.LineCountiesInCityStateAndZipCount();
			for iteration equals 1 to countyCount do
				{ The TaxEngine method CountyInCityStateAndZip returns a county name for the
				   zero based index specified. For example txEngn.CountyInCityStateAndZip(0) will
				   return the county name of the first county that the zip code can
				   refer to. (The majority of zip codes do not cross county lines
				   and therefore there will only be one county name to retrieve.}
				try
					county = txEngn.LineCountyInCityStateAndZip(iteration - 1);
				else
					exit for;
				end try;
				if county = \"\" then
					exit for;
				end if;
				counties[iteration] = county;
				countyCount = iteration;
			end for;
			
			assert countyCount > 0;
			textMessage = \"Counties in \" + txEngn.LineCity + \", \" + txEngn.LineState + \" \" + txEngn.LineZip;
			for iteration equals 1 to countyCount do
				if iteration > 1 then
					textMessage = textMessage + \",\";
				end if;
				textMessage = textMessage + \" \" + counties[iteration];
			end for;
			
			if (countyCount > 1) then
				warning textMessage;
				county = txEngn.LineCounty;
				message = \"Enter a county. Press enter for \" + county;
				if not getstring(message, false, county) then
					abort script; {user pressed Cancel}
				end if;
				if (county <> txEngn.LineCounty) then
				
					try
					
						txEngn.LineCounty = county;
					
					else 
					
						warning(Exception_Message());
					end try;
				end if;
			end if;
		end if;
		txEngn.LineInCityLimits = true;
		if txEngn.DocReselectionRequired then
           txEngn.LineTaxScheduleSelect();
		end if;
		warning(\"Tax schedule [\" + txEngn.LineTaxScheduleID + \"] \" + txEngn.LineTaxScheduleDescription +
			\" Rate: \" + string(txEngn.LineInitialTaxPercent));
	until (zip = \"\");
end if;
"
}

Sample Code
Frequently Asked Questions

taxengine/integration/code_samples/dex_sample_code.txt · Last modified: 2015/06/29 23:06 by conni