Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

WarehouseService.cs

using Nop.Core.Domain.Common;
using Nop.Core.Domain.Shipping;
using Nop.Data;
using Nop.Services.Catalog;
using Nop.Services.Common;

namespace Nop.Services.Shipping;

/// 
/// Warehouse service
/// 
public partial class WarehouseService : IWarehouseService
{
    #region Fields

    protected readonly IAddressService _addressService;
    protected readonly IRepository _warehouseRepository;

    #endregion

    #region Ctor

    public WarehouseService(IAddressService addressService,
        IRepository warehouseRepository)
    {
        _addressService = addressService;
        _warehouseRepository = warehouseRepository;
    }

    #endregion

    #region Methods

    /// 
    /// Deletes a warehouse
    /// 
    /// The warehouse
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteWarehouseAsync(Warehouse warehouse)
    {
        await _warehouseRepository.DeleteAsync(warehouse);
    }

    /// 
    /// Gets a warehouse
    /// 
    /// The warehouse identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the warehouse
    /// 
    public virtual async Task GetWarehouseByIdAsync(int warehouseId)
    {
        return await _warehouseRepository.GetByIdAsync(warehouseId, _ => default);
    }

    /// 
    /// Gets all warehouses
    /// 
    /// Warehouse name
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the warehouses
    /// 
    public virtual async Task> GetAllWarehousesAsync(string name = null)
    {
        var warehouses = await _warehouseRepository.GetAllAsync(query =>
            from wh in query
            orderby wh.Name
            select wh, _ => default);

        if (!string.IsNullOrEmpty(name))
            warehouses = warehouses.Where(wh => wh.Name.Contains(name)).ToList();

        return warehouses;
    }

    /// 
    /// Inserts a warehouse
    /// 
    /// Warehouse
    /// A task that represents the asynchronous operation
    public virtual async Task InsertWarehouseAsync(Warehouse warehouse)
    {
        await _warehouseRepository.InsertAsync(warehouse);
    }

    /// 
    /// Updates the warehouse
    /// 
    /// Warehouse
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateWarehouseAsync(Warehouse warehouse)
    {
        await _warehouseRepository.UpdateAsync(warehouse);
    }

    /// 
    /// Get the nearest warehouse for the specified address
    /// 
    /// Address
    /// List of warehouses, if null all warehouses are used.
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the 
    /// 
    public virtual async Task GetNearestWarehouseAsync(Address address, IList warehouses = null)
    {
        warehouses ??= await GetAllWarehousesAsync();

        //no address specified. return any
        if (address == null)
            return warehouses.FirstOrDefault();

        //of course, we should use some better logic to find nearest warehouse,
        //but we don't have a built-in geographic database which supports "distance" functionality
        //that's why we simply look for exact matches

        //find by country
        var matchedByCountry = new List();
        foreach (var warehouse in warehouses)
        {
            var warehouseAddress = await _addressService.GetAddressByIdAsync(warehouse.AddressId);
            if (warehouseAddress == null)
                continue;

            if (warehouseAddress.CountryId == address.CountryId)
                matchedByCountry.Add(warehouse);
        }
        //no country matches. return any
        if (!matchedByCountry.Any())
            return warehouses.FirstOrDefault();

        //find by state
        var matchedByState = new List();
        foreach (var warehouse in matchedByCountry)
        {
            var warehouseAddress = await _addressService.GetAddressByIdAsync(warehouse.AddressId);
            if (warehouseAddress == null)
                continue;

            if (warehouseAddress.StateProvinceId == address.StateProvinceId)
                matchedByState.Add(warehouse);
        }

        if (matchedByState.Any())
            return matchedByState.FirstOrDefault();

        //no state matches. return any
        return matchedByCountry.FirstOrDefault();
    }

    #endregion
}