using Indotalent.ConfigBackEnd.Exceptions; using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Payroll.PayrollDetails.Cqrs; public class GetPayrollSlipByIdRequest { public string? Id { get; set; } } public class GetPayrollSlipByIdResponse { public string? Id { get; set; } public string? EmployeeName { get; set; } public string? EmployeeCode { get; set; } public string? DesignationName { get; set; } public string? DepartmentName { get; set; } public string? PeriodName { get; set; } public DateTime FromDate { get; set; } public DateTime ToDate { get; set; } public decimal BasicSalary { get; set; } public decimal TotalIncome { get; set; } public decimal TotalDeduction { get; set; } public decimal TakeHomePay { get; set; } public List Incomes { get; set; } = new(); public List Deductions { get; set; } = new(); } public class PayrollSlipComponentResponse { public string? ComponentName { get; set; } public decimal Amount { get; set; } } public record GetPayrollSlipByIdQuery(GetPayrollSlipByIdRequest Data) : IRequest; public class GetPayrollSlipByIdHandler : IRequestHandler { private readonly AppDbContext _context; public GetPayrollSlipByIdHandler(AppDbContext context) => _context = context; public async Task Handle(GetPayrollSlipByIdQuery request, CancellationToken cancellationToken) { var data = await _context.PayrollDetail .Include(x => x.PayrollProcess) .Include(x => x.Components) .Include(x => x.Employee) .ThenInclude(e => e!.Designation) .Include(x => x.Employee) .ThenInclude(e => e!.Department) .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken); if (data == null) { throw new NotFoundException("Payroll Detail", request.Data.Id ?? string.Empty); } var response = new GetPayrollSlipByIdResponse { Id = data.Id, EmployeeName = data.EmployeeName, EmployeeCode = data.EmployeeCode, DesignationName = data.Employee?.Designation?.Name, DepartmentName = data.Employee?.Department?.Name, PeriodName = data.PayrollProcess?.PeriodName, FromDate = data.PayrollProcess?.FromDate ?? DateTime.MinValue, ToDate = data.PayrollProcess?.ToDate ?? DateTime.MinValue, BasicSalary = data.BasicSalary, TotalIncome = data.TotalIncome, TotalDeduction = data.TotalDeduction, TakeHomePay = data.TakeHomePay, Incomes = data.Components .Where(c => c.ComponentType == "Income") .Select(c => new PayrollSlipComponentResponse { ComponentName = c.ComponentName, Amount = c.Amount }).ToList(), Deductions = data.Components .Where(c => c.ComponentType == "Deduction") .Select(c => new PayrollSlipComponentResponse { ComponentName = c.ComponentName, Amount = c.Amount }).ToList() }; return response; } }