public Task <OrderStatusDetail> SwitchOrderToNextDispatchCompany(SwitchOrderToNextDispatchCompanyRequest request)
        {
            var req = string.Format("/account/orders/{0}/switchDispatchCompany", request.OrderId);

            return(Client.PostAsync <OrderStatusDetail>(req, request, logger: Logger));
        }
Exemple #2
0
        public object Post(SwitchOrderToNextDispatchCompanyRequest request)
        {
            _logger.LogMessage("Switching order to another IBS : " + request.ToJson());

            var account           = _accountDao.FindById(new Guid(this.GetSession().UserAuthId));
            var order             = _orderDao.FindById(request.OrderId);
            var orderStatusDetail = _orderDao.FindOrderStatusById(request.OrderId);

            if (orderStatusDetail.Status != OrderStatus.TimedOut)
            {
                // Only switch companies if order is timedout
                return(orderStatusDetail);
            }

            // We are in a network timeout situation.
            if (orderStatusDetail.CompanyKey == request.NextDispatchCompanyKey)
            {
                _ibsCreateOrderService.CancelIbsOrder(order.IBSOrderId, order.CompanyKey, order.Settings.Phone, account.Id);

                orderStatusDetail.IBSStatusId          = VehicleStatuses.Common.Timeout;
                orderStatusDetail.IBSStatusDescription = _resources.Get("OrderStatus_" + VehicleStatuses.Common.Timeout);
                return(orderStatusDetail);
            }

            var market = _taxiHailNetworkServiceClient.GetCompanyMarket(order.PickupAddress.Latitude, order.PickupAddress.Longitude);

            var isConfiguredForCmtPayment = _taxiHailNetworkHelper.FetchCompanyPaymentSettings(request.NextDispatchCompanyKey);

            var chargeTypeId      = order.Settings.ChargeTypeId;
            var chargeTypeDisplay = order.Settings.ChargeType;

            if (!isConfiguredForCmtPayment)
            {
                // Only companies configured for CMT payment can support CoF orders outside of home market
                chargeTypeId      = ChargeTypes.PaymentInCar.Id;
                chargeTypeDisplay = ChargeTypes.PaymentInCar.Display;
            }

            var newOrderRequest = new CreateOrderRequest
            {
                PickupDate     = GetCurrentOffsetedTime(request.NextDispatchCompanyKey),
                PickupAddress  = order.PickupAddress,
                DropOffAddress = order.DropOffAddress,
                Settings       = new BookingSettings
                {
                    LargeBags    = order.Settings.LargeBags,
                    Name         = order.Settings.Name,
                    NumberOfTaxi = order.Settings.NumberOfTaxi,
                    Passengers   = order.Settings.Passengers,
                    Phone        = order.Settings.Phone,
                    ProviderId   = null,

                    ChargeType   = chargeTypeDisplay,
                    ChargeTypeId = chargeTypeId,

                    // Reset vehicle type
                    VehicleType   = null,
                    VehicleTypeId = null
                },
                Note = order.UserNote,
                ClientLanguageCode = account.Language
            };

            var fare = FareHelper.GetFareFromEstimate(new RideEstimate {
                Price = order.EstimatedFare
            });
            var newReferenceData = (ReferenceData)_referenceDataService.Get(new ReferenceDataRequest {
                CompanyKey = request.NextDispatchCompanyKey
            });

            // This must be localized with the priceformat to be localized in the language of the company
            // because it is sent to the driver
            var chargeTypeIbs = _resources.Get(chargeTypeDisplay, _serverSettings.ServerData.PriceFormat);

            var ibsInformationNote = IbsHelper.BuildNote(
                _serverSettings.ServerData.IBS.NoteTemplate,
                chargeTypeIbs,
                order.UserNote,
                order.PickupAddress.BuildingName,
                newOrderRequest.Settings.LargeBags,
                _serverSettings.ServerData.IBS.HideChargeTypeInUserNote);

            var networkErrorMessage = string.Format(_resources.Get("Network_CannotCreateOrder", order.ClientLanguageCode), request.NextDispatchCompanyName);

            int ibsAccountId;

            try
            {
                // Recreate order on next dispatch company IBS
                ibsAccountId = CreateIbsAccountIfNeeded(account, request.NextDispatchCompanyKey);
            }
            catch (Exception ex)
            {
                _logger.LogMessage(networkErrorMessage);
                _logger.LogError(ex);

                throw new HttpError(HttpStatusCode.InternalServerError, networkErrorMessage);
            }

            ValidateProvider(newOrderRequest, newReferenceData, market.HasValue(), null);

            var newOrderCommand = Mapper.Map <CreateOrder>(newOrderRequest);

            newOrderCommand.OrderId = request.OrderId;
            newOrderCommand.ReferenceDataCompanyList = newReferenceData.CompaniesList.ToArray();
            newOrderCommand.Market             = market;
            newOrderCommand.CompanyKey         = request.NextDispatchCompanyKey;
            newOrderCommand.CompanyName        = request.NextDispatchCompanyName;
            newOrderCommand.Fare               = fare;
            newOrderCommand.IbsInformationNote = ibsInformationNote;

            _commandBus.Send(new InitiateIbsOrderSwitch
            {
                NewIbsAccountId = ibsAccountId,
                NewOrderCommand = newOrderCommand
            });

            return(new OrderStatusDetail
            {
                OrderId = request.OrderId,
                Status = OrderStatus.Created,
                CompanyKey = request.NextDispatchCompanyKey,
                CompanyName = request.NextDispatchCompanyName,
                NextDispatchCompanyKey = null,
                NextDispatchCompanyName = null,
                IBSStatusId = string.Empty,
                IBSStatusDescription = string.Format(_resources.Get("OrderStatus_wosWAITINGRoaming", order.ClientLanguageCode), request.NextDispatchCompanyName),
            });
        }