Thursday, 25 May 2017

Post the Purchase Journal in ax 2009

class       PurchJournalChangeType
extends     RunBaseBatch
{
    PurchaseType        purchaseType;
    QueryRun            journal;
    Query               query;

    DialogField         fieldContinue;
    PurchTable          purchTable;


    #define.CurrentVersion(1)
    #LOCALMACRO.ParmList
        PurchaseType
    #ENDMACRO
}


public boolean canGoBatchJournal()
{
    return true;
}

Object dialog()
{
    FormRun formRun = classfactory.createQueryForm(this);
    ;

    formRun.init();
    return formRun;
}

void new(PurchaseType  _purchaseType)
{
    QueryBuildDataSource    queryData;
    QueryBuildRange         queryRange;
    ;

    super();

    this.getLast();

    purchaseType = _purchaseType;

    query = new Query();

    query.addDataSource(tablenum(PurchTable));
    queryData   = query.dataSourceTable(tablenum(PurchTable));
    queryRange  = queryData.addRange(fieldnum(PurchTable, PurchaseType));
    queryRange.value(queryValue(PurchaseType::Journal));
    queryRange.status(1);  // locked

    queryData.addRange(fieldnum(PurchTable, PurchId));
    queryData.addRange(fieldnum(PurchTable, CreatedDateTime));

    journal = new QueryRun(query);
}

