214 lines
7.5 KiB
C#
214 lines
7.5 KiB
C#
using Microsoft.AspNetCore.Mvc.Infrastructure;
|
|
using Microsoft.AspNetCore.Mvc.Routing;
|
|
using Nop.Core.Domain.Catalog;
|
|
using Nop.Core.Domain.Common;
|
|
using Nop.Core.Domain.Directory;
|
|
using Nop.Core.Domain.Orders;
|
|
using Nop.Core.Domain.Shipping;
|
|
using Nop.Core.Domain.Tax;
|
|
using Nop.Core;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Services;
|
|
using Nop.Services.Affiliates;
|
|
using Nop.Services.Catalog;
|
|
using Nop.Services.Common;
|
|
using Nop.Services.Configuration;
|
|
using Nop.Services.Customers;
|
|
using Nop.Services.Directory;
|
|
using Nop.Services.Discounts;
|
|
using Nop.Services.Helpers;
|
|
using Nop.Services.Localization;
|
|
using Nop.Services.Media;
|
|
using Nop.Services.Orders;
|
|
using Nop.Services.Payments;
|
|
using Nop.Services.Security;
|
|
using Nop.Services.Seo;
|
|
using Nop.Services.Shipping;
|
|
using Nop.Services.Stores;
|
|
using Nop.Services.Tax;
|
|
using Nop.Services.Vendors;
|
|
using Nop.Web.Areas.Admin.Factories;
|
|
using Nop.Web.Areas.Admin.Models.Orders;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Models;
|
|
using System.Reflection;
|
|
using Nop.Plugin.Misc.FruitBankPlugin.Helpers;
|
|
|
|
namespace Nop.Plugin.Misc.FruitBankPlugin.Factories
|
|
{
|
|
public class CustomOrderModelFactory : OrderModelFactory
|
|
{
|
|
private readonly IOrderMeasurementService _orderMeasurementService;
|
|
|
|
public CustomOrderModelFactory(
|
|
IOrderMeasurementService orderMeasurementService,
|
|
AddressSettings addressSettings,
|
|
CatalogSettings catalogSettings,
|
|
CurrencySettings currencySettings,
|
|
IActionContextAccessor actionContextAccessor,
|
|
IAddressModelFactory addressModelFactory,
|
|
IAddressService addressService,
|
|
IAffiliateService affiliateService,
|
|
IBaseAdminModelFactory baseAdminModelFactory,
|
|
ICountryService countryService,
|
|
ICurrencyService currencyService,
|
|
ICustomerService customerService,
|
|
IDateTimeHelper dateTimeHelper,
|
|
IDiscountService discountService,
|
|
IDownloadService downloadService,
|
|
IEncryptionService encryptionService,
|
|
IGiftCardService giftCardService,
|
|
ILocalizationService localizationService,
|
|
IMeasureService measureService,
|
|
IOrderProcessingService orderProcessingService,
|
|
IOrderReportService orderReportService,
|
|
IOrderService orderService,
|
|
IPaymentPluginManager paymentPluginManager,
|
|
IPaymentService paymentService,
|
|
IPictureService pictureService,
|
|
IPriceCalculationService priceCalculationService,
|
|
IPriceFormatter priceFormatter,
|
|
IProductAttributeService productAttributeService,
|
|
IProductService productService,
|
|
IReturnRequestService returnRequestService,
|
|
IRewardPointService rewardPointService,
|
|
ISettingService settingService,
|
|
IShipmentService shipmentService,
|
|
IShippingService shippingService,
|
|
IStateProvinceService stateProvinceService,
|
|
IStoreService storeService,
|
|
ITaxService taxService,
|
|
IUrlHelperFactory urlHelperFactory,
|
|
IVendorService vendorService,
|
|
IWorkContext workContext,
|
|
MeasureSettings measureSettings,
|
|
NopHttpClient nopHttpClient,
|
|
OrderSettings orderSettings,
|
|
ShippingSettings shippingSettings,
|
|
IUrlRecordService urlRecordService,
|
|
TaxSettings taxSettings
|
|
) : base(addressSettings,
|
|
catalogSettings,
|
|
currencySettings,
|
|
actionContextAccessor,
|
|
addressModelFactory,
|
|
addressService,
|
|
affiliateService,
|
|
baseAdminModelFactory,
|
|
countryService,
|
|
currencyService,
|
|
customerService,
|
|
dateTimeHelper,
|
|
discountService,
|
|
downloadService,
|
|
encryptionService,
|
|
giftCardService,
|
|
localizationService,
|
|
measureService,
|
|
orderProcessingService,
|
|
orderReportService,
|
|
orderService,
|
|
paymentPluginManager,
|
|
paymentService,
|
|
pictureService,
|
|
priceCalculationService,
|
|
priceFormatter,
|
|
productAttributeService,
|
|
productService,
|
|
returnRequestService,
|
|
rewardPointService,
|
|
settingService,
|
|
shipmentService,
|
|
shippingService,
|
|
stateProvinceService,
|
|
storeService,
|
|
taxService,
|
|
urlHelperFactory,
|
|
vendorService,
|
|
workContext,
|
|
measureSettings,
|
|
nopHttpClient,
|
|
orderSettings,
|
|
shippingSettings,
|
|
urlRecordService,
|
|
taxSettings
|
|
)
|
|
{
|
|
_orderMeasurementService = orderMeasurementService;
|
|
}
|
|
|
|
public override async Task<OrderSearchModel> PrepareOrderSearchModelAsync(OrderSearchModel searchModel)
|
|
{
|
|
// let base prepare default model first
|
|
var baseModel = await base.PrepareOrderSearchModelAsync(searchModel);
|
|
|
|
// create derived/extended instance
|
|
var extended = new OrderSearchModelExtended();
|
|
|
|
// copy all public instance properties from baseModel to extended
|
|
CopyModelHelper.CopyPublicProperties(baseModel, extended);
|
|
|
|
// try to obtain NeedsMeasurement from the incoming searchModel (if it's extended)
|
|
bool? needsMeasurement = null;
|
|
var prop = searchModel?.GetType().GetProperty("NeedsMeasurement", BindingFlags.Public | BindingFlags.Instance);
|
|
if (prop != null && prop.PropertyType == typeof(bool?))
|
|
{
|
|
needsMeasurement = (bool?)prop.GetValue(searchModel);
|
|
}
|
|
|
|
extended.NeedsMeasurement = needsMeasurement;
|
|
|
|
// return the extended object (it's assignable to OrderSearchModel)
|
|
return extended;
|
|
}
|
|
|
|
public override async Task<OrderListModel> PrepareOrderListModelAsync(OrderSearchModel searchModel)
|
|
{
|
|
// get the default model first
|
|
var baseModel = await base.PrepareOrderListModelAsync(searchModel);
|
|
|
|
|
|
|
|
// project the rows into your extended row type
|
|
var extendedRows = baseModel.Data.Select(async order => new OrderModelExtended
|
|
{
|
|
Id = order.Id,
|
|
CustomOrderNumber = order.CustomOrderNumber,
|
|
OrderStatus = order.OrderStatus,
|
|
PaymentStatus = order.PaymentStatus,
|
|
ShippingStatus = order.ShippingStatus,
|
|
OrderTotal = order.OrderTotal,
|
|
CreatedOn = order.CreatedOn,
|
|
|
|
// custom field
|
|
NeedsMeasurement = await ShouldMarkAsNeedsMeasurementAsync(order)
|
|
}).ToList();
|
|
|
|
// build a new OrderListModel but replace Data with the extended items
|
|
var model = new OrderListModel
|
|
{
|
|
Data = extendedRows.Cast<OrderModel>().ToList(), // still satisfies the grid
|
|
//Total = baseModel.Total
|
|
};
|
|
|
|
return model;
|
|
}
|
|
|
|
// example async custom logic
|
|
private async Task<bool> ShouldMarkAsNeedsMeasurementAsync(OrderModel order)
|
|
{
|
|
// TODO: your logic (e.g. check if order has products that need measuring)
|
|
if (order == null)
|
|
return false;
|
|
|
|
var fullOrder = await _orderService.GetOrderByIdAsync(order.Id);
|
|
if (fullOrder != null)
|
|
{
|
|
return await _orderMeasurementService.IsPendingMeasurementAsync(fullOrder);
|
|
}
|
|
return await Task.FromResult(false);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|