Asset reservation posting class with mutiple method

 class IHSReserveEAAssets

{

    IHSAssetMovementHeader      ihsAssetMovementHeader;

    EntAssetRequestTable        entAssetRequestTable;

    str                         output;

    boolean                     checkToStatus;

    boolean                     getToStatus;

    EntAssetWorkOrderTable      workOrderTable;

    EntAssetWorkOrderTable      workOrderTablePerformWork;

    EntAssetWorkOrderTable      workOrderTableQualityCheck;


    EntAssetWorkOrderRecID      materialWorkOrderRecid;

    EntAssetWorkOrderRecID      performWorkOrderRecid;

    EntAssetWorkOrderRecID      qualityCheckWorkOrderRecid;

    container                   conInfo;

    int                         i;

    boolean                     failed = false;

    InventTransferId            transferCreated;

    boolean                     createdWorkorder = false;


    EntAssetObjectTable         entAssetObjectTableGlobal;

    str                         returnMessage;

    //InventTransferTable     inventTransferTable;

    



    public static void main(Args    _args)

    {

        IHSReserveEAAssets  reserveEAAssets = new IHSReserveEAAssets();

        FormName formName = _args.callerName();


        IHSAssetMovementHeader  ihsAssetMovementHeader;

        EntAssetRequestTable    entAssetRequestTable;

        FormDataSource          datasource;

       

        if (formName == formStr(IHSAssetMovementForm))

        {

            ihsAssetMovementHeader = _args.record();

            reserveEAAssets.parmIHSAssetMovementHeader(ihsAssetMovementHeader);

        }

        else if (formName == formStr(EntAssetRequestTable))

        {

            entAssetRequestTable = _args.record();

            reserveEAAssets.parmEntAssetRequestTable(entAssetRequestTable);

        }


        if (ihsAssetMovementHeader.RecId || entAssetRequestTable.RecId)

        {

            reserveEAAssets.run();


            if (_args.record() && FormDataUtil::getFormDataSource(_args.record()))

            {

                datasource = FormDataUtil::getFormDataSource(_args.record());

                datasource.research(true);

            }

        }

        else

        {

            Info("Please inform me in case of any issue.");

        }

    }


    public void run()

    {

        EntAssetParameters          parameters = EntAssetParameters::find();

        EntAssetObjectTable         deleteObjectTable;

        IHSAssetRequestFulfilment   deleteAssetRequestFulfilment;

        InventTransferTable         deleteInventTransferTable;

         

        ttsBegin;

        try

        {

        

        if (ihsAssetMovementHeader.RecId)

        {

            this.reserveAssets();

        }

        else if (entAssetRequestTable.RecId)

        {

            //this.reserveAssetsV2();

            if(IHSAssetRequestFulfilment::existRequestId(entAssetRequestTable.RequestId))

            {

                if(this.validationsCheck())

                {

                    this.reserveAssetsV3();

                }

            }

            else

            {

                 warning(strFmt("No Fulfilment record found in maintance request %1", entAssetRequestTable.RequestId)); 

            }

        }

        // Added by VT

      //  getToStatus = this.parmCheckToStatus();


        if((inventTransferTable::exist(transferCreated) || createdWorkorder) && parameters.IHSGenerateWorkOrder == NoYes::Yes)//EntAssetWorkOrderLifecycleState::findID(EntAssetRequestLifecycleState::find(entAssetRequestTable.RequestLifecycleState).Name).IHSGenerateWorkOrder == NoYes::Yes)

        {

            

            

                conInfo = conNull();

                i       = 0;

               

                    if(entAssetRequestTable::existIHSIsMaterialMovement(entAssetRequestTable.RecId))

                    {

                        this.createWorkOrderTableMaterialMovement(); // get header recid


                        if(this.parmMaterialWorkOrderRecid())

                        {

                            this.createWorkOrderLineMaterialMovement(entAssetRequestTable); //Only Execute if Headr rec found

                        }

                    }

                    //add check dont execute the next if pre fails

                    if(entAssetRequestTable::existIHSIsPerformWork(entAssetRequestTable.RecId))

                    {

                        this.createWorkOrderTablePerformWork();// get header recid


                        if(this.parmPerformWorkOrderRecid())

                        {

                            this.createWorkOrderLinePerformWork(entAssetRequestTable);//Only Execute if Headr rec found

                        }


                    }

                    //add check dont execute the next if pre fails

                    if(entAssetRequestTable::existIHSIsPerformQc(entAssetRequestTable.RecId))

                    {

                        this.createWorkOrderTableQualityCheck();// get header recid

                

                        if(this.parmQualityCheckWorkOrderRecid())

                        {

                            this.createWorkOrderLineQualityCheck(entAssetRequestTable);// Only Execute if Headr rec found

                        }

                    }


                    if(inventTransferTable::exist(transferCreated))

                    {                       

                        Info(strFmt("Assets reserved under transfer order number %1",transferCreated));


                        EntAssetRequestTable                     assetRequestTableUpdate;

                        ttsbegin;

                        assetRequestTableUpdate = EntAssetRequestTable::find(entAssetRequestTable.RequestId);

                        assetRequestTableUpdate.selectForUpdate(true);

                        assetRequestTableUpdate.IHSTransferOrder = transferCreated;

                        assetRequestTableUpdate.update();

                        ttscommit;                             

                    }

                    else

                    {

                        //this helps to update the parent work order id from maintance request to the asset if To is not created --> Start

                        if(!transferCreated && createdWorkorder)

                        {

                            EntAssetObjectTable entAssetObjectTableWO, childEntAssetObjectTableWO;

                            IHSAssetRequestFulfilment   ihsAssetRequestFulfilmentWO;

                            EntAssetRequestTable    assetRequestTableWO = EntAssetRequestTable::findRecID(entAssetRequestTable.RecId, true);


                        while select ihsAssetRequestFulfilmentWO

                                where ihsAssetRequestFulfilmentWO.RequestId == assetRequestTableWO.RequestId

                            {

                                entAssetObjectTableWO = EntAssetObjectTable::find(ihsAssetRequestFulfilmentWO.ObjectID, true);


                                entAssetObjectTableWO.Reserved         = NoYes::Yes;

                                entAssetObjectTableWO.ReservedBy       = EntAssetWorkOrderTable::findRecId(assetRequestTableWO.WorkOrder).WorkOrderId;

                                entAssetObjectTableWO.AssetStateStatus = IHSAssetStateStatus::IsWIP; //Validation check done by Vivek as suggested by Sai.

                                entAssetObjectTableWO.update();


                            while select forupdate childEntAssetObjectTableWO

                                    where childEntAssetObjectTableWO.ParentObject == entAssetObjectTableWO.RecId

                                {

                                    childEntAssetObjectTableWO.Reserved    = NoYes::Yes;

                                    childEntAssetObjectTableWO.ReservedBy  = EntAssetWorkOrderTable::findRecId(assetRequestTableWO.WorkOrder).WorkOrderId;

                                    childEntAssetObjectTableWO.AssetStateStatus = IHSAssetStateStatus::IsWIP; //Validation check done by Vivek as suggested by Sai.

                                    childEntAssetObjectTableWO.update();

                                }

                            }

                        }

                        //this helps to update the parent work order id from maintance request to the asset if To is not created --> End

                    }


                    for(i=1; i<=conLen(conInfo); i++)

                    {                        

                        Info(strFmt("Work order is created %1", conPeek(conInfo, i)));

                    }

                    

                    this.parmOutput(IHSReserveEAAssets::GetInfoLogMessages());

                    

        }


            ttsCommit;

            

        }

        catch(Exception::Error)

        {

            this.parmOutput(IHSReserveEAAssets::GetInfoLogMessages());

            failed = true;

            ttsabort;

        }

        catch(Exception::CLRError)

        {

            this.parmOutput(IHSReserveEAAssets::GetInfoLogMessages());

            failed = true;

            ttsabort;

        }

                

        // Added by Vt

    }


    /// <summary>

    /// Method to check if the validations are met or not.

    /// </summary>

    /// <returns>boolean value</returns>

    public boolean validationsCheck()

    {

        boolean ret = true;


        EntAssetRequestTable            localEntAssetRequestTable = entAssetRequestTable;

        IHSAssetRequestFulfilment       ihsAssetRequestFulfilment;

        EntAssetObjectTable             entAssetObjectTable;

        EntAssetParameters              entAssetParameters;

        EntAssetObjectLifecycleState    entAssetObjectLifecycleState;


        entAssetParameters              = EntAssetParameters::find();

        entAssetObjectLifecycleState    = EntAssetObjectLifecycleState::findID(entAssetParameters.AOLStatePreTransfer);


        while select ihsAssetRequestFulfilment

                where ihsAssetRequestFulfilment.RequestId == localEntAssetRequestTable.RequestId

                    join entAssetObjectTable

                where entAssetObjectTable.ObjectID == ihsAssetRequestFulfilment.ObjectID

                    &&  entAssetObjectTable.ObjectLifecycleState == entAssetObjectLifecycleState.RecId

        {

            entAssetObjectTable = EntAssetObjectTable::find(ihsAssetRequestFulfilment.ObjectID);

            if(entAssetObjectTable.RecId && entAssetObjectTable.Reserved == NoYes::No)

            {

                if(entAssetObjectLifecycleState.ObjectLifecycleStateId != entAssetParameters.IHSObjectLifecycleStateId)

                {

                    warning(strFmt("Current lifecycle state for the asset: %1 is %2.", entAssetObjectTable.ObjectID, entAssetObjectLifecycleState.ObjectLifecycleStateId));

                    ret = false;

                }

                if(entAssetObjectTable.ReservedBy != "")

                {

                    warning(strFmt("The asset: %1 is already reserved by %2.", entAssetObjectTable.ObjectID, entAssetObjectTable.ReservedBy));

                    ret = false;

                }

                if(entAssetObjectTable.AssetStateStatus != IHSAssetStateStatus::IsStocked && entAssetObjectTable.AssetStateStatus != IHSAssetStateStatus::IsWIP)

                {

                    warning(strFmt("The asset state status for the asset: %1 is %2.", entAssetObjectTable.ObjectID, entAssetObjectTable.AssetStateStatus));

                    ret = false;

                }

            }

        }


        return ret;

    }


     static str GetInfoLogMessages()

