initial commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.CommissionReport.Cqrs;
|
||||
|
||||
public record GetCommissionReportListResponse
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? SalesOrderId { get; init; }
|
||||
public string? SalesOrderNumber { get; init; }
|
||||
public string? CustomerName { get; init; }
|
||||
public string? EmployeeName { get; init; }
|
||||
public decimal? CommissionRate { get; init; }
|
||||
public decimal? CommissionAmount { get; init; }
|
||||
public string? ProductId { get; init; }
|
||||
public string? ProductName { get; init; }
|
||||
public string? ProductNumber { get; init; }
|
||||
public string? Summary { get; init; }
|
||||
public decimal? UnitPrice { get; init; }
|
||||
public double? Quantity { get; init; }
|
||||
public decimal? Total { get; init; }
|
||||
public DateTime? OrderDate { get; init; }
|
||||
}
|
||||
|
||||
public record GetCommissionReportListQuery() : IRequest<List<GetCommissionReportListResponse>>;
|
||||
|
||||
public class GetCommissionReportListHandler : IRequestHandler<GetCommissionReportListQuery, List<GetCommissionReportListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetCommissionReportListHandler(AppDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<List<GetCommissionReportListResponse>> Handle(GetCommissionReportListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var results = await _context.SalesOrderItem
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesOrder)
|
||||
.ThenInclude(x => x!.Customer)
|
||||
.Include(x => x.SalesOrder)
|
||||
.ThenInclude(x => x!.Employee)
|
||||
.Include(x => x.Product)
|
||||
.OrderByDescending(x => x.SalesOrder!.OrderDate)
|
||||
.Select(x => new GetCommissionReportListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
SalesOrderId = x.SalesOrderId,
|
||||
SalesOrderNumber = x.SalesOrder != null ? x.SalesOrder.AutoNumber : string.Empty,
|
||||
CustomerName = x.SalesOrder!.Customer != null ? x.SalesOrder.Customer.Name : string.Empty,
|
||||
EmployeeName = x.SalesOrder!.Employee != null ? x.SalesOrder.Employee.Name : "N/A",
|
||||
CommissionRate = x.SalesOrder!.Employee != null ? x.SalesOrder.Employee.CommissionRate : 0,
|
||||
CommissionAmount = x.SalesOrder!.Employee != null ? (x.Total * x.SalesOrder.Employee.CommissionRate) : 0,
|
||||
ProductId = x.ProductId,
|
||||
ProductName = x.Product != null ? x.Product.Name : string.Empty,
|
||||
ProductNumber = x.Product != null ? x.Product.AutoNumber : string.Empty,
|
||||
Summary = x.Summary,
|
||||
UnitPrice = x.UnitPrice,
|
||||
Quantity = x.Quantity,
|
||||
Total = x.Total,
|
||||
OrderDate = x.SalesOrder != null ? x.SalesOrder.OrderDate : null
|
||||
})
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user