public async Task <IActionResult> Create([Bind("FacturesGenereeID,DateFactures,DateGeneration,FactureDe,AssociationID")] FacturesGeneree facturesGeneree)
        {
            if (ModelState.IsValid)
            {
                _context.Add(facturesGeneree);

                List <CompteurEau> compteurs = _context.CompteurEaus.Where(c => c.Actif == true && c.Client.AssociationID == facturesGeneree.AssociationID).ToList();
                for (int i = 0; i < compteurs.Count(); i++)
                {
                    Facture facture = new Facture();
                    facture.CompteurEauID = compteurs[i].CompteurEauID;

                    facture.DateFacture       = facturesGeneree.DateFactures;
                    facture.FactureDe         = facturesGeneree.FactureDe;
                    facture.FacturesGenereeID = facturesGeneree.FacturesGenereeID;
                    var f = await _context.Factures.OrderByDescending(m => m.FactureDe).FirstOrDefaultAsync(m => m.CompteurEauID == compteurs[i].CompteurEauID);

                    if (f != null)
                    {
                        facture.ValeurCompteurPrecedente = f.ValeurCompteur;
                    }
                    facture.isPayee = false;
                    _context.Add(facture);
                    facturesGeneree.Factures.Add(facture);
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AssociationID"] = new SelectList(_context.Associations, "AssociationID", "AssociationID", facturesGeneree.AssociationID);
            return(View(facturesGeneree));
        }
        public async Task <IActionResult> Edit(int id, [Bind("FacturesGenereeID,DateFactures,DateGeneration,FactureDe,AssociationID")] FacturesGeneree facturesGeneree)
        {
            if (id != facturesGeneree.FacturesGenereeID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(facturesGeneree);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FacturesGenereeExists(facturesGeneree.FacturesGenereeID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AssociationID"] = new SelectList(_context.Associations, "AssociationID", "AssociationID", facturesGeneree.AssociationID);
            return(View(facturesGeneree));
        }
        // GET: FacturesGenerees/Create
        public IActionResult Create()
        {
            ViewData["Association"] = new SelectList(_context.Associations, "AssociationID", "Nom");

            var model = new FacturesGeneree();

            model.DateFactures   = DateTime.Now;
            model.FactureDe      = DateTime.Now;
            model.DateGeneration = DateTime.Now;


            return(View(model));
        }