Files
2026-07-21 13:52:43 +07:00

132 lines
6.1 KiB
C#

using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Indotalent.Shared.Utils;
namespace Indotalent.Features.Sales.SalesOrder.Cqrs;
public class SalesOrderItemResponse
{
public string? Id { get; set; }
public string? ProductId { get; set; }
public string? ProductName { get; set; }
public string? Summary { get; set; }
public decimal? UnitPrice { get; set; }
public double? Quantity { get; set; }
public decimal? Total { get; set; }
}
public class GetSalesOrderByIdResponse
{
public string? Id { get; set; }
public string? AutoNumber { get; set; }
public DateTime? OrderDate { get; set; }
public Data.Enums.SalesOrderStatus OrderStatus { get; set; }
public string? Description { get; set; }
public string? CustomerId { get; set; }
public string? CustomerName { get; set; }
public string? CustomerStreet { get; set; }
public string? CustomerCity { get; set; }
public string? CustomerState { get; set; }
public string? CustomerZipCode { get; set; }
public string? CustomerPhone { get; set; }
public string? CustomerEmail { get; set; }
public string? EmployeeId { get; set; }
public string? EmployeeName { get; set; }
public string? SalesOrderGroupId { get; set; }
public string? SalesOrderGroupName { get; set; }
public string? SalesOrderCategoryId { get; set; }
public string? SalesOrderCategoryName { get; set; }
public string? CompanyName { get; set; }
public string? CompanyStreet { get; set; }
public string? CompanyCity { get; set; }
public string? CompanyState { get; set; }
public string? CompanyZipCode { get; set; }
public string? CompanyPhone { get; set; }
public string? CompanyEmail { get; set; }
public string? CurrencySymbol { get; set; }
public string? TaxId { get; set; }
public decimal? BeforeTaxAmount { get; set; }
public decimal? TaxAmount { get; set; }
public decimal? AfterTaxAmount { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public string? CreatedBy { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
public List<SalesOrderItemResponse> Items { get; set; } = new();
}
public record GetSalesOrderByIdQuery(string Id) : IRequest<GetSalesOrderByIdResponse?>;
public class GetSalesOrderByIdHandler : IRequestHandler<GetSalesOrderByIdQuery, GetSalesOrderByIdResponse?>
{
private readonly AppDbContext _context;
public GetSalesOrderByIdHandler(AppDbContext context) => _context = context;
public async Task<GetSalesOrderByIdResponse?> Handle(GetSalesOrderByIdQuery request, CancellationToken cancellationToken)
{
var company = await _context.Company
.AsNoTracking()
.Include(x => x.Currency)
.OrderByDescending(x => x.IsDefault)
.FirstOrDefaultAsync(cancellationToken);
return await _context.SalesOrder
.AsNoTracking()
.Include(x => x.Customer)
.Include(x => x.Employee)
.Include(x => x.SalesOrderGroup)
.Include(x => x.SalesOrderCategory)
.Include(x => x.SalesOrderItemList)
.ThenInclude(x => x.Product)
.Where(x => x.Id == request.Id)
.Select(x => new GetSalesOrderByIdResponse
{
Id = x.Id,
AutoNumber = x.AutoNumber,
OrderDate = x.OrderDate,
OrderStatus = x.OrderStatus,
Description = x.Description,
CustomerId = x.CustomerId,
CustomerName = x.Customer != null ? x.Customer.Name : string.Empty,
CustomerStreet = x.Customer != null ? x.Customer.Street : string.Empty,
CustomerCity = x.Customer != null ? x.Customer.City : string.Empty,
CustomerState = x.Customer != null ? x.Customer.State : string.Empty,
CustomerZipCode = x.Customer != null ? x.Customer.ZipCode : string.Empty,
CustomerPhone = x.Customer != null ? x.Customer.PhoneNumber : string.Empty,
CustomerEmail = x.Customer != null ? x.Customer.EmailAddress : string.Empty,
EmployeeId = x.EmployeeId,
EmployeeName = x.Employee != null ? x.Employee.Name : string.Empty,
SalesOrderGroupId = x.SalesOrderGroupId,
SalesOrderGroupName = x.SalesOrderGroup != null ? x.SalesOrderGroup.Name : string.Empty,
SalesOrderCategoryId = x.SalesOrderCategoryId,
SalesOrderCategoryName = x.SalesOrderCategory != null ? x.SalesOrderCategory.Name : string.Empty,
CompanyName = company != null ? company.Name : string.Empty,
CompanyStreet = company != null ? company.StreetAddress : string.Empty,
CompanyCity = company != null ? company.City : string.Empty,
CompanyState = company != null ? company.StateProvince : string.Empty,
CompanyZipCode = company != null ? company.ZipCode : string.Empty,
CompanyPhone = company != null ? company.Phone : string.Empty,
CompanyEmail = company != null ? company.Email : string.Empty,
CurrencySymbol = company != null && company.Currency != null ? company.Currency.Symbol : "IDR",
TaxId = x.TaxId,
BeforeTaxAmount = x.BeforeTaxAmount,
TaxAmount = x.TaxAmount,
AfterTaxAmount = x.AfterTaxAmount,
CreatedAt = x.CreatedAt,
CreatedBy = x.CreatedBy,
UpdatedAt = x.UpdatedAt,
UpdatedBy = x.UpdatedBy,
Items = x.SalesOrderItemList.Select(i => new SalesOrderItemResponse
{
Id = i.Id,
ProductId = i.ProductId,
ProductName = i.Product != null ? i.Product.Name : string.Empty,
Summary = i.Summary,
UnitPrice = i.UnitPrice,
Quantity = i.Quantity,
Total = i.Total
}).ToList()
}).FirstOrDefaultAsync(cancellationToken);
}
}