container pack()
{
    return [#CurrentVersion,#ParmList,journal.pack()];
}

public QueryRun queryRun()
{
    return journal;
}

void  run()
{
    #OCCRetryCount
    ;
    journal.query().dataSourceTable(tablenum(PurchTable)).update(true);
    journal.query().dataSourceTable(tablenum(PurchTable)).concurrencyModel(ConcurrencyModel::Optimistic);

    while (journal.next())
    {
        purchTable = journal.get(tablenum(PurchTable));

        setprefix(#PreFixField(PurchTable, PurchId));
        try
        {
            ttsbegin;

            purchTable.PurchaseType = purchaseType;

            if (!purchTable.validateWrite())
            {
                throw Exception::Error;
            }

            purchTable.write();
            ttscommit;
        }

        catch (Exception::Deadlock)
        {
            purchTable = PurchTable::find(purchTable.PurchId, true, ConcurrencyModel::Optimistic);
            retry;
        }

        catch (Exception::UpdateConflict)
        {
            if (appl.ttsLevel() == 0)
            {
                if (xSession::currentRetryCount() >= #RetryNum)
                {
                    throw Exception::UpdateConflictNotRecovered;
                }
                else
                {
                    purchTable = PurchTable::find(purchTable.PurchId, true, ConcurrencyModel::Optimistic);
                    retry;
                }
            }
            else
            {
                throw Exception::UpdateConflict;
            }
        }

        catch (Exception::Error)
        {
            checkFailed("@SYS25904");
        }
    }
}

public boolean runsImpersonated()
{
    return true;
}

boolean unpack(container packedClass)
{
    Integer         _version      = conpeek(packedClass,1);
    container       _packedQuery;

    switch (_version)
    {
        case #CurrentVersion:
            [_version,#ParmList,_packedQuery] = packedClass;
            if (_packedQuery)
                journal = new QueryRun(_packedQuery);
            break;

        default:
            return false;
    }

    return true;
}

static PurchJournalChangeType  construct(PurchaseType  purchaseType)
{
    return new PurchJournalChangeType(purchaseType);
}

static ClassDescription description()
{
    return "@SYS6146";
}

static void  main(Args  args)
{
    PurchJournalChangeType  purchJournalChangeType;
    ;

    if (!args)
        throw error("@SYS25407");

    purchJournalChangeType = PurchJournalChangeType::construct(args.parmEnum());

    if (purchJournalChangeType.prompt())
        purchJournalChangeType.run();
}



Wednesday, 17 May 2017

LoadStandardCost through X++ in ax 2009

static void FS_Load_Standard_Cost_DB(Args _args)
{

//scapistrant-20100903
//Mods 2010-12-29 per DBucher

    inventitempricesim          inventitempricesim;

    FileName                    fileName;
    SysExcelApplication         excelApp;

    boolean                         rowFound = true;
    boolean                         TrueFalse = true;

    counter                         readCounter   = 0;
    counter                         ErrCounter    = 0;
    counter                         InsertCounter = 0;
    counter                         updateCounter = 0;

    Itemid                          fsName;

    int                             lineNum = 4;
    int                             lineNumEnd = 999999;

    Dialog                          dialog;
    DialogField                     dialogFieldFileName;
    DialogField                     dialogFieldStartLineNum;
    DialogField                     dialogFieldEndLineNum;
    DialogField                     dialogFieldInventDimId;


    SysOperationProgress            progress = new SysOperationProgress();
    #AviFiles

;

    progress.setAnimation(#AviUpdate);
    progress.setCaption("Load Standard Cost into InventItemPriceSim");

     dialog              = new Dialog('Load Standard Price into InventItemPriceSim table');
    dialogFieldFileName = dialog.addField(typeId(FileNameOpen), 'File name', 'Filename containing input data');
    dialogFieldFileName.lookupButton(2);
    dialogFieldStartLineNum = dialog.addField(typeid(Integer),"Starting Liine Number","start loading records at line number specified");
    dialogFieldStartLineNum.value(lineNum);
    dialogFieldEndLineNum = dialog.addField(typeid(Integer),"Ending Liine Number","stop loading records at line number specified");
    dialogFieldEndLineNum.value(lineNumEnd);
    dialogFieldInventDimId = dialog.addField(typeid(inventDimId),"Enter InventDimId relating to InventDimTable filtered by Site ID for AE");


    if (dialog.run())
    {
        fileName = dialogFieldFileName.value();
        if(!fileName)
        {
            info(strfmt('No file name specified'));
            return;
        }

        if (dialogFieldStartLineNum.value() > 0)
        {
            lineNum = dialogFieldStartLineNum.value();
        }

        if (dialogFieldEndLineNum.value() > 0)
        {
            lineNumEnd = dialogFieldEndLineNum.value();
        }


        ExcelApp = SysExcelApplication::construct();
        ExcelApp.workbooks().open(filename);
        try
        {
          while (rowFound && ExcelApp.activeSheet().cells().range(strfmt('A%1',lineNum)).item(lineNum))
          {
            readCounter++;


            if (lineNum > lineNumEnd)
            {
                rowfound = false;
                continue;
            }

            if (ExcelApp.activeSheet().cells().item(lineNum, 1).value().variantType() == ComVariantType::VT_EMPTY &&
                ExcelApp.activeSheet().cells().item(lineNum, 2).value().variantType() == ComVariantType::VT_EMPTY)
            {
                 rowfound = false;
                 continue;
            }

            //load data
            ttsbegin;
            //values provided by Jeffery

            //inventItemPriceSim.ItemId = ExcelApp.activeSheet().cells().item(lineNum, 1).value().bstr();
            if (ExcelApp.activeSheet().cells().item(lineNum, 1).value().varianttype() == comVariantType::VT_R8)
                {
                    inventItemPriceSim.ItemId    = num2str(ExcelApp.activeSheet().cells().item(lineNum, 1).value().double(),0,0,2,0);//item ID-verify both
                }
                else
                {
                    inventItemPriceSim.ItemId    = ExcelApp.activeSheet().cells().item(lineNum, 1).value().bstr(); //item ID-verify both
                }
            info(ExcelApp.activeSheet().cells().item(lineNum, 1).value().bstr());
            info(num2str(ExcelApp.activeSheet().cells().item(lineNum, 1).value().double(),0,0,2,0));
            inventItemPriceSim.VersionId = "Std-2011fe";  //verify from accounting
            inventItemPriceSim.FromDate = str2date("01-31-2011",213);  // verify with acconting
            inventItemPriceSim.PriceType = CostingVersionPriceType::Cost;  // never change
            inventItemPriceSim.Price =  ExcelApp.activeSheet().cells().item(lineNum, 3).value().double();  //unit price
            inventItemPriceSim.PriceUnit = 1;  // always a 1
            inventItemPriceSim.UnitID = ExcelApp.activeSheet().cells().item(lineNum, 2).value().bstr();  //unit of measure
            inventItemPriceSim.InventDimId =  dialogFieldInventDimId.value();  //never changes
            inventItemPriceSim.insert();
            ttscommit;
            InsertCounter ++;
            lineNum ++;

          }
        }

        catch
        {
            excelapp.workbooks().close();
            excelapp.quit();
            info(strfmt('Error occured at rcd %1 for Item: %2', lineNum, fsName));
        }

    info("Records Read " + int2str(readCounter));
    info("Records Inserted " + int2str(insertCounter));

    }
    return;
}