Implemented settings, new scan, enhanced UI/UX
This commit is contained in:
@ -1,28 +1,254 @@
|
||||
import 'package:flutter/cupertino.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';
|
||||
|
||||
class TourBloc extends Bloc<TourEvent, TourState> {
|
||||
OperationBloc opBloc;
|
||||
TourRepository deliveryRepository;
|
||||
TourRepository tourRepository;
|
||||
|
||||
TourBloc({required this.opBloc, required this.deliveryRepository})
|
||||
TourBloc({required this.opBloc, required this.tourRepository})
|
||||
: super(TourInitial()) {
|
||||
on<LoadTour>(_load);
|
||||
on<UpdateTour>(_update);
|
||||
on<AssignCarEvent>(_assignCar);
|
||||
on<IncrementArticleScanAmount>(_increment);
|
||||
on<ScanArticleEvent>(_scan);
|
||||
on<HoldDeliveryEvent>(_holdDelivery);
|
||||
on<CancelDeliveryEvent>(_cancelDelivery);
|
||||
on<ReactivateDeliveryEvent>(_reactiveateDelivery);
|
||||
}
|
||||
|
||||
void _reactiveateDelivery(
|
||||
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,
|
||||
);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
|
||||
emit(TourLoaded(tour: tourCopied, distances: currentState.distances));
|
||||
} catch (e, st) {
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Zurückstellen der Lieferung"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
|
||||
emit(TourLoaded(tour: tourCopied, distances: currentState.distances));
|
||||
} catch (e, st) {
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Zurückstellen der Lieferung"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
|
||||
opBloc.add(FinishOperation());
|
||||
|
||||
emit(TourLoaded(tour: tourCopied, distances: currentState.distances));
|
||||
} catch (e, st) {
|
||||
debugPrint("$e");
|
||||
debugPrint("$st");
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Zurückstellen der Lieferung"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _scan(ScanArticleEvent event, Emitter<TourState> emit) async {
|
||||
final currentState = state;
|
||||
opBloc.add(LoadOperation());
|
||||
|
||||
if (currentState is TourLoaded) {
|
||||
try {
|
||||
if (currentState.tour.deliveries.any(
|
||||
(delivery) => delivery.articles.any(
|
||||
(article) => article.articleNumber == 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 {
|
||||
opBloc.add(
|
||||
FailOperation(
|
||||
message: 'Alle ${article.name} wurden bereits gescannt',
|
||||
),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
opBloc.add(
|
||||
FailOperation(
|
||||
message: 'Fehler: Artikel ist für keine Lieferung vorgesehen',
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e, st) {
|
||||
debugPrint(st.toString());
|
||||
opBloc.add(FailOperation(message: "Fehler beim Scannnen 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,
|
||||
) async {
|
||||
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
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
opBloc.add(
|
||||
FailOperation(message: "Fehler beim Zuweisen des Fahrzeugs"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _load(LoadTour event, Emitter<TourState> emit) async {
|
||||
opBloc.add(LoadOperation());
|
||||
try {
|
||||
Tour tour = await deliveryRepository.loadAll(event.teamId);
|
||||
List<Payment> payments = await deliveryRepository.loadPaymentOptions();
|
||||
Tour tour = await tourRepository.loadAll(event.teamId);
|
||||
List<Payment> payments = await tourRepository.loadPaymentOptions();
|
||||
tour.paymentMethods = payments;
|
||||
Map<String, double> distances = {};
|
||||
|
||||
emit(TourLoaded(tour: tour));
|
||||
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(
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import 'package:hl_lieferservice/model/tour.dart';
|
||||
|
||||
abstract class TourEvent {}
|
||||
|
||||
class LoadTour extends TourEvent {
|
||||
@ -5,3 +7,49 @@ class LoadTour extends TourEvent {
|
||||
|
||||
LoadTour({required this.teamId});
|
||||
}
|
||||
|
||||
class UpdateTour extends TourEvent {
|
||||
Tour tour;
|
||||
|
||||
UpdateTour({required this.tour});
|
||||
}
|
||||
|
||||
class AssignCarEvent extends TourEvent {
|
||||
String deliveryId;
|
||||
String carId;
|
||||
|
||||
AssignCarEvent({required this.deliveryId, required this.carId});
|
||||
}
|
||||
|
||||
class IncrementArticleScanAmount extends TourEvent {
|
||||
String internalArticleId;
|
||||
String deliveryId;
|
||||
|
||||
IncrementArticleScanAmount({required this.internalArticleId, required this.deliveryId});
|
||||
}
|
||||
|
||||
class ScanArticleEvent extends TourEvent {
|
||||
ScanArticleEvent({required this.articleNumber, required this.carId, required this.deliveryId});
|
||||
|
||||
String articleNumber;
|
||||
String deliveryId;
|
||||
String carId;
|
||||
}
|
||||
|
||||
class CancelDeliveryEvent extends TourEvent {
|
||||
String deliveryId;
|
||||
|
||||
CancelDeliveryEvent({required this.deliveryId});
|
||||
}
|
||||
|
||||
class HoldDeliveryEvent extends TourEvent {
|
||||
String deliveryId;
|
||||
|
||||
HoldDeliveryEvent({required this.deliveryId});
|
||||
}
|
||||
|
||||
class ReactivateDeliveryEvent extends TourEvent {
|
||||
String deliveryId;
|
||||
|
||||
ReactivateDeliveryEvent({required this.deliveryId});
|
||||
}
|
||||
@ -8,6 +8,7 @@ class TourLoading extends TourState {}
|
||||
|
||||
class TourLoaded extends TourState {
|
||||
Tour tour;
|
||||
Map<String, double> distances;
|
||||
|
||||
TourLoaded({required this.tour});
|
||||
TourLoaded({required this.tour, required this.distances});
|
||||
}
|
||||
@ -1,12 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hl_lieferservice/model/delivery.dart';
|
||||
|
||||
import 'package:hl_lieferservice/feature/delivery/detail/presentation/delivery_detail_page.dart';
|
||||
|
||||
class DeliveryListItem extends StatelessWidget {
|
||||
final Delivery delivery;
|
||||
final double distance;
|
||||
|
||||
const DeliveryListItem({super.key, required this.delivery});
|
||||
const DeliveryListItem({super.key, required this.delivery, required this.distance});
|
||||
|
||||
Widget _leading(BuildContext context) {
|
||||
if (delivery.state == DeliveryState.finished) {
|
||||
@ -21,7 +21,7 @@ class DeliveryListItem extends StatelessWidget {
|
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||
children: [
|
||||
Icon(Icons.location_on, color: Theme.of(context).primaryColor),
|
||||
Text("5min"),
|
||||
Text("${distance.toStringAsFixed(2)}km"),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@ -5,8 +5,13 @@ import 'delivery_item.dart';
|
||||
|
||||
class DeliveryList extends StatefulWidget {
|
||||
final List<Delivery> deliveries;
|
||||
final Map<String, double> distances;
|
||||
|
||||
const DeliveryList({super.key, required this.deliveries});
|
||||
const DeliveryList({
|
||||
super.key,
|
||||
required this.deliveries,
|
||||
required this.distances,
|
||||
});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _DeliveryListState();
|
||||
@ -23,9 +28,13 @@ class _DeliveryListState extends State<DeliveryList> {
|
||||
|
||||
return ListView.separated(
|
||||
separatorBuilder: (context, index) => const Divider(height: 0),
|
||||
itemBuilder:
|
||||
(context, index) =>
|
||||
DeliveryListItem(delivery: widget.deliveries[index]),
|
||||
itemBuilder: (context, index) {
|
||||
Delivery delivery = widget.deliveries[index];
|
||||
return DeliveryListItem(
|
||||
delivery: delivery,
|
||||
distance: widget.distances[delivery.id]!,
|
||||
);
|
||||
},
|
||||
itemCount: widget.deliveries.length,
|
||||
);
|
||||
}
|
||||
|
||||
@ -6,32 +6,48 @@ import 'package:hl_lieferservice/feature/delivery/overview/presentation/delivery
|
||||
import 'package:hl_lieferservice/feature/delivery/overview/presentation/delivery_list.dart';
|
||||
import 'package:hl_lieferservice/model/tour.dart';
|
||||
|
||||
import '../../../../model/delivery.dart';
|
||||
import '../../../authentication/bloc/auth_bloc.dart';
|
||||
import '../../../authentication/bloc/auth_state.dart';
|
||||
|
||||
class DeliveryOverview extends StatefulWidget {
|
||||
const DeliveryOverview({super.key, required this.tour});
|
||||
const DeliveryOverview({super.key, required this.tour, required this.distances});
|
||||
|
||||
final Tour tour;
|
||||
final Map<String, double> distances;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _DeliveryOverviewState();
|
||||
}
|
||||
|
||||
class _DeliveryOverviewState extends State<DeliveryOverview> {
|
||||
String? _selectedCarPlate;
|
||||
int? _selectedCarId;
|
||||
late List<Delivery> _deliveries;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// Select the first car for initialization
|
||||
_selectedCarPlate = widget.tour.driver.cars.firstOrNull?.plate;
|
||||
_selectedCarId = widget.tour.driver.cars.firstOrNull?.id;
|
||||
_updateDeliveries();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant DeliveryOverview oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_updateDeliveries();
|
||||
}
|
||||
|
||||
void _updateDeliveries() {
|
||||
_deliveries = [...widget.tour.deliveries];
|
||||
}
|
||||
|
||||
Future<void> _loadTour() async {
|
||||
Authenticated state = context.read<AuthBloc>().state as Authenticated;
|
||||
context.read<TourBloc>().add(LoadTour(teamId: state.teamId));
|
||||
Authenticated state = context
|
||||
.read<AuthBloc>()
|
||||
.state as Authenticated;
|
||||
context.read<TourBloc>().add(LoadTour(teamId: state.user.number));
|
||||
}
|
||||
|
||||
Widget _carSelection() {
|
||||
@ -41,47 +57,58 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
|
||||
child: ListView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
children:
|
||||
widget.tour.driver.cars.map((car) {
|
||||
Color? backgroundColor;
|
||||
Color? iconColor = Theme.of(context).primaryColor;
|
||||
Color? textColor;
|
||||
widget.tour.driver.cars.map((car) {
|
||||
Color? backgroundColor;
|
||||
Color? iconColor = Theme
|
||||
.of(context)
|
||||
.primaryColor;
|
||||
Color? textColor;
|
||||
|
||||
if (_selectedCarPlate == car.plate) {
|
||||
backgroundColor = Theme.of(context).primaryColor;
|
||||
textColor = Theme.of(context).colorScheme.onSecondary;
|
||||
iconColor = Theme.of(context).colorScheme.onSecondary;
|
||||
}
|
||||
if (_selectedCarId == car.id) {
|
||||
backgroundColor = Theme
|
||||
.of(context)
|
||||
.primaryColor;
|
||||
textColor = Theme
|
||||
.of(context)
|
||||
.colorScheme
|
||||
.onSecondary;
|
||||
iconColor = Theme
|
||||
.of(context)
|
||||
.colorScheme
|
||||
.onSecondary;
|
||||
}
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_selectedCarPlate = car.plate;
|
||||
});
|
||||
},
|
||||
child: Chip(
|
||||
backgroundColor: backgroundColor,
|
||||
label: Row(
|
||||
children: [
|
||||
Icon(Icons.local_shipping, color: iconColor, size: 20),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 5),
|
||||
child: Text(
|
||||
car.plate,
|
||||
style: TextStyle(color: textColor, fontSize: 12),
|
||||
),
|
||||
),
|
||||
],
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_selectedCarId = car.id;
|
||||
});
|
||||
},
|
||||
child: Chip(
|
||||
backgroundColor: backgroundColor,
|
||||
label: Row(
|
||||
children: [
|
||||
Icon(Icons.local_shipping, color: iconColor, size: 20),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 5),
|
||||
child: Text(
|
||||
car.plate,
|
||||
style: TextStyle(color: textColor, fontSize: 12),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshIndicator(
|
||||
@ -99,11 +126,46 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
|
||||
children: [
|
||||
Text(
|
||||
"Fahrten",
|
||||
style: Theme.of(context).textTheme.headlineSmall,
|
||||
style: Theme
|
||||
.of(context)
|
||||
.textTheme
|
||||
.headlineSmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
IconButton(icon: Icon(Icons.filter_list), onPressed: () {}),
|
||||
PopupMenuButton<String>(
|
||||
onSelected: (String value) {
|
||||
setState(() {
|
||||
if (value == "name-asc") {
|
||||
setState(() {
|
||||
_deliveries.sort();
|
||||
});
|
||||
}
|
||||
|
||||
if (value == "name-desc") {
|
||||
setState(() {
|
||||
_deliveries = _deliveries.reversed.toList();
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
|
||||
PopupMenuItem<String>(
|
||||
value: 'name-asc',
|
||||
child: Text('Name (A-Z)'),
|
||||
),
|
||||
PopupMenuItem<String>(
|
||||
value: 'name-desc',
|
||||
child: Text('Name (Z-A)'),
|
||||
),
|
||||
PopupMenuItem<String>(
|
||||
value: 'distance',
|
||||
child: Text('Entfernung'),
|
||||
),
|
||||
],
|
||||
child: Icon(Icons.filter_list),
|
||||
)
|
||||
|
||||
],
|
||||
),
|
||||
),
|
||||
@ -111,7 +173,19 @@ class _DeliveryOverviewState extends State<DeliveryOverview> {
|
||||
padding: const EdgeInsets.only(left: 10, right: 10, bottom: 20),
|
||||
child: _carSelection(),
|
||||
),
|
||||
Expanded(child: DeliveryList(deliveries: widget.tour.deliveries)),
|
||||
Expanded(
|
||||
child: DeliveryList(
|
||||
distances: widget.distances,
|
||||
deliveries:
|
||||
_deliveries
|
||||
.where(
|
||||
(delivery) =>
|
||||
delivery.carId == _selectedCarId &&
|
||||
delivery.allArticlesScanned(),
|
||||
)
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
@ -20,7 +20,7 @@ class _DeliveryOverviewPageState extends State<DeliveryOverviewPage> {
|
||||
if (state is TourLoaded) {
|
||||
final currentState = state;
|
||||
|
||||
return Center(child: DeliveryOverview(tour: currentState.tour));
|
||||
return Center(child: DeliveryOverview(tour: currentState.tour, distances: currentState.distances));
|
||||
}
|
||||
|
||||
return Container();
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
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';
|
||||
|
||||
class TourRepository {
|
||||
@ -16,4 +17,16 @@ class TourRepository {
|
||||
.map((option) => Payment.fromDTO(option))
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> assignCar(String deliveryId, String carId) async {
|
||||
await service.assignCar(deliveryId, carId);
|
||||
}
|
||||
|
||||
Future<void> scanArticle(String internalArticleId) async {
|
||||
return await service.scanArticle(internalArticleId);
|
||||
}
|
||||
|
||||
Future<void> updateDelivery(Delivery delivery) {
|
||||
return service.updateDelivery(delivery);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:docuframe/docuframe.dart' as df;
|
||||
import 'package:flutter/material.dart';
|
||||
@ -13,30 +14,41 @@ import 'package:hl_lieferservice/model/delivery.dart';
|
||||
import 'package:hl_lieferservice/model/tour.dart';
|
||||
import 'package:hl_lieferservice/util.dart';
|
||||
import 'package:hl_lieferservice/services/erpframe.dart';
|
||||
import 'package:http/http.dart';
|
||||
|
||||
import '../../../../dto/basic_response.dart';
|
||||
import '../../../../dto/discount_add_response.dart';
|
||||
import '../../../../dto/discount_remove_response.dart';
|
||||
import '../../../../dto/discount_update_response.dart';
|
||||
import '../../../../dto/scan_response.dart';
|
||||
import '../../../authentication/exceptions.dart';
|
||||
|
||||
class DeliveryInfoService extends ErpFrameService {
|
||||
DeliveryInfoService({required super.config});
|
||||
|
||||
Future<void> updateDelivery(Delivery delivery) async {
|
||||
df.LoginSession? session;
|
||||
|
||||
try {
|
||||
session = await getSession();
|
||||
df.DocuFrameMacroResponse response =
|
||||
await df.Macro(config: dfConfig, session: session).execute(
|
||||
"_web_updateDelivery",
|
||||
parameter: DeliveryUpdateDTO.fromEntity(delivery).toJson()
|
||||
as Map<String, dynamic>);
|
||||
var headers = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
headers.addAll(getSessionOrThrow());
|
||||
|
||||
df.Logout(config: dfConfig, session: session).logout();
|
||||
debugPrint(getSessionOrThrow().toString());
|
||||
debugPrint(jsonEncode(DeliveryUpdateDTO.fromEntity(delivery).toJson()));
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
|
||||
var response = await post(
|
||||
urlBuilder("_web_updateDelivery"),
|
||||
headers: headers,
|
||||
body: jsonEncode(DeliveryUpdateDTO.fromEntity(delivery).toJson()),
|
||||
);
|
||||
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
debugPrint("BODY: ${response.body}");
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
DeliveryUpdateResponseDTO responseDto =
|
||||
DeliveryUpdateResponseDTO.fromJson(responseJson);
|
||||
|
||||
@ -44,7 +56,45 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
return;
|
||||
}
|
||||
|
||||
throw responseDto.message;
|
||||
debugPrint("ERROR UPDATING:");
|
||||
debugPrint(responseDto.message);
|
||||
} on df.DocuFrameException catch (e, st) {
|
||||
debugPrint("ERROR WHILE UPDATING DELIVERY");
|
||||
debugPrint(e.errorMessage);
|
||||
debugPrint(e.errorCode);
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> assignCar(String deliveryId, String carId) async {
|
||||
try {
|
||||
var headers = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
headers.addAll(getSessionOrThrow());
|
||||
|
||||
var response = await post(
|
||||
urlBuilder("_web_updateDelivery"),
|
||||
headers: headers,
|
||||
body: jsonEncode({"delivery_id": deliveryId, "car_id": carId}),
|
||||
);
|
||||
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
DeliveryUpdateResponseDTO responseDto =
|
||||
DeliveryUpdateResponseDTO.fromJson(responseJson);
|
||||
|
||||
if (responseDto.code == "200") {
|
||||
return;
|
||||
}
|
||||
|
||||
debugPrint("ERROR UPDATING:");
|
||||
debugPrint(responseDto.message);
|
||||
} on df.DocuFrameException catch (e, st) {
|
||||
debugPrint("ERROR WHILE UPDATING DELIVERY");
|
||||
debugPrint(e.errorMessage);
|
||||
@ -52,27 +102,25 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
|
||||
/// List all available deliveries for today.
|
||||
|
||||
Future<Tour?> getTourOfToday(String userId) async {
|
||||
df.LoginSession? session;
|
||||
|
||||
try {
|
||||
session = await getSession();
|
||||
df.DocuFrameMacroResponse response =
|
||||
await df.Macro(config: dfConfig, session: session).execute(
|
||||
"_web_getDeliveries",
|
||||
parameter: {"driver_id": userId, "date": getTodayDate()});
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
var response = await post(
|
||||
urlBuilder("_web_getDeliveries"),
|
||||
headers: getSessionOrThrow(),
|
||||
body: {"driver_id": userId, "date": getTodayDate()},
|
||||
);
|
||||
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
DeliveryResponseDTO responseDto =
|
||||
DeliveryResponseDTO.fromJson(responseJson);
|
||||
DeliveryResponseDTO.fromJson(jsonDecode(response.body));
|
||||
|
||||
return Tour(
|
||||
discountArticleNumber: responseDto.discountArticleNumber,
|
||||
@ -93,21 +141,22 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
debugPrint("RANDOM EXCEPTION!");
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<PaymentMethodDTO>> getPaymentMethods() async {
|
||||
df.LoginSession? session;
|
||||
|
||||
try {
|
||||
session = await getSession();
|
||||
df.DocuFrameMacroResponse response =
|
||||
await df.Macro(config: dfConfig, session: session)
|
||||
.execute("_web_getPaymentMethods", parameter: {});
|
||||
var response = await post(
|
||||
urlBuilder("_web_getPaymentMethods"),
|
||||
headers: getSessionOrThrow(),
|
||||
body: {},
|
||||
);
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
PaymentMethodListDTO responseDto =
|
||||
PaymentMethodListDTO.fromJson(responseJson);
|
||||
|
||||
@ -118,30 +167,27 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> unscanArticle(
|
||||
String internalId, int amount, String reason) async {
|
||||
df.LoginSession? session;
|
||||
|
||||
debugPrint("AMOUNT: $amount");
|
||||
debugPrint("ID: $internalId");
|
||||
|
||||
try {
|
||||
session = await getSession();
|
||||
df.DocuFrameMacroResponse response =
|
||||
await df.Macro(config: dfConfig, session: session)
|
||||
.execute("_web_unscanArticle", parameter: {
|
||||
"article_id": internalId,
|
||||
"amount": amount.toString(),
|
||||
"reason": reason
|
||||
});
|
||||
var response = await post(
|
||||
urlBuilder("_web_unscanArticle"),
|
||||
headers: getSessionOrThrow(),
|
||||
body: {
|
||||
"article_id": internalId,
|
||||
"amount": amount.toString(),
|
||||
"reason": reason
|
||||
},
|
||||
);
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
debugPrint(responseJson.toString());
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
ScanResponseDTO responseDto = ScanResponseDTO.fromJson(responseJson);
|
||||
|
||||
if (responseDto.succeeded == true) {
|
||||
@ -155,22 +201,22 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> resetScannedArticleAmount(String receiptRowId) async {
|
||||
df.LoginSession? session;
|
||||
|
||||
try {
|
||||
session = await getSession();
|
||||
df.DocuFrameMacroResponse response =
|
||||
await df.Macro(config: dfConfig, session: session).execute(
|
||||
"_web_unscanArticleReset",
|
||||
parameter: {"receipt_row_id": receiptRowId});
|
||||
var response = await post(
|
||||
urlBuilder("_web_unscanArticleReset"),
|
||||
headers: getSessionOrThrow(),
|
||||
body: {"receipt_row_id": receiptRowId},
|
||||
);
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);
|
||||
|
||||
if (responseDto.succeeded == true) {
|
||||
@ -184,27 +230,27 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
|
||||
Future<DiscountAddResponseDTO> addDiscount(
|
||||
String deliveryId, int discount, String note) async {
|
||||
df.LoginSession? session;
|
||||
|
||||
try {
|
||||
session = await getSession();
|
||||
df.DocuFrameMacroResponse response =
|
||||
await df.Macro(config: dfConfig, session: session)
|
||||
.execute("_web_addDiscount", parameter: {
|
||||
"delivery_id": deliveryId,
|
||||
"discount": discount.toString(),
|
||||
"note": note
|
||||
});
|
||||
var response = await post(
|
||||
urlBuilder("_web_addDiscount"),
|
||||
headers: getSessionOrThrow(),
|
||||
body: {
|
||||
"delivery_id": deliveryId,
|
||||
"discount": discount.toString(),
|
||||
"note": note
|
||||
},
|
||||
);
|
||||
|
||||
debugPrint("BODY: ${response.body!}");
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
|
||||
// let it throw, if the values are invalid
|
||||
return DiscountAddResponseDTO.fromJson(responseJson);
|
||||
@ -214,25 +260,24 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
|
||||
Future<DiscountRemoveResponseDTO> removeDiscount(String deliveryId) async {
|
||||
df.LoginSession? session;
|
||||
|
||||
try {
|
||||
session = await getSession();
|
||||
df.DocuFrameMacroResponse response =
|
||||
await df.Macro(config: dfConfig, session: session)
|
||||
.execute("_web_removeDiscount", parameter: {
|
||||
"delivery_id": deliveryId,
|
||||
});
|
||||
var response = await post(
|
||||
urlBuilder("_web_removeDiscount"),
|
||||
headers: getSessionOrThrow(),
|
||||
body: {
|
||||
"delivery_id": deliveryId,
|
||||
},
|
||||
);
|
||||
|
||||
debugPrint("${response.body!}");
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
|
||||
// let it throw, if the values are invalid
|
||||
return DiscountRemoveResponseDTO.fromJson(responseJson);
|
||||
@ -242,26 +287,27 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
|
||||
Future<DiscountUpdateResponseDTO> updateDiscount(
|
||||
String deliveryId, String? note, int? discount) async {
|
||||
df.LoginSession? session;
|
||||
|
||||
try {
|
||||
session = await getSession();
|
||||
df.DocuFrameMacroResponse response =
|
||||
await df.Macro(config: dfConfig, session: session)
|
||||
.execute("_web_updateDiscount", parameter: {
|
||||
"delivery_id": deliveryId,
|
||||
"discount": discount,
|
||||
"note": note
|
||||
});
|
||||
var response = await post(
|
||||
urlBuilder("_web_updateDiscount"),
|
||||
headers: getSessionOrThrow(),
|
||||
body: {
|
||||
"delivery_id": deliveryId,
|
||||
"discount": discount,
|
||||
"note": note
|
||||
},
|
||||
);
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
|
||||
// let it throw, if the values are invalid
|
||||
return DiscountUpdateResponseDTO.fromJson(responseJson);
|
||||
@ -271,8 +317,37 @@ class DeliveryInfoService extends ErpFrameService {
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> scanArticle(String internalId) async {
|
||||
try {
|
||||
var response = await post(
|
||||
urlBuilder("_web_scanArticle"),
|
||||
headers: getSessionOrThrow(),
|
||||
body: {"internal_id": internalId},
|
||||
);
|
||||
|
||||
debugPrint(jsonEncode({"internal_id": internalId}));
|
||||
|
||||
if (response.statusCode == HttpStatus.unauthorized) {
|
||||
throw UserUnauthorized();
|
||||
}
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body);
|
||||
debugPrint(responseJson.toString());
|
||||
ScanResponseDTO responseDto = ScanResponseDTO.fromJson(
|
||||
responseJson,
|
||||
);
|
||||
|
||||
if (responseDto.succeeded == true) {
|
||||
return;
|
||||
} else {
|
||||
debugPrint("ERROR: ${responseDto.message}");
|
||||
throw responseDto.message;
|
||||
}
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import '../../../../services/erpframe.dart';
|
||||
import 'package:docuframe/docuframe.dart' as df;
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
import '../../../../dto/discount_add_response.dart';
|
||||
import '../../../../dto/discount_remove_response.dart';
|
||||
import '../../../../dto/discount_update_response.dart';
|
||||
|
||||
class DiscountService extends ErpFrameService {
|
||||
DiscountService({required super.config});
|
||||
|
||||
}
|
||||
82
lib/feature/delivery/overview/service/distance_service.dart
Normal file
82
lib/feature/delivery/overview/service/distance_service.dart
Normal file
@ -0,0 +1,82 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:geolocator/geolocator.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:convert';
|
||||
|
||||
class DistanceService {
|
||||
static const String GOOGLE_MAPS_API_KEY = 'DEIN_API_KEY_HIER';
|
||||
|
||||
static Future<Position> getCurrentLocation() async {
|
||||
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
throw Exception('Location services sind deaktiviert');
|
||||
}
|
||||
|
||||
LocationPermission permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
}
|
||||
|
||||
return await Geolocator.getCurrentPosition();
|
||||
}
|
||||
|
||||
// Adresse in Koordinaten umwandeln (Geocoding)
|
||||
static Future<Map<String, double>> getCoordinates(String address) async {
|
||||
String url =
|
||||
'https://maps.googleapis.com/maps/api/geocode/json'
|
||||
'?address=${Uri.encodeComponent(address)}'
|
||||
'&key=AIzaSyB5_1ftLnoswoy59FzNFkrQ7SSDma5eu5E';
|
||||
|
||||
final response = await http.get(Uri.parse(url));
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
var json = jsonDecode(response.body);
|
||||
|
||||
if (json['results'].isNotEmpty) {
|
||||
var location = json['results'][0]['geometry']['location'];
|
||||
return {
|
||||
'lat': location['lat'],
|
||||
'lng': location['lng'],
|
||||
};
|
||||
}
|
||||
throw Exception('Adresse nicht gefunden');
|
||||
}
|
||||
throw Exception('Geocoding Fehler: ${response.statusCode}');
|
||||
}
|
||||
|
||||
// Distanz berechnen
|
||||
static Future<double> getDistanceByRoad(String address) async {
|
||||
try {
|
||||
Position currentPos = await getCurrentLocation();
|
||||
Map<String, double> coords = await getCoordinates(address);
|
||||
|
||||
String origin = "${currentPos.latitude},${currentPos.longitude}";
|
||||
String destination = "${coords['lat']},${coords['lng']}";
|
||||
|
||||
String url =
|
||||
'https://maps.googleapis.com/maps/api/distancematrix/json'
|
||||
'?origins=$origin'
|
||||
'&destinations=$destination'
|
||||
'&key=AIzaSyB5_1ftLnoswoy59FzNFkrQ7SSDma5eu5E';
|
||||
|
||||
final response = await http.get(Uri.parse(url));
|
||||
|
||||
debugPrint(response.body);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
var json = jsonDecode(response.body);
|
||||
|
||||
if (json['rows'][0]['elements'][0]['status'] == 'OK') {
|
||||
int distanceMeters = json['rows'][0]['elements'][0]['distance']['value'];
|
||||
return distanceMeters / 1000; // In km
|
||||
} else {
|
||||
throw Exception('Route nicht gefunden');
|
||||
}
|
||||
} else {
|
||||
throw Exception('API Fehler: ${response.statusCode}');
|
||||
}
|
||||
} catch (e) {
|
||||
throw Exception('Fehler: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user