mirror of
https://git.meshkee.com/applications/cssd.git
synced 2026-10-10 19:48:56 +03:30
469 lines
12 KiB
Dart
Executable File
469 lines
12 KiB
Dart
Executable File
import 'dart:convert';
|
|
|
|
import 'package:cssd/models/item_model.dart';
|
|
import 'package:cssd/models/package_model.dart';
|
|
import 'package:flutter_downloader/flutter_downloader.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:cssd/utils/scan.dart';
|
|
|
|
import '../config/urls.dart';
|
|
import '../config/user.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
class PackageProvider extends ChangeNotifier {
|
|
List<ItemResultObject>? filteredItems;
|
|
|
|
// List<Map<String, dynamic>> itemVals = [];
|
|
List<String> itemVals = [];
|
|
|
|
List<TextEditingController> itemValues = [];
|
|
String initialItemValue = '';
|
|
int curItemVal = 1;
|
|
|
|
resetItemVals() {
|
|
itemVals = [];
|
|
itemValues = [];
|
|
initialItemValue = '';
|
|
curItemVal = 1;
|
|
}
|
|
|
|
addItemVal() {
|
|
curItemVal += 1;
|
|
TextEditingController lbl = new TextEditingController();
|
|
lbl.text = '';
|
|
itemValues.add(lbl);
|
|
notifyListeners();
|
|
}
|
|
|
|
removeItemVal() {
|
|
curItemVal -= 1;
|
|
TextEditingController lbl = new TextEditingController();
|
|
lbl.text = initialItemValue;
|
|
itemValues.removeLast();
|
|
notifyListeners();
|
|
}
|
|
|
|
bool isClear = false;
|
|
|
|
setClear(int index) {
|
|
isClear = true;
|
|
itemValues[index].clear();
|
|
notifyListeners();
|
|
}
|
|
|
|
reSetClear() {
|
|
isClear = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
changeItemVal(String value, int index) {
|
|
for (var item in filteredItems!) {
|
|
if (item.udi_name == value) {
|
|
itemValues[index].text = item.id;
|
|
}
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<bool> changeStatus(String id, String value) async {
|
|
print(id);
|
|
var response = await http.post(
|
|
Uri.parse(Url.changePackageStatus(id)),
|
|
body: json.encode({'status': value}),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json"
|
|
},
|
|
);
|
|
|
|
var packageData = json.decode(response.body);
|
|
|
|
try {
|
|
if (packageData['data']['transfers']['status'] == 'approved') {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
var isCurrectItem = false;
|
|
|
|
changeItemValByScan(String value, int index) async {
|
|
filteredItems?.forEach((item) {
|
|
if (item.id == value || item.udi == value) {
|
|
itemValues[index].text = "${item.name} - ${item.udi}";
|
|
isCurrectItem = true;
|
|
}
|
|
print("item value : ${itemValues[index].text}");
|
|
});
|
|
notifyListeners();
|
|
}
|
|
|
|
void scanItem(isEnabled, context, index) async {
|
|
await scan().then(
|
|
(String result) async {
|
|
if (result == 'cancel') {
|
|
if (index != 0) await removeItemVal();
|
|
print('cancel');
|
|
} else if (result == 'error') {
|
|
print('error');
|
|
} else if (result == 'access') {
|
|
print('access');
|
|
} else {
|
|
await changeItemValByScan(result, index);
|
|
isEnabled = false;
|
|
print("currect item : ${isCurrectItem}");
|
|
if (isCurrectItem) {
|
|
Fluttertoast.showToast(
|
|
msg: "Item added.",
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.BOTTOM,
|
|
);
|
|
scanItem(isEnabled, context, index += 1);
|
|
await addItemVal();
|
|
isCurrectItem = false;
|
|
} else {
|
|
Fluttertoast.showToast(
|
|
msg: "No item exists,try again!",
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.BOTTOM,
|
|
);
|
|
scanItem(isEnabled, context, index);
|
|
}
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
String searchBoxValue = '';
|
|
bool filterOn = false;
|
|
List<PackageResultObject>? items;
|
|
List<PackageResultObject>? originalItems;
|
|
|
|
PackageDetailObject? result;
|
|
PackageFilterObject? filters = PackageFilterObject(
|
|
id: '',
|
|
hospital: '',
|
|
name: '',
|
|
// udi: '',
|
|
);
|
|
int pageNumber = 1;
|
|
|
|
void resetPageNumber() => pageNumber = 1;
|
|
bool isloading = false;
|
|
|
|
void resetLoading() {
|
|
isloading = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
void setLoading() {
|
|
isloading = true;
|
|
// notifyListeners();
|
|
}
|
|
|
|
bool isloadingButton = false;
|
|
|
|
void resetLoadingButton() {
|
|
isloadingButton = false;
|
|
notifyListeners();
|
|
}
|
|
|
|
void setLoadingButton() {
|
|
isloadingButton = true;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<List<Map<String, String>>> getHospitalsFilters() async {
|
|
var response = await http.get(
|
|
Uri.parse(Url.filterItems),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
},
|
|
);
|
|
List<Map<String, String>> result = [];
|
|
var filterData = json.decode(response.body);
|
|
for (var hospital in filterData['data']['MedicalCenters']) {
|
|
result.add({
|
|
'name': hospital['name'].toString(),
|
|
'id': hospital['id'].toString()
|
|
});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Future<List<String>> getItemFilters() async {
|
|
var response = await http.get(
|
|
Uri.parse(Url.itemItems),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
},
|
|
);
|
|
List<String> result = [];
|
|
filteredItems = [];
|
|
var filterData = json.decode(response.body);
|
|
for (var item in filterData['data']) {
|
|
result.add("${item['name']} - ${item['udi'] != null ? item['udi'] : ''}");
|
|
// result.add(item['id'].toString());
|
|
filteredItems!.add(
|
|
ItemResultObject(
|
|
name: item['name'],
|
|
id: item['id'].toString(),
|
|
size: item['size'].toString(),
|
|
userName: item['user'] != null ? item['user']['name'] : '',
|
|
status: item['status'],
|
|
udi: item['udi'] != null ? item['udi'] : '',
|
|
udi_name:
|
|
"${item['name']} - ${item['udi'] != null ? item['udi'] : ''}",
|
|
),
|
|
);
|
|
}
|
|
if (result.isEmpty) result.add("");
|
|
itemVals = result;
|
|
initialItemValue = itemVals[0];
|
|
TextEditingController newLbl = new TextEditingController();
|
|
newLbl.text = "";
|
|
itemValues.add(newLbl);
|
|
notifyListeners();
|
|
return result;
|
|
}
|
|
|
|
Future<void> createPackageResult(
|
|
name,
|
|
// udi,
|
|
items,
|
|
note,
|
|
) async {
|
|
// List<Map<String, dynamic>> items = [];
|
|
// for (var item in items) {
|
|
// items.add({'id': item});
|
|
// }
|
|
var response = await http.post(
|
|
Uri.parse(Url.createPackageResult),
|
|
body: json.encode(
|
|
{
|
|
'note': note,
|
|
'items': items,
|
|
'name': name,
|
|
// 'package_id': udi,
|
|
},
|
|
),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json"
|
|
},
|
|
);
|
|
|
|
print("paccc body : ${response.body}");
|
|
print("paccc status : ${response.statusCode}");
|
|
}
|
|
|
|
Future<void> updatePackageResult(
|
|
id,
|
|
name,
|
|
// udi,
|
|
description,
|
|
items,
|
|
) async {
|
|
// List<Map<String, Object>> items = [];
|
|
// for (var item in items) {
|
|
// items.add({'name': '', 'id': item});
|
|
// }
|
|
print({
|
|
'note': description,
|
|
'name': name,
|
|
// 'package_id': udi,
|
|
'items': items,
|
|
});
|
|
var response = await http.put(Uri.parse(Url.updatePackageResult(id)),
|
|
body: json.encode(
|
|
{
|
|
'name': name,
|
|
// 'package_id': udi,
|
|
'note': description,
|
|
'items': items,
|
|
},
|
|
),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
"Content-Type": "application/json",
|
|
"Accept": "application/json"
|
|
});
|
|
|
|
print(response.body);
|
|
}
|
|
|
|
Future<bool> removePackages(String id) async {
|
|
var response = await http.post(Uri.parse(Url.removePackages(id)), body: {
|
|
'_method': 'delete'
|
|
}, headers: {
|
|
'Authorization': User.token,
|
|
});
|
|
|
|
if (response.statusCode == 200) {
|
|
items!.removeWhere((item) {
|
|
return item.id.toString() == id;
|
|
});
|
|
}
|
|
|
|
notifyListeners();
|
|
return true;
|
|
}
|
|
|
|
void setFilters(
|
|
id,
|
|
name,
|
|
hospital,
|
|
// udi,
|
|
) {
|
|
pageNumber = 1;
|
|
filters = PackageFilterObject(
|
|
id: id,
|
|
name: name,
|
|
hospital: hospital,
|
|
// udi: udi,
|
|
);
|
|
if (filters!.id == '' && filters!.name == '' && filters!.hospital == '') {
|
|
filterOn = false;
|
|
} else {
|
|
filterOn = true;
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
void search(String value) {
|
|
items = [];
|
|
searchBoxValue = value;
|
|
originalItems!.forEach((result) {
|
|
if (result.id
|
|
.toString()
|
|
.toLowerCase()
|
|
.contains(searchBoxValue.toLowerCase()) ||
|
|
result.id
|
|
.toString()
|
|
.toUpperCase()
|
|
.contains(searchBoxValue.toUpperCase()) ||
|
|
result.id
|
|
.toString()
|
|
.toLowerCase()
|
|
.contains(searchBoxValue.toUpperCase()) ||
|
|
result.id
|
|
.toString()
|
|
.toUpperCase()
|
|
.contains(searchBoxValue.toLowerCase())) {
|
|
items!.add(result);
|
|
}
|
|
});
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> getPackageResults() async {
|
|
String url = Url.packageResults(pageNumber);
|
|
if (filterOn) {
|
|
if (filters!.id != '') {
|
|
url += '&package_id=${filters!.id}';
|
|
}
|
|
if (filters!.name != '') {
|
|
url += '&name=${filters!.name}';
|
|
}
|
|
if (filters!.hospital != '') {
|
|
url += '&medical_center_id=${filters!.hospital}';
|
|
}
|
|
// if (filters!.udi != '') {
|
|
// url += '&package_id=${filters!.udi}';
|
|
// }
|
|
}
|
|
var response = await http.get(
|
|
Uri.parse(url),
|
|
headers: {
|
|
'Authorization': User.token,
|
|
},
|
|
);
|
|
|
|
var testResultData = json.decode(response.body);
|
|
if (pageNumber == 1) {
|
|
originalItems = [];
|
|
}
|
|
|
|
for (var item in testResultData['data']) {
|
|
originalItems!.add(
|
|
PackageResultObject(
|
|
createdAt: item['created_at'],
|
|
id: item['id'].toString(),
|
|
name: item['name'],
|
|
status: item['status']),
|
|
);
|
|
}
|
|
pageNumber += 1;
|
|
search('');
|
|
}
|
|
|
|
Future<PackageDetailObject> showPackageResult(String id) async {
|
|
var response =
|
|
await http.get(Uri.parse(Url.getPackageResult(id)), headers: {
|
|
'Authorization': User.token,
|
|
});
|
|
print(response.statusCode);
|
|
print("body : ${response.body}");
|
|
if (response.statusCode != 200) {
|
|
return PackageDetailObject(
|
|
id: 'null',
|
|
name: 'null',
|
|
description: 'null',
|
|
createDate: '2020-07-14 13:50:42',
|
|
hospitalName: 'null',
|
|
// udi: 'null',
|
|
items: [],
|
|
userName: 'null',
|
|
userCreator: 'null',
|
|
status: 'null');
|
|
}
|
|
var data = json.decode(response.body)['data'];
|
|
List<String> itemsList = [];
|
|
if (data['items'] != null) {
|
|
for (var item in data['items']) {
|
|
itemsList.add("${item['name']} - ${item['udi']}");
|
|
}
|
|
}
|
|
print(data);
|
|
result = PackageDetailObject(
|
|
id: data['id'].toString(),
|
|
name: data['name'] != null ? data['name'] : '',
|
|
description: data['note'] != null ? data['note'] : '',
|
|
// udi: data['package_id'] != null ? data['package_id'] : '',
|
|
createDate: data['created_at'] != null ? data['created_at'] : '',
|
|
hospitalName:
|
|
data['user'] != null ? data['user']['medical_center']['name'] : '',
|
|
userName: data['user'] != null ? data['user']['name'] : '',
|
|
userCreator: data['creator'] != null ? data['creator']['name'] : '',
|
|
items: itemsList,
|
|
status: data['status']);
|
|
return result!;
|
|
}
|
|
|
|
Future<bool> exportPackages() async {
|
|
final baseStorage = await getExternalStorageDirectory();
|
|
final taskId = await FlutterDownloader.enqueue(
|
|
url: Url.exportPackages,
|
|
headers: {'Authorization': User.token, "Accept": "*"},
|
|
savedDir: baseStorage!.path,
|
|
showNotification: true,
|
|
openFileFromNotification: true,
|
|
saveInPublicStorage: true,
|
|
fileName: "Packages " + DateTime.now().toString() + ".xlsx",
|
|
);
|
|
print(taskId);
|
|
return true;
|
|
}
|
|
}
|