public static double GetRate(LomsContext db, Booking booking, DebugWriter debugInfoWriter) { double extras; bool autoPending = false; double price = 0.0; try { price = GetBaseRateWithAdminFees(db, booking, debugInfoWriter, out autoPending, out extras); if (price == 0.0 || autoPending) return 0.0; } catch (Exception ex) { debugInfoWriter.WriteLine("Exception during GetBaseRateWithAdminFees!"); debugInfoWriter.WriteLine(ex.ToString()); return 0.0; } RateHelper.ApplyVehicleMargin(db, booking, debugInfoWriter, ref price); price += extras; RateHelper.ApplyHourZone(db, booking, debugInfoWriter, ref price); //RateHelper.ApplyEventMargin(db, booking, debugInfoWriter, ref price, ref autoPending, ref eventName); //if (autoPending) // return 0.0; return price; }
private static double GetBaseRateWithAdminFees(LomsContext db, Booking booking, DebugWriter debug, out bool autoPending, out double extras) { autoPending = false; extras = 0; //pick-up debug.WriteLine("Pick-Up:"); double extrasForPickUp; SubDivision subFrom = GetSubDivision(db, booking, booking.PickUpEndpoint, debug, out autoPending, out extrasForPickUp); if (subFrom == null || autoPending) return 0.0; Zone zoneFrom; Region regionFrom; debug.WriteSubDivisionInfo(db, subFrom, out zoneFrom, out regionFrom); if (extrasForPickUp != 0.0) debug.WriteLine("Airport Extras: {0:0.00}", extrasForPickUp); //drop-off debug.WriteLine(); debug.WriteLine("Drop-Off:"); double extrasForDropOff; SubDivision subTo = GetSubDivision(db, booking, booking.DropOffEndpoint, debug, out autoPending, out extrasForDropOff); if (subTo == null) return 0.0; Zone zoneTo; Region regionTo; debug.WriteSubDivisionInfo(db, subTo, out zoneTo, out regionTo); if (extrasForDropOff != 0.0) debug.WriteLine("Airport Extras: {0:0.00}", extrasForDropOff); debug.WriteLine(); SubDivisionRate baseRate = db.SubDivisionRates.FirstOrDefault(r => r.SubDivisionFromId == subFrom.Id && r.SubDivisionToId == subTo.Id); debug.WriteBaseRate(baseRate); if (baseRate == null) return 0.0; double price = 0.0; if (baseRate != null) { price = baseRate.Rate; //admin fees List<AdminFee> adminFees = GetAdminFees(db, booking, zoneFrom, regionFrom, zoneTo, regionTo); if (adminFees != null) foreach (var value in adminFees) price += baseRate.Rate * value.Margin / 100.0 + value.Fee; } extras = extrasForPickUp + extrasForDropOff; return price; }
public static void FillBookingFareInfoAndItems(LomsContext db, Booking booking, BookingFareInfo bookingFareInfo, decimal price, byte associationCurrencyId, DebugWriter debugInfoWriter) { try { CountryFareType[] countryFareTypes = RateHelper.GetCountryFareTypes(db, booking, debugInfoWriter); if (countryFareTypes == null) { bookingFareInfo.DebugInfo = debugInfoWriter.ToString(); return; } var farePromotions = RateHelper.GetFarePromotions(db, booking, debugInfoWriter); double[] fareMargins = RateHelper.GetFareMargins(db, booking, debugInfoWriter); if (fareMargins == null) { bookingFareInfo.DebugInfo = debugInfoWriter.ToString(); return; } if (farePromotions[0] == null) fareMargins[0] = 0.0; if (farePromotions[1] == null) fareMargins[1] = 0.0; if (farePromotions[3] == null) fareMargins[3] = 0.0; //if (fareMargins[0] + fareMargins[1] + fareMargins[2] + fareMargins[3] + fareMargins[4] == 0.0) //{ // bookingFareInfo.DebugInfo = debugInfoWriter.ToString(); // return; //} bookingFareInfo.Price = price; CreateOrUpdateBookingFareInfoItems(bookingFareInfo, price, countryFareTypes, farePromotions, fareMargins); ApplyCurrencyBookingFareInfoAndItems(db, booking, bookingFareInfo, associationCurrencyId, debugInfoWriter); ApplyPromoCodeToBookingFareInfoAndItems(db, booking, bookingFareInfo, debugInfoWriter); //rounding bookingFareInfo.Price = booking.City.Round(bookingFareInfo.Price); bookingFareInfo.Items.ForEach(i => i.Price = booking.City.Round(i.Price)); } catch (Exception ex) { debugInfoWriter.WriteLine(ex.ToString()); } bookingFareInfo.DebugInfo = debugInfoWriter.ToString(); }
private static void ApplyVehicleMargin(LomsContext db, Booking booking, DebugWriter debug, ref double price) { var cityVehicleType = db.CityVehicleTypes.FirstOrDefault(cvt => cvt.CityId == booking.CityId && cvt.VehicleTypeId == booking.VehicleTypeId); if (cityVehicleType == null) { debug.WriteLine("{0} city does not support {1} vehicle!", booking.City.Name, booking.VehicleType.Name); debug.WriteLine(); } else { debug.WriteLine("City Vehicle Rate ({0}):", booking.VehicleType.Name); debug.WriteLine(" Margin: {0:0.00}%", cityVehicleType.Margin); debug.WriteLine(" Fee: ${0:0.00}", cityVehicleType.Fee); debug.WriteLine(); price = price * cityVehicleType.Margin / 100.0 + cityVehicleType.Fee; } }
private static void ApplyHourZone(LomsContext db, Booking booking, DebugWriter debug, ref double price) { HourZoneMargin hourZoneMargin = RateHelper.GetHourZone(db, booking, debug); if (hourZoneMargin != null) { if (booking.JourneyType == JourneyType.ToAirport) price = price * hourZoneMargin.MarginToAirport / 100.0 + hourZoneMargin.FeeToAirport; else if (booking.JourneyType == JourneyType.FromAirport) price = price * hourZoneMargin.MarginFromAirport / 100.0 + hourZoneMargin.FeeFromAirport; //else if (booking.JourneyType == JourneyType.CityToSuburb) // price = price * hourZoneMargin.MarginFromCity / 100.0 + hourZoneMargin.FeeFromCity; //else if (booking.JourneyType == JourneyType.SuburbToCity) //price = price * hourZoneMargin.MarginToCity / 100.0 + hourZoneMargin.FeeToCity; else if (booking.JourneyType == JourneyType.SuburbToSuburb) price = price * hourZoneMargin.MarginSuburbToSuburb / 100.0 + hourZoneMargin.FeeSuburbToSuburb; else if (booking.JourneyType == JourneyType.AsDirected) price = price * hourZoneMargin.MarginAsDirected / 100.0 + hourZoneMargin.FeeAsDirected; } }
public PickUpTimeResponse GetPickUpTime(int bookingId, DateTime dropOffTime) { try { using (var db = new LomsContext()) { var booking = GetBooking(db, bookingId); DebugWriter debug = new DebugWriter(); DateTime pickUpTime; if (!PickUpTimeHelper.GetPickUpTime(db, booking, dropOffTime, debug, out pickUpTime)) return new PickUpTimeResponse() { PickUpTime = null, DebugInfo = debug.ToString() }; return new PickUpTimeResponse() { PickUpTime = pickUpTime, DebugInfo = debug.ToString() }; } } catch (Exception ex) { var response = new PickUpTimeResponse(); response.AddError("Response", ex.ToString()); return response; } }
private static double GetAirportExtras(LomsContext db, Booking booking, int airportId, bool isPickUp, DebugWriter debug) { AirportExtrasDate date = (from d in db.AirportExtrasDates where d.AirportId == airportId && d.PickUpType == (isPickUp ? (int)AirportExtrasDirection.From : (int)AirportExtrasDirection.To) && d.StartDate <= booking.PickUpTime orderby d.StartDate descending select d) .Take(1).FirstOrDefault(); if (date != null) { var extras = db.AirportExtrasDateValues.FirstOrDefault(e => e.DateId == date.Id && e.VehicleId == booking.VehicleTypeId); if (extras != null) return extras.Extras1 + extras.Extras2 + extras.Extras3 + extras.Extras4 + extras.Extras5; } return 0; }
public static FareTypeName[] GetFarePromotions(LomsContext db, Booking booking, DebugWriter debugInfoWriter) { FareTypeName fareTypeNameEFR = RateHelper.GetFarePromotion(db, FareType.EFR, booking, debugInfoWriter); FareTypeName fareTypeNameDSC = RateHelper.GetFarePromotion(db, FareType.DSC, booking, debugInfoWriter); FareTypeName fareTypeNamePRM = RateHelper.GetFarePromotion(db, FareType.PRM, booking, debugInfoWriter); return new FareTypeName[] { fareTypeNameEFR, fareTypeNameDSC, null, fareTypeNamePRM, null }; }
public static CountryFareType[] GetCountryFareTypes(LomsContext db, Booking booking, DebugWriter debugInfoWriter) { int countryId = booking.City.CountryId; CountryFareType typeEFR = db.CountryFareTypes.FirstOrDefault(t => t.CountryId == countryId && t.FareTypeId == (int)FareType.EFR); if (typeEFR == null) { debugInfoWriter.WriteLine("No country fare types!"); return null; } CountryFareType typeDSC = db.CountryFareTypes.FirstOrDefault(t => t.CountryId == countryId && t.FareTypeId == (int)FareType.DSC); CountryFareType typeNET = db.CountryFareTypes.FirstOrDefault(t => t.CountryId == countryId && t.FareTypeId == (int)FareType.NET); CountryFareType typePRM = db.CountryFareTypes.FirstOrDefault(t => t.CountryId == countryId && t.FareTypeId == (int)FareType.PRM); CountryFareType typeCOM = db.CountryFareTypes.FirstOrDefault(t => t.CountryId == countryId && t.FareTypeId == (int)FareType.COM); return new CountryFareType[] { typeEFR, typeDSC, typeNET, typePRM, typeCOM }; }
static Suburb GetSuburb(LomsContext db, BookingEndpoint endpoint, DebugWriter debug) { Suburb suburb = null; switch (endpoint.Type) { case BookingEndpointType.Suburb: suburb = db.Suburbs.FirstOrDefault(s => s.Id == endpoint.SuburbId); break; case BookingEndpointType.AirportPrivate: case BookingEndpointType.AirportOther: { Airport airport = endpoint.Airport; if (airport == null) debug.WriteLine("Airport has been removed from system!"); else suburb = db.Suburbs.FirstOrDefault(s => s.Id == airport.SuburbId); } break; } if (suburb != null) debug.WriteLine("Suburb: {0}", suburb.FullName); else { debug.WriteLine("Suburb has been removed from system!"); return null; } return suburb; }
public static double[] GetFareMargins(LomsContext db, Booking booking, DebugWriter debugInfoWriter) { var date = (from d in db.FareTypeDates where d.CityId == booking.CityId orderby d.StartDate descending select d) .Take(1).FirstOrDefault(); if (date != null) { var cityVehicleType = db.CityVehicleTypes.FirstOrDefault(cvt => cvt.CityId == booking.CityId && cvt.VehicleTypeId == booking.VehicleTypeId); FareTypeDateMargin margin = db.FareTypeDateMargins.FirstOrDefault(m => m.DateId == date.Id && m.CityVehicleId == cityVehicleType.Id); if (margin != null) { debugInfoWriter.WriteLine("City Fare Type Vehicle Margins:"); debugInfoWriter.WriteLine(" Margins: {0:0.00}%, {1:0.00}%, {2:0.00}%, {3:0.00}%, {4:0.00}%", margin.Margin1, margin.Margin2, margin.Margin3, margin.Margin4, margin.Margin5); debugInfoWriter.WriteLine(); return new double[] { margin.Margin1, margin.Margin2, margin.Margin3, margin.Margin4, margin.Margin5 }; } } debugInfoWriter.WriteLine("No Fare Margins"); debugInfoWriter.WriteLine(); return null; }
private void FillBookingWithFareInfo(LomsContext db, Booking booking, DebugWriter debugInfoWriter) { try { decimal price = 0; if (booking.Stops.Count == 0 && booking.JourneyType != JourneyType.AsDirected) { bool autoPending; string eventName; EventVehicleMargin eventMargin = RateHelper.GetEventVehicle(db, booking, out autoPending, out eventName, debugInfoWriter); booking.FareInfo.EventName = eventName; if (!autoPending) { price = (decimal)RateHelper.GetRate(db, booking, debugInfoWriter); if (eventMargin != null) { price = (decimal)((double)price * eventMargin.Margin / 100.0 + eventMargin.Fee); if ((double)price < eventMargin.MinimalRate) price = (decimal)eventMargin.MinimalRate; } } } var association = db.Associations.Single(a => a.Id == CurrentAssociationId); int creatorCountryId; if (booking.CreatorId.HasValue) { var creator = db.AssociationUsers.Single(u => u.Id == booking.CreatorId.Value); creatorCountryId = creator.CountryId; } else { int creatorId = CurrentUserId(); var creator = db.AssociationUsers.Single(u => u.Id == creatorId); booking.CreatorId = creator.Id; creatorCountryId = creator.CountryId; } BookingFareInfoHelper.FillBookingFareInfoAndItems(db, booking, booking.FareInfo, price, association.CurrencyId, debugInfoWriter); } catch (Exception ex) { debugInfoWriter.WriteLine(ex.ToString()); } booking.FareInfo.DebugInfo = debugInfoWriter.ToString(); }
public static bool GetTripTime(LomsContext db, Booking booking, DebugWriter debug, out int time) { time = 30; return true; }
public BookingResponse GetBookingWithFareInfo(int bookingId) { DebugWriter debugInfoWriter = new DebugWriter(); try { DateTime utc = DateTime.UtcNow; using (var db = new LomsContext()) { var booking = GetBooking(db, bookingId); if (!booking.CreatorId.HasValue) { booking.CreatorId = CurrentUserId(); booking.PrimaryPassengerId = CurrentUserId(); } //generate fares if (booking.Status == BookingStatus.InProgress) { if (booking.FareInfo == null) booking.FareInfo = new BookingFareInfo() { BookingId = booking.Id }; //vehicle availability if (VehicleAvailabilityHelper.AllocateVehicle(db, booking, true, debugInfoWriter.GetStringBuilder())) //phantom { booking.Status = BookingStatus.Fare; FillBookingWithFareInfo(db, booking, debugInfoWriter); if (booking.FareInfo.Price == 0) booking.Status = BookingStatus.Pending; utc = DateTime.UtcNow; booking.ExpiryTime = utc.AddMinutes(10.0); } else if (VehicleAvailabilityHelper.AllocateVehicle(db, booking, false, debugInfoWriter.GetStringBuilder())) //pending { booking.Status = BookingStatus.Pending; FillBookingWithFareInfo(db, booking, debugInfoWriter); utc = DateTime.UtcNow; booking.ExpiryTime = utc.AddMinutes(10.0); } else { booking.Status = BookingStatus.NoAvailability; booking.ExpiryTime = null; } } db.Bookings.ApplyChanges(booking); db.SaveChanges(); booking = GetBooking(db, booking.Id); return new BookingResponse { Booking = booking, DebugInfo = debugInfoWriter.ToString(), ServerUtcTime = utc }; } } catch (Exception ex) { var response = new BookingResponse() { DebugInfo = debugInfoWriter.ToString() }; response.AddError("Response", ex.ToString()); return response; } }
public BookingResponse SaveBookingPickUpTimeDropOffTimeAndPromoCode(int bookingId, DateTime pickUptime, DateTime? dropOffTime, string promoCode) { DebugWriter debugInfoWriter = new DebugWriter(); try { using (var db = new LomsContext()) { var booking = db.Bookings.IncludeAll("FareInfo", "FareInfo.Items").FirstOrDefault(b => b.Id == bookingId); if (booking.PickUpTime != pickUptime) { booking.PickUpTime = pickUptime; booking.OnPickUpTimeChanged(); } if (booking.DropOffTime != dropOffTime) { booking.DropOffTime = dropOffTime; booking.OnDropOffTimeChanged(); } if (booking.PromoCode != promoCode) { booking.PromoCode = promoCode; booking.OnPromoCodeChanged(); } db.Bookings.ApplyChanges(booking); db.SaveChanges(); booking = GetBooking(db, booking.Id); return new BookingResponse { Booking = booking, DebugInfo = debugInfoWriter.ToString() }; } } catch (Exception ex) { var response = new BookingResponse() { DebugInfo = debugInfoWriter.ToString() }; response.AddError("Response", ex.ToString()); return response; } }
public PromoCodeResponse ValidatePromoCode(int bookingId, DateTime pickUpTime, string promoCode) { try { using (var db = new LomsContext()) { var booking = GetBooking(db, bookingId); DebugWriter debug = new DebugWriter(); Promotion promotionalCode = RateHelper.GetPromotionalCode(db, booking, pickUpTime, promoCode, debug); if (promotionalCode != null) { //margin = promotionalCode.margin; //bonus = promotionalCode.bonus; return new PromoCodeResponse() { IsValid = true, DebugInfo = debug.ToString() }; } else { //margin = 100.0; //bonus = 0; return new PromoCodeResponse() { IsValid = false, DebugInfo = debug.ToString() }; } } } catch (Exception ex) { var response = new PromoCodeResponse(); response.AddError("Response", ex.ToString()); return response; } }
private static HourZoneMargin GetHourZone(LomsContext db, Booking booking, DebugWriter debug) { int time = (int)booking.PickUpTime.Value.TimeOfDay.TotalMinutes; var zone = (from z in db.HourZones where (z.FromTime <= time && time <= z.EndTime) || (z.FromTime > z.EndTime && ((z.FromTime <= time && time <= 24 * 60) || (0 <= time && time <= z.EndTime))) select z).FirstOrDefault(); if (zone == null) { debug.WriteLine(" Hour zone cannot be found, city: {0}, pick-up time: {1}", booking.City.Name, booking.PickUpTime); debug.WriteLine(); return null; } var margin = (from m in db.HourZoneMargins where m.HourZoneId == zone.Id && m.StartDate <= booking.PickUpTime orderby m.StartDate descending select m).Take(1).FirstOrDefault(); if (margin != null) { debug.WriteLine("Hour Zone - {0}, {1}-{2}:", zone.Name, ToTimeString(zone.FromTime), ToTimeString(zone.EndTime)); debug.WriteLine("Start Date {0}:", margin.StartDate.ToString("dd/MM/yy")); debug.WriteLine(" From Airport: {0:0.00}% + {1:0.00}{2}", margin.MarginFromAirport, margin.FeeFromAirport, ""); debug.WriteLine(" To Airport: {0:0.00}% + {1:0.00}{2}", margin.MarginToAirport, margin.FeeToAirport, ""); debug.WriteLine(" From City: {0:0.00}% + {1:0.00}{2}", margin.MarginFromCity, margin.FeeFromCity, ""); debug.WriteLine(" To City: {0:0.00}% + {1:0.00}{2}", margin.MarginToCity, margin.FeeToCity, ""); debug.WriteLine(" Suburb To Suburb: {0:0.00}% + {1:0.00}{2}", margin.MarginSuburbToSuburb, margin.FeeSuburbToSuburb, ""); debug.WriteLine(" As Directed: {0:0.00}% + {1:0.00}{2}", margin.MarginAsDirected, margin.FeeAsDirected, ""); debug.WriteLine(" Wedding: {0:0.00}% + {1:0.00}{2}", margin.MarginWedding, margin.FeeWedding, ""); debug.WriteLine(" Funeral: {0:0.00}% + {1:0.00}{2}", margin.MarginFuneral, margin.FeeFuneral, ""); debug.WriteLine(" Secure Parcel: {0:0.00}% + {1:0.00}{2}", margin.MarginSecureParcel, margin.FeeSecureParcel, ""); debug.WriteLine(); return margin; } debug.WriteLine("No Hour Zones"); 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; }
//public static void ApplyEventMargin(LomsContext db, Booking booking, DebugWriter debug, ref double price, ref bool autoPending, ref string eventName) //{ // EventVehicleMargin eventMargin = RateHelper.GetEventVehicle(db, booking, ref autoPending, ref eventName, debug); // if (autoPending) // return; // if (eventMargin != null) // { // price = price * eventMargin.Margin / 100.0 + eventMargin.Fee; // if (price < eventMargin.MinimalRate) // price = eventMargin.MinimalRate; // } //} public static EventVehicleMargin GetEventVehicle(LomsContext db, Booking booking, out bool autoPending, out string eventName, DebugWriter debug) { autoPending = false; eventName = null; 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 events = from ev in db.Events where ev.CityId == booking.CityId && ev.StartDate <= pickUpDate && pickUpDate <= ev.EndDate && ev.StartTime <= pickUpTimeOfDayMinutes && pickUpTimeOfDayMinutes <= ev.EndTime select ev; debug.WriteLine(string.Format("Events: {0} found", events.Count())); //from SubDivision subDivisionFrom = GetSubDivision(db, booking, booking.PickUpEndpoint); Division divisionFrom = null; Zone zoneFrom = null; Region regionFrom = null; if (subDivisionFrom.DivisionId.HasValue) { divisionFrom = db.Divisions.FirstOrDefault(d => d.Id == subDivisionFrom.DivisionId.Value); if (divisionFrom.ZoneId.HasValue) { zoneFrom = db.Zones.FirstOrDefault(z => z.Id == divisionFrom.ZoneId.Value); if (zoneFrom.RegionId.HasValue) regionFrom = db.Regions.FirstOrDefault(r => r.Id == zoneFrom.RegionId.Value); } } //to SubDivision subDivisionTo = GetSubDivision(db, booking, booking.DropOffEndpoint); Division divisionTo = null; Zone zoneTo = null; Region regionTo = null; if (subDivisionTo.DivisionId.HasValue) { divisionTo = db.Divisions.FirstOrDefault(d => d.Id == subDivisionTo.DivisionId.Value); if (divisionTo.ZoneId.HasValue) { zoneTo = db.Zones.FirstOrDefault(z => z.Id == divisionTo.ZoneId.Value); if (zoneTo.RegionId.HasValue) regionTo = db.Regions.FirstOrDefault(r => r.Id == zoneTo.RegionId.Value); } } foreach (Event ev in events) { var areas = db.EventAreas.Where(m => m.EventId == ev.Id); bool contains = false; foreach (var area in areas) { if (area.Contains(regionFrom, zoneFrom, divisionFrom, subDivisionFrom) || area.Contains(regionTo, zoneTo, divisionTo, subDivisionTo)) { contains = true; break; } } if (contains) { eventName = ev.Name; if (ev.Autopend) { debug.WriteLine(string.Format("Mathched event: {0}, autopending"), ev.Name); autoPending = true; return null; } var vehicleMargin = db.EventVehicleMargins.FirstOrDefault(m => m.EventId == ev.Id && m.VehicleCityId == cityVehicleType.Id); if (vehicleMargin != null) { debug.WriteLine(string.Format("Mathched event: {0}, vehicle margin={1}, minimal rate={2}, fee={3}", ev.Name, vehicleMargin.Margin, vehicleMargin.MinimalRate, vehicleMargin.Fee)); return vehicleMargin; } } } return null; }
public EntityResponse<BookingFareInfo> ApplyPrice(int bookingId, decimal price) { try { using (var db = new LomsContext()) { var booking = db.Bookings.IncludeAll("City", "FareInfo", "FareInfo.Currency", "FareInfo.Items").Single(b => b.Id == bookingId); var creator = db.AssociationUsers.Single(u => u.Id == booking.CreatorId.Value); //int creatorCountryId = creator.CountryId; var association = db.Associations.Single(a => a.Id == creator.AssociationId); DebugWriter debugInfoWriter = new DebugWriter(); BookingFareInfoHelper.ApplyPriceToBookingFareInfoAndItems(db, booking, booking.FareInfo, price, association.CurrencyId, debugInfoWriter); return new EntityResponse<BookingFareInfo>(booking.FareInfo); } } catch (Exception ex) { StringBuilder builder = new StringBuilder(); builder.AppendLine(ex.Message); if (ex.InnerException != null) { builder.AppendLine(ex.InnerException.Message); if (ex.InnerException.InnerException != null) builder.AppendLine(ex.InnerException.InnerException.Message); } return new EntityResponse<BookingFareInfo>(builder.ToString()); } }
public static void ApplyPriceToBookingFareInfoAndItems(LomsContext db, Booking booking, BookingFareInfo bookingFareInfo, decimal price, byte associationCurrencyId, DebugWriter debugInfoWriter) { bookingFareInfo.Price = price; for (int i = 0; i < 5; i++) { var item = bookingFareInfo.Items[i]; item.Price = (decimal)Math.Round((double)price * item.Margin / 100.0, 4); } ApplyCurrencyBookingFareInfoAndItems(db, booking, bookingFareInfo, associationCurrencyId, debugInfoWriter); ApplyPromoCodeToBookingFareInfoAndItems(db, booking, bookingFareInfo, debugInfoWriter); //rounding bookingFareInfo.Price = booking.City.Round(bookingFareInfo.Price); bookingFareInfo.Items.ForEach(i => i.Price = booking.City.Round(i.Price)); }
public static void ApplyPromoCodeToBookingFareInfoAndItems(LomsContext db, Booking booking, BookingFareInfo bookingFareInfo, DebugWriter debugInfoWriter) { //promo code if (!string.IsNullOrWhiteSpace(booking.PromoCode)) { Promotion promotionalCode = RateHelper.GetPromotionalCode(db, booking, booking.PickUpTime.Value, booking.PromoCode, debugInfoWriter); if (promotionalCode != null) { booking.PromoMargin = promotionalCode.Margin; booking.PromoBonus = promotionalCode.Bonus; } else { booking.PromoMargin = 0; booking.PromoBonus = 0; } bookingFareInfo.Price = bookingFareInfo.Price * (100 - (decimal)booking.PromoMargin) / 100; bookingFareInfo.Items.ForEach(i => i.Price = i.Price * (100 - (decimal)booking.PromoMargin) / 100); } }
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; }
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; }
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; }
public static void ApplyCurrencyBookingFareInfoAndItems(LomsContext db, Booking booking, BookingFareInfo bookingFareInfo, byte associationCurrencyId, DebugWriter debugInfoWriter) { try { ////gateway //var gateway = db.Gateways.FirstOrDefault(g => g.CountryId == creatorCountryId); //var gatewayCountry = db.GatewayCountries.Include("DisplayedCurrency").FirstOrDefault(gc => gc.GatewayId == gateway.Id && gc.CountryId == booking.City.CountryId); //if (gatewayCountry == null) // bookingFareInfo.Currency = db.CurrencyInfoes.Single(c => c.Name == "USD"); //else // bookingFareInfo.Currency = gatewayCountry.DisplayedCurrency; bookingFareInfo.Currency = db.CurrencyInfoes.Single(c => c.Id == associationCurrencyId); ; //currency conversion if (bookingFareInfo.CurrencyId != booking.City.Country.CurrencyId) { var currencyFrom = db.CurrencyInfoes.Single(c => c.Id == booking.City.Country.CurrencyId); var conversionRate = GeoPluginHelper.GetCurrencyConversionRate(currencyFrom.Name, bookingFareInfo.Currency.Name); debugInfoWriter.WriteLine(string.Format("Conversion rate from {0} to {1} is {2}", currencyFrom.Name, bookingFareInfo.Currency.Name, conversionRate)); //fee var conversionFee = (decimal)(db.ConversionFees.Single(f => f.FromId == currencyFrom.Id && f.ToId == bookingFareInfo.Currency.Id).Fee); debugInfoWriter.WriteLine(string.Format("Conversion fee is {0}%", conversionFee)); bookingFareInfo.Price = (bookingFareInfo.Price * conversionRate) * (100 + conversionFee) / 100; bookingFareInfo.Items.ForEach(i => { if (i.Price > 0) i.Price = (i.Price * conversionRate) * (100 + conversionFee) / 100; }); bookingFareInfo.ConversionRate = conversionRate; bookingFareInfo.ConversionFee = conversionFee; } else { bookingFareInfo.ConversionRate = 1; bookingFareInfo.ConversionFee = 0; } bookingFareInfo.Items.ForEach(i => i.CurrencySymbol = bookingFareInfo.Currency.Symbol); } catch (Exception ex) { debugInfoWriter.WriteLine(ex.ToString()); } }