using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Purchase.PaymentDisburse.Cqrs; public class PaymentDisburseItemResponse { 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 GetPaymentDisburseByIdResponse { public string? Id { get; set; } public string? AutoNumber { get; set; } public string? BillId { get; set; } public string? BillNumber { get; set; } public string? Description { get; set; } public DateTime? PaymentDate { get; set; } public string? PaymentMethodId { get; set; } public string? PaymentMethodName { get; set; } public decimal PaymentAmount { get; set; } public Data.Enums.PaymentDisburseStatus Status { get; set; } public string? VendorName { get; set; } public string? VendorStreet { get; set; } public string? VendorCity { get; set; } public string? CompanyName { get; set; } public string? CompanyStreet { get; set; } public string? CompanyCity { get; set; } public string? CurrencySymbol { get; set; } public decimal PurchaseOrderBeforeTaxAmount { get; set; } public decimal PurchaseOrderTaxAmount { get; set; } public decimal PurchaseOrderAfterTaxAmount { get; set; } public DateTimeOffset? CreatedAt { get; set; } public string? CreatedBy { get; set; } public DateTimeOffset? UpdatedAt { get; set; } public string? UpdatedBy { get; set; } public List Items { get; set; } = new(); } public record GetPaymentDisburseByIdQuery(string Id) : IRequest; public class GetPaymentDisburseByIdHandler : IRequestHandler { private readonly AppDbContext _context; public GetPaymentDisburseByIdHandler(AppDbContext context) => _context = context; public async Task Handle(GetPaymentDisburseByIdQuery request, CancellationToken cancellationToken) { var company = await _context.Company .AsNoTracking() .Include(x => x.Currency) .OrderByDescending(x => x.IsDefault) .FirstOrDefaultAsync(cancellationToken); var payment = await _context.PaymentDisburse .AsNoTracking() .Include(x => x.Bill) .ThenInclude(b => b!.PurchaseOrder) .ThenInclude(po => po!.Vendor) .Include(x => x.Bill) .ThenInclude(b => b!.PurchaseOrder) .ThenInclude(po => po!.PurchaseOrderItemList) .ThenInclude(pi => pi.Product) .Include(x => x.PaymentMethod) .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (payment == null) return null; var purchaseOrder = payment.Bill?.PurchaseOrder; return new GetPaymentDisburseByIdResponse { Id = payment.Id, AutoNumber = payment.AutoNumber, BillId = payment.BillId, BillNumber = payment.Bill?.AutoNumber, Description = payment.Description, PaymentDate = payment.PaymentDate, PaymentMethodId = payment.PaymentMethodId, PaymentMethodName = payment.PaymentMethod?.Name, PaymentAmount = payment.PaymentAmount ?? 0, Status = payment.Status, VendorName = purchaseOrder?.Vendor?.Name, VendorStreet = purchaseOrder?.Vendor?.Street, VendorCity = purchaseOrder?.Vendor?.City, CompanyName = company?.Name, CompanyStreet = company?.StreetAddress, CompanyCity = company?.City, CurrencySymbol = company?.Currency?.Symbol ?? "IDR", PurchaseOrderBeforeTaxAmount = purchaseOrder?.BeforeTaxAmount ?? 0, PurchaseOrderTaxAmount = purchaseOrder?.TaxAmount ?? 0, PurchaseOrderAfterTaxAmount = purchaseOrder?.AfterTaxAmount ?? 0, CreatedAt = payment.CreatedAt, CreatedBy = payment.CreatedBy, UpdatedAt = payment.UpdatedAt, UpdatedBy = payment.UpdatedBy, Items = purchaseOrder?.PurchaseOrderItemList.Select(x => new PaymentDisburseItemResponse { Id = x.Id, ProductId = x.ProductId, ProductName = x.Product?.Name, Summary = x.Summary, UnitPrice = x.UnitPrice ?? 0, Quantity = x.Quantity ?? 0, Total = x.Total ?? 0 }).ToList() ?? new() }; } }