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