2020-10-15 03:06:13 +06:30
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
|
|
|
import 'package:fcs/domain/constants.dart';
|
|
|
|
|
import 'package:fcs/domain/entities/cargo_type.dart';
|
|
|
|
|
import 'package:fcs/domain/entities/custom_duty.dart';
|
|
|
|
|
import 'package:fcs/domain/entities/discount_by_weight.dart';
|
|
|
|
|
import 'package:fcs/domain/entities/rate.dart';
|
|
|
|
|
import 'package:fcs/helpers/api_helper.dart';
|
|
|
|
|
import 'package:fcs/helpers/firebase_helper.dart';
|
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
|
|
class RateDataProvider {
|
|
|
|
|
final log = Logger('RateDataProvider');
|
|
|
|
|
|
|
|
|
|
static final RateDataProvider instance = RateDataProvider._();
|
|
|
|
|
RateDataProvider._();
|
|
|
|
|
|
2021-09-10 14:27:38 +06:30
|
|
|
late StreamController<Rate> controller;
|
2020-10-15 03:06:13 +06:30
|
|
|
static Rate _rate = Rate();
|
|
|
|
|
|
|
|
|
|
Stream<Rate> _rateStream() async* {
|
2021-09-10 14:27:38 +06:30
|
|
|
Stream<DocumentSnapshot> snapshot = FirebaseFirestore.instance
|
2020-10-15 03:06:13 +06:30
|
|
|
.collection(config_collection)
|
2021-09-10 14:27:38 +06:30
|
|
|
.doc(rate_doc_id)
|
2020-10-15 03:06:13 +06:30
|
|
|
.snapshots();
|
|
|
|
|
await for (var snap in snapshot) {
|
2021-09-10 14:27:38 +06:30
|
|
|
Rate rate = Rate.fromMap(snap.data() as Map<String, dynamic>);
|
2020-10-15 03:06:13 +06:30
|
|
|
yield rate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<List<CargoType>> _cargoTypeStream() async* {
|
|
|
|
|
List<CargoType> cargoTypes = [];
|
2021-09-10 14:27:38 +06:30
|
|
|
Stream<QuerySnapshot> snapshots = FirebaseFirestore.instance
|
2020-10-15 03:06:13 +06:30
|
|
|
.collection(config_collection)
|
2021-09-10 14:27:38 +06:30
|
|
|
.doc(rate_doc_id)
|
2020-10-15 03:06:13 +06:30
|
|
|
.collection(cargo_types_collection)
|
2021-01-08 17:13:51 +06:30
|
|
|
.where("custom_duty", isEqualTo: false)
|
2020-10-15 03:06:13 +06:30
|
|
|
.snapshots();
|
|
|
|
|
|
|
|
|
|
await for (var snaps in snapshots) {
|
|
|
|
|
cargoTypes = [];
|
2021-09-10 14:27:38 +06:30
|
|
|
cargoTypes = snaps.docs.map((snap) {
|
|
|
|
|
return CargoType.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
2020-10-15 03:06:13 +06:30
|
|
|
}).toList();
|
|
|
|
|
|
|
|
|
|
yield cargoTypes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-08 17:13:51 +06:30
|
|
|
Stream<List<CargoType>> _customDutiesStream() async* {
|
|
|
|
|
List<CargoType> customDuries = [];
|
2021-09-10 14:27:38 +06:30
|
|
|
Stream<QuerySnapshot> snapshots = FirebaseFirestore.instance
|
2020-10-15 03:06:13 +06:30
|
|
|
.collection(config_collection)
|
2021-09-10 14:27:38 +06:30
|
|
|
.doc(rate_doc_id)
|
2021-01-08 17:13:51 +06:30
|
|
|
.collection(cargo_types_collection)
|
|
|
|
|
.where("custom_duty", isEqualTo: true)
|
2020-10-15 03:06:13 +06:30
|
|
|
.snapshots();
|
|
|
|
|
|
|
|
|
|
await for (var snaps in snapshots) {
|
|
|
|
|
customDuries = [];
|
2021-09-10 14:27:38 +06:30
|
|
|
customDuries = snaps.docs.map((snap) {
|
|
|
|
|
return CargoType.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
2020-10-15 03:06:13 +06:30
|
|
|
}).toList();
|
|
|
|
|
yield customDuries;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stream<List<DiscountByWeight>> _discountByWeightStream() async* {
|
|
|
|
|
List<DiscountByWeight> discountByWeight = [];
|
2021-09-10 14:27:38 +06:30
|
|
|
Stream<QuerySnapshot> snapshots = FirebaseFirestore.instance
|
2020-10-15 03:06:13 +06:30
|
|
|
.collection(config_collection)
|
2021-09-10 14:27:38 +06:30
|
|
|
.doc(rate_doc_id)
|
2020-10-15 03:06:13 +06:30
|
|
|
.collection(discounts_by_weights_collection)
|
|
|
|
|
.snapshots();
|
|
|
|
|
|
|
|
|
|
await for (var snaps in snapshots) {
|
|
|
|
|
discountByWeight = [];
|
2021-09-10 14:27:38 +06:30
|
|
|
discountByWeight = snaps.docs.map((snap) {
|
|
|
|
|
return DiscountByWeight.fromMap(
|
|
|
|
|
snap.data() as Map<String, dynamic>, snap.id);
|
2020-10-15 03:06:13 +06:30
|
|
|
}).toList();
|
|
|
|
|
yield discountByWeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 14:27:38 +06:30
|
|
|
late StreamSubscription<Rate> rateListener;
|
|
|
|
|
late StreamSubscription<List<CargoType>> cargoListener;
|
|
|
|
|
late StreamSubscription<List<CargoType>> customListener;
|
|
|
|
|
late StreamSubscription<List<DiscountByWeight>> discountListener;
|
2020-10-15 03:06:13 +06:30
|
|
|
Stream<Rate> rate() {
|
|
|
|
|
Future<void> _start() async {
|
|
|
|
|
rateListener = _rateStream().listen((rate) {
|
|
|
|
|
_rate.deliveryFee = rate.deliveryFee;
|
|
|
|
|
_rate.freeDeliveryWeight = rate.freeDeliveryWeight;
|
|
|
|
|
_rate.volumetricRatio = rate.volumetricRatio;
|
2021-01-04 17:19:01 +06:30
|
|
|
_rate.diffDiscountWeight = rate.diffDiscountWeight;
|
|
|
|
|
_rate.diffWeightRate = rate.diffWeightRate;
|
2020-10-15 03:06:13 +06:30
|
|
|
controller.add(_rate);
|
|
|
|
|
});
|
|
|
|
|
cargoListener = _cargoTypeStream().listen((cargoTypes) {
|
|
|
|
|
_rate.cargoTypes = cargoTypes;
|
|
|
|
|
controller.add(_rate);
|
|
|
|
|
});
|
|
|
|
|
customListener = _customDutiesStream().listen((customDuties) {
|
|
|
|
|
_rate.customDuties = customDuties;
|
|
|
|
|
controller.add(_rate);
|
|
|
|
|
});
|
|
|
|
|
discountListener = _discountByWeightStream().listen((discountByWeights) {
|
|
|
|
|
_rate.discountByWeights = discountByWeights;
|
|
|
|
|
controller.add(_rate);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _stop() {
|
|
|
|
|
if (rateListener != null) {
|
|
|
|
|
rateListener.cancel();
|
|
|
|
|
}
|
|
|
|
|
if (cargoListener != null) {
|
|
|
|
|
cargoListener.cancel();
|
|
|
|
|
}
|
|
|
|
|
if (customListener != null) {
|
|
|
|
|
customListener.cancel();
|
|
|
|
|
}
|
|
|
|
|
if (discountListener != null) {
|
|
|
|
|
discountListener.cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
controller = StreamController<Rate>(
|
|
|
|
|
onListen: _start, onPause: _stop, onResume: _start, onCancel: _stop);
|
|
|
|
|
|
|
|
|
|
return controller.stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> updateRates(Rate rate) async {
|
|
|
|
|
return await requestAPI("/rates", "PUT",
|
|
|
|
|
payload: rate.toMap(), token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> createCargoType(CargoType cargoType) async {
|
|
|
|
|
return await requestAPI("/cargo_types", "POST",
|
|
|
|
|
payload: cargoType.toMap(), token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> updateCargoType(CargoType cargoType) async {
|
|
|
|
|
return await requestAPI("/cargo_types", "PUT",
|
|
|
|
|
payload: cargoType.toMap(), token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deleteCargoType(String id) async {
|
|
|
|
|
return await requestAPI("/cargo_types", "DELETE",
|
|
|
|
|
payload: {"id": id}, token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> createCustomDuty(CustomDuty customDuty) async {
|
|
|
|
|
return await requestAPI("/custom_duties", "POST",
|
|
|
|
|
payload: customDuty.toMap(), token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> updateCustomDuty(CustomDuty customDuty) async {
|
|
|
|
|
return await requestAPI("/custom_duties", "PUT",
|
|
|
|
|
payload: customDuty.toMap(), token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deleteCustomDuty(String id) async {
|
2020-10-15 15:05:07 +06:30
|
|
|
return await requestAPI("/custom_duties", "DELETE",
|
2020-10-15 03:06:13 +06:30
|
|
|
payload: {"id": id}, token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> createDiscountByWeight(DiscountByWeight discountByWeight) async {
|
|
|
|
|
return await requestAPI("/discounts_by_weight", "POST",
|
|
|
|
|
payload: discountByWeight.toMap(), token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> updateDiscountByWeight(DiscountByWeight discountByWeight) async {
|
|
|
|
|
return await requestAPI("/discounts_by_weight", "PUT",
|
|
|
|
|
payload: discountByWeight.toMap(), token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> deleteDiscountByWeight(String id) async {
|
2020-10-15 15:05:07 +06:30
|
|
|
return await requestAPI("/discounts_by_weight", "DELETE",
|
2020-10-15 03:06:13 +06:30
|
|
|
payload: {"id": id}, token: await getToken());
|
|
|
|
|
}
|
|
|
|
|
}
|