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.
543 lines
18 KiB
Dart
543 lines
18 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:test_sa/controllers/api_routes/api_manager.dart';
|
|
import 'package:test_sa/controllers/api_routes/http_status_manger.dart';
|
|
import 'package:test_sa/controllers/api_routes/urls.dart';
|
|
import 'package:test_sa/models/issue.dart';
|
|
import 'package:test_sa/models/service_report.dart';
|
|
import 'package:test_sa/models/service_request/service_request.dart';
|
|
import 'package:test_sa/models/service_request/service_request_search.dart';
|
|
import 'package:test_sa/models/subtitle.dart';
|
|
import 'package:test_sa/models/timer_model.dart';
|
|
import 'package:test_sa/models/user.dart';
|
|
|
|
import '../../../models/service_request/search_work_order.dart';
|
|
|
|
class ServiceRequestsProvider extends ChangeNotifier {
|
|
// number of items call in each request
|
|
final pageItemNumber = 10;
|
|
|
|
//reset provider data
|
|
void reset() {
|
|
serviceRequests = null;
|
|
nextPage = true;
|
|
stateCode = null;
|
|
}
|
|
|
|
// state code of current request to defied error message
|
|
// like 400 customer request failed
|
|
// 500 service not available
|
|
int stateCode;
|
|
|
|
// true if there is next page in product list and false if not
|
|
bool nextPage = true;
|
|
|
|
// list of user requests
|
|
List<ServiceRequest> serviceRequests;
|
|
List<SearchWorkOrders> workOrders = [];
|
|
|
|
// when requests in-process _loading = true
|
|
// done _loading = true
|
|
// failed _loading = false
|
|
bool isLoading;
|
|
|
|
ServiceRequestSearch search = ServiceRequestSearch();
|
|
|
|
/// return -2 if request in progress
|
|
/// return -1 if error happen when sending request
|
|
/// return state code if request complete may be 200, 404 or 403
|
|
/// for more details check http state manager
|
|
/// lib\controllers\http_status_manger\http_status_manger.dart
|
|
Future<int> getRequests({
|
|
@required String host,
|
|
@required User user,
|
|
@required int hospitalId,
|
|
}) async {
|
|
if (isLoading == true) return -2;
|
|
isLoading = true;
|
|
if (serviceRequests == null) notifyListeners();
|
|
Response response;
|
|
try {
|
|
Map<String, dynamic> body = {};
|
|
body.addAll(search.toMap());
|
|
body["pageNumber"] = (serviceRequests?.length ?? 0) ~/ pageItemNumber + 1;
|
|
body["pageSize"] = pageItemNumber;
|
|
|
|
response = await ApiManager.instance.post(
|
|
URLs.getServiceRequests,
|
|
body: body,
|
|
);
|
|
|
|
stateCode = response.statusCode;
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// client's request was successfully received
|
|
List requestsListJson = json.decode(response.body)["data"];
|
|
List<ServiceRequest> serviceRequestsPage = requestsListJson.map((request) => ServiceRequest.fromJson(request)).toList();
|
|
serviceRequests ??= [];
|
|
serviceRequests.addAll(serviceRequestsPage);
|
|
if (serviceRequestsPage.length == pageItemNumber) {
|
|
nextPage = true;
|
|
} else {
|
|
nextPage = false;
|
|
}
|
|
}
|
|
isLoading = false;
|
|
notifyListeners();
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
isLoading = false;
|
|
stateCode = -1;
|
|
notifyListeners();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<ServiceRequest> getSingleServiceRequest({
|
|
@required String requestId,
|
|
@required String host,
|
|
@required User user,
|
|
@required Subtitle subtitle,
|
|
}) async {
|
|
String userData = '';
|
|
if (user != null) {
|
|
userData += "&uid=" + user.id;
|
|
userData += "&token=" + user.token;
|
|
}
|
|
|
|
Response response;
|
|
try {
|
|
response = await get(Uri.parse(
|
|
host +
|
|
URLs.getSingleServiceRequest +
|
|
'?call_nid=$requestId'
|
|
'$userData',
|
|
));
|
|
} catch (error) {
|
|
throw (HttpStatusManger.getStatusMessage(status: -1, subtitle: subtitle));
|
|
}
|
|
|
|
// If the call to the server was successful, parse the JSON.
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// If the call to the server was successful, parse the JSON.
|
|
List jsonList = json.decode(utf8.decode(response.bodyBytes).replaceAll("\\", ""));
|
|
List<ServiceRequest> _requests = jsonList.map((i) => ServiceRequest.fromJson(i)).toList();
|
|
return _requests[0];
|
|
} else {
|
|
throw (HttpStatusManger.getStatusMessage(status: response.statusCode, subtitle: subtitle));
|
|
}
|
|
}
|
|
|
|
Future<int> createRequest({
|
|
@required String host,
|
|
@required User user,
|
|
@required ServiceRequest serviceRequest,
|
|
}) async {
|
|
var body = {
|
|
"id": 0,
|
|
"calNo": "",
|
|
"callCreatedBy": {"id": user.userID, "name": user.userName ?? ""},
|
|
"assets": serviceRequest.deviceId == null ? [] : [serviceRequest.deviceId],
|
|
"requestedDate": DateTime.now().toIso8601String(),
|
|
"requestedTime": DateTime.now().toIso8601String(),
|
|
"client": user.clientId,
|
|
"callComments": serviceRequest.callComments,
|
|
if (serviceRequest.devicePhotos.isNotEmpty) "attachmentsCallRequest": serviceRequest.devicePhotos.map((e) => {"name": e}).toList(),
|
|
"priority": serviceRequest.priority.toMap(),
|
|
"defectType": serviceRequest.defectType.toMap(),
|
|
"typeofRequest": serviceRequest.type.toMap(),
|
|
"requestedThrough": serviceRequest.requestedThrough.toMap(),
|
|
"reviewComment": null,
|
|
if (serviceRequest.audio != null) "voiceNote": serviceRequest.audio,
|
|
"callSiteContactPerson": [
|
|
{
|
|
"id": 0,
|
|
// "employeeCode": user.email,
|
|
"name": user.userName,
|
|
"telephone": user.phoneNumber,
|
|
// "job": "",
|
|
"email": user.email,
|
|
// "land": "",
|
|
"contactUserId": user.userID,
|
|
},
|
|
],
|
|
"noofFollowup": 0,
|
|
"status": null,
|
|
"callLastSituation": null,
|
|
"firstAction": null,
|
|
"loanAvailablity": null,
|
|
"comments": null,
|
|
"firstActionDate": null,
|
|
"visitDate": null,
|
|
"callReview": null,
|
|
};
|
|
if (serviceRequest.audio != null) {
|
|
body["voiceNote"] = serviceRequest.audio;
|
|
}
|
|
Response response;
|
|
try {
|
|
response = await ApiManager.instance.post(URLs.createRequest, body: body);
|
|
stateCode = response.statusCode;
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
if (serviceRequests != null) serviceRequests.insert(0, ServiceRequest.fromJson(json.decode(utf8.decode(response.bodyBytes))[0]));
|
|
notifyListeners();
|
|
}
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<int> createIssueReport({
|
|
@required String host,
|
|
@required User user,
|
|
@required Issue issue,
|
|
}) async {
|
|
Response response;
|
|
Map<String, String> body = issue.toMap();
|
|
body["uid"] = user.id;
|
|
body["token"] = user.token;
|
|
try {
|
|
response = await post(
|
|
Uri.parse(host + URLs.createReport),
|
|
body: body,
|
|
);
|
|
|
|
stateCode = response.statusCode;
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {}
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<int> updateDate({
|
|
@required String host,
|
|
@required User user,
|
|
@required ServiceRequest request,
|
|
DateTime date,
|
|
}) async {
|
|
Map<String, dynamic> serviceRequest = await getServiceRequestById(requestId: request.id) ?? "";
|
|
final List callSiteContacts = (serviceRequest['callSiteContactPerson'] as List);
|
|
final Map<String, dynamic> callSiteContactPerson = callSiteContacts.isEmpty ? {} : callSiteContacts[0];
|
|
print(callSiteContactPerson);
|
|
Response response;
|
|
var body = {
|
|
"id": request.id,
|
|
"callNo": serviceRequest['callNo'],
|
|
"callCreatedBy": serviceRequest['callCreatedBy'],
|
|
"requestedDate": date?.toIso8601String() ?? "",
|
|
"requestedTime": date?.toIso8601String() ?? "",
|
|
"priority": request.priority?.toMap(),
|
|
"defectType": request.defectType?.toMap(),
|
|
"typeofRequest": request.type?.toMap(),
|
|
"requestedThrough": request.requestedThrough?.toMap(),
|
|
"voiceNote": request.audio,
|
|
"assets": request.deviceId == null ? [] : [request.deviceId],
|
|
"attachmentsCallRequest": request.devicePhotos?.map((e) => {"name": e})?.toList(),
|
|
"callSiteContactPerson": [
|
|
{
|
|
"id": callSiteContactPerson['id'] ?? 0,
|
|
"employeeCode": callSiteContactPerson['employeeCode'],
|
|
"name": callSiteContactPerson['name'] ?? user.userName,
|
|
"telephone": callSiteContactPerson['telephone'] ?? user.phoneNumber,
|
|
"job": callSiteContactPerson['job'],
|
|
"email": callSiteContactPerson['email'] ?? user.email,
|
|
"land": callSiteContactPerson['land'],
|
|
"contactUserId": user.userID,
|
|
},
|
|
],
|
|
"callComments": request.callComments,
|
|
"noofFollowup": 0,
|
|
// "status": null,
|
|
"callLastSituation": null,
|
|
"firstAction": request.firstAction?.toMap(),
|
|
"loanAvailablity": request.loanAvailability?.toMap(),
|
|
"comments": request.reviewComment,
|
|
"firstActionDate": serviceRequest['firstActionDate'],
|
|
"visitDate": date?.toIso8601String() ?? "",
|
|
"callReview": null,
|
|
"reviewComment": null,
|
|
};
|
|
try {
|
|
response = await ApiManager.instance.put(
|
|
URLs.updateRequestDate,
|
|
body: body,
|
|
);
|
|
stateCode = response.statusCode;
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// request.engineerName = employee.name;
|
|
notifyListeners();
|
|
}
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<int> createServiceReport({
|
|
@required String host,
|
|
@required User user,
|
|
@required ServiceReport report,
|
|
@required ServiceRequest request,
|
|
}) async {
|
|
Response response;
|
|
try {
|
|
Map<String, dynamic> body = {
|
|
"id": 0,
|
|
"parentWOId": null,
|
|
"workOrderNo": "",
|
|
"workOrderYear": null,
|
|
"workOrderSequennce": null,
|
|
"callRequest": {
|
|
"id": request.id,
|
|
},
|
|
"assetType": report.assetType?.toMap(),
|
|
// "assignedEmployee": {"id": report.engineer?.id, "name": report.engineer?.name ?? ""},
|
|
"visitDate": report.visitDate?.toIso8601String() ?? "",
|
|
// "assistantEmployees": [
|
|
// {"id": report.engineer.id, "name": report.engineer.name},
|
|
// ],
|
|
"supplier": null,
|
|
"vendorTicketNumber": null,
|
|
"contactPersonWorkOrders": [
|
|
{
|
|
"id": 0,
|
|
// "employeeCode": "",
|
|
"name": user.userName,
|
|
"telephone": user.phoneNumber,
|
|
// "job": "",
|
|
"email": user.email,
|
|
// "land": "",
|
|
"contactUserId": user.userID,
|
|
}
|
|
],
|
|
"calllastSituation": report.callLastSituation?.toMap(),
|
|
"currentSituation": null,
|
|
"repairLocation": report.repairLocation?.toMap(),
|
|
"reason": report.reason?.toMap(),
|
|
"startofWorkTime": report.startDate?.toIso8601String() ?? "",
|
|
"endofWorkTime": report.endDate?.toIso8601String() ?? "",
|
|
"workingHours": report.timer?.durationInSecond,
|
|
"travelingHours": report.travelingHours,
|
|
"travelingExpenses": report.travelingExpense ?? 0,
|
|
"faultDescription": {
|
|
"id": report.faultDescriptionId,
|
|
"defectName": report.type?.name,
|
|
"workPerformed": report.workPreformed,
|
|
"estimatedTime": report.operatingHours,
|
|
},
|
|
"sparePartsWorkOrders": report.parts
|
|
?.map(
|
|
(p) => {
|
|
"id": p.id,
|
|
"sparePart": {"id": p.reportPartID ?? 0, "partNo": p.code, "partName": p.name},
|
|
"qty": p.quantity
|
|
},
|
|
)
|
|
?.toList(),
|
|
"reviewComment": report.reviewComment ?? "",
|
|
"comment": report.comment,
|
|
"attachmentsWorkOrder": request.devicePhotos?.map((e) => {"name": e})?.toList(),
|
|
"equipmentStatus": report.status?.toMap(),
|
|
"suppEngineerWorkOrders": null,
|
|
"engSignature": report.signatureEngineer,
|
|
"nurseSignature": report.signatureNurse,
|
|
"woParentDto": null,
|
|
};
|
|
// body["uid"] = user.id;
|
|
// body["token"] = user.token;
|
|
response = await ApiManager.instance.post(URLs.createServiceReport, body: body);
|
|
print(response.body);
|
|
// response = await post(
|
|
// Uri.parse(
|
|
// host+URLs.createServiceReport),
|
|
// body: body,
|
|
// );
|
|
|
|
stateCode = response.statusCode;
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
reset();
|
|
notifyListeners();
|
|
}
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
print(error);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<List<SearchWorkOrders>> searchWorkOrders({@required String callId}) async {
|
|
Response response;
|
|
|
|
try {
|
|
var body = {
|
|
"pageSize": pageItemNumber,
|
|
"pageNumber": ((workOrders?.length ?? 0) ~/ pageItemNumber) + 1,
|
|
"callId": callId,
|
|
};
|
|
response = await ApiManager.instance.post(URLs.searchWorkOrders, body: body);
|
|
stateCode = response.statusCode;
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// client's request was successfully received
|
|
List workOrdersJson = json.decode(response.body)["data"];
|
|
print(workOrdersJson);
|
|
workOrders = workOrdersJson.map<SearchWorkOrders>((request) => SearchWorkOrders.fromJson(request)).toList();
|
|
if (workOrders.length == pageItemNumber) {
|
|
nextPage = true;
|
|
} else {
|
|
nextPage = false;
|
|
}
|
|
}
|
|
return workOrders;
|
|
} catch (e) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<int> createDuplicatedReport({
|
|
@required String host,
|
|
@required User user,
|
|
@required ServiceRequest request,
|
|
}) async {
|
|
Response response;
|
|
String userData = '';
|
|
if (user != null) {
|
|
userData += "&uid=" + user.id;
|
|
userData += "&token=" + user.token;
|
|
}
|
|
|
|
try {
|
|
response = await get(
|
|
Uri.parse(host + URLs.createDuplicatedReport + "?nid=${request.id}" + userData),
|
|
);
|
|
|
|
stateCode = response.statusCode;
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
reset();
|
|
notifyListeners();
|
|
}
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<int> updateServiceReport({
|
|
@required String host,
|
|
@required User user,
|
|
@required ServiceReport report,
|
|
@required ServiceRequest request,
|
|
}) async {
|
|
Response response;
|
|
//Map<String,dynamic> body = report.toMap(request);
|
|
// body["uid"] = user.id;
|
|
// body["token"] = user.token;
|
|
// body["job_id"] = request.id;
|
|
// body["report_id"] = request.reportID;
|
|
try {
|
|
Map<String, dynamic> body = report.toMap(request);
|
|
// body["uid"] = user.id;
|
|
// body["token"] = user.token;
|
|
response = await ApiManager.instance.put(URLs.updateServiceReport, body: body);
|
|
// response = await post(
|
|
// Uri.parse(
|
|
// host+URLs.updateServiceReport),
|
|
// body: body,
|
|
// );
|
|
// stateCode = response.statusCode;
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
reset();
|
|
notifyListeners();
|
|
}
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<int> updateServiceReportTimer({
|
|
@required String host,
|
|
@required User user,
|
|
@required TimerModel timer,
|
|
@required ServiceRequest request,
|
|
}) async {
|
|
Response response;
|
|
Map<String, dynamic> body = {};
|
|
body["uid"] = user.id;
|
|
body["token"] = user.token;
|
|
body["job_id"] = request.id;
|
|
body["start_time"] = (timer.startAt.millisecondsSinceEpoch / 1000).toStringAsFixed(0);
|
|
body["end_time"] = (timer.endAt.millisecondsSinceEpoch / 1000).toStringAsFixed(0);
|
|
body["working_hours"] = (timer.durationInSecond / 60 / 60).toStringAsFixed(5);
|
|
body["report_id"] = request.reportID;
|
|
try {
|
|
response = await post(
|
|
Uri.parse(host + URLs.updateServiceReport),
|
|
body: body,
|
|
);
|
|
//stateCode = response.statusCode;
|
|
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// reset();
|
|
// notifyListeners();
|
|
}
|
|
return response.statusCode;
|
|
} catch (error) {
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
Future<ServiceReport> getSingleServiceReport({
|
|
@required int reportId,
|
|
@required String host,
|
|
@required User user,
|
|
@required Subtitle subtitle,
|
|
}) async {
|
|
Response response;
|
|
try {
|
|
response = await ApiManager.instance.get(
|
|
URLs.getServiceReport + "?workOrderId=$reportId",
|
|
);
|
|
} catch (error) {
|
|
throw (HttpStatusManger.getStatusMessage(status: -1, subtitle: subtitle));
|
|
}
|
|
|
|
// If the call to the server was successful, parse the JSON.
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// If the call to the server was successful, parse the JSON.
|
|
return ServiceReport.fromJson(json.decode(response.body)["data"], reportId);
|
|
} else {
|
|
throw (HttpStatusManger.getStatusMessage(status: response.statusCode, subtitle: subtitle));
|
|
}
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getServiceRequestById({@required String requestId}) async {
|
|
Response response;
|
|
try {
|
|
response = await ApiManager.instance.get(
|
|
URLs.getServiceRequestById + "?callRequestId=$requestId",
|
|
);
|
|
} catch (error) {
|
|
print(error);
|
|
return {};
|
|
}
|
|
|
|
// If the call to the server was successful, parse the JSON.
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
// If the call to the server was successful, parse the JSON.
|
|
return {
|
|
"callNo": json.decode(response.body)["data"]["callNo"],
|
|
"callCreatedBy": json.decode(response.body)["data"]["callCreatedBy"],
|
|
"callSiteContactPerson": json.decode(response.body)["data"]["callSiteContactPerson"],
|
|
"firstActionDate": json.decode(response.body)["data"]["firstActionDate"],
|
|
};
|
|
} else {
|
|
return {};
|
|
}
|
|
}
|
|
}
|