Monday, 15 December 2014

Upload JV through csv for Ledgers only (not for subledgers) in AX 2012



  • ClassDeclaration
public class UploadJournal extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    container                   readCon;
    counter                     inserted;
    LedgerJournalACType         ledgerJournalACType;
    LedgerJournalType           ledgerJournalType;
    ledgerJournalName           ledgerJournalName;
    LedgerJournalTable          ledgerJournalTable;
    LedgerJournalTrans          ledgerJournalTrans;

    Ledgerjournalnameid         ledgerJournalNameId;
    args                        args;
    DialogField                 dialogFilename,dialogJournalname;


    #define.CurrentVersion(2)
    #localmacro.CurrentList
        filename,
        inserted,
    #endmacro

    #localmacro.ListVersion1
        filename,
        inserted
    #endmacro

    #File

}
  • Method- 1:
public Object dialog()
{
    DialogRunbase       dialog = super();
    ;
    dialog.caption("Journal Upload");

    dialogFilename  = dialog.addField(extendedTypeStr(FilenameOpen));

    dialogFilename.value(filename);
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    dialogJournalname = dialog.addFieldvalue(extendedtypestr(LedgerJournalNameid),"JournalName","JournalName");

    return dialog;
}
  • Method- 2:
public boolean getFromDialog()
{
    filename = dialogFilename.value();
    ledgerjournalnameid = dialogJournalname.Value();

    return true;
}
  • Method- 3:
