using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using Newtonsoft.Json; using PayRollDAL.CustomCode.YearlyCalendar; using PayRollDAL.CustomeCode.YearlyCalendar; using PayRollDAL.DTO.YearlyCalendar; using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; namespace PayRollBLL.YearlyCalendar { public class YearlyCalendarBLL : IYearlyCalendarBLL { private readonly IYearlyCalendarDAL _yearlyCalendarDAL; public YearlyCalendarBLL(IYearlyCalendarDAL yearlyCalendarDAL) { _yearlyCalendarDAL = yearlyCalendarDAL; } public async Task GetYearlyCalendar( long employeeId, DateTime fromDate, DateTime toDate, CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { string jsonData = await _yearlyCalendarDAL.GetYearlyCalendar( employeeId, fromDate, toDate, criteriaDTO, loginDTO); var data = JsonConvert.DeserializeObject>(jsonData) ?? new List(); var outputList = new List>(); foreach (var row in data) { if (row == null) continue; var outputRow = new Dictionary { ["Year"] = row.Year, ["MonthNumber"] = row.MonthNumber, ["Month"] = row.Month }; for (int day = 1; day <= 31; day++) { // D → -1 when null var dayVal = GetValue(row, $"D{day}"); outputRow[$"D{day}"] = dayVal.HasValue ? (object)dayVal.Value : -1; // LeaveId → -1 when null var leaveIdVal = GetValue(row, $"LeaveId{day}"); outputRow[$"LeaveId{day}"] = leaveIdVal.HasValue ? (object)leaveIdVal.Value : -1; // LeaveCode → "NONE" when null outputRow[$"LeaveCode{day}"] = GetValue(row, $"LeaveCode{day}") ?? "NONE"; // LeaveName → "NONE" when null outputRow[$"LeaveName{day}"] = GetValue(row, $"LeaveName{day}") ?? "NONE"; // LateIn → null when no attendance (no default) var lateInVal = GetValue(row, $"LateIn{day}"); outputRow[$"LateIn{day}"] = lateInVal.HasValue ? (object)lateInVal.Value : null; // EarlyOut → null when no attendance (no default) var earlyOutVal = GetValue(row, $"EarlyOut{day}"); outputRow[$"EarlyOut{day}"] = earlyOutVal.HasValue ? (object)earlyOutVal.Value : null; } outputList.Add(outputRow); } return JsonConvert.SerializeObject(outputList); } catch (Exception ex) { throw new Exception("Error in GetYearlyCalendar BLL: " + ex.Message, ex); } } private T GetValue(YearlyCalendarDTO row, string propName) { PropertyInfo prop = typeof(YearlyCalendarDTO).GetProperty(propName); if (prop == null) return default; object value = prop.GetValue(row); if (value == null) return default; return (T)value; } } }