public async Task InsertAsync(EleveEnrollement eleveEnrollement) { await _context.EleveEnrollements.AddAsync(eleveEnrollement); Enrollement enrollement = await _context.Enrollements.Where(e => e.Id == eleveEnrollement.EnrollementId).FirstOrDefaultAsync(); if (enrollement.Type == "Groupe") { EleveEnrollement currentEleveEnrollement = await _context.EleveEnrollements.Include(ee => ee.Enrollement).Where(ee => ee.Enrollement.Type == "Groupe" && ee.DateDeFin > eleveEnrollement.DateDeDebut && ee.EleveId == eleveEnrollement.EleveId).FirstOrDefaultAsync(); if (currentEleveEnrollement != null) { PayementEnrollement currentPayementEnrollement = new PayementEnrollement(); var paid = 0; List <PayementEnrollement> currentPayementEnrollements = await _context.PayementEnrollements.Where(pe => pe.EleveEnrollementId == currentEleveEnrollement.Id && pe.DateDeFin == currentEleveEnrollement.DateDeFin).ToListAsync(); if (currentPayementEnrollements.Count > 0) { foreach (PayementEnrollement cpe in currentPayementEnrollements) { if (cpe.DateDeDebut >= eleveEnrollement.DateDeDebut) { paid = paid + cpe.Paid; _context.PayementEnrollements.Remove(cpe); } else { currentPayementEnrollement = cpe; } } PayementEnrollementViewModel payementEnrollement = new PayementEnrollementViewModel() { Section = "Scolarité", Paid = 0, EleveEnrollementId = eleveEnrollement.Id, DateDeDebut = eleveEnrollement.DateDeDebut, DateDeFin = currentEleveEnrollement.DateDeFin.Value, }; currentEleveEnrollement.DateDeFin = eleveEnrollement.DateDeDebut; _context.Entry(currentEleveEnrollement).State = EntityState.Modified; await _context.SaveChangesAsync(); currentPayementEnrollement.DateDeFin = eleveEnrollement.DateDeDebut; _context.Entry(currentPayementEnrollement).State = EntityState.Modified; await _context.SaveChangesAsync(); var pe = _mapper.Map <PayementEnrollement>(payementEnrollement); await _context.PayementEnrollements.AddAsync(pe); } else { currentEleveEnrollement.DateDeFin = eleveEnrollement.DateDeDebut; _context.Entry(currentEleveEnrollement).State = EntityState.Modified; await _context.SaveChangesAsync(); } } } }
public void Delete(int eleveid, int Enrollementid, DateTime Datededebut) { EleveEnrollement eleveEnrollement = _context.EleveEnrollements.Where(p => p.EleveId == eleveid && p.EnrollementId == Enrollementid).First(); _context.EleveEnrollements.Remove(eleveEnrollement); }
public void Update(EleveEnrollement eleveEnrollement) { _context.Entry(eleveEnrollement).State = EntityState.Modified; }