DEV Community

Rohith_Veerapalli
Rohith_Veerapalli

Posted on

Delivery Remainder cancellation

using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;
/// <summary>
/// This class is used to create, udate the Loyalty memberships using excel uploader option
/// </summary>
internal final class AT_PODeliveryRemainderCancellationJob extends RunBaseBatch implements BatchRetryable
{
    public  container          purchId,failedPOLines;

    #define.CurrentVersion(2)
    #localmacro.CurrentList
        purchId,failedPOLines
        #endmacro




    /// <summary>
    ///  Stores parameters of the batch
    /// </summary>
    /// <returns>CurrentVersion</returns>
    public container pack()
    {
        return [#CurrentVersion, #CurrentList];
    }

    /// <summary>
    /// Returns the stored object for the batch to use
    /// </summary>
    /// <param name = "packedClass">CurrentList</param>
    /// <returns>true</returns>
    public boolean unpack(container packedClass)
    {
        Version version = runbase::getVersion(packedClass);

        switch (version)
        {
            case #CurrentVersion:
                [version, #CurrentList] = packedClass;
                break;

            default:
                return false;
        }

        return true;
    }

    /// <summary>
    /// Determines whether to run on server or client
    /// </summary>
    /// <returns>true</returns>

    public boolean runsImpersonated()
    {
        return true;
    }

    /// <summary>
    /// This method isused to prompt the dialog
    /// </summary>
    /// <returns>True</returns>
    boolean prompt()
    {
        return super();
    }

    /// <summary>
    /// This method is used to construct the instance of the class
    /// </summary>
    /// <returns>AT_PODeliveryRemainderCancellationJob</returns>
    public static AT_PODeliveryRemainderCancellationJob construct()
    {
        return new AT_PODeliveryRemainderCancellationJob();
    }

    /// <summary>
    /// This method is used to dispaly description of the job
    /// </summary>
    /// <returns>description</returns>
    public static ClassDescription description()
    {
        return "@ATG_Label01:AT_PODeliveryRemainderCancellationJob";
    }

    /// <summary>
    /// Describes whether the class is designed for execution in a new session.
    /// </summary>
    /// <returns>
    /// false.
    /// </returns>
    protected boolean canRunInNewSession()
    {
        return false;
    }

    //Needs to be set to true so class can be ran in a batch
    public boolean canGoBatch()
    {
        return true;
    }

    /// <summary>
    /// Specifies if the batch task is retryable for transient exceptions or not.
    /// </summary>
    /// <returns>
    /// If true is returned, the batch task is retryable, otherwise it is not.
    /// </returns>
    [Hookable(false)]
    final boolean isRetryable()
    {
        return true;
    }

    /// <summary>
    /// Class entry point. The system will call this method when a designated menu
    /// is selected or when execution starts and this class is set as the startup class.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {
        syslastvalue SysLastValue;
        #define.memberShipclass('AT_PODeliveryRemainderCancellationJob')

        delete_from SysLastValue where SysLastValue.elementName == #memberShipclass;
        AT_PODeliveryRemainderCancellationJob ImportClass = new AT_PODeliveryRemainderCancellationJob();
        ImportClass.importData();
        if(ImportClass.prompt())
        {
            ImportClass.runOperation();
        }

    }

    /// <summary>
    /// This method is used to run the actual process of updating and creating retail loyalty membership cards
    /// </summary>
    public void run()
    {
        purchLine PurchLine;
        PurchTable purchTable;
        PurchStatus PurchStatus;
        int j;

        for(int i=1;i<= conLen(purchId); i++)
        {
            PurchId purchIdNum = conPeek(purchId,i);

            purchTable = purchTable::find(purchIdNum,true);
            if(purchTable.DocumentState != VersioningDocumentState::Draft)
            {
                MenuFunction menuFunction = new MenuFunction(menuItemActionStr(PurchRequestChange),Menuitemtype::Action);
                Args args = new Args(menuFunction);
                VersioningPurchaseOrder versioningPurchaseOrder;
                args.parmEnum(VersioningAction::RequestChange);
                args.record(purchTable);
                versioningPurchaseOrder::main(args);
            }
                while select forupdate PurchLine
                where PurchLine.PurchId == purchIdNum
                {
                    try
                    {
                        ttsbegin;
                        purchLine.RemainInventPhysical  = 0;
                        // Set remaining physical Qty to zero
                        purchLine.RemainPurchPhysical   = 0;
                        //// We have to cancel the purchLine
                        //// Not necessary, I did this to do exactly like AX does
                        //  PurchStatus = purchLine.PurchStatus ;

                        // Update PurchLine
                        purchLine.update();
                        // This method will update the inventory transactions
                        InventMovement::bufferSetRemainQty(purchLine);
                        //purchTable = purchTable::find(purchIdNum,true);
                        //purchTable.WorkflowStatus = VersioningDocumentState::Draft;
                        //purchTable.update();
                        ttscommit;
                    }
                    catch
                    {
                        failedPOLines+= strFmt('Process failed for PO %1 and Line %2',PurchLine.PurchId, PurchLine.LineNumber);
                        continue;
                    }
                }

        }


        info(strFmt("Failed List",failedPOLines));
    }

    /// <summary>
    /// This method is used to read the excel file upload by user
    /// </summary>
    void importData()
    {
        System.IO.Stream                        stream;
        DialogGroup                             dlgUploadGroup;
        FileUploadBuild                         fileUploadBuild;
        FormBuildControl                        formBuildControl;
        #define.uploadFile('Upload file')
        #define.file('file')
        #define.upload('upload')
        #define.xlsx('.xlsx')

        Dialog           dialog =    new Dialog(#uploadFile);

        dlgUploadGroup          = dialog.addGroup(#file);
        formBuildControl        = dialog.formBuildDesign().control(dlgUploadGroup.name());
        fileUploadBuild         = formBuildControl.addControlEx(classstr(FileUpload), #upload);

        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
        fileUploadBuild.fileTypesAccepted(#xlsx);

        if (dialog.run() && dialog.closedOk())
        {
            FileUpload fileUploadControl     = dialog.formRun().control(dialog.formRun().controlId(#upload));
            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
            if(fileUploadResult != null && fileUploadResult.getUploadStatus())
            {
                stream = fileUploadResult.openResult();
                using (ExcelPackage Package = new ExcelPackage(stream))
                {
                    int rowCount, i,columncount;

                    Package.Load(stream);
                    ExcelWorksheet worksheet           = package.get_Workbook().get_Worksheets().get_Item(1);
                    OfficeOpenXml.ExcelRange range     = worksheet.Cells;

                    rowCount           = (worksheet.Dimension.End.Row) - (worksheet.Dimension.Start.Row)  + 1;
                    columncount        = (worksheet.Dimension.End.Column);

                    for (i = 1; i<= rowCount; i++)
                    {
                        if(i == 1)
                        {
                            continue;
                        }
                        purchId += conIns(str2con((range.get_Item(i,1).Value)),i);
                    }
                }
            }
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)