Thursday, 12 March 2015

Chart of Account Import using CSV file

public class ChartOfAccountsMasterImport extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    DialogField                 dialogFilename,dialogChartOfAccountsName;
    str                         ChartOfAccountsName;
    #define.CurrentVersion(2)
    #localmacro.CurrentList
    filename,
    insertIncorrectRecords,
    #endmacro
    #localmacro.ListVersion1
    filename,
    insertIncorrectRecords
    #endmacro
    #File
}

public Object dialog()
{
    DialogRunbase       dialog = super();
    ;
    dialogFilename  = dialog.addField(extendedTypeStr(FilenameOpen));
    dialogFilename.value(filename);
    dialogChartOfAccountsName = dialog.addField(extendedTypeStr("Name"), "Chart of account name");
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    return dialog;
}

public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    ChartOfAccountsName     = diALOGChartOfAccountsName.value();
    return true;
}
void run()
{
    ChartOfAccountsService                      chartOfAccountsService;
    MainAccountContract                         mainAccountContract;
    CommaTextIo                                 file;
    container                                   rec;
    Name                                        ledgerChartOfAccountsName;
    MainAccountNum                              mainAccountId;
    DimensionLedgerAccountType                  dimensionledgerAccountType;
    MainAccountCategory                         MainAccountCategory;
    MainAccount                                 MainAccount;
    DimensionAttribute                          mainAccountDimAttribute;
    DimensionAttributeValue                     dimensionAttributeValue;
    DimensionAttributeValueTotallingCriteria    totalCriteria;
    AccountCategory                             _accountCategory;
    str                                         strOfAccounts, fromA, toA,MainAccountName;
    int                                         sep;
    container                                   readCon;
    counter                                     icount,inserted;
    ;

    csvFile = new CommaIO(filename, 'r');
    try
    {
        if (csvFile)
        {
            ttsbegin;
            readCon = csvFile.read();
            while (csvFile.status() == IO_Status::OK)
            {
                readCon = csvFile.read();
                if(readCon)
                {
                    mainAccountId = conPeek(readCon, 1);//strlrTrim(
                    mainAccountName = conPeek(readCon, 2);
                    dimensionledgerAccountType = conPeek(readCon, 3);
                    _accountCategory = conPeek(readCon,4);
                    if(!mainAccountId)
                    continue;
                    if (!mainAccount::findByMainAccountId( mainAccountId , false, LedgerChartOfAccounts::findByName(ledgerChartOfAccountsName).RecId))
                    {
                        mainAccountContract = new MainAccountContract();
                        mainAccountContract.parmMainAccountId( mainAccountId );
                        mainAccountContract.parmName(mainAccountName);
                        mainAccountContract.parmLedgerChartOfAccounts(ChartOfAccountsName);
                        mainAccountContract.parmType(enum2int(dimensionledgerAccountType::Expense));
                        select firstonly mainAccountCategory where mainAccountCategory.AccountCategory == _accountCategory;
                        MainAccountContract.parmAccountCategoryRef(mainAccountCategory.AccountCategoryRef);
                        chartOfAccountsService = new ChartOfAccountsService();
                        chartOfAccountsService.createMainAccount(mainAccountContract);
                    }
                }
            }
            ttsCommit;
        }
        icount--;//Remove header recount from total record count
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,icount));
    }
}

static void main(Args  args)
{
    ChartOfAccountsMasterImport        ChartOfAccountsMasterImport;
    ;
    ChartOfAccountsMasterImport =  new ChartOfAccountsMasterImport();
    if(ChartOfAccountsMasterImport.prompt())
    {
        ChartOfAccountsMasterImport.run();
    }
}

Sunday, 8 March 2015

Create Purchase Order through code

