private void MyInit() { lblProId.Text = "Not active"; lblProId.ForeColor = Color.Red; bot = Boting.GetInstance(); uPD = ChangeLabel; uPL = OutLog; uPB = UpdateImg; }
private void addReservationButton_Click(object sender, EventArgs e) { Reservation newReservation; DateTime beginDate = beginDateTimePicker.Value; DateTime endDate = endDateTimePicker.Value; String reservationHolderName = reservationHolderComboBox.Text; Client client = _db.Client.FirstOrDefault(a => reservationHolderName.Equals(a.Personne.Nom_Personne + " " + a.Personne.Prenom_Personne)); if (client?.Personne == null) { MessageBox.Show(Resources.client_not_found); return; } if (endDate < beginDate) { MessageBox.Show(Resources.beginDate_superior_endDate); return; } if (locationsListBox.Items.Count == 0) { MessageBox.Show(Resources.reservation_need_at_least_1_location); return; } foreach (LocationItem locationItem in locationsListBox.Items) { if (!locationItem.Lodgers.Any()) { MessageBox.Show("L'emplacement " + locationItem.LocationName + " ne contient aucun résident"); return; } if (_db.Reservation.Where(a => ((beginDate >= a.Date_Debut && beginDate <= a.Date_Fin) || (endDate >= a.Date_Debut && endDate <= a.Date_Fin)) && a.Code_Reservation != _resToEditCode) .SelectMany(a => a.Loge) .Any(a => a.Code_Emplacement == locationItem.Location.Code_Emplacement)) { MessageBox.Show("L'emplacement " + locationItem.LocationName + " n'est pas libre pendant la période sélectionnée"); return; } } if (_mode == Mode.Add) { newReservation = new Reservation { Personne = client.Personne }; _db.Reservation.Add(newReservation); } else { newReservation = _db.Reservation.FirstOrDefault(a => a.Code_Reservation == _resToEditCode); if (newReservation != null) { foreach (Loge loge in _db.Loge.Where(a => a.Code_Reservation == _resToEditCode)) { _db.Loge.Remove(loge); } } } if (newReservation != null) { foreach (LocationItem locationItem in locationsListBox.Items) { foreach (Lodger lodger in locationItem.Lodgers) { Loge loge = new Loge { Emplacement = locationItem.Location, Personne = lodger.Client.Personne, Reservation = newReservation }; newReservation.Loge.Add(loge); } } newReservation.Date_Debut = beginDate; newReservation.Date_Fin = endDate; newReservation.Est_Paye = PayedCheckBox.Checked; try { _db.SaveChanges(); if (_mode == Mode.Add) { MessageBox.Show(Resources.reservation_done); } else { MessageBox.Show(Resources.reservation_edited); } } catch (DbUpdateException ex) { MessageBox.Show(Resources.db_save_error + "\n" + ex.InnerException?.InnerException?.ToString()); } } }