Webiant Logo Webiant Logo
  1. No results found.

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

ReturnRequestService.cs

using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Orders;
using Nop.Data;

namespace Nop.Services.Orders;

/// 
/// Return request service
/// 
public partial class ReturnRequestService : IReturnRequestService
{
    #region Fields

    protected readonly IRepository _returnRequestRepository;
    protected readonly IRepository _returnRequestActionRepository;
    protected readonly IRepository _returnRequestReasonRepository;
    protected readonly IRepository _orderItemRepository;
    protected readonly IRepository _productRepository;

    #endregion

    #region Ctor

    public ReturnRequestService(IRepository returnRequestRepository,
        IRepository returnRequestActionRepository,
        IRepository returnRequestReasonRepository,
        IRepository orderItemRepository,
        IRepository productRepository)
    {
        _returnRequestRepository = returnRequestRepository;
        _returnRequestActionRepository = returnRequestActionRepository;
        _returnRequestReasonRepository = returnRequestReasonRepository;
        _orderItemRepository = orderItemRepository;
        _productRepository = productRepository;
    }

    #endregion

    #region Methods

    /// 
    /// Deletes a return request
    /// 
    /// Return request
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteReturnRequestAsync(ReturnRequest returnRequest)
    {
        await _returnRequestRepository.DeleteAsync(returnRequest);
    }

    /// 
    /// Gets a return request
    /// 
    /// Return request identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the return request
    /// 
    public virtual async Task GetReturnRequestByIdAsync(int returnRequestId)
    {
        return await _returnRequestRepository.GetByIdAsync(returnRequestId);
    }

    /// 
    /// Search return requests
    /// 
    /// Store identifier; 0 to load all entries
    /// Customer identifier; 0 to load all entries
    /// Order item identifier; 0 to load all entries
    /// Custom number; null or empty to load all entries
    /// Return request status; null to load all entries
    /// Created date from (UTC); null to load all records
    /// Created date to (UTC); null to load all records
    /// Page index
    /// Page size
    /// A value in indicating whether you want to load only total number of records. Set to "true" if you don't want to load data from database
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the return requests
    /// 
    public virtual async Task> SearchReturnRequestsAsync(int storeId = 0, int customerId = 0,
        int orderItemId = 0, string customNumber = "", ReturnRequestStatus? rs = null, DateTime? createdFromUtc = null,
        DateTime? createdToUtc = null, int pageIndex = 0, int pageSize = int.MaxValue, bool getOnlyTotalCount = false)
    {
        var query = _returnRequestRepository.Table;
        if (storeId > 0)
            query = query.Where(rr => storeId == rr.StoreId);
        if (customerId > 0)
            query = query.Where(rr => customerId == rr.CustomerId);
        if (rs.HasValue)
        {
            var returnStatusId = (int)rs.Value;
            query = query.Where(rr => rr.ReturnRequestStatusId == returnStatusId);
        }

        if (orderItemId > 0)
            query = query.Where(rr => rr.OrderItemId == orderItemId);

        if (!string.IsNullOrEmpty(customNumber))
            query = query.Where(rr => rr.CustomNumber == customNumber);

        if (createdFromUtc.HasValue)
            query = query.Where(rr => createdFromUtc.Value <= rr.CreatedOnUtc);
        if (createdToUtc.HasValue)
            query = query.Where(rr => createdToUtc.Value >= rr.CreatedOnUtc);

        query = query.OrderByDescending(rr => rr.CreatedOnUtc).ThenByDescending(rr => rr.Id);

        var returnRequests = await query.ToPagedListAsync(pageIndex, pageSize, getOnlyTotalCount);

        return returnRequests;
    }

    /// 
    /// Gets the return request availability
    /// 
    /// The order identifier
    /// The  containing the 
    public virtual async Task GetReturnRequestAvailabilityAsync(int orderId)
    {
        var result = new ReturnRequestAvailability();

        if (orderId > 0)
        {
            var cancelledStatusId = (int)ReturnRequestStatus.Cancelled;
            var requestedOrderItemsForReturn =
                from rr in _returnRequestRepository.Table
                where rr.ReturnRequestStatusId != cancelledStatusId
                group rr by new
                {
                    rr.OrderItemId
                } into g
                select new
                {
                    OrderItemId = g.Key.OrderItemId,
                    RequestedQuantityForReturn = g.Sum(rr => rr.Quantity)
                };

            var query =
                from oi in _orderItemRepository.Table
                join roi in requestedOrderItemsForReturn
                    on oi.Id equals roi.OrderItemId into alreadyRequestedForReturn
                from aroi in alreadyRequestedForReturn.DefaultIfEmpty()
                join p in _productRepository.Table
                    on oi.ProductId equals p.Id
                where !p.NotReturnable && oi.OrderId == orderId
                select new ReturnableOrderItem
                {
                    AvailableQuantityForReturn = aroi != null
                        ? Math.Max(oi.Quantity - aroi.RequestedQuantityForReturn, 0)
                        : oi.Quantity,
                    OrderItem = oi
                };

            result.ReturnableOrderItems = await query.ToListAsync();
        }

        return result;
    }

    /// 
    /// Delete a return request action
    /// 
    /// Return request action
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteReturnRequestActionAsync(ReturnRequestAction returnRequestAction)
    {
        await _returnRequestActionRepository.DeleteAsync(returnRequestAction);
    }

    /// 
    /// Gets all return request actions
    /// 
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the return request actions
    /// 
    public virtual async Task> GetAllReturnRequestActionsAsync()
    {
        return await _returnRequestActionRepository.GetAllAsync(query =>
        {
            return from rra in query
                orderby rra.DisplayOrder, rra.Id
                select rra;
        }, cache => default);
    }

    /// 
    /// Gets a return request action
    /// 
    /// Return request action identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the return request action
    /// 
    public virtual async Task GetReturnRequestActionByIdAsync(int returnRequestActionId)
    {
        return await _returnRequestActionRepository.GetByIdAsync(returnRequestActionId, cache => default);
    }

    /// 
    /// Inserts a return request
    /// 
    /// Return request
    /// A task that represents the asynchronous operation
    public virtual async Task InsertReturnRequestAsync(ReturnRequest returnRequest)
    {
        await _returnRequestRepository.InsertAsync(returnRequest);
    }

    /// 
    /// Inserts a return request action
    /// 
    /// Return request action
    /// A task that represents the asynchronous operation
    public virtual async Task InsertReturnRequestActionAsync(ReturnRequestAction returnRequestAction)
    {
        await _returnRequestActionRepository.InsertAsync(returnRequestAction);
    }

    /// 
    /// Updates the return request
    /// 
    /// Return request
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateReturnRequestAsync(ReturnRequest returnRequest)
    {
        await _returnRequestRepository.UpdateAsync(returnRequest);
    }

    /// 
    /// Updates the return request action
    /// 
    /// Return request action
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateReturnRequestActionAsync(ReturnRequestAction returnRequestAction)
    {
        await _returnRequestActionRepository.UpdateAsync(returnRequestAction);
    }

    /// 
    /// Delete a return request reason
    /// 
    /// Return request reason
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteReturnRequestReasonAsync(ReturnRequestReason returnRequestReason)
    {
        await _returnRequestReasonRepository.DeleteAsync(returnRequestReason);
    }

    /// 
    /// Gets all return request reasons
    /// 
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the return request reasons
    /// 
    public virtual async Task> GetAllReturnRequestReasonsAsync()
    {
        return await _returnRequestReasonRepository.GetAllAsync(query =>
        {
            return from rra in query
                orderby rra.DisplayOrder, rra.Id
                select rra;
        }, cache => default);
    }

    /// 
    /// Gets a return request reason
    /// 
    /// Return request reason identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the return request reason
    /// 
    public virtual async Task GetReturnRequestReasonByIdAsync(int returnRequestReasonId)
    {
        return await _returnRequestReasonRepository.GetByIdAsync(returnRequestReasonId, cache => default);
    }

    /// 
    /// Inserts a return request reason
    /// 
    /// Return request reason
    /// A task that represents the asynchronous operation
    public virtual async Task InsertReturnRequestReasonAsync(ReturnRequestReason returnRequestReason)
    {
        await _returnRequestReasonRepository.InsertAsync(returnRequestReason);
    }

    /// 
    /// Updates the  return request reason
    /// 
    /// Return request reason
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateReturnRequestReasonAsync(ReturnRequestReason returnRequestReason)
    {
        await _returnRequestReasonRepository.UpdateAsync(returnRequestReason);
    }

    #endregion
}