Monday, 15 December 2014

CIS upload in AX 2012



public class BillingJournalEntriesNew extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    DialogField                 dialogFilename,dialogcheckbox,dialogFromDate,dialogledger;
    container                   readCon;
    container                   ledgerDimensions, offsetDimensions;
    counter                     icount,inserted;
    Amount                      amount;
    LedgerJournalACType         ledgerJournalACType;
    ledgerJournalName           ledgerJournalName;
    LedgerJournalTable          ledgerJournalTable;
    LedgerJournalTrans          ledgerJournalTrans;
    LedgerJournalTrans_Project  ledgerProjects;
    TransDate                   TransactionDate,Fromdate,TransDate,StartDate,enddate,reversedate;
    NoYesID                     NoYes;
    Ledgerjournalnameid         Ledgerjournalnameid1,LedgerJournalNameId;
    int                         cutoffdays,prevcutoffdays;
    currencycode                currencycode;
    // ExchRates                   exchRates;
    Amount                      AmountDB,AmountCR,CrCutOffAmountCalc,DrCutOffAmountCalc;
    str                         AccountNum;
    args                        args;


    #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);
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    dialogFromDate      = dialog.addFieldValue(extendedtypestr(TransDate), TransactionDate,"TransactionDate");
    dialogledger        = dialog.addFieldvalue(extendedtypestr(LedgerJournalNameid),"JournalName","JournalName");
    dialogcheckbox      = dialog.addFieldValue(enumstr(NoYes),"","Reversing","");

    return dialog;
}



public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    FromDate    = dialogFromDate.value();
    NoYes      = dialogcheckbox.value();
    Ledgerjournalnameid1 = dialogledger.Value();

    return true;
}



public LedgerDimensionAccount getLedgerDimension(AccountNum _accountNum)
{
    MainAccount                 mainAccount;

    RefRecId                    accountStructureId;
    List                        dimensionList = new List(Types::Class);
    DimensionDefaultingEngine   dimensionDefaultingEngine;
    ;

    mainAccount = MainAccount::findByMainAccountId(_accountNum);

    accountStructureId = DimensionHierarchy::getAccountStructure(mainAccount.RecId);

    if(mainAccount)
    {
        dimensionDefaultingEngine = DimensionDefaultingEngine::constructForMainAccountId(mainAccount.RecId, accountStructureId);
        dimensionDefaultingEngine.applyDimensionSources(dimensionList);

    return dimensionDefaultingEngine.getLedgerDimension();
    }
    else
    return 0;
}



void run()

