Code Delete From Entity for Items

 static void DeleteItemsFromExcelEntity(Args _args)

{

    SysExcelApplication         application;

    SysExcelWorkbooks           workbooks;

    SysExcelWorkbook            workbook;

    SysExcelWorksheets          worksheets;

    SysExcelWorksheet           worksheet;

    SysExcelCells               cells;

    COMVariantType              type;

    FilenameOpen                filename;

    Dialog                      dialog;

    DialogField                 dialogFilename;

    str                         itemId;

    int                         row;

    str                         targetCompany = "usmf"; // Change to your target legal entity (DataAreaId)


    InventTable                 inventTable;

    InventTrans                 inventTrans;


    #Excel


    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()));

        }

    }


    dialog = new Dialog("Delete Items in Specific Entity");

    dialogFilename = dialog.addField(extendedTypeStr(FilenameOpen), "Select Excel File");

    dialog.filenameLookupFilter(["@SYS28576", #XLSX, "@SYS28576", #XLS]);

    dialog.caption("Delete Items Job");


    if (!dialog.run())

        return;


    filename = dialogFilename.value();


    application = SysExcelApplication::construct();

    workbooks = application.workbooks();


    try

    {

        workbooks.open(filename);

    }

    catch

    {

        throw error("Cannot open Excel file.");

    }


    workbook = workbooks.item(1);

    worksheets = workbook.worksheets();

    worksheet = worksheets.itemFromNum(1);

    cells = worksheet.cells();


    row = 1;


    try

    {

        ttsBegin;


        changeCompany(targetCompany)

        {

            do

            {

                row++;

                itemId = COMVariant2Str(cells.item(row, 1).value());


                if (!itemId)

                    continue;


                select firstOnly inventTrans where inventTrans.ItemId == itemId;


                if (inventTrans.RecId)

                {

                    warning(strFmt("Item '%1' has transactions in company '%2'. Skipping.", itemId, targetCompany));

                    continue;

                }


                select forUpdate inventTable where inventTable.ItemId == itemId;


                if (inventTable.RecId)

                {

                    try

                    {

                        inventTable.delete();

                        info(strFmt("Deleted item '%1' from company '%2'.", itemId, targetCompany));

                    }

                    catch

                    {

                        error(strFmt("Error deleting item '%1'.", itemId));

                    }

                }

                else

                {

                    warning(strFmt("Item '%1' not found in company '%2'.", itemId, targetCompany));

                }


                type = cells.item(row + 1, 1).value().variantType();

            }

            while (type != COMVariantType::VT_EMPTY);

        }


        ttsCommit;

        info("Deletion process completed.");

    }

    catch

    {

        error("Exception occurred during item deletion.");

    }


    application.quit();

}


Comments

Popular Posts