private CostPerDay GetTodaysTotalCost(int vehicleId, DateTime todaysDate) { VehicleQueries vehicleQueries = new VehicleQueries(); CostPerDay cost = new CostPerDay(); cost = vehicleQueries.GetTodaysCost(vehicleId, todaysDate); return(cost); }
private void Save() { //dodawanie urządzenia do bazy //tworzenie obiektów i przypisanie im wartości z wypełnionego formularza genre tmpGenre = new genre(); tmpGenre.genre_name = KindOfDevice.ToLower(); device tmpDevice = new device(); tmpDevice.genre = tmpGenre; tmpDevice.manufacturer_name = ManufacturerName.ToLower(); tmpDevice.model_name = ModelName.ToLower(); tmpDevice.serial_number = SerialNumber.ToLower(); try { CostPerDay = CostPerDay.Replace(".", ","); tmpDevice.price = Convert.ToDouble(CostPerDay); } catch (Exception e) { File.AppendAllText(MainWindowViewModel.PathToLog, e.ToString()); System.Windows.MessageBox.Show("Zła wartość liczbowa"); return; } try { using (SubscriptionContext context = new SubscriptionContext()) { context.devices.Add(tmpDevice); context.SaveChanges(); } System.Windows.MessageBox.Show("Dodano urządzenie do bazy."); } catch (Exception e) { File.AppendAllText(MainWindowViewModel.PathToLog, e.ToString()); System.Windows.MessageBox.Show("Nie dodano urządzenia do bazy."); } }
public CostPerDay GetTodaysCost(int vehicleId, DateTime todaysDate) { CostPerDay cost = new CostPerDay(); try { // Establish DB connection VehicleDB db = new VehicleDB(connectionString); db.Connection.Open(); //Query. Check if there is any post for the date. var queryResult = (from v in db.CostPerDays where v.VehicleId == vehicleId && v.Date.Date == todaysDate.Date select v).SingleOrDefault(); //Close connection db.Connection.Close(); if (queryResult != null) { cost.Id = queryResult.Id; cost.VehicleId = queryResult.VehicleId; cost.Date = queryResult.Date; cost.CostThisDay = queryResult.CostThisDay; } else { cost = null; } } catch (Exception ex) { cost = null; } return(cost); }
public string ReturnTotalTollFeeForToday(string typedRegistrationNumber) { DateTime vehiclePassedAt = DateTime.Now; //Try to get vehicle from database Vehicle vehicle = new Vehicle(); vehicle = GetVehicle(typedRegistrationNumber.ToUpper()); if (vehicle != null) { //In case the vehicle is a toll free vehicle, return a message saying so. //(And the only vehicle that is not a toll free vehicle is the type is the: CAR.) if (vehicle.VehicleType != "Car") { return("The vehicle with registration number " + vehicle.RegistrationNumber + " is a " + vehicle.VehicleType + " vehicle and it is a toll free vehicle."); } //Is it weekend or a holiday today? If yes, Yay! NO TOLL FEE!! if (DateSystem.IsWeekend(vehiclePassedAt, CountryCode.SE) | DateSystem.IsPublicHoliday(vehiclePassedAt, CountryCode.SE)) { return("It's weekend or holiday today. So " + vehicle.RegistrationNumber + " does not have to pay any toll fee today!"); } //Has the CAR passed at least once in the last 60 min period? //Then the passage with the highest price is the only one that counts. List <DriveBy> lastHoursDriveBys = new List <DriveBy>(); lastHoursDriveBys = GetDriveBys1HourBack(vehicle.Id, vehiclePassedAt); //Get current Toll fee int tollFeeForCurrentDriveBy = GetTollFee(vehiclePassedAt); //Mapp driveBy DriveBy curentDriveBy = new DriveBy(); curentDriveBy.PassageCost = tollFeeForCurrentDriveBy; curentDriveBy.PassedAt = vehiclePassedAt; curentDriveBy.VehicleId = vehicle.Id; if (lastHoursDriveBys != null) { //Check if the latest passage is the more expensive than the other ones. bool currentTollFeeIsMoreExpensive = false; List <int> tollFeeCostsTheLastHour = new List <int>(); for (int i = 0; i < lastHoursDriveBys.Count(); i++) { tollFeeCostsTheLastHour.Add(lastHoursDriveBys[i].PassageCost); } if (tollFeeForCurrentDriveBy > tollFeeCostsTheLastHour.Max()) { currentTollFeeIsMoreExpensive = true; } if (currentTollFeeIsMoreExpensive) { //If yes, okay, get the diffrence and add that difference to the CostPerDay to update the total toll fee for the day. int costDiff = tollFeeForCurrentDriveBy - tollFeeCostsTheLastHour.Max(); //Add current passing to DriveBys table bool driveByAdded = false; curentDriveBy.PassageCost = costDiff; driveByAdded = AddDriveBy(curentDriveBy); //Update the CostPerDays table with the diff bool costPerDayWasAddedOrUpdated = false; costPerDayWasAddedOrUpdated = UpdateOrAddCostPerDay(curentDriveBy); if (driveByAdded && costPerDayWasAddedOrUpdated) { CostPerDay cost = new CostPerDay(); cost = GetTodaysTotalCost(vehicle.Id, curentDriveBy.PassedAt); if (cost == null) { return("Could not get the total cost for today but the current toll fee replaced the previous highest toll fee for the past hour for " + vehicle.RegistrationNumber + "."); } else { return("Current Toll fee (" + tollFeeForCurrentDriveBy.ToString() + " kr) replaced the previous highest toll fee for the past hour for " + vehicle.RegistrationNumber + ". Now the total cost for " + cost.Date.ToShortDateString() + " is " + cost.CostThisDay + " kr."); } } else { return("Could not Add the current driveby and/or could not Add/Update the total cost for today with the current toll fee for: " + vehicle.RegistrationNumber + "."); } } else { //If NO, good. No need to add anything to the CostPerDays table. Only add to the DriveBys table. //Add current passaing to DriveBys table bool driveByAdded = false; driveByAdded = AddDriveBy(curentDriveBy); if (driveByAdded) { CostPerDay cost = new CostPerDay(); cost = GetTodaysTotalCost(vehicle.Id, curentDriveBy.PassedAt); if (cost == null) { return("Could not get the total cost for today. Current toll fee did not need to be added to the DB for: " + vehicle.RegistrationNumber + "."); } else { return("Toll fee (" + curentDriveBy.PassageCost.ToString() + " kr) did not need to be added for " + vehicle.RegistrationNumber + ". Total cost for " + cost.Date.ToShortDateString() + " is " + cost.CostThisDay + " kr."); } } else { return("Someting went wrong when adding DriveBy to the DB when one or more already exist in the table for " + vehicle.RegistrationNumber + "."); } } } else { // No Drivebys the last hour for this car. Just add the latest passage to the CostPerDay & DriveBys tables. bool driveByAdded = false; driveByAdded = AddDriveBy(curentDriveBy); if (driveByAdded) { //Update or add total cost for the day. bool costPerDayWasAddedOrUpdated = false; costPerDayWasAddedOrUpdated = UpdateOrAddCostPerDay(curentDriveBy); if (costPerDayWasAddedOrUpdated) { CostPerDay cost = new CostPerDay(); cost = GetTodaysTotalCost(vehicle.Id, curentDriveBy.PassedAt); if (cost == null) { return("Could not get the total cost for today but the current toll fee was added for " + vehicle.RegistrationNumber + "."); } else { return("Toll fee (" + curentDriveBy.PassageCost.ToString() + " kr) was added for " + vehicle.RegistrationNumber + ". Total cost for " + cost.Date.ToShortDateString() + " is " + cost.CostThisDay + " kr."); } } else { return("Could not Add or Update the total cost for the day with the current toll fee for: " + vehicle.RegistrationNumber + "."); } } else { //Something went wrong return("Something went wrong when trying to add the passage to the DB for " + vehicle.RegistrationNumber + "."); } } } //Vehicle not found. return("Vehicle not found. Try a different registration number."); }