using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using Newtonsoft.Json; using PayRollDAL.CustomeCode.YearlyCalendar; using PayRollDAL.DTO.YearlyCalendar; using PayRollDAL.Query.YearlyCalendar; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace PayRollDAL.CustomCode.YearlyCalendar { public class YearlyCalendarDAL : IYearlyCalendarDAL { private readonly IQueryExecutor _queryExecutor; public YearlyCalendarDAL(IQueryExecutor queryExecutor) { _queryExecutor = queryExecutor; } public async Task GetYearlyCalendar( long employeeId, DateTime fromDate, DateTime toDate, CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { var parameters = new { employeeid = employeeId, fromdate = fromDate, todate = toDate }; // ✅ Use EXEC syntax — no commandType needed List results = (await _queryExecutor.QueryAsync( loginDTO, YearlyCalendarQB.GET_YEARLYCALENDAR, parameters)) .ToList(); var completeData = GenerateCompleteCalendar(fromDate, toDate); foreach (var month in completeData) { var actualData = results.FirstOrDefault(a => a.Year == month.Year && a.MonthNumber == month.MonthNumber); if (actualData == null) continue; for (int day = 1; day <= 31; day++) { CopyProperty(actualData, month, $"D{day}"); CopyProperty(actualData, month, $"LeaveId{day}"); CopyProperty(actualData, month, $"LeaveCode{day}"); CopyProperty(actualData, month, $"LeaveName{day}"); CopyProperty(actualData, month, $"LateIn{day}"); // ← NEW CopyProperty(actualData, month, $"EarlyOut{day}"); // ← NEW } } return JsonConvert.SerializeObject(completeData); } catch (Exception ex) { throw new Exception( "Error in YearlyCalendarDAL.GetYearlyCalendar: " + ex.Message, ex); } } private void CopyProperty( YearlyCalendarDTO source, YearlyCalendarDTO target, string propName) { PropertyInfo prop = typeof(YearlyCalendarDTO).GetProperty(propName); if (prop == null) return; object value = prop.GetValue(source); if (value != null) prop.SetValue(target, value); } private List GenerateCompleteCalendar( DateTime fromDate, DateTime toDate) { var result = new List(); for (var date = new DateTime(fromDate.Year, fromDate.Month, 1); date <= toDate; date = date.AddMonths(1)) { result.Add(new YearlyCalendarDTO { Year = date.Year, MonthNumber = date.Month, Month = date.ToString("MMM") }); } return result; } } }