public async Task <IHttpActionResult> GetHdgetype(int Id)
        {
            ManagementIntent managementIntent = await _db.ManagementIntents.SingleOrDefaultAsync(i => i.Id == Id);

            if (managementIntent == null)
            {
                return(NotFound());
            }

            return(Ok(managementIntent));
        }
        public async Task <IHttpActionResult> GetInitialFxHedgeRB(int operationId)
        {
            HedgeLeg hedgeLeg1 = new HedgeLeg();
            HedgeLeg hedgeLeg2 = new HedgeLeg();

            hedgeLeg1.PurchaseSaleId = 1;
            hedgeLeg2.PurchaseSaleId = 2;

            #region hedgeLeg.Operation
            Operation operation = _dbContext.Operations.FirstOrDefault(o => o.Id == operationId);
            if (operation == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "invalid operation"));
            }
            hedgeLeg1.OperationId     = operation.Id;
            hedgeLeg1.Operation       = operation;
            hedgeLeg2.OperationId     = operation.Id;
            hedgeLeg2.Operation       = operation;
            hedgeLeg1.UnderlyingMonth = operation.OperationDate;
            hedgeLeg2.UnderlyingMonth = operation.OperationDate;
            if (operation.PayementDate2.HasValue)
            {
                hedgeLeg1.Maturity = operation.PayementDate2.Value;
                hedgeLeg2.Maturity = operation.PayementDate2.Value;
            }
            else
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "Selected operation must have a payment date in order to complete this action"));
            }
            #endregion

            #region hedgeLeg.FxContract
            LinkFxContractSignalContract lfcsc = null;
            if (operation.PurchaseSaleId == 1)
            {
                lfcsc = _dbContext.LinkFxContractSignalContracts
                        .Where(o => o.PurchaseContractId == operation.PurchaseContractId)
                        .FirstOrDefault();
            }
            else if (operation.PurchaseSaleId == 2)
            {
                lfcsc = _dbContext.LinkFxContractSignalContracts
                        .Where(o => o.SupplyContractId == operation.SupplyContractId)
                        .FirstOrDefault();
            }
            if (lfcsc == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "invalid contract"));
            }
            FxContract fxContract = lfcsc.FxContract;
            if (fxContract == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "invalid contract"));
            }
            if (fxContract.OrderNumberPrefix == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "contract has no order prefix number - please set one in order to perform this operation"));
            }
            hedgeLeg1.FxContractId = fxContract.Id;
            hedgeLeg2.FxContractId = fxContract.Id;
            hedgeLeg1.FxContract   = fxContract;
            hedgeLeg2.FxContract   = fxContract;
            #endregion

            #region hedgeLeg.Amount
            if (!operation.Amount2.HasValue)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "Selected operation must have an amount in order to complete this action"));
            }

            if (fxContract.IsFiftyFifty)
            {
                if (fxContract.IsSpotContract)
                {
                    hedgeLeg1.Amount = operation.Amount2 / 2;
                    hedgeLeg2.Amount = operation.Amount2 / 2;
                }
                else if (fxContract.IsMTContract)
                {
                    hedgeLeg1.Amount = Math.Min(Math.Abs(operation.Amount2.Value) / 2, Math.Abs(operation.ContractAvailableFxHedge.Value));
                    hedgeLeg2.Amount = Math.Min(Math.Abs(operation.Amount2.Value) / 2, Math.Abs(operation.ContractAvailableFxHedge.Value));
                }
            }
            else
            {
                if (fxContract.IsSpotContract)
                {
                    hedgeLeg1.Amount = operation.Amount2;
                    hedgeLeg2.Amount = operation.Amount2;
                }
                else if (fxContract.IsMTContract)
                {
                    hedgeLeg1.Amount = Math.Min(Math.Abs(operation.Amount2.Value), Math.Abs(operation.ContractAvailableFxHedge.Value));
                    hedgeLeg2.Amount = Math.Min(Math.Abs(operation.Amount2.Value), Math.Abs(operation.ContractAvailableFxHedge.Value));
                }
            }

            if (hedgeLeg1.Amount.HasValue)
            {
                hedgeLeg1.Amount = Decimal.Truncate(Decimal.Floor(hedgeLeg1.Amount.Value));
            }
            if (hedgeLeg2.Amount.HasValue)
            {
                hedgeLeg2.Amount = Decimal.Truncate(Decimal.Floor(hedgeLeg2.Amount.Value));
            }
            #endregion

            FXHedge fxHedge = new FXHedge();

            #region fxHedge.InternalState
            InternalState internalState = _dbContext.InternalStates.FirstOrDefault(i => i.Id == 1);
            if (internalState == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "invalid internalState"));
            }
            fxHedge.InternalStateId = internalState.Id;
            fxHedge.InternalState   = internalState;
            #endregion

            #region fxHedge.HedgeType
            HedgeType hedgeType = _dbContext.HedgeTypes.FirstOrDefault(ht => ht.Id == 3);
            if (hedgeType == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "invalid hedgeType"));
            }
            fxHedge.HedgeTypeId = hedgeType.Id;
            fxHedge.HedgeType   = hedgeType;
            #endregion

            #region fxHedge.ManagementIntent
            fxHedge.ManagementIntentId = 3;
            ManagementIntent managementIntent = _dbContext.ManagementIntents.FirstOrDefault(m => m.Id == 4);
            if (managementIntent == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "invalid managementIntent"));
            }
            fxHedge.ManagementIntent = managementIntent;
            fxHedge.ManagementIntent.Qualification = managementIntent.Qualification;
            #endregion

            #region fxHedge.Currency
            fxHedge.CurrencyId = operation.CurrencyId;
            Currency currency = _dbContext.Currencies.FirstOrDefault(c => c.Id == operation.CurrencyId);
            if (currency == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "invalid currency"));
            }
            fxHedge.Currency = currency;
            #endregion

            #region fxHedge.workFlow
            WorkflowState hedgeWorkflow = _dbContext.WorkflowStates.FirstOrDefault(ht => ht.Id == 1);
            if (hedgeWorkflow == null)
            {
                return(new HttpActionResult(HttpStatusCode.Conflict, "invalid hedgeWorkflow"));
            }
            fxHedge.WorkflowStateId = hedgeWorkflow.Id;
            fxHedge.WorkflowState   = hedgeWorkflow;
            #endregion

            #region fxHedge.Qualification
            fxHedge.Qualification   = managementIntent.Qualification;
            fxHedge.QualificationId = managementIntent.Qualification.Id;
            #endregion
            #region fxHedge.Qualification
            fxHedge.ExecutionFXes = null;
            #endregion

            hedgeLeg1.FXHedge = fxHedge;
            hedgeLeg2.FXHedge = fxHedge;
            fxHedge.HedgeLegs.Add(hedgeLeg1);
            fxHedge.HedgeLegs.Add(hedgeLeg2);

            return(Ok(fxHedge));
        }