void run()
{
    boolean                             first = true;

    ledgerJournalACType                 accType;
    TransDate                           transDate;
    TransDate                           invoiceDate;
    TransDate                           documentDate;

    str                                 accountType;

    LedgerAccount                       ledgerAccount;
    VendAccount                         vendAccount;
    CustAccount                         custAccount;
    BankAccount                         bankAccount;

    AssetId                             assetId;
    ProjId                              projiD;
    ProjCategoryCostId                  categoryId;
    CostPrice                           CostPrice;
    Qty                                 qty;
    ProjLinePropertyId                  linePropertyiD;

    str                                 description;

    AmountCur                           amountCurDebit;
    AmountCur                           amountCurCredit;

    CurrencyCode                        currencyCode;

    str                                 costCenter;
    str                                 department;

    InvoiceId                           invoiceId;
    DocumentNum                         documentNum;

    PostingProfile                      postingprofile;

    container                           dimm;

    DimensionAttributeValueCombination  dimensionAttributeValueCombination;
    DimensionAttributeValueCombination  ledgerDimension,dimensionAttributeValueCombinationrecid;
    NumberSequenceTable                 numberSequenceTable;
    Voucher                             voucher;

    AxLedgerJournalTrans                trans = new AxLedgerJournalTrans();
    AxLedgerJournalTable                table = new AxLedgerJournalTable();
    AxLedgerJournalTable_CustVend       tableCustVend = new AxLedgerJournalTable_CustVend();
    AxLedgerJournalTrans_CustPaymJour   transCustPaym = new AxLedgerJournalTrans_CustPaymJour();

    str                                 dimensn;
    Struct                              struct = new Struct();
    container                           conledgerDimension;
    DimensionDefault                    dimensionDefault;
    ExchRate                            exchRate;
    LedgerJournalTrans_Project          LedgerJournalTransProject;
    LedgerJournalTrans_Asset            ledgerJournalTransAssets;
    assetTable                          assetTable;
    AssetBook                           assetBook;
    str                                 chequeNo;
    ledgerJournalEngine                 ledgerJournalEngine;
    ;
    csvFile = new CommaIO(filename, 'r');
    if (LedgerJournalName::find(ledgerjournalnameid).JournalType != ledgerJournalType::Daily)
    {
        table = tableCustVend;
        trans = transCustPaym;
    }
    try
    {
        if (csvFile)
        {
            ttsbegin;
            if(first)
            {
                table.parmJournalName(LedgerJournalNameId);
                table.save();
                first = false;
            }
            numberSequenceTable = NumberSequenceTable::find(LedgerJournalName::find(LedgerJournalNameId).NumberSequenceTable);
            voucher =  NumberSeq::newGetVoucherFromCode(numberSequenceTable.NumberSequence).voucher();
            //voucher = 'RVL - A000013';
            while (csvFile.status() == IO_Status::OK)
            {
                ledgerAccount = '';
                vendAccount = '';
                custAccount = '';
                bankAccount = '';

                amountCurDebit = 0;
                amountCurCredit = 0;

                readCon = csvFile.read();

                if (readCon)
                {
                    accType = conPeek(readCon,1);
                    if (accType == ledgerJournalACType::Ledger)
                    {
                        transdate = conPeek(readCon,2);
                        ledgerAccount = conPeek(readCon,3);
                        description = conPeek(readcon,4);
                        currencycode = conPeek(readCon,5);
                        amountCurDebit = conPeek(readCon,6);
                        amountCurCredit = conPeek(readCon,7);
                        costCenter = conPeek(readcon,8);
                        department = conPeek(readcon,9);
                        //invoiceId = conPeek(readCon,10);
                        //invoiceDate = conPeek(readCon,11);
                        documentNum = conPeek(readCon,12);
                       //exchRate = conPeek(readcon,15);

                        if (costcenter != "" && department != "")
                        {
                            struct.add('Department', department);
                            struct.add('costcenter', costcenter);

                            conledgerDimension += struct.fields();
                            conledgerDimension += struct.fieldName(1);
                            conledgerDimension += struct.valueIndex(1);
                            conledgerDimension += struct.fieldName(2);
                            conledgerDimension += struct.valueIndex(2);

                            dimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(conledgerDimension);
                            select dimensionAttributeValueCombinationrecid
                                where dimensionAttributeValueCombinationrecid.RecId == DimensionDefault;

                            dimensn = strFmt("%1-%2-%3,%1,2,department,%2,costcenter,%3",ledgerAccount,department,costcenter);
                            dimm = str2con(dimensn);

                            trans.parmAccountType(LedgerJournalACTypE::Ledger);

                            trans.parmLedgerDimension(AxdDimensionUtil::getLedgerAccountId(dimm));
                        }
                        else if(costcenter == "" && department == "")
                        {
                            dimm = [ledgeraccount,ledgeraccount,"",""];
                            trans.parmAccountType(LedgerJournalACTypE::Ledger);
                            trans.parmLedgerDimension(AxdDimensionUtil::getLedgerAccountId(dimm));
                        }
                        trans.parmJournalNum(table.ledgerJournalTable().JournalNum);
                        //trans.parmJournalNum('JN-000830');
                        trans.parmVoucher(voucher);
                        trans.parmCurrencyCode(currencyCode);
                        trans.parmExchRate(Currency::exchRate(currencyCode,transDate));
                        //trans.parmExchRate(exchRate);
                        trans.parmTxt(strLRTrim(description));
                        trans.parmTransDate(TransDate);
                        trans.parmAmountCurDebit(amountCurDebit);
                        trans.parmAmountCurCredit(amountCurCredit) ;
                        trans.parmInvoice(invoiceId);

                        trans.parmReverseEntry(NoYes::Yes);
                        trans.parmReverseDate(01\04\2013);
                        //trans.parmDue(invoiceDate);
                       // trans.parmApprover(HcmWorker::userId2Worker('NNourrice'));
                       // trans.parmApproved(NoYes::Yes);
                        trans.parmDocumentNum(documentNum);
                        trans.currentRecord().insert();
                        inserted++;
                        info(strFmt("%1 inserted",inserted));
                    }
                    else if (accType == ledgerJournalACType::Cust)
                    {
                        trans.parmAccountType(LedgerJournalACTypE::Cust);
                        transdate = conPeek(readCon,2);
                        custAccount = conPeek(readCon,3);
                        description = conPeek(readcon,4);
                        currencycode = conPeek(readCon,5);
                        amountCurDebit = conPeek(readCon,6);
                        amountCurCredit = conPeek(readCon,7);
                        invoiceId = conPeek(readCon,10);
                        invoiceDate = conPeek(readCon,11);
                        //documentNum = conPeek(readCon,12);
                        //documentDate = conPeek(readCon,13);
                        postingprofile = conPeek(readcon,14);
                        exchRate = conPeek(readcon,15);

                        trans.parmLedgerDimension(DimensionStorage::getDynamicAccount(custAccount,acctype));
                        trans.parmJournalNum(table.ledgerJournalTable().JournalNum);
                        trans.parmVoucher(voucher);
                        trans.parmCurrencyCode(currencyCode);
                        //trans.parmExchRate(Currency::exchRate(currencyCode,transDate));
                        trans.parmExchRate(exchrate);
                        trans.parmTxt(strLRTrim(description));
                        trans.parmTransDate(TransDate);
                        trans.parmAmountCurDebit(amountCurDebit);
                        trans.parmAmountCurCredit(amountCurCredit) ;
                        trans.parmInvoice(invoiceId);
                        trans.parmDue(invoiceDate);
                        //trans.parmDocumentDate(documentDate);
                        //trans.parmDocumentNum(documentNum);
                        trans.parmPostingProfile(postingProfile);
                        trans.parmApprover(HcmWorker::userId2Worker('NNourrice'));
                        trans.parmApproved(NoYes::Yes);

                        trans.currentRecord().insert();
                        inserted++;

                        info(strFmt("%1 inserted",inserted));
                    }
                    else if (accType == ledgerJournalACType::Vend)
                    {
                        trans.parmAccountType(LedgerJournalACTypE::Vend);
                        transdate = conPeek(readCon,2);
                        vendAccount = conPeek(readCon,3);
                        description = conPeek(readcon,4);
                        currencycode = conPeek(readCon,5);
                        amountCurDebit = conPeek(readCon,6);
                        amountCurCredit = conPeek(readCon,7);
                        invoiceId = conPeek(readCon,10);
                        //invoiceDate = conPeek(readCon,11);
                        //documentNum = conPeek(readCon,12);
                        //documentDate = conPeek(readCon,13);
                        postingprofile = conPeek(readcon,14);
                        exchRate = conPeek(readcon,15);

                        trans.parmLedgerDimension(DimensionStorage::getDynamicAccount(vendAccount,acctype));
                        trans.parmJournalNum(table.ledgerJournalTable().JournalNum);
                        trans.parmVoucher(voucher);
                        trans.parmCurrencyCode(currencyCode);
                        //trans.parmExchRate(Currency::exchRate(currencyCode,transDate));
                        trans.parmExchRate(exchRate);
                        trans.parmTxt(strLRTrim(description));
                        trans.parmTransDate(TransDate);
                        trans.parmAmountCurDebit(amountCurDebit);
                        trans.parmAmountCurCredit(amountCurCredit) ;
                        trans.parmInvoice(invoiceId);
                        trans.parmDue(invoiceDate);
                        //trans.parmDocumentDate(documentDate);
                        //trans.parmDocumentNum(documentNum);
                        trans.parmPostingProfile(postingProfile);
                        trans.parmApprover(HcmWorker::userId2Worker('NNourrice'));
                        trans.parmApproved(NoYes::Yes);

                        trans.currentRecord().insert();
                        inserted++;

                        info(strFmt("%1 inserted",inserted));
                    }
                    else if (accType == ledgerJournalACType::Bank)
                    {
                        trans.parmAccountType(LedgerJournalACType::Bank);
                        transdate = conPeek(readCon,2);
                        bankAccount = conPeek(readCon,3);
                        description = conPeek(readcon,4);
                        currencycode = conPeek(readCon,5);
                        amountCurDebit = conPeek(readCon,6);
                        amountCurCredit = conPeek(readCon,7);
                        /*invoiceId = conPeek(readCon,10);
                        invoiceDate = conPeek(readCon,11);*/
                        documentNum = conPeek(readCon,12);
                        //documentDate = conPeek(readCon,13);
                        //postingprofile = conPeek(readcon,14);*/
                        //exchRate = conPeek(readcon,15);
                        //chequeNo = conPeek(readCon,16);

                        trans.parmLedgerDimension(DimensionStorage::getDynamicAccount(bankAccount,acctype));
                        trans.parmJournalNum(table.ledgerJournalTable().JournalNum);
                        //trans.parmJournalNum('JN-000830');
                        trans.parmVoucher(voucher);
                        trans.parmCurrencyCode(currencyCode);
                        trans.parmExchRate(Currency::exchRate(currencyCode,transDate));
                        //trans.parmExchRate(exchrate);
                        trans.parmTxt(strLRTrim(description));
                        trans.parmTransDate(TransDate);
                        trans.parmAmountCurDebit(amountCurDebit);
                        trans.parmAmountCurCredit(amountCurCredit) ;
                        /*trans.parmInvoice(invoiceId);
                        trans.parmDue(invoiceDate);
                        trans.parmDocumentDate(documentDate);*/
                        trans.parmDocumentNum(documentNum);
                        //trans.parmBankChequeNum(chequeNo);
                        //trans.parmApprover(HcmWorker::userId2Worker('NNourrice'));
                        //trans.parmApproved(NoYes::Yes);

                        trans.currentRecord().insert();
                        inserted++;

                        info(strFmt("%1 inserted",inserted));
                    }
                    else if (accType == ledgerJournalACType::Project)
                    {
                        trans.parmAccountType(LedgerJournalACTypE::Project);
                        transdate = conPeek(readCon,2);
                        projiD = conPeek(readCon,3);
                        description = conPeek(readcon,4);
                        currencycode = 'SCR';//conPeek(readCon,5);
                        amountCurDebit = conPeek(readCon,6);
                        amountCurCredit = conPeek(readCon,7);
                        if (amountCurDebit && !amountCurCredit)
                        {
                            CostPrice = amountCurDebit;
                            qty = 1;
                        }
                        else
                        {
                            CostPrice = amountCurCredit;
                            qty = -1;
                        }
                        categoryId = 'Expense';//conPeek(readCon,16);
                        linePropertyiD = 'No charge';//conPeek(readCon,17);

                        trans.parmLedgerDimension(DimensionStorage::getDynamicAccount(projiD,acctype));
                        trans.parmJournalNum(table.ledgerJournalTable().JournalNum);
                        trans.parmVoucher(voucher);
                        trans.parmCurrencyCode(currencyCode);
                        Currency::exchRate(currencyCode,transDate);
                        trans.parmTxt(strLRTrim(description));
                        trans.parmTransDate(TransDate);
                        trans.parmAmountCurDebit(amountCurDebit);
                        trans.parmAmountCurCredit(amountCurCredit) ;

                        trans.currentRecord().insert();

                        LedgerJournalTransProject.clear();
                        LedgerJournalTransProject.ProjId         = projId;
                        LedgerJournalTransProject.CategoryId     = categoryid;
                        LedgerJournalTransProject.LinePropertyId = linePropertyiD;
                        LedgerJournalTransProject.Qty = qty;
                        LedgerJournalTransProject.CostPrice = CostPrice;
                        LedgerJournalTransProject.RefRecId = trans.currentRecord().RecId;
                        LedgerJournalTransProject.SalesCurrencyId = currencyCode;
                        LedgerJournalTransProject.insert();
                        inserted++;

                        info(strFmt("%1 inserted",inserted));
                    }
                    else if (accType == ledgerJournalACType::FixedAssets)
                    {
                        ledgerAccount = '';
                        trans.parmAccountType(LedgerJournalACTypE::FixedAssets);
                        transdate = conPeek(readCon,2);
                        assetid = conPeek(readCon,3);
                        description = conPeek(readcon,4);
                        currencycode = conPeek(readCon,5);
                        amountCurDebit = conPeek(readCon,6);
                        amountCurCredit = conPeek(readCon,7);
                        ledgerAccount = conPeek(readCon,8);

                        dimm = [ledgeraccount,ledgeraccount,"",""];
                        trans.parmAccountType(LedgerJournalACTypE::Ledger);
                        trans.parmOffsetLedgerDimension(AxdDimensionUtil::getLedgerAccountId(dimm));

                        trans.parmLedgerDimension(DimensionStorage::getDynamicAccount(assetid,acctype));
                        trans.parmJournalNum(table.ledgerJournalTable().JournalNum);
                        trans.parmVoucher(voucher);
                        trans.parmCurrencyCode(currencyCode);
                        trans.parmExchRate(Currency::exchRate(currencyCode,transDate));
                        trans.parmTxt(strLRTrim(description));
                        trans.parmTransDate(TransDate);
                        trans.parmAmountCurDebit(amountCurDebit);
                        trans.parmAmountCurCredit(amountCurCredit) ;
                        trans.parmPostingProfile(AssetParameters::find().PostingProfile);
                        trans.parmCompany(curext());
                        trans.parmOffsetCompany(curext());
                        trans.currentRecord().insert();

                        assetTable=assetTable::find(assetid);
                        select assetBook where assetBook.AssetId == assetid;

                        ledgerJournalTransAssets.clear();
                        ledgerJournalTransAssets.BookId         = assetBook.BookId;
                        ledgerJournalTransAssets.TransType      = AssetTransTypeJournal::DepreciationAdj;
                        ledgerJournalTransAssets.AssetId        = assetid;
                        ledgerJournalTransAssets.Company        = curext();
                        ledgerJournalTransAssets.RefRecId       = trans.currentRecord().RecId;
                        ledgerJournalTransAssets.insert();
                        inserted++;
                        //ledgerJournalEngine = new LedgerJournalEngine();
                        //ledgerJournalEngine.initDefaultDimension(trans, ledgerJournalTransAssets);

                    }
                }
            }
            ttsCommit;
        }
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,inserted));
    }

    args = new Args();
    args.record(LedgerJournalTable::find(table.ledgerJournalTable().JournalNum ));

    new MenuFunction(MenuItemDisplayStr(LedgerJournalTable),MenuItemType::Display).run(args);
}
  • Method- 4:
static void main(Args  args)
{
    UploadJournal        uploadJournal;
    ;
    uploadJournal =  new UploadJournal();

    if (uploadJournal.prompt())
    {
        uploadJournal.run();
    }
}

No comments:

Post a Comment