Esempio n. 1
0
        /// <summary>
        /// Initializes the returns model.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <param name="model">The model.</param>
        /// <returns>OrderReturns.</returns>
        private OrderReturns InitReturnsModel(Order order, OrderReturns model = null)
        {
            model = model ?? new OrderReturns();

            model.OrderId = order.OrderGroupId;
            var rmaLis =
                order.RmaRequests.SelectMany(r => r.RmaReturnItems).SelectMany(r => r.RmaLineItems).ToList();

            if (model.OrderReturnItems.Count == 0)
            {
                foreach (var ori in from li in order.OrderForms.SelectMany(of => of.LineItems)
                                    let item = _catalogClient.GetItem(li.CatalogItemId)
                                    let parentItem = _catalogClient.GetItem(li.ParentCatalogItemId)
                                    where item != null && rmaLis.All(r => r.LineItemId != li.LineItemId)
                                    select new OrderReturnItem(new LineItemModel(li, item, parentItem, order.BillingCurrency)))
                {
                    model.OrderReturnItems.Add(ori);
                }
            }
            else
            {
                foreach (var returnItem in model.OrderReturnItems)
                {
                    var li =
                        order.OrderForms.SelectMany(of => of.LineItems)
                             .First(l => l.LineItemId == returnItem.LineItemId);

                    //Filter already returned items
                    if (rmaLis.Any(r => r.LineItemId == li.LineItemId))
                    {
                        continue;
                    }

                    var item = _catalogClient.GetItem(li.CatalogItemId);
                    var parentItem = _catalogClient.GetItem(li.ParentCatalogItemId);
                    returnItem.LineItemModel = new LineItemModel(li, item, parentItem, order.BillingCurrency);
                }
            }

            //Fill return Reasons
            if (OrderReturns.ReturnReasons == null || OrderReturns.ReturnReasons.Count == 0)
            {
                OrderReturns.ReturnReasons = _settingsClient.GetSettings("ReturnReasons")
                                                            .Select(r => new SelectListItem
                                                            {
                                                                Value = r.ToString(),
                                                                Text = r.ToString()
                                                            }).ToList();
            }

            //Fill address book
            model.Addresses = UserHelper.GetAllCustomerAddresses()
                                        .Select(addr => new SelectListItem
                                        {
                                            Text = addr.ToString(),
                                            Value = addr.AddressId
                                        }).ToList();

            return model;
        }
Esempio n. 2
0
        public ActionResult OrderReturn(OrderReturns model)
        {
            var order = _orderClient.GetCustomerOrder(UserHelper.CustomerSession.CustomerId, model.OrderId);

            if (order == null || String.IsNullOrEmpty(order.CustomerId))
            {
                throw new UnauthorizedAccessException();
            }

            InitReturnsModel(order, model);

            //Filter validation only for selected items
            var selectedReturnItems = new List<OrderReturnItem>();
            for (var i = 0; i < model.OrderReturnItems.Count; i++)
            {
                if (model.OrderReturnItems[i].IsSelected)
                {
                    selectedReturnItems.Add(model.OrderReturnItems[i]);
                    continue;
                }
                var i1 = i;
                foreach (var k in ModelState.Keys.Where(k => k.StartsWith(string.Format("OrderReturnItems[{0}]", i1))).ToArray())
                {
                    ModelState.Remove(k);
                }
            }

            //No items to return
            if (selectedReturnItems.Count == 0)
            {
                ModelState.AddModelError("", @"Select at least one item to return");
            }

            //If Validation passed create RmaReturns
            if (ModelState.IsValid)
            {
                var request = new RmaRequest
                {
                    Comment = model.Comment,
                    OrderId = model.OrderId,
                    Status = RmaRequestStatus.AwaitingStockReturn.ToString(),
                    ReturnFromAddressId = model.ReturnFromAddressId
                };
                request.AuthorizationCode = request.RmaRequestId;

                foreach (var item in selectedReturnItems)
                {
                    var rmali = new RmaLineItem
                    {
                        LineItemId = item.LineItemId,
                        LineItem = order.OrderForms.SelectMany(of => of.LineItems).Single(li => li.LineItemId == item.LineItemId),
                        ReturnQuantity = item.Quantity
                    };

                    var rmaritem = new RmaReturnItem
                    {
                        ItemState = RmaLineItemState.AwaitingReturn.ToString(),
                        ReturnReason = item.ReturnReason,
                        //ReturnAmount = item.LineItemModel.LineItem.ListPrice
                    };

                    rmaritem.RmaLineItems.Add(rmali);
                    request.RmaReturnItems.Add(rmaritem);
                }

                order.RmaRequests.Add(request);

                //Calculate return totals
                _orderService.ExecuteWorkflow("CalculateReturnTotalsWorkflow", order);
                //Save changes
                _orderClient.SaveChanges();

                return RedirectToAction("RmaReturns", new { orderId = model.OrderId });
            }

            return View(model);
        }