Using SysOperationFramework
Create DataContract classs - We can define parameters in this class.
[DataContractAttribute]
class Ava_SysOperationImportFreeTxDataContract
{
Name name;
BirthDate birthDate;
FilenameOpen Filename;
MonthsOfYear monthThatWontBeSerialized;
}
//getting birthdate as parameter
//this parameter won't display in the form since no [DataMemberAttribute] is defined for this method
public BirthDate parmBirthDate(BirthDate _birthDate = birthDate)
{
birthDate = _birthDate;
return birthDate;
}
//getting file name as parameter
[DataMemberAttribute]
public FilenameOpen parmFilePath(FilenameOpen _Filename = Filename)
{
Filename = _Filename;
return Filename;
}
Create Service Class - I
t’s not a good practice to keep whole business logic in one
controller class. Because it will be a big responsibility for a single class to
handle.Thatswhy here we have created a Service class which is referenced in
Controller class.
class Ava_SysOperationImportFreeTextService extends SysOperationServiceBase
{
}
//Method for reading CSV file and stored into CustInvoiceTable and CustInvoiceLines table
public void Ava_FreeTextInvoiceImportfromCSV()
{
//public void Ava_FreeTextInvoiceImportfromCSV(Ava_SysOperationImportFreeTxDataContract _contract)
Dialog dialog = new Dialog();
DialogField
dialogField;
AsciiIo importFile;
str filePath,fileNameOnly;
filetype type;
container record, offsetAccount;
str Delimiter = ",";
int totalRecords, dimCount;
InventDim InventDim;
CustInvoiceTable custInvoiceTable, custInvoiceTableSel;
CustInvoiceLine custInvoiceLine;
CustTable custTable;
Map customerFTI = new Map(Types::String, Types::String);
MapEnumerator mapEnum;
LineNum lineNum;
DimensionAttribute dimAttr;
DimensionAttributeSetItem dimAttrSetItem;
DimensionEnumeration dimensionSetId;
Container combinedContainer;
CustAccount custAccount;
Counter counter;
DimensionAttributeValue dimAttributeValue;
LedgerDimensionDefaultAccount ledgerDimension;
container conAttr;
container conValue;
DimensionAttributeValueSetStorage valueSetStorage = new DimensionAttributeValueSetStorage();
DimensionDefault result;
DimensionAttribute dimensionAttribute;
DimensionAttributeValue dimensionAttributeValue;
str dimValue;
DimensionDefault defaultDimension;
MainAccount MainAccount;
DimensionAttributeValueCombination dimensionAttributeValueCombination;
Ava_InterfaceParameters interfaceParameters;
str60 invoiceId;
FileIOPermission readPermission;
Filename fileName = "Momentum RC Jan 2016.CSV";
TaxGroupHeading taxGroupHeading;
TaxGroup taxGroup;
Notes invalidValues;
select interfaceParameters;
readPermission = new FileIOPermission(interfaceParameters.FilePath + "\\" + fileName, 'R'); //#io_read);
readPermission.assert();
importFile = new AsciiIo(interfaceParameters.FilePath + "\\" + fileName, 'R');
if((!importFile) || (importFile.status() != IO_Status::Ok))
{
warning("Error in opening import file");
throw(Exception::Error);
}
importFile.inFieldDelimiter(Delimiter);
if((!importFile) || (importFile.status() != IO_Status::Ok))
{
warning("Error in opening log file");
throw(Exception::Error);
}
try
{
record = importFile.read();
while(importFile.status() == IO_Status::Ok)
{
record = importFile.read();
if(!record)
break;
if(!interfaceParameters.CustAccount || !interfaceParameters.MainAccountNum)
{
error("Customer account and Main account should not be empty");
throw(Exception::Error);
}
ttsbegin;
invoiceId = conPeek(record,1);
select RecId from custInvoiceTableSel
where custInvoiceTableSel.AvaExternalInvoiceId==invoiceId;
if(!custInvoiceTableSel.RecId)
{
taxGroup=conPeek(record,5);
select TaxGroup from taxGroupHeading
where taxGroupHeading.TaxGroup==taxGroup;
if(!taxGroupHeading.TaxGroup)
{
invalidValues += "Invalid Sales tax group for invoice id " + invoiceId;
}
else
{
//error("Invoice has alredy been imported");
//throw(Exception::Error);
totalRecords = totalRecords + 1;
custInvoiceTable.clear();
custTable = CustTable::find(interfaceParameters.CustAccount);
if (custTable.RecId != 0)
{
custInvoiceTable.initValue();
custInvoiceTable.AvaExternalInvoiceId=conPeek(record,1);
custInvoiceTable.InvoiceDate = str2Date(conPeek(record,3),213);//systemDateGet();
custInvoiceTable.initFromCustTable(custTable);
custInvoiceTable.initFromTaxItemGroup();
custInvoiceTable.DlvTerm = custTable.DlvTerm;
custInvoiceTable.setIntrastatDestinationData();
/*custInvoiceTable.OrderAccount = custTable.AccountNum;
custInvoiceTable.modifiedField(fieldNum(CustInvoiceTable, OrderAccount));
custInvoiceTable.InvoiceId = customerInvoices.InvoiceId;*/
custInvoiceTable.insert();
lineNum = 0;
customerFTI.insert(custTable.AccountNum, custInvoiceTable.InvoiceAccount);
}
if (custInvoiceTable.RecId != 0)
{
custInvoiceLine.clear();
custInvoiceLine.initValue();
custInvoiceLine.initFromCustInvoiceTable(custInvoiceTable);
//get customer account number
custAccount = custTable.AccountNum;
counter = 1; //Dimension starting point
combinedContainer = conNull();
dimAttributeValue = null;
custInvoiceLine.Description = conPeek(record,8);
custInvoiceLine.TaxGroup = conPeek(record,5);
custInvoiceLine.TaxItemGroup = "ALL";
custInvoiceLine.Quantity = conPeek(record,6);
custInvoiceLine.UnitPrice = conPeek(record,7);
custInvoiceLine.AmountCur = custInvoiceLine.Quantity * custInvoiceLine.UnitPrice;
/*custInvoiceLine.AmountCur = Currency::curAmount(123 * nextLineNum,
custInvoiceTable.CurrencyCode,
custInvoiceTable.InvoiceDate);*/
custInvoiceLine.ParentRecId = custInvoiceTable.RecId;
ledgerDimension = DimensionStorage::getDefaultAccountForMainAccountNum(interfaceParameters.MainAccountNum);
custInvoiceLine.LedgerDimension = ledgerDimension;
if(conPeek(record,4)!='')
{
conAttr += 'GeoCode';
conValue += conPeek(record,4);
select dimAttr
order by Name
where dimAttr.Type != DimensionAttributeType::MainAccount
&& dimAttr.Name=="Geocode"
join RecId from dimAttributeValue
where dimAttributeValue.DimensionAttribute == dimAttr.RecId;
dimAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimAttr, conPeek(record,4));
}
if(conPeek(record,5)!='')
{
conAttr += 'Province';
conValue += conPeek(record,5);
select dimAttr
order by Name
where dimAttr.Type != DimensionAttributeType::MainAccount
&& dimAttr.Name=="Province"
join RecId from dimAttributeValue
where dimAttributeValue.DimensionAttribute == dimAttr.RecId;
dimAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimAttr, conPeek(record,5));
}
if(interfaceParameters.BusinessUnit)
{
conAttr += 'BusinessUnit';
conValue += interfaceParameters.BusinessUnit;
}
if(interfaceParameters.BusinessUnit)
{
conAttr += 'Department';
conValue += interfaceParameters.Department;
}
for (dimCount = 1; dimCount <= conLen(conAttr); dimCount++)
{
dimensionAttribute = dimensionAttribute::findByName(conPeek(conAttr,dimCount));
if (dimensionAttribute.RecId == 0)
{
continue;
}
dimValue = conPeek(conValue,dimCount);
if (dimValue != "")
{
// _createIfNotFound is "true". A dimensionAttributeValue record will be created if not found.
dimensionAttributeValue = dimensionAttributeValue::findByDimensionAttributeAndValue(dimensionAttribute,dimValue,false,true);
// Add the dimensionAttibuteValue to the default dimension
valueSetStorage.addItem(dimensionAttributeValue);
}
}
defaultDimension = valueSetStorage.save();
custInvoiceLine.DefaultDimension = defaultDimension;
custInvoiceLine.LedgerDimension = ledgerDimension;
//LINE NUM LOGIC.
if(!lineNum)
{
lineNum = CustInvoiceLine::lastLineNum_W(custInvoiceLine.ParentRecId);
}
lineNum += 1;
custInvoiceLine.LineNum = lineNum;
custInvoiceLine.insert();
if(str2int(conPeek(record,10)) > 0 || str2int(conPeek(record,10)) < 0) //Discount
{
//get customer account number
custAccount = custTable.AccountNum;
counter = 1; //Dimension starting point
combinedContainer = conNull();
dimAttributeValue = null;
custInvoiceLine.Description = "Discount";
//custInvoiceLine.LedgerDimension = 40300
custInvoiceLine.TaxGroup = conPeek(record,5);
custInvoiceLine.TaxItemGroup = "ALL";
custInvoiceLine.Quantity = conPeek(record,6);
custInvoiceLine.UnitPrice = conPeek(record,10);
custInvoiceLine.AmountCur = custInvoiceLine.Quantity * custInvoiceLine.UnitPrice;
/*custInvoiceLine.AmountCur = Currency::curAmount(123 * nextLineNum,
custInvoiceTable.CurrencyCode,
custInvoiceTable.InvoiceDate);*/
custInvoiceLine.ParentRecId = custInvoiceTable.RecId;
custInvoiceLine.LedgerDimension = ledgerDimension;
custInvoiceLine.DefaultDimension = defaultDimension;
//LINE NUM LOGIC.
if(!lineNum)
{
lineNum = CustInvoiceLine::lastLineNum_W(custInvoiceLine.ParentRecId);
}
lineNum += 1;
custInvoiceLine.LineNum = lineNum;
custInvoiceLine.insert();
}
if(str2int(conPeek(record,12)) > 0 || str2int(conPeek(record,12)) < 0) //Card Fee
{
//get customer account number
custAccount = custTable.AccountNum;
counter = 1; //Dimension starting point
combinedContainer = conNull();
dimAttributeValue = null;
custInvoiceLine.Description = "Card Fee";
//custInvoiceLine.LedgerDimension = 40300
custInvoiceLine.TaxGroup = conPeek(record,5);
custInvoiceLine.TaxItemGroup = "ALL";
custInvoiceLine.Quantity = conPeek(record,6);
custInvoiceLine.UnitPrice = conPeek(record,12);
custInvoiceLine.AmountCur = custInvoiceLine.Quantity * custInvoiceLine.UnitPrice;
/*custInvoiceLine.AmountCur = Currency::curAmount(123 * nextLineNum,
custInvoiceTable.CurrencyCode,
custInvoiceTable.InvoiceDate);*/
custInvoiceLine.ParentRecId = custInvoiceTable.RecId;
custInvoiceLine.LedgerDimension = ledgerDimension;
custInvoiceLine.DefaultDimension = defaultDimension;
//LINE NUM LOGIC.
if(!lineNum)
{
lineNum = CustInvoiceLine::lastLineNum_W(custInvoiceLine.ParentRecId);
}
lineNum += 1;
custInvoiceLine.LineNum = lineNum;
custInvoiceLine.insert();
}
if(str2int(conPeek(record,14)) > 0 || str2int(conPeek(record,10)) < 0) //20% Reseller (Branches)
{
//get customer account number
custAccount = custTable.AccountNum;
counter = 1; //Dimension starting point
combinedContainer = conNull();
dimAttributeValue = null;
custInvoiceLine.Description = "20% Reseller (Branches)";
//custInvoiceLine.LedgerDimension = 40300
custInvoiceLine.TaxGroup = conPeek(record,5);
custInvoiceLine.TaxItemGroup = "ALL";
custInvoiceLine.Quantity = conPeek(record,6);
custInvoiceLine.UnitPrice = conPeek(record,14);
custInvoiceLine.AmountCur = custInvoiceLine.Quantity * custInvoiceLine.UnitPrice;
/*custInvoiceLine.AmountCur = Currency::curAmount(123 * nextLineNum,
custInvoiceTable.CurrencyCode,
custInvoiceTable.InvoiceDate);*/
custInvoiceLine.ParentRecId = custInvoiceTable.RecId;
custInvoiceLine.LedgerDimension = ledgerDimension;
custInvoiceLine.DefaultDimension = defaultDimension;
//LINE NUM LOGIC.
if(!lineNum)
{
lineNum = CustInvoiceLine::lastLineNum_W(custInvoiceLine.ParentRecId);
}
lineNum += 1;
custInvoiceLine.LineNum = lineNum;
custInvoiceLine.insert();
}
}
}
}
ttscommit;
}
info(invalidValues);
mapEnum = customerFTI.getEnumerator();
info(strFmt('Following Free Text Invoices are successfully created.'));
while (mapEnum.moveNext())
{
info(strFmt('Customer Account : %1 , Invoice Account : %2', mapEnum.currentKey(), mapEnum.currentValue()));
}
// Optionally call revertAssert() to limit scope of assert.
CodeAccessPermission::revertAssert();
}
catch(Exception::Error)
{
Throw(Exception::Error);
}
info(strFmt("Total Read Records = %1",totalRecords));
}
Controller Class -
As the name implies
Controller, this class has great responsibility to initiate the operation. This
class holds all the information regarding Execution mode, it should show a
progress form or Dialog.
class Ava_SysOperationImportFreeTextController extends SysOperationServiceController
{
}
//setting caption for Task description for parameter dialog
protected ClassDescription defaultCaption()
{
// This will be the dialog's caption
return 'Importing Free Text Invoices';
}
//new method
protected void new()
{
// This tells the controller what method it should execute as the service. In this case, we'll run SysOperationDemoService.performDemo()
//super(classStr(AvaSysOperationDemoService), methodStr(AvaSysOperationDemoService, performDemo), SysOperationExecutionMode::Synchronous);
super(classStr(Ava_SysOperationImportFreeTextService), methodStr(Ava_SysOperationImportFreeTextService, Ava_FreeTextInvoiceImportfromCSV), SysOperationExecutionMode::Synchronous);
}
//validate your parameters
protected boolean validate()
{
//Ava_SysOperationImportFreeTxDataContract contract;
boolean ret = true;
/*
contract = this.getDataContractObject();
//info(contract.parmFilePath());
if (contract.parmBirthDate() > DateTimeUtil::date(DateTimeUtil::addYears(DateTimeUtil::utcNow(), -18)))
{
// Failing the validate will not close the dialog, and the user will have another chance of inputting the correct values
ret = checkFailed('The user is underage!');
}*/
return ret;
}
//run method
public void run()
{
//info('Run method has been called');
super();
}
//construct method
public static Ava_SysOperationImportFreeTextController construct()
{
Ava_SysOperationImportFreeTextController controller;
controller = new Ava_SysOperationImportFreeTextController();
controller.parmShowDialog(true); // Actually the default value
controller.parmShowProgressForm(false);
return controller;
}
//main method
public static void main(Args _args)
{
Ava_SysOperationImportFreeTextController controller;
controller = Ava_SysOperationImportFreeTextController::construct();
controller.startOperation();
}
you can add action menuitem for this controller class and run the logic.