import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; import 'package:flutter/cupertino.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:test_sa/extensions/context_extension.dart'; import 'package:test_sa/models/ppm/ppm_attachment.dart'; import 'package:test_sa/models/ppm/ppm_calibration_tools.dart'; import 'package:test_sa/models/ppm/ppm_check_list.dart'; import 'package:test_sa/models/ppm/ppm_contacts.dart'; import 'package:test_sa/models/ppm/ppm_kit.dart'; import 'package:test_sa/models/timer_model.dart'; class VisitTimers { int? id; String? startDateTime; String? endDateTime; double? workingHours; VisitTimers({this.id, this.startDateTime, this.endDateTime, this.workingHours}); VisitTimers.fromJson(Map json) { id = json['id']; startDateTime = json['startDateTime']; endDateTime = json['endDateTime']; workingHours = json['workingHours']; } Map toJson() { final Map data = {}; data['id'] = id; data['startDateTime'] = startDateTime; data['endDateTime'] = endDateTime; data['workingHours'] = workingHours; return data; } } class Ppm { Ppm({ this.id, this.ppmScheduleId, this.assetId, this.jobSheetNo, this.assignedEmployeeId, this.expectedDate, this.actualDate, this.nextDate, this.forwardToId, this.maintenanceContractId, this.typeOfServiceId, this.executionTimeFrameId, this.externalEngineer, this.telephone, this.groupLeaderReviewId, this.timePeriodId, this.assignedToId, this.vCalibrationTools, this.vKits, this.vContacts, this.vChecklists, this.files, this.visitStatusId, this.visitTimers, this.startDate, this.endDate, this.workingHours, this.travelingHours, this.deviceStatusId, this.comments, this.workPerformed, this.supplierId, this.suppPersonId, this.suppStartDate, this.suppEndDate, this.suppWorkingHours, this.taskStatusId, this.engSignature, this.nurseSignature, this.safetyId, this.assetAvailabilityId, this.assetAvailabilityName, this.assetName, this.assetNumber, this.assetSerialNo, this.assetSupplierId, this.assetSupplierName, this.assignedEmployeeName, this.assignedToName, this.contractNumber, this.createdOn, this.departmentId, this.departmentName, this.deviceStatusName, this.dueDate, this.executionTimeFrameName, this.forwardToName, this.groupLeaderReviewName, this.manufacturerId, this.manufacturerName, this.modelId, this.modelName, this.modifiedOn, this.notified, this.planCode, this.planNo, this.ppmId, this.ppmScheduleCode, this.ppmScheduleNo, this.ppmSupplierId, this.ppmSupplierName, this.safetyName, this.siteId, this.siteName, this.buildingName, this.floorName, this.roomName, this.supplierName, this.suppPerson, this.taskStatusName, this.timePeriodName, this.timePeriodValue, this.typeOfServiceName, this.visitCode, this.visitNo, this.visitStatusName, this.warrantyEndDate, }); Ppm.fromJson(dynamic json) { id = json['id']; ppmScheduleId = json['ppmScheduleId']; assetId = json['assetId']; jobSheetNo = json['jobSheetNo']; assignedEmployeeId = json['assignedEmployeeId']; expectedDate = json['expectedDate']; actualDate = json['actualDate']; nextDate = json['nextDate']; forwardToId = json['forwardToId']; maintenanceContractId = json['maintenanceContractId']; typeOfServiceId = json['typeOfServiceId']; executionTimeFrameId = json['executionTimeFrameId']; externalEngineer = json['externalEngineer']; telephone = json['telephone']; groupLeaderReviewId = json['groupLeaderReviewId']; timePeriodId = json['timePeriodId']; assignedToId = json['assignedToId']; if (json['vCalibrationTools'] != null) { vCalibrationTools = []; json['vCalibrationTools'].forEach((v) { vCalibrationTools!.add(PpmCalibrationTools.fromJson(v)); }); } if (json['vKits'] != null) { vKits = []; json['vKits'].forEach((v) { vKits!.add(PpmKits.fromJson(v)); }); } if (json['vContacts'] != null) { vContacts = []; json['vContacts'].forEach((v) { vContacts!.add(PpmContacts.fromJson(v)); }); } if (json['vChecklists'] != null) { vChecklists = []; json['vChecklists'].forEach((v) { vChecklists!.add(PpmChecklists.fromJson(v)); }); } if (json['vAttachments'] != null) { files = []; json['vAttachments'].forEach((v) { files!.add(PpmAttachments.fromJson(v)); }); } visitStatusId = json['visitStatusId']; visitTimers = []; if (json['visitTimers'] != null) { json['visitTimers'].forEach((v) { visitTimers!.add(VisitTimers.fromJson(v)); }); } startDate = json['startDate']; endDate = json['endDate']; workingHours = json['workingHours']; tbsTimer = TimerModel(); // try { // tbsTimer = TimerModel(startAt: DateTime.tryParse(startDate ?? ""), endAt: DateTime.tryParse(endDate ?? "")); // if (tbsTimer.endAt != null && tbsTimer.startAt != null) { // tbsTimer.durationInSecond = (tbsTimer.endAt.difference(tbsTimer.startAt))?.inSeconds; // workingHours = (((tbsTimer.durationInSecond ?? 0) / 60) / 60).toStringAsFixed(2); // } // } catch (e) { // print(e.toString()); // } travelingHours = json['travelingHours']; deviceStatusId = json['deviceStatusId']; comments = json['comments']; workPerformed = json['workPerformed']; supplierId = json['supplierId']; suppPersonId = json['suppPersonId']; suppStartDate = json['suppStartDate']; suppEndDate = json['suppEndDate']; suppWorkingHours = json['suppWorkingHours']; try { externalEngineerTimer = TimerModel(startAt: DateTime.tryParse(suppStartDate ?? ""), endAt: DateTime.tryParse(suppEndDate ?? "")); if (externalEngineerTimer?.endAt != null && externalEngineerTimer?.startAt != null) { externalEngineerTimer?.durationInSecond = (externalEngineerTimer!.endAt!.difference(externalEngineerTimer!.startAt!)).inSeconds; suppWorkingHours = (((externalEngineerTimer!.durationInSecond ?? 0) / 60) / 60).toStringAsFixed(2); } } catch (e) { print(e.toString()); } taskStatusId = json['taskStatusId']; engSignature = json['engSignature']; nurseSignature = json['nurseSignature']; safetyId = json['safetyId']; assetAvailabilityId = json['assetAvailabilityId']; assetAvailabilityName = json['assetAvailabilityName']; assetName = json['assetName']; assetNumber = json['assetNumber']; assetSerialNo = json['assetSerialNo']; assetSupplierId = json['assetSupplierId']; assetSupplierName = json['assetSupplierName']; assignedEmployeeName = json['assignedEmployeeName']; assignedToName = json['assignedToName']; createdOn = json['createdOn']; departmentId = json['departmentId']; departmentName = json['departmentName']; deviceStatusName = json['deviceStatusName']; dueDate = json['dueDate']; executionTimeFrameName = json['executionTimeFrameName']; forwardToName = json['forwardToName']; manufacturerId = json['manufacturerId']; manufacturerName = json['manufacturerName']; modifiedOn = json['modifiedOn']; notified = json['notified']; planCode = json['planCode']; planNo = json['planNo']; ppmId = json['ppmId']; ppmScheduleCode = json['ppmScheduleCode']; ppmScheduleNo = json['ppmScheduleNo']; ppmSupplierId = json['ppmSupplierId']; ppmSupplierName = json['ppmSupplierName']; safetyName = json['safetyName']; siteId = json['siteId']; siteName = json['siteName']; buildingName = json['buildingName']; floorName = json['floorName']; roomName = json['roomName']; supplierName = json['supplierName']; suppPerson = json['suppPerson']; taskStatusName = json['taskStatusName']; timePeriodName = json['timePeriodName']; timePeriodValue = json['timePeriodValue']; typeOfServiceName = json['typeOfServiceName']; visitCode = json['visitCode']; visitNo = json['visitNo']; visitStatusName = json['visitStatusName']; warrantyEndDate = json['warrantyEndDate']; } num? id; num? ppmScheduleId; num? assetId; String? jobSheetNo; String? assignedEmployeeId; String? expectedDate; String? actualDate; String? nextDate; String? forwardToId; num? maintenanceContractId; num? typeOfServiceId; num? executionTimeFrameId; String? externalEngineer; String? telephone; num? groupLeaderReviewId; num? timePeriodId; num? assignedToId; List? vCalibrationTools; List? vKits; List? vContacts; List? vChecklists; List? files; num? visitStatusId; List? visitTimers; String? startDate; String? endDate; String? workingHours; String? travelingHours; num? deviceStatusId; String? comments; String? workPerformed; num? supplierId; num? suppPersonId; String? suppStartDate; String? suppEndDate; String? suppWorkingHours; num? taskStatusId; String? engSignature; String? nurseSignature; num? safetyId; num? assetAvailabilityId; String? assetAvailabilityName; String? assetName; String? assetNumber; String? assetSerialNo; num? assetSupplierId; String? assetSupplierName; String? assignedEmployeeName; String? assignedToName; String? contractNumber; String? createdOn; num? departmentId; String? departmentName; String? deviceStatusName; String? dueDate; String? executionTimeFrameName; String? forwardToName; String? groupLeaderReviewName; num? manufacturerId; String? manufacturerName; num? modelId; String? modelName; String? modifiedOn; bool? notified; String? planCode; num? planNo; num? ppmId; String? ppmScheduleCode; num? ppmScheduleNo; num? ppmSupplierId; String? ppmSupplierName; String? safetyName; num? siteId; String? siteName; String? buildingName; String? floorName; String? roomName; String? supplierName; String? suppPerson; String? taskStatusName; String? timePeriodName; num? timePeriodValue; String? typeOfServiceName; String? visitCode; num? visitNo; String? visitStatusName; String? warrantyEndDate; TimerModel? tbsTimer = TimerModel(); TimerModel? externalEngineerTimer = TimerModel(); Uint8List? localNurseSignature; Uint8List? localEngineerSignature; Ppm copyWith({ num? id, num? ppmScheduleId, num? assetId, String? jobSheetNo, String? assignedEmployeeId, String? expectedDate, String? actualDate, String? nextDate, String? forwardToId, num? maintenanceContractId, num? typeOfServiceId, num? executionTimeFrameId, String? externalEngineer, String? telephone, num? groupLeaderReviewId, num? timePeriodId, num? assignedToId, List? vCalibrationTools, List? vKits, List? vContacts, List? vChecklists, List? files, num? visitStatusId, List? visitTimers, String? startDate, String? endDate, String? workingHours, String? travelingHours, num? deviceStatusId, String? comments, String? workPerformed, num? supplierId, num? suppPersonId, String? suppStartDate, String? suppEndDate, String? suppWorkingHours, num? taskStatusId, String? engSignature, String? nurseSignature, num? safetyId, num? assetAvailabilityId, String? assetAvailabilityName, String? assetName, String? assetNumber, String? assetSerialNo, num? assetSupplierId, String? assetSupplierName, String? assignedEmployeeName, String? assignedToName, String? contractNumber, String? createdOn, num? departmentId, String? departmentName, String? deviceStatusName, String? dueDate, String? executionTimeFrameName, String? forwardToName, String? groupLeaderReviewName, num? manufacturerId, String? manufacturerName, num? modelId, String? modelName, String? modifiedOn, bool? notified, String? planCode, num? planNo, num? ppmId, String? ppmScheduleCode, num? ppmScheduleNo, num? ppmSupplierId, String? ppmSupplierName, String? safetyName, num? siteId, String? siteName, String? buildingName, String? floorName, String? roomName, String? supplierName, String? suppPerson, String? taskStatusName, String? timePeriodName, num? timePeriodValue, String? typeOfServiceName, String? visitCode, num? visitNo, String? visitStatusName, String? warrantyEndDate, TimerModel? tbsTimer, TimerModel? externalEngineerTimer, }) { Ppm ppm = Ppm( id: id ?? this.id, ppmScheduleId: ppmScheduleId ?? this.ppmScheduleId, assetId: assetId ?? this.assetId, jobSheetNo: jobSheetNo ?? this.jobSheetNo, assignedEmployeeId: assignedEmployeeId ?? this.assignedEmployeeId, expectedDate: expectedDate ?? this.expectedDate, actualDate: actualDate ?? this.actualDate, nextDate: nextDate ?? this.nextDate, forwardToId: forwardToId ?? this.forwardToId, maintenanceContractId: maintenanceContractId ?? this.maintenanceContractId, typeOfServiceId: typeOfServiceId ?? this.typeOfServiceId, executionTimeFrameId: executionTimeFrameId ?? this.executionTimeFrameId, externalEngineer: externalEngineer ?? this.externalEngineer, telephone: telephone ?? this.telephone, groupLeaderReviewId: groupLeaderReviewId ?? this.groupLeaderReviewId, timePeriodId: timePeriodId ?? this.timePeriodId, assignedToId: assignedToId ?? this.assignedToId, vCalibrationTools: vCalibrationTools ?? this.vCalibrationTools, vKits: vKits ?? this.vKits, vContacts: vContacts ?? this.vContacts, vChecklists: vChecklists ?? this.vChecklists, files: files ?? this.files, visitStatusId: visitStatusId ?? this.visitStatusId, visitTimers: visitTimers ?? this.visitTimers, startDate: startDate ?? this.startDate, endDate: endDate ?? this.endDate, workingHours: workingHours ?? this.workingHours, travelingHours: travelingHours ?? this.travelingHours, deviceStatusId: deviceStatusId ?? this.deviceStatusId, comments: comments ?? this.comments, workPerformed: workPerformed ?? this.workPerformed, supplierId: supplierId ?? this.supplierId, suppPersonId: suppPersonId ?? this.suppPersonId, suppStartDate: suppStartDate ?? this.suppStartDate, suppEndDate: suppEndDate ?? this.suppEndDate, suppWorkingHours: suppWorkingHours ?? this.suppWorkingHours, taskStatusId: taskStatusId ?? this.taskStatusId, engSignature: engSignature ?? this.engSignature, nurseSignature: nurseSignature ?? this.nurseSignature, safetyId: safetyId ?? this.safetyId, assetAvailabilityId: assetAvailabilityId ?? this.assetAvailabilityId, assetAvailabilityName: assetAvailabilityName ?? this.assetAvailabilityName, assetName: assetName ?? this.assetName, assetNumber: assetNumber ?? this.assetNumber, assetSerialNo: assetSerialNo ?? this.assetSerialNo, assetSupplierId: assetSupplierId ?? this.assetSupplierId, assetSupplierName: assetSupplierName ?? this.assetSupplierName, assignedEmployeeName: assignedEmployeeName ?? this.assignedEmployeeName, assignedToName: assignedToName ?? this.assignedToName, createdOn: createdOn ?? this.createdOn, departmentId: departmentId ?? this.departmentId, departmentName: departmentName ?? this.departmentName, deviceStatusName: deviceStatusName ?? this.deviceStatusName, dueDate: dueDate ?? this.dueDate, executionTimeFrameName: executionTimeFrameName ?? this.executionTimeFrameName, forwardToName: forwardToName ?? this.forwardToName, manufacturerId: manufacturerId ?? this.manufacturerId, manufacturerName: manufacturerName ?? this.manufacturerName, modifiedOn: modifiedOn ?? this.modifiedOn, notified: notified ?? this.notified, planCode: planCode ?? this.planCode, planNo: planNo ?? this.planNo, ppmId: ppmId ?? this.ppmId, ppmScheduleCode: ppmScheduleCode ?? this.ppmScheduleCode, ppmScheduleNo: ppmScheduleNo ?? this.ppmScheduleNo, ppmSupplierId: ppmSupplierId ?? this.ppmSupplierId, ppmSupplierName: ppmSupplierName ?? this.ppmSupplierName, safetyName: safetyName ?? this.safetyName, siteId: siteId ?? this.siteId, siteName: siteName ?? this.siteName, buildingName: buildingName ?? this.buildingName, floorName: floorName ?? this.floorName, roomName: roomName ?? this.roomName, supplierName: supplierName ?? this.supplierName, suppPerson: suppPerson ?? this.suppPerson, taskStatusName: taskStatusName ?? this.taskStatusName, timePeriodName: timePeriodName ?? this.timePeriodName, timePeriodValue: timePeriodValue ?? this.timePeriodValue, typeOfServiceName: typeOfServiceName ?? this.typeOfServiceName, visitCode: visitCode ?? this.visitCode, visitNo: visitNo ?? this.visitNo, visitStatusName: visitStatusName ?? this.visitStatusName, warrantyEndDate: warrantyEndDate ?? this.warrantyEndDate, ); ppm.tbsTimer = tbsTimer ?? this.tbsTimer; ppm.externalEngineerTimer = externalEngineerTimer ?? this.externalEngineerTimer; return ppm; } Map toJson() { final map = {}; map['id'] = id; map['ppmScheduleId'] = ppmScheduleId; map['assetId'] = assetId; map['jobSheetNo'] = jobSheetNo; map['assignedEmployeeId'] = assignedEmployeeId; map['expectedDate'] = expectedDate; map['actualDate'] = actualDate; map['nextDate'] = nextDate; map['forwardToId'] = forwardToId; map['maintenanceContractId'] = maintenanceContractId; map['typeOfServiceId'] = typeOfServiceId; map['executionTimeFrameId'] = executionTimeFrameId; map['externalEngineer'] = externalEngineer; map['telephone'] = telephone; map['groupLeaderReviewId'] = groupLeaderReviewId; map['timePeriodId'] = timePeriodId; map['assignedToId'] = assignedToId; if (vCalibrationTools != null) { map['vCalibrationTools'] = vCalibrationTools!.map((v) => v.copyWith(visitId: id).toJson()).toList(); } if (vKits != null) { map['vKits'] = vKits!.map((v) => v.copyWith(visitId: id).toJson()).toList(); } if (vContacts != null) { map['vContacts'] = vContacts!.map((v) => v.toJson()).toList(); } if (vChecklists != null) { map['vChecklists'] = vChecklists!.map((v) => v.toJson()).toList(); } if (files?.isNotEmpty ?? false) { map["vAttachments"] = files! .map((file) => {"attachmentName": _isLocalUrl(file.attachmentName!) ? ("${file.attachmentName!.split("/").last}|${base64Encode(File(file.attachmentName!).readAsBytesSync())}") : file.attachmentName}) .toList(); } map['visitStatusId'] = visitStatusId; if (visitTimers != null) { map['visitTimers'] = visitTimers!.map((v) => v.toJson()).toList(); } // map['startDate'] = tbsTimer?.startAt?.toIso8601String(); // map['endDate'] = tbsTimer?.endAt?.toIso8601String(); // map['workingHours'] = ((tbsTimer?.durationInSecond ?? 0) / 60 / 60).toStringAsFixed(5); map['travelingHours'] = travelingHours; map['deviceStatusId'] = deviceStatusId; map['comments'] = comments; map['workPerformed'] = workPerformed; map['supplierId'] = supplierId; map['suppPersonId'] = suppPersonId; map['suppStartDate'] = externalEngineerTimer?.startAt?.toIso8601String(); map['suppEndDate'] = externalEngineerTimer?.endAt?.toIso8601String(); map['suppWorkingHours'] = ((externalEngineerTimer?.durationInSecond ?? 0) / 60 / 60).toStringAsFixed(5); map['taskStatusId'] = taskStatusId; map['engSignature'] = engSignature; map['nurseSignature'] = nurseSignature; map['safetyId'] = safetyId; map['assetAvailabilityId'] = assetAvailabilityId; map['assetAvailabilityName'] = assetAvailabilityName; map['assetName'] = assetName; map['assetNumber'] = assetNumber; map['assetSerialNo'] = assetSerialNo; map['assetSupplierId'] = assetSupplierId; map['assetSupplierName'] = assetSupplierName; map['assignedEmployeeName'] = assignedEmployeeName; map['assignedToName'] = assignedToName; map['createdOn'] = createdOn; map['departmentId'] = departmentId; map['departmentName'] = departmentName; map['deviceStatusName'] = deviceStatusName; map['dueDate'] = dueDate; map['executionTimeFrameName'] = executionTimeFrameName; map['forwardToName'] = forwardToName; map['manufacturerId'] = manufacturerId; map['manufacturerName'] = manufacturerName; map['modifiedOn'] = modifiedOn; map['notified'] = notified; map['planCode'] = planCode; map['planNo'] = planNo; map['ppmId'] = ppmId; map['ppmScheduleCode'] = ppmScheduleCode; map['ppmScheduleNo'] = ppmScheduleNo; map['ppmSupplierId'] = ppmSupplierId; map['ppmSupplierName'] = ppmSupplierName; map['safetyName'] = safetyName; map['siteId'] = siteId; map['siteName'] = siteName; map['supplierName'] = supplierName; map['suppPerson'] = suppPerson; map['taskStatusName'] = taskStatusName; map['timePeriodName'] = timePeriodName; map['timePeriodValue'] = timePeriodValue; map['typeOfServiceName'] = typeOfServiceName; map['visitCode'] = visitCode; map['visitNo'] = visitNo; map['visitStatusName'] = visitStatusName; map['warrantyEndDate'] = warrantyEndDate; return map; } bool _isLocalUrl(String url) { if (url.isEmpty != false) return false; return url.startsWith("/") || url.startsWith("file://") || url.substring(1).startsWith(':\\'); } Future validate(BuildContext context) async { if (deviceStatusId == null || visitStatusId == null) { await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.status}"); return false; } if (actualDate == null) { await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.actualDate}"); return false; } if (expectedDate == null) { await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.visitDate}"); return false; } if (tbsTimer?.startAt == null) { await Fluttertoast.showToast(msg: "Working Hours Required"); return false; } if (tbsTimer?.endAt == null) { await Fluttertoast.showToast(msg: "Please Stop The Timer"); return false; } if (externalEngineerTimer?.startAt != null && externalEngineerTimer?.endAt == null) { await Fluttertoast.showToast(msg: "Please Stop External Engineer Timer"); return false; } return true; } void removeEmptyObjects() { if (vCalibrationTools?.isNotEmpty ?? false) vCalibrationTools!.removeWhere((element) => element.assetId == null && element.calibrationDateOfTesters == null); if (vKits?.isNotEmpty ?? false) vKits!.removeWhere((element) => element.partName == null && element.partNumber == null); } }