Implemented settings, new scan, enhanced UI/UX
This commit is contained in:
@ -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