using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Purchase.PaymentDisburse.Cqrs; public record GetBillDetailForPaymentQuery(string BillId) : IRequest; public class GetBillDetailForPaymentHandler : IRequestHandler { private readonly AppDbContext _context; public GetBillDetailForPaymentHandler(AppDbContext context) => _context = context; public async Task Handle(GetBillDetailForPaymentQuery request, CancellationToken cancellationToken) { var bill = await _context.Bill .AsNoTracking() .Include(x => x.PurchaseOrder) .ThenInclude(po => po!.Vendor) .Include(x => x.PurchaseOrder) .ThenInclude(po => po!.PurchaseOrderItemList) .ThenInclude(pi => pi.Product) .FirstOrDefaultAsync(x => x.Id == request.BillId, cancellationToken); if (bill == null) return null; var purchaseOrder = bill.PurchaseOrder; return new GetPaymentDisburseByIdResponse { BillId = bill.Id, BillNumber = bill.AutoNumber, VendorName = purchaseOrder?.Vendor?.Name, VendorStreet = purchaseOrder?.Vendor?.Street, VendorCity = purchaseOrder?.Vendor?.City, PurchaseOrderBeforeTaxAmount = purchaseOrder?.BeforeTaxAmount ?? 0, PurchaseOrderTaxAmount = purchaseOrder?.TaxAmount ?? 0, PurchaseOrderAfterTaxAmount = purchaseOrder?.AfterTaxAmount ?? 0, PaymentAmount = purchaseOrder?.AfterTaxAmount ?? 0, 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() }; } }