You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
7.7 KiB
Dart
204 lines
7.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:test_sa/controllers/api_routes/urls.dart';
|
|
import 'package:test_sa/models/lookup.dart';
|
|
import 'package:test_sa/models/pantry/calibration_tools.dart';
|
|
import 'package:test_sa/models/pantry/pm_kit.dart';
|
|
import 'package:test_sa/models/pantry/ppm_check_list.dart';
|
|
import 'package:test_sa/models/timer_model.dart';
|
|
|
|
class Pentry {
|
|
Lookup ppmVisitStatus;
|
|
Lookup status;
|
|
TimerModel timer;
|
|
DateTime actualVisitDate;
|
|
DateTime expectedVisitDate;
|
|
String travelingHours;
|
|
String image;
|
|
List<File> files = [];
|
|
|
|
// List<Contact> contacts;
|
|
List<PPMCheckList> ppmCheckLists;
|
|
List<CalibrationTool> calibrationTools;
|
|
List<PMKit> pmKits;
|
|
String signatureNurse;
|
|
String signatureEngineer;
|
|
Uint8List localNurseSignature;
|
|
Uint8List localEngineerSignature;
|
|
|
|
Pentry(
|
|
{this.travelingHours,
|
|
this.timer,
|
|
this.status,
|
|
this.ppmVisitStatus,
|
|
this.actualVisitDate,
|
|
this.expectedVisitDate,
|
|
this.image,
|
|
this.files,
|
|
// this.contacts,
|
|
this.ppmCheckLists,
|
|
this.calibrationTools,
|
|
this.pmKits,
|
|
this.signatureNurse,
|
|
this.signatureEngineer,
|
|
this.localNurseSignature,
|
|
this.localEngineerSignature});
|
|
|
|
Future<bool> validate() async {
|
|
if (ppmVisitStatus == null) {
|
|
await Fluttertoast.showToast(msg: "PPM Visit Status is Required");
|
|
return false;
|
|
}
|
|
if (actualVisitDate == null) {
|
|
await Fluttertoast.showToast(msg: "Actual visit date is Required");
|
|
return false;
|
|
}
|
|
if (expectedVisitDate == null) {
|
|
await Fluttertoast.showToast(msg: "Expected visit date is Required");
|
|
return false;
|
|
}
|
|
if (timer?.startAt == null) {
|
|
await Fluttertoast.showToast(msg: "Working Hours Required");
|
|
return false;
|
|
}
|
|
if (timer?.endAt == null) {
|
|
await Fluttertoast.showToast(msg: "Please Stop The Timer");
|
|
return false;
|
|
}
|
|
if (status?.id == null) {
|
|
await Fluttertoast.showToast(msg: "Device Status is Required");
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
Map<String, dynamic> toMap(int visitId) {
|
|
Map<String, dynamic> map = {};
|
|
map["visitStatusId"] = ppmVisitStatus?.id;
|
|
if (status != null) map["deviceStatusId"] = status?.id;
|
|
if (travelingHours != null) map["travelingHours"] = travelingHours;
|
|
//if(imageFile != null) map["file_attachement"] = base64Encode(imageFile.readAsBytesSync());
|
|
map["actualDate"] = actualVisitDate.toIso8601String();
|
|
map["expectedDate"] = expectedVisitDate.toIso8601String();
|
|
if (timer != null) {
|
|
map["startDate"] = timer.startAt.toIso8601String();
|
|
map["endDate"] = timer.endAt?.toIso8601String() ?? DateTime.now().toIso8601String();
|
|
map["workingHours"] = (timer.durationInSecond / 60 / 60).toStringAsFixed(5);
|
|
}
|
|
if (files?.isNotEmpty ?? false) {
|
|
map["vAttachments"] = files.map((file) => {"attachmentName": _isLocalUrl(file.path) ? (file.path.split("/").last + "|" + base64Encode(file.readAsBytesSync())) : file.path}).toList();
|
|
}
|
|
map["travelingHours"] = travelingHours;
|
|
// if(contacts?.isNotEmpty == true) {
|
|
// for(int i = 0;i<contacts.length;i++){
|
|
// contacts[i].toMap().forEach((key, value) {
|
|
// body["contacts[$i].$key"] = value;
|
|
// });
|
|
// }
|
|
// }
|
|
map["vChecklists"] = ppmCheckLists.map((e) => e.toMap(visitId)).toList();
|
|
map["vCalibrationTools"] = calibrationTools.map((e) => e.toMap(visitId)).toList();
|
|
map["vKits"] = pmKits.map((e) => e.toMap(visitId)).toList();
|
|
map["nurseSignature"] = signatureNurse; //"${DateTime.now().toIso8601String()}.png|$signatureNurse";
|
|
map["engSignature"] = signatureEngineer; //"${DateTime.now().toIso8601String()}.png|$signatureEngineer";
|
|
return map;
|
|
}
|
|
|
|
bool _isLocalUrl(String url) {
|
|
if (url?.isEmpty != false) return false;
|
|
return url.startsWith("/") || url.startsWith("file://") || url.substring(1).startsWith(':\\');
|
|
}
|
|
|
|
factory Pentry.fromMap(Map<String, dynamic> map) {
|
|
// List<Contact> contacts = [];
|
|
// if(map['contacts'] != null){
|
|
// contacts =(map['contacts'] as List<dynamic>)
|
|
// .map((e) => Contact.fromMap(e as Map<String, dynamic>))
|
|
// .toList();
|
|
// }
|
|
|
|
List<PMKit> pmKits = [];
|
|
if (map['vKits'] != null) {
|
|
pmKits = (map['vKits'] as List<dynamic>).map((e) => PMKit.fromMap(e as Map<String, dynamic>)).toList();
|
|
}
|
|
List<String> files = [];
|
|
if (map["vAttachments"] != null) if (map["vAttachments"] is List) {
|
|
List list = map["vAttachments"];
|
|
list.removeWhere((element) => (element["attachmentURL"]?.toString() ?? "").isEmpty);
|
|
files = list.map((e) => URLs.getFileUrl(e["attachmentName"])).toList();
|
|
}
|
|
List<PPMCheckList> ppmCheckLists = [];
|
|
if (map['vChecklists'] != null) {
|
|
ppmCheckLists = (map['vChecklists'] as List<dynamic>).map((e) => PPMCheckList.fromMap(e as Map<String, dynamic>)).toList();
|
|
}
|
|
|
|
List<CalibrationTool> calibrationTools = [];
|
|
if (map['vCalibrationTools'] != null) {
|
|
calibrationTools = (map['vCalibrationTools'] as List<dynamic>).map((e) => CalibrationTool.fromMap(e as Map<String, dynamic>)).toList();
|
|
}
|
|
return Pentry(
|
|
status: Lookup(id: map["deviceStatusId"], name: map["deviceStatusName"]),
|
|
ppmVisitStatus: Lookup(id: map["visitStatusId"], name: map["visitStatusName"]),
|
|
actualVisitDate: DateTime.tryParse(map["actualDate"] ?? ""),
|
|
expectedVisitDate: DateTime.tryParse(map["expectedDate"] ?? ""),
|
|
travelingHours: map["travelingHours"],
|
|
timer: TimerModel(
|
|
startAt: DateTime.tryParse(map["startDate"] ?? ""),
|
|
endAt: DateTime.tryParse(map["endDate"] ?? ""),
|
|
durationInSecond: (int.tryParse(map["workingHours"] ?? "") ?? 0) * 60 * 60,
|
|
),
|
|
// contacts: contacts,
|
|
ppmCheckLists: ppmCheckLists,
|
|
calibrationTools: calibrationTools,
|
|
pmKits: pmKits,
|
|
files: files?.map((e) {
|
|
return File(e);
|
|
})?.toList() ??
|
|
[],
|
|
signatureNurse: URLs.getFileUrl(map["nurseSignature"]),
|
|
signatureEngineer: URLs.getFileUrl(map["engSignature"]),
|
|
);
|
|
}
|
|
|
|
Pentry copyWith({
|
|
Lookup ppmVisitStatus,
|
|
Lookup status,
|
|
TimerModel timer,
|
|
DateTime actualVisitDate,
|
|
DateTime expectedVisitDate,
|
|
String travelingHours,
|
|
String image,
|
|
List<File> files,
|
|
List<PPMCheckList> ppmCheckLists,
|
|
List<CalibrationTool> calibrationTools,
|
|
List<PMKit> pmKits,
|
|
String signatureNurse,
|
|
String signatureEngineer,
|
|
}) {
|
|
return Pentry(
|
|
ppmVisitStatus: ppmVisitStatus ?? this.ppmVisitStatus,
|
|
status: status ?? this.status,
|
|
timer: timer ?? this.timer,
|
|
actualVisitDate: actualVisitDate ?? this.actualVisitDate,
|
|
expectedVisitDate: expectedVisitDate ?? this.expectedVisitDate,
|
|
travelingHours: travelingHours ?? this.travelingHours,
|
|
image: image ?? this.image,
|
|
files: files ?? this.files,
|
|
ppmCheckLists: ppmCheckLists ?? this.ppmCheckLists?.map((e) => e.copyWith())?.toList(),
|
|
calibrationTools: calibrationTools ?? this.calibrationTools?.map((e) => e.copyWith())?.toList(),
|
|
pmKits: pmKits ?? this.pmKits.map((e) => e.copyWith()).toList(),
|
|
signatureNurse: signatureNurse ?? this.signatureNurse,
|
|
signatureEngineer: signatureEngineer ?? this.signatureEngineer);
|
|
}
|
|
|
|
void clearEmptyObjects() {
|
|
ppmCheckLists.removeWhere((element) => (element?.title?.isEmpty ?? true) && (element?.comment?.isEmpty ?? true) && (element?.measuredValue?.isEmpty ?? true) && element.status == null);
|
|
calibrationTools.removeWhere((element) => (element?.assetsNumber == null) && (element?.dataOfTesting == null));
|
|
pmKits.removeWhere((element) => (element?.itemCode == null));
|
|
}
|
|
}
|