{
    boolean                             first = true;
    MainAccount                         mainAccount;
    DimensionAttributeValueCombination  dimensionAttributeValueCombination;
    MainAccountNum                      mainAccountNum;
    NumberSequenceTable                 numberSequenceTable;
    LedgerJournalEngine_Daily           ledgerJournalEngine_Daily;
    Voucher                             voucher;
    DimensionLedgerAccountType          ledgerAccountType,acctype;
    str                                 accountType;
    Amount                              amountCurDebit,amountCurCredit,amtcrdt,amtdebt;
    container                           conSplitValue,dimm,offdim;
    projTable                           projTable;
    DimensionAttributeValueCombination  ledgerDimension,dimensionAttributeValueCombinationrecid;
    ProjId                              projid;
    JournalTableData                    JournalTableData;
    AxLedgerJournalTrans                trans  = new AxLedgerJournalTrans();
    AxLedgerJournalTable                header = new AxLedgerJournalTable();
    str                                 dimensn,offdimensn;
    str 64                              custAccount,ledgerAccount;
    CustTable                           custtable;
    dimensionAttribute                  dimensionAttribute;
    dimensionAttributevalue             dimensionAttributevalue;
    DimensionAttributeValueSetStorage   dimstorage;
    int64                              reccid;
    Struct struct = new Struct();
    container conledgerDimension;
    DimensionDefault DimensionDefault;
    str                                 costcenter,department,revenueDimension,description;
    CISBillingInterfaceTable           cisBillingInterfaceTable;
    int i=0,a;
    ;

    TransDate   = fromDate;
    startdate   = datestartmth(transdate);
    enddate     = endmth(transdate);
    prevcutoffdays = (transdate -  startdate)+1;
    cutoffdays     = enddate - transdate;
    if(noyes == noyes::Yes)
    reversedate    = nextmth(startdate);
    else
    reversedate    = datenull();

    currencycode    =  companyinfo::standardCurrency();
    //exchRates       = ExchRates::findExchRateDate(currencycode,today());
    LedgerJournalNameId = Ledgerjournalnameid1;

    csvFile = new CommaIO(filename, 'r');
    try
    {
        if (csvFile)
        {
            ttsbegin;
            if(first) //Create Journal Header
            {
                header.parmJournalName(LedgerJournalNameId);

                if(noyes == noyes::Yes)
                {
                    header.parmReverseEntry(noyes::Yes);
                    header.parmReverseDate(reversedate);
                }

                header.save();

                first = false;
            }

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

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

            while (csvFile.status() == IO_Status::OK)
            {
                readCon = csvFile.read();

                icount++;

                amountCurDebit = 0;
                amountCurCredit = 0;

                if (readCon && icount > 1 )//dont insert first record of file : header
                {
                    A++;

                    description = conPeek(readcon,1);
                    //currencycode = conPeek(readCon,2);
                    //acctype = conPeek(readCon,4);
                    amountCurDebit = conPeek(readCon,3);
                    amountCurCredit = conPeek(readCon,4);
                    ledgerAccount = conPeek(readCon,2);
                    costcenter = conPeek(readcon,5);
                    department = conPeek(readcon,6);
                    //voucher = conPeek(readcon,10);

                    select cisBillingInterfaceTable where cisBillingInterfaceTable.CISLedgerCode == ledgerAccount;

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

                    if((acctype == 0) && department && costcenter )
                    {
                        struct.add('Department', department);
                        struct.add('costcenter', costcenter);
                        //struct.add('revenuedimensions', revenueDimension);

                        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",cisBillingInterfaceTable.AXLedgerCode,department,costcenter);

                        dimm = str2con(dimensn);

                        trans.parmAccountType(LedgerJournalACTypE::Ledger);

                        trans.parmLedgerDimension(AxdDimensionUtil::getLedgerAccountId(dimm));

                    }
                    else if(acctype == 0 && !department && !costcenter)
                    {
                        dimm = [cisBillingInterfaceTable.AXLedgerCode,cisBillingInterfaceTable.AXLedgerCode,"",""];
                        trans.parmAccountType(LedgerJournalACTypE::Ledger);

                        trans.parmLedgerDimension(AxdDimensionUtil::getLedgerAccountId(dimm));
                    }

                    trans.parmDocumentDate(TransDate);
                    trans.parmTransDate(TransDate);
                    trans.parmTxt(description);

//-----------------------------------------------------------------------------------------------------------------------------------------------------------

                    if(noyes == noyes::Yes)
                        {
                            // calculation made with respect to Debit an credit cutoff dates starts

                            // DrCutOffAmountCalc = ((AmountCurDebit)/prevcutoffdays) * cutoffdays; /// commented by Vimal on July 30, 2013
                            DrCutOffAmountCalc = decRound((((AmountCurDebit)/prevcutoffdays) * cutoffdays), 2); /// added by Vimal

                            //amtdebt = round((AmountCurDebit + DrCutOffAmountCalc),1);//Removed round off as sugested by Kunjal on 12/Feb/2013
                            amtdebt = AmountCurDebit + DrCutOffAmountCalc;

                            trans.parmAmountCurDebit(amtdebt);

                            //CrCutOffAmountCalc = ((AmountCurCredit)/prevcutoffdays) * cutoffdays; /// commented by Vimal on July 30, 2013
                            CrCutOffAmountCalc = decRound((((AmountCurCredit)/prevcutoffdays) * cutoffdays), 2); /// added by Vimal

                            //trans.parmAmountCurCredit(round((AmountCurCredit + CrCutOffAmountCalc),1));//Removed round off as sugested by Kunjal on 12/Feb/2013
                            trans.parmAmountCurCredit(AmountCurCredit + CrCutOffAmountCalc);
                            trans.parmReverseEntry(noyes::Yes);
                            trans.parmReverseDate(reversedate);
                        }
                        else
                        {
                            trans.parmAmountCurDebit(AmountCurDebit);
                            trans.parmAmountCurCredit(AmountCurCredit);
                        }

                        trans.parmCompany(curext());//strLRTrim(conPeek(readCon,2));
                        trans.parmCurrencyCode(currencycode);//strLRTrim(conPeek(readCon,9));
                        trans.parmVoucher(voucher);
                        //trans.parmExchRate(1); /// commented by Vimal on July 30, 2013
                        trans.parmExchRate(100); /// added by Vimal

                    Trans.currentRecord().insert();
                    inserted++;
                    }
            }
if(a > 0)
            trans.save();
            ttsCommit;
        }

        icount--;//Remove header recount from total record count
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,inserted));
    }

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

    new MenuFunction(MenuItemDisplayStr(LedgerJournalTable),MenuItemType::Display).run(args);
}

public container splitAccountNumAndDept(str _accountNumDept, str _separator = '-')
        {

            int     separatorPosition;
            container con;

            ;

                separatorPosition = strfind(_accountNumDept, _separator, strlen(_accountNumDept), -strlen(_accountNumDept));

                if(separatorPosition)
                {
                    //AccountNumber
                    con = conIns(con, 1,substr(_accountNumDept, 1, separatorPosition - 1));
                    //Department
                    con = conIns(con, 2,substr(_accountNumDept, separatorPosition + 1, 3));

                }
                else
                {
                    //AccountNumber
                    con = conIns(con, 1,_accountNumDept);

                }

            return con;
        }


static void main(Args  args)
        {
            BillingJournalEntriesnew        billingJournalEntriesnew;
            ledgerJournalTrans              ledgerJournalTrans;

            ;

            billingJournalEntriesnew =  new billingJournalEntriesnew();

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


}



No comments:

Post a Comment