public ActionResult UpdateCompte(string jsondata)
        {
            var compteeditvm = new JavaScriptSerializer().Deserialize<AgregCompteEditVM>(jsondata);

            Guid idClient = Guid.Empty;
            SessionManager.Get<Guid>(agupsideocompteBL.AgregClientIdSessionKey, out idClient);
            Guid idCgpAgreg = Guid.Empty;
            User currentUser = SessionManager.GetUserSession();
            if (currentUser.IsEndUser())
            {
                CustomerProspect currentCustomer = SessionManager.GetCustomerProspectSession();
                User currenttUserCreated = currentCustomer.User1;
                idCgpAgreg = currenttUserCreated.idAgregCGP.Value;
            }
            else
            {
                idCgpAgreg = currentUser.idAgregCGP.Value;
            }
            ag_upsideo_compte comptedb = null;
            var agregcompte = new AgregCompteViewModel();
            List<SqlAgregCompteModel> lstCompte = null;
            
            try
            {
                if (compteeditvm.Idfrontcompte <= 0)
                {
                    comptedb = new ag_upsideo_compte();
                    comptedb.idclient = idClient;
                    comptedb.idcgp = idCgpAgreg;
                    comptedb.deleted = "n";
                }
                else
                {
                    comptedb = agupsideocompteBL.GetComptesByIdCompte(compteeditvm.Idfrontcompte);
                }
                //
                //
                //
                comptedb.typecontrat = compteeditvm.typecontrat;
                comptedb.Idfrontcompte = compteeditvm.Idfrontcompte;
                comptedb.type = "o";

                if (compteeditvm.typecontrat.ToLower() == agupsideocompteBL.comptetitrespea.ToLower() ||
                    compteeditvm.typecontrat.ToLower() == agupsideocompteBL.capitalisation.ToLower())
                {
                    comptedb.pea = compteeditvm.pea.ToLower() == "true" ? "o" : "n";
                    comptedb.numcompte = compteeditvm.numcompte;
                    comptedb.NomTitulaire1 = compteeditvm.NomTitulaire1;
                    comptedb.idfrontfournisseur = compteeditvm.idfrontfournisseur;
                    if (!string.IsNullOrEmpty(compteeditvm.dds))
                    {
                        comptedb.dds = DateTime.ParseExact(compteeditvm.dds, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        comptedb.dds = null;
                    }
                    //
                    //
                    //
                    comptedb = agupsideocompteBL.Updatecompte(comptedb);
                    lstCompte = agupsideocompteBL.GetComptesClient(idClient);
                    agregcompte.AgregCompteContrat = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.assurancevie.ToLower()
                                                                 || c.Typecontrat.ToLower() == agupsideocompteBL.comptetitrespea.ToLower()
                                                                 || c.Typecontrat.ToLower() == agupsideocompteBL.capitalisation.ToLower()
                                                                 || c.Typecontrat.ToLower() == agupsideocompteBL.comptebancaire.ToLower()
                                                                 ).ToList();

                    //historisé 9711
                    agupsideocompteBL.AddCompteHisto(comptedb);

                    return PartialView("ComptesContratsPartial", agregcompte);
                }
                if (compteeditvm.typecontrat.ToLower() == agupsideocompteBL.assurancevie.ToLower() ||
                    compteeditvm.typecontrat.ToLower() == agupsideocompteBL.comptebancaire.ToLower())
                {
                    comptedb.numcompte = compteeditvm.numcompte;
                    comptedb.NomTitulaire1 = compteeditvm.NomTitulaire1;
                    comptedb.idfrontfournisseur = compteeditvm.idfrontfournisseur;
                    if (!string.IsNullOrEmpty(compteeditvm.dds))
                    {
                        comptedb.dds = DateTime.ParseExact(compteeditvm.dds, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        comptedb.dds = null;
                    }
                    //
                    //
                    //
                    comptedb = agupsideocompteBL.Updatecompte(comptedb);
                    lstCompte = agupsideocompteBL.GetComptesClient(idClient);
                    agregcompte.AgregCompteContrat = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.assurancevie.ToLower()
                                             || c.Typecontrat.ToLower() == agupsideocompteBL.comptetitrespea.ToLower()
                                             || c.Typecontrat.ToLower() == agupsideocompteBL.capitalisation.ToLower()
                                             || c.Typecontrat.ToLower() == agupsideocompteBL.comptebancaire.ToLower()
                                             ).ToList();
                    //historisé 9711
                    agupsideocompteBL.AddCompteHisto(comptedb);
                    return PartialView("ComptesContratsPartial", agregcompte);
                }
                if (compteeditvm.typecontrat.ToLower() == agupsideocompteBL.immobilier.ToLower())
                {
                    comptedb.designation = compteeditvm.designation;
                    comptedb.dateacquisition = DateTime.ParseExact(compteeditvm.dateacquisition, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                    comptedb.valeuracquisition = compteeditvm.valeuracquisition;
                    comptedb.totalcompte = compteeditvm.totalcompte;
                    comptedb.fiscalite = compteeditvm.fiscalite;
                    //
                    //
                    //
                    comptedb = agupsideocompteBL.Updatecompte(comptedb);
                    lstCompte = agupsideocompteBL.GetComptesClient(idClient);
                    agregcompte.AgregCompteImmobilier = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.immobilier.ToLower()).OrderBy(c => c.Designation).ToList();
                    //historisé 9711
                    agupsideocompteBL.AddCompteHisto(comptedb);
                    return PartialView("ComptesImmobiliersPartial", agregcompte);
                }
                if (compteeditvm.typecontrat.ToLower() == agupsideocompteBL.professionels.ToLower())
                {
                    DateTime? dt;
                    comptedb.designation = compteeditvm.designation;
                    if (!string.IsNullOrEmpty(compteeditvm.dateacquisition))
                        comptedb.dateacquisition = DateTime.ParseExact(compteeditvm.dateacquisition, "dd/MM/yyyy",
                            System.Globalization.CultureInfo.InvariantCulture);
                    comptedb.valeuracquisition = compteeditvm.valeuracquisition;
                    comptedb.totalcompte = compteeditvm.totalcompte;
                    //
                    //
                    //
                    comptedb = agupsideocompteBL.Updatecompte(comptedb);
                    lstCompte = agupsideocompteBL.GetComptesClient(idClient);
                    agregcompte.AgregCompteProfessionnels = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.professionels.ToLower()).OrderBy(c => c.Designation).ToList();
                    //historisé 9711
                    agupsideocompteBL.AddCompteHisto(comptedb);
                    return PartialView("ComptesProfessionnelsPartial", agregcompte);
                }
                if (compteeditvm.typecontrat.ToLower() == agupsideocompteBL.capitalrisque.ToLower())
                {
                    comptedb.designation = compteeditvm.designation;
                    comptedb.totalcompte = compteeditvm.totalcompte;
                    if (!string.IsNullOrEmpty(compteeditvm.dds))
                    {
                        comptedb.dds = DateTime.ParseExact(compteeditvm.dds, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        comptedb.dds = null;
                    }
                    //
                    //compteeditvm.nbretitre;
                    //compteeditvm.prixunitaire;
                    #region Nbre titre et prixunitaire
                    //
                    AgUpsideoCptrelsupportsBL.UpdateFondCapitalRisque(idCgpAgreg, comptedb, compteeditvm.nbretitre, compteeditvm.prixunitaire);
                    //
                    #endregion
                    //
                    //
                    //
                    lstCompte = agupsideocompteBL.GetComptesClient(idClient);
                    agregcompte.AgregCompteCapitalRisque = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.capitalrisque.ToLower()).OrderBy(c => c.Designation).ToList();
                    //historisé 9711
                    agupsideocompteBL.AddCompteHisto(comptedb);
                    return PartialView("ComptesCapitalRisquePartial", agregcompte);
                }                

            }
            catch (Exception e)
            {
                return Json(new { error = "ok",msg= e.Message }, JsonRequestBehavior.AllowGet);
            }
            return Json(new { error = "ko", msg = string.Empty }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult RepartitionTypeActif(string id = null)
        {

            Guid idClient = Guid.Empty;
            AgregCompteViewModel agregcompte = new AgregCompteViewModel();
            try
            {
                SessionManager.Get<Guid>(agupsideocompteBL.AgregClientIdSessionKey, out idClient);
                Guid idCgpAgreg = Guid.Empty;
                User currentUser = SessionManager.GetUserSession();
                if (currentUser.IsEndUser())
                {
                    CustomerProspect currentCustomer = SessionManager.GetCustomerProspectSession();
                    User currenttUserCreated = currentCustomer.User1;
                    idCgpAgreg = currenttUserCreated.idAgregCGP.Value;
                }
                else
                {
                    idCgpAgreg = currentUser.idAgregCGP.Value;
                }
                //Répartition des actifs par type contrat
                List<AgregChart> lstrepartition = new List<AgregChart>();
                lstrepartition = agupsideocompteBL.GetActifByIdCgpIdclient(idCgpAgreg, idClient);
                List<AgregChart> lstrepartition2 = new List<AgregChart>();
                lstrepartition.ToList().ForEach(u =>
                {
                    lstrepartition2.Add(new AgregChart()
                    {
                        label = u.label,
                        value = Math.Round(u.value,2)
                    });
                });
                //string xmlData = ChartBL.GetChartPie2DTagWithList(lstrepartition2);
                string xmlData = ChartBL.GetChartDougnhutWithList(lstrepartition2);
                //Create the chart - Column 2D Chart with data contained in xmlData
                
                //ViewBag.MyChart = FusionCharts.RenderChart(Url.Content("~/Content/FusionCharts/Pie2D.swf"), "", xmlData, "myChartId", "620", "230", false, true, true, "FFFFFF", "noscale", "EN");
                ViewBag.MyChart = FusionCharts.RenderChart(Url.Content("~/Content/FusionCharts/Doughnut2D.swf"), "", xmlData, "myChartId", "620", "230", false, true, true, "FFFFFF", "noscale", "EN");
                //Client                
                using (UpsilabEntities db = new UpsilabEntities())
                {
                    ag_upsideo_client client = db.ag_upsideo_client.Where(c => c.idclient == idClient && c.deleted == "n").FirstOrDefault();
                    agregcompte.AgregClient = client;
                }
            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
            }
            return View(agregcompte);
        }
        public ActionResult SupprimerCompte(int idcompte, string typecompte)
        {
            //
            //Delete client
            //
            try
            {
                agupsideocompteBL.DeleteCompte(idcompte);
            }
            catch (Exception e)
            {
                return Json(new { error = "ok", msg = e.Message }, JsonRequestBehavior.AllowGet);
            }

            //
            //
            //
            Guid idClient = Guid.Empty;
            SessionManager.Get<Guid>(agupsideocompteBL.AgregClientIdSessionKey, out idClient);
            var lstCompte = agupsideocompteBL.GetComptesClient(idClient);
            var agregcompte = new AgregCompteViewModel();

            switch (typecompte)
            {
                case "comptecontrat":
                    agregcompte.AgregCompteContrat = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.assurancevie.ToLower()
                                                                || c.Typecontrat.ToLower() == agupsideocompteBL.comptetitrespea.ToLower()
                                                                || c.Typecontrat.ToLower() == agupsideocompteBL.capitalisation.ToLower()
                                                                || c.Typecontrat.ToLower() == agupsideocompteBL.comptebancaire.ToLower()
                                                                ).ToList();
                    return PartialView("ComptesContratsPartial", agregcompte);
                case "immobilier":
                    agregcompte.AgregCompteImmobilier = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.immobilier.ToLower()).OrderBy(c => c.Designation).ToList();
                    return PartialView("ComptesImmobiliersPartial", agregcompte);
                case "professionnel":
                    agregcompte.AgregCompteProfessionnels = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.professionels.ToLower()).OrderBy(c => c.Designation).ToList();
                    return PartialView("ComptesProfessionnelsPartial", agregcompte);
                case "capitalrisque":
                    agregcompte.AgregCompteCapitalRisque = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.capitalrisque.ToLower()).OrderBy(c => c.Designation).ToList();
                    return PartialView("ComptesCapitalRisquePartial", agregcompte);
            }
            return Json(new { error = "ko", msg = string.Empty }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index(string id)
        {

            Guid idClient = Guid.Empty;
            ag_upsideo_client client = null;
            var idCustomer = new Guid();
            Guid idCgpAgreg = Guid.Empty;
            User currentUser = SessionManager.GetUserSession();
            CustomerProspect currentCustomer = null;
            if (currentUser.IsEndUser())
            {
                currentCustomer =  SessionManager.GetCustomerProspectSession();
                User currenttUserCreated = currentCustomer.User1;
                idCgpAgreg = currenttUserCreated.idAgregCGP.Value;
            }
            else
            {
                idCgpAgreg = currentUser.idAgregCGP.Value;
            }
            if (!string.IsNullOrEmpty(id))
            {
                idClient = new Guid(id);
                using (UpsilabEntities db = new UpsilabEntities())
                {
                    bool existclient = true;
                    if (currentUser.IsEndUser())
                    {
                        existclient = (currentCustomer.idAgregClient.HasValue && currentCustomer.idAgregClient.Value.Equals(idClient)) ? true : false;
                    }
                    else
                    {
                        existclient = db.ag_upsideo_client.Any(c => c.idclient == idClient && c.idcgp == idCgpAgreg && (c.deleted == "n" || c.deleted == null));
                    }
                    if (!existclient)
                    {
                        var LanguageData = PageLanguageHelper.GetLanguageContent("User", "agregcompte");
                        throw new Exception(LanguageData.GetContent("incorrect_client"));
                        //throw new Exception("Ce client n'est pas un client de l'utilisateur en cours.");
                    }
                }
            }
            AgregCompteViewModel agregcompte = new AgregCompteViewModel();
            try
            {
                //clear session idcompte
                SessionManager.Clear(agupsideocompteBL.AgregCompteIdSessionKey);

                if (!string.IsNullOrEmpty(id))
                {
                    SessionManager.Set<Guid>(agupsideocompteBL.AgregClientIdSessionKey, idClient);
                }
                else
                {
                    SessionManager.Get<Guid>(agupsideocompteBL.AgregClientIdSessionKey, out idClient);
                }

                using (UpsilabEntities db = new UpsilabEntities())
                {
                    client = db.ag_upsideo_client.FirstOrDefault(c => c.idclient == idClient && c.idcgp == idCgpAgreg && c.deleted == "n");
                    if (client!=null && client.CustomerProspect != null && client.CustomerProspect.Count > 0)
                        idCustomer = client.CustomerProspect.ElementAt(0).idCustomer;

                    //obtenir liste de fournisseur d'un cabinet                    
                    List<ag_upsideo_fournisseur> listeFournisseur = new List<ag_upsideo_fournisseur>();
                    listeFournisseur = (from f in db.ag_upsideo_fournisseur
                                        join c in db.ag_upsideo_cabinet on f.idcrmcabinet equals c.idcrmcabinet
                                        join cgp in db.ag_upsideo_cgp on c.idcrmcabinet equals cgp.idcrmcabinet
                                        where cgp.idcgp == idCgpAgreg
                                        select f).ToList();
                    if (listeFournisseur != null)
                    {
                        listeFournisseur.Insert(0, new ag_upsideo_fournisseur());
                        ViewBag.Fournisseur = new SelectList(listeFournisseur, "idfrontfournisseur", "nomfournisseur");
                    }
                    //liste fiscalite
                    List<Fiscalite> listeFiscalite = new List<Fiscalite>();
                    listeFiscalite = db.Fiscalite.ToList();
                    if (listeFiscalite != null)
                    {
                        listeFiscalite.Insert(0, new Fiscalite());
                        ViewBag.Fiscalite = new SelectList(listeFiscalite, "idFiscalite", "nomFiscalite");
                    }

                }

                agregcompte.AgregClient = client;
                agregcompte.CustomerProspectId = idCustomer;
                List<SqlAgregCompteModel> lstCompte = new List<SqlAgregCompteModel>();
                lstCompte = agupsideocompteBL.GetComptesClient(idClient);

                agregcompte.AgregCompteContrat = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.assurancevie.ToLower()
                                                                      || c.Typecontrat.ToLower() == agupsideocompteBL.comptetitrespea.ToLower() 
                                                                      || c.Typecontrat.ToLower() == agupsideocompteBL.capitalisation.ToLower()
                                                                      || c.Typecontrat.ToLower() == agupsideocompteBL.comptebancaire.ToLower()
                                                                      ).ToList();
                                                            
                agregcompte.AgregCompteImmobilier = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.immobilier.ToLower()).OrderBy(c => c.Designation).ToList();
                agregcompte.AgregCompteProfessionnels = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.professionels.ToLower()).OrderBy(c => c.Designation).ToList();
                agregcompte.AgregCompteCapitalRisque = lstCompte.Where(c => c.Typecontrat.ToLower() == agupsideocompteBL.capitalrisque.ToLower()).OrderBy(c => c.Designation).ToList();

                //total
                var totalcomptecontrat = agregcompte.AgregCompteContrat.Sum(c => c.Totalcompte);
                agregcompte.AgregTotalCompteContrat = totalcomptecontrat.HasValue ? totalcomptecontrat.Value : 0;

                var totalCompteImmobilier = agregcompte.AgregCompteImmobilier.Sum(c => c.Totalcompte);
                agregcompte.AgregTotalCompteImmobilier = totalCompteImmobilier.HasValue ? totalCompteImmobilier.Value : 0;

                var totalCompteProfessionnels = agregcompte.AgregCompteProfessionnels.Sum(c => c.Totalcompte);
                agregcompte.AgregTotalCompteProfessionnels = totalCompteProfessionnels.HasValue ? totalCompteProfessionnels.Value : 0;

                var totalCompteCapitalRisque = agregcompte.AgregCompteCapitalRisque.Sum(c => c.Totalcompte);
                agregcompte.AgregTotalCompteCapitalRisque = totalCompteCapitalRisque.HasValue ? totalCompteCapitalRisque.Value : 0;

                agregcompte.AgregTotalCompte = agregcompte.AgregTotalCompteContrat + agregcompte.AgregTotalCompteImmobilier + agregcompte.AgregTotalCompteProfessionnels + agregcompte.AgregTotalCompteCapitalRisque;


            }
            catch (Exception ex)
            {
                Upsilab.Business.Log.Log.AppendException(ex);
            }
            return View(agregcompte);
        }