    {

        SysInfologEnumerator    sysInfologEnumerator;

        SysInfologMessageStruct infoMessageStruct;

        str                     logMessage;

        const str NewLine       = '\n';


        sysInfologEnumerator = SysInfologEnumerator::newData(infolog.export());

    

        while (sysInfologEnumerator.moveNext())

        {

            int i = 1;

            if (logMessage)

            {

                logMessage += Newline;

            }

            infoMessageStruct = SysInfologMessageStruct::construct(sysInfologEnumerator.currentMessage());

        

            while (i <= infoMessageStruct.prefixDepth())

            {

                logMessage += infoMessageStruct.preFixTextElement(i) + '. ';

                i++;

            }

            logMessage += infoMessageStruct.message();

        }

        return logMessage;

    }


    /// <summary>

    /// Creates a new work order table.

    /// </summary>

    protected void createWorkOrderTableMaterialMovement()

    {

        EntAssetRequestTable         requestTableLocalUpdate;

        NumberSeq   numberSeq;

    

        if (workOrderTable)

        {

            return;

        }

    

        setPrefix("@EnterpriseAssetManagementAppSuite:EntAsset7");

    

        if (!workOrderTable.WorkOrderID)

        {

            numberSeq = NumberSeq::newGetNum(EntAssetParameters::numRefWorkOrderId(), true);

            workOrderTable.WorkOrderID = numberSeq.num();

        

            workOrderTable.initFromRequestTable(entAssetRequestTable);

            workOrderTable.Description = entAssetRequestTable.RequestId + '-' + entAssetRequestTable.Description;//EntAssetJobType::findRecId(entAssetRequestTable.JobType).JobTypeID;

             

            if (workOrderTable.ServiceLevel)

            {

                workOrderTable.initFromServiceLevel(workOrderTable.ServiceLevel);

            }


            workOrderTable.WorkOrderType    = IHSMaintenanceJobTypeSetup::findByJobType(entAssetRequestTable.IHSMaterialMovementJobType).WorkOrderType;

            

            // asset movment fill start

            workOrderTable.IsLogistics                  = entAssetRequestTable.IsLogistics;

            workOrderTable.Department                   = entAssetRequestTable.Department;

            workOrderTable.VendAccount                  = entAssetRequestTable.VendAccount;

            workOrderTable.IHSTransferType              = entAssetRequestTable.IHSTransferType;

            workOrderTable.FromSite                     = entAssetRequestTable.FromSite;

            workOrderTable.ToSite                       = entAssetRequestTable.ToSite;

            workOrderTable.FromLocationId               = entAssetRequestTable.FromLocationId;

            workOrderTable.ToLocationId                 = entAssetRequestTable.ToLocationId;

            workOrderTable.FromwMSLocationId            = entAssetRequestTable.FromwMSLocationId;

            workOrderTable.TowMSLocationId              = entAssetRequestTable.TowMSLocationId;

            workOrderTable.IHSReasonName                = entAssetRequestTable.IHSReasonName;

            workOrderTable.IHSIsCounter                 = entAssetRequestTable.IHSIsCounter;

            workOrderTable.IHSPurpose                   = entAssetRequestTable.IHSPurpose;

            workOrderTable.IHSPurposeTypes              = entAssetRequestTable.IHSPurposeTypes;

            workOrderTable.FromLocationId               = entAssetRequestTable.FromLocationId;

            workOrderTable.IHSFromFunctionalLocation    = entAssetRequestTable.IHSFromFunctionalLocation;

            workOrderTable.IHSToFunctionalLocation      = entAssetRequestTable.IHSToFunctionalLocation;

            workOrderTable.FunctionalLocation           = entAssetRequestTable.IHSFromFunctionalLocation;

            workOrderTable.IHSStopGap                   = entAssetRequestTable.IHSStopGap;


            workOrderTable.IHSIsMaterialMovement         = entAssetRequestTable.IHSIsMaterialMovement;

            workOrderTable.IHSMaterialMovementJobType    = entAssetRequestTable.IHSMaterialMovementJobType;

            workOrderTable.IHSMaterialMovementJobVariant = entAssetRequestTable.IHSMaterialMovementJobVariant;


          //  workOrderTable.IHSIsPerformWork         = entAssetRequestTable.IHSIsPerformWork;

            workOrderTable.IHSPerformWorkJobType    = entAssetRequestTable.IHSPerformWorkJobType;

            workOrderTable.IHSPerformWorkJobVariant = entAssetRequestTable.IHSPerformWorkJobVariant;


          //  workOrderTable.IHSIsPerformQc           = entAssetRequestTable.IHSIsPerformQc;

            workOrderTable.IHSPerformQcJobType      = entAssetRequestTable.IHSPerformQcJobType;

            workOrderTable.IHSPerformQcJobVariant   = entAssetRequestTable.IHSPerformQcJobVariant;


            workOrderTable.IHSDelivery                                  = entAssetRequestTable.IHSDelivery;

            workOrderTable.IHSDriverContact                             = entAssetRequestTable.IHSDriverContact;

            workOrderTable.IHSDriverName                                = entAssetRequestTable.IHSDriverName;

            workOrderTable.IHSInternalDriverContact                     = entAssetRequestTable.IHSInternalDriverContact;

            workOrderTable.IHSDriverMaster                              = entAssetRequestTable.IHSDriverMaster;

            workOrderTable.IHSLogisticsPlanIntExt                       = entAssetRequestTable.IHSLogisticsPlanIntExt;

            workOrderTable.IHSPickUp                                    = entAssetRequestTable.IHSPickUp;

            workOrderTable.IHSPurchAgreement                            = entAssetRequestTable.IHSPurchAgreement;

            workOrderTable.IHSSerialNumber                              = entAssetRequestTable.IHSSerialNumber;

            workOrderTable.IHSFromFunctionalLocationSrcLocation         = entAssetRequestTable.IHSFromFunctionalLocation;

            workOrderTable.IHSTransferOrder                             = transferCreated;//entAssetRequestTable.IHSTransferOrder;

            workOrderTable.IHSTruckNumber                               = entAssetRequestTable.IHSTruckNumber;

            workOrderTable.IHSVendAccount                               = entAssetRequestTable.IHSVendAccount;

            workOrderTable.IHSService                                   = entAssetRequestTable.IHSService;


            workOrderTable.IHSIsToShipped           = NoYes::No;

            workOrderTable.IHSIsToReceived          = NoYes::No;

            // asset movement fill stop

        }

        

        if (workOrderTable.validateWrite())

        {

            workOrderTable.insert();


            this.parmMaterialWorkOrderRecid(workOrderTable.RecId);

    

            if (numberSeq)

            {

                numberSeq.used();

            }

        }

        else

        {

            if (numberSeq)

            {

                numberSeq.abort();

            }

    

            throw error(strfmt("@SYS4007721", tablePname(EntAssetWorkOrderTable)));

        }


        ttsBegin;

        requestTableLocalUpdate = EntAssetRequestTable::findRecID(entAssetRequestTable.RecId, true);

        requestTableLocalUpdate.WorkOrder = workOrderTable.RecId;

    

        if (requestTableLocalUpdate.validateWrite())

        {

            requestTableLocalUpdate.update();

        }

        ttscommit;

    }


    /// <summary>

    /// Creates a new work order table.

    /// </summary>

    protected void createWorkOrderTablePerformWork()

