using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text.Json; using System.Threading.Tasks; public class AttendanceService { private readonly OdooService _odooService; public AttendanceService(OdooService odooService) { _odooService = odooService ?? throw new ArgumentNullException(nameof(odooService)); } /// /// Records attendance for an employee. /// /// The attendance data to record. /// ID of the created attendance record. public async Task RecordAttendanceAsync(object attendanceData) { if (attendanceData == null) { throw new ArgumentNullException(nameof(attendanceData), "Attendance data cannot be null."); } return await _odooService.CreateAsync("hr.attendance", attendanceData); } /// /// Records attendance for an employee. /// /// The attendance data to record. /// ID of the created attendance record. public async Task MarkAsAbsentAsync(object absenceData) { if (absenceData == null) { throw new ArgumentNullException(nameof(absenceData), "Attendance data cannot be null."); } return await _odooService.CreateAsync("hr.attendance", absenceData); } // Method to delete attendance record for a given employee and date public async Task DeleteAttendanceAsync(int employeeId, DateTime date) { // Prepare the filter data to search for the attendance record var filterData = new { domain = new List { new object[] { "employee_id", "=", employeeId }, new object[] { "check_in", ">=", date.Date.ToString("yyyy-MM-dd") + " 00:00:00" }, new object[] { "check_out", "<=", date.Date.ToString("yyyy-MM-dd") + " 23:59:59" } } }; // Search for the attendance record using the Odoo service var attendanceRecords = await _odooService.SearchReadAttendanceAsync("hr.attendance", filterData); if (attendanceRecords == null || !attendanceRecords.Any()) { // No record found to delete return false; } var attendanceRecord = attendanceRecords.FirstOrDefault(); var attendanceId = -1; if (attendanceRecord != null) { // Cast attendanceRecord to JsonElement if it's not already JsonElement recordElement = JsonSerializer.Deserialize(attendanceRecord.ToString()); if (recordElement.ValueKind == JsonValueKind.Object && recordElement.TryGetProperty("id", out var idElement)) { attendanceId = idElement.GetInt32(); // Get the ID as an integer // Now use attendanceId for deletion or further operations } else { // Handle the case where the "id" property is missing or the structure is not as expected throw new Exception("Attendance record does not contain 'id' property or is invalid."); } } else { throw new Exception("No attendance records found."); } // Use the DeleteAsync method to delete the attendance record var success = await _odooService.DeleteAsync("hr.attendance", attendanceId); return success; } /// /// Retrieves attendance records for employees within a specified date range. /// /// The filter criteria for retrieving attendance records. /// A list of attendance records. /// Thrown when filterData is null. public async Task> GetAttendanceAsync(object filterData) { if (filterData == null) { throw new ArgumentNullException(nameof(filterData), "Filter data cannot be null."); } // Use OdooService to perform the search_read request return await _odooService.SearchReadAttendanceAsync("hr.attendance", filterData); } }