Skip to content

Commit

Permalink
Allow to Vendor to manage return requests
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofPajak committed Mar 30, 2020
1 parent 2ca3093 commit 9377052
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 16 deletions.
5 changes: 5 additions & 0 deletions Grand.Core/Domain/Orders/ReturnRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public ReturnRequest()
/// </summary>
public string CustomerId { get; set; }

/// <summary>
/// Gets or sets the vendor item identifier
/// </summary>
public string VendorId { get; set; }

/// <summary>
/// Gets or sets the customer comments
/// </summary>
Expand Down
17 changes: 15 additions & 2 deletions Grand.Services/Messages/WorkflowMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,11 +1194,11 @@ public virtual async Task<int> SendProductQuestionMessage(Customer customer, str
var toEmail = emailAccount.Email;
var toName = "";

if(!string.IsNullOrEmpty(product?.VendorId))
if (!string.IsNullOrEmpty(product?.VendorId))
{
var vendorService = _serviceProvider.GetRequiredService<IVendorService>();
var vendor = await vendorService.GetVendorById(product.VendorId);
if(vendor!=null)
if (vendor != null)
{
toEmail = vendor.Email;
toName = vendor.Name;
Expand Down Expand Up @@ -1247,6 +1247,19 @@ public virtual async Task<int> SendNewReturnRequestStoreOwnerNotification(Return
//event notification
await _mediator.MessageTokensAdded(messageTemplate, liquidObject);

if (!string.IsNullOrEmpty(returnRequest.VendorId))
{
var vendorService = _serviceProvider.GetRequiredService<IVendorService>();
var vendor = await vendorService.GetVendorById(returnRequest.VendorId);
if (vendor != null)
{
var vendorEmail = vendor.Email;
var vendorName = vendor.Name;
await SendNotification(messageTemplate, emailAccount,
languageId, liquidObject,
vendorEmail, vendorName);
}
}
var toEmail = emailAccount.Email;
var toName = emailAccount.DisplayName;
return await SendNotification(messageTemplate, emailAccount,
Expand Down
3 changes: 2 additions & 1 deletion Grand.Services/Orders/IReturnRequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ public partial interface IReturnRequestService
/// <param name="storeId">Store identifier; 0 to load all entries</param>
/// <param name="customerId">Customer identifier; 0 to load all entries</param>
/// <param name="orderItemId">Order item identifier; 0 to load all entries</param>
/// <param name="vendorId">Vendor identifier</param>
/// <param name="rs">Return request status; null to load all entries</param>
/// <param name="createdFromUtc">Created date from (UTC); null to load all records</param>
/// <param name="createdToUtc">Created date to (UTC); null to load all records</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Return requests</returns>
Task<IPagedList<ReturnRequest>> SearchReturnRequests(string storeId = "", string customerId = "",
string orderItemId = "", ReturnRequestStatus? rs = null,
string orderItemId = "", string vendorId = "", ReturnRequestStatus? rs = null,
int pageIndex = 0, int pageSize = int.MaxValue, DateTime? createdFromUtc = null, DateTime? createdToUtc = null);

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion Grand.Services/Orders/ReturnRequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ public virtual Task<ReturnRequest> GetReturnRequestById(int id)
/// <param name="storeId">Store identifier; 0 to load all entries</param>
/// <param name="customerId">Customer identifier; null to load all entries</param>
/// <param name="orderItemId">Order item identifier; 0 to load all entries</param>
/// <param name="vendorId">Vendor identifier</param>
/// <param name="createdFromUtc">Created date from (UTC); null to load all records</param>
/// <param name="createdToUtc">Created date to (UTC); null to load all records</param>
/// <param name="rs">Return request status; null to load all entries</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>Return requests</returns>
public virtual async Task<IPagedList<ReturnRequest>> SearchReturnRequests(string storeId = "", string customerId = "",
string orderItemId = "", ReturnRequestStatus? rs = null,
string orderItemId = "", string vendorId = "", ReturnRequestStatus? rs = null,
int pageIndex = 0, int pageSize = int.MaxValue, DateTime? createdFromUtc = null, DateTime? createdToUtc = null)
{
var model = new GetReturnRequestQuery() {
Expand All @@ -113,6 +114,7 @@ public virtual async Task<IPagedList<ReturnRequest>> SearchReturnRequests(string
PageIndex = pageIndex,
PageSize = pageSize,
CustomerId = customerId,
VendorId = vendorId,
StoreId = storeId,
OrderItemId = orderItemId,
Rs = rs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public Task<IMongoQueryable<ReturnRequest>> Handle(GetReturnRequestQuery request
if (!string.IsNullOrEmpty(request.CustomerId))
query = query.Where(rr => request.CustomerId == rr.CustomerId);

if (!string.IsNullOrEmpty(request.VendorId))
query = query.Where(rr => request.VendorId == rr.VendorId);

if (request.Rs.HasValue)
{
var returnStatusId = (int)request.Rs.Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class GetReturnRequestQuery : IRequest<IMongoQueryable<ReturnRequest>>
{
public string StoreId { get; set; } = "";
public string CustomerId { get; set; } = "";
public string VendorId { get; set; } = "";
public string OrderItemId { get; set; } = "";
public ReturnRequestStatus? Rs { get; set; } = null;
public int PageIndex { get; set; } = 0;
Expand Down
5 changes: 5 additions & 0 deletions Grand.Web/App_Data/Localization/Upgrade/EN_460_470.nopres.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,10 @@
<LocaleResource Name="Checkout.MinOrderOneProduct">
<Value>You need to have at least one product marked to checkout</Value>
</LocaleResource>
<LocaleResource Name="ReturnRequests.MultiVendorsItems">
<Value>Return request can be associated with only one vendor</Value>
</LocaleResource>


</Language>

3 changes: 3 additions & 0 deletions Grand.Web/App_Data/Localization/defaultResources.grandres.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20303,6 +20303,9 @@
<LocaleResource Name="ReturnRequests.Comments">
<Value>Comments</Value>
</LocaleResource>
<LocaleResource Name="ReturnRequests.MultiVendorsItems">
<Value>Return request can be associated with only one vendor</Value>
</LocaleResource>
<LocaleResource Name="ReturnRequests.NewAddress">
<Value>New Address</Value>
</LocaleResource>
Expand Down
29 changes: 21 additions & 8 deletions Grand.Web/Areas/Admin/Controllers/ReturnRequestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public async Task<IActionResult> Edit(string id)
return RedirectToAction("List", "ReturnRequest");
}

//a vendor should have access only to his return request
if (_workContext.CurrentVendor != null && returnRequest.VendorId != _workContext.CurrentVendor.Id)
return RedirectToAction("List", "ReturnRequest");

var model = new ReturnRequestModel();
await _returnRequestViewModelService.PrepareReturnRequestModel(model, returnRequest, false);
return View(model);
Expand All @@ -157,6 +161,10 @@ [FromServices] OrderSettings orderSettings
return RedirectToAction("List", "ReturnRequest");
}

//a vendor should have access only to his return request
if (_workContext.CurrentVendor != null && returnRequest.VendorId != _workContext.CurrentVendor.Id)
return RedirectToAction("List", "ReturnRequest");

var customAddressAttributes = string.Empty;
if (orderSettings.ReturnRequests_AllowToSpecifyPickupAddress)
{
Expand Down Expand Up @@ -194,6 +202,10 @@ public async Task<IActionResult> Delete(string id)
return RedirectToAction("List", "ReturnRequest");
}

//a vendor can't delete return request
if (_workContext.CurrentVendor != null)
return RedirectToAction("List", "ReturnRequest");

if (ModelState.IsValid)
{
await _returnRequestViewModelService.DeleteReturnRequest(returnRequest);
Expand All @@ -215,14 +227,14 @@ public async Task<IActionResult> ReturnRequestNotesSelect(string returnRequestId
if (returnRequest == null)
throw new ArgumentException("No return request found with the specified id");

//a vendor does not have access to this functionality
if (_workContext.CurrentVendor != null && !_workContext.CurrentCustomer.IsStaff())
return Content("");

if (_workContext.CurrentCustomer.IsStaff() && returnRequest.StoreId != _workContext.CurrentCustomer.StaffStoreId)
{
return Content("");
}
//a vendor should have access only to his return request
if (_workContext.CurrentVendor != null && returnRequest.VendorId != _workContext.CurrentVendor.Id)
return Content("");

//return request notes
var returnRequestNoteModels = await _returnRequestViewModelService.PrepareReturnRequestNotes(returnRequest);
var gridModel = new DataSourceResult {
Expand All @@ -242,14 +254,15 @@ public async Task<IActionResult> ReturnRequestNoteAdd(string returnRequestId, st
if (order == null)
return Json(new { Result = false });

//a vendor does not have access to this functionality
if (_workContext.CurrentVendor != null && !_workContext.CurrentCustomer.IsStaff())
return Json(new { Result = false });

if (_workContext.CurrentCustomer.IsStaff() && returnRequest.StoreId != _workContext.CurrentCustomer.StaffStoreId)
{
return Json(new { Result = false });
}

//a vendor should have access only to his return request
if (_workContext.CurrentVendor != null && returnRequest.VendorId != _workContext.CurrentVendor.Id)
return Json(new { Result = false });

await _returnRequestViewModelService.InsertReturnRequestNote(returnRequest, order, downloadId, displayToCustomer, message);

return Json(new { Result = true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public virtual async Task<ReturnRequestModel> PrepareReturnRequestModel(ReturnRe
var returnRequests = await _returnRequestService.SearchReturnRequests(model.StoreId,
customerId,
"",
_workContext.CurrentVendor?.Id,
(model.SearchReturnRequestStatusId >= 0 ? (ReturnRequestStatus?)model.SearchReturnRequestStatusId : null),
pageIndex - 1,
pageSize,
Expand Down Expand Up @@ -249,7 +250,7 @@ public virtual ReturnReqestListModel PrepareReturnReqestListModel()
public virtual async Task<IList<ReturnRequestModel.ReturnRequestItemModel>> PrepareReturnRequestItemModel(string returnRequestId)
{
var returnRequest = await _returnRequestService.GetReturnRequestById(returnRequestId);
List<ReturnRequestModel.ReturnRequestItemModel> items = new List<ReturnRequestModel.ReturnRequestItemModel>();
var items = new List<ReturnRequestModel.ReturnRequestItemModel>();
var order = await _orderService.GetOrderById(returnRequest.OrderId);

foreach (var item in returnRequest.ReturnRequestItems)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Grand.Web.Models.Orders;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -25,7 +26,7 @@ public class ReturnRequestSubmitCommandHandler : IRequestHandler<ReturnRequestSu
private readonly ILocalizationService _localizationService;
private readonly LocalizationSettings _localizationSettings;


public ReturnRequestSubmitCommandHandler(IWorkContext workContext,
IStoreContext storeContext,
IProductService productService,
Expand Down Expand Up @@ -59,7 +60,7 @@ public ReturnRequestSubmitCommandHandler(IWorkContext workContext,

if (request.Model.PickupDate.HasValue)
rr.PickupDate = request.Model.PickupDate.Value;

var vendors = new List<string>();
foreach (var orderItem in request.Order.OrderItems)
{
var product = await _productService.GetProductById(orderItem.ProductId);
Expand Down Expand Up @@ -97,10 +98,16 @@ public ReturnRequestSubmitCommandHandler(IWorkContext workContext,
Quantity = quantity,
OrderItemId = orderItem.Id
});
rr.VendorId = orderItem.VendorId;
vendors.Add(orderItem.VendorId);
}
}
}

if (vendors.Distinct().Count() > 1)
{
request.Model.Error = _localizationService.GetResource("ReturnRequests.MultiVendorsItems");
return (request.Model, rr);
}
if (rr.ReturnRequestItems.Any())
{
await _returnRequestService.InsertReturnRequest(rr);
Expand Down
6 changes: 6 additions & 0 deletions Grand.Web/Features/Handlers/Orders/GetReturnRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Grand.Services.Seo;
using Grand.Services.Shipping;
using Grand.Services.Stores;
using Grand.Services.Vendors;
using Grand.Web.Features.Models.Common;
using Grand.Web.Features.Models.Orders;
using Grand.Web.Infrastructure.Cache;
Expand All @@ -33,6 +34,7 @@ public class GetReturnRequestHandler : IRequestHandler<GetReturnRequest, ReturnR
private readonly IPriceFormatter _priceFormatter;
private readonly ICountryService _countryService;
private readonly IStoreMappingService _storeMappingService;
private readonly IVendorService _vendorService;
private readonly IMediator _mediator;
private readonly OrderSettings _orderSettings;

Expand All @@ -46,6 +48,7 @@ public GetReturnRequestHandler(
IPriceFormatter priceFormatter,
ICountryService countryService,
IStoreMappingService storeMappingService,
IVendorService vendorService,
IMediator mediator,
OrderSettings orderSettings
)
Expand All @@ -59,6 +62,7 @@ OrderSettings orderSettings
_priceFormatter = priceFormatter;
_countryService = countryService;
_storeMappingService = storeMappingService;
_vendorService = vendorService;
_mediator = mediator;
_orderSettings = orderSettings;
}
Expand Down Expand Up @@ -146,6 +150,8 @@ private async Task PrepareItems(GetReturnRequest request, ReturnRequestModel mod
ProductName = product.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id),
ProductSeName = product.GetSeName(_workContext.WorkingLanguage.Id),
AttributeInfo = orderItem.AttributeDescription,
VendorId = orderItem.VendorId,
VendorName = string.IsNullOrEmpty(orderItem.VendorId) ? "" : (await _vendorService.GetVendorById(orderItem.VendorId))?.Name,
Quantity = qtyDelivery - qtyReturn,
};
if (orderItemModel.Quantity > 0)
Expand Down
3 changes: 3 additions & 0 deletions Grand.Web/Models/Orders/ReturnRequestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public partial class OrderItemModel : BaseGrandEntityModel

public string ProductName { get; set; }

public string VendorId { get; set; }
public string VendorName { get; set; }

public string ProductSeName { get; set; }

public string AttributeInfo { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions Grand.Web/Views/ReturnRequest/ReturnRequest.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
@Html.Raw(item.AttributeInfo)
</div>
}
@if (!String.IsNullOrEmpty(item.VendorName))
{
<div class="vendor">
@item.VendorName
</div>
}
</td>
<td class="unit-price">
@item.UnitPrice
Expand Down

0 comments on commit 9377052

Please sign in to comment.