Ejemplo n.º 1
0
        public HttpResponseMessage CreateReturn(ReturnInfo returnInfo)
        {
            var returFormCalculator  = ServiceLocator.Current.GetInstance <IReturnFormTotalCalculator>();
            var paymentCreator       = ServiceLocator.Current.GetInstance <ICreditPaymentCreator>();
            var paymentRetriever     = ServiceLocator.Current.GetInstance <IOrderFormPaymentRetriever>();
            var returnOrderValidator = ServiceLocator.Current.GetInstance <IReturnOrderValidator>();

            var order = Get(returnInfo.OrderNumber);

            string[] messages;
            if (!returnOrderValidator.TryValidateReturn(order, returnInfo, out messages))
            {
                return(CreateResponseMessage(HttpStatusCode.NotAcceptable, string.Join(", ", messages)));
            }

            var returnForm = ReturnExchangeManager.AddNewReturnFormToPurchaseOrder(order);

            foreach (var item in returnInfo.ReturnItems)
            {
                var returnableLineItems = ReturnExchangeManager.GetAvailableForReturnLineItems(order.OrderForms[0].Shipments[0]).Where(l => l.CatalogEntryId == item.Sku).ToList();
                ReturnExchangeManager.AddNewReturnLineItemToReturnForm(returnForm, returnableLineItems.FirstOrDefault(), item.Quantity, item.ReturnReason);
                ReturnExchangeManager.AddNewShipmetToReturnForm(returnForm, order.OrderForms[0].Shipments[0]);
            }

            ReturnFormStatusManager.AcknowledgeReceiptItems(returnForm);

            returFormCalculator.AdjustReturnTotal(returnForm);
            paymentCreator.CreateCreditPayment <OtherPayment>(returnForm, order,
                                                              paymentRetriever.GetCapturedPayment(order).PaymentMethodId);
            order.AcceptChanges();
            var respons = CreateResponseMessage(HttpStatusCode.OK, "Return Created");

            return(respons);
        }