Wednesday, June 8, 2011

Inventory Journal Import

I wrote a simple class to handle csv imports into Movement Journals.

1. Create a Movement Journal and go into the Lines screen.
2. Select Functions, Import Lines
3. Select a csv file with the columns: ItemId, Warehouse, Location, Batch, Qty, Cost.  (you can mod the code to expand this)
4. The lines are added to the journal.

You can download the xpo below, and here are some of the key bits of code.

Create a class (mine is called inventJournalImport) extending runbase.  Create a menu item (action) for the class and drop it on the InventJournalMovement form.

The main method of the class is as follows:
client static void main(Args args)
{
    inventJournalImport             inventJournalImport;
    Object                          formRunObject;
    JournalForm                     journalForm;
    InventJournalId                 inventJournalId;
    FormDataSource                  journalTrans_ds;
    ;

    inventJournalImport = new inventJournalImport();
    inventJournalImport.getLast();

    if (!args || !args.caller() || args.caller().name() != formStr(InventJournalMovement))
    {
        throw error(strfmt("This function must be called from the %1 form.", formStr(InventJournalMovement)));
    }

    if (formHasMethod(args.caller(), identifierstr(journalForm)))
    {
        formRunObject = args.caller();

        journalForm = formRunObject.journalForm();
        inventJournalImport.parmJournalForm(journalForm);  //needed to index numOfLines on inventJournalTable

        inventJournalId = journalForm.journalTableData().journalTable().JournalId;
        inventJournalImport.parmInventJournalId(inventJournalId);  //for defaulting on the imported lines

        journalTrans_ds = journalForm.journalTransData().journalTrans().dataSource();  //for .executeQuery, below
    }

    if (inventJournalImport.prompt())
    {
        inventJournalImport.ImportRecords();

        //refresh the grid
        journalTrans_ds.executeQuery();
    }
}

The ImportRecords method is below.  There is a dialog in the class that fills filename and transactionDate.
void ImportRecords()
{
    AsciiIo             asciiIo;
    container           con;
    FileIoPermission    fioPermission;

    InventJournalTrans  inventJournalTrans;
    InventDim           inventDim;

    wmsLocationId       wmsLocationId;
    inventLocationId    inventLocationId;
    inventBatchId       inventBatchId;

    boolean             useDefaultInventDim, useDefaultCost;
    int                 imported;
    ;

    if (WINAPI::fileExists(fileName))
    {
        //show wait cursor
        startLengthyOperation();

        // The AsciiIO.new method runs under code access permission.
        fioPermission = new FileIoPermission(fileName,"R");

        if (fioPermission == null)
        {
            info(strfmt("Not able to read file: %1",fileName));
            return;
        }

        // Code access permission scope starts here.
        fioPermission.assert();

        ttsbegin;

        asciiIo = new AsciiIo(fileName,"R");
        asciiIo.inFieldDelimiter(",");
        if (asciiIo != null)
        {
            con = asciiIo.read();

            while (asciiIo.status() == IO_Status::Ok)
            {
                try
                {
                    //1-itemId, 2-warehouse, 3-location, 4-batch, 5-qty, 6-cost
                    inventJournalTrans.clear();
                    inventJournalTrans.TransDate = transactionDate;
                    inventJournalTrans.JournalId = journalId;
                    inventJournalTrans.initFromInventJournalTable(inventJournalTrans.inventJournalTable());

                    inventJournalTrans.ItemId = strLTrim(strRTrim(conpeek(con,1)));

                    inventLocationId = strLTrim(strRTrim(conpeek(con,2)));
                    wmsLocationId = strLTrim(strRTrim(conpeek(con,3)));
                    inventBatchId = strLTrim(strRTrim(conpeek(con,4)));
                    if (inventLocationId || wmsLocationId || inventBatchId)
                    {
                        useDefaultInventDim = false;

                        inventDim.clear();
                        inventDim.InventLocationId = inventLocationId;
                        inventDim.wMSLocationId = wmsLocationId;
                        inventDim.inventBatchId = inventBatchId;
                        inventDim = InventDim::findOrCreate(inventDim);

                        inventJournalTrans.InventDimId = inventDim.inventDimId;
                    }
                    else
                    {
                        useDefaultInventDim = true;
                    }

                    inventJournalTrans.Qty = str2num(strLTrim(strRTrim(conpeek(con,5))));
                    inventJournalTrans.Qty= decround(inventJournalTrans.Qty,InventTable::inventDecimals(inventJournalTrans.ItemId));

                    if (inventJournalTrans.Qty > 0)
                    {
                        //import the cost if this is an addition to inventory
                        useDefaultCost = false;
                        inventJournalTrans.CostPrice          = str2num(strLTrim(strRTrim(conpeek(con,6))));
                        inventJournalTrans.PriceUnit          = 1;
                        inventJournalTrans.CostMarkup         = 0;
                        inventJournalTrans.CostAmount         = inventJournalTrans.calcCostAmount();
                    }
                    else
                    {
                        useDefaultCost = true;
                    }

                    inventJournalTrans.initFromInventTable(inventJournalTrans.inventTable(),false, useDefaultInventDim, useDefaultCost);

                    inventJournalTrans.insertFromCode();
                    imported++;

                    //updates the number of lines count on the journal table
                    journalForm.journalTableData().addTotal(inventJournalTrans);
                }
                catch (Exception::Error)
                {
                    exceptionTextFallThrough();
                }

                con = asciiIo.read();
            }
        }

        // revertAssert is not really necessary here because the method is ending.
        CodeAccessPermission::revertAssert();

        ttscommit;
        info (strfmt("%1 records imported", imported));

        //remove wait cursor
        endLengthyOperation();
    }
    else
    {
        error(strfmt("Import File not found: %1",fileName));
    }
}

That's the bulk of the code.  You can download the xpo below to see everything.

SharedProject_InventoryJournalImport.xpo (google docs - shortened via goo.gl - 12kb)

6 comments:

  1. Nate,

    I am having trouble with your solution - I get the following error when I complie the class:

    The table InventJournalTrans does not contain this function.

    It is refering to inventJournalTrans.insertFromCode() which does not exist in my database.

    Any suggestions?

    AX2009 SP 1


    ReplyDelete
    Replies
    1. Hi Mark,

      I haven't tested it, but I think you'd be fine using .insert() instead of .insertFromCode().

      The code was originally written for 4.0.

      Hope that helps,
      Nate

      Delete
  2. Nate,

    I forgot to thank you for your help on this, the insert() method worked for 2009.

    Thanks again.
    Mark

    ReplyDelete
  3. Alhamdulillah, May God Bless You. It works...

    ReplyDelete
  4. Thanks.This works.

    ReplyDelete