89 lines
2.5 KiB
Dart
89 lines
2.5 KiB
Dart
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
||
|
|
import 'package:logging/logging.dart';
|
||
|
|
import 'package:fcs/model/constants.dart';
|
||
|
|
import 'package:fcs/vo/pd.dart';
|
||
|
|
import 'package:fcs/vo/user.dart';
|
||
|
|
|
||
|
|
import 'base_model.dart';
|
||
|
|
import 'firebase_helper.dart';
|
||
|
|
|
||
|
|
class PDModel extends BaseModel {
|
||
|
|
final log = Logger('PDModel');
|
||
|
|
|
||
|
|
List<PD> pds = [];
|
||
|
|
int dateIndex = 0;
|
||
|
|
DateTime selectedDate = DateTime.now();
|
||
|
|
|
||
|
|
void initUser(User user) async {
|
||
|
|
super.initUser(user);
|
||
|
|
loadPDs();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
@override
|
||
|
|
logout() async {
|
||
|
|
pds = [];
|
||
|
|
}
|
||
|
|
|
||
|
|
loadPDs() {
|
||
|
|
if (!user.isOwnerAndAbove() && !user.hasInventory()) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
String path = "/$biz_collection/${setting.okEnergyId}/$pds_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);
|
||
|
|
pds.clear();
|
||
|
|
getFilterDateSnapshot(path, 'date', startDate, endDate, 'pd_number')
|
||
|
|
.listen((QuerySnapshot snapshot) {
|
||
|
|
pds = snapshot.documents.map((documentSnapshot) {
|
||
|
|
var data =
|
||
|
|
PD.fromMap(documentSnapshot.data, documentSnapshot.documentID);
|
||
|
|
return data;
|
||
|
|
}).toList();
|
||
|
|
notifyListeners();
|
||
|
|
}).onError((e) {
|
||
|
|
log.warning("Error! $e");
|
||
|
|
});
|
||
|
|
} catch (e) {
|
||
|
|
log.warning("Error!! $e");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<PD> loadPDLines(PD pd) async {
|
||
|
|
var snaps = await getSnapshot(
|
||
|
|
"/$biz_collection/${setting.okEnergyId}/$pds_collection/${pd.id}/$product_collection");
|
||
|
|
pd.pdLines = snaps.documents.map((s) => PDLine.fromMap(s.data)).toList();
|
||
|
|
return pd;
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<void> createPD(PD pd) async {
|
||
|
|
await request("/pd", "POST",
|
||
|
|
payload: pd.toMap(), token: await getToken());
|
||
|
|
}
|
||
|
|
|
||
|
|
void filterDate(DateTime dateTime, int _dateIndex) {
|
||
|
|
this.selectedDate = dateTime;
|
||
|
|
this.dateIndex = _dateIndex;
|
||
|
|
String path = "/$biz_collection/${setting.okEnergyId}/$pds_collection";
|
||
|
|
var endDate =
|
||
|
|
new DateTime(dateTime.year, dateTime.month, dateTime.day, 23, 59, 59);
|
||
|
|
pds.clear();
|
||
|
|
getFilterDateSnapshot(path, 'date', dateTime, endDate, 'pd_number').listen(
|
||
|
|
(snaps) {
|
||
|
|
pds = snaps.documents.map((documentSnapshot) {
|
||
|
|
var data =
|
||
|
|
PD.fromMap(documentSnapshot.data, documentSnapshot.documentID);
|
||
|
|
return data;
|
||
|
|
}).toList();
|
||
|
|
notifyListeners();
|
||
|
|
}, onError: (error) {
|
||
|
|
log.warning("FIRESTORE ERROR>>$error");
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|