Add attendance log and regularization log models; enhance attendance screen with date range selection and regularization tab
This commit is contained in:
parent
fd14243f5a
commit
7936072b74
@ -1,85 +1,102 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:get/get.dart';
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
import 'package:geolocator/geolocator.dart';
|
import 'package:geolocator/geolocator.dart';
|
||||||
import 'package:get/get.dart';
|
|
||||||
import 'package:marco/helpers/services/api_service.dart';
|
import 'package:marco/helpers/services/api_service.dart';
|
||||||
import 'package:marco/model/attendance_model.dart';
|
import 'package:marco/model/attendance_model.dart';
|
||||||
import 'package:marco/model/project_model.dart'; // Assuming you have a ProjectModel for the projects.
|
import 'package:marco/model/project_model.dart';
|
||||||
import 'package:marco/model/employee_model.dart'; // Assuming you have an EmployeeModel for the employees.
|
import 'package:marco/model/employee_model.dart';
|
||||||
import 'package:marco/model/AttendanceLogModel.dart';
|
import 'package:marco/model/attendance_log_model.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:marco/model/regularization_log_model.dart';
|
||||||
|
import 'package:marco/model/attendance_log_view_model.dart';
|
||||||
|
|
||||||
class AttendanceController extends GetxController {
|
class AttendanceController extends GetxController {
|
||||||
List<AttendanceModel> attendances = [];
|
List<AttendanceModel> attendances = [];
|
||||||
List<ProjectModel> projects = []; // List of projects
|
List<ProjectModel> projects = [];
|
||||||
String? selectedProjectId; // Currently selected project ID
|
String? selectedProjectId;
|
||||||
List<EmployeeModel> employees = []; // Employees of the selected project
|
List<EmployeeModel> employees = [];
|
||||||
|
|
||||||
|
DateTime? startDateAttendance;
|
||||||
|
DateTime? endDateAttendance;
|
||||||
|
|
||||||
|
List<AttendanceLogModel> attendanceLogs = [];
|
||||||
|
List<RegularizationLogModel> regularizationLogs = [];
|
||||||
|
List<AttendanceLogViewModel> attendenceLogsView = [];
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onInit() {
|
void onInit() {
|
||||||
super.onInit();
|
super.onInit();
|
||||||
fetchProjects(); // Fetch projects when initializing
|
_initializeDefaults();
|
||||||
// fetchAttendanceLogs(selectedProjectId);
|
}
|
||||||
// fetchAttendanceLogs(selectedProjectId);
|
|
||||||
|
void _initializeDefaults() {
|
||||||
|
_setDefaultDateRange();
|
||||||
|
fetchProjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _setDefaultDateRange() {
|
||||||
|
final today = DateTime.now();
|
||||||
|
startDateAttendance = today.subtract(const Duration(days: 7));
|
||||||
|
endDateAttendance = today;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch projects from API
|
|
||||||
Future<void> fetchProjects() async {
|
Future<void> fetchProjects() async {
|
||||||
var response = await ApiService.getProjects(); // Call the project API
|
final response = await ApiService.getProjects();
|
||||||
|
|
||||||
if (response != null) {
|
if (response != null && response.isNotEmpty) {
|
||||||
projects = response
|
projects = response.map((json) => ProjectModel.fromJson(json)).toList();
|
||||||
.map<ProjectModel>((json) => ProjectModel.fromJson(json))
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
// Set default to the first project if available
|
|
||||||
if (projects.isNotEmpty) {
|
|
||||||
selectedProjectId = projects.first.id.toString();
|
selectedProjectId = projects.first.id.toString();
|
||||||
await fetchEmployeesByProject(
|
await _fetchProjectData(selectedProjectId);
|
||||||
selectedProjectId); // Fetch employees for the first project
|
update(['attendance_dashboard_controller']);
|
||||||
}
|
|
||||||
|
|
||||||
update([
|
|
||||||
'attendance_dashboard_controller'
|
|
||||||
]); // Notify GetBuilder with your tag
|
|
||||||
} else {
|
} else {
|
||||||
print("No projects data found or failed to fetch data.");
|
print("No projects data found or failed to fetch data.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch employees by project ID
|
Future<void> _fetchProjectData(String? projectId) async {
|
||||||
|
if (projectId == null) return;
|
||||||
|
|
||||||
|
await Future.wait([
|
||||||
|
fetchEmployeesByProject(projectId),
|
||||||
|
fetchAttendanceLogs(projectId,
|
||||||
|
dateFrom: startDateAttendance, dateTo: endDateAttendance),
|
||||||
|
fetchRegularizationLogs(projectId),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> fetchEmployeesByProject(String? projectId) async {
|
Future<void> fetchEmployeesByProject(String? projectId) async {
|
||||||
if (projectId == null) return;
|
if (projectId == null) return;
|
||||||
|
|
||||||
var response = await ApiService.getEmployeesByProject(int.parse(projectId));
|
final response =
|
||||||
|
await ApiService.getEmployeesByProject(int.parse(projectId));
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
employees = response
|
employees = response.map((json) => EmployeeModel.fromJson(json)).toList();
|
||||||
.map<EmployeeModel>((json) => EmployeeModel.fromJson(json))
|
update();
|
||||||
.toList();
|
|
||||||
update(); // Trigger UI rebuild
|
|
||||||
} else {
|
} else {
|
||||||
print("Failed to fetch employees for project $projectId.");
|
print("Failed to fetch employees for project $projectId.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> captureAndUploadAttendance(int employeeId, int projectId,
|
Future<bool> captureAndUploadAttendance(
|
||||||
{String comment = "Marked via mobile app"}) async {
|
int employeeId,
|
||||||
|
int projectId, {
|
||||||
|
String comment = "Marked via mobile app",
|
||||||
|
}) async {
|
||||||
try {
|
try {
|
||||||
final XFile? image = await ImagePicker().pickImage(
|
final image = await ImagePicker().pickImage(
|
||||||
source: ImageSource.camera,
|
source: ImageSource.camera,
|
||||||
imageQuality: 80,
|
imageQuality: 80,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (image == null) return false;
|
if (image == null) return false;
|
||||||
|
|
||||||
final position = await Geolocator.getCurrentPosition(
|
final position = await Geolocator.getCurrentPosition(
|
||||||
desiredAccuracy: LocationAccuracy.high);
|
desiredAccuracy: LocationAccuracy.high,
|
||||||
|
|
||||||
String imageName = ApiService.generateImageName(
|
|
||||||
employeeId,
|
|
||||||
employees.length + 1,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
final imageName =
|
||||||
|
ApiService.generateImageName(employeeId, employees.length + 1);
|
||||||
|
|
||||||
return await ApiService.uploadAttendanceImage(
|
return await ApiService.uploadAttendanceImage(
|
||||||
employeeId,
|
employeeId,
|
||||||
image,
|
image,
|
||||||
@ -90,57 +107,94 @@ class AttendanceController extends GetxController {
|
|||||||
comment: comment,
|
comment: comment,
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Error capturing or uploading: $e");
|
print("Error capturing or uploading attendance: $e");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DateTime? startDate;
|
Future<void> selectDateRangeForAttendance(
|
||||||
DateTime? endDate;
|
BuildContext context,
|
||||||
|
AttendanceController controller,
|
||||||
Future<void> selectDateRange(BuildContext context, AttendanceController controller) async {
|
) async {
|
||||||
final DateTimeRange? picked = await showDateRangePicker(
|
final picked = await showDateRangePicker(
|
||||||
context: context,
|
context: context,
|
||||||
firstDate: DateTime(2022),
|
firstDate: DateTime(2022),
|
||||||
lastDate: DateTime.now(),
|
lastDate: DateTime.now(),
|
||||||
initialDateRange: DateTimeRange(
|
initialDateRange: DateTimeRange(
|
||||||
start: startDate ?? DateTime.now().subtract(Duration(days: 7)),
|
start: startDateAttendance ??
|
||||||
end: endDate ?? DateTime.now(),
|
DateTime.now().subtract(const Duration(days: 7)),
|
||||||
|
end: endDateAttendance ?? DateTime.now(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (picked != null) {
|
if (picked != null) {
|
||||||
startDate = picked.start;
|
startDateAttendance = picked.start;
|
||||||
endDate = picked.end;
|
endDateAttendance = picked.end;
|
||||||
|
|
||||||
await controller.fetchAttendanceLogs(
|
await controller.fetchAttendanceLogs(
|
||||||
controller.selectedProjectId,
|
controller.selectedProjectId,
|
||||||
dateFrom: startDate,
|
dateFrom: picked.start,
|
||||||
dateTo: endDate,
|
dateTo: picked.end,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> fetchAttendanceLogs(
|
||||||
List<AttendanceLogModel> attendanceLogs = [];
|
String? projectId, {
|
||||||
Future<void> fetchAttendanceLogs(String? projectId,
|
DateTime? dateFrom,
|
||||||
{DateTime? dateFrom, DateTime? dateTo}) async {
|
DateTime? dateTo,
|
||||||
|
}) async {
|
||||||
if (projectId == null) return;
|
if (projectId == null) return;
|
||||||
|
|
||||||
var response = await ApiService.getAttendanceLogs(
|
final response = await ApiService.getAttendanceLogs(
|
||||||
int.parse(projectId),
|
int.parse(projectId),
|
||||||
dateFrom: dateFrom,
|
dateFrom: dateFrom,
|
||||||
dateTo: dateTo,
|
dateTo: dateTo,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
attendanceLogs = response
|
attendanceLogs =
|
||||||
.map<AttendanceLogModel>((json) => AttendanceLogModel.fromJson(json))
|
response.map((json) => AttendanceLogModel.fromJson(json)).toList();
|
||||||
.toList();
|
print("Attendance logs fetched: ${response}");
|
||||||
update();
|
update();
|
||||||
} else {
|
} else {
|
||||||
print("Failed to fetch logs for project $projectId.");
|
print("Failed to fetch attendance logs for project $projectId.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> fetchRegularizationLogs(
|
||||||
|
String? projectId, {
|
||||||
|
DateTime? dateFrom,
|
||||||
|
DateTime? dateTo,
|
||||||
|
}) async {
|
||||||
|
if (projectId == null) return;
|
||||||
|
|
||||||
|
final response =
|
||||||
|
await ApiService.getRegularizationLogs(int.parse(projectId));
|
||||||
|
|
||||||
|
if (response != null) {
|
||||||
|
regularizationLogs = response
|
||||||
|
.map((json) => RegularizationLogModel.fromJson(json))
|
||||||
|
.toList();
|
||||||
|
update();
|
||||||
|
} else {
|
||||||
|
print("Failed to fetch regularization logs for project $projectId.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> fetchLogsView(String? id) async {
|
||||||
|
if (id == null) return;
|
||||||
|
|
||||||
|
final response =
|
||||||
|
await ApiService.getAttendanceLogView(int.parse(id));
|
||||||
|
|
||||||
|
if (response != null) {
|
||||||
|
attendenceLogsView = response
|
||||||
|
.map((json) => AttendanceLogViewModel.fromJson(json))
|
||||||
|
.toList();
|
||||||
|
update();
|
||||||
|
} else {
|
||||||
|
print("Failed to fetch regularization logs for project $id.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,147 +4,69 @@ import 'package:image_picker/image_picker.dart';
|
|||||||
import 'package:marco/helpers/services/storage/local_storage.dart';
|
import 'package:marco/helpers/services/storage/local_storage.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
|
|
||||||
|
|
||||||
class ApiService {
|
class ApiService {
|
||||||
static const String baseUrl = "https://api.marcoaiot.com/api";
|
static const String baseUrl = "https://api.marcoaiot.com/api";
|
||||||
|
|
||||||
// Fetch the list of projects
|
// ===== Common Helpers =====
|
||||||
static Future<List<dynamic>?> getProjects() async {
|
|
||||||
try {
|
static Future<String?> _getToken() async {
|
||||||
String? jwtToken = LocalStorage.getJwtToken();
|
final token = LocalStorage.getJwtToken();
|
||||||
if (jwtToken == null) {
|
if (token == null) {
|
||||||
print("No JWT token found. Please log in.");
|
print("No JWT token found. Please log in.");
|
||||||
return null;
|
}
|
||||||
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
final response = await http.get(
|
static Map<String, String> _headers(String token) => {
|
||||||
Uri.parse("$baseUrl/project/list"),
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': 'Bearer $jwtToken', // Add Authorization header
|
'Authorization': 'Bearer $token',
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
final json = jsonDecode(response.body);
|
|
||||||
if (json['success'] == true) {
|
|
||||||
return json['data']; // Return the data if success is true
|
|
||||||
} else {
|
|
||||||
print("Error: ${json['message']}");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
print("Error fetching projects: ${response.statusCode}");
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
print("Exception while fetching projects: $e");
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fetch employees by project ID
|
|
||||||
static Future<List<dynamic>?> getEmployeesByProject(int projectId) async {
|
|
||||||
try {
|
|
||||||
String? jwtToken = LocalStorage.getJwtToken();
|
|
||||||
if (jwtToken == null) {
|
|
||||||
print("No JWT token found. Please log in.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final response = await http.get(
|
|
||||||
Uri.parse(
|
|
||||||
"$baseUrl/attendance/project/team?projectId=$projectId"), // Ensure correct endpoint
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': 'Bearer $jwtToken',
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
final json = jsonDecode(response.body);
|
|
||||||
if (json['success'] == true) {
|
|
||||||
return json['data']; // Return employee data
|
|
||||||
} else {
|
|
||||||
print("Error: ${json['message']}");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
print("Error fetching employees: ${response.statusCode}");
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
print("Exception while fetching employees: $e");
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
static String generateImageName(int employeeId, int count) {
|
|
||||||
final now = DateTime.now();
|
|
||||||
final formattedDate = "${now.year.toString().padLeft(4, '0')}"
|
|
||||||
"${now.month.toString().padLeft(2, '0')}"
|
|
||||||
"${now.day.toString().padLeft(2, '0')}_"
|
|
||||||
"${now.hour.toString().padLeft(2, '0')}"
|
|
||||||
"${now.minute.toString().padLeft(2, '0')}"
|
|
||||||
"${now.second.toString().padLeft(2, '0')}";
|
|
||||||
final imageNumber = count.toString().padLeft(3, '0');
|
|
||||||
return "${employeeId}_${formattedDate}_$imageNumber.jpg";
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future<bool> uploadAttendanceImage(
|
|
||||||
int employeeId, XFile imageFile, double latitude, double longitude,
|
|
||||||
{required String imageName,
|
|
||||||
required int projectId,
|
|
||||||
String comment = "",
|
|
||||||
int action = 0}) async {
|
|
||||||
try {
|
|
||||||
String? jwtToken = LocalStorage.getJwtToken();
|
|
||||||
if (jwtToken == null) {
|
|
||||||
print("No JWT token found. Please log in.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
final bytes = await imageFile.readAsBytes();
|
|
||||||
final base64Image = base64Encode(bytes);
|
|
||||||
final fileSize = await imageFile.length();
|
|
||||||
final contentType = "image/${imageFile.path.split('.').last}";
|
|
||||||
|
|
||||||
final imageObject = {
|
|
||||||
"FileName": imageName,
|
|
||||||
"Base64Data": base64Image,
|
|
||||||
"ContentType": contentType,
|
|
||||||
"FileSize": fileSize,
|
|
||||||
"Description": "Employee attendance photo"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
final now = DateTime.now();
|
static Future<http.Response?> _getRequest(String endpoint,
|
||||||
|
{Map<String, String>? queryParams}) async {
|
||||||
|
final token = await _getToken();
|
||||||
|
if (token == null) return null;
|
||||||
|
|
||||||
// You can now include the attendance record directly in the main body
|
final uri =
|
||||||
final response = await http.post(
|
Uri.parse("$baseUrl$endpoint").replace(queryParameters: queryParams);
|
||||||
Uri.parse("$baseUrl/attendance/record"),
|
print('GET request: $uri');
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
try {
|
||||||
'Authorization': 'Bearer $jwtToken',
|
return await http.get(uri, headers: _headers(token));
|
||||||
},
|
} catch (e) {
|
||||||
body: jsonEncode({
|
print("HTTP GET Exception: $e");
|
||||||
"ID": null,
|
return null;
|
||||||
"employeeId": employeeId,
|
}
|
||||||
"projectId": projectId,
|
}
|
||||||
"markTime": DateFormat('hh:mm a').format(now),
|
|
||||||
"comment": comment,
|
static dynamic _parseResponse(http.Response response, {String label = ''}) {
|
||||||
"action": action,
|
print("$label Response: ${response.body}");
|
||||||
"date": DateFormat('yyyy-MM-dd').format(now),
|
|
||||||
"latitude": latitude,
|
|
||||||
"longitude": longitude,
|
|
||||||
"image": [imageObject], // Directly included in the body
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
print('uploadAttendanceImage: $baseUrl/attendance/record');
|
|
||||||
if (response.statusCode == 200) {
|
if (response.statusCode == 200) {
|
||||||
final json = jsonDecode(response.body);
|
final json = jsonDecode(response.body);
|
||||||
return json['success'] == true;
|
if (json['success'] == true) return json['data'];
|
||||||
|
print("API Error: ${json['message']}");
|
||||||
} else {
|
} else {
|
||||||
print("Error uploading image: ${response.statusCode}");
|
print("HTTP Error [$label]: ${response.statusCode}");
|
||||||
}
|
}
|
||||||
} catch (e) {
|
return null;
|
||||||
print("Exception during image upload: $e");
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
|
// ===== API Calls =====
|
||||||
|
|
||||||
|
static Future<List<dynamic>?> getProjects() async {
|
||||||
|
final response = await _getRequest("/project/list");
|
||||||
|
return response != null
|
||||||
|
? _parseResponse(response, label: 'Projects')
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<List<dynamic>?> getEmployeesByProject(int projectId) async {
|
||||||
|
final response = await _getRequest("/attendance/project/team",
|
||||||
|
queryParams: {"projectId": "$projectId"});
|
||||||
|
return response != null
|
||||||
|
? _parseResponse(response, label: 'Employees')
|
||||||
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<List<dynamic>?> getAttendanceLogs(
|
static Future<List<dynamic>?> getAttendanceLogs(
|
||||||
@ -152,46 +74,101 @@ class ApiService {
|
|||||||
DateTime? dateFrom,
|
DateTime? dateFrom,
|
||||||
DateTime? dateTo,
|
DateTime? dateTo,
|
||||||
}) async {
|
}) async {
|
||||||
try {
|
final query = {
|
||||||
String? jwtToken = LocalStorage.getJwtToken();
|
"projectId": "$projectId",
|
||||||
if (jwtToken == null) {
|
|
||||||
print("No JWT token found. Please log in.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final queryParameters = {
|
|
||||||
"projectId": projectId.toString(),
|
|
||||||
if (dateFrom != null)
|
if (dateFrom != null)
|
||||||
"dateFrom": DateFormat('yyyy-MM-dd').format(dateFrom),
|
"dateFrom": DateFormat('yyyy-MM-dd').format(dateFrom),
|
||||||
if (dateTo != null) "dateTo": DateFormat('yyyy-MM-dd').format(dateTo),
|
if (dateTo != null) "dateTo": DateFormat('yyyy-MM-dd').format(dateTo),
|
||||||
};
|
};
|
||||||
|
|
||||||
final uri = Uri.parse("$baseUrl/attendance/project/team")
|
final response =
|
||||||
.replace(queryParameters: queryParameters);
|
await _getRequest("/attendance/project/team", queryParams: query);
|
||||||
print('uri: $uri');
|
return response != null
|
||||||
final response = await http.get(
|
? _parseResponse(response, label: 'Attendance Logs')
|
||||||
uri,
|
: null;
|
||||||
headers: {
|
}
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'Authorization': 'Bearer $jwtToken',
|
static Future<List<dynamic>?> getAttendanceLogView(int id) async {
|
||||||
},
|
final response = await _getRequest("/attendance/log/attendance/$id");
|
||||||
|
return response != null
|
||||||
|
? _parseResponse(response, label: 'Attendance Log Details')
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<List<dynamic>?> getRegularizationLogs(int projectId) async {
|
||||||
|
final response = await _getRequest("/attendance/regularize",
|
||||||
|
queryParams: {"projectId": "$projectId"});
|
||||||
|
return response != null
|
||||||
|
? _parseResponse(response, label: 'Regularization')
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== Upload Image =====
|
||||||
|
|
||||||
|
static Future<bool> uploadAttendanceImage(
|
||||||
|
int employeeId,
|
||||||
|
XFile imageFile,
|
||||||
|
double latitude,
|
||||||
|
double longitude, {
|
||||||
|
required String imageName,
|
||||||
|
required int projectId,
|
||||||
|
String comment = "",
|
||||||
|
int action = 0,
|
||||||
|
}) async {
|
||||||
|
final token = await _getToken();
|
||||||
|
if (token == null) return false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final bytes = await imageFile.readAsBytes();
|
||||||
|
final base64Image = base64Encode(bytes);
|
||||||
|
final fileSize = await imageFile.length();
|
||||||
|
final contentType = "image/${imageFile.path.split('.').last}";
|
||||||
|
|
||||||
|
final imageObject = {
|
||||||
|
"fileName": '$imageName',
|
||||||
|
"contentType": '$contentType',
|
||||||
|
"fileSize": fileSize,
|
||||||
|
"description": "Employee attendance photo",
|
||||||
|
"base64Data": '$base64Image',
|
||||||
|
};
|
||||||
|
|
||||||
|
final now = DateTime.now();
|
||||||
|
|
||||||
|
final body = {
|
||||||
|
"id": null,
|
||||||
|
"employeeId": employeeId,
|
||||||
|
"projectId": projectId,
|
||||||
|
"markTime": DateFormat('hh:mm a').format(now),
|
||||||
|
"comment": comment,
|
||||||
|
"action": action,
|
||||||
|
"date": DateFormat('yyyy-MM-dd').format(now),
|
||||||
|
"latitude": '$latitude',
|
||||||
|
"longitude": '$longitude',
|
||||||
|
"image": imageObject
|
||||||
|
};
|
||||||
|
print("Upload body Image: $body");
|
||||||
|
final response = await http.post(
|
||||||
|
Uri.parse("$baseUrl/attendance/record-image"),
|
||||||
|
headers: _headers(token),
|
||||||
|
body: jsonEncode(body),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
print('Upload request: $baseUrl/attendance/record-image');
|
||||||
final json = jsonDecode(response.body);
|
final json = jsonDecode(response.body);
|
||||||
print("Response body: ${response.body}");
|
print("Upload Image response: ${response.body}");
|
||||||
if (json['success'] == true) {
|
return response.statusCode == 200 && json['success'] == true;
|
||||||
return json['data'];
|
|
||||||
|
|
||||||
} else {
|
|
||||||
print("Error: ${json['message']}");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
print("Error fetching logs: ${response.statusCode}");
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("Exception while fetching logs: $e");
|
print("Exception during image upload: $e");
|
||||||
}
|
return false;
|
||||||
return null;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== Utilities =====
|
||||||
|
|
||||||
|
static String generateImageName(int employeeId, int count) {
|
||||||
|
final now = DateTime.now();
|
||||||
|
final dateStr = DateFormat('yyyyMMdd_HHmmss').format(now);
|
||||||
|
final imageNumber = count.toString().padLeft(3, '0');
|
||||||
|
return "${employeeId}_${dateStr}_$imageNumber.jpg";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ class AttendanceLogModel {
|
|||||||
final DateTime? checkIn;
|
final DateTime? checkIn;
|
||||||
final DateTime? checkOut;
|
final DateTime? checkOut;
|
||||||
final int activity;
|
final int activity;
|
||||||
|
final int id;
|
||||||
|
|
||||||
AttendanceLogModel({
|
AttendanceLogModel({
|
||||||
required this.name,
|
required this.name,
|
||||||
@ -11,6 +12,7 @@ class AttendanceLogModel {
|
|||||||
this.checkIn,
|
this.checkIn,
|
||||||
this.checkOut,
|
this.checkOut,
|
||||||
required this.activity,
|
required this.activity,
|
||||||
|
required this.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory AttendanceLogModel.fromJson(Map<String, dynamic> json) {
|
factory AttendanceLogModel.fromJson(Map<String, dynamic> json) {
|
||||||
@ -20,6 +22,7 @@ class AttendanceLogModel {
|
|||||||
checkIn: json['checkInTime'] != null ? DateTime.tryParse(json['checkInTime']) : null,
|
checkIn: json['checkInTime'] != null ? DateTime.tryParse(json['checkInTime']) : null,
|
||||||
checkOut: json['checkOutTime'] != null ? DateTime.tryParse(json['checkOutTime']) : null,
|
checkOut: json['checkOutTime'] != null ? DateTime.tryParse(json['checkOutTime']) : null,
|
||||||
activity: json['activity'] ?? 0,
|
activity: json['activity'] ?? 0,
|
||||||
|
id: json['id'] != null ? json['id'] : null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
24
lib/model/attendance_log_view_model.dart
Normal file
24
lib/model/attendance_log_view_model.dart
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import 'package:intl/intl.dart';
|
||||||
|
class AttendanceLogViewModel {
|
||||||
|
final DateTime? activityTime;
|
||||||
|
final String? imageUrl;
|
||||||
|
final String? description;
|
||||||
|
|
||||||
|
AttendanceLogViewModel({
|
||||||
|
this.activityTime,
|
||||||
|
this.imageUrl,
|
||||||
|
this.description,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory AttendanceLogViewModel.fromJson(Map<String, dynamic> json) {
|
||||||
|
return AttendanceLogViewModel(
|
||||||
|
activityTime: json['activityTime'] != null ? DateTime.tryParse(json['activityTime']) : null,
|
||||||
|
imageUrl: json['imageUrl'],
|
||||||
|
description: json['description'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
String? get formattedDate => activityTime != null ? DateFormat('yyyy-MM-dd').format(activityTime!) : null;
|
||||||
|
|
||||||
|
String? get formattedTime => activityTime != null ? DateFormat('hh:mm a').format(activityTime!) : null;
|
||||||
|
}
|
||||||
25
lib/model/regularization_log_model.dart
Normal file
25
lib/model/regularization_log_model.dart
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
class RegularizationLogModel {
|
||||||
|
final String name;
|
||||||
|
final String role;
|
||||||
|
final DateTime? checkIn;
|
||||||
|
final DateTime? checkOut;
|
||||||
|
final int activity;
|
||||||
|
|
||||||
|
RegularizationLogModel({
|
||||||
|
required this.name,
|
||||||
|
required this.role,
|
||||||
|
this.checkIn,
|
||||||
|
this.checkOut,
|
||||||
|
required this.activity,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory RegularizationLogModel.fromJson(Map<String, dynamic> json) {
|
||||||
|
return RegularizationLogModel(
|
||||||
|
name: "${json['firstName'] ?? ''} ${json['lastName'] ?? ''}".trim(),
|
||||||
|
role: json['jobRoleName'] ?? '',
|
||||||
|
checkIn: json['checkInTime'] != null ? DateTime.tryParse(json['checkInTime']) : null,
|
||||||
|
checkOut: json['checkOutTime'] != null ? DateTime.tryParse(json['checkOutTime']) : null,
|
||||||
|
activity: json['activity'] ?? 0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -76,6 +76,8 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
.fetchEmployeesByProject(value);
|
.fetchEmployeesByProject(value);
|
||||||
attendanceController
|
attendanceController
|
||||||
.fetchAttendanceLogs(value);
|
.fetchAttendanceLogs(value);
|
||||||
|
attendanceController
|
||||||
|
.fetchAttendanceLogs(value);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
itemBuilder: (BuildContext context) {
|
itemBuilder: (BuildContext context) {
|
||||||
@ -134,7 +136,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
children: [
|
children: [
|
||||||
MyFlexItem(
|
MyFlexItem(
|
||||||
child: DefaultTabController(
|
child: DefaultTabController(
|
||||||
length: 2,
|
length: 3,
|
||||||
child: MyCard.bordered(
|
child: MyCard.bordered(
|
||||||
borderRadiusAll: 4,
|
borderRadiusAll: 4,
|
||||||
border:
|
border:
|
||||||
@ -154,6 +156,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
tabs: const [
|
tabs: const [
|
||||||
Tab(text: 'Employee List'),
|
Tab(text: 'Employee List'),
|
||||||
Tab(text: 'Logs'),
|
Tab(text: 'Logs'),
|
||||||
|
Tab(text: 'Regularization'),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
MySpacing.height(16),
|
MySpacing.height(16),
|
||||||
@ -163,6 +166,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
children: [
|
children: [
|
||||||
employeeListTab(),
|
employeeListTab(),
|
||||||
reportsTab(context),
|
reportsTab(context),
|
||||||
|
regularizationTab(context),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -235,7 +239,7 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
SnackBar(
|
SnackBar(
|
||||||
content: Text(
|
content: Text(
|
||||||
success
|
success
|
||||||
? 'Image uploaded successfully!'
|
? 'Attendence Marked successfully!'
|
||||||
: 'Image upload failed.',
|
: 'Image upload failed.',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -260,9 +264,9 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
child: TextButton.icon(
|
child: TextButton.icon(
|
||||||
icon: Icon(Icons.date_range),
|
icon: Icon(Icons.date_range),
|
||||||
label: Text("Select Date Range"),
|
label: Text("Select Date Range for Attendance"),
|
||||||
onPressed: () => attendanceController.selectDateRange(context, attendanceController),
|
onPressed: () => attendanceController.selectDateRangeForAttendance(
|
||||||
|
context, attendanceController),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (attendanceController.attendanceLogs.isEmpty)
|
if (attendanceController.attendanceLogs.isEmpty)
|
||||||
@ -308,6 +312,156 @@ class _AttendanceScreenState extends State<AttendanceScreen> with UIMixin {
|
|||||||
color: contentTheme.primary)),
|
color: contentTheme.primary)),
|
||||||
],
|
],
|
||||||
rows: attendanceController.attendanceLogs
|
rows: attendanceController.attendanceLogs
|
||||||
|
.mapIndexed((index, log) => DataRow(cells: [
|
||||||
|
DataCell(
|
||||||
|
MyText.bodyMedium(log.name, fontWeight: 600)),
|
||||||
|
DataCell(
|
||||||
|
MyText.bodyMedium(log.role, fontWeight: 600)),
|
||||||
|
DataCell(MyText.bodyMedium(
|
||||||
|
log.checkIn != null
|
||||||
|
? DateFormat('dd MMM yyyy hh:mm a')
|
||||||
|
.format(log.checkIn!)
|
||||||
|
: '-',
|
||||||
|
fontWeight: 600,
|
||||||
|
)),
|
||||||
|
DataCell(MyText.bodyMedium(
|
||||||
|
log.checkOut != null
|
||||||
|
? DateFormat('dd MMM yyyy hh:mm a')
|
||||||
|
.format(log.checkOut!)
|
||||||
|
: '-',
|
||||||
|
fontWeight: 600,
|
||||||
|
)),
|
||||||
|
DataCell(
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () async {
|
||||||
|
// Call fetchLogsView to load the log data
|
||||||
|
await attendanceController.fetchLogsView(log.id
|
||||||
|
.toString()); // Assuming `log.id` is available
|
||||||
|
// Open the bottom sheet to display the log details
|
||||||
|
showModalBottomSheet(
|
||||||
|
context: context,
|
||||||
|
shape: const RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.vertical(
|
||||||
|
top: Radius.circular(16)),
|
||||||
|
),
|
||||||
|
backgroundColor: theme.cardTheme.color,
|
||||||
|
builder: (context) {
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment:
|
||||||
|
CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
MyText.titleMedium(
|
||||||
|
"Attendance Log Details",
|
||||||
|
fontWeight: 700),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
// Display the log details
|
||||||
|
if (attendanceController
|
||||||
|
.attendenceLogsView.isNotEmpty)
|
||||||
|
...attendanceController
|
||||||
|
.attendenceLogsView
|
||||||
|
.map((log) => Column(
|
||||||
|
crossAxisAlignment:
|
||||||
|
CrossAxisAlignment
|
||||||
|
.start,
|
||||||
|
children: [
|
||||||
|
MyText.bodyMedium(
|
||||||
|
"Date: ${log.formattedDate ?? '-'}",
|
||||||
|
fontWeight: 600,
|
||||||
|
),
|
||||||
|
MyText.bodyMedium(
|
||||||
|
"Time: ${log.formattedTime ?? '-'}",
|
||||||
|
fontWeight: 600,
|
||||||
|
),
|
||||||
|
MyText.bodyMedium(
|
||||||
|
"Description: ${log.description ?? '-'}",
|
||||||
|
fontWeight: 600,
|
||||||
|
),
|
||||||
|
const Divider(
|
||||||
|
thickness: 1,
|
||||||
|
height: 24),
|
||||||
|
],
|
||||||
|
)),
|
||||||
|
Align(
|
||||||
|
alignment: Alignment.centerRight,
|
||||||
|
child: ElevatedButton(
|
||||||
|
onPressed: () =>
|
||||||
|
Navigator.pop(context),
|
||||||
|
child: const Text("Close"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: const Text('View'),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
]))
|
||||||
|
.toList(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Widget regularizationTab(BuildContext context) {
|
||||||
|
final attendanceController = Get.find<AttendanceController>();
|
||||||
|
|
||||||
|
return Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
),
|
||||||
|
if (attendanceController.regularizationLogs.isEmpty)
|
||||||
|
Expanded(
|
||||||
|
child: Center(
|
||||||
|
child: MyText.bodySmall("No Regularization Records Found",
|
||||||
|
fontWeight: 600),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
else
|
||||||
|
Expanded(
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
scrollDirection: Axis.horizontal,
|
||||||
|
child: DataTable(
|
||||||
|
sortAscending: true,
|
||||||
|
columnSpacing: 15,
|
||||||
|
headingRowColor:
|
||||||
|
WidgetStatePropertyAll(contentTheme.primary.withAlpha(40)),
|
||||||
|
dataRowMaxHeight: 60,
|
||||||
|
showBottomBorder: true,
|
||||||
|
clipBehavior: Clip.antiAliasWithSaveLayer,
|
||||||
|
border: TableBorder.all(
|
||||||
|
borderRadius: BorderRadius.circular(4),
|
||||||
|
style: BorderStyle.solid,
|
||||||
|
width: 0.4,
|
||||||
|
color: Colors.grey,
|
||||||
|
),
|
||||||
|
columns: [
|
||||||
|
DataColumn(
|
||||||
|
label: MyText.labelLarge('Name',
|
||||||
|
color: contentTheme.primary)),
|
||||||
|
DataColumn(
|
||||||
|
label: MyText.labelLarge('Role',
|
||||||
|
color: contentTheme.primary)),
|
||||||
|
DataColumn(
|
||||||
|
label: MyText.labelLarge('Check-In',
|
||||||
|
color: contentTheme.primary)),
|
||||||
|
DataColumn(
|
||||||
|
label: MyText.labelLarge('Check-Out',
|
||||||
|
color: contentTheme.primary)),
|
||||||
|
DataColumn(
|
||||||
|
label: MyText.labelLarge('Action',
|
||||||
|
color: contentTheme.primary)),
|
||||||
|
],
|
||||||
|
rows: attendanceController.regularizationLogs
|
||||||
.mapIndexed((index, log) => DataRow(cells: [
|
.mapIndexed((index, log) => DataRow(cells: [
|
||||||
DataCell(
|
DataCell(
|
||||||
MyText.bodyMedium(log.name, fontWeight: 600)),
|
MyText.bodyMedium(log.name, fontWeight: 600)),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user