    {

        NumberSeq   numberSeq;

        EntAssetRequestTable         requestTableLocalPerform;

    

        if (workOrderTablePerformWork)

        {

            return;

        }

    

        setPrefix("@EnterpriseAssetManagementAppSuite:EntAsset7");

    

        if (!workOrderTablePerformWork.WorkOrderID)

        {

            numberSeq = NumberSeq::newGetNum(EntAssetParameters::numRefWorkOrderId(), true);

            workOrderTablePerformWork.WorkOrderID = numberSeq.num();

        

            workOrderTablePerformWork.initFromRequestTable(entAssetRequestTable);

            workOrderTablePerformWork.Description = entAssetRequestTable.RequestId + '-' + entAssetRequestTable.Description;//+ EntAssetJobType::findRecId(entAssetRequestTable.JobType).JobTypeID;

             

            if (workOrderTablePerformWork.ServiceLevel)

            {

                workOrderTablePerformWork.initFromServiceLevel(workOrderTablePerformWork.ServiceLevel);

            }


            workOrderTablePerformWork.WorkOrderType    = IHSMaintenanceJobTypeSetup::findByJobType(entAssetRequestTable.IHSPerformWorkJobType).WorkOrderType;

            

            // asset movment fill start

            workOrderTablePerformWork.IsLogistics          = entAssetRequestTable.IsLogistics;

            workOrderTablePerformWork.Department           = entAssetRequestTable.Department;

            workOrderTablePerformWork.VendAccount          = entAssetRequestTable.VendAccount;

            workOrderTablePerformWork.IHSTransferType      = entAssetRequestTable.IHSTransferType;

            workOrderTablePerformWork.FromSite             =   entAssetRequestTable.FromSite;

            workOrderTablePerformWork.ToSite               = entAssetRequestTable.ToSite;

            workOrderTablePerformWork.FromLocationId       = entAssetRequestTable.FromLocationId;

            workOrderTablePerformWork.ToLocationId         = entAssetRequestTable.ToLocationId;

            workOrderTablePerformWork.FromwMSLocationId    = entAssetRequestTable.FromwMSLocationId;

            workOrderTablePerformWork.TowMSLocationId      = entAssetRequestTable.TowMSLocationId;

            workOrderTablePerformWork.IHSReasonName        = entAssetRequestTable.IHSReasonName;

            workOrderTablePerformWork.IHSIsCounter         = entAssetRequestTable.IHSIsCounter;

            workOrderTablePerformWork.IHSPurpose           = entAssetRequestTable.IHSPurpose;

            workOrderTablePerformWork.IHSPurposeTypes      = entAssetRequestTable.IHSPurposeTypes;

            workOrderTablePerformWork.FromLocationId       = entAssetRequestTable.FromLocationId;

            workOrderTablePerformWork.FunctionalLocation   = entAssetRequestTable.IHSFromFunctionalLocation;

            workOrderTablePerformWork.IHSFromFunctionalLocation    = entAssetRequestTable.IHSFromFunctionalLocation;

            workOrderTablePerformWork.IHSToFunctionalLocation      = entAssetRequestTable.IHSToFunctionalLocation;

            workOrderTablePerformWork.IHSStopGap           = entAssetRequestTable.IHSStopGap;


          //  workOrderTablePerformWork.IHSIsMaterialMovement         = entAssetRequestTable.IHSIsMaterialMovement;

            workOrderTablePerformWork.IHSMaterialMovementJobType    = entAssetRequestTable.IHSMaterialMovementJobType;

            workOrderTablePerformWork.IHSMaterialMovementJobVariant = entAssetRequestTable.IHSMaterialMovementJobVariant;


            workOrderTablePerformWork.IHSIsPerformWork         = entAssetRequestTable.IHSIsPerformWork;

            workOrderTablePerformWork.IHSPerformWorkJobType    = entAssetRequestTable.IHSPerformWorkJobType;

            workOrderTablePerformWork.IHSPerformWorkJobVariant = entAssetRequestTable.IHSPerformWorkJobVariant;


          //  workOrderTablePerformWork.IHSIsPerformQc           = entAssetRequestTable.IHSIsPerformQc;

            workOrderTablePerformWork.IHSPerformQcJobType      = entAssetRequestTable.IHSPerformQcJobType;

            workOrderTablePerformWork.IHSPerformQcJobVariant   = entAssetRequestTable.IHSPerformQcJobVariant;


            if(this.parmMaterialWorkOrderRecid())

            {

                workOrderTablePerformWork.ParentWorkOrder          = this.parmMaterialWorkOrderRecid();

            }


            workOrderTablePerformWork.IHSLogisticsPlanIntPerformWork        = entAssetRequestTable.IHSLogisticsPlanIntExtPerformWork;

            workOrderTablePerformWork.IHSPurchAgreementPerformWork          = entAssetRequestTable.IHSPurchAgreementPerformWork;

            workOrderTablePerformWork.IHSTransferOrder                      = transferCreated;

            workOrderTablePerformWork.IHSVendAccountPerformWork             = entAssetRequestTable.IHSVendAccountPerformWork;

            workOrderTablePerformWork.IHSServicePerformWork                 = entAssetRequestTable.IHSServicePerformWork;

            workOrderTablePerformWork.IHSRfi                                = entAssetRequestTable.IHSRfi;


            workOrderTablePerformWork.IHSIsToShipped           = NoYes::No;

            workOrderTablePerformWork.IHSIsToReceived          = NoYes::No;


            // asset movement fill stop

        }

    

        if (workOrderTablePerformWork.validateWrite())

        {

            workOrderTablePerformWork.insert();

            this.parmPerformWorkOrderRecid(workOrderTablePerformWork.RecId);

           // info(strFmt(" has been created %1", workOrderTablePerformWork.WorkOrderId));


    

            if (numberSeq)

            {

                numberSeq.used();

            }

        }

        else

        {

            if (numberSeq)

            {

                numberSeq.abort();

            }

    

            throw error(strfmt("@SYS4007721", tablePname(EntAssetWorkOrderTable)));

        }


        ttsBegin;

        requestTableLocalPerform = EntAssetRequestTable::findRecID(entAssetRequestTable.RecId, true);


        if(!requestTableLocalPerform.WorkOrder)

        {

            requestTableLocalPerform.WorkOrder = workOrderTable.RecId;

    

            if (requestTableLocalPerform.validateWrite())

            {

                requestTableLocalPerform.update();

            }

        }

        ttscommit;

    }


    /// <summary>

    /// Creates a new work order table.

    /// </summary>

    protected void createWorkOrderTableQualityCheck()

    {

        NumberSeq   numberSeq;

        EntAssetRequestTable    requestTableLocalQualityCheck;

    

        if (workOrderTableQualityCheck)

        {

            return;

        }

    

        setPrefix("@EnterpriseAssetManagementAppSuite:EntAsset7");

    

        if (!workOrderTableQualityCheck.WorkOrderID)

        {

            numberSeq = NumberSeq::newGetNum(EntAssetParameters::numRefWorkOrderId(), true);

            workOrderTableQualityCheck.WorkOrderID = numberSeq.num();

        

            workOrderTableQualityCheck.initFromRequestTable(entAssetRequestTable);

            workOrderTableQualityCheck.Description = entAssetRequestTable.RequestId + '-' + entAssetRequestTable.Description;//+ EntAssetJobType::findRecId(entAssetRequestTable.JobType).JobTypeID;

             

            if (workOrderTableQualityCheck.ServiceLevel)

            {

                workOrderTableQualityCheck.initFromServiceLevel(workOrderTableQualityCheck.ServiceLevel);

            }


            workOrderTableQualityCheck.WorkOrderType    = IHSMaintenanceJobTypeSetup::findByJobType(entAssetRequestTable.IHSPerformQcJobType).WorkOrderType;


            // asset movment fill start

            workOrderTableQualityCheck.IsLogistics          = entAssetRequestTable.IsLogistics;

            workOrderTableQualityCheck.Department           = entAssetRequestTable.Department;

            workOrderTableQualityCheck.VendAccount          = entAssetRequestTable.VendAccount;

            workOrderTableQualityCheck.IHSTransferType      = entAssetRequestTable.IHSTransferType;

            workOrderTableQualityCheck.FromSite             =   entAssetRequestTable.FromSite;

            workOrderTableQualityCheck.ToSite               = entAssetRequestTable.ToSite;

            workOrderTableQualityCheck.FromLocationId       = entAssetRequestTable.FromLocationId;

            workOrderTableQualityCheck.ToLocationId         = entAssetRequestTable.ToLocationId;

            workOrderTableQualityCheck.FromwMSLocationId    = entAssetRequestTable.FromwMSLocationId;

            workOrderTableQualityCheck.TowMSLocationId      = entAssetRequestTable.TowMSLocationId;

            workOrderTableQualityCheck.IHSReasonName        = entAssetRequestTable.IHSReasonName;

            workOrderTableQualityCheck.IHSIsCounter         = entAssetRequestTable.IHSIsCounter;

            workOrderTableQualityCheck.IHSPurpose           = entAssetRequestTable.IHSPurpose;

            workOrderTableQualityCheck.IHSPurposeTypes      = entAssetRequestTable.IHSPurposeTypes;

            workOrderTableQualityCheck.FromLocationId       = entAssetRequestTable.FromLocationId;

            workOrderTableQualityCheck.FunctionalLocation   = entAssetRequestTable.IHSFromFunctionalLocation;

            workOrderTableQualityCheck.IHSFromFunctionalLocation    = entAssetRequestTable.IHSFromFunctionalLocation;

            workOrderTableQualityCheck.IHSToFunctionalLocation      = entAssetRequestTable.IHSToFunctionalLocation;

            workOrderTableQualityCheck.IHSStopGap           = entAssetRequestTable.IHSStopGap;


           // workOrderTableQualityCheck.IHSIsMaterialMovement         = entAssetRequestTable.IHSIsMaterialMovement;

            workOrderTableQualityCheck.IHSMaterialMovementJobType    = entAssetRequestTable.IHSMaterialMovementJobType;

            workOrderTableQualityCheck.IHSMaterialMovementJobVariant = entAssetRequestTable.IHSMaterialMovementJobVariant;


           // workOrderTableQualityCheck.IHSIsPerformWork         = entAssetRequestTable.IHSIsPerformWork;

            workOrderTableQualityCheck.IHSPerformWorkJobType    = entAssetRequestTable.IHSPerformWorkJobType;

            workOrderTableQualityCheck.IHSPerformWorkJobVariant = entAssetRequestTable.IHSPerformWorkJobVariant;


            workOrderTableQualityCheck.IHSIsPerformQc           = entAssetRequestTable.IHSIsPerformQc;

            workOrderTableQualityCheck.IHSPerformQcJobType      = entAssetRequestTable.IHSPerformQcJobType;

            workOrderTableQualityCheck.IHSPerformQcJobVariant   = entAssetRequestTable.IHSPerformQcJobVariant;

            workOrderTableQualityCheck.IHSTransferOrder         = transferCreated;//entAssetRequestTable.IHSTransferOrder;


            if(this.parmMaterialWorkOrderRecid())

            {

                workOrderTableQualityCheck.ParentWorkOrder          = this.parmMaterialWorkOrderRecid();

            }

            else

            {

                workOrderTableQualityCheck.ParentWorkOrder          = this.parmPerformWorkOrderRecid();

            }


            workOrderTableQualityCheck.IHSIsToShipped           = NoYes::No;

            workOrderTableQualityCheck.IHSIsToReceived          = NoYes::No;


            // asset movement fill stop

            

        }

    

        if (workOrderTableQualityCheck.validateWrite())

        {

            workOrderTableQualityCheck.insert();

            this.parmQualityCheckWorkOrderRecid(workOrderTableQualityCheck.RecId);

          //  info(strFmt(" has been created %1", workOrderTableQualityCheck.WorkOrderId));


    

            if (numberSeq)

            {

                numberSeq.used();

            }

        }

        else

        {

            if (numberSeq)

            {

                numberSeq.abort();

            }

    

            throw error(strfmt("@SYS4007721", tablePname(EntAssetWorkOrderTable)));

        }


        ttsBegin;

        requestTableLocalQualityCheck = EntAssetRequestTable::findRecID(entAssetRequestTable.RecId, true);


        if(!requestTableLocalQualityCheck.WorkOrder)

        {

            requestTableLocalQualityCheck.WorkOrder = workOrderTableQualityCheck.RecId;

    

            if (requestTableLocalQualityCheck.validateWrite())

            {

                requestTableLocalQualityCheck.update();

            }

        }

        ttscommit;

    }


    /// <summary>

    /// Creates a work order line if asset and job type is set.

    /// </summary>

    protected void createWorkOrderLineMaterialMovement(EntAssetRequestTable _requestTable)

