public void btnAddVacation(object sender, EventArgs e) { workers = new EFGenericRepository <Person>(new WorkerContext()); vacations = new EFGenericRepository <Vacation>(new WorkerContext()); Vacation holydayn = new Vacation(); Guid id = SearcId(); if (id == IdError) { return; } //db.Workers.AsNoTracking(); // db.Workers.Load(); Person person = workers.FindById(i => i.Id == id); if (person == null) { return; } // db.Entry(person).Reload(); // db.Entry(person).State = EntityState.Modified; // holydayn.People = db.Peoples.AsNoTracking().ToList().FirstOrDefault(i=>i.Id==id); holydayn.Peopleid = person.Id; AddHol addHolForm = new AddHol(person.Day - 1); DateTime date = new DateTime(person.Year, 1, 1); if (person.Day < 1) { MessageBox.Show("don`t have weekend!!!"); return; } addHolForm.dateTimePicker1.Value = date; int Daysu = person.Day; DialogResult result = addHolForm.ShowDialog(this); holydayn.Id = Guid.NewGuid(); holydayn.FirstDate = addHolForm.dateTimePicker1.Value; DateTime CountDate = addHolForm.dateTimePicker1.Value; holydayn.IndexDate = false; holydayn.SecontDate = addHolForm.dateTimePicker2.Value; if (result == DialogResult.Cancel) { return; } int IndexDay = Daysu; for (DateTime i = addHolForm.dateTimePicker1.Value; i <= addHolForm.dateTimePicker2.Value;) { if (i.DayOfWeek != DayOfWeek.Sunday && i.DayOfWeek != DayOfWeek.Saturday && !AuditDate(i)) { IndexDay--; } i = i.AddDays(1); } Daysu = IndexDay; holydayn.TeamName = person.TeamName; holydayn.Days = person.Day - IndexDay; person.Day = Daysu; if (CountTeam(person.TeamName) - CountWeekend(holydayn.FirstDate, holydayn.SecontDate, person.TeamName) <= 1) { MessageBox.Show("Date busy"); return; } vacations.Create(holydayn); workers.Update(person); // db.Vacations.Add(holydayn); addHolForm.Close(); //db.SaveChanges(); evenstb(); }
private void BtnOk(object sender, EventArgs e) { workers = new EFGenericRepository <Person>(new WorkerContext()); Guid id = Id; Person person = workers.FindById(i => i.Id == id); if (person == null) { return; } Vacation holydayn = new Vacation(); holydayn.Peopleid = person.Id; DateTime date = new DateTime(person.Year, 1, 1); if (person.Days < 1) { MessageBox.Show("don`t have weekend!!!"); return; } Team team = EFTeams.FindById(i => i.TeamName == person.Team.TeamName); if (team == null) { return; } int Daysu = person.Days; //DialogResult result = addHolForm.ShowDialog(this); holydayn.Id = Guid.NewGuid(); holydayn.FirstDate = dateTimePickerFirstDate.Value; DateTime CountDate = dateTimePickerFirstDate.Value; holydayn.IndexDate = false; holydayn.SecontDate = dateTimePickerSecondDate.Value; if ((CountTeam(person.Team.TeamName) - CountWeekend(holydayn.FirstDate, holydayn.SecontDate, person.Team.TeamName) <= team.MinNumberWorkers) && team.MinNumberWorkers != 0) { MessageBox.Show("Date busy"); return; } int CountDaysHolyDays = 0; int IndexDay = 0; for (DateTime i = dateTimePickerFirstDate.Value.Date; i <= dateTimePickerSecondDate.Value.Date;) { if (i.DayOfWeek == DayOfWeek.Sunday || i.DayOfWeek == DayOfWeek.Saturday || AuditDate(i)) { IndexDay++; CountDaysHolyDays--; } CountDaysHolyDays++; i = i.AddDays(1); } holydayn.TeamName = person.Team.TeamName; holydayn.Days = CountDaysHolyDays; Daysu = person.Days - CountDaysHolyDays; person.Days = Daysu; if (CountTeam(person.Team.TeamName) - CountWeekend(holydayn.FirstDate, holydayn.SecontDate, person.Team.TeamName) <= team.MinNumberWorkers && team.MinNumberWorkers != 0) { MessageBox.Show("Date busy"); return; } this.GetVacation = holydayn; this.PersonGet = person; workers.Update(person); }