Added Streams to TourRepository
This commit is contained in:
@ -1,49 +1,130 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_event.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/overview/bloc/tour_state.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/overview/repository/tour_repository.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/overview/service/distance_service.dart';
|
||||
import 'package:hl_lieferservice/model/delivery.dart';
|
||||
import 'package:hl_lieferservice/model/tour.dart';
|
||||
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
|
||||
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
class TourBloc extends Bloc<TourEvent, TourState> {
|
||||
OperationBloc opBloc;
|
||||
TourRepository tourRepository;
|
||||
StreamSubscription? _combinedSubscription;
|
||||
|
||||
TourBloc({required this.opBloc, required this.tourRepository})
|
||||
: super(TourInitial()) {
|
||||
_combinedSubscription = CombineLatestStream.combine2(
|
||||
tourRepository.tour,
|
||||
tourRepository.paymentOptions,
|
||||
(tour, payments) => {'tour': tour, 'payments': payments},
|
||||
).listen((combined) {
|
||||
final tour = combined['tour'] as Tour?;
|
||||
final payments = combined['payments'] as List<Payment>;
|
||||
|
||||
if (tour == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
add(TourUpdated(tour: tour, payments: payments));
|
||||
});
|
||||
|
||||
on<LoadTour>(_load);
|
||||
on<UpdateTour>(_update);
|
||||
on<AssignCarEvent>(_assignCar);
|
||||
on<IncrementArticleScanAmount>(_increment);
|
||||
on<ScanArticleEvent>(_scan);
|
||||
on<HoldDeliveryEvent>(_holdDelivery);
|
||||
on<CancelDeliveryEvent>(_cancelDelivery);
|
||||
on<ReactivateDeliveryEvent>(_reactiveateDelivery);
|
||||
on<ReactivateDeliveryEvent>(_reactivateDelivery);
|
||||
on<UnscanArticleEvent>(_unscan);
|
||||
on<ResetScanAmountEvent>(_resetAmount);
|
||||
on<AddDiscountEvent>(_addDiscount);
|
||||
on<RemoveDiscountEvent>(_removeDiscount);
|
||||
on<UpdateDiscountEvent>(_updateDiscount);
|
||||
on<UpdateDeliveryOptionEvent>(_updateDeliveryOptions);
|
||||
on<UpdateSelectedPaymentMethodEvent>(_updatePayment);
|
||||
on<FinishDeliveryEvent>(_finishDelivery);
|
||||
on<TourUpdated>(_updated);
|
||||
on<RequestDeliveryDistanceEvent>(_calculateDistances);
|
||||
}
|
||||
|
||||
void _reactiveateDelivery(
|
||||
ReactivateDeliveryEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
@override
|
||||
Future<void> close() {
|
||||
_combinedSubscription?.cancel();
|
||||
|
||||
return super.close();
|
||||
}
|
||||
|
||||
void _calculateDistances(
|
||||
RequestDeliveryDistanceEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
Map<String, double> distances = {};
|
||||
opBloc.add(LoadOperation());
|
||||
emit(TourRequestingDistances(tour: event.tour, payments: event.payments));
|
||||
|
||||
try {
|
||||
for (final delivery in event.tour.deliveries) {
|
||||
distances[delivery.id] = await DistanceService.getDistanceByRoad(
|
||||
delivery.customer.address.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e) {
|
||||
debugPrint("Fehler beim Berechnen der Distanzen: $e");
|
||||
opBloc.add(FailOperation(message: "Fehler beim Berechnen der Distanzen"));
|
||||
return;
|
||||
}
|
||||
|
||||
emit(
|
||||
TourLoaded(
|
||||
tour: event.tour,
|
||||
paymentOptions: event.payments,
|
||||
distances: distances,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _updated(TourUpdated event, Emitter<TourState> emit) {
|
||||
final currentState = state;
|
||||
final tour = event.tour.copyWith();
|
||||
final payments =
|
||||
event.payments.map((payment) => payment.copyWith()).toList();
|
||||
|
||||
if (currentState is TourLoaded) {
|
||||
debugPrint("TEST UPDATE");
|
||||
emit(
|
||||
TourLoaded(
|
||||
tour: tour,
|
||||
paymentOptions: payments,
|
||||
distances: Map<String, double>.from(currentState.distances ?? {}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (currentState is TourLoading) {
|
||||
add(
|
||||
RequestDeliveryDistanceEvent(tour: tour.copyWith(), payments: payments),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _reactivateDelivery(
|
||||
ReactivateDeliveryEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
final currentState = state;
|
||||
if (currentState is TourLoaded) {
|
||||
opBloc.add(LoadOperation());
|
||||
try {
|
||||
Tour tourCopied = currentState.tour.copyWith();
|
||||
Delivery delivery = tourCopied.deliveries.firstWhere((delivery) => delivery.id == event.deliveryId);
|
||||
delivery.state = DeliveryState.ongoing;
|
||||
|
||||
await tourRepository.updateDelivery(
|
||||
delivery,
|
||||
);
|
||||
await tourRepository.reactivateDelivery(event.deliveryId);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
|
||||
emit(TourLoaded(tour: tourCopied, distances: currentState.distances));
|
||||
} catch (e, st) {
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
@ -54,25 +135,14 @@ class TourBloc extends Bloc<TourEvent, TourState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _holdDelivery(
|
||||
HoldDeliveryEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
void _holdDelivery(HoldDeliveryEvent event, Emitter<TourState> emit) async {
|
||||
final currentState = state;
|
||||
if (currentState is TourLoaded) {
|
||||
opBloc.add(LoadOperation());
|
||||
try {
|
||||
Tour tourCopied = currentState.tour.copyWith();
|
||||
Delivery delivery = tourCopied.deliveries.firstWhere((delivery) => delivery.id == event.deliveryId);
|
||||
delivery.state = DeliveryState.onhold;
|
||||
|
||||
await tourRepository.updateDelivery(
|
||||
delivery,
|
||||
);
|
||||
await tourRepository.holdDelivery(event.deliveryId);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
|
||||
emit(TourLoaded(tour: tourCopied, distances: currentState.distances));
|
||||
} catch (e, st) {
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
@ -83,22 +153,17 @@ class TourBloc extends Bloc<TourEvent, TourState> {
|
||||
}
|
||||
}
|
||||
|
||||
void _cancelDelivery(CancelDeliveryEvent event, Emitter<TourState> emit) async {
|
||||
void _cancelDelivery(
|
||||
CancelDeliveryEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
final currentState = state;
|
||||
if (currentState is TourLoaded) {
|
||||
opBloc.add(LoadOperation());
|
||||
try {
|
||||
Tour tourCopied = currentState.tour.copyWith();
|
||||
Delivery delivery = tourCopied.deliveries.firstWhere((delivery) => delivery.id == event.deliveryId);
|
||||
delivery.state = DeliveryState.canceled;
|
||||
|
||||
await tourRepository.updateDelivery(
|
||||
delivery,
|
||||
);
|
||||
await tourRepository.cancelDelivery(event.deliveryId);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
|
||||
emit(TourLoaded(tour: tourCopied, distances: currentState.distances));
|
||||
} catch (e, st) {
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
@ -115,54 +180,33 @@ class TourBloc extends Bloc<TourEvent, TourState> {
|
||||
|
||||
if (currentState is TourLoaded) {
|
||||
try {
|
||||
if (currentState.tour.deliveries.any(
|
||||
(delivery) => delivery.articles.any(
|
||||
(article) => article.articleNumber == event.articleNumber,
|
||||
),
|
||||
switch (await tourRepository.scanArticle(
|
||||
event.deliveryId,
|
||||
event.carId,
|
||||
event.articleNumber,
|
||||
)) {
|
||||
var tourCopied = currentState.tour.copyWith();
|
||||
var delivery = tourCopied.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == event.deliveryId,
|
||||
);
|
||||
var article = delivery.articles.firstWhere(
|
||||
(article) => article.articleNumber == event.articleNumber,
|
||||
);
|
||||
|
||||
await tourRepository.scanArticle(article.internalId.toString());
|
||||
|
||||
if (article.scannedAmount < article.amount) {
|
||||
article.scannedAmount += 1;
|
||||
|
||||
emit(TourLoaded(tour: tourCopied, distances: currentState.distances));
|
||||
opBloc.add(FinishOperation(message: '${article.name} gescannt'));
|
||||
} else {
|
||||
case ScanResult.scanned:
|
||||
opBloc.add(FinishOperation(message: 'Artikel gescannt'));
|
||||
break;
|
||||
case ScanResult.alreadyScanned:
|
||||
opBloc.add(
|
||||
FailOperation(message: 'Artikel wurde bereits gescannt'),
|
||||
);
|
||||
break;
|
||||
case ScanResult.notFound:
|
||||
opBloc.add(
|
||||
FailOperation(
|
||||
message: 'Alle ${article.name} wurden bereits gescannt',
|
||||
message: 'Artikel ist für keine Lieferung vorgesehen',
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
opBloc.add(
|
||||
FailOperation(
|
||||
message: 'Fehler: Artikel ist für keine Lieferung vorgesehen',
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
} catch (e, st) {
|
||||
debugPrint(st.toString());
|
||||
opBloc.add(FailOperation(message: "Fehler beim Scannnen des Artikels"));
|
||||
} on TourNotFoundException catch (e) {
|
||||
opBloc.add(FailOperation(message: "Fehler beim Scannen des Artikels"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _update(UpdateTour event, Emitter<TourState> emit) async {
|
||||
final currentState = state;
|
||||
if (currentState is TourLoaded) {
|
||||
emit(TourLoaded(tour: event.tour, distances: currentState.distances));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _increment(
|
||||
IncrementArticleScanAmount event,
|
||||
Emitter<TourState> emit,
|
||||
@ -170,62 +214,27 @@ class TourBloc extends Bloc<TourEvent, TourState> {
|
||||
final currentState = state;
|
||||
|
||||
if (currentState is TourLoaded) {
|
||||
var deliveryCopied = currentState.tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == event.deliveryId,
|
||||
);
|
||||
var articleCopied = deliveryCopied.articles.firstWhere(
|
||||
(article) => article.internalId == int.parse(event.internalArticleId),
|
||||
);
|
||||
articleCopied.scannedAmount += 1;
|
||||
|
||||
emit(
|
||||
TourLoaded(
|
||||
tour: currentState.tour.copyWith(
|
||||
deliveries:
|
||||
currentState.tour.deliveries.map((delivery) {
|
||||
if (delivery.id == event.deliveryId) {
|
||||
return deliveryCopied;
|
||||
}
|
||||
|
||||
return delivery;
|
||||
}).toList(),
|
||||
),
|
||||
distances: currentState.distances
|
||||
),
|
||||
);
|
||||
opBloc.add(LoadOperation());
|
||||
try {
|
||||
await tourRepository.scanArticle(
|
||||
event.deliveryId,
|
||||
event.carId,
|
||||
event.internalArticleId,
|
||||
);
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint(st.toString());
|
||||
opBloc.add(FailOperation(message: "Fehler beim Scannen des Artikels"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _assignCar(AssignCarEvent event, Emitter<TourState> emit) async {
|
||||
final currentState = state;
|
||||
|
||||
if (currentState is TourLoaded) {
|
||||
opBloc.add(LoadOperation());
|
||||
var copiedTour = currentState.tour.copyWith();
|
||||
var delivery = copiedTour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == event.deliveryId,
|
||||
);
|
||||
|
||||
try {
|
||||
await tourRepository.assignCar(event.deliveryId, event.carId);
|
||||
delivery.carId = int.parse(event.carId);
|
||||
|
||||
emit(
|
||||
TourLoaded(
|
||||
tour: copiedTour.copyWith(
|
||||
deliveries:
|
||||
copiedTour.deliveries.map((d) {
|
||||
if (d.id == delivery.id) {
|
||||
return delivery;
|
||||
}
|
||||
|
||||
return d;
|
||||
}).toList(),
|
||||
),
|
||||
distances: currentState.distances
|
||||
),
|
||||
);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint(st.toString());
|
||||
@ -239,16 +248,10 @@ class TourBloc extends Bloc<TourEvent, TourState> {
|
||||
Future<void> _load(LoadTour event, Emitter<TourState> emit) async {
|
||||
opBloc.add(LoadOperation());
|
||||
try {
|
||||
Tour tour = await tourRepository.loadAll(event.teamId);
|
||||
List<Payment> payments = await tourRepository.loadPaymentOptions();
|
||||
tour.paymentMethods = payments;
|
||||
Map<String, double> distances = {};
|
||||
emit(TourLoading());
|
||||
await tourRepository.loadTourOfToday(event.teamId);
|
||||
await tourRepository.loadPaymentOptions();
|
||||
|
||||
for (final delivery in tour.deliveries) {
|
||||
distances[delivery.id] = await DistanceService.getDistanceByRoad(delivery.customer.address.toString());
|
||||
}
|
||||
|
||||
emit(TourLoaded(tour: tour, distances: distances));
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e) {
|
||||
opBloc.add(
|
||||
@ -256,4 +259,177 @@ class TourBloc extends Bloc<TourEvent, TourState> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _finishDelivery(
|
||||
FinishDeliveryEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
final currentState = state;
|
||||
opBloc.add(LoadOperation());
|
||||
|
||||
if (currentState is TourLoaded) {
|
||||
try {
|
||||
await tourRepository.uploadDriverSignature(
|
||||
event.deliveryId,
|
||||
event.driverSignature,
|
||||
);
|
||||
await tourRepository.uploadCustomerSignature(
|
||||
event.deliveryId,
|
||||
event.customerSignature,
|
||||
);
|
||||
|
||||
await tourRepository.finishDelivery(event.deliveryId);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
opBloc.add(FailOperation(message: "Failed to update delivery"));
|
||||
debugPrint(st.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _updatePayment(
|
||||
UpdateSelectedPaymentMethodEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
try {
|
||||
opBloc.add(LoadOperation());
|
||||
await tourRepository.updatePayment(event.deliveryId, event.payment);
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint(st.toString());
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Aktualisieren des Betrags"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _updateDeliveryOptions(
|
||||
UpdateDeliveryOptionEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
try {
|
||||
opBloc.add(LoadOperation());
|
||||
await tourRepository.updateOption(
|
||||
event.deliveryId,
|
||||
event.key,
|
||||
event.value,
|
||||
);
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint("$st");
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Aktualisieren der Optionen"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _updateDiscount(
|
||||
UpdateDiscountEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
opBloc.add(LoadOperation());
|
||||
|
||||
try {
|
||||
opBloc.add(FinishOperation());
|
||||
await tourRepository.updateDiscount(
|
||||
event.deliveryId,
|
||||
event.reason,
|
||||
event.value,
|
||||
);
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint(
|
||||
"Fehler beim Hinzufügen eins Discounts zur Lieferung: ${event.deliveryId}:",
|
||||
);
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Hinzufügen des Discounts: $e"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _removeDiscount(
|
||||
RemoveDiscountEvent event,
|
||||
Emitter<TourState> emit,
|
||||
) async {
|
||||
opBloc.add(LoadOperation());
|
||||
|
||||
try {
|
||||
await tourRepository.removeDiscount(event.deliveryId);
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint(
|
||||
"Fehler beim Löschen des Discounts der Lieferung: ${event.deliveryId}:",
|
||||
);
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Löschen des Discounts: $e"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _addDiscount(AddDiscountEvent event, Emitter<TourState> emit) async {
|
||||
opBloc.add(LoadOperation());
|
||||
|
||||
try {
|
||||
await tourRepository.addDiscount(
|
||||
event.deliveryId,
|
||||
event.reason,
|
||||
event.value,
|
||||
);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint(
|
||||
"Fehler beim Hinzufügen eins Discounts zur Lieferung: ${event.deliveryId}:",
|
||||
);
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Hinzufügen des Discounts: $e"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void _unscan(UnscanArticleEvent event, Emitter<TourState> emit) async {
|
||||
opBloc.add(LoadOperation());
|
||||
|
||||
try {
|
||||
await tourRepository.unscan(
|
||||
event.deliveryId,
|
||||
event.articleId,
|
||||
event.newAmount,
|
||||
event.reason,
|
||||
);
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint("Fehler beim Unscan des Artikels: ${event.articleId}:");
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
|
||||
opBloc.add(FailOperation(message: "Fehler beim Unscan des Artikels: $e"));
|
||||
}
|
||||
}
|
||||
|
||||
void _resetAmount(ResetScanAmountEvent event, Emitter<TourState> emit) async {
|
||||
opBloc.add(LoadOperation());
|
||||
|
||||
try {
|
||||
await tourRepository.resetScan(event.articleId, event.deliveryId);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
} catch (e, st) {
|
||||
debugPrint("Fehler beim Unscan des Artikels: ${event.articleId}:");
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
|
||||
opBloc.add(FailOperation(message: "Fehler beim Zurücksetzen: $e"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:hl_lieferservice/model/tour.dart';
|
||||
|
||||
import '../../../../model/delivery.dart';
|
||||
|
||||
abstract class TourEvent {}
|
||||
|
||||
class LoadTour extends TourEvent {
|
||||
@ -8,6 +12,26 @@ class LoadTour extends TourEvent {
|
||||
LoadTour({required this.teamId});
|
||||
}
|
||||
|
||||
class RequestDeliveryDistanceEvent extends TourEvent {
|
||||
Tour tour;
|
||||
List<Payment> payments;
|
||||
|
||||
RequestDeliveryDistanceEvent({required this.tour, required this.payments});
|
||||
}
|
||||
|
||||
class TourUpdated extends TourEvent {
|
||||
Tour tour;
|
||||
List<Payment> payments;
|
||||
|
||||
TourUpdated({required this.tour, required this.payments});
|
||||
}
|
||||
|
||||
class PaymentOptionsUpdated extends TourEvent {
|
||||
List<Payment> options;
|
||||
|
||||
PaymentOptionsUpdated({required this.options});
|
||||
}
|
||||
|
||||
class UpdateTour extends TourEvent {
|
||||
Tour tour;
|
||||
|
||||
@ -24,8 +48,9 @@ class AssignCarEvent extends TourEvent {
|
||||
class IncrementArticleScanAmount extends TourEvent {
|
||||
String internalArticleId;
|
||||
String deliveryId;
|
||||
String carId;
|
||||
|
||||
IncrementArticleScanAmount({required this.internalArticleId, required this.deliveryId});
|
||||
IncrementArticleScanAmount({required this.internalArticleId, required this.deliveryId, required this.carId});
|
||||
}
|
||||
|
||||
class ScanArticleEvent extends TourEvent {
|
||||
@ -52,4 +77,88 @@ class ReactivateDeliveryEvent extends TourEvent {
|
||||
String deliveryId;
|
||||
|
||||
ReactivateDeliveryEvent({required this.deliveryId});
|
||||
}
|
||||
|
||||
class LoadDeliveryEvent extends TourEvent {
|
||||
LoadDeliveryEvent({required this.delivery});
|
||||
|
||||
Delivery delivery;
|
||||
}
|
||||
|
||||
class UnscanArticleEvent extends TourEvent {
|
||||
UnscanArticleEvent({
|
||||
required this.articleId,
|
||||
required this.newAmount,
|
||||
required this.reason,
|
||||
required this.deliveryId
|
||||
});
|
||||
|
||||
String articleId;
|
||||
String deliveryId;
|
||||
String reason;
|
||||
int newAmount;
|
||||
}
|
||||
|
||||
class ResetScanAmountEvent extends TourEvent {
|
||||
ResetScanAmountEvent({required this.articleId, required this.deliveryId});
|
||||
|
||||
String articleId;
|
||||
String deliveryId;
|
||||
}
|
||||
|
||||
class AddDiscountEvent extends TourEvent {
|
||||
AddDiscountEvent({
|
||||
required this.deliveryId,
|
||||
required this.value,
|
||||
required this.reason,
|
||||
});
|
||||
|
||||
String deliveryId;
|
||||
String reason;
|
||||
int value;
|
||||
}
|
||||
|
||||
class RemoveDiscountEvent extends TourEvent {
|
||||
RemoveDiscountEvent({required this.deliveryId});
|
||||
|
||||
String deliveryId;
|
||||
}
|
||||
|
||||
class UpdateDiscountEvent extends TourEvent {
|
||||
UpdateDiscountEvent({
|
||||
required this.deliveryId,
|
||||
required this.value,
|
||||
required this.reason,
|
||||
});
|
||||
|
||||
String deliveryId;
|
||||
String? reason;
|
||||
int? value;
|
||||
}
|
||||
|
||||
class UpdateDeliveryOptionEvent extends TourEvent {
|
||||
UpdateDeliveryOptionEvent({required this.key, required this.value, required this.deliveryId});
|
||||
|
||||
String deliveryId;
|
||||
String key;
|
||||
dynamic value;
|
||||
}
|
||||
|
||||
class UpdateSelectedPaymentMethodEvent extends TourEvent {
|
||||
UpdateSelectedPaymentMethodEvent({required this.payment, required this.deliveryId});
|
||||
|
||||
Payment payment;
|
||||
String deliveryId;
|
||||
}
|
||||
|
||||
class FinishDeliveryEvent extends TourEvent {
|
||||
FinishDeliveryEvent({
|
||||
required this.deliveryId,
|
||||
required this.driverSignature,
|
||||
required this.customerSignature,
|
||||
});
|
||||
|
||||
String deliveryId;
|
||||
Uint8List customerSignature;
|
||||
Uint8List driverSignature;
|
||||
}
|
||||
@ -6,9 +6,33 @@ class TourInitial extends TourState {}
|
||||
|
||||
class TourLoading extends TourState {}
|
||||
|
||||
class TourRequestingDistances extends TourState {
|
||||
Tour tour;
|
||||
List<Payment> payments;
|
||||
|
||||
TourRequestingDistances({required this.tour, required this.payments});
|
||||
}
|
||||
|
||||
class TourLoaded extends TourState {
|
||||
Tour tour;
|
||||
Map<String, double> distances;
|
||||
Map<String, double>? distances;
|
||||
List<Payment> paymentOptions;
|
||||
|
||||
TourLoaded({required this.tour, required this.distances});
|
||||
}
|
||||
TourLoaded({
|
||||
required this.tour,
|
||||
this.distances,
|
||||
required this.paymentOptions,
|
||||
});
|
||||
|
||||
TourLoaded copyWith({
|
||||
Tour? tour,
|
||||
Map<String, double>? distances,
|
||||
List<Payment>? paymentOptions,
|
||||
}) {
|
||||
return TourLoaded(
|
||||
tour: tour ?? this.tour,
|
||||
distances: distances ?? this.distances,
|
||||
paymentOptions: paymentOptions ?? this.paymentOptions,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +1,24 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/model/delivery.dart';
|
||||
import 'package:hl_lieferservice/feature/delivery/detail/presentation/delivery_detail_page.dart';
|
||||
|
||||
import '../../../../bloc/app_bloc.dart';
|
||||
import '../../../../bloc/app_states.dart';
|
||||
import '../../../../widget/operations/bloc/operation_bloc.dart';
|
||||
import '../../detail/bloc/note_bloc.dart';
|
||||
import '../../detail/repository/note_repository.dart';
|
||||
import '../../detail/service/notes_service.dart';
|
||||
|
||||
class DeliveryListItem extends StatelessWidget {
|
||||
final Delivery delivery;
|
||||
final double distance;
|
||||
|
||||
const DeliveryListItem({super.key, required this.delivery, required this.distance});
|
||||
const DeliveryListItem({
|
||||
super.key,
|
||||
required this.delivery,
|
||||
required this.distance,
|
||||
});
|
||||
|
||||
Widget _leading(BuildContext context) {
|
||||
if (delivery.state == DeliveryState.finished) {
|
||||
@ -17,6 +29,10 @@ class DeliveryListItem extends StatelessWidget {
|
||||
return Icon(Icons.cancel_rounded, color: Colors.red);
|
||||
}
|
||||
|
||||
if (delivery.state == DeliveryState.onhold) {
|
||||
return Icon(Icons.pause_circle, color: Colors.orange);
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
@ -29,7 +45,19 @@ class DeliveryListItem extends StatelessWidget {
|
||||
void _goToDelivery(BuildContext context) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => DeliveryDetail(delivery: delivery),
|
||||
builder:
|
||||
(context) => BlocProvider(
|
||||
create:
|
||||
(context) => NoteBloc(
|
||||
deliveryId: delivery.id,
|
||||
opBloc: context.read<OperationBloc>(),
|
||||
repository: NoteRepository(
|
||||
service: NoteService(),
|
||||
),
|
||||
),
|
||||
|
||||
child: DeliveryDetail(deliveryId: delivery.id),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@ -32,7 +32,7 @@ class _DeliveryListState extends State<DeliveryList> {
|
||||
Delivery delivery = widget.deliveries[index];
|
||||
return DeliveryListItem(
|
||||
delivery: delivery,
|
||||
distance: widget.distances[delivery.id]!,
|
||||
distance: widget.distances[delivery.id] ?? 0.0,
|
||||
);
|
||||
},
|
||||
itemCount: widget.deliveries.length,
|
||||
|
||||
@ -23,7 +23,7 @@ class _DeliveryOverviewPageState extends State<DeliveryOverviewPage> {
|
||||
return Center(
|
||||
child: DeliveryOverview(
|
||||
tour: currentState.tour,
|
||||
distances: currentState.distances,
|
||||
distances: currentState.distances ?? {},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,32 +1,351 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:hl_lieferservice/feature/delivery/overview/service/delivery_info_service.dart';
|
||||
import 'package:hl_lieferservice/model/delivery.dart';
|
||||
import 'package:hl_lieferservice/model/tour.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
|
||||
import '../../../../dto/discount_add_response.dart';
|
||||
import '../../../../dto/discount_remove_response.dart';
|
||||
import '../../../../dto/discount_update_response.dart';
|
||||
import '../../../../model/article.dart';
|
||||
import '../../detail/repository/note_repository.dart';
|
||||
import '../../detail/service/notes_service.dart';
|
||||
|
||||
enum ScanResult { scanned, alreadyScanned, notFound }
|
||||
|
||||
class TourNotFoundException implements Exception {}
|
||||
|
||||
class TourRepository {
|
||||
DeliveryInfoService service;
|
||||
TourService service;
|
||||
|
||||
final _tourStream = BehaviorSubject<Tour?>();
|
||||
final _paymentOptionsStream = BehaviorSubject<List<Payment>>.seeded([]);
|
||||
|
||||
Stream<Tour?> get tour => _tourStream.stream;
|
||||
|
||||
Stream<List<Payment>> get paymentOptions => _paymentOptionsStream.stream;
|
||||
|
||||
TourRepository({required this.service});
|
||||
|
||||
Future<Tour> loadAll(String userId) async {
|
||||
Tour? tour = await service.getTourOfToday(userId);
|
||||
return tour!;
|
||||
Future<void> loadTourOfToday(String userId) async {
|
||||
_tourStream.add(await service.getTourOfToday(userId));
|
||||
}
|
||||
|
||||
Future<List<Payment>> loadPaymentOptions() async {
|
||||
return (await service.getPaymentMethods())
|
||||
.map((option) => Payment.fromDTO(option))
|
||||
.toList();
|
||||
Future<void> loadPaymentOptions() async {
|
||||
_paymentOptionsStream.add(
|
||||
(await service.getPaymentMethods())
|
||||
.map((option) => Payment.fromDTO(option))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> assignCar(String deliveryId, String carId) async {
|
||||
await service.assignCar(deliveryId, carId);
|
||||
|
||||
final tour = _tourStream.value!;
|
||||
final index = tour.deliveries.indexWhere(
|
||||
(delivery) => delivery.id == deliveryId,
|
||||
);
|
||||
tour.deliveries[index].carId = int.parse(carId);
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
|
||||
Future<void> scanArticle(String internalArticleId) async {
|
||||
return await service.scanArticle(internalArticleId);
|
||||
Future<ScanResult> scanArticle(
|
||||
String deliveryId,
|
||||
String carId,
|
||||
String articleNumber,
|
||||
) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
final tour = _tourStream.value!;
|
||||
|
||||
if (tour.deliveries.any(
|
||||
(delivery) => delivery.articles.any(
|
||||
(article) => article.articleNumber == articleNumber,
|
||||
),
|
||||
)) {
|
||||
var delivery = tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == deliveryId,
|
||||
);
|
||||
var article = delivery.articles.firstWhere(
|
||||
(article) => article.articleNumber == articleNumber,
|
||||
);
|
||||
|
||||
await service.scanArticle(article.internalId.toString());
|
||||
|
||||
if (article.scannedAmount < article.amount) {
|
||||
article.scannedAmount += 1;
|
||||
_tourStream.add(tour);
|
||||
return ScanResult.scanned;
|
||||
} else {
|
||||
return ScanResult.alreadyScanned;
|
||||
}
|
||||
} else {
|
||||
return ScanResult.notFound;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateDelivery(Delivery delivery) {
|
||||
return service.updateDelivery(delivery);
|
||||
Future<void> unscan(
|
||||
String deliveryId,
|
||||
String articleId,
|
||||
int newAmount,
|
||||
String reason,
|
||||
) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
Tour tour = _tourStream.value!;
|
||||
String? noteId = await service.unscanArticle(articleId, newAmount, reason);
|
||||
Article article = tour.deliveries
|
||||
.firstWhere((delivery) => delivery.id == deliveryId)
|
||||
.articles
|
||||
.firstWhere((article) => article.internalId == int.parse(articleId));
|
||||
|
||||
article.removeNoteId = noteId;
|
||||
article.scannedRemovedAmount += newAmount;
|
||||
article.scannedAmount -= newAmount;
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
|
||||
Future<void> resetScan(String articleId, String deliveryId) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
Tour tour = _tourStream.value!;
|
||||
await service.resetScannedArticleAmount(articleId);
|
||||
|
||||
Article article = tour.deliveries
|
||||
.firstWhere((delivery) => delivery.id == deliveryId)
|
||||
.articles
|
||||
.firstWhere((article) => article.internalId == int.parse(articleId));
|
||||
|
||||
article.removeNoteId = null;
|
||||
article.scannedRemovedAmount = 0;
|
||||
article.scannedAmount = article.amount;
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
|
||||
Future<void> uploadDriverSignature(
|
||||
String deliveryId,
|
||||
Uint8List signature,
|
||||
) async {
|
||||
NoteRepository noteRepository = NoteRepository(service: NoteService());
|
||||
await noteRepository.addNamedImage(
|
||||
deliveryId,
|
||||
signature,
|
||||
"delivery_${deliveryId}_signature_driver.jpg",
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> uploadCustomerSignature(
|
||||
String deliveryId,
|
||||
Uint8List signature,
|
||||
) async {
|
||||
NoteRepository noteRepository = NoteRepository(service: NoteService());
|
||||
await noteRepository.addNamedImage(
|
||||
deliveryId,
|
||||
signature,
|
||||
"delivery_${deliveryId}_signature_customer.jpg",
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> addDiscount(String deliveryId, String reason, int value) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
Tour tour = _tourStream.value!;
|
||||
DiscountAddResponseDTO response = await service.addDiscount(
|
||||
deliveryId,
|
||||
value,
|
||||
reason,
|
||||
);
|
||||
|
||||
Delivery delivery = tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == deliveryId,
|
||||
);
|
||||
delivery.totalNetValue = response.values.receipt.net;
|
||||
delivery.totalGrossValue = response.values.receipt.gross;
|
||||
delivery.discount = Discount(
|
||||
article: Article.fromDTO(response.values.article),
|
||||
note: response.values.note.noteDescription,
|
||||
noteId: response.values.note.rowId,
|
||||
);
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
|
||||
Future<void> removeDiscount(String deliveryId) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
Tour tour = _tourStream.value!;
|
||||
DiscountRemoveResponseDTO response = await service.removeDiscount(
|
||||
deliveryId,
|
||||
);
|
||||
|
||||
Delivery delivery = tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == deliveryId,
|
||||
);
|
||||
delivery.articles =
|
||||
delivery.articles
|
||||
.where(
|
||||
(article) =>
|
||||
article.internalId != delivery.discount?.article.internalId,
|
||||
)
|
||||
.toList();
|
||||
|
||||
delivery.discount = null;
|
||||
delivery.totalGrossValue = response.receipt.gross;
|
||||
delivery.totalNetValue = response.receipt.net;
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
|
||||
Future<void> updateDiscount(
|
||||
String deliveryId,
|
||||
String? reason,
|
||||
int? value,
|
||||
) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
Tour tour = _tourStream.value!;
|
||||
DiscountUpdateResponseDTO response = await service.updateDiscount(
|
||||
deliveryId,
|
||||
reason,
|
||||
value,
|
||||
);
|
||||
|
||||
Delivery delivery = tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == deliveryId,
|
||||
);
|
||||
if (response.values?.receipt != null) {
|
||||
delivery.totalNetValue = response.values!.receipt.net;
|
||||
delivery.totalGrossValue = response.values!.receipt.gross;
|
||||
}
|
||||
|
||||
String discountArticleNumber = delivery.discount!.article.articleNumber;
|
||||
delivery.discount = Discount(
|
||||
article:
|
||||
response.values?.article != null
|
||||
? Article.fromDTO(response.values!.article)
|
||||
: delivery.discount!.article,
|
||||
note:
|
||||
response.values?.note != null
|
||||
? response.values!.note.noteDescription
|
||||
: delivery.discount!.note,
|
||||
noteId:
|
||||
response.values?.note != null
|
||||
? response.values!.note.rowId
|
||||
: delivery.discount!.noteId,
|
||||
);
|
||||
|
||||
delivery.articles = [
|
||||
...delivery.articles.where(
|
||||
(article) => article.articleNumber != discountArticleNumber,
|
||||
),
|
||||
delivery.discount!.article,
|
||||
];
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
|
||||
Future<void> reactivateDelivery(String deliveryId) async {
|
||||
await _changeState(deliveryId, DeliveryState.ongoing);
|
||||
}
|
||||
|
||||
Future<void> holdDelivery(String deliveryId) async {
|
||||
await _changeState(deliveryId, DeliveryState.onhold);
|
||||
}
|
||||
|
||||
Future<void> cancelDelivery(String deliveryId) async {
|
||||
await _changeState(deliveryId, DeliveryState.canceled);
|
||||
}
|
||||
|
||||
Future<void> finishDelivery(String deliveryId) async {
|
||||
await _changeState(deliveryId, DeliveryState.finished);
|
||||
}
|
||||
|
||||
Future<void> _changeState(String deliveryId, DeliveryState state) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
Tour tour = _tourStream.value!;
|
||||
Delivery delivery = tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == deliveryId,
|
||||
);
|
||||
|
||||
delivery.state = state;
|
||||
await _updateDelivery(delivery);
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
|
||||
Future<void> _updateDelivery(Delivery newDelivery) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
await service.updateDelivery(newDelivery);
|
||||
}
|
||||
|
||||
Future<void> updatePayment(String deliveryId, Payment payment) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
Tour tour = _tourStream.value!;
|
||||
Delivery delivery = tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == deliveryId,
|
||||
);
|
||||
|
||||
delivery.payment = payment;
|
||||
await _updateDelivery(delivery);
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
|
||||
Future<void> updateOption(
|
||||
String deliveryId,
|
||||
String key,
|
||||
dynamic value,
|
||||
) async {
|
||||
if (!_tourStream.hasValue) {
|
||||
throw TourNotFoundException();
|
||||
}
|
||||
|
||||
Tour tour = _tourStream.value!;
|
||||
Delivery delivery = tour.deliveries.firstWhere(
|
||||
(delivery) => delivery.id == deliveryId,
|
||||
);
|
||||
|
||||
delivery.options =
|
||||
delivery.options.map((option) {
|
||||
if (option.key == key) {
|
||||
if (option.numerical) {
|
||||
return option.copyWith(value: value);
|
||||
} else {
|
||||
return option.copyWith(value: value == true ? "1" : "0");
|
||||
}
|
||||
}
|
||||
|
||||
return option;
|
||||
}).toList();
|
||||
|
||||
await _updateDelivery(delivery);
|
||||
|
||||
_tourStream.add(tour);
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,8 +20,8 @@ import '../../../../dto/discount_update_response.dart';
|
||||
import '../../../../dto/scan_response.dart';
|
||||
import '../../../authentication/exceptions.dart';
|
||||
|
||||
class DeliveryInfoService {
|
||||
DeliveryInfoService();
|
||||
class TourService {
|
||||
TourService();
|
||||
|
||||
Future<void> updateDelivery(Delivery delivery) async {
|
||||
try {
|
||||
@ -29,6 +29,7 @@ class DeliveryInfoService {
|
||||
headers.addAll(getSessionOrThrow());
|
||||
|
||||
debugPrint(getSessionOrThrow().toString());
|
||||
debugPrint(delivery.state.toString());
|
||||
debugPrint(jsonEncode(DeliveryUpdateDTO.fromEntity(delivery).toJson()));
|
||||
|
||||
var response = await post(
|
||||
@ -97,7 +98,7 @@ class DeliveryInfoService {
|
||||
|
||||
/// List all available deliveries for today.
|
||||
|
||||
Future<Tour?> getTourOfToday(String userId) async {
|
||||
Future<Tour> getTourOfToday(String userId) async {
|
||||
try {
|
||||
var response = await post(
|
||||
urlBuilder("_web_getDeliveries"),
|
||||
|
||||
Reference in New Issue
Block a user