/**
         * Creates a Work Order Transaction header for a given Work Order
         * Transactions handled : Resource Usage (RU), Component Return (CR), Component Issue (CI)
         * @param ctx
         * @param VAMFG_M_WorkOrder_ID Work Order
         * @param TxnType Work Order Transaction Type
         * @param parent_M_WorkOrderTransaction_ID Parent Work Order Transaction
         * @param M_Locator_ID Work Order Transaction Locator
         * @param Qty Work Order Transaction Quantity
         * @param trx
         * @return MVAMFGMWrkOdrTransaction
         */
        public ViennaAdvantage.Model.MVAMFGMWrkOdrTransaction createWOTxn(Ctx ctx, int VAMFG_M_WorkOrder_ID, String TxnType,
                                                                          int parent_M_WorkOrderTransaction_ID, int M_Locator_ID, Decimal Qty, Trx trx)
        {
            int _countGOM01 = Convert.ToInt32(DB.ExecuteScalar("SELECT COUNT(AD_ModuleInfo_ID) FROM AD_ModuleInfo WHERE Prefix like 'GOM01_'"));

            if (!(TxnType.Equals(X_VAMFG_M_WrkOdrTransaction.VAMFG_WORKORDERTXNTYPE_ResourceUsage) ||
                  TxnType.Equals(X_VAMFG_M_WrkOdrTransaction.VAMFG_WORKORDERTXNTYPE_1_ComponentIssueToWorkOrder) ||
                  TxnType.Equals(X_VAMFG_M_WrkOdrTransaction.VAMFG_WORKORDERTXNTYPE_ComponentReturnFromWorkOrder)))
            {
                log.Severe("Not correct transaction type to generate WO Transaction");
                return(null);
            }
            //Check the validity of the Work Order
            ViennaAdvantage.Model.MVAMFGMWorkOrder wo = new ViennaAdvantage.Model.MVAMFGMWorkOrder(ctx, VAMFG_M_WorkOrder_ID, trx);
            if (wo == null || !wo.GetDocStatus().Equals(X_VAMFG_M_WorkOrder.DOCSTATUS_InProgress))
            {
                log.Severe("Work Order number not valid for transactions.");
                return(null);
            }

            //	Checking if Quantity can be derived from associated WorkOrder
            if (parent_M_WorkOrderTransaction_ID == 0)
            {
                if (Qty == null || Qty == 0)
                {
                    log.Info("Deriving Quantity from Work Order");
                    Qty = wo.GetVAMFG_QtyEntered();
                }
            }

            //Deriving Qty from parent WO Move Txn
            if (Qty.CompareTo(Decimal.Zero) <= 0)
            {
                ViennaAdvantage.Model.MVAMFGMWrkOdrTransaction parentWOT = new ViennaAdvantage.Model.MVAMFGMWrkOdrTransaction(ctx, parent_M_WorkOrderTransaction_ID, trx);
                Qty = parentWOT.GetVAMFG_QtyEntered();
            }

            //Creating new WorkOrder Transaction
            ViennaAdvantage.Model.MVAMFGMWrkOdrTransaction wot = new ViennaAdvantage.Model.MVAMFGMWrkOdrTransaction(ctx, 0, trx);
            wot.SetRequiredColumns(VAMFG_M_WorkOrder_ID, M_Locator_ID, X_VAMFG_M_WrkOdrTransaction.VAMFG_WOTXNSOURCE_Generated, TxnType);
            if (parent_M_WorkOrderTransaction_ID > 0)
            {
                wot.SetParentWorkOrderTxn_ID(parent_M_WorkOrderTransaction_ID);
                ViennaAdvantage.Model.MVAMFGMWrkOdrTransaction parentWOT = new ViennaAdvantage.Model.MVAMFGMWrkOdrTransaction(ctx, parent_M_WorkOrderTransaction_ID, trx);
                wot.SetC_DocType_ID(parentWOT.GetC_DocType_ID());
                // set the client & org derived from parent work order transaction
                wot.SetClientOrg(parentWOT);
                if (_countGOM01 > 0)
                {
                    wot.SetGOM01_Density(parentWOT.GetGOM01_Density());
                    wot.SetGOM01_Quantity(parentWOT.GetGOM01_Quantity());
                    wot.SetGOM01_ActualQuantity(parentWOT.GetGOM01_ActualQuantity());
                    wot.SetGOM01_ActualDensity(parentWOT.GetGOM01_ActualDensity());
                    wot.SetGOM01_ActualLiter(parentWOT.GetGOM01_ActualLiter());
                }
            }
            else
            {   //	derive the doctype from the WorkOrderClass
                MVAMFGMWorkOrderClass woclass = new MVAMFGMWorkOrderClass(ctx, wo.GetVAMFG_M_WorkOrderClass_ID(), trx);
                wot.SetC_DocType_ID(woclass.GetWOT_DocType_ID());
                // since there is no parent work order transaction
                // set the client & org derived from the associated work order
                wot.SetClientOrg(wo);
            }
            //wot.SetVAMFG_QtyEntered(Qty.setScale(MUOM.getPrecision(ctx, wot.getC_UOM_ID()), Decimal.ROUND_HALF_UP));
            wot.SetVAMFG_QtyEntered(Decimal.Round((Qty), VAdvantage.Model.MUOM.GetPrecision(ctx, wot.GetC_UOM_ID()), MidpointRounding.AwayFromZero));
            if (save)
            {
                if (!wot.Save(trx))
                {
                    log.Severe("Could not save WO Txn.");
                    return(null);
                }
            }

            return(wot);
        }