    {

        EntAssetWorkOrderLine        workOrderLine;

        EntAssetRequestTable         requestTable = _requestTable;

        EntAssetRequestTable         requestTableLocal;

        EntAssetObjectCalendar       objectCalendar;

        IHSAssetRequestFulfilment    AssetRequestFulfilment;

        boolean                      recordFound;

        EntAssetParameters           entMaterialAssetParameters = EntAssetParameters::find();

        EntAssetObjectLifecycleState entMaterialAssetObjectLifecycleState = EntAssetObjectLifecycleState::findID(entMaterialAssetParameters.AOLStatePreTransfer);

        EntAssetObjectTable          entMaterialobjectTable;


        recordFound = false;


        while select AssetRequestFulfilment

            where AssetRequestFulfilment.RequestId == requestTable.RequestId


        {

            entMaterialobjectTable = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID);


            //if (!workOrderTable)

            //{

            //    return;

            //}

    

            setPrefix("@EnterpriseAssetManagementAppSuite:EntAsset10");

    

            workOrderLine.clear();

            workOrderLine.initValue();

            workOrderLine.initFromWorkOrderTable(workOrderTable);

            workOrderLine.Object     = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).RecId;//EntAssetObjectTable::find(IHSAssetRequestFulfilment::findByRequestID(entAssetRequestTable.RequestId).ObjectID).RecId;


            if (entMaterialobjectTable.ObjectLifecycleState != entMaterialAssetObjectLifecycleState.RecId)

            {

               // workOrderLine.FunctionalLocation = workOrderTable.IHSFromFunctionalLocation;//EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

                workOrderLine.IHSBarcode = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).IHSBarcode;

