Showing posts with label SAc codes in ax 2012. Show all posts
Showing posts with label SAc codes in ax 2012. Show all posts

Saturday, 26 May 2018

Update Item Related HSN ,SAc codes in ax 2012

tatic void  Nar_ItemHSN_SACUpdate(Args _args)
{
    Dialog                            dialog;
    DialogField                       dialogFileName;
    Filename                          filename;
    SysOperationProgress              simpleProgress;
    FileIOPermission                  permission;
    Container                         filterCriteria;
    SysExcelApplication               application;
    SysExcelWorkbooks                 workbooks;
    SysExcelWorkbook                  workbook;
    SysExcelWorksheets                worksheets;
    SysExcelWorksheet                 worksheet;
    SysExcelCells                     cells;
    SysExcelCell                      cell;
    COMVariantType                    type;
    int                               row;
    InventTable                       inventtable;
    ItemId                            itemid;
    ItemType                          itemtype,itmtype;
    EcoResProductRecId                ecoresproductrecid;
    EcoResProductSearchName           productname;
    EcoResProductType                 producttype,prodtype;
    EcoResProductDisplayProductNumber productnumber;
    HSNCode_IN                        hsncode;
    SAC_IN                            sac;

    #File
    #avifiles

     str COMVariant2Str(COMVariant _cv, int _decimals = 0,int _characters = 0,int _separator1 = 0,int _separator2 = 0)
    {
        switch(_cv.variantType())
        {
            case (COMVariantType::VT_BSTR):
                return _cv.bStr();
            case (COMVariantType::VT_R4):
                return num2str(_cv.float(),_characters,_decimals, _separator1,_separator2);
            case (COMVariantType::VT_R8):
                return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);
            case (COMVariantType::VT_DECIMAL):
                return num2str(_cv.decimal(),_characters,_decimals, _separator1, _separator2);
            case (COMVariantType::VT_DATE):
                return date2str(_cv.date(),123,2,1,2, 1,4);
            case (COMVariantType::VT_EMPTY):
                return "";
            default:
                throw error(strfmt("@SYS26908",_cv.variantType()));
        }
        return "";
    }
    application = SysExcelApplication::construct();
    workbooks = application.workbooks();

    dialog = new Dialog("Importing Excel File");
    dialogFileName = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    filterCriteria = ['*.xls'];
    filterCriteria = dialog.filenameLookupFilter(filterCriteria);
    dialog.run();

    if (dialog.run())
    filename = dialogFileName.value();

    try
    {
    workbooks.open(filename);
    }
    catch (Exception::Error)
    {
    throw error(strFmt("File cannot be opened."));
    }

    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    row =1;
    do
    {
        row++;
        try
        {
            itemid              = COMVariant2Str(cells.item(Row,1).value());
            productnumber       = COMVariant2Str(cells.item(Row,2).value());
            ItemType            = str2enum(itmtype,cells.item(Row,3).value().bStr());
            hsncode             = COMVariant2Str(cells.item(Row,4).value());
            sac                 = COMVariant2Str(cells.item(Row,5).value());

            select inventtable where inventtable.ItemId == itemid
                                    && inventtable.Product  == EcoResProduct::findByDisplayProductNumber(productnumber).RecId
                                    && inventtable.ItemType == ItemType;
            if(inventtable.RecId)
            {
                ttsBegin;
                inventtable.selectForUpdate(true);
                inventtable.HSNCodeTable_IN = HSNCodeTable_IN::findByCode(hsncode);
                inventtable.ServiceAccountingCodeTable_IN = ServiceAccountingCodeTable_IN::findByServiceAccountingCode(sac);
                inventtable.update();
                ttsCommit;
                info(strFmt("%1 %2 updated to %3 ", hsncode, sac,itemid));
            }
        }
        catch
        {
             info(strFmt("%1 %2 not updated to %3 ", hsncode, sac,itemid));
        }

        type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);

    application.quit();

}