public static Promotion GetPromotionalCode(LomsContext db, Booking booking, DateTime pickUpTime, string promoCode, DebugWriter debug) { if (string.IsNullOrWhiteSpace(promoCode)) { debug.WriteLine("Promotional Code was not set in the booking!"); debug.WriteLine(); return null; } var promotions = db.Promotions.Where(p => p.Code == promoCode.Trim()); if (promotions.Count() == 0) { debug.WriteLine("No Promotional Codes with code {0}!", promoCode); debug.WriteLine(); return null; } DateTime pickUpDate = pickUpTime.Date; int pickUpTimeOfDayMinutes = (int)pickUpTime.TimeOfDay.TotalMinutes; var cityVehicleType = db.CityVehicleTypes.FirstOrDefault(cvt => cvt.CityId == booking.CityId && cvt.VehicleTypeId == booking.VehicleTypeId); foreach (Promotion promotion in promotions) { if (promotion.StartDate < pickUpDate && pickUpDate < promotion.EndDate && promotion.FromTime < pickUpTimeOfDayMinutes && pickUpTimeOfDayMinutes < promotion.EndTime) { var vehicles = db.PromotionVehicles.Where(pv => pv.PromotionId == promotion.Id); foreach (var promotionVehicle in vehicles) { if (promotionVehicle.CityVehicleId == cityVehicleType.Id) { if (booking.JourneyType == JourneyType.ToAirport && promotionVehicle.ToAirport || booking.JourneyType == JourneyType.FromAirport && promotionVehicle.FromAirport || //booking.JourneyType == JourneyType.CityToSuburb && promotionVehicle.FromCity || //booking.JourneyType == JourneyType.SuburbToCity && promotionVehicle.ToCity || booking.JourneyType == JourneyType.SuburbToSuburb && promotionVehicle.SuburbToSuburb || booking.JourneyType == JourneyType.AsDirected && promotionVehicle.AsDirected) { debug.WriteLine("Promotional Code - {0} ({1}):", promotion.Name, promotion.Code); debug.Write(" {0}-{1}", promotion.StartDate.ToString("dd/MM/yy"), promotion.EndDate.ToString("dd/MM/yy")); debug.WriteLine(" {0}-{1}:", ToTimeString(promotion.FromTime), ToTimeString(promotion.EndTime)); debug.WriteLine(" Margins: {0}, Bonus: {1}", promotion.Margin, promotion.Bonus); debug.WriteLine(); return promotion; } } } } } debug.WriteLine("Promotional Codes with name {0} do not match booking details!", promoCode); debug.WriteLine(); return null; }
private static SubDivision GetSubDivision(LomsContext db, Booking booking, BookingEndpoint endpoint, DebugWriter debug, out bool autoPending, out double extras) { autoPending = false; extras = 0; int? subId = null; switch (endpoint.Type) { case BookingEndpointType.Suburb: { if (endpoint.Suburb != null) { Landmark autoPendingLandmark = db.Landmarks.FirstOrDefault(l => l.Address1 == endpoint.Address1 && l.SuburbId == endpoint.SuburbId && l.Autopend); if (autoPendingLandmark != null) { autoPending = true; debug.WriteLine("Suburb {0} has autopending landmark {1}!", endpoint.Suburb.FullName, autoPendingLandmark.Name); return null; } subId = endpoint.Suburb.SubDivisionId; debug.Write("Suburb: {0}", endpoint.Suburb.FullName); } else { debug.WriteLine("Suburb has been removed from system!"); return null; } } break; case BookingEndpointType.AirportPrivate: case BookingEndpointType.AirportOther: { if (endpoint.Airport != null) { subId = endpoint.Airport.SubDivisionId; debug.Write("Airport: {0}", endpoint.Airport.Name); //airport extras extras = RateHelper.GetAirportExtras(db, booking, endpoint.Airport.Id, booking.PickUpEndpointId == endpoint.Id, debug); } else { debug.WriteLine("Airport has been removed from system!"); return null; } } break; } if (subId == null) { debug.WriteLine("Sub Division is not specified!"); return null; } var sub = db.SubDivisions.FirstOrDefault(sd => sd.Id == subId); if (sub == null) { debug.WriteLine("Sub Division is not specified!"); return null; } return sub; }
private static FareTypeName GetFarePromotion(LomsContext db, FareType fareType, Booking booking, DebugWriter debug) { var timeZone = db.CityCurrentTimeZones.FirstOrDefault(z => z.CityId == booking.CityId); if (timeZone == null) { debug.WriteLine("Cannot get city local time."); debug.WriteLine(); return null; } DateTime cityLocalTime = DateTime.UtcNow.AddMinutes(timeZone.UtcOffset); var cityVehicleType = db.CityVehicleTypes.FirstOrDefault(cvt => cvt.CityId == booking.CityId && cvt.VehicleTypeId == booking.VehicleTypeId); DateTime pickUpDate = booking.PickUpTime.Value.Date; int pickUpTimeOfDayMinutes = (int)booking.PickUpTime.Value.TimeOfDay.TotalMinutes; var name = (from n in db.FareTypeNames where n.CityId == booking.CityId && n.FareTypeId == (int)fareType && n.StartDate <= pickUpDate && pickUpDate <= n.EndDate && n.FromTime <= pickUpTimeOfDayMinutes && pickUpTimeOfDayMinutes <= n.EndTime select n).FirstOrDefault(); if (name != null) { var nameVehicle = db.FareTypeNameVehicles.FirstOrDefault(nv => nv.FareTypeNameId == name.Id && nv.CityVehicleId == cityVehicleType.Id); if (nameVehicle != null) { if (booking.JourneyType == JourneyType.ToAirport && nameVehicle.ToAirport || booking.JourneyType == JourneyType.FromAirport && nameVehicle.FromAirport || //booking.JourneyType == JourneyType.CityToSuburb && nameVehicle.FromCity || //booking.JourneyType == JourneyType.SuburbToCity && nameVehicle.ToCity || booking.JourneyType == JourneyType.SuburbToSuburb && nameVehicle.SuburbToSuburb || booking.JourneyType == JourneyType.AsDirected && nameVehicle.AsDirected) { if (cityLocalTime.AddDays(nameVehicle.Advance) <= booking.PickUpTime.Value) { var d1 = pickUpDate; var d2 = pickUpDate.AddDays(1.0); //not selected fares var query1 = from b in db.Bookings join bfi in db.BookingFareInfoes on b.Id equals bfi.BookingId join bfii in db.BookingFareInfoItems on bfi.BookingId equals bfii.BookingId where b.Id != booking.Id && b.FarePromotionId == 0 && (b.StatusId == (int)BookingStatus.Fare || b.StatusId == (int)BookingStatus.Pending || b.StatusId == (int)BookingStatus.PendingSubmitted) && b.VehicleTypeId == booking.VehicleTypeId && b.PickUpTime >= d1 && b.PickUpTime <= d2 && bfii.PromotionId == name.Id select b; int used1 = query1.Count(); //selected fares int used2 = db.Bookings.Count(b => b.Id != booking.Id && b.FarePromotionId == name.Id && (b.StatusId == (int)BookingStatus.Fare || b.StatusId == (int)BookingStatus.Submitted || b.StatusId == (int)BookingStatus.Confirmed) && b.VehicleTypeId == booking.VehicleTypeId && b.PickUpTime >= d1 && b.PickUpTime <= d2); if (used1 + used2 < nameVehicle.Limit) { debug.WriteLine("Fare Promotion - {0} ({1}):", name.Name, ((FareType)name.FareTypeId).ToString()); debug.Write(" {0}-{1}", name.StartDate.ToString("dd/MM/yy"), name.EndDate.ToString("dd/MM/yy")); debug.WriteLine(" {0}-{1}:", ToTimeString(name.FromTime), ToTimeString(name.EndTime)); debug.WriteLine(); return name; } } } } } debug.WriteLine("No Fare Promotions for {0}", fareType); debug.WriteLine(); return null; }
public static bool GetPickUpTime(LomsContext db, Booking booking, DateTime dropOffTime, DebugWriter debug, out DateTime pickUpTime) { pickUpTime = DateTime.MinValue; if (booking.JourneyType == JourneyType.ToAirport && (booking.DropOffEndpoint.Type == BookingEndpointType.AirportOther || booking.DropOffEndpoint.Type == BookingEndpointType.AirportPrivate)) { dropOffTime = dropOffTime.AddMinutes((-1.0) * booking.DropOffEndpoint.Airport.CheckInWaitingTime.Value); debug.WriteLine("Airport Check In Waiting Time - " + booking.DropOffEndpoint.Airport.CheckInWaitingTime.ToString() + "minutes"); } //pick-up debug.Write("Pick-Up "); Suburb subFrom = GetSuburb(db, booking.PickUpEndpoint, debug); if (subFrom == null) return false; //drop-off debug.Write("Drop-Off "); Suburb subTo = GetSuburb(db, booking.DropOffEndpoint, debug); if (subTo == null) return false; var info = db.TripInfoes.FirstOrDefault(i => i.FromId == subFrom.Id && i.ToId == subTo.Id); if (info == null) { debug.WriteLine("No Trip Info for this suburbs!"); return false; } debug.WriteLine(); debug.WriteLine("Trip Info:"); debug.WriteLine(" Distance - " + info.Distance.ToString() + "km"); debug.WriteLine(" Time - " + ToTimeString(info.Time)); debug.Write(" Buffer Time (7:00-19:00) - "); debug.WriteLine(info.Time1 != 0 ? ToTimeString(info.Time1) : "<not set>"); debug.Write(" Buffer Time (19:01 - 06:59) - "); debug.WriteLine(info.Time2 != 0 ? ToTimeString(info.Time2) : "<not set>"); int minutes = dropOffTime.Hour * 60 + dropOffTime.Minute; debug.WriteLine(); debug.WriteLine("Drop-off time is " + dropOffTime.ToString("dd/MM/yy HH:mm")); if (7 * 60 <= minutes && minutes <= 19 * 60 && info.Time1 != 0) { pickUpTime = dropOffTime.AddMinutes((info.Time + info.Time1) * (-1)); } else if (minutes < 7 * 60 && 19 * 60 < minutes && info.Time2 != 0) { pickUpTime = dropOffTime.AddMinutes((info.Time + info.Time2) * (-1)); } else { debug.WriteLine("Using City Buffer Times"); City city = booking.City; if (city == null) return false; int bufferTime = 0; if (7 * 60 <= minutes && minutes <= 19 * 60) { if (info.Distance <= 40) bufferTime = city.DayBufferTime40.Value; else if (info.Distance <= 80) bufferTime = city.DayBufferTime80.Value; else if (info.Distance <= 140) bufferTime = city.DayBufferTime140.Value; else if (info.Distance <= 200) bufferTime = city.DayBufferTime200.Value; else bufferTime = city.DayBufferTimeMore.Value; } else if (minutes < 7 * 60 && 19 * 60 < minutes) { if (info.Distance <= 40) bufferTime = city.NightBufferTime40.Value; else if (info.Distance <= 80) bufferTime = city.NightBufferTime80.Value; else if (info.Distance <= 140) bufferTime = city.NightBufferTime140.Value; else if (info.Distance <= 200) bufferTime = city.NightBufferTime200.Value; else bufferTime = city.NightBufferTimeMore.Value; } pickUpTime = dropOffTime.AddMinutes((info.Time + bufferTime) * (-1)); } debug.WriteLine("Pick-up time is " + pickUpTime.ToString("dd/MM/yy HH:mm")); return true; }