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.
129 lines
4.0 KiB
Dart
129 lines
4.0 KiB
Dart
|
3 years ago
|
import 'dart:convert';
|
||
|
|
import 'dart:io';
|
||
|
|
|
||
|
3 years ago
|
import '../lookup.dart';
|
||
|
|
import '../timer_model.dart';
|
||
|
|
import 'calibration_tools.dart';
|
||
|
|
import 'pm_kit.dart';
|
||
|
|
import 'ppm_check_list.dart';
|
||
|
3 years ago
|
|
||
|
3 years ago
|
class Pentry {
|
||
|
|
Lookup? ppmVisitStatus;
|
||
|
|
Lookup? status;
|
||
|
|
TimerModel? timer;
|
||
|
|
DateTime? actualVisitDate;
|
||
|
|
String? travelingHours;
|
||
|
|
String? image;
|
||
|
|
File? imageFile;
|
||
|
|
List<PPMCheckList>? ppmCheckLists;
|
||
|
|
List<CalibrationTool>? calibrationTools;
|
||
|
|
List<PMKit>? pmKits;
|
||
|
3 years ago
|
|
||
|
|
Pentry({
|
||
|
|
this.travelingHours,
|
||
|
|
this.timer,
|
||
|
|
this.status,
|
||
|
|
this.ppmVisitStatus,
|
||
|
|
this.actualVisitDate,
|
||
|
|
this.image,
|
||
|
|
this.imageFile,
|
||
|
|
// this.contacts,
|
||
|
|
this.ppmCheckLists,
|
||
|
|
this.calibrationTools,
|
||
|
|
this.pmKits,
|
||
|
|
});
|
||
|
|
|
||
|
3 years ago
|
bool validate() {
|
||
|
|
if (actualVisitDate == null) return false;
|
||
|
|
if (timer == null && timer?.endAt != null) return false;
|
||
|
|
if (ppmVisitStatus == null) return false;
|
||
|
3 years ago
|
//if(status == null) return false;
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
Map<String, String> toMap() {
|
||
|
|
Map<String, String> map = {};
|
||
|
3 years ago
|
if (ppmVisitStatus != null)
|
||
|
|
map["visit_status"] = ppmVisitStatus!.id.toString();
|
||
|
|
if (status != null) map["pentry_status"] = status!.id.toString();
|
||
|
|
if (travelingHours != null) map["traveling_hours"] = travelingHours!;
|
||
|
|
if (imageFile != null)
|
||
|
|
map["file_attachement"] = base64Encode(imageFile!.readAsBytesSync());
|
||
|
|
map["actual_date"] = ((actualVisitDate?.millisecondsSinceEpoch ?? 0) / 1000)
|
||
|
|
.toStringAsFixed(0);
|
||
|
|
if (timer != null) {
|
||
|
|
map["start_date"] =
|
||
|
3 years ago
|
((timer!.startAt?.millisecondsSinceEpoch??0) / 1000).toStringAsFixed(0);
|
||
|
3 years ago
|
map["end_date"] =
|
||
|
3 years ago
|
(((timer!.endAt)?.millisecondsSinceEpoch??0) / 1000).toStringAsFixed(0);
|
||
|
3 years ago
|
map["working_hours"] =
|
||
|
3 years ago
|
((timer!.durationInSecond??0) / 60 / 60).toStringAsFixed(5);
|
||
|
3 years ago
|
}
|
||
|
|
// if(contacts?.isNotEmpty == true) {
|
||
|
|
// for(int i = 0;i<contacts.length;i++){
|
||
|
|
// contacts[i].toMap().forEach((key, value) {
|
||
|
|
// body["contacts[$i].$key"] = value;
|
||
|
|
// });
|
||
|
|
// }
|
||
|
|
// }
|
||
|
3 years ago
|
map["ppmCheckLists"] =
|
||
|
|
jsonEncode(ppmCheckLists?.map((e) => e.toMap()).toList());
|
||
|
|
map["calibrationTools"] =
|
||
|
|
jsonEncode(calibrationTools?.map((e) => e.toMap()).toList());
|
||
|
|
map["pmKits"] = jsonEncode(pmKits?.map((e) => e.toMap()).toList());
|
||
|
3 years ago
|
return map;
|
||
|
|
}
|
||
|
|
|
||
|
|
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 = [];
|
||
|
3 years ago
|
if (map['pmKits'] != null) {
|
||
|
|
pmKits = (map['pmKits'] as List<dynamic>)
|
||
|
3 years ago
|
.map((e) => PMKit.fromMap(e as Map<String, dynamic>))
|
||
|
|
.toList();
|
||
|
|
}
|
||
|
|
|
||
|
|
List<PPMCheckList> ppmCheckLists = [];
|
||
|
3 years ago
|
if (map['ppmCheckLists'] != null) {
|
||
|
|
ppmCheckLists = (map['ppmCheckLists'] as List<dynamic>)
|
||
|
3 years ago
|
.map((e) => PPMCheckList.fromMap(e as Map<String, dynamic>))
|
||
|
|
.toList();
|
||
|
|
}
|
||
|
|
|
||
|
|
List<CalibrationTool> calibrationTools = [];
|
||
|
3 years ago
|
if (map['calibrationTools'] != null) {
|
||
|
|
calibrationTools = (map['calibrationTools'] as List<dynamic>)
|
||
|
3 years ago
|
.map((e) => CalibrationTool.fromMap(e as Map<String, dynamic>))
|
||
|
|
.toList();
|
||
|
|
}
|
||
|
|
return Pentry(
|
||
|
|
status: Lookup.fromJson(map["pentry_status"]),
|
||
|
|
ppmVisitStatus: Lookup.fromJson(map["visit_status"]),
|
||
|
|
actualVisitDate: getDate(map["actual_date"]),
|
||
|
|
travelingHours: map["traveling_hours"],
|
||
|
|
timer: TimerModel(
|
||
|
3 years ago
|
startAt: getDate(map["start_date"]),
|
||
|
|
endAt: getDate(map["end_date"]),
|
||
|
|
durationInSecond:
|
||
|
|
(int.tryParse(map["working_hours"] ?? "") ?? 0) * 60 * 60),
|
||
|
3 years ago
|
// contacts: contacts,
|
||
|
|
ppmCheckLists: ppmCheckLists,
|
||
|
|
calibrationTools: calibrationTools,
|
||
|
|
pmKits: pmKits,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
static getDate(String date) {
|
||
|
|
return date.isEmpty
|
||
|
|
? null
|
||
|
|
: DateTime.fromMillisecondsSinceEpoch((int.tryParse(date) ?? 0) * 1000);
|
||
|
3 years ago
|
}
|
||
|
3 years ago
|
}
|