static void purchorder(Args _args)
{

    PurchTable      purchTable;
    PurchLine       purchLine;
    VendTable       vendTable = VendTable::find("3008");
    AxPurchTable    axPurchTable;
    AxPurchLine     axPurchLine;
    PurchFormLetter purchFormLetter;

    //Create Purchase order
    purchTable.initFromVendTable(vendTable);

    axPurchTable = axPurchTable::newPurchTable(purchTable);
    axPurchTable.parmPurchaseType(PurchaseType::Purch);
    axPurchTable.parmDocumentStatus(DocumentStatus::PurchaseOrder);
    axPurchTable.parmAccountingDate(systemDateGet());
    axPurchTable.parmDeliveryDate(01\06\2012);
    axPurchTable.parmPurchStatus(PurchStatus::Backorder);
    axPurchTable.doSave();

    //Create PurchLine for item 1000
    purchLine.initFromPurchTable(purchTable);

    axPurchLine = AxPurchLine::newPurchLine(purchLine);
    axpurchLine.parmItemId("1000");
    axPurchLine.parmPurchQty(10);
    axPurchLine.parmPurchPrice(1900);
    axPurchLine.doSave();
    info(strFmt("%1",purchTable.PurchId));

    //posting po confirmation,i guess its mandatory
    //you cannot do invoice without doing po confirm
    //purchtable = axpurchtable.purchtable();
    //purchformletter = purchformletter::construct(documentstatus::purchaseorder);
    //purchformletter.update(purchtable, strfmt("inv_%1", purchtable.purchid));
//
    //posting po invoice
    //purchformletter = purchformletter::construct(documentstatus::invoice);
    //purchformletter.update(purchtable, strfmt("inv_%1", purchtable.purchid));

Sunday, 1 March 2015

Create Rental Revenue Journal

static void createRentalRevenueJournal()
          {
              NumberSequenceTable                 numberSequenceTable;

              Voucher                             voucher;
              Amount                              amountCurDebit,amountCurCredit;

              AxLedgerJournalTrans                trans  = new AxLedgerJournalTrans();
              AxLedgerJournalTable                header ;//= new AxLedgerJournalTable();


              str                                 costcenter,department,assetcode,customer,description,IntegPostingProf;


              DimensionStorage                    DimensionStorage;
              LedgerJournalNameId                 LedgerJournalNameId;
              AX_CustomerPaymentsStagingTable_IN  RentalRevenueStagingTable;
              CurrencyCode                        currencycode;
              TransDate                           TransDate = today();
              args                                args;
              LedgerAccountContract               ledgerAccountContract,OffledgerAccountContract;
              MainAccountNum                      mainAccount;
              mainAccountNum                      OffmainAccount ;
              DimensionAttributeValueContract     attributeValueContract;
              DimensionMapping                    dimensionMappingRg,dimensionMappingBr;
              LedgerJournalCheckPost              ledgerJournalCheckPost;

              boolean     errorCheck;
              CustomerPaymentsRefunds customerPaymentsRefunds;



                      numberSequenceTable = NumberSequenceTable::find(LedgerJournalName::find(LedgerJournalNameId).NumberSequenceTable);

                      voucher =  NumberSeq::newGetVoucherFromCode(numberSequenceTable.NumberSequence).voucher();


                      amountCurDebit = 0;
                      amountCurCredit = 0;

                      description  = "Accrual Revenue";
                      currencycode = CompanyInfo::standardCurrency();

                      amountCurDebit = RentalRevenueStagingTable.Amount;

                      amountCurCredit = 0;

                      mainAccount = IntegPostingProf.RentalRevenue;
                      OffmainAccount  = IntegPostingProf.Revenue;



                      costcenter = dimensionMappingRg.DimValue;
                      department = dimensionMappingBr.DimValue;
                      assetcode  = RentalRevenueStagingTable.AssetCode;
                      customer   = RentalRevenueStagingTable.AccountNum;

                      trans.parmJournalNum(header.ledgerJournalTable().JournalNum);

                      //AccountNum
                      ledgerAccountContract = new LedgerAccountContract();
                      ledgerAccountContract.parmValues(new List(Types::Class));


                      ledgerAccountContract.parmMainAccount(mainAccount);
                      if (department)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS850");
                          attributeValueContract.parmValue(department);

                          ledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (costcenter)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS343410");
                          attributeValueContract.parmValue(costcenter);

                          ledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (assetcode)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS343374");
                          attributeValueContract.parmValue(assetcode);

                          ledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (customer)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("Customer");
                          attributeValueContract.parmValue(customer);

                          ledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }

                      dimensionStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(ledgerAccountContract);

                      trans.parmLedgerDimension(dimensionStorage.save());
                      //-------------------------------------
                      //OffsetAccount
                      OffledgerAccountContract = new LedgerAccountContract();
                      OffledgerAccountContract.parmValues(new List(Types::Class));


                      OffledgerAccountContract.parmMainAccount(OffmainAccount);
                      if (department)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS850");
                          attributeValueContract.parmValue(department);

                          OffledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (costcenter)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS343410");
                          attributeValueContract.parmValue(costcenter);

                          OffledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (assetcode)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("@SYS343374");
                          attributeValueContract.parmValue(assetcode);

                          OffledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }
                      if (customer)
                      {
                          attributeValueContract = new DimensionAttributeValueContract();
                          attributeValueContract.parmName("Customer");
                          attributeValueContract.parmValue(customer);

                          OffledgerAccountContract.parmValues().addEnd(attributeValueContract);
                      }

                      dimensionStorage = DimensionServiceProvider::buildDimensionStorageForLedgerAccount(OffledgerAccountContract);

                      trans.parmOffsetLedgerDimension(dimensionStorage.save());
                      //------------------------------------
                      trans.parmTxt(description);

                      trans.parmDocumentDate(RentalRevenueStagingTable.Date);
                      trans.parmTransDate(RentalRevenueStagingTable.Date);

                      trans.parmAmountCurDebit(AmountCurDebit);

                      trans.parmAmountCurCredit(AmountCurCredit) ;

                      trans.parmCompany(curext());
                      trans.parmCurrencyCode(currencycode);
                      trans.parmVoucher(voucher);
                      trans.parmExchRate(1);

                      // Customized fields
                      trans.parmKRContractNo(RentalRevenueStagingTable.KR_ContractNo);
                      trans.parmKRCustomerCode(RentalRevenueStagingTable.KR_CustomerCode);
                      trans.parmKRCustomerName(RentalRevenueStagingTable.KR_CustomerName);
                      trans.parmKROperationalActivity(RentalRevenueStagingTable.KR_OperationalActivity);
                      trans.parmKRRevenueType(RentalRevenueStagingTable.KR_TypeId);
                      trans.parmKRContractstatus(RentalRevenueStagingTable.ContractStatus);
                      trans.parmKRInvoiceNo(RentalRevenueStagingTable.KR_InvoiceNumber);
                      // Customized fields
                      trans.currentRecord().insert();

                      trans.save();

                      //ledgerJournalCheckPost= ledgerJournalCheckPost::newLedgerJournalTable(header.ledgerJournalTable(),NoYes::Yes);
                      //ledgerJournalCheckPost.run();

                      // create Free text invoice



                      ttsCommit;
                  }


                  next RentalRevenueStagingTable;
              }

          }