Monday, 8 December 2014

Import Opening Stock in ax 2012

public class ImportOpeningStock extends RunBase
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    int row;
    InventJournalTrans          InventJournalTrans,_ValueJournalTrans,_ValueJournalTranslocal,_ValueJournalnum,_ValueJournalupdate;
    InventDim                   InventDim;
    InventTransOrigin           InventTransOrigin;
    InventJournalTransTaxExtensionIN   InventJournalTransTaxExtensionIN;
    TaxInformation_IN            TaxInformationIN;
    inventJournalTable            inventJournalTable;
    Filename                    filename,Jounralid;
    DialogField                 dialogFilename,dialogJounalid;
    counter                     icount,inserted;
    Amount                      amount;
    int                         cutoffdays,prevcutoffdays;
    currencycode                currencycode;
    str                         AccountNum,date1;
    args                        args;
    container productDimensions;
    #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]);
    return dialog;
}
public boolean getFromDialog()
{
    filename                = dialogFilename.value();
    return true;
}

void OpeningStockloc(JounalID  _JounalID)
{
    itemid                             itemId ;
    InventSiteId                       Site;
    InventLocationId                   Warehouse;
    InventDim                          _InventDim;
    RecId                              i,Countvalue;
    Qty                                mslQTY,CostPrice;
    InventJournalNameId             inventJournalName;
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File cannot be opened.");
    }
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    row = 1;
    do
    {
        row++;
        itemId      = cells.item(row, 1).value().bStr();
        Site        = cells.item(row, 3).value().bStr();
        Warehouse   = cells.item(row, 4).value().bStr();
        mslQTY      = cells.item(row, 7).value().double();
        //CostPrice   = cells.item(row, 6).value().double();
        ttsBegin;
        if(mslQTY)
        {
            select InventDim where InventDim.InventSiteId     == Site &&  InventDim.InventLocationId == Warehouse;
            InventDim.InventSiteId     = Site;
            InventDim.InventLocationId = Warehouse;
            _InventDim = InventDim::findOrCreate(InventDim);
             select forupdate _ValueJournalnum where _ValueJournalnum.JournalId   == _JounalID
                                                  && _ValueJournalnum.InventDimId ==  _InventDim.inventDimId
                                                  && _ValueJournalnum.ItemId      == itemId;
            if(!_ValueJournalnum.RecId)
            {
                inventJournalTrans.clear();
                inventJournalTable = InventJournalTable::find(_JounalID);
                inventJournalTrans.initFromInventJournalTable(inventJournalTable);
                inventJournalTrans.TransDate = systemDateGet();
                inventJournalTrans.ItemId = itemId;
                inventJournalTrans.initFromInventTable(InventTable::find(itemId));
                inventJournalTrans.Qty = mslQTY;
                inventDim.InventSiteId  = Site;
                inventDim.InventLocationId = Warehouse;
                inventJournalTrans.InventDimId = _InventDim.inventDimId;
                inventJournalTrans.insert();
            }
            else
            {
                select forupdate _ValueJournalupdate where _ValueJournalupdate.JournalId   == _JounalID
                                                        && _ValueJournalupdate.InventDimId == _InventDim.inventDimId
                                                        && _ValueJournalupdate.ItemId      == itemId;
                {
                    _ValueJournalupdate.Qty       = mslQTY;
                    _ValueJournalupdate.doupdate();
                }
            }
            ttsCommit;
       }
        type = cells.item(row+1, 1).value().variantType();
    }while (type != COMVariantType::VT_EMPTY);
    ttsBegin;
    select count(RecId) from _ValueJournalTranslocal where _ValueJournalTranslocal.JournalId ==_JounalID;
    Countvalue = _ValueJournalTranslocal.RecId;
    select forUpdate  InventJournalTable where InventJournalTable.JournalId == _JounalID;
    InventJournalTable.NumOfLines =  Countvalue;
    InventJournalTable.doUpdate();
    ttsCommit;
    application.quit();
}

// Here goes a description of the class
static ClassDescription description()
{
    return "Upload Opening stock from template";
}
static void main(Args  args)
{
    ImportOpeningStock      _ImportOpeningStock;
    JournalForm                 journalForm;
    InventJournalTable          journalTable;
    JounalID                _JounalID;
    ;
    journalForm         = JournalForm::fromArgs(args);
    journalTable        = journalForm.journalTableData().journalTable();
    _JounalID  =journalTable.JournalId;
    _ImportOpeningStock =  new ImportOpeningStock();
    if(_ImportOpeningStock.prompt())
    {
        _ImportOpeningStock.OpeningStockloc(_JounalID);
    }
}



Open other form with one form and get data form other form
Form init method:
public void init()
{
    CustTable       custTable;
    HcmWorker       hcmworker;
    Args  args = new Args();
    ;

    if (element.args().dataset() == tableNum (CustTable) && element.args().record())
    {
        custTable = element.args().record();
        custId    = custTable.AccountNum;
    }
    if (element.args().dataset() == tableNum (HcmWorker) && element.args().record())
    {
        hcmworker      = element.args().record();
        HcmWorkerRecId = hcmworker.RecId;
    }

    super();
  
}
DATASOURCE EXECUTE QUERY
public void executeQuery()
{
    if(HcmWorkerRecId)
    {
        this.query().dataSourceNo(1).addRange(fieldNum(CustomerSalesType,adcHcmWorkerRecId)).value(SysQuery::value(HcmWorkerRecId));

    }
    else if(custId)
    {
       this.query().dataSourceNo(1).addRange(fieldNum(CustomerSalesType,ADCCustomerAccountNum)).value(SysQuery::value(custId));

    }
    super();
}






No comments:

Post a Comment