using Nop.Core; using Nop.Data; using Nop.Plugin.Tax.Avalara.Domain; namespace Nop.Plugin.Tax.Avalara.Services; /// /// Represents the item classification service implementation /// public class ItemClassificationService { #region Fields protected readonly AvalaraTaxSettings _avalaraTaxSettings; protected readonly IRepository _itemClassificationRepository; #endregion #region Ctor public ItemClassificationService(AvalaraTaxSettings avalaraTaxSettings, IRepository itemClassificationRepository) { _avalaraTaxSettings = avalaraTaxSettings; _itemClassificationRepository = itemClassificationRepository; } #endregion #region Methods /// /// Get item classification /// /// Country identifier /// Product identifier /// Page index /// Page size /// /// A task that represents the asynchronous operation /// The task result contains the paged list of classification items /// public async Task> GetItemClassificationAsync(int? countryId = null, int? productId = null, int pageIndex = 0, int pageSize = int.MaxValue) { //get all items var query = _itemClassificationRepository.Table; //filter by country if (countryId.HasValue && countryId > 0) query = query.Where(item => item.CountryId == countryId); //filter by product if (productId.HasValue) query = query.Where(item => item.ProductId == productId); //order item records query = query.OrderByDescending(item => item.UpdatedOnUtc).ThenByDescending(item => item.Id); return await query.ToPagedListAsync(pageIndex, pageSize); } /// /// Get item classification by the identifier /// /// Item identifier /// /// A task that represents the asynchronous operation /// The task result contains the item classification /// public async Task GetItemClassificationByIdAsync(int itemId) { return await _itemClassificationRepository.GetByIdAsync(itemId); } /// /// Get item classification by the request identifier /// /// Request identifier /// /// A task that represents the asynchronous operation /// The task result contains the item classification /// public async Task GetItemClassificationByRequestIdAsync(string requestId) { if (string.IsNullOrEmpty(requestId)) return null; return await _itemClassificationRepository.Table .FirstOrDefaultAsync(item => requestId.Equals(item.HSClassificationRequestId, StringComparison.InvariantCultureIgnoreCase)); } /// /// Add items for classification /// /// Product identifiers /// A task that represents the asynchronous operation public async Task AddItemClassificationAsync(List productIds) { if (!productIds?.Any() ?? true) return; var newProductIds = productIds.Except(await _itemClassificationRepository.Table.Select(record => record.ProductId).ToListAsync()).ToList(); if (!newProductIds.Any()) return; if (!_avalaraTaxSettings.SelectedCountryIds?.Any() ?? true) return; var records = _avalaraTaxSettings.SelectedCountryIds.SelectMany(countryId => newProductIds.Select(productId => new ItemClassification { CountryId = countryId, ProductId = productId, UpdatedOnUtc = DateTime.UtcNow, })); await _itemClassificationRepository.InsertAsync(records.ToList(), false); } /// /// Update the item classification /// /// Item classification /// A task that represents the asynchronous operation public async Task UpdateItemClassificationAsync(ItemClassification item) { ArgumentNullException.ThrowIfNull(item); await _itemClassificationRepository.UpdateAsync(item, false); } /// /// Delete items /// /// Items identifiers /// A task that represents the asynchronous operation public async Task DeleteItemsAsync(List ids) { await _itemClassificationRepository.DeleteAsync(item => ids.Contains(item.Id)); } /// /// Clear the item classification /// /// A task that represents the asynchronous operation public async Task ClearItemClassificationAsync() { await _itemClassificationRepository.TruncateAsync(); } #endregion }