Monday, 15 December 2014

Upload Inventory transfer/Movement journal through csv file in AX 2012






public class ssUploadInventTransferJournal_VS extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    container                   readCon;
    counter                     inserted;

    InventJournalType           inventJournalType;
    InventJournalName           inventJournalName;
    InventJournalTable          inventJournalTable;
    InventJournalTrans          inventJournalTrans;
    InventDim                   inventDim, toInventDim;

    InventJournalNameId         inventJournalNameId;
    args                        args;
    DialogField                 dialogFilename, dialogJournalnameId;


    #define.CurrentVersion(2)




public class ssUploadInventTransferJournal_VS extends RunBase
{
    CommaIo                     csvFile;
    Filename                    filename;
    container                   readCon;
    counter                     inserted;

    InventJournalType           inventJournalType;
    InventJournalName           inventJournalName;
    InventJournalTable          inventJournalTable;
    InventJournalTrans          inventJournalTrans;
    InventDim                   inventDim, toInventDim;

    InventJournalNameId         inventJournalNameId;
    args                        args;
    DialogField                 dialogFilename, dialogJournalnameId;


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

    #localmacro.ListVersion1
        filename,
        inserted
    #endmacro

    #File
}

public Object dialog()
{
    DialogRunbase       dialog = super();
    ;

    dialog.caption("Upload Inventory transfer journal");

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

    dialogFilename.value(filename);
    dialog.filenameLookupFilter(["All files", #AllFiles]);
    dialogJournalnameId = dialog.addFieldvalue(extendedtypestr(InventJournalNameId)," ","Journal name");

    return dialog;
}


public boolean getFromDialog()
{
    filename = dialogFilename.value();
    inventJournalNameId = dialogJournalnameId.Value();

    return true;
}

// <Summary>
// this logic is used to create inventory transfer Journal and upload data from csv file to DAX 2012
// this is for Nominal ledgers only <as per current requirement>
// <Summary>

void run()
{
    boolean                             first = true;
    container                           conDimension, conledgerDimension;
    str                                 description, costCenter, department, ledgerDimOffset;
    anytype                             ledgerDimItem, defDim;

    LedgerAccount                       ledgerAccount;
    DimensionDefault                    dimensionDefault;

    Qty                                 qty;
    TransDate                           transDate;
    PriceUnit                           priceUnit;
    //InventDimId                         inventDimId, toInventDimId;
    InventSiteId                        inventSiteId, toInventSiteId;
    InventLocationId                    inventLocationId, toInventLocationId;
    AmountMST                           itemCostPrice, itemCostAmount;
    InventJournalId                     inventJournalId;
    ItemIdInventoried                   itemId;

    Voucher                             voucher;
    NumberSequenceTable                 numberSequenceTable;
    DimensionAttributeValueCombination  dimAttrValueCombination;

    //AxInventJournalTable                table = new AxInventJournalTable();
    //AxInventJournalTrans                trans = new AxInventJournalTrans(inventJournalTrans);
    AxInventJournalTable                table = AxInventJournalTable::newInventJournalTable(inventJournalTable);
    AxInventJournalTrans                trans = AxInventJournalTrans::newInventJournalTrans(inventJournalTrans);
    Struct                              struct = new Struct();
    ;

    csvFile = new CommaIO(filename, 'r');

    try
    {
        if (csvFile)
        {
            ttsbegin;
            if(first)
            {
                table.parmJournalNameId(inventJournalNameId);
                table.parmJournalType(InventJournalType::Transfer);
                table.parmDescription("Transfer journal upload");
                table.parmVoucherDraw(JournalVoucherDraw::Post);
                table.parmVoucherNumberSequenceTable(5637145299);
                table.save();
                first = false;
            }
            //numberSequenceTable = NumberSequenceTable::find(InventJournalName::find(inventJournalNameId).VoucherNumberSequenceTable);
            //voucher =  NumberSeq::newGetVoucherFromCode(numberSequenceTable.NumberSequence).voucher();

            while (csvFile.status() == IO_Status::OK)
            {
                ledgerAccount    = '';
                dimensionDefault = 0;
                itemCostPrice    = 0;
                itemCostAmount   = 0;

                readCon = csvFile.read();

                if (readCon)
                {
                    inventJournalType   = conPeek(readcon,1);
                    if (inventJournalType == InventJournalType::Transfer)
                    {
                        transdate           = conPeek(readcon,2);
                        inventSiteId        = conPeek(readcon,3);
                        inventLocationId    = conPeek(readCon,4);
                        toInventSiteId      = conPeek(readCon,5);
                        toInventLocationId  = conPeek(readCon,6);
                        itemId              = conPeek(readcon,7);
                        description         = "Opening stock balance"; //conPeek(readcon,5);
                        qty                 = conPeek(readcon,9);
                        //itemCostPrice   = conPeek(readcon,10);
                        //itemCostAmount  = qty * itemCostPrice;
                        priceUnit       = 1;

                        inventDim.clear();
                        inventDim.InventSiteId = inventSiteId;
                        inventDim.InventLocationId = inventLocationId;
                        toInventDim.clear();
                        toInventDim.InventSiteId = toInventSiteId;
                        toInventDim.InventLocationId = toInventLocationId;

                        inventJournalTrans.clear();
                        inventJournalId = table.inventJournalTable().JournalId;
                        trans.parmJournalId(inventJournalId);
                        trans.parmJournalType(inventJournalType);
                        trans.parmTransDate(transDate);
                        trans.parmInventDimId(InventDim::findOrCreate(inventDim).inventDimId);
                        trans.parmToInventDimId(InventDim::findOrCreate(toInventDim).inventDimId);
                        trans.parmItemId(itemId);
                        trans.parmDescription(strLRTrim(description));
                        trans.parmQty(qty);
                        //trans.parmCostPrice(itemCostPrice);
                        //trans.parmCostAmount(itemCostAmount);
                        trans.parmPriceUnit(priceUnit);

                        trans.currentRecord().insert();
                        inserted++;
                        info(strFmt("%1 inserted",inserted));
                    }
                }
            }
            ttsCommit;
        }
    }
    catch(Exception::Error)
    {
        info(strFmt("%1 %2",Exception::Error,inserted));
    }

    args = new Args();
    args.record(InventJournalTable::find(table.inventJournalTable().JournalId ));

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


static void main(Args  args)
{
    ssUploadInventTransferJournal_VS        uploadJournal;
    ;
    uploadJournal =  new ssUploadInventTransferJournal_VS();

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



static void createMovJournal(Args _args)
{
    InventJournalTable      journalTable;
    InventJournalTrans      journalTrans;
    InventJournalTableData  journalTableData;
    InventJournalTransData  journalTransData;
    InventTable             inventTable;
    InventDim               inventDim;
    Counter                 cnt;
    InventJournalCheckPost journalCheckPost = new InventJournalCheckPost();
    ;

    journalTableData = JournalTableData::newTable(journalTable);
    journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans,journalTableData);

    // Init JournalTable

    journalTable.clear();

    journalTable.JournalId = journalTableData.nextJournalId();
    journalTable.JournalType = InventJournalType::Movement;
    journalTable.JournalNameId = journalTableData.journalStatic().standardJournalNameId(journalTable.JournalType);

    journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(journalTable.JournalNameId));

    // Init JournalTrans
    select firstonly inventTable;
    for(cnt=1; cnt<4; cnt++)
    {
        journalTrans.clear();
        journalTransData.initFromJournalTable();

        journalTrans.TransDate = systemdateget(); 
        journalTrans.ItemId = inventTable.ItemId;
        journalTrans.Description = 'Testing';
        journalTrans.Qty = -100;
        journalTrans.CostPrice = 100;
        journalTrans.CostAmount = journalTrans.Qty * journalTrans.CostPrice;

        // Dimension details
        journalTrans.InventDimId = 'CWS-000121'; //InventDim::findOrCreate(inventDim).inventDimId;

        journalTransData.create();
    }

    journalTable.insert();

    // Call the static method to post the journal

    /*if(InventJournalCheckPost::newPostJournal(journalTable).validate())
    InventJournalCheckPost::newPostJournal(journalTable).run();*/
}


static void ssCreateTransferJournal(Args _args)
{
    InventJournalTable      journalTable;
    InventJournalTrans      journalTrans;
    InventJournalTableData  journalTableData;
    InventJournalTransData  journalTransData;
    InventTable             inventTable;
    InventDim               inventDim;
    Counter                 cnt;
    InventJournalCheckPost journalCheckPost = new InventJournalCheckPost();
    ;

    journalTableData = JournalTableData::newTable(journalTable);
    journalTransData = journalTableData.journalStatic().newJournalTransData(journalTrans,journalTableData);

    // Init JournalTable

    journalTable.clear();

    journalTable.JournalId = journalTableData.nextJournalId();
    journalTable.JournalType = InventJournalType::Movement;
    journalTable.JournalNameId = journalTableData.journalStatic().standardJournalNameId(journalTable.JournalType);

    journalTableData.initFromJournalName(journalTableData.journalStatic().findJournalName(journalTable.JournalNameId));

    // Init JournalTrans
    select firstonly inventTable;
    for(cnt=1; cnt<4; cnt++)
    {
        journalTrans.clear();
        journalTransData.initFromJournalTable();

        journalTrans.TransDate = systemdateget(); 
        journalTrans.ItemId = inventTable.ItemId;
        journalTrans.Description = 'Testing';
        journalTrans.Qty = -100;
        journalTrans.CostPrice = 100;
        journalTrans.CostAmount = journalTrans.Qty * journalTrans.CostPrice;

        // Dimension details
        journalTrans.InventDimId = 'CWS-000023'; //InventDim::findOrCreate(inventDim).inventDimId;

        journalTransData.create();
    }

    journalTable.insert();

    // Call the static method to post the journal

    /*if(InventJournalCheckPost::newPostJournal(journalTable).validate())
    InventJournalCheckPost::newPostJournal(journalTable).run();*/
}


No comments:

Post a Comment