                workOrderLine.IHSName = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).Name;

                workOrderLine.IHSItemId = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).ItemId;

                workOrderLine.IHSObjectLifecycleState = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).ObjectLifecycleState;

              //  workOrderLine.IHSFunctionalLocation = workOrderTable.IHSFromFunctionalLocation;//requestTable.FunctionalLocation;//EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;

              //  workOrderLine.IHSToFunctionalLocation = workOrderTable.IHSToFunctionalLocation;//requestTable.IHSToFunctionalLocation;//EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ToFunctionalLocation;

                

            }

            else

            {

               // workOrderLine.FunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

                workOrderLine.IHSBarcode = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).IHSBarcode;

                workOrderLine.IHSName = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).Name;

                workOrderLine.IHSItemId = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ItemId;

                workOrderLine.IHSObjectLifecycleState = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ObjectLifecycleState;

               // workOrderLine.IHSFunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;

               // workOrderLine.IHSToFunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ToFunctionalLocation;

            }


            workOrderLine.FunctionalLocation = workOrderTable.IHSFromFunctionalLocation;

            workOrderLine.IHSFunctionalLocation = workOrderTable.IHSFromFunctionalLocation;//requestTable.FunctionalLocation;//EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;

            workOrderLine.IHSToFunctionalLocation = workOrderTable.IHSToFunctionalLocation;



            //workOrderLineMaterialMovement.FunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

            workOrderLine.jobType    = entAssetRequestTable.IHSMaterialMovementJobType;//entAssetRequestTable.JobType;

            workOrderLine.JobVariant = entAssetRequestTable.IHSMaterialMovementJobVariant;//entAssetRequestTable.JobVariant;


                

        

            ttsBegin;

                requestTableLocal = EntAssetRequestTable::findRecID(entAssetRequestTable.RecId, true);

                requestTableLocal.WorkOrder = workOrderTable.RecId;

    

                    if (requestTableLocal.validateWrite())

                    {

                        requestTableLocal.update();

                    }

                    else

                    {

                        throw error(strfmt("@SYS4007721", tablePname(EntAssetRequestTable)));

                    }


                    select firstOnly forupdate objectCalendar

                        where objectCalendar.RefTableId == requestTableLocal.TableId &&

                              objectCalendar.RefRecId   == requestTableLocal.RecId;


                    if (objectCalendar)

                    {


                        objectCalendar.WorkOrder = workOrderTable.RecId;

                     

                        objectCalendar.Status    = EntAssetObjectCalendarStatus::WorkOrderCreated;


                        if (objectCalendar.validateWrite())

                        {

                            objectCalendar.update();

                        }

                        else

                        {

                            throw error(strfmt("@SYS4007721", tablePname(EntAssetObjectCalendar)));

                        }

                    }

            else

            {

                objectCalendar.clear();

                objectCalendar.initValue();

                objectCalendar.ihsInitFromRequestTable(requestTableLocal);

                objectCalendar.WorkOrder = workOrderTable.RecId;

                objectCalendar.Status    = EntAssetObjectCalendarStatus::WorkOrderCreated;

    

                if (!objectCalendar.JobType)

                {

                    objectCalendar.Object = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).RecId;

                    objectCalendar.JobType    = entAssetRequestTable.IHSMaterialMovementJobType;

                    objectCalendar.JobVariant = entAssetRequestTable.IHSMaterialMovementJobVariant;

                   // objectCalendar.JobTrade   = requestTableLocal.JobTrade;

                }

    

                if (objectCalendar.validateWrite())

                {

                    objectCalendar.insert();

                }

                else

                {

                    throw error(strfmt("@SYS4007721", tablePname(EntAssetObjectCalendar)));

                }

            }

                ttsCommit;

    

            workOrderLine.RefTableId = requestTableLocal.TableId;

            workOrderLine.RefRecId   = requestTableLocal.RecId;

                

             //   workOrderLine.insert();

            if (workOrderLine.validateWrite())

            {

                workOrderLine.insert();

            }

            //else

           // {

              //  throw error(strfmt("@SYS4007721", tablePname(EntAssetWorkOrderLine)));

            //}

        }


        i = conLen(conInfo) + 1;

        conInfo = conIns(conInfo, i, workOrderTable.WorkOrderId);

        //info(strFmt(" Work order has been created %1", workOrderTable.WorkOrderId));

    }


    /// <summary>

    /// Creates a work order line if asset and job type is set.

    /// </summary>

    protected void createWorkOrderLinePerformWork(EntAssetRequestTable _requestTable)

    {

        EntAssetWorkOrderLine        workOrderLine;

        EntAssetRequestTable         requestTable = _requestTable;

        EntAssetRequestTable         requestTableLocal;

        EntAssetObjectCalendar       objectCalendar;

        IHSAssetRequestFulfilment    AssetRequestFulfilment;

        boolean                      recordFound;

        EntAssetParameters           entPerformAssetParameters = EntAssetParameters::find();

        EntAssetObjectLifecycleState entPerformAssetObjectLifecycleState = EntAssetObjectLifecycleState::findID(entPerformAssetParameters.AOLStatePreTransfer);

        EntAssetObjectTable          entPerformobjectTable;


        recordFound = false;


        while select AssetRequestFulfilment

            where AssetRequestFulfilment.RequestId == requestTable.RequestId


        {

            //if (!workOrderTable)

            //{

            //    return;

            //}

    

            setPrefix("@EnterpriseAssetManagementAppSuite:EntAsset10");

    

            workOrderLine.clear();

            workOrderLine.initValue();

            workOrderLine.initFromWorkOrderTable(workOrderTablePerformWork);

            workOrderLine.Object     = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).RecId;//EntAssetObjectTable::find(IHSAssetRequestFulfilment::findByRequestID(entAssetRequestTable.RequestId).ObjectID).RecId;


            if (entPerformobjectTable.ObjectLifecycleState != entPerformAssetObjectLifecycleState.RecId)

            {

               // workOrderLine.FunctionalLocation = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

                workOrderLine.IHSBarcode = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).IHSBarcode;

                workOrderLine.IHSName = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).Name;

                workOrderLine.IHSItemId = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).ItemId;

                workOrderLine.IHSObjectLifecycleState = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).ObjectLifecycleState;

              //  workOrderLine.IHSFunctionalLocation = requestTable.FunctionalLocation;//EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;

               // workOrderLine.IHSToFunctionalLocation = requestTable.IHSToFunctionalLocation;//EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ToFunctionalLocation;

                

            }

            else

            {

               // workOrderLine.FunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

                workOrderLine.IHSBarcode = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).IHSBarcode;

                workOrderLine.IHSName = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).Name;

                workOrderLine.IHSItemId = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ItemId;

                workOrderLine.IHSObjectLifecycleState = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ObjectLifecycleState;

               // workOrderLine.IHSFunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;

               // workOrderLine.IHSToFunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ToFunctionalLocation;

            }


            workOrderLine.FunctionalLocation = workOrderTablePerformWork.IHSFromFunctionalLocation;

            workOrderLine.IHSFunctionalLocation = workOrderTablePerformWork.IHSFromFunctionalLocation;//requestTable.FunctionalLocation;//EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;

            workOrderLine.IHSToFunctionalLocation = workOrderTablePerformWork.IHSToFunctionalLocation;

            //if(entAssetRequestTable::existIHSIsPerformWork(entAssetRequestTable.RecId) && !recordFound)

            //{

                workOrderLine.jobType    = entAssetRequestTable.IHSPerformWorkJobType;//entAssetRequestTable.JobType;

                workOrderLine.JobVariant = entAssetRequestTable.IHSPerformWorkJobVariant;//entAssetRequestTable.JobVariant;


            //    

            //}


            

           // workOrderLine.FunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

        

            ttsBegin;

            requestTableLocal = EntAssetRequestTable::findRecID(entAssetRequestTable.RecId, true);


            if(this.parmMaterialWorkOrderRecid())

            {

                requestTableLocal.WorkOrder = this.parmMaterialWorkOrderRecid();

            }

            else

            {

                requestTableLocal.WorkOrder = workOrderTablePerformWork.RecId;

            }

    

            if (requestTableLocal.validateWrite())

            {

                requestTableLocal.update();

            }

            else

            {

                throw error(strfmt("@SYS4007721", tablePname(EntAssetRequestTable)));

            }


            select firstOnly forupdate objectCalendar

                        where objectCalendar.RefTableId == requestTableLocal.TableId &&

                              objectCalendar.RefRecId   == requestTableLocal.RecId;


            if(!objectCalendar.RecId)

            {

                if (objectCalendar)

                {

                    objectCalendar.WorkOrder = workOrderTablePerformWork.RecId;

                    objectCalendar.Status    = EntAssetObjectCalendarStatus::WorkOrderCreated;

    

                    if (objectCalendar.validateWrite())

                    {

                        objectCalendar.update();

                    }

                    else

                    {

                        throw error(strfmt("@SYS4007721", tablePname(EntAssetObjectCalendar)));

                    }

                }

                else 

                {

                    objectCalendar.clear();

                    objectCalendar.initValue();

                    objectCalendar.ihsinitFromRequestTable(requestTableLocal);

                    objectCalendar.WorkOrder = workOrderTablePerformWork.RecId;

                    objectCalendar.Status    = EntAssetObjectCalendarStatus::WorkOrderCreated;

    

                    if (!objectCalendar.JobType)

                    {

                        objectCalendar.Object = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).RecId;

                        objectCalendar.JobType    = entAssetRequestTable.IHSPerformWorkJobType;

                        objectCalendar.JobVariant = entAssetRequestTable.IHSPerformWorkJobVariant;

                    }

    

                    if (objectCalendar.validateWrite())

                    {

                        objectCalendar.insert();

                    }

                    else

                    {

                        throw error(strfmt("@SYS4007721", tablePname(EntAssetObjectCalendar)));

                    }

                }

            }

            

            ttsCommit;

    

            workOrderLine.RefTableId = requestTableLocal.TableId;

            workOrderLine.RefRecId   = requestTableLocal.RecId;

                

            //   workOrderLine.insert();

            if (workOrderLine.validateWrite())

            {

                workOrderLine.insert();

            }

            else

            {

                throw error(strfmt("@SYS4007721", tablePname(EntAssetWorkOrderLine)));

            }

        }

        i = conLen(conInfo) + 1;

        conInfo = conIns(conInfo, i, workOrderTablePerformWork.WorkOrderId);

        //info(strFmt(" Work order has been created %1", workOrderTablePerformWork.WorkOrderId));

    }


    /// <summary>

    /// Creates a work order line if asset and job type is set.

    /// </summary>

    protected void createWorkOrderLineQualityCheck(EntAssetRequestTable _requestTable)

    {

        EntAssetWorkOrderLine        workOrderLine;

        EntAssetRequestTable         requestTable = _requestTable;

        EntAssetRequestTable         requestTableLocal;

        EntAssetObjectCalendar       objectCalendar;

        IHSAssetRequestFulfilment    AssetRequestFulfilment;

        boolean                      recordFound;

        EntAssetParameters           entQualityAssetParameters = EntAssetParameters::find();

        EntAssetObjectLifecycleState entQualityAssetObjectLifecycleState = EntAssetObjectLifecycleState::findID(entQualityAssetParameters.AOLStatePreTransfer);

        EntAssetObjectTable          entQualityobjectTable;


        recordFound = false;


        while select AssetRequestFulfilment

            where AssetRequestFulfilment.RequestId == requestTable.RequestId


        {

            //if (!workOrderTable)

            //{

            //    return;

            //}

    

            setPrefix("@EnterpriseAssetManagementAppSuite:EntAsset10");

    

            workOrderLine.clear();

            workOrderLine.initValue();

            workOrderLine.initFromWorkOrderTable(workOrderTableQualityCheck);

            workOrderLine.Object     = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).RecId;//EntAssetObjectTable::find(IHSAssetRequestFulfilment::findByRequestID(entAssetRequestTable.RequestId).ObjectID).RecId;


            if (entQualityobjectTable.ObjectLifecycleState != entQualityAssetObjectLifecycleState.RecId)

            {

                workOrderLine.FunctionalLocation = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

                workOrderLine.IHSBarcode = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).IHSBarcode;

                workOrderLine.IHSName = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).Name;

                workOrderLine.IHSItemId = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).ItemId;

                workOrderLine.IHSObjectLifecycleState = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).ObjectLifecycleState;

                workOrderLine.IHSFunctionalLocation = requestTable.FunctionalLocation;//EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;

                workOrderLine.IHSToFunctionalLocation = requestTable.IHSToFunctionalLocation;//EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ToFunctionalLocation;

                

            }

            else

            {

                workOrderLine.FunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

                workOrderLine.IHSBarcode = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).IHSBarcode;

                workOrderLine.IHSName = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).Name;

                workOrderLine.IHSItemId = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ItemId;

                workOrderLine.IHSObjectLifecycleState = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ObjectLifecycleState;

                workOrderLine.IHSFunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;

                workOrderLine.IHSToFunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).ToFunctionalLocation;

            }

           

            workOrderLine.jobType    = entAssetRequestTable.IHSPerformQcJobType;//entAssetRequestTable.JobType;

            workOrderLine.JobVariant = entAssetRequestTable.IHSPerformQcJobVariant;//entAssetRequestTable.JobVariant;


            

           // workOrderLine.FunctionalLocation = EAAssetsTransferDetails::findByIHSBarcode(AssetRequestFulfilment.IHSBarcode).FunctionalLocation;//entAssetRequestTable.IHSToFunctionalLocation;

        

            ttsBegin;

            requestTableLocal = EntAssetRequestTable::findRecID(entAssetRequestTable.RecId, true);


            if(this.parmMaterialWorkOrderRecid())

            {

                requestTableLocal.WorkOrder = this.parmMaterialWorkOrderRecid();

            }

            else if(!this.parmMaterialWorkOrderRecid() && this.parmPerformWorkOrderRecid())

            {

                requestTableLocal.WorkOrder = this.parmPerformWorkOrderRecid();

            }

            else

            {

                requestTableLocal.WorkOrder = workOrderTableQualityCheck.RecId;

            }

    

            if (requestTableLocal.validateWrite())

            {

                requestTableLocal.update();

            }

            else

            {

                throw error(strfmt("@SYS4007721", tablePname(EntAssetRequestTable)));

            }


            select firstOnly forupdate objectCalendar

                        where objectCalendar.RefTableId == requestTableLocal.TableId &&

                              objectCalendar.RefRecId   == requestTableLocal.RecId;

            if(!objectCalendar.RecId)

            {

                if (objectCalendar)

                {

                    objectCalendar.WorkOrder = workOrderTableQualityCheck.RecId;

                    objectCalendar.Status    = EntAssetObjectCalendarStatus::WorkOrderCreated;

    

                    if (objectCalendar.validateWrite())

                    {

                        objectCalendar.update();

                    }

                    else

                    {

                        throw error(strfmt("@SYS4007721", tablePname(EntAssetObjectCalendar)));

                    }

                }

                else

                {

                    objectCalendar.clear();

                    objectCalendar.initValue();

                    objectCalendar.ihsinitFromRequestTable(requestTableLocal);

                    objectCalendar.WorkOrder = workOrderTableQualityCheck.RecId;

                    objectCalendar.Status    = EntAssetObjectCalendarStatus::WorkOrderCreated;

    

                    if (!objectCalendar.JobType)

                    {

                        objectCalendar.Object = EntAssetObjectTable::find(AssetRequestFulfilment.ObjectID).RecId;

                        objectCalendar.JobType    = entAssetRequestTable.IHSPerformQcJobType;

                        objectCalendar.JobVariant = entAssetRequestTable.IHSPerformQcJobVariant;

                    }

    

                    if (objectCalendar.validateWrite())

                    {

                        objectCalendar.insert();

                    }

                    else

                    {

                        throw error(strfmt("@SYS4007721", tablePname(EntAssetObjectCalendar)));

                    }

                }

            }

            ttsCommit;

    

            workOrderLine.RefTableId = requestTableLocal.TableId;

            workOrderLine.RefRecId   = requestTableLocal.RecId;

                

            //   workOrderLine.insert();

            if (workOrderLine.validateWrite())

            {

                workOrderLine.insert();

            }

            else

            {

                throw error(strfmt("@SYS4007721", tablePname(EntAssetWorkOrderLine)));

            }

        }


        i = conLen(conInfo) + 1;

        conInfo = conIns(conInfo, i, workOrderTableQualityCheck.WorkOrderId);

        //info(strFmt(" Work order has been created %1", workOrderTableQualityCheck.WorkOrderId));

    }


    protected void reserveAssets()

    {

        IHSAssetMovementHeader  localIhsAssetMovementHeader;

        IHSAssetMovementDetails ihsAssetMovementDetails;

        InventJournalTable      inventJournalTable;

        EntAssetObjectTable     entAssetObjectTable;

        EntAssetParameters      entAssetParameters;

        EntAssetObjectLifecycleState   entAssetObjectLifecycleState;


        try

        {

            ttsbegin;

            entAssetParameters = EntAssetParameters::find();

            entAssetObjectLifecycleState = EntAssetObjectLifecycleState::findID(entAssetParameters.AOLStatePreTransfer);

            localIhsAssetMovementHeader = ihsAssetMovementHeader;

            inventJournalTable = this.createdTransferJournalHeader(localIhsAssetMovementHeader);


            while select ihsAssetMovementDetails

            where ihsAssetMovementDetails.AssetMovement == localIhsAssetMovementHeader.AssetMovementId

            {

                entAssetObjectTable = EntAssetObjectTable::find(ihsAssetMovementDetails.ObjectID);

                if (entAssetObjectTable.RecId && entAssetObjectTable.Reserved == NoYes::No)

                {

                    if (entAssetObjectTable.ItemId)

                    {

                        if (entAssetObjectTable.ObjectLifecycleState == entAssetObjectLifecycleState.RecId)

                        {

                            this.createTransferJournalLine(inventJournalTable,localIhsAssetMovementHeader,ihsAssetMovementDetails);

                        }

                        else

                        {

                            warning(strFmt("Asset %1 life cycle state does not match criteria", entAssetObjectTable.ObjectID));

                        }

                    }

                    else

                    {

                        warning(strFmt("Item number not found for Asset %1", entAssetObjectTable.ObjectID));

                    }

                }

                else

                {

                    warning(strFmt("Asset %1 is already reserved", entAssetObjectTable.ObjectID));

                }

            }


        

            inventJournalTable.SystemBlocked = false;

            inventJournalTable.update();


            ttscommit;


            this.populateAssetDetails(inventJournalTable);

            this.triggerMarking(inventJournalTable);


            Info(strFmt("Assets reserved under transfer journal number %1",inventJournalTable.JournalId));

        }

        catch

        {

            throw error("Error occured.");

        }

    }


    public IHSAssetMovementHeader parmIHSAssetMovementHeader(IHSAssetMovementHeader _ihsAssetMovementHeader)

    {

        ihsAssetMovementHeader = _ihsAssetMovementHeader;

        return ihsAssetMovementHeader;

    }


    public EntAssetRequestTable parmEntAssetRequestTable(EntAssetRequestTable _entAssetRequestTable)

    {

        entAssetRequestTable = _entAssetRequestTable;

        return entAssetRequestTable;

    }


    public str parmOutput(str _output = output)

    {

        output = _output;

        return output;

    }


    public NoYesId parmCheckToStatus(NoYesId _checkToStatus = checkToStatus)

    {

        checkToStatus = _checkToStatus;

        return checkToStatus;

    }


    public EntAssetWorkOrderRecID parmMaterialWorkOrderRecid(EntAssetWorkOrderRecID _materialWorkOrderRecid = materialWorkOrderRecid)

    {

        materialWorkOrderRecid = _materialWorkOrderRecid;

        return materialWorkOrderRecid;

    }


    public EntAssetWorkOrderRecID parmPerformWorkOrderRecid(EntAssetWorkOrderRecID _performWorkOrderRecid = performWorkOrderRecid)

    {

        performWorkOrderRecid = _performWorkOrderRecid;

        return performWorkOrderRecid;

    }


    public EntAssetWorkOrderRecID parmQualityCheckWorkOrderRecid(EntAssetWorkOrderRecID _qualityCheckWorkOrderRecid = qualityCheckWorkOrderRecid)

    {

        qualityCheckWorkOrderRecid = _qualityCheckWorkOrderRecid;

        return qualityCheckWorkOrderRecid;

    }


    protected void createTransferJournalLine(InventJournalTable _inventJournalTable,

                                           IHSAssetMovementHeader   _IHSAssetMovementHeader,

                                           IHSAssetMovementDetails _IHSAssetMovementDetails)

    {

        InventJournalTable inventJournalTable;

        InventJournalTrans inventJournalTrans;

        InventDim frominventDim,ToinventDim;

        EntAssetObjectType  entAssetObjectType;

        EntAssetObjectTable entAssetObjectTable, childEntAssetObjectTable;

        InventTrans     inventTrans;

        ;


        inventJournalTable = _inventJournalTable;

        entAssetObjectType = EntAssetObjectType::findId(_IHSAssetMovementDetails.ObjectTypeID);

        entAssetObjectTable = EntAssetObjectTable::find(_IHSAssetMovementDetails.ObjectID, true);

        inventTrans = InventTrans::findTransId(entAssetObjectTable.InventTransId);


        inventJournalTrans.clear();

        inventJournalTrans.initFromInventJournalTable(inventJournalTable);

        inventJournalTrans.ItemId = entAssetObjectTable.ItemId;

        inventJournalTrans.EntAssetObjectID = entAssetObjectTable.ObjectID;

        if (inventTrans.RecId)

        {

            frominventDim = inventTrans.inventDim();

            ToinventDim = inventTrans.inventDim();

        }

        frominventDim.InventLocationId=_IHSAssetMovementHeader.FromLocationId;

        frominventDim.inventSiteId =_IHSAssetMovementHeader.FromSite;

        frominventDim.wMSLocationId =_IHSAssetMovementHeader.FromwMSLocationId;

        ToinventDim.InventLocationId = _IHSAssetMovementHeader.ToLocationId;

        ToinventDim.InventSiteId = _IHSAssetMovementHeader.ToSite;

        ToinventDim.wMSLocationId = _IHSAssetMovementHeader.TowMSLocationId;

        ToinventDim = InventDim::findOrCreate(ToinventDim);

        frominventDim = InventDim::findOrCreate(frominventDim);

        inventJournalTrans.InventDimId = frominventDim.inventDimId;

        inventJournalTrans.initFromInventTable(InventTable::find(entAssetObjectTable.ItemId));

        inventJournalTrans.Qty = -1;

        inventJournalTrans.CostPrice = inventJournalTrans.CostPrice * -1;

        inventJournalTrans.CostAmount = inventJournalTrans.CostAmount * -1;

        inventJournalTrans.ToInventDimId = ToinventDim.inventDimId;

        inventJournalTrans.TransDate = SystemDateget();

        inventJournalTrans.insert();


        entAssetObjectTable.Reserved = NoYes::Yes;

        entAssetObjectTable.ReservedBy = _IHSAssetMovementHeader.AssetMovementId;

        entAssetObjectTable.update();


        while select forupdate childEntAssetObjectTable

            where childEntAssetObjectTable.ParentObject == entAssetObjectTable.RecId

        {

            childEntAssetObjectTable.Reserved = NoYes::Yes;

            childEntAssetObjectTable.ReservedBy = _IHSAssetMovementHeader.AssetMovementId;

            childEntAssetObjectTable.update();

        }

    

    }


    protected InventJournalTable createdTransferJournalHeader(IHSAssetMovementHeader _IHSAssetMovementHeader)

    {

        InventJournalTable inventJournalTable;

        NumberSeq num;


        

        inventJournalTable.clear();

        num = new NumberSeq();

        num = NumberSeq::newGetNum

            (InventParameters::numRefTransferId());

        inventJournalTable.initFromInventJournalName(InventJournalName::find

            (InventParameters::find().TransferJournalNameId));

        inventJournalTable.Description = strFmt("Asset movement request %1",_IHSAssetMovementHeader.AssetMovementId);

        inventJournalTable.SystemBlocked = true;

        inventJournalTable.IHSTransferType = _IHSAssetMovementHeader.IHSTransferType;

        inventJournalTable.insert();

        


        return inventJournalTable;


    }


    protected void populateAssetDetailsV2(InventTransferTable  _inventTransferTable)

    {

        try

        {

            EAAssetsTransferDetails localAssetsTransferDetails,

                                        AssetsTransferDetailsFind;

            InventTransferLine      localInventTransferLine;

            EntAssetObjectTable     localEntAssetObjectTable;

            EntAssetParameters      entAssetParameters = EntAssetParameters::find();


            while select forupdate localInventTransferLine

                        where localInventTransferLine.TransferId == _inventTransferTable.TransferId

                        && localInventTransferLine.EntAssetObjectID != ''

            {

                ttsbegin;

                localEntAssetObjectTable = EntAssetObjectTable::find(localInventTransferLine.EntAssetObjectID);


                select RecId from AssetsTransferDetailsFind

                        where AssetsTransferDetailsFind.InventJournalTrans == localInventTransferLine.RecId;


                if (!AssetsTransferDetailsFind.RecId)

                {

                    localAssetsTransferDetails.clear();


                    localAssetsTransferDetails.ObjectID = localEntAssetObjectTable.ObjectID;

                    localAssetsTransferDetails.Name = localEntAssetObjectTable.Name;

                    localAssetsTransferDetails.FunctionalLocation = localEntAssetObjectTable.FunctionalLocation;

                    localAssetsTransferDetails.IHSBarcode = localEntAssetObjectTable.IHSBarcode;

                    localAssetsTransferDetails.ItemId = localEntAssetObjectTable.ItemId;

                    localAssetsTransferDetails.ObjectLifecycleState = localEntAssetObjectTable.ObjectLifecycleState;

                    localAssetsTransferDetails.ToFunctionalLocation = entAssetRequestTable.IHSToFunctionalLocation;

                    localAssetsTransferDetails.InventTransferLine = localInventTransferLine.RecId;

                    

                    localAssetsTransferDetails.insert();


                    if (EntAssetObjectLifecycleState::find(localEntAssetObjectTable.ObjectLifecycleState).ObjectLifecycleStateId == entAssetParameters.AOLStatePostTransfer)

                    {

                        localInventTransferLine.delete();

                    }

                }

                ttscommit;

                

            }

        }

        catch

        {

            throw Error("Error occurred");

        }

    }


    protected void populateAssetDetails(InventJournalTable  _inventJournalTable)

    {

        try

        {

            EAAssetsTransferDetails localAssetsTransferDetails,

                                        AssetsTransferDetailsFind;

            InventJournalTrans      localInventJournalTrans;

            EntAssetObjectTable     localEntAssetObjectTable;

            EntAssetParameters      entAssetParameters = EntAssetParameters::find();


            while select forupdate localInventJournalTrans

                        where localInventJournalTrans.JournalId == _inventJournalTable.JournalId

                        && localInventJournalTrans.JournalType == InventJournalType::Transfer

                        && localInventJournalTrans.EntAssetObjectID != ''

            {

                ttsbegin;

                localEntAssetObjectTable = EntAssetObjectTable::find(localInventJournalTrans.EntAssetObjectID);


                select RecId from AssetsTransferDetailsFind

                        where AssetsTransferDetailsFind.InventJournalTrans == localInventJournalTrans.RecId;


                if (!AssetsTransferDetailsFind.RecId)

                {

                    localAssetsTransferDetails.clear();


                    localAssetsTransferDetails.ObjectID = localEntAssetObjectTable.ObjectID;

                    localAssetsTransferDetails.Name = localEntAssetObjectTable.Name;

                    localAssetsTransferDetails.FunctionalLocation = localEntAssetObjectTable.FunctionalLocation;

                    localAssetsTransferDetails.IHSBarcode = localEntAssetObjectTable.IHSBarcode;

                    localAssetsTransferDetails.ItemId = localEntAssetObjectTable.ItemId;

                    localAssetsTransferDetails.ObjectLifecycleState = localEntAssetObjectTable.ObjectLifecycleState;

                    localAssetsTransferDetails.ToFunctionalLocation = EntAssetFunctionalLocation::find(localInventJournalTrans.toInventDim().InventLocationId).RecId;

                    localAssetsTransferDetails.InventJournalTrans = localInventJournalTrans.RecId;

                

                    localAssetsTransferDetails.insert();


                    if (EntAssetObjectLifecycleState::find(localEntAssetObjectTable.ObjectLifecycleState).ObjectLifecycleStateId == entAssetParameters.AOLStatePostTransfer)

                    {

                        localInventJournalTrans.delete();

                    }

                }

                ttscommit;

                

            }

        }

        catch

        {

            throw Error("Error occurred");

        }

    }


    public void triggerMarking(InventJournalTable   _inventJournalTable)

    {

        try

        {

            InventJournalTrans      localInventJournalTrans;

            EntAssetObjectTable     localEntAssetObjectTable;


            while select forupdate localInventJournalTrans

                        where localInventJournalTrans.JournalId == _inventJournalTable.JournalId

                        && localInventJournalTrans.JournalType == InventJournalType::Transfer

                        && localInventJournalTrans.EntAssetObjectID != ''

            {

                if (localInventJournalTrans.inventTable().isItemSerialNumberActivated() == false)

                {

                    ttsbegin;

                    localEntAssetObjectTable = EntAssetObjectTable::find(localInventJournalTrans.EntAssetObjectID);

                    this.applyMarking(localInventJournalTrans.InventTransId, localEntAssetObjectTable.InventTransId);

                 

                    ttscommit;

                }

                

            }

        }

        catch

        {

            throw Error("Error occurred while marking");

        }

    }


    private void applyMarking(InventTransId _issueInventTransId, InventTransId _receiptInventTransId)

    {

        InventTransId         issueInventTransId   = _issueInventTransId;

        InventTransId         receiptInventTransId = _receiptInventTransId;

        TmpInventTransMark  tmpInventTransMark;

        Map                 mapUpdated;

 

        InventTransOriginId receiptInventTransOriginId =

        InventTransOrigin::findByInventTransId(receiptInventTransId).RecId;

        InventTrans         receiptInventTrans         =

        InventTrans::findByInventTransOrigin(receiptInventTransOriginId);

 

        InventTransOriginId issueInventTransOriginId =

        InventTransOrigin::findByInventTransId(issueInventTransId).RecId;

        InventTrans         issueInventTrans       =

        InventTrans::findByInventTransOrigin(issueInventTransOriginId);

 

        InventTransMarkCollection collection = TmpInventTransMark::markingCollection(

        InventTransOrigin::find(receiptInventTransOriginId),

        receiptInventTrans.inventDim(),

        receiptInventTrans.Qty);

 

        collection.insertCollectionToTmpTable(tmpInventTransMark);

 

        select firstonly tmpInventTransMark

        where tmpInventTransMark.InventTransOrigin == issueInventTrans.InventTransOrigin

           && tmpInventTransMark.InventDimId       == issueInventTrans.InventDimId;

 

        if (tmpInventTransMark.RecId != 0)

        {

            Qty qtyToMark = issueInventTrans.Qty;

 

            tmpInventTransMark.QtyMarkNow =  qtyToMark;

            tmpInventTransMark.QtyRemain  -= tmpInventTransMark.QtyMarkNow;

 

            mapUpdated = new Map(Types::Int64, Types::Record);

            mapUpdated.insert(tmpInventTransMark.RecId, tmpInventTransMark);

 

            TmpInventTransMark::updateTmpMark(

                receiptInventTransOriginId,

                receiptInventTrans.inventDim(),

                -qtyToMark,

                mapUpdated.pack());

        }

    }


    protected InventTransferTable createTransferOrder(EntAssetRequestTable  _entAssetRequestTable)

    {

        InventTransferTable inventTransferTable;


        

        ////Order header inventTransferTable.clear();

        inventTransferTable.initValue();

        inventTransferTable.TransferId = InventTransferTable::numberSeq().num();

        inventTransferTable.InventLocationIdFrom = _entAssetRequestTable.FromLocationId;

        inventTransferTable.InventLocationIdTo = _entAssetRequestTable.ToLocationId;

        //inventTransferTable.DlvModeId = CustVendTransportPointLine::defaultDeliveryMode(inventTransferTable.InventLocationIdFrom,'','','','',inventTransferTable.InventLocationIdTo);

        inventTransferTable.IHSDescription = strFmt("Maintainence request %1",entAssetRequestTable.RequestId);

        //inventJournalTable.SystemBlocked = true;

        inventTransferTable.IHSTransferType = _entAssetRequestTable.IHSTransferType;

        inventTransferTable.InventLocationIdTransit = InventLocation::find(inventTransferTable.InventLocationIdFrom).InventLocationIdTransit;


        inventTransferTable.initFromAddress();

        inventTransferTable.initToAddress();

        inventTransferTable.ShipDate = today();


        inventTransferTable.ReceiveDate = today();


        if (inventTransferTable.validateWrite())

        {

            inventTransferTable.insert();


            //EntAssetRequestTable                     assetRequestTableUpdate;

            //ttsbegin;

            //assetRequestTableUpdate = EntAssetRequestTable::find(_entAssetRequestTable.RequestId);

            //assetRequestTableUpdate.selectForUpdate(true);

            //assetRequestTableUpdate.IHSTransferOrder = inventTransferTable.TransferId;

            //assetRequestTableUpdate.update();

            //ttscommit;

        }




        return inventTransferTable;

    }


    protected void createTransferOrderLine(InventTransferTable _inventTransferTable,

                                           EntAssetRequestTable   _entAssetRequestTable,

                                           IHSAssetRequestFulfilment _assetRequestFulfilment)

    {

        InventJournalTrans inventJournalTrans;

        InventDim frominventDim,ToinventDim;

        EntAssetObjectType  entAssetObjectType;

        EntAssetObjectTable entAssetObjectTable, childEntAssetObjectTable;

        InventTrans     inventTrans;

      

       // ttsbegin;

        entAssetObjectType = EntAssetObjectType::findId(_assetRequestFulfilment.ObjectTypeID);

        //entAssetObjectTable = entAssetObjectTable::ihsFindBySerialNumber(_assetRequestFulfilment.ObjectTypeID, EntAssetObjectTable::find(_assetRequestFulfilment.ObjectTypeID).SerialID, true);

        //if(!entAssetObjectTable)

        //{

            entAssetObjectTable = EntAssetObjectTable::find(_assetRequestFulfilment.ObjectID, true);

        //}

        inventTrans = InventTrans::findTransId(entAssetObjectTable.InventTransId);



        InventTransferLine inventTransferLine;

        InventDim inventDim;

        inventTransferLine.clear();

        inventTransferLine.initValue();

        inventTransferLine.initFromInventTransferTable(_inventTransferTable,true);

        

        inventTransferLine.initFromInventTable(InventTable::find(entAssetObjectTable.ItemId));

        inventTransferLine.EntAssetObjectID = entAssetObjectTable.ObjectID;

        inventTransferLine.ItemId = entAssetObjectTable.ItemId;


        if (inventTrans.RecId)

        {

            frominventDim = inventTrans.inventDim();

            ToinventDim = inventTrans.inventDim();

        }

       //Inventdim Vinay Tak

    //  TestStart

        frominventDim.InventLocationId=_EntAssetRequestTable.FromLocationId;

        frominventDim.inventSiteId =_EntAssetRequestTable.FromSite;

        frominventDim.wMSLocationId =_EntAssetRequestTable.FromwMSLocationId;

        frominventDim.inventSerialId = entAssetObjectTable.SerialID;

       // frominventDim.InventStyleId = InventTable::find(inventTrans.ItemId).StandardInventStyleId;

        ToinventDim.InventLocationId = _EntAssetRequestTable.ToLocationId;

        ToinventDim.InventSiteId = _EntAssetRequestTable.ToSite;

        ToinventDim.wMSLocationId = _EntAssetRequestTable.TowMSLocationId;

        ToinventDim.inventSerialId = entAssetObjectTable.SerialID;

        ToinventDim.InventStyleId = InventTable::find(inventTrans.ItemId).StandardInventStyleId;

        ToinventDim = InventDim::findOrCreate(ToinventDim);

        frominventDim = InventDim::findOrCreate(frominventDim);

        inventTransferLine.InventDimId = frominventDim.inventDimId;

    // TestEnd

     //  inventDim.inventSiteId = InventLocation::find(_inventTransferTable.InventLocationIdFrom).InventSiteId;

    //Start

        //inventDim.InventLocationId =_inventTransferTable.InventLocationIdFrom;

        //inventDim.inventSiteId =InventLocation::find(_inventTransferTable.InventLocationIdFrom).InventSiteId;

        //inventDim.wMSLocationId =_entAssetRequestTable.FromwMSLocationId;

        //inventDim.InventLocationId = _entAssetRequestTable.ToLocationId;

        //inventDim.InventSiteId = _entAssetRequestTable.ToSite;

        //inventDim.wMSLocationId = _entAssetRequestTable.TowMSLocationId;

        //inventDim.inventSerialId = entAssetObjectTable.SerialID;

        //inventDim.InventStyleId = InventTable::find(entAssetObjectTable.ItemId).StandardInventStyleId;

    //End

       //inventDim.inventSerialId = entAssetObjectTable.SerialID;

       // inventDim.InventStyleId = //_inventDim.InventStyleId;

       //inventDim.InventStatusId = _inventoryStatusId;

       //inventDim.LicensePlateId = _licensePlateId;

       //inventDim.wMSLocationId = _wmsLocationId;



      //  InventDimId inventDimId = InventDim::findOrCreate(inventDim).inventDimId;


       // inventTransferLine.ItemId = entAssetObjectTable.ItemId;

      //  inventTransferLine.InventDimId = inventDimId;

       // Inventdim Vinay Tak


        //inventTransferLine.InventDimId = inventTrans.inventDimId;

        inventTransferLine.QtyTransfer = 1;


        inventTransferLine.QtyRemainReceive = inventTransferLine.QtyTransfer;

        inventTransferLine.QtyRemainShip = inventTransferLine.QtyTransfer;

        inventTransferLine.initFromInventTransferTable(_inventTransferTable, false);

        inventTransferLine.LineNum = InventTransferLine::lastLineNum(inventTransferLine.TransferId) + 1.0;

        if (inventTransferLine.validateWrite())

        {

            inventTransferLine.insert();

        }


        entAssetObjectTable.Reserved         = NoYes::Yes;

        entAssetObjectTable.ReservedBy       = _inventTransferTable.TransferId;

        entAssetObjectTable.AssetStateStatus = IHSAssetStateStatus::IsWIP; //Validation check done by Vivek as suggested by Sai.

        entAssetObjectTable.update();


        while select forupdate childEntAssetObjectTable

            where childEntAssetObjectTable.ParentObject == entAssetObjectTable.RecId

        {

            childEntAssetObjectTable.Reserved    = NoYes::Yes;

            childEntAssetObjectTable.ReservedBy  = _inventTransferTable.TransferId;

            entAssetObjectTable.AssetStateStatus = IHSAssetStateStatus::IsWIP; //Validation check done by Vivek as suggested by Sai.

            childEntAssetObjectTable.update();

        }

       // ttscommit;

    }


    protected void reserveAssetsV3()

    {

        EntAssetRequestTable  localEntAssetRequestTable,assetRequestTable;

        IHSAssetRequestFulfilment ihsAssetRequestFulfilment;

        EntAssetObjectTable     entAssetObjectTable;

        boolean                 success;

        str                     message, localOutput;

        EntAssetParameters      entAssetParameters;

        EntAssetObjectLifecycleState   entAssetObjectLifecycleState;

        InventTransferTable     inventTransferTable;

        boolean                 createToHeader = false;

        boolean                 checkReservedError = false;


       // ttsbegin;

        try

        {

            

            localEntAssetRequestTable = entAssetRequestTable;

        

           

           

            //inventJournalTable = this.createdTransferJournalHeaderV2(localEntAssetRequestTable);

            entAssetParameters = EntAssetParameters::find();

            entAssetObjectLifecycleState = EntAssetObjectLifecycleState::findID(entAssetParameters.AOLStatePreTransfer);


             select firstonly ihsAssetRequestFulfilment

                where ihsAssetRequestFulfilment.RequestId == localEntAssetRequestTable.RequestId

                    join entAssetObjectTable

                where entAssetObjectTable.ObjectID == ihsAssetRequestFulfilment.ObjectID

                    &&  entAssetObjectTable.ObjectLifecycleState == entAssetObjectLifecycleState.RecId;


                if (entAssetObjectTable.RecId && entAssetObjectTable.Reserved == NoYes::No)

                {

                    createToHeader = true;

                }

            


            if(createToHeader)

            {

                inventTransferTable = this.createTransferOrder(localEntAssetRequestTable);

            }


            while select ihsAssetRequestFulfilment

                where ihsAssetRequestFulfilment.RequestId == localEntAssetRequestTable.RequestId

            {

                entAssetObjectTable = EntAssetObjectTable::find(ihsAssetRequestFulfilment.ObjectID);

                entAssetObjectTableGlobal = entAssetObjectTable;

                if (entAssetObjectTable.RecId && entAssetObjectTable.Reserved == NoYes::No)

                {

                    if (entAssetObjectTable.ItemId)

                    {

                        if (entAssetObjectTable.ObjectLifecycleState == entAssetObjectLifecycleState.RecId)

                        {

                            //this.createTransferJournalLineV2(inventJournalTable,localEntAssetRequestTable,ihsAssetRequestFulfilment);

                            this.createTransferOrderLine(inventTransferTable,localEntAssetRequestTable,ihsAssetRequestFulfilment);

                            createdWorkorder = true;

                        }

                        else

                        {

                            warning(strFmt("Asset %1 life cycle state does not match criteria", entAssetObjectTable.ObjectID));

                            if(!checkReservedError)

                            {

                                createdWorkorder = true;

                            }

                            else

                            {

                                createdWorkorder = false;

                            }

                        }

                    }

                    else

                    {

                        warning(strFmt("Item number not found for Asset %1", entAssetObjectTable.ObjectID));

                    }

                }

                else

                {

                    warning(strFmt("Asset %1 is already reserved", entAssetObjectTable.ObjectID));

                   // createdWorkorder = false;

                    checkReservedError = true;

                    

                }

            }

            

             this.populateAssetDetailsV2(inventTransferTable);

            //this.triggerMarking(inventJournalTable);


           // success = true;


          //  this.parmCheckToStatus(success);

          //  message = "Transfer order has been created";

          transferCreated = inventTransferTable.TransferId;

            

        }

        catch

        {

            success = false;

            message = "Transfer order can't be created, please contact D365 Support.";

        }


        localOutput = strFmt("%1, %2, %3, %4",success,message,inventTransferTable.TransferId,inventTransferTable.DataAreaId);

        this.parmOutput(localOutput);

       // ttscommit;

    }


    protected void reserveAssetsV2()

    {

        EntAssetRequestTable  localEntAssetRequestTable;

        IHSAssetRequestFulfilment ihsAssetRequestFulfilment;

        InventJournalTable      inventJournalTable;

        EntAssetObjectTable     entAssetObjectTable;

        boolean                 success;

        str                     message, localOutput;

        EntAssetParameters      entAssetParameters;

        EntAssetObjectLifecycleState   entAssetObjectLifecycleState;


        try

        {

            ttsbegin;

            localEntAssetRequestTable = entAssetRequestTable;

            inventJournalTable = this.createdTransferJournalHeaderV2(localEntAssetRequestTable);

            entAssetParameters = EntAssetParameters::find();

            entAssetObjectLifecycleState = EntAssetObjectLifecycleState::findID(entAssetParameters.AOLStatePreTransfer);


            while select ihsAssetRequestFulfilment

                where ihsAssetRequestFulfilment.RequestId == localEntAssetRequestTable.RequestId

                {

                    entAssetObjectTable = EntAssetObjectTable::find(ihsAssetRequestFulfilment.ObjectID);

                    if (entAssetObjectTable.RecId && entAssetObjectTable.Reserved == NoYes::No)

                    {

                        if (entAssetObjectTable.ItemId)

                        {

                            if (entAssetObjectTable.ObjectLifecycleState == entAssetObjectLifecycleState.RecId)

                            {

                                this.createTransferJournalLineV2(inventJournalTable,localEntAssetRequestTable,ihsAssetRequestFulfilment);

                            }

                            else

                            {

                                warning(strFmt("Asset %1 life cycle state does not match criteria", entAssetObjectTable.ObjectID));

                            }

                        }

                        else

                        {

                            warning(strFmt("Item number not found for Asset %1", entAssetObjectTable.ObjectID));

                        }

                    }

                    else

                    {

                        warning(strFmt("Asset %1 is already reserved", entAssetObjectTable.ObjectID));

                    }

                }


            inventJournalTable.SystemBlocked = false;

            inventJournalTable.update();


            ttscommit;


            this.populateAssetDetails(inventJournalTable);

            this.triggerMarking(inventJournalTable);


            Info(strFmt("Assets reserved under transfer journal number %1",inventJournalTable.JournalId));

            success = true;

            message = "Journal has been created";


            this.parmCheckToStatus(success);

        }

        catch

        {

            success = false;

            message = "Journal can't be created, please contact D365 Support.";

            this.parmCheckToStatus(success);

        }


        localOutput = strFmt("%1, %2, %3, %4",success,message,inventJournalTable.journalId,inventJournalTable.DataAreaId);

        this.parmOutput(localOutput);

    }


    protected InventJournalTable createdTransferJournalHeaderV2(EntAssetRequestTable _EntAssetRequestTable)

    {

        InventJournalTable inventJournalTable;

        NumberSeq num;


        

        inventJournalTable.clear();

        num = new NumberSeq();

        num = NumberSeq::newGetNum

            (InventParameters::numRefTransferId());

        inventJournalTable.initFromInventJournalName(InventJournalName::find

            (InventParameters::find().TransferJournalNameId));

        inventJournalTable.Description = strFmt("Maintainence request %1",_EntAssetRequestTable.RequestId);

        inventJournalTable.SystemBlocked = true;

        inventJournalTable.IHSTransferType = _EntAssetRequestTable.IHSTransferType;

        inventJournalTable.insert();

        


        return inventJournalTable;


    }


    protected void createTransferJournalLineV2(InventJournalTable _inventJournalTable,

                                           EntAssetRequestTable   _EntAssetRequestTable,

                                           IHSAssetRequestFulfilment _IHSAssetRequestFulfilment)

    {

        InventJournalTable inventJournalTable;

        InventJournalTrans inventJournalTrans;

        InventDim frominventDim,ToinventDim;

        EntAssetObjectType  entAssetObjectType;

        EntAssetObjectTable entAssetObjectTable, childEntAssetObjectTable;

        InventTrans     inventTrans;

        ;


        inventJournalTable = _inventJournalTable;

        entAssetObjectType = EntAssetObjectType::findId(_IHSAssetRequestFulfilment.ObjectTypeID);

        entAssetObjectTable = EntAssetObjectTable::find(_IHSAssetRequestFulfilment.ObjectID, true);

        inventTrans = InventTrans::findTransId(entAssetObjectTable.InventTransId);


        inventJournalTrans.clear();

        inventJournalTrans.initFromInventJournalTable(inventJournalTable);

        inventJournalTrans.ItemId = entAssetObjectTable.ItemId;

        inventJournalTrans.EntAssetObjectID = entAssetObjectTable.ObjectID;

        if (inventTrans.RecId)

        {

            frominventDim = inventTrans.inventDim();

            ToinventDim = inventTrans.inventDim();

        }

        frominventDim.InventLocationId=_EntAssetRequestTable.FromLocationId;

        frominventDim.inventSiteId =_EntAssetRequestTable.FromSite;

        frominventDim.wMSLocationId =_EntAssetRequestTable.FromwMSLocationId;

        ToinventDim.InventLocationId = _EntAssetRequestTable.ToLocationId;

        ToinventDim.InventSiteId = _EntAssetRequestTable.ToSite;

        ToinventDim.wMSLocationId = _EntAssetRequestTable.TowMSLocationId;

        ToinventDim = InventDim::findOrCreate(ToinventDim);

        frominventDim = InventDim::findOrCreate(frominventDim);

        inventJournalTrans.InventDimId = frominventDim.inventDimId;

        inventJournalTrans.initFromInventTable(InventTable::find(entAssetObjectTable.ItemId));

        inventJournalTrans.Qty = -1;

        inventJournalTrans.CostPrice = inventJournalTrans.CostPrice * -1;

        inventJournalTrans.CostAmount = inventJournalTrans.CostAmount * -1;

        inventJournalTrans.ToInventDimId = ToinventDim.inventDimId;

        inventJournalTrans.TransDate = SystemDateget();

        inventJournalTrans.insert();


        entAssetObjectTable.Reserved = NoYes::Yes;

        entAssetObjectTable.ReservedBy = _EntAssetRequestTable.RequestId;

        entAssetObjectTable.update();


        while select forupdate childEntAssetObjectTable

            where childEntAssetObjectTable.ParentObject == entAssetObjectTable.RecId

        {

            childEntAssetObjectTable.Reserved = NoYes::Yes;

            childEntAssetObjectTable.ReservedBy = _EntAssetRequestTable.RequestId;

            childEntAssetObjectTable.update();

        }

    

    }


}

Comments

Popular posts from this blog

Customization on Sales invoice Report in D365 F&O

75) COC - Create a coc of the table modified method

46) D365 FO: SHAREPOINT FILE UPLOAD USING X++