public async Task <IActionResult> Edit(string id)
        {
            var merchandiseReturn = await _merchandiseReturnService.GetMerchandiseReturnById(id);

            if (merchandiseReturn == null)
            {
                //No merchandise return found with the specified id
                return(RedirectToAction("List"));
            }

            if (await _groupService.IsStaff(_workContext.CurrentCustomer) && merchandiseReturn.StoreId != _workContext.CurrentCustomer.StaffStoreId)
            {
                return(RedirectToAction("List", "MerchandiseReturn"));
            }

            //a vendor should have access only to his merchandise return
            if (_workContext.CurrentVendor != null && merchandiseReturn.VendorId != _workContext.CurrentVendor.Id)
            {
                return(RedirectToAction("List", "MerchandiseReturn"));
            }

            var model = new MerchandiseReturnModel();
            await _merchandiseReturnViewModelService.PrepareMerchandiseReturnModel(model, merchandiseReturn, false);

            return(View(model));
        }
        protected async Task <Address> PrepareAddress(MerchandiseReturnModel model, IFormCollection form)
        {
            string pickupAddressId = form["pickup_address_id"];
            var    address         = new Address();

            if (_orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)
            {
                if (!string.IsNullOrEmpty(pickupAddressId))
                {
                    address = _workContext.CurrentCustomer.Addresses.FirstOrDefault(a => a.Id == pickupAddressId);
                }
                else
                {
                    var customAttributes = await _mediator.Send(new GetParseCustomAddressAttributes()
                    {
                        Form = form
                    });

                    var addressAttributeParser  = HttpContext.RequestServices.GetRequiredService <IAddressAttributeParser>();
                    var customAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAttributes);

                    foreach (var error in customAttributeWarnings)
                    {
                        ModelState.AddModelError("", error);
                    }
                    await TryUpdateModelAsync(model.NewAddress, "MerchandiseReturnNewAddress");

                    address = model.NewAddress.ToEntity();
                    model.NewAddressPreselected = true;
                    address.Attributes          = customAttributes;
                    address.CreatedOnUtc        = DateTime.UtcNow;
                }
            }
            return(address);
        }
        public async Task <IActionResult> Edit(MerchandiseReturnModel model, bool continueEditing, IFormCollection form,
                                               [FromServices] IAddressAttributeService addressAttributeService,
                                               [FromServices] IAddressAttributeParser addressAttributeParser,
                                               [FromServices] OrderSettings orderSettings
                                               )
        {
            var merchandiseReturn = await _merchandiseReturnService.GetMerchandiseReturnById(model.Id);

            if (merchandiseReturn == null)
            {
                //No merchandise return found with the specified id
                return(RedirectToAction("List"));
            }

            if (await _groupService.IsStaff(_workContext.CurrentCustomer) && merchandiseReturn.StoreId != _workContext.CurrentCustomer.StaffStoreId)
            {
                return(RedirectToAction("List", "MerchandiseReturn"));
            }

            //a vendor should have access only to his merchandise return
            if (_workContext.CurrentVendor != null && merchandiseReturn.VendorId != _workContext.CurrentVendor.Id)
            {
                return(RedirectToAction("List", "MerchandiseReturn"));
            }

            var customAddressAttributes = new List <CustomAttribute>();

            if (orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)
            {
                customAddressAttributes = await form.ParseCustomAddressAttributes(addressAttributeParser, addressAttributeService);

                var customAddressAttributeWarnings = await addressAttributeParser.GetAttributeWarnings(customAddressAttributes);

                foreach (var error in customAddressAttributeWarnings)
                {
                    ModelState.AddModelError("", error);
                }
            }
            if (ModelState.IsValid)
            {
                merchandiseReturn = await _merchandiseReturnViewModelService.UpdateMerchandiseReturnModel(merchandiseReturn, model, customAddressAttributes);

                Success(_translationService.GetResource("Admin.Orders.MerchandiseReturns.Updated"));
                return(continueEditing ? RedirectToAction("Edit", new { id = merchandiseReturn.Id }) : RedirectToAction("List"));
            }

            //If we got this far, something failed, redisplay form
            await _merchandiseReturnViewModelService.PrepareMerchandiseReturnModel(model, merchandiseReturn, false);

            return(View(model));
        }
Exemple #4
0
        public virtual async Task <(IList <MerchandiseReturnModel> merchandiseReturnModels, int totalCount)> PrepareMerchandiseReturnModel(MerchandiseReturnListModel model, int pageIndex, int pageSize)
        {
            string customerId = string.Empty;

            if (!string.IsNullOrEmpty(model.SearchCustomerEmail))
            {
                var customer = await _customerService.GetCustomerByEmail(model.SearchCustomerEmail.ToLowerInvariant());

                if (customer != null)
                {
                    customerId = customer.Id;
                }
                else
                {
                    customerId = "00000000-0000-0000-0000-000000000000";
                }
            }
            DateTime?startDateValue = (model.StartDate == null) ? null
                : (DateTime?)_dateTimeService.ConvertToUtcTime(model.StartDate.Value, _dateTimeService.CurrentTimeZone);

            DateTime?endDateValue = (model.EndDate == null) ? null
                : (DateTime?)_dateTimeService.ConvertToUtcTime(model.EndDate.Value, _dateTimeService.CurrentTimeZone);

            var merchandiseReturns = await _merchandiseReturnService.SearchMerchandiseReturns(model.StoreId,
                                                                                              customerId,
                                                                                              "",
                                                                                              _workContext.CurrentVendor?.Id,
                                                                                              "",
                                                                                              (model.SearchMerchandiseReturnStatusId >= 0 ? (MerchandiseReturnStatus?)model.SearchMerchandiseReturnStatusId : null),
                                                                                              pageIndex - 1,
                                                                                              pageSize,
                                                                                              startDateValue,
                                                                                              endDateValue);

            var merchandiseReturnModels = new List <MerchandiseReturnModel>();

            foreach (var rr in merchandiseReturns)
            {
                var rrmodel = new MerchandiseReturnModel();
                merchandiseReturnModels.Add(await PrepareMerchandiseReturnModel(rrmodel, rr, true));
            }
            return(merchandiseReturnModels, merchandiseReturns.TotalCount);
        }
Exemple #5
0
        public virtual async Task <MerchandiseReturnModel> PrepareMerchandiseReturnModel(MerchandiseReturnModel model,
                                                                                         MerchandiseReturn merchandiseReturn, bool excludeProperties)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (merchandiseReturn == null)
            {
                throw new ArgumentNullException(nameof(merchandiseReturn));
            }

            var order = await _orderService.GetOrderById(merchandiseReturn.OrderId);

            decimal unitPriceInclTaxInCustomerCurrency = 0;

            foreach (var item in merchandiseReturn.MerchandiseReturnItems)
            {
                var orderItem = order.OrderItems.Where(x => x.Id == item.OrderItemId).First();
                unitPriceInclTaxInCustomerCurrency += orderItem.UnitPriceInclTax * item.Quantity;
            }

            model.Total          = _priceFormatter.FormatPrice(unitPriceInclTaxInCustomerCurrency);
            model.Quantity       = merchandiseReturn.MerchandiseReturnItems.Sum(x => x.Quantity);
            model.Id             = merchandiseReturn.Id;
            model.OrderId        = order.Id;
            model.OrderNumber    = order.OrderNumber;
            model.OrderCode      = order.Code;
            model.ReturnNumber   = merchandiseReturn.ReturnNumber;
            model.CustomerId     = merchandiseReturn.CustomerId;
            model.NotifyCustomer = merchandiseReturn.NotifyCustomer;
            var customer = await _customerService.GetCustomerById(merchandiseReturn.CustomerId);

            if (customer != null)
            {
                model.CustomerInfo = !string.IsNullOrEmpty(customer.Email) ? customer.Email : _translationService.GetResource("Admin.Customers.Guest");
            }
            else
            {
                model.CustomerInfo = _translationService.GetResource("Admin.Customers.Guest");
            }

            model.MerchandiseReturnStatusStr = merchandiseReturn.MerchandiseReturnStatus.ToString();
            model.CreatedOn  = _dateTimeService.ConvertToUserTime(merchandiseReturn.CreatedOnUtc, DateTimeKind.Utc);
            model.PickupDate = merchandiseReturn.PickupDate;
            model.UserFields = merchandiseReturn.UserFields;

            if (!excludeProperties)
            {
                var addr = new AddressModel();
                model.PickupAddress = await PrepareAddressModel(addr, merchandiseReturn.PickupAddress, excludeProperties);

                model.CustomerComments          = merchandiseReturn.CustomerComments;
                model.ExternalId                = merchandiseReturn.ExternalId;
                model.StaffNotes                = merchandiseReturn.StaffNotes;
                model.MerchandiseReturnStatusId = merchandiseReturn.MerchandiseReturnStatusId;
            }

            return(model);
        }
Exemple #6
0
        public virtual async Task <MerchandiseReturn> UpdateMerchandiseReturnModel(MerchandiseReturn merchandiseReturn, MerchandiseReturnModel model, List <CustomAttribute> customAddressAttributes)
        {
            merchandiseReturn.CustomerComments          = model.CustomerComments;
            merchandiseReturn.StaffNotes                = model.StaffNotes;
            merchandiseReturn.MerchandiseReturnStatusId = model.MerchandiseReturnStatusId;
            merchandiseReturn.ExternalId                = model.ExternalId;
            merchandiseReturn.UpdatedOnUtc              = DateTime.UtcNow;
            merchandiseReturn.UserFields                = model.UserFields;

            if (_orderSettings.MerchandiseReturns_AllowToSpecifyPickupDate)
            {
                merchandiseReturn.PickupDate = model.PickupDate;
            }
            if (_orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)
            {
                merchandiseReturn.PickupAddress = model.PickupAddress.ToEntity();
                if (merchandiseReturn.PickupAddress != null)
                {
                    merchandiseReturn.PickupAddress.Attributes = customAddressAttributes;
                }
            }
            merchandiseReturn.NotifyCustomer = model.NotifyCustomer;
            await _merchandiseReturnService.UpdateMerchandiseReturn(merchandiseReturn);

            //activity log
            await _customerActivityService.InsertActivity("EditMerchandiseReturn", merchandiseReturn.Id, _translationService.GetResource("ActivityLog.EditMerchandiseReturn"), merchandiseReturn.Id);

            if (model.NotifyCustomer)
            {
                await NotifyCustomer(merchandiseReturn);
            }
            return(merchandiseReturn);
        }
        public virtual async Task <IActionResult> MerchandiseReturnSubmit(string orderId, MerchandiseReturnModel model, IFormCollection form)
        {
            var order = await _orderService.GetOrderById(orderId);

            if (!await order.Access(_workContext.CurrentCustomer, _groupService))
            {
                return(Challenge());
            }

            if (!await _mediator.Send(new IsMerchandiseReturnAllowedQuery()
            {
                Order = order
            }))
            {
                return(RedirectToRoute("HomePage"));
            }

            ModelState.Clear();

            if (_orderSettings.MerchandiseReturns_AllowToSpecifyPickupDate && _orderSettings.MerchandiseReturns_PickupDateRequired && model.PickupDate == null)
            {
                ModelState.AddModelError("", _translationService.GetResource("MerchandiseReturns.PickupDateRequired"));
            }

            var address = await PrepareAddress(model, form);

            if (!ModelState.IsValid && ModelState.ErrorCount > 0)
            {
                var returnmodel = await _mediator.Send(new GetMerchandiseReturn()
                {
                    Order    = order,
                    Language = _workContext.WorkingLanguage,
                    Store    = _workContext.CurrentStore
                });

                returnmodel.Error                 = string.Join(", ", ModelState.Keys.SelectMany(k => ModelState[k].Errors).Select(m => m.ErrorMessage).ToArray());
                returnmodel.Comments              = model.Comments;
                returnmodel.PickupDate            = model.PickupDate;
                returnmodel.NewAddressPreselected = model.NewAddressPreselected;
                returnmodel.NewAddress            = model.NewAddress;
                if (returnmodel.NewAddressPreselected || _orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)
                {
                    await PrepareModelAddress(model.NewAddress, address);
                }
                return(View(returnmodel));
            }
            else
            {
                var result = await _mediator.Send(new MerchandiseReturnSubmitCommand()
                {
                    Address = address, Model = model, Form = form, Order = order
                });

                if (result.rr.ReturnNumber > 0)
                {
                    model.Result      = string.Format(_translationService.GetResource("MerchandiseReturns.Submitted"), result.rr.ReturnNumber, Url.Link("MerchandiseReturnDetails", new { merchandiseReturnId = result.rr.Id }));
                    model.OrderNumber = order.OrderNumber;
                    model.OrderCode   = order.Code;
                    return(View(result.model));
                }

                var returnmodel = await _mediator.Send(new GetMerchandiseReturn()
                {
                    Order    = order,
                    Language = _workContext.WorkingLanguage,
                    Store    = _workContext.CurrentStore
                });

                returnmodel.Error                 = result.model.Error;
                returnmodel.Comments              = model.Comments;
                returnmodel.PickupDate            = model.PickupDate;
                returnmodel.NewAddressPreselected = model.NewAddressPreselected;
                returnmodel.NewAddress            = model.NewAddress;
                if (returnmodel.NewAddressPreselected || _orderSettings.MerchandiseReturns_AllowToSpecifyPickupAddress)
                {
                    await PrepareModelAddress(model.NewAddress, address);
                }
                return(View(returnmodel));
            }
        }