/// <summary> /// The method of adding info about Client and Car to Parking /// </summary> private void AddToBalanceParking() { using (ParkingContext db = new ParkingContext()) { var tempCar = db.Cars.Where(c => c.NumberCar == txbInfoNumber.Text).FirstOrDefault(); int tempNum = Int32.Parse(txbInfoPhone.Text); var tempClient = db.Clients.Where(c => c.Phone == tempNum).FirstOrDefault(); BalanceParking temPlace = new BalanceParking { ClientId = tempClient.Id, CarId = tempCar.Id, DataAdded = DateTime.Parse(txbInfoDate.Text), Place = (int)SenderPlace }; var isExistPlace = db.BalanceParking.Where(pl => pl.CarId == temPlace.CarId).FirstOrDefault(); if (isExistPlace == null) { db.BalanceParking.Add(temPlace); db.HistoryAddedCars.Add(new HistoryAddedCars { ClientId = tempClient.Id, CarId = tempCar.Id, DataAdded = DateTime.Parse(txbInfoDate.Text), Place = (int)SenderPlace }); db.SaveChanges(); StatisticsOfPlaces.UpdateStatisticsPlaces(temPlace.Place, '+'); StatisticsOfPlaces.ShowStatisticsInMainWind(); SetColor.SetColorForPlaces((List <Button>)(this.Owner as MainWindow).ListButtons); this.Close(); } } }
private void SelectCar_Loaded(object sender, RoutedEventArgs e) { using (ParkingContext db = new ParkingContext()) { var ListCars = db.Cars.OrderBy(c => c.Manufacture).ToList(); DataGridCars.ItemsSource = ListCars; } }
private void SelectClient_Loaded(object sender, RoutedEventArgs e) { using (ParkingContext db = new ParkingContext()) { //var ListCars = db.Cars.Select(i => new { Manufacture = i.Manufacture, Model = i.Model, Number = i.NumberCar, Id = i.Id }).ToList(); var ListClients = db.Clients.OrderBy(c => c.LastName).OrderBy(cc => cc.FirstName).ToList(); DataGridClients.ItemsSource = ListClients; } }
/// <summary> /// Find place by numberCar /// </summary> /// <param name="numberCar"></param> private void FindPlaceByNumberCar(string numberCar) { using (ParkingContext db = new ParkingContext()) { BalanceParking findPlace = db.BalanceParking.Where(p => p.Car.NumberCar == numberCar).FirstOrDefault(); if (findPlace != null) { if (FindPlaceSetScheme(ListButtons, findPlace)) { ShowSector(findPlace); } } } }
/// <summary> /// Find place by client's phone /// </summary> /// <param name="phoneClient"></param> private void FindPlaceByPhoneClient(string phoneClient) { phoneClient = phoneClient.TrimStart('0'); using (ParkingContext db = new ParkingContext()) { BalanceParking findPlace = db.BalanceParking.Where(p => p.Client.Phone.ToString() == phoneClient.ToString()).FirstOrDefault(); if (findPlace != null) { if (FindPlaceSetScheme(ListButtons, findPlace)) { ShowSector(findPlace); } } } }
/// <summary> /// Initial loading of data into the window ClickPlace /// </summary> private void LoadDataToClickPlace() { using (ParkingContext db = new ParkingContext()) { var placeInfo = db.BalanceParking.Where(p => p.Place == SenderPlace) .Select(pl => new { place = pl.Place, clientLName = pl.Client.LastName, clientFName = pl.Client.FirstName, phone = pl.Client.Phone, manufacture = pl.Car.Manufacture, model = pl.Car.Model, color = pl.Car.Color, number = pl.Car.NumberCar, date = pl.DataAdded }).FirstOrDefault(); // case, that no data in placeInfo if (placeInfo != null) { txbInfoPlace.Text = placeInfo.place.ToString(); txbInfoClientFirstName.Text = placeInfo.clientFName; txbInfoClientLastName.Text = placeInfo.clientLName; txbInfoPhone.Text = placeInfo.phone.ToString(); if (txbInfoPhone.Text.Length < 10) { for (int i = 0; i < 10 - txbInfoPhone.Text.Length; i++) { txbInfoPhone.Text = txbInfoPhone.Text.Insert(0, "0"); } } txbInfoManufacture.Text = placeInfo.manufacture; txbInfoModel.Text = placeInfo.model; txbInfoColor.Text = placeInfo.color; txbInfoNumber.Text = placeInfo.number; txbInfoDate.Text = placeInfo.date.ToShortDateString(); CaseCheckedCar(); } else { txbInfoPlace.Text = SenderPlace.Value.ToString(); CaseNotCheckedCar(); } } }
/// <summary> /// Add Car toDatabase /// </summary> private void AddCarToDB() { Car newCar = new Car(); newCar.Manufacture = txbAddManufacture.Text; newCar.Model = txbAddModel.Text; newCar.Color = txbAddColorCar.Text; newCar.NumberCar = txbAddNumberCar.Text; using (ParkingContext db = new ParkingContext()) { var tempCar = db.Cars.Where(c => c.NumberCar == newCar.NumberCar).FirstOrDefault(); if (tempCar == null) { db.Cars.Add(newCar); db.SaveChanges(); } else { MessageBox.Show("I can't to add this car. It exists in Database!", "Warrning!"); } } }
/// <summary> /// Set color shceme for buttons /// </summary> /// <param name="ListButtons - list with Buttons"></param> public static void SetColorForPlaces(List <Button> ListButtons) { List <int> Places = new List <int>(); using (ParkingContext db = new ParkingContext()) { Places = db.BalanceParking.Select(pl => pl.Place).ToList(); List <int> tt = new List <int>(); for (int i = 0; i < ListButtons.Count; i++) { tt.Add(Int32.Parse(ListButtons[i].Content.ToString())); } IEnumerable <int> unique1 = tt.Except(Places); foreach (int item in unique1) { ListButtons[item - 1].Background = Brushes.White; } foreach (int item in Places) { ListButtons[item - 1].Background = Brushes.Red; } } }
/// <summary> /// Add Car toDatabase /// </summary> private void AddClientToDB() { Client newClient = new Client(); newClient.LastName = txbAddClientLastName.Text; newClient.FirstName = txbAddClientFirstName.Text; newClient.MidleName = txbAddClientMidleName.Text; newClient.Phone = Int32.Parse(txbAddClientPhone.Text); newClient.Adress = txbAddClientAdress.Text; using (ParkingContext db = new ParkingContext()) { var tempClient = db.Clients.Where(c => c.Phone == newClient.Phone).FirstOrDefault(); if (tempClient == null) { db.Clients.Add(newClient); db.SaveChanges(); } else { MessageBox.Show("I can't to add this client. He exist in Database!", "Warrning!"); } } }
/// <summary> /// Take the car out off the parking place /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMoveFromPlace_Click(object sender, RoutedEventArgs e) { if (CheckAllFieldsClickPlace()) { using (ParkingContext db = new ParkingContext()) { using (var tran = db.Database.BeginTransaction()) { try { BalanceParking bal = db.BalanceParking.Where(b => b.Place == SenderPlace).FirstOrDefault(); //BalanceParking bal = new BalanceParking { Place = (int)SenderPlace }; //db.BalanceParking.Attach(bal); if (bal != null) { db.BalanceParking.Remove(bal); db.HistoryDeletedCars.Add(new HistoryDeletedCars { ClientId = bal.ClientId, CarId = bal.CarId, DataAdded = bal.DataAdded, Place = (int)SenderPlace }); db.SaveChanges(); tran.Commit(); btnMoveFromPlace.IsEnabled = false; btnSaveToPlace.IsEnabled = false; StatisticsOfPlaces.UpdateStatisticsPlaces(bal.Place, '-'); StatisticsOfPlaces.ShowStatisticsInMainWind(); SetColor.SetColorForPlaces((List <Button>)(this.Owner as MainWindow).ListButtons); this.Close(); } } catch (Exception) { tran.Rollback(); } } } } }
/// <summary> /// Calculate the number of free places in each sectors /// </summary> public static void StatisticOfPlacesLoad() { using (ParkingContext db = new ParkingContext()) { ListPlaces = db.BalanceParking.Select(pl => pl.Place).ToList(); for (int i = 0; i < ListPlaces.Count; i++) { if (ListPlaces[i] > 0 & ListPlaces[i] < 22) { PlaceNotFreeSector1++; } if (ListPlaces[i] > 21 & ListPlaces[i] < 46) { PlaceNotFreeSector2++; } if (ListPlaces[i] > 45 & ListPlaces[i] < 67) { PlaceNotFreeSector3++; } } } }