public async Task <IHttpActionResult> Put(int Id, LinkFxContractSignalContract fxContractSignalContract)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (Id != fxContractSignalContract.Id)
            {
                return(BadRequest());
            }

            try
            {
                _db.UpdateGraph(fxContractSignalContract, map => map
                                .AssociatedEntity(s => s.PurchaseContract)
                                .AssociatedEntity(s => s.SupplyContract)
                                .AssociatedEntity(s => s.FxContract)
                                );
                await _db.SaveChangesAsync();
            }
            catch (DbEntityValidationException e)
            {
                List <string> errors = new List <string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    errors.Add(
                        string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        errors.Add(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                 ve.PropertyName, ve.ErrorMessage));
                    }
                }
                throw;
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SignalContractExists(Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
                throw;
            }
            return(Updated(fxContractSignalContract));
        }
        public async Task <IHttpActionResult> GetAssociatedFxContractByOperationId(int id)
        {
            Operation  op         = _db.Operations.Where(o => o.Id == id).FirstOrDefault();
            FxContract fxContract = null;
            LinkFxContractSignalContract lfcsc = null;

            if (op != null)
            {
                if (op.PurchaseSaleId == 1)
                {
                    lfcsc = _db.LinkFxContractSignalContracts
                            .Where(o => o.PurchaseContractId == op.PurchaseContractId)
                            .FirstOrDefault();
                }
                else if (op.PurchaseSaleId == 2)
                {
                    lfcsc = _db.LinkFxContractSignalContracts
                            .Where(o => o.SupplyContractId == op.SupplyContractId)
                            .FirstOrDefault();
                }

                if (lfcsc != null)
                {
                    fxContract = lfcsc.FxContract;
                    if (fxContract != null)
                    {
                        return(Ok(fxContract));
                    }
                    else
                    {
                        return(NotFound());
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            else
            {
                return(NotFound());
            }
        }
        public async Task <IHttpActionResult> Delete(int Id)
        {
            LinkFxContractSignalContract fxContractSignalContract = await _db.LinkFxContractSignalContracts.FindAsync(Id);

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

            _db.LinkFxContractSignalContracts.Remove(fxContractSignalContract);

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (Exception exp)
            {
                throw;
            }

            return(Ok(fxContractSignalContract));
        }
        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));
        }