public async Task <IHttpActionResult> GetHedge(int Id)
        {
            FXHedge hedge = await _dbContext.FXHedges.AsNoTracking().SingleOrDefaultAsync(i => i.Id == Id);

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

            return(Ok(hedge));
        }
        public async Task <IHttpActionResult> PostHedge([FromBody] FXHedge hedge)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (hedge == null)
            {
                return(BadRequest("Object value null"));
            }

            _dbContext.FXHedges.AddOrUpdate(hedge);

            try
            {
                await _dbContext.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 (!HedgeExists(hedge.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Created(hedge));
        }
        public async Task <IHttpActionResult> DeleteHedge(int Id)
        {
            FXHedge hedge = await _dbContext.FXHedges.FindAsync(Id);

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

            _dbContext.FXHedges.Remove(hedge);

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

            return(Ok(hedge));
        }
        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));
        }
        //[ValidateModelAttribute]
        public async Task <IHttpActionResult> PutHedge(int Id, [FromBody] FXHedge hedge) //
        {
            Configuration.Services.GetTraceWriter().Info(Request, "HedgeController", "");
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Select(x => x.Value.Errors)
                             .Where(y => y.Count > 0)
                             .ToList();
                return(BadRequest(ModelState));
            }

            if (Id != hedge.Id)
            {
                return(BadRequest());
            }
            if (Id == 0)
            {
                hedge.CreationDate = DateTime.Now;
            }
            else
            {
                hedge.ModificationDate = DateTime.Now;
            }

            if (hedge.WorkflowStateId == 2)////en cours d’exécution
            {
                hedge.ExecutionDate = DateTime.Now;
            }

            try
            {
                // hedgeProvider.Update(hedge, Key);
                //_dbContext.Entry(entity).CurrentValues.SetValues(hedge);
                _dbContext.UpdateGraph(hedge, map => map
                                       .OwnedCollection(h => h.HedgeLegs, with => with.OwnedEntity(hleg => hleg.Operation, andWith => andWith.OwnedEntity(ope => ope.Cargo)))
                                       .OwnedCollection(h => h.ExecutionFXes)
                                       //.OwnedEntity(x => x.Address)
                                       //.AssociatedEntity(h=>h.HedgeType)
                                       );
                _dbContext.SaveChanges();
                //_ModelContext.GnlCoreModelContext.ExecuteStoreCommand("[compute]", new object[] { });
            }
            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 (!HedgeExists(Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp.Message);
                throw;
            }
            return(Updated(hedge));
        }
Esempio n. 6
0
        public async void ImportKtp(string filePath)
        {
            List <ExecutionFX> ExecutionList = _dbContext.ExecutionFXes.Include("FXHedge").ToList();
            List <FXHedge>     fxHedgeList   = _dbContext.FXHedges.ToList();
            FileDataImportHelper <ExecutionKTP, ExecutionFX> importHelper = new FileDataImportHelper <ExecutionKTP, ExecutionFX>();
            Dictionary <int, FXHedge>        modifiedFxHedge   = new Dictionary <int, FXHedge>();
            Dictionary <string, ExecutionFX> executionsInError = new Dictionary <string, ExecutionFX>();
            Dictionary <string, ExecutionFX> executionsIgnored = new Dictionary <string, ExecutionFX>();

            System.Globalization.CultureInfo before = System.Threading.Thread.CurrentThread.CurrentCulture;
            try
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR");
                var data          = importHelper.GetData(filePath, ErrorMode.SaveAndContinue, false);
                var newExecutions = new List <ExecutionFX>();
                var oldExecutions = new List <ExecutionFX>();
                if (data.IsNotNull() && !data.HasTechnicalError)
                {
                    foreach (var item in data.ResultData)
                    {
                        if (item.IsNotNull())
                        {
                            ExecutionFX exec = ExecutionList.FirstOrDefault(e => String.Equals(e.ExecutionCode, item.ExecutionCode) &&
                                                                            String.Equals(e.FXHedge.Code, item.FXHedge.Code));
                            if (exec.IsNotNull())
                            {
                                if (exec.FXHedge.WorkflowStateId == (int)eWorkflowState.EN_COURS &&
                                    exec.FXHedge.CurrencyId == item.AmountCurrencyId)
                                {
                                    exec.AllIn              = item.AllIn;
                                    exec.PurchaseSaleId     = item.PurchaseSaleId;
                                    exec.Nature             = item.Nature;
                                    exec.Amount             = item.Amount;
                                    exec.AmountCurrencyId   = item.AmountCurrencyId;
                                    exec.SpotRate           = item.SpotRate;
                                    exec.FwdPoint           = item.FwdPoint;
                                    exec.AllIn              = item.AllIn;
                                    exec.Maturity           = item.Maturity;
                                    exec.ExchValue          = item.ExchValue;
                                    exec.ExchCurrencyId     = item.ExchCurrencyId;
                                    exec.ConfirmationNumber = item.ConfirmationNumber;
                                    //
                                    oldExecutions.Add(exec);

                                    //Check Dates
                                    string message;

                                    if (!HasSameMaturityDate(exec, exec.PurchaseSaleId, out message))
                                    {
                                        LogAndAddWarningtoData(message, ref data);
                                    }

                                    if (!modifiedFxHedge.ContainsKey(exec.FXHedge.Id))
                                    {
                                        modifiedFxHedge.Add(exec.FXHedge.Id, exec.FXHedge);
                                    }
                                }
                                if (exec.FXHedge.WorkflowStateId != (int)eWorkflowState.EN_COURS)
                                {
                                    string message = string.Format(MESSAGE_FORMAT, "FX hedge status in FX-Win is not \"En cours d'exécution\""
                                                                   , exec.ExecutionCode
                                                                   , exec.FXHedge.Code
                                                                   , exec.Amount);

                                    IErrorMessage error = new ErrorMessage(ErrorMessageType.BusinessMessage, message);
                                    data.ErrorsMessage.Add(error);

                                    if (!executionsInError.ContainsKey(item.ExecutionCode))
                                    {
                                        executionsInError.Add(item.ExecutionCode, item);
                                    }
                                }

                                else if (exec.FXHedge.CurrencyId != item.AmountCurrencyId)
                                {
                                    string message = string.Format(MESSAGE_FORMAT, "The field \"Currency\" in file must beidentical to the FX hedge currency in FX-Win"
                                                                   , exec.ExecutionCode
                                                                   , exec.FXHedge.Code
                                                                   , exec.Amount);

                                    IErrorMessage error = new ErrorMessage(ErrorMessageType.BusinessMessage, message);
                                    data.ErrorsMessage.Add(error);

                                    if (!executionsInError.ContainsKey(item.ExecutionCode))
                                    {
                                        executionsInError.Add(item.ExecutionCode, item);
                                    }
                                }
                            }

                            else
                            {
                                ExecutionFX execution = _dbContext.ExecutionFXes.FirstOrDefault(e => e.ExecutionCode == item.ExecutionCode);
                                if (execution.IsNotNull())
                                {
                                    string message = string.Format(MESSAGE_FORMAT, "SIAM# in the file already exists in FX-Win and is linked to an other FX hedge"
                                                                   , execution.ExecutionCode
                                                                   , execution.FXHedge.Code
                                                                   , execution.Amount);

                                    IErrorMessage error = new ErrorMessage(ErrorMessageType.BusinessMessage, message);
                                    data.ErrorsMessage.Add(error);

                                    if (!executionsInError.ContainsKey(item.ExecutionCode))
                                    {
                                        executionsInError.Add(item.ExecutionCode, item);
                                    }
                                }

                                else
                                {
                                    FXHedge fx = _dbContext.FXHedges.FirstOrDefault(f => f.Code.Equals(item.FXHedge.Code));
                                    if (fx.IsNotNull())
                                    {
                                        if (fx.WorkflowStateId == (int)eWorkflowState.EN_COURS && fx.CurrencyId == item.AmountCurrencyId)
                                        {
                                            ExecutionFX newExecution = new ExecutionFX();
                                            newExecution.AllIn              = item.AllIn;
                                            newExecution.PurchaseSaleId     = item.PurchaseSaleId;
                                            newExecution.Nature             = item.Nature;
                                            newExecution.Amount             = item.Amount;
                                            newExecution.AmountCurrencyId   = item.AmountCurrencyId;
                                            newExecution.SpotRate           = item.SpotRate;
                                            newExecution.FwdPoint           = item.FwdPoint;
                                            newExecution.AllIn              = item.AllIn;
                                            newExecution.Maturity           = item.Maturity;
                                            newExecution.ExchValue          = item.ExchValue;
                                            newExecution.ExchCurrencyId     = item.ExchCurrencyId;
                                            newExecution.FXHedgeId          = fx.Id;
                                            newExecution.FXHedge            = fx;
                                            newExecution.ExecutionCode      = item.ExecutionCode;
                                            newExecution.ConfirmationNumber = item.ConfirmationNumber;

                                            newExecutions.Add(newExecution);
                                            if (!modifiedFxHedge.ContainsKey(newExecution.FXHedgeId))
                                            {
                                                modifiedFxHedge.Add(newExecution.FXHedgeId, newExecution.FXHedge);
                                            }

                                            //Check Dates
                                            string message;
                                            if (!HasSameMaturityDate(newExecution, newExecution.PurchaseSaleId, out message))
                                            {
                                                LogAndAddWarningtoData(message, ref data);
                                            }
                                        }
                                        else if (fx.WorkflowStateId != (int)eWorkflowState.EN_COURS)
                                        {
                                            string message = string.Format(MESSAGE_FORMAT, "FX hedge status in FX-Win is not \"En cours d'exécution\""
                                                                           , item.ExecutionCode
                                                                           , fx.Code
                                                                           , item.Amount);


                                            IErrorMessage error = new ErrorMessage(ErrorMessageType.BusinessMessage, message);
                                            data.ErrorsMessage.Add(error);

                                            if (!executionsInError.ContainsKey(item.ExecutionCode))
                                            {
                                                executionsInError.Add(item.ExecutionCode, item);
                                            }
                                        }

                                        else if (fx.CurrencyId != item.AmountCurrencyId)
                                        {
                                            string message = string.Format(MESSAGE_FORMAT, "The field \"Currency\" in file must beidentical to the FX hedge currency in FX-Win"
                                                                           , item.ExecutionCode
                                                                           , fx.Code
                                                                           , item.Amount);

                                            IErrorMessage error = new ErrorMessage(ErrorMessageType.BusinessMessage, message);
                                            data.ErrorsMessage.Add(error);

                                            if (!executionsInError.ContainsKey(item.ExecutionCode))
                                            {
                                                executionsInError.Add(item.ExecutionCode, item);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (!executionsIgnored.ContainsKey(item.ExecutionCode))
                                        {
                                            executionsIgnored.Add(item.ExecutionCode, item);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    #region verification de la somme des execution
                    if (modifiedFxHedge.Values.Count > 0)
                    {
                        foreach (var fxHedge in modifiedFxHedge.Values)
                        {
                            if (fxHedge.ExecutionFXes.IsNotNull() && fxHedge.ExecutionFXes.Count > 0)
                            {
                                HedgeLeg buyLeg  = fxHedge.HedgeLegs.Where(h => h.PurchaseSaleId == (int)ePurchaseSale.ACHAT).FirstOrDefault();
                                HedgeLeg saleLeg = fxHedge.HedgeLegs.Where(h => h.PurchaseSaleId == (int)ePurchaseSale.VENTE).FirstOrDefault();

                                decimal purchaseExecutionAmountSum = 0;
                                decimal saleExecutionAmountSum     = 0;

                                if (buyLeg.IsNotNull())
                                {
                                    purchaseExecutionAmountSum = (from ex in fxHedge.ExecutionFXes
                                                                  where ex.PurchaseSaleId == (int)ePurchaseSale.ACHAT &&
                                                                  ex.Amount.HasValue &&
                                                                  buyLeg.FXHedgeId == fxHedge.Id &&
                                                                  buyLeg.Maturity.ToShortDateString() == ex.Maturity.ToShortDateString()
                                                                  select ex).Sum(ex => ex.Amount.Value);
                                }

                                if (saleLeg.IsNotNull())
                                {
                                    saleExecutionAmountSum = (from ex in fxHedge.ExecutionFXes
                                                              where ex.PurchaseSaleId == (int)ePurchaseSale.VENTE &&
                                                              ex.Amount.HasValue &&
                                                              saleLeg.FXHedgeId == fxHedge.Id &&
                                                              saleLeg.Maturity.ToShortDateString() == ex.Maturity.ToShortDateString()
                                                              select ex).Sum(ex => ex.Amount.Value);
                                }

                                if (fxHedge.HedgeLegs.IsNotNull())
                                {
                                    if (fxHedge.HedgeLegs.Count == 1)
                                    {
                                        var buyHedgeLeg  = fxHedge.HedgeLegs.FirstOrDefault(h => h.PurchaseSaleId == (int)ePurchaseSale.ACHAT);
                                        var saleHedgeLeg = fxHedge.HedgeLegs.FirstOrDefault(h => h.PurchaseSaleId == (int)ePurchaseSale.VENTE);

                                        if (buyHedgeLeg.IsNotNull())
                                        {
                                            bool    IsInBuyError = false;
                                            decimal buyAmount    = 0;

                                            if (buyHedgeLeg.Amount.HasValue)
                                            {
                                                buyAmount = buyHedgeLeg.Amount.Value;
                                            }

                                            IsInBuyError = purchaseExecutionAmountSum != buyHedgeLeg.Amount;

                                            if (IsInBuyError)
                                            {
                                                fxHedge.InternalStateId = (int)eInternalStatus.EN_ERREUR_MONTANT_EXECUTION;
                                                string warn = String.Format("Import KTP: FxHedge {0} is in error - Please Check that buy leg amount is equal to the amount sum of  Fx Execution of type buy ", fxHedge.Code);
                                                LogAndAddWarningtoData(warn, ref data);
                                            }
                                            else
                                            {
                                                fxHedge.InternalStateId = (int)eInternalStatus.OK;
                                                fxHedge.WorkflowStateId = (int)eWorkflowState.VALIDE;
                                            }
                                        }

                                        if (saleHedgeLeg.IsNotNull())
                                        {
                                            bool    IsInSaleError = false;
                                            decimal saleAmount    = 0;

                                            if (saleHedgeLeg.Amount.HasValue)
                                            {
                                                saleAmount = saleHedgeLeg.Amount.Value;
                                            }

                                            IsInSaleError = saleExecutionAmountSum != saleHedgeLeg.Amount;

                                            if (IsInSaleError)
                                            {
                                                fxHedge.InternalStateId = (int)eInternalStatus.EN_ERREUR_MONTANT_EXECUTION;
                                                string warn = String.Format("Import KTP: FxHedge {0} is in Error - Please check that sale leg amount is equal to the amount sum of  Fx Execution of type sale ", fxHedge.Code);
                                                LogAndAddWarningtoData(warn, ref data);
                                            }
                                            else
                                            {
                                                fxHedge.InternalStateId = (int)eInternalStatus.OK;
                                                fxHedge.WorkflowStateId = (int)eWorkflowState.VALIDE;
                                            }
                                        }
                                    }

                                    if (fxHedge.HedgeLegs.Count == 2)
                                    {
                                        bool IsInSaleError = false;
                                        bool IsInBuyError  = false;

                                        var buyHedgeLeg  = fxHedge.HedgeLegs.FirstOrDefault(h => h.PurchaseSaleId == (int)ePurchaseSale.ACHAT);
                                        var saleHedgeLeg = fxHedge.HedgeLegs.FirstOrDefault(h => h.PurchaseSaleId == (int)ePurchaseSale.VENTE);

                                        if (saleHedgeLeg.Amount.HasValue && buyHedgeLeg.Amount.HasValue)
                                        {
                                            IsInBuyError = purchaseExecutionAmountSum != buyHedgeLeg.Amount;

                                            IsInSaleError = saleExecutionAmountSum != saleHedgeLeg.Amount;
                                        }

                                        if (!IsInSaleError && !IsInBuyError)
                                        {
                                            fxHedge.InternalStateId = (int)eInternalStatus.OK;
                                            fxHedge.WorkflowStateId = (int)eWorkflowState.VALIDE;
                                        }
                                        else
                                        {
                                            if (IsInBuyError && !IsInSaleError)
                                            {
                                                fxHedge.InternalStateId = (int)eInternalStatus.EN_ERREUR_MONTANT_EXECUTION;
                                                string warn = String.Format("Import KTP: FxHedge {0} is in Error - Please Check that Buy Leg Amount is equal to the amount sum of  Fx Execution of type BUY ", fxHedge.Code);
                                                LogAndAddWarningtoData(warn, ref data);
                                            }
                                            if (IsInSaleError && !IsInBuyError)
                                            {
                                                fxHedge.InternalStateId = (int)eInternalStatus.EN_ERREUR_MONTANT_EXECUTION;
                                                string warn = String.Format("Import KTP: FxHedge {0} is in Error - Please Check that Sale Leg Amount is equal to the amount sum of  Fx Execution of type SALE ", fxHedge.Code);
                                                LogAndAddWarningtoData(warn, ref data);
                                            }
                                            if (IsInBuyError && IsInSaleError)
                                            {
                                                fxHedge.InternalStateId = (int)eInternalStatus.EN_ERREUR_MONTANT_EXECUTION;
                                                string warn = String.Format("Import KTP: FxHedge {0} is in Error - Please Check that Buy Leg Amount is equal to the amount sum of  Fx Execution of type BUY ", fxHedge.Code);
                                                LogAndAddWarningtoData(warn, ref data);
                                                warn = String.Format("Import KTP: FxHedge {0} is in Error - Please Check that Sale Leg Amount is equal to the amount sum of  Fx Execution of type SALE ", fxHedge.Code);
                                                LogAndAddWarningtoData(warn, ref data);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    #endregion
                    // Sauvegarder les dates d'executions pour la couverture.
                    List <FXHedge> fxList = new List <FXHedge>();

                    // Sauvegarder les nouvelles executions.
                    if (!newExecutions.IsNullOrEmpty())
                    {
                        //_ModelContext.GnlCoreModelContext.SaveCollection<ExecutionFX>(newExecutions);
                        _dbContext.ExecutionFXes.AddRange(newExecutions);

                        newExecutions.ForEach(ex =>
                        {
                            if (!fxList.Contains(ex.FXHedge))
                            {
                                fxList.Add(ex.FXHedge);
                            }
                        });
                        _dbContext.SaveChanges();
                    }
                    // Updater les anciennes.
                    if (!oldExecutions.IsNullOrEmpty())
                    {
                        // _ModelContext.GnlCoreModelContext.SaveCollection<ExecutionFX>(oldExecutions);
                        foreach (ExecutionFX executionFX in oldExecutions)
                        {
                            var Oldvalue = _dbContext.ExecutionFXes.SingleOrDefault(i => i.Id == executionFX.Id);
                            _dbContext.Entry(Oldvalue).CurrentValues.SetValues(executionFX);
                            _dbContext.SaveChanges();
                        }

                        oldExecutions.ForEach(ex =>
                        {
                            if (!fxList.Contains(ex.FXHedge))
                            {
                                fxList.Add(ex.FXHedge);
                            }
                        });
                    }

                    if (!fxList.IsNullOrEmpty())
                    {
                        fxList.ForEach(fx => fx.ExecutionDate = DateTime.Now);
                        //_ModelContext.GnlCoreModelContext.SaveCollection<FXHedge>(fxList);
                        foreach (FXHedge hedge in fxList)
                        {
                            var Oldvalue = _dbContext.FXHedges.SingleOrDefault(i => i.Id == hedge.Id);
                            _dbContext.Entry(Oldvalue).CurrentValues.SetValues(hedge);
                            _dbContext.SaveChanges();
                        }
                    }

                    // Afficher les erreurs.
                    if (data.HasBusinessError)
                    {
                        //DXMessageBox.Show(Application.Current.MainWindow,
                        //                "Some errors occured during import operation, please see your trace log",
                        //                "Information",
                        //                MessageBoxButton.OK,
                        //                MessageBoxImage.Warning);
                        logMessage("Executions in Error : " + executionsInError.Count.ToString(), Category.Info, Priority.High);
                        logMessage(data.GetFormattedErrors(ErrorMessageType.BusinessMessage), Category.Info, Priority.High);
                        logMessage("Ignored Execution : " + executionsIgnored.Count.ToString(), Category.Info, Priority.High);
                        logMessage("Successfully updated execution : " + oldExecutions.Count.ToString(), Category.Info, Priority.High);
                        logMessage("Successfully newly imported execution : " + newExecutions.Count.ToString(), Category.Info, Priority.High);
                        logMessage("Successfully updated Fx Hedge : " + modifiedFxHedge.Count.ToString(), Category.Info, Priority.High);
                    }

                    else if (!data.ResultData.IsNullOrEmpty() && (!newExecutions.IsNullOrEmpty() || !oldExecutions.IsNullOrEmpty()))
                    {
                        StringBuilder infoMessage = new StringBuilder();
                        infoMessage.Append("Successfully updated execution : " + oldExecutions.Count.ToString() + "\n"
                                           + "Successfully newly imported execution : " + newExecutions.Count.ToString() + "\n"
                                           + "Successfully updated Fx Hedge : " + modifiedFxHedge.Count.ToString() + "\n"
                                           + "Ignored Execution : " + executionsIgnored.Count.ToString());
                        //DXMessageBox.Show(Application.Current.MainWindow,
                        //                "Import KTP has successfully completed" + "\n" + infoMessage.ToString(),
                        //                "Information",
                        //                MessageBoxButton.OK,
                        //                MessageBoxImage.Information);

                        if (data.HasWarnings)
                        {
                            StringBuilder warningMessage = new StringBuilder();
                            foreach (var warning in data.WarningMessage)
                            {
                                warningMessage.Append("\n" + warning.Message);
                            }

                            // DXMessageBox.Show(Application.Current.MainWindow, warningMessage.ToString(), "Warnings", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                        logMessage("Import KTP has successfully completed", Category.Info, Priority.High);
                        //additionnal information about imported Execution
                        logMessage("Successfully updated execution : " + oldExecutions.Count.ToString(), Category.Info, Priority.High);
                        logMessage("Successfully newly imported execution : " + newExecutions.Count.ToString(), Category.Info, Priority.High);
                        logMessage("Successfully updated Fx Hedge : " + modifiedFxHedge.Count.ToString(), Category.Info, Priority.High);
                        logMessage("Ignored Execution : " + executionsIgnored.Count.ToString(), Category.Info, Priority.High);
                    }

                    else
                    {
                        //DXMessageBox.Show(Application.Current.MainWindow,
                        //                "No data imported",
                        //                "Information",
                        //                MessageBoxButton.OK,
                        //                MessageBoxImage.Information);
                        logMessage("No data imported", Category.Info, Priority.High);
                        foreach (var execution in executionsIgnored.Values)
                        {
                            logMessage(execution.ExecutionCode + " - FxHedge : " + execution.FXHedge.Code + " not found ", Category.Info, Priority.High);
                        }
                    }
                }
                else
                {
                    // il y a des erreurs techniques.
                    // Afficher les erreurs techniques.
                    //DXMessageBox.Show(Application.Current.MainWindow,
                    //                    data.GetFormattedErrors(ErrorMessageType.TechnicalMessage),
                    //                    "Error",
                    //                    MessageBoxButton.OK,
                    //                    MessageBoxImage.Error);
                    logMessage(data.GetFormattedErrors(ErrorMessageType.TechnicalMessage), Category.Info, Priority.High);
                }
            }
            catch (Exception ex)
            {
                logMessage(ex.InnerException.IsNotNull() ? ex.InnerException.Message : ex.Message, Category.Info, Priority.Medium);
                //DXMessageBox.Show("Import KTP has failed, please try again",
                //                                               "Error",
                //                                               MessageBoxButton.OK,
                //                                               MessageBoxImage.Error);
                logMessage("Import KTP has failed, please try again", Category.Info, Priority.High);
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = before;
            }
        }