add structure
This commit is contained in:
258
lib/model/do_model.dart
Normal file
258
lib/model/do_model.dart
Normal file
@@ -0,0 +1,258 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:path/path.dart' as Path;
|
||||
import 'package:fcs/model/api_helper.dart';
|
||||
import 'package:fcs/model/constants.dart';
|
||||
import 'package:fcs/pages/do/do_files.dart';
|
||||
import 'package:fcs/vo/do.dart';
|
||||
import 'package:fcs/vo/popup_menu.dart';
|
||||
|
||||
import 'base_model.dart';
|
||||
import 'firebase_helper.dart';
|
||||
|
||||
class DOModel extends BaseModel {
|
||||
StreamSubscription<QuerySnapshot> listener;
|
||||
|
||||
List<DOSubmission> dos = [];
|
||||
PopupMenu popupMenu = new PopupMenu(index: 0);
|
||||
int dateIndex = 0;
|
||||
DateTime selectedDate = DateTime.now();
|
||||
int timber = 0;
|
||||
|
||||
void initUser(user) async {
|
||||
super.initUser(user);
|
||||
_loadDOs();
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
if (listener != null) await listener.cancel();
|
||||
dos = [];
|
||||
}
|
||||
|
||||
Future<void> _loadDOs() async {
|
||||
String path;
|
||||
if (user.hasDO() || user.isOwnerAndAbove()) {
|
||||
path = "/$biz_collection/${setting.okEnergyId}/$dos_collection";
|
||||
}
|
||||
if (user.isBuyer()) {
|
||||
path =
|
||||
"/$biz_collection/${setting.okEnergyId}/$buyer_collection/${user.docID}/$dos_collection";
|
||||
}
|
||||
var startDate = new DateTime(
|
||||
selectedDate.year, selectedDate.month, selectedDate.day, 0, 0, 0);
|
||||
var endDate = new DateTime(
|
||||
selectedDate.year, selectedDate.month, selectedDate.day, 23, 59, 59);
|
||||
|
||||
listener =
|
||||
getFilterDateSnapshot(path, 'do_date', startDate, endDate, 'do_number')
|
||||
.listen((snaps) async {
|
||||
dos.clear();
|
||||
snaps.documents.forEach((d) {
|
||||
dos.add(DOSubmission.fromMap(d.data, d.documentID));
|
||||
});
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<DOSubmission> loadDOPOLines(DOSubmission doSub) async {
|
||||
String path =
|
||||
"/$biz_collection/${setting.okEnergyId}/$dos_collection/${doSub.id}/$do_po_lines_collection";
|
||||
if (user.isBuyer()) {
|
||||
path =
|
||||
"/$biz_collection/${setting.okEnergyId}/$buyer_collection/${user.docID}/$dos_collection/${doSub.id}/$do_po_lines_collection";
|
||||
}
|
||||
var snaps = await getSnapshot(path);
|
||||
doSub.dopoLies =
|
||||
snaps.documents.map((s) => DOPOLine.fromMap(s.data)).toList();
|
||||
return doSub;
|
||||
}
|
||||
|
||||
Future<DOSubmission> getDO(String id) async {
|
||||
String path = "/$biz_collection/${setting.okEnergyId}/$dos_collection";
|
||||
if (user.isBuyer()) {
|
||||
path =
|
||||
"/$biz_collection/${setting.okEnergyId}/$buyer_collection/${user.docID}/$dos_collection";
|
||||
}
|
||||
var doSnap = await getDocSnap(path, id);
|
||||
return DOSubmission.fromMap(doSnap.data, doSnap.documentID);
|
||||
}
|
||||
|
||||
Future<void> createDO(DOSubmission doSubmission, DOFiles files) async {
|
||||
doSubmission.userID = user.docID;
|
||||
String path = Path.join(do_files_path, user.docID);
|
||||
|
||||
if (files.storageChargeFile != null) {
|
||||
String url = await uploadStorage(path, files.storageChargeFile);
|
||||
doSubmission.storageReceiptUrl = url;
|
||||
}
|
||||
if (files.licenseFile != null) {
|
||||
String url = await uploadStorage(path, files.licenseFile);
|
||||
doSubmission.driverLicenceUrl = url;
|
||||
}
|
||||
|
||||
await request("/do", "POST",
|
||||
payload: doSubmission.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> updateDO(DOSubmission doSubmission, DOFiles files) async {
|
||||
String path = Path.join(do_files_path, user.docID);
|
||||
if (files.storageFileChanged) {
|
||||
if (doSubmission.storageReceiptUrl != null &&
|
||||
doSubmission.storageReceiptUrl != '') {
|
||||
await deleteStorageFromUrl(doSubmission.storageReceiptUrl);
|
||||
}
|
||||
doSubmission.storageReceiptUrl = null;
|
||||
String url = await uploadStorage(path, files.storageChargeFile);
|
||||
doSubmission.storageReceiptUrl = url;
|
||||
}
|
||||
|
||||
if (files.licenseFileChanged) {
|
||||
if (doSubmission.driverLicenceUrl != null &&
|
||||
doSubmission.driverLicenceUrl != '') {
|
||||
await deleteStorageFromUrl(doSubmission.driverLicenceUrl);
|
||||
}
|
||||
doSubmission.driverLicenceUrl = null;
|
||||
String url = await uploadStorage(path, files.licenseFile);
|
||||
doSubmission.driverLicenceUrl = url;
|
||||
}
|
||||
await request("/do", "PUT",
|
||||
payload: doSubmission.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> approveDO(DOSubmission doObj) async {
|
||||
await request("/do/approved", "POST",
|
||||
payload: doObj.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> rejectDO(DOSubmission doObj) async {
|
||||
await request("/do/rejected", "POST",
|
||||
payload: doObj.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> cancelDO(DOSubmission doObj) async {
|
||||
await request("/do/canceled", "POST",
|
||||
payload: doObj.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> initDelivery(DOSubmission doObj) async {
|
||||
String path = Path.join(do_files_path, user.docID);
|
||||
String imgUrl =
|
||||
await uploadStorage(path, doObj.driverImg, fileName: doObj.id);
|
||||
doObj.driverImgUrl = imgUrl;
|
||||
await request("/do/initiated", "POST",
|
||||
payload: doObj.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> startDelivery(DOSubmission doObj) async {
|
||||
await request("/do/started", "POST",
|
||||
payload: doObj.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> endDelivery(DOSubmission doObj, Uint8List img) async {
|
||||
String path = Path.join(do_files_path, user.docID);
|
||||
String imgUrl = await uploadStorageData(path, img, fileName: doObj.id);
|
||||
doObj.doReceiptUrl = imgUrl;
|
||||
await request("/do/ended", "POST",
|
||||
payload: doObj.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
void filterData(
|
||||
String status, DateTime dateTime, int _selectedIndex, int _dateIndex) {
|
||||
dos.clear();
|
||||
if (listener != null) {
|
||||
listener.cancel();
|
||||
}
|
||||
|
||||
this.popupMenu.index = _selectedIndex;
|
||||
this.dateIndex = _dateIndex;
|
||||
this.selectedDate = dateTime == null
|
||||
? new DateTime(
|
||||
selectedDate.year, selectedDate.month, selectedDate.day, 0, 0, 0)
|
||||
: dateTime;
|
||||
|
||||
String path = "/$biz_collection/${setting.okEnergyId}/$dos_collection";
|
||||
if (user.isBuyer()) {
|
||||
path =
|
||||
"/$biz_collection/${setting.okEnergyId}/$buyer_collection/${user.docID}/$dos_collection";
|
||||
}
|
||||
|
||||
if (status != null && dateTime == null) {
|
||||
listener = getFilterStatusSnapshot(path, status, 'do_number')
|
||||
.listen((snaps) async {
|
||||
dos.clear();
|
||||
snaps.documents.forEach((d) {
|
||||
dos.add(DOSubmission.fromMap(d.data, d.documentID));
|
||||
});
|
||||
notifyListeners();
|
||||
});
|
||||
} else if (dateTime != null && status == null) {
|
||||
var endDate =
|
||||
new DateTime(dateTime.year, dateTime.month, dateTime.day, 23, 59, 59);
|
||||
listener =
|
||||
getFilterDateSnapshot(path, 'do_date', dateTime, endDate, 'do_number')
|
||||
.listen((snaps) async {
|
||||
dos.clear();
|
||||
snaps.documents.forEach((d) {
|
||||
dos.add(DOSubmission.fromMap(d.data, d.documentID));
|
||||
});
|
||||
notifyListeners();
|
||||
});
|
||||
} else if (status != null && dateTime != null) {
|
||||
var endDate =
|
||||
new DateTime(dateTime.year, dateTime.month, dateTime.day, 23, 59, 59);
|
||||
listener = getFilterDataSnapshot(
|
||||
path, status, 'do_date', dateTime, endDate, 'do_number')
|
||||
.listen((snaps) async {
|
||||
dos.clear();
|
||||
snaps.documents.forEach((d) {
|
||||
dos.add(DOSubmission.fromMap(d.data, d.documentID));
|
||||
});
|
||||
notifyListeners();
|
||||
});
|
||||
} else {
|
||||
listener =
|
||||
getQuerySnapshotByOrder(path, 'do_number').listen((snaps) async {
|
||||
dos.clear();
|
||||
snaps.documents.forEach((d) {
|
||||
dos.add(DOSubmission.fromMap(d.data, d.documentID));
|
||||
});
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
addTimber(int count) {
|
||||
timber = count;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<List<DOSubmission>> getDOForDelivery(DateTime dateTime) async {
|
||||
List<DOSubmission> dos = [];
|
||||
|
||||
String path = "/$biz_collection/${setting.okEnergyId}/$dos_collection";
|
||||
if (user.isBuyer()) {
|
||||
path =
|
||||
"/$biz_collection/${setting.okEnergyId}/$buyer_collection/${user.docID}/$dos_collection";
|
||||
}
|
||||
DateTime date = DateTime(dateTime.year, dateTime.month, dateTime.day);
|
||||
DateTime dateAddOne = date.add(Duration(days: 1));
|
||||
|
||||
QuerySnapshot snapshots = await Firestore.instance
|
||||
.collection(path)
|
||||
.where("status", isEqualTo: do_approved_status)
|
||||
.where("delivery_date", isGreaterThanOrEqualTo: date)
|
||||
.where("delivery_date", isLessThan: dateAddOne)
|
||||
.orderBy("delivery_date").orderBy("user_name")
|
||||
.limit(100)
|
||||
.getDocuments();
|
||||
snapshots.documents.forEach((d) {
|
||||
dos.add(DOSubmission.fromMap(d.data, d.documentID));
|
||||
});
|
||||
return dos;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user