/// <summary> /// Create the order, bookings, guests and associated events /// </summary> /// <param name="businessId">Business to create order for</param> /// <param name="order">Order to create</param> /// <returns>True if successful</returns> public bool CreateOrder(long businessId, OrderModel.Order order) { var isSuccessful = false; if (order.IsValid()) { // validate all the bookings where availability is not ignored foreach (var booking in order.Bookings.Where(b => !b.IsAvailabilityIgnored)) { var searchCriteria = AvailabilitySearchCriteria.GetSearchCriteriaBasedOnBookingDetails(booking, order.CustomerCurrencyCode); searchCriteria.ModifyBookingId = null; var searchResult = availabilityManager.CheckAvailability(searchCriteria); if (searchResult.UnavailabilityReasonCode.HasValue) { throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30116, "OrderManager.CreateOrder")); } if (searchResult.BusinessCandidates != null && searchResult.BusinessCandidates.Count > 0) { var roomType = searchResult.BusinessCandidates[0].RoomTypes.First(rt => rt.RoomTypeId == booking.RoomTypeId); if (roomType != null && roomType.UnAvailabilityReasonCode.HasValue) { throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30116, "OrderManager.CreateOrder")); } } } using (var busTran = new BusinessTransaction()) { if (order.LeadGuest == null && order.Bookings != null && order.Bookings.Any()) { // assign the first booking guest for lead guest if not already set up order.LeadGuest = order.Bookings.First().Guest; } // exception if no guest if (order.LeadGuest == null || order.LeadGuest.TryIsValid() == false) { throw new ValidationException(ErrorFactory.CreateAndLogError(Errors.SRVEX30019, "OrderManager.CreateOrder")); } // if guest is set up, create it if (order.LeadGuest != null) { if (!order.LeadGuest.Id.HasValue) { guestManager.Create(order.LeadGuest); order.LeadGuestId = order.LeadGuest.Id.Value; } else { guestManager.Modify(order.LeadGuest); } } // Create order orderDao.CreateOrder(businessId, order); if (order.Id.HasValue && order.Id != default(int)) { // Create each booking within the order foreach (var booking in order.Bookings) { booking.OrderId = order.Id.Value; if (booking.Guest == null && order.LeadGuest != null) { // set lead guest as booking guest when booking guest not specified booking.Guest = order.LeadGuest; } bookingManager.CreateBooking(booking, order); } eventTrackingManager.CreateOrderEvent(order.Id.Value, OrderEventTypeEnum.OrderCreated, null, null); // Send a confirmation email (confirmed bookings) if (order.Bookings.Exists(b => b.BookingStatus.Code == BookingStatusType.CONFIRMED)) { var cultureCode = order.CustomerCultureCode; var guestMessage = order.GuestMessage; if (string.IsNullOrEmpty(cultureCode)) { var business = Cache.Cache.Business.TryGetValue(businessId); cultureCode = business.CultureCode; } bool isConfirmationEmailToBeSent = order.LeadGuest.IsConfirmationEmailToBeSent; // Get the order again with bookings so all translations are visible and set the custom guest message for email confirmation as its not stored order = GetOrderWithBookingsByKey(order.Id.Value, cultureCode: cultureCode); order.GuestMessage = guestMessage; order.LeadGuest.IsConfirmationEmailToBeSent = isConfirmationEmailToBeSent; SendConfirmationEmails(order); } busTran.Commit(); isSuccessful = true; } } } return isSuccessful; }
/// <summary> /// Get Order with bookings by Id /// </summary> /// <returns>The Order model with bookings related to the id </returns> public OrderModel.Order GetOrderWithBookingsByKey(int id = default(int), string channelReference = "", string orderReference = "", string cultureCode = "", OrderModel.GetOrderWithBookingsByEnum type = OrderModel.GetOrderWithBookingsByEnum.Id) { var order = orderDao.GetOrderWithBookingsByKey(id, channelReference, orderReference, cultureCode, type); order.Bookings.ForEach(b => b.CancellationDetails.CancellationCharge = cancellationPoliciesManager.CalculateCancellationCharge(b, false)); return order; }