public ActionResult InsertMvmt(ag_upsideo_mouvement _mvmt)
        {
            int idCompte = 0;
            SessionManager.Get<int>(agupsideocompteBL.AgregCompteIdSessionKey, out idCompte);
            Guid idAgregCgp = SessionManager.GetUserSession().idAgregCGP.Value;
            ag_upsideo_cgp agregcgp = AgregCgpBL.GetCgpById(idAgregCgp);
            try
            {
                int idmouvement = 0;
                idmouvement = _mvmt.idmouv;
                ag_upsideo_mouvement newmvmt;
                ag_upsideo_compte compte;
                bool isValorisation = false;
                string action = "historique";
                if (Request.Form["Isvalorisation"] != null && Convert.ToInt32(Request.Form["Isvalorisation"]) == 1)
                {
                    isValorisation = true;
                    action = "valoriser";
                }
                //new
                if (_mvmt.libellemouvement.ToLower().Equals("souscription")
                    || _mvmt.libellemouvement.ToLower().Equals("coupon")
                    || _mvmt.libellemouvement.ToLower().Equals("remboursement")
                    || _mvmt.libellemouvement.ToLower() == "participation aux bénéfices"
                    || _mvmt.libellemouvement.ToLower() == "participation benefice"
                    || _mvmt.libellemouvement.ToLower() == "régulation historique"
                    || _mvmt.libellemouvement.ToLower() == "transfert"
                                || _mvmt.libellemouvement.ToLower().Equals("souscription/adhésion/ouverture de compte")
                                || _mvmt.libellemouvement.ToLower().Contains("versement"))
                {
                    _mvmt.montantnet = _mvmt.montantnet.HasValue ? _mvmt.montantnet.Value : 0;
                    _mvmt.montantbrut = _mvmt.montantbrut.HasValue ? _mvmt.montantbrut.Value : 0;
                    _mvmt.frais = _mvmt.frais.HasValue ? _mvmt.frais.Value : 0;
                }
                else
                {
                    _mvmt.montantnet = _mvmt.montantnet.HasValue ? -_mvmt.montantnet.Value : 0;
                    _mvmt.montantbrut = _mvmt.montantbrut.HasValue ? -_mvmt.montantbrut.Value : 0;
                    _mvmt.frais = _mvmt.frais.HasValue ? -_mvmt.frais.Value : 0;
                }
                //
                using (UpsilabEntities db = new UpsilabEntities())
                {
                    newmvmt = db.ag_upsideo_mouvement.Where(m => m.idmouv == idmouvement).FirstOrDefault();
                    if (idmouvement == 0)
                    {
                        newmvmt = new ag_upsideo_mouvement()
                        {
                            Idfrontcompte = idCompte,                            
                            libellemouvement = _mvmt.libellemouvement,
                            montantbrut = _mvmt.montantbrut,
                            montantnet = _mvmt.montantnet,
                            frais = _mvmt.frais/100,
                            devise = _mvmt.devise,
                            action = action,
                            datemvt = _mvmt.datemvt,
                            deleted = "n"
                        };
                        //insert
                        db.ag_upsideo_mouvement.AddObject(newmvmt);
                    }
                    else
                    {
                        newmvmt.datemvt = _mvmt.datemvt;
                        newmvmt.libellemouvement = _mvmt.libellemouvement;
                        newmvmt.montantbrut = _mvmt.montantbrut;
                        newmvmt.montantnet = _mvmt.montantnet;
                        newmvmt.frais = _mvmt.frais/100;
                        newmvmt.devise = _mvmt.devise;
                        newmvmt.action = action;                        
                    }
                    //save
                    db.SaveChanges();
                    //valoriser
                    //compte
                    compte = db.ag_upsideo_compte.Where(c => c.Idfrontcompte == idCompte && (c.deleted == null || (c.deleted != null && c.deleted == "n"))).FirstOrDefault();
                    if (isValorisation && compte !=null)
                    {
                        string libellemouvement = _mvmt.libellemouvement.ToLower();
                        double pru = 0;
                        //déjà definit en haut
                        /*if (libellemouvement.ToLower().Equals("souscription")
                                || libellemouvement.ToLower().Equals("souscription/adhésion/ouverture de compte")                                
                                || libellemouvement.ToLower().Contains("versement"))
                        {
                            pru = _mvmt.montantnet.HasValue ? _mvmt.montantnet.Value : 0;
                        }
                        else
                        {
                            pru = _mvmt.montantnet.HasValue ? -_mvmt.montantnet.Value : 0;
                        }*/
                        pru = _mvmt.montantnet.HasValue ? _mvmt.montantnet.Value : 0;
                        
                        //voir si le cabinet a un fond fictif (nomsupport = Liquidités)                        
                        ag_upsideo_support support = db.ag_upsideo_support.Where(s => s.idcrmcabinet == agregcgp.idcrmcabinet && s.nomsupport == "Liquidités" && (s.deleted == null || (s.deleted != null && s.deleted == "n"))).FirstOrDefault();
                        //crée un support si n'existe pas
                        if (support == null)
                        {
                            ag_upsideo_referentiel referentiel = db.ag_upsideo_referentiel.Where(r => r.nomsupport == "Liquidités").OrderByDescending(o => o.datevl).FirstOrDefault();
                            support = new ag_upsideo_support()
                            {
                                idrefsupport = referentiel.idrefsupport,
                                idcrmcabinet = agregcgp.idcrmcabinet,
                                idsg = -1,
                                idtypesupport = -1,
                                nomsupport = referentiel.nomsupport,
                                datevl = referentiel.datevl,
                                devise = referentiel.devise,
                                deleted = "n",
                                vl = 0,


                            };
                            support = AgregSupportBL.AddObjetcSupport(support);
                        }
                        else
                        {
                            support.deleted = "n";
                            support.vl = 0;
                        }
                        ag_upsideo_cptrelsupports relation = db.ag_upsideo_cptrelsupports.Where(s => s.Idfrontcompte == compte.Idfrontcompte && s.idfrontsupport == support.idfrontsupport).FirstOrDefault();
                        if (relation == null)
                        {
                            relation = new ag_upsideo_cptrelsupports()
                            {
                                Idfrontcompte = compte.Idfrontcompte,
                                idfrontsupport = support.idfrontsupport,
                                pru = pru,
                                qte = 1,
                                datemaj = DateTime.Now
                            };
                            db.ag_upsideo_cptrelsupports.AddObject(relation);
                        }
                        else
                        {
                            relation.pru = relation.pru + pru;
                            relation.qte = 1;
                        }
                        //save
                        db.SaveChanges();                                                                           
                        
                        db.CommandTimeout = 180;                        
                        db.ExecuteStoreCommand("exec agreg.frontalweb_majtotalcompte @idcrmcabinet,@idfrontcompte", new SqlParameter("@idcrmcabinet", agregcgp.idcrmcabinet), new SqlParameter("@idfrontcompte", idCompte));
                        //save
                        db.SaveChanges();
                        
                    }                    

                }

                if (isValorisation && compte != null)
                {
                    using (UpsilabEntities db = new UpsilabEntities())
                    {
                        //compte aprés maj
                        ag_upsideo_compte compteahistoriser = db.ag_upsideo_compte.Where(c => c.Idfrontcompte == idCompte && (c.deleted == null || (c.deleted != null && c.deleted == "n"))).FirstOrDefault();

                        //historisé dans ag_upsideo_compte_histo
                        ag_upsideo_compte_histo cpthisto = db.ag_upsideo_compte_histo.Where(h => h.idfrontcompte == idCompte
                                                                                            && h.datehisto.Year == DateTime.Now.Year
                                                                                            && h.datehisto.Month == DateTime.Now.Month
                                                                                            && h.datehisto.Day == DateTime.Now.Day).FirstOrDefault();
                        if (cpthisto != null)
                        {
                            cpthisto.datehisto = DateTime.Now;
                            cpthisto.totalcompte = compteahistoriser.totalcompte.Value;
                        }
                        else
                        {
                            ag_upsideo_compte_histo cpthistonew = new ag_upsideo_compte_histo()
                            {
                                datehisto = DateTime.Now,
                                idfrontcompte = compteahistoriser.Idfrontcompte,
                                devise = string.IsNullOrEmpty(compteahistoriser.devise) ? "EUR" : compteahistoriser.devise,
                                totalcompte = compteahistoriser.totalcompte.HasValue ? compteahistoriser.totalcompte.Value : 0
                            };
                            //insert
                            db.ag_upsideo_compte_histo.AddObject(cpthistonew);
                        }
                        //save
                        db.SaveChanges();
                        //fin historisation
                    }
                }
               
            }
            catch (Exception ex)
            {
                ViewBag.Style = "style=color:red";
                ViewBag.Message = "Erreur: " + ex.Message;                
            }

            return RedirectToAction("Index", new { id = idCompte });
        }
 public PartialViewResult LoadPopupSaisieMvmt(string id)
 {
     try
     {
         int idCompte = 0;
         ag_upsideo_compte compte = null;
         SessionManager.Get<int>(agupsideocompteBL.AgregCompteIdSessionKey, out idCompte);
         int idmouv = 0;
         ag_upsideo_mouvement mouvement = null;
         if (!string.IsNullOrEmpty(id))
         {
             idmouv = Convert.ToInt32(id);
         }
         using (UpsilabEntities db = new UpsilabEntities())
         {
             compte = db.ag_upsideo_compte.Include("ag_upsideo_client").Where(c => c.Idfrontcompte == idCompte && c.deleted == "n").FirstOrDefault();
             mouvement = db.ag_upsideo_mouvement.Where(m => m.idmouv == idmouv).FirstOrDefault();
             if (mouvement != null)
             {
                 mouvement.montantbrut = mouvement.montantbrut.HasValue ? Math.Abs(mouvement.montantbrut.Value) : 0;
                 mouvement.montantnet = mouvement.montantnet.HasValue ? Math.Abs(mouvement.montantnet.Value) : 0;
                 mouvement.frais = mouvement.frais.HasValue ? Math.Abs(mouvement.frais.Value) : 0;
             }
         }
         if (compte == null)
         {
             return PartialView("ErreurPartial", "Session compte perdue.Il faut revenir au compte.");
         }
         if (mouvement == null)
         {
             mouvement = new ag_upsideo_mouvement()
             {
                 Idfrontcompte = idCompte
             };
         }
         Tuple<ag_upsideo_mouvement, ag_upsideo_compte> tuple = new Tuple<ag_upsideo_mouvement, ag_upsideo_compte>(mouvement, compte);
         return PartialView("PopupSaisieMouvementPartial", tuple);
         
     }
     catch (Exception ex)
     {
         return PartialView("ErreurPartial", ex.Message);
     }
 }