Monday, October 7, 2013

Creating Invent Movement Journal in AX 2012 using X++ programming

public class InventMovementJournal extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    DialogField                 dialogFilename,dialogcheckbox,dialogFromDate,dialogledger,dialogjournalnum,dialogInvent;
    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;
    Amount                      AmountDB,AmountCR,CrCutOffAmountCalc,DrCutOffAmountCalc;
    str                         AccountNum,date1;
    JournalId                   journalnum;
    args                        args;
    InventJournalNameid         InventJournalNameid;

    #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));
    dialogFromDate      = dialog.addFieldValue(extendedtypestr(TransDate), TransactionDate,"TransactionDate");
    dialogFilename.value(filename);
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    dialogInvent        = dialog.addFieldvalue(extendedtypestr(InventJournalNameid),"JournalName","JournalName");
    return dialog;
}

public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    FromDate    = dialogFromDate.value();
    InventJournalNameid = dialoginvent.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;
    NumberSequenceTable                 numberSequenceTable;
    LedgerJournalEngine_Daily           ledgerJournalEngine_Daily;
    Voucher                             voucher;
    DimensionLedgerAccountType          ledgerAccountType;
    str                                 accountType;
    Amount                              amountCurDebit,amountCurCredit,amtcrdt,amtdebt,price;
    container                           conSplitValue,dimm,offdim;
    DimensionAttributeValueCombination  ledgerDimension,dimensionAttributeValueCombinationrecid;
    str                                 dimensn,offdimensn;
    str 64                              custAccount,ledgerAccount,Quantity;
    CustTable                           custtable;
    dimensionAttribute                  dimensionAttribute;
    dimensionAttributevalue             dimensionAttributevalue;
    DimensionAttributeValueSetStorage   dimstorage;
    int64                              reccid;
    Struct struct = new Struct();
    container conledgerDimension;
    DimensionDefault DimensionDefault;
    str                                 ItemId,Costprice,Costamount,site,warehouse,ldgracc,batchid,serialnum,location;
    LedgerJournalACTypE                 acctype,offacctype;
    DimensionStorage                    DimensionStorage;
    InventJournalTable journalTable;
    InventJournalTrans journalTrans;
    InventJournalTableData journalTableData;
    InventJournalTransData journalTransData;
    InventTable inventTable;
    InventDim inventDim;
    Counter cnt;
    InventBatch         inventBatch;
   
    ;
    LedgerJournalNameId = Ledgerjournalnameid1;
    TransDate= Fromdate;
    journalnum = journalnum;
    InventJournalNameid = InventJournalNameid;
    csvFile = new CommaIO(filename, 'r');
    try
    {
        if (csvFile)
        {
            ttsbegin;
                    journalTableData = JournalTableData::newTable(journalTable);
                    journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans,journalTableData);
                    // Init JournalTable
                    journalTable.clear();
                    journalTable.JournalId = journalTableData.nextJournalId();
                    journalTable.JournalType = InventJournalType::Movement;
                    journalTable.JournalNameId = InventJournalNameid;//journalTableData.journalStatic().standardJournalNameId(journalTable.JournalType);
            while (csvFile.status() == IO_Status::OK)
            {
                readCon = csvFile.read();
                icount++;
                journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(journalTable.JournalNameId));
                    // Init JournalTrans
                    journalTrans.clear();
                    journalTransData.initFromJournalTable();
                if (readCon && icount )//dont insert first record of file : header
                {
                    TransDate = TransDate;//str2Date(conPeek(readCon,1),213);
                    ItemId = conpeek(readcon,1);
                    ldgracc = conPeek(readCon,2);
                    CostPrice = conPeek(readCon,3);
                    Quantity = conPeek(readCon,4);
                    Costamount= conPeek(readCon,5);
                    site = conPeek(readCon,6);
                    warehouse = conPeek(readCon,7);
                    batchid = conPeek(readCon,8);
                    location = conPeek(readCon,9);
                    serialnum = conPeek(readCon,10);

                    journalTrans.TransDate = Transdate;
                    journalTrans.ItemId = ItemId;
                    journalTrans.Qty = str2num(Quantity);
                    journalTrans.CostAmount = str2num(Costamount);
                    journalTrans.CostPrice = str2num(Costprice);
                    journalTrans.LedgerDimension = this.getLedgerDimension(ldgracc);
                    inventDim.InventLocationId = warehouse;
                    inventDim.InventSiteId = site;
                    inventDim.inventBatchId = BatchID;
                    inventDim.wMSLocationId = location;
                    inventDim.inventSerialId = serialnum;
                    // Dimension details
                    //inventDim.InventLocationId = ‘MW’;
                    journalTrans.InventDimId = InventDim::findOrCreate(inventDim).inventDimId;
                    journalTransData.create();

                    // Call the static method to post the journal
                    //if(InventJournalCheckPost::newPostJournal(journalTable).validate())
                    //InventJournalCheckPost::newPostJournal(journalTable).run();
                    inserted++;

                }
            }
            journalTable.insert();
            ttsCommit;
        }
        icount--;//Remove header recount from total record count
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,icount));
    }
    args = new Args();
    args.record(InventJournalTable::find(journalTable.JournalId));
    new MenuFunction(MenuItemDisplayStr(InventJournalTablemovement),MenuItemType::Display).run(args);
}

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

}

No comments:

Post a Comment