public static List<AgregHistoryModel> GetCpteHistoryByCompteGroupByDate(int IdCompte, int IdLiquiditeSupport)
 {
     using (Data.Model.UpsilabEntities context = new UpsilabEntities())
     {
         var histos = context.ag_upsideo_compte_histo.Where(x => x.idfrontcompte == IdCompte).ToList();
         var listhistory = new List<AgregHistoryModel>();
         var liquiditecptesupports = context.ag_upsideo_cptrelsupports_histo.Where(x => x.idfrontcompte == IdCompte && x.idfrontsupport == IdLiquiditeSupport).ToList();
         foreach (var histo in histos)
         {
             var liquiditecptesupport = GetLiquidite(liquiditecptesupports, histo.datehisto.Date);
             var agreghistorymodel = new AgregHistoryModel
             {
                 IdCompteHisto = histo.Id,
                 IdFrontCompte = IdCompte,
                 DateHistory = histo.datehisto.Date,
                 TotalEspece = liquiditecptesupport == null ? 0 : (liquiditecptesupport.pru.HasValue ? liquiditecptesupport.pru.Value : 0),
                 CompteHistoryDetails = AgregHistoryModel.ModelDbToViewModel(histo.Id, Convert.ToSingle(histo.totalcompte), histo.ag_upsideo_compte.ag_upsideo_cptrelsupports_histo.Where(x => x.datehisto.HasValue && x.datehisto.Value.ToString("dd/MM/yyyy") == histo.datehisto.Date.ToString("dd/MM/yyyy")).ToList()),
                 TotalCompte = histo.totalcompte
             };
             listhistory.Add(agreghistorymodel);
         }
         return listhistory;
     }
 }
        public static bool UpdateAgregHistory(AgregHistoryModel model,int idSupportLiquidite)
        {
            using (Data.Model.UpsilabEntities context = new UpsilabEntities())
            {
                context.Connection.Open();
                using (var dbContextTransaction = context.Connection.BeginTransaction())
                {
                    var isCompteHistoryDetailsEmpty = false;
                    try
                    {
                        if (model.IdCompteHisto > 0)
                        {
                            var cptehisto = context.ag_upsideo_compte_histo.FirstOrDefault(x => x.Id == model.IdCompteHisto);
                            cptehisto.totalcompte = model.TotalCompte;
                        }
                        else
                        {
                            var cptehisto = new ag_upsideo_compte_histo
                            {
                                datehisto = model.DateHistory,
                                idfrontcompte = model.IdFrontCompte,
                                totalcompte = model.TotalCompte,
                                devise = string.Empty
                            };
                            context.ag_upsideo_compte_histo.AddObject(cptehisto);
                        }
                        context.SaveChanges();
                        //
                        //
                        //
                        if (model.CompteHistoryDetails != null && model.CompteHistoryDetails.Count > 0)
                        {
                            if (model.CompteHistoryDetails.Count == 1)
                            {
                                //Liquidité
                                if (model.CompteHistoryDetails[0].Quantite == 0 && model.CompteHistoryDetails[0].Vl == 1)
                                    isCompteHistoryDetailsEmpty = true;
                            }

                            foreach (var cptesupporthisto in model.CompteHistoryDetails)
                            {
                                if (cptesupporthisto.IdCpteSupportHistory == 0)
                                {
                                    var ag_cptesupporthi = new ag_upsideo_cptrelsupports_histo
                                    {
                                        datehisto = model.DateHistory,
                                        datemaj = model.DateHistory,
                                        idfrontcompte = model.IdFrontCompte,
                                        idfrontsupport = cptesupporthisto.IdFrontSupport,
                                        qte = cptesupporthisto.Quantite,
                                        vl = cptesupporthisto.Vl
                                    };
                                    context.ag_upsideo_cptrelsupports_histo.AddObject(ag_cptesupporthi);
                                }
                                else
                                {
                                    var ag_cptesupporthi = context.ag_upsideo_cptrelsupports_histo.FirstOrDefault(x => x.Id == cptesupporthisto.IdCpteSupportHistory);
                                    ag_cptesupporthi.datehisto = model.DateHistory;
                                    ag_cptesupporthi.datemaj = model.DateHistory;
                                    ag_cptesupporthi.idfrontcompte = model.IdFrontCompte;
                                    ag_cptesupporthi.idfrontsupport = cptesupporthisto.IdFrontSupport;
                                    ag_cptesupporthi.qte = cptesupporthisto.Quantite;
                                    ag_cptesupporthi.vl = cptesupporthisto.Vl;
                                }
                                context.SaveChanges();
                            }
                        }                        
                        //
                        //Liquidites
                        //
                        var ag_liquiditehistos = context.ag_upsideo_cptrelsupports_histo.Where(x => x.idfrontcompte == model.IdFrontCompte && x.idfrontsupport == idSupportLiquidite).ToList();
                        var ag_liquiditehisto = GetLiquiditeSupport(ag_liquiditehistos, model.DateHistory);
                        if (ag_liquiditehisto != null)
                        {
                            if (!isCompteHistoryDetailsEmpty)
                                ag_liquiditehisto.pru = model.TotalEspece;
                        }
                        else
                        {
                            ag_liquiditehisto = new ag_upsideo_cptrelsupports_histo
                            {
                                datehisto = model.DateHistory,
                                datemaj = model.DateHistory,
                                idfrontcompte = model.IdFrontCompte,
                                idfrontsupport = idSupportLiquidite,
                                qte = 0,
                                vl = 1,
                                pru = model.TotalEspece
                            };
                            context.ag_upsideo_cptrelsupports_histo.AddObject(ag_liquiditehisto);
                        }
                        context.SaveChanges();
                        //
                        //
                        //
                        dbContextTransaction.Commit();
                        return true;
                    }
                    catch (Exception e)
                    {
                        dbContextTransaction.Rollback();
                        Log.Log.AppendException(e);
                        return false;
                    }
                }
            }
        }
        public ActionResult UpdateCpteSupportHistory(string jsondata)
        {
            Upsilab.Data.Model.Agreg.AgregHistoryModel.AgregCompteHistoryModel cpteHistories = null;
            try
            {
                cpteHistories = UpsGridVMSession.Current.data as Upsilab.Data.Model.Agreg.AgregHistoryModel.AgregCompteHistoryModel;
            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
                return Json(401, JsonRequestBehavior.AllowGet);
            }
            var supportHist = new JavaScriptSerializer().Deserialize<Upsilab.Data.Model.Agreg.AgregHistoryModel.AgregCompteHistoryDetail>(jsondata);

            AgregHistoryModel currentCpteHistory = null;
            currentCpteHistory = cpteHistories.HistoryCollection.FirstOrDefault(x => x.IdCompteHisto == supportHist.IdCompteHisto);
            //
            //
            //
            int idcompte = int.Parse(cpteHistories.IdFrontCompte);
            if (currentCpteHistory == null)
            {
                currentCpteHistory =
                    new AgregHistoryModel
                    {
                        DateHistory = DateTime.ParseExact(supportHist.DateHistory, "dd/MM/yyyy", CultureInfo.InvariantCulture),
                        TotalEspece = supportHist.Espece,
                        CompteHistoryDetails = new List<AgregHistoryModel.AgregCompteHistoryDetail>(),
                        TotalCompte = supportHist.Espece,
                        IdFrontCompte = idcompte
                    };
                currentCpteHistory.CompteHistoryDetails.Add(supportHist);
                cpteHistories.HistoryCollection.Add(currentCpteHistory);
            }
            else
            {
                if (supportHist.IdCpteSupportHistory == 0)
                {
                    //Get by CODEISIN
                    var cptesupport = currentCpteHistory.CompteHistoryDetails.FirstOrDefault(x => x.CodeIsIn == supportHist.CodeIsIn);
                    if (cptesupport != null)
                    {
                        cptesupport.Quantite = supportHist.Quantite;
                        cptesupport.Vl = supportHist.Vl;
                    }
                    else
                    {
                        currentCpteHistory.CompteHistoryDetails.Add(supportHist);
                    }
                }
                else
                { //Get by IdCompteSupport
                    var cptesupport = currentCpteHistory.CompteHistoryDetails.FirstOrDefault(x => x.IdCpteSupportHistory == supportHist.IdCpteSupportHistory);
                    cptesupport.Quantite = supportHist.Quantite;
                    cptesupport.Vl = supportHist.Vl;
                }
            }
            currentCpteHistory.TotalCompte = supportHist.Espece + currentCpteHistory.CompteHistoryDetails.Sum(x => x.Quantite * x.Vl);

            return PartialView("HistoriqueCompteDetailGrille", currentCpteHistory);

        }
        public ActionResult UpdateCpteHistory(string jsondata)
        {
            Upsilab.Data.Model.Agreg.AgregHistoryModel.AgregCompteHistoryModel cpteHistories = null;
            try
            {
                cpteHistories = UpsGridVMSession.Current.data as Upsilab.Data.Model.Agreg.AgregHistoryModel.AgregCompteHistoryModel;
            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
                return Json(401, JsonRequestBehavior.AllowGet);
            }

            var LanguageData = PageLanguageHelper.GetLanguageContent("User", "agregcompte");
            var cpteHist = new JavaScriptSerializer().Deserialize<Upsilab.Data.Model.Agreg.AgregHistoryModel>(jsondata);
            //
            //
            //
            int idcompte = int.Parse(cpteHistories.IdFrontCompte);
            if (cpteHist.IdCompteHisto == 0)
            {
                var isDateExist = cpteHistories.HistoryCollection.Any(x => x.DateHistory.ToString("dd/MM/yyyy") == cpteHist.DateHistoryString);
                if (isDateExist)
                {
                    return Json(new { error = "ok", msg = LanguageData.GetContent("error_date") }, JsonRequestBehavior.AllowGet);
                }
            }
            //
            //
            //
            var currentcptehistori = cpteHistories.HistoryCollection.FirstOrDefault(x => x.IdCompteHisto == cpteHist.IdCompteHisto);
            if (currentcptehistori == null)
            {
                currentcptehistori = new AgregHistoryModel
                {
                    IdCompteHisto = cpteHist.IdCompteHisto,
                    DateHistory = DateTime.ParseExact(cpteHist.DateHistoryString, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture),
                    TotalCompte = cpteHist.TotalCompte,
                    IdFrontCompte = idcompte,
                    TotalEspece = cpteHist.TotalEspece
                };
                cpteHistories.HistoryCollection.Add(currentcptehistori);
            }
            else
            {
                currentcptehistori.TotalCompte = cpteHist.TotalCompte;
                currentcptehistori.TotalEspece = cpteHist.TotalEspece;
            }

            //
            //Update DB
            //
            Guid idcrmcabinet;
            SessionManager.Get<Guid>(AgregCgpBL.AgregCrmcabinetIdSessionKey, out idcrmcabinet);
            var liquidite = AgregSupportBL.GetLiquiditeSupportByCabinet(idcrmcabinet);
            //
            //
            //
            var isok = AgregCompteHistoryBL.UpdateAgregHistory(currentcptehistori, liquidite.idfrontsupport);
            //
            //
            //
            return Json(isok ? new { error = "ko", msg = idcompte.ToString() } : new { error = "ok", msg = "Invalid Server error" }, JsonRequestBehavior.AllowGet);

        }
        private ActionResult HistoriqueCompteDetail(AgregHistoryModel vm)
        {
            var model = UpsGridVMSession.Current.data as Upsilab.Data.Model.Agreg.AgregHistoryModel.AgregCompteHistoryModel;
            vm.User = model.User;
            vm.Compte = model.Compte;

            return View("HistoriqueCompteDetail", vm);
        }