public dynamic deleteDevis(long idDevis, string token)
        {
            int idCompte           = this._tokenHelper.getCompteIdToken(token); // get compte id from token claims
            DevisInformation devis = _context.DevisInformation.FirstOrDefault(d => d.Id == idDevis);

            if (devis == null)
            {
                return(true);
            }
            if (devis.IdCpt == idCompte)
            {
                try
                {
                    _context.DevisInformation.Remove(devis);
                    this._context.SaveChanges();
                    return(new { deleted = true, idDevisDeleted = devis.Id });
                }
                catch
                {
                    return(new { deleted = false });
                }
            }
            else
            {
                return(false);
            }
        }
        public JObject SaveDevisInformation(JObject j, string token)
        {
            dynamic          res   = new JObject();
            DevisInformation devis = null;
            //   try
            //    {
            int id = j["Devis"]["Id"].ToObject <int>();
            //Il s'agit bien d'une modification d'un devis
            var requ = j["Devis"];

            if (id != -1)
            {
                devis = _context.DevisInformation.First(d => d.Id == id);

                if (requ["nombrePoste"] != null)
                {
                    devis.NbrPoste = requ["nombrePoste"].ToObject <int>();
                }
                if (requ["nombreContrat"] != null)
                {
                    devis.NbrContrat = requ["nombreContrat"].ToObject <int>();
                }
                if (requ["logCompta"] != null)
                {
                    devis.LogCompta = requ["logCompta"].ToObject <Softwares>().Name;
                }
                if (requ["LogGestion"] != null)
                {
                    devis.LogGestion = requ["LogGestion"].ToObject <Softwares>().Name;
                }
                if (requ["lettragePart"] != null)
                {
                    devis.LettragePartiel = requ["lettragePart"].ToObject <int>();
                }
                if (requ["clotureFacture"] != null)
                {
                    devis.ClotureFacture = requ["clotureFacture"].ToObject <int>();
                }
                if (requ["preLettrage"] != null)
                {
                    devis.PreLettrage = requ["preLettrage"].ToObject <int>();
                }
                if (requ["volumetrie"] != null)
                {
                    devis.Volumetrie = requ["volumetrie"].ToObject <int>();
                }
                if (requ["mdrVRTS"] != null)
                {
                    devis.MdrPrcntVirts = requ["mdrVRTS"].ToObject <int>();
                }
                if (requ["mdrLCM"] != null)
                {
                    devis.MdrPrcntTrtSaccept = requ["mdrLCM"].ToObject <int>();
                }
                if (requ["mdrBOR"] != null)
                {
                    devis.MdrPrcntTrtAaccept = requ["mdrBOR"].ToObject <int>();
                }
                if (requ["mdrCHQ"] != null)
                {
                    devis.MdrPrcntChq = requ["mdrCHQ"].ToObject <int>();
                }
                if (requ["mdrAutre"] != null)
                {
                    devis.MdrPrcntAutres = requ["mdrAutre"].ToObject <int>();
                }



                _context.SaveChanges();
                res.saved = true;
            }
            //Il s'agit d'une nouvelle insertion d'un devis
            else
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    int        idPublicUser = _tokenHelper.getSidToken(token);
                    PublicUser user         = _context.PublicUser.Include(u => u.Contact).Where(t => t.Id == idPublicUser).First();
                    // Console.WriteLine("PUBLIC USER ====> " + user.Username);
                    Comptes cpt = _context.CompteContact.Select(t => t.IdCptNavigation).Where(p => p.MainContactId == user.Contact.Id).FirstOrDefault();
                    Console.WriteLine("COMPTE ID ====> " + cpt.Id);

                    devis = new DevisInformation();
                    if (requ["nombrePoste"] != null)
                    {
                        devis.NbrPoste = requ["nombrePoste"].ToObject <int>();
                    }
                    if (requ["nombreContrat"] != null)
                    {
                        devis.NbrContrat = requ["nombreContrat"].ToObject <int>();
                    }
                    if (requ["logCompta"] != null)
                    {
                        devis.LogCompta = requ["logCompta"].ToObject <Softwares>().Name;
                    }
                    if (requ["LogGestion"] != null)
                    {
                        devis.LogGestion = requ["LogGestion"].ToObject <Softwares>().Name;
                    }
                    if (requ["lettragePart"] != null)
                    {
                        devis.LettragePartiel = requ["lettragePart"].ToObject <int>();
                    }
                    if (requ["clotureFacture"] != null)
                    {
                        devis.ClotureFacture = requ["clotureFacture"].ToObject <int>();
                    }
                    if (requ["preLettrage"] != null)
                    {
                        devis.PreLettrage = requ["preLettrage"].ToObject <int>();
                    }
                    if (requ["volumetrie"] != null)
                    {
                        devis.Volumetrie = requ["volumetrie"].ToObject <int>();
                    }
                    if (requ["mdrVRTS"] != null)
                    {
                        devis.MdrPrcntVirts = requ["mdrVRTS"].ToObject <int>();
                    }
                    if (requ["mdrLCM"] != null)
                    {
                        devis.MdrPrcntTrtSaccept = requ["mdrLCM"].ToObject <int>();
                    }
                    if (requ["mdrBOR"] != null)
                    {
                        devis.MdrPrcntTrtAaccept = requ["mdrBOR"].ToObject <int>();
                    }
                    if (requ["mdrCHQ"] != null)
                    {
                        devis.MdrPrcntChq = requ["mdrCHQ"].ToObject <int>();
                    }
                    if (requ["mdrAutre"] != null)
                    {
                        devis.MdrPrcntAutres = requ["mdrAutre"].ToObject <int>();
                    }



                    devis.IdCpt = cpt.Id;

                    _context.DevisInformation.Add(devis);
                    _context.SaveChanges();
                    transaction.Commit();
                    res.newId = devis.Id;
                    res.saved = true;
                }
            }
            //  }

            /*    catch (Exception e)
             *  {
             *      res.saved = false;
             *      res.error = e.Message;
             *      throw;
             *  }
             *  finally
             *  {
             *      res.time = DateTime.Now.ToString("HH:mm:ss - dd/MM/yyyy");
             *  }*/

            Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            return(res);
        }
        public JObject SaveIdentiteInformation(JObject j, string token)
        {
            dynamic          res   = new JObject();
            DevisInformation devis = null;
            //   try
            //    {
            int id = j["Devis"]["Id"].ToObject <int>();
            //Il s'agit bien d'une modification d'un devis
            var requ = j["Devis"];

            if (id != -1)
            {
                devis = _context.DevisInformation.First(d => d.Id == id);

                //Identity informations
                if (requ["siren"] != null)
                {
                    devis.Siren = requ["siren"].ToObject <string>();
                }
                if (requ["nomSociete"] != null)
                {
                    devis.NomSociete = requ["nomSociete"].ToObject <string>();
                }
                if (requ["pays"] != null)
                {
                    devis.Pays = requ["pays"].ToObject <string>();
                }
                if (requ["activite"] != null)
                {
                    devis.Activite = requ["activite"].ToObject <string>();
                }
                if (requ["ca"] != null)
                {
                    devis.Ch = requ["ca"].ToObject <int>();
                }
                if (requ["factor"] != null)
                {
                    devis.Factor = requ["factor"].ToObject <string>();
                }
                if (requ["produit"] != null)
                {
                    devis.Produit = requ["produit"].ToObject <string>();
                }

                _context.SaveChanges();
                res.saved = true;
            }
            //Il s'agit d'une nouvelle insertion d'un devis
            else
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    int        idPublicUser = _tokenHelper.getSidToken(token);
                    PublicUser user         = _context.PublicUser.Include(u => u.Contact).Where(t => t.Id == idPublicUser).First();
                    Comptes    cpt          = _context.CompteContact.Select(t => t.IdCptNavigation).Where(p => p.MainContactId == user.Contact.Id).FirstOrDefault();
                    Console.WriteLine("COMPTE ID ====> " + cpt.Id);

                    //Identity informations
                    if (requ["siren"] != null)
                    {
                        devis.Siren = requ["siren"].ToObject <string>();
                    }
                    if (requ["nomSociete"] != null)
                    {
                        devis.NomSociete = requ["nomSociete"].ToObject <string>();
                    }
                    if (requ["pays"] != null)
                    {
                        devis.Pays = requ["pays"].ToObject <string>();
                    }
                    if (requ["activite"] != null)
                    {
                        devis.Activite = requ["activite"].ToObject <string>();
                    }
                    if (requ["ca"] != null)
                    {
                        devis.Ch = requ["ca"].ToObject <int>();
                    }
                    if (requ["factor"] != null)
                    {
                        devis.Factor = requ["factor"].ToObject <string>();
                    }
                    if (requ["produit"] != null)
                    {
                        devis.Produit = requ["produit"].ToObject <string>();
                    }

                    devis.IdCpt = cpt.Id;

                    _context.DevisInformation.Add(devis);
                    _context.SaveChanges();
                    transaction.Commit();
                    res.newId = devis.Id;
                    res.saved = true;
                }
            }
            return(res);
        }
        /**
         * génération du prix total du devis
         */

        public double calculDevis(int produitId, int IdDevis)
        {
            DevisInformation devis      = null;
            double           resuDevis  = 0.0;
            double           mdrCalcul  = 0.0;
            double           LettP      = 0.0;
            DevisInformation devisInfos = _context.DevisInformation.Include(c => c.IdCptNavigation).Where(d => d.Id == IdDevis).FirstOrDefault();

            /** Récupération des infos du devis à partir de son identifiant **/
            devis = new DevisInformation()
            {
                NbrPoste           = devisInfos.NbrPoste,
                NbrContrat         = devisInfos.NbrContrat,
                LogCompta          = devisInfos.LogCompta,
                LogGestion         = devisInfos.LogGestion,
                LettragePartiel    = devisInfos.LettragePartiel,
                ClotureFacture     = devisInfos.ClotureFacture,
                PreLettrage        = devisInfos.PreLettrage,
                Volumetrie         = devisInfos.Volumetrie,
                IdCpt              = devisInfos.IdCpt,
                MdrPrcntTrtAaccept = devisInfos.MdrPrcntTrtAaccept,
                MdrPrcntChq        = devisInfos.MdrPrcntChq,
                MdrPrcntTrtSaccept = devisInfos.MdrPrcntTrtSaccept,
                MdrPrcntVirts      = devisInfos.MdrPrcntVirts,
                MdrPrcntAutres     = devisInfos.MdrPrcntAutres
            };
            if (devis.NbrPoste > 0 && devis.NbrContrat > 0 && !devis.LogCompta.Equals("") && !devis.LogGestion.Equals("") && ((devis.LettragePartiel == 1) ||
                                                                                                                              (devis.LettragePartiel == 0)) && ((devis.PreLettrage == 1) || (devis.PreLettrage == 0)) && (devis.Volumetrie > 0))
            {
                double impactPrix = 0.0;
                /** Récupérer le type du contrat à partir de l'identifiant du produit **/
                TypesContrat tc = this.getContratType(produitId);
                resuDevis = (double)tc.PrixBase;
                if (devis.NbrPoste >= tc.MinNbrPosteInstalle)
                {
                    resuDevis += ((double)devis.NbrPoste - (double)tc.MinNbrPosteInstalle) * (double)tc.UniteSuppNbrPoste;
                }
                if (devis.NbrContrat >= tc.MinNbrContat)
                {
                    resuDevis += ((double)devis.NbrContrat - (double)tc.MinNbrPosteInstalle) * (double)tc.UniteSuppContrat;
                }
                if (devis.Volumetrie >= tc.LimiteHausse)
                {
                    impactPrix += ((double)tc.LimiteHausse - (double)tc.Vnh) * (double)tc.Vspf;
                }
                else
                {
                    if (devis.Volumetrie >= tc.Vnh)
                    {
                        impactPrix += (double)tc.Vspf * ((double)devis.Volumetrie - (double)tc.Vnh);
                    }
                    else
                    {
                        if (devis.Volumetrie >= tc.Vnb)
                        {
                            impactPrix += 0.0;
                        }
                        else
                        {
                            if (devis.Volumetrie >= tc.LimiteBasse)
                            {
                                impactPrix += ((double)tc.Vnb - (double)devis.Volumetrie) * (double)tc.Vipf * (-1);
                            }
                            else
                            {
                                impactPrix += ((double)tc.Vnb - (double)tc.LimiteBasse) * (double)tc.Vipf * (-1);
                            }
                        }
                    }
                }
                resuDevis += impactPrix;
                if ((impactPrix + tc.PrixBase) > 0)
                {
                    if ((devis.MdrPrcntTrtAaccept + devis.MdrPrcntTrtSaccept) > 0)
                    {
                        if (tc.Sigle != "CLA")
                        {
                            mdrCalcul += (double)tc.MdrtrtSacceptation * (double)devis.MdrPrcntTrtSaccept + (double)tc.MdrtrtAacceptation * (double)devis.MdrPrcntTrtAaccept;
                        }
                        else
                        {
                            mdrCalcul += (double)tc.MdrtrtSacceptation;
                        }
                    }
                }
                if (devis.LettragePartiel == 1)
                {
                    LettP = (double)tc.LettragePart;
                }
                resuDevis += (double)(impactPrix + tc.PrixBase) * (mdrCalcul + LettP);
                Console.WriteLine("Achat licence == " + resuDevis);
            }
            return(resuDevis);
        }