Initial draft
This commit is contained in:
278
lib/feature/delivery/overview/service/delivery_info_service.dart
Normal file
278
lib/feature/delivery/overview/service/delivery_info_service.dart
Normal file
@ -0,0 +1,278 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:docuframe/docuframe.dart' as df;
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:hl_lieferservice/dto/delivery_response.dart';
|
||||
import 'package:hl_lieferservice/dto/delivery_update.dart';
|
||||
import 'package:hl_lieferservice/dto/delivery_update_response.dart';
|
||||
import 'package:hl_lieferservice/dto/payment.dart';
|
||||
import 'package:hl_lieferservice/dto/payments.dart';
|
||||
import 'package:hl_lieferservice/model/car.dart';
|
||||
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 '../../../../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';
|
||||
|
||||
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>);
|
||||
|
||||
df.Logout(config: dfConfig, session: session).logout();
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
DeliveryUpdateResponseDTO responseDto =
|
||||
DeliveryUpdateResponseDTO.fromJson(responseJson);
|
||||
|
||||
if (responseDto.code == "200") {
|
||||
return;
|
||||
}
|
||||
|
||||
throw responseDto.message;
|
||||
} on df.DocuFrameException catch (e, st) {
|
||||
debugPrint("ERROR WHILE UPDATING DELIVERY");
|
||||
debugPrint(e.errorMessage);
|
||||
debugPrint(e.errorCode);
|
||||
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!);
|
||||
|
||||
DeliveryResponseDTO responseDto =
|
||||
DeliveryResponseDTO.fromJson(responseJson);
|
||||
|
||||
return Tour(
|
||||
discountArticleNumber: responseDto.discountArticleNumber,
|
||||
date: DateTime.now(),
|
||||
deliveries: responseDto.deliveries.map(Delivery.fromDTO).toList(),
|
||||
paymentMethods: [],
|
||||
driver: Driver(
|
||||
cars: responseDto.driver.cars
|
||||
.map((carDto) =>
|
||||
Car(id: int.parse(carDto.id), plate: carDto.plate))
|
||||
.toList(),
|
||||
teamNumber: int.parse(responseDto.driver.id),
|
||||
name: responseDto.driver.name,
|
||||
salutation: responseDto.driver.salutation));
|
||||
} catch (e, stacktrace) {
|
||||
debugPrint(e.toString());
|
||||
debugPrint(stacktrace.toString());
|
||||
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: {});
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
PaymentMethodListDTO responseDto =
|
||||
PaymentMethodListDTO.fromJson(responseJson);
|
||||
|
||||
return responseDto.paymentMethods;
|
||||
} catch (e, st) {
|
||||
debugPrint("ERROR while retrieving allowed payment methods");
|
||||
debugPrint(e.toString());
|
||||
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
|
||||
});
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
debugPrint(responseJson.toString());
|
||||
ScanResponseDTO responseDto = ScanResponseDTO.fromJson(responseJson);
|
||||
|
||||
if (responseDto.succeeded == true) {
|
||||
return responseDto.noteId;
|
||||
} else {
|
||||
throw responseDto.message;
|
||||
}
|
||||
} catch (e, st) {
|
||||
debugPrint("ERROR WHILE REVERTING THE SCAN OF ARTICLE $internalId");
|
||||
debugPrint(e.toString());
|
||||
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});
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
BasicResponseDTO responseDto = BasicResponseDTO.fromJson(responseJson);
|
||||
|
||||
if (responseDto.succeeded == true) {
|
||||
return;
|
||||
} else {
|
||||
throw responseDto.message;
|
||||
}
|
||||
} catch (e, st) {
|
||||
debugPrint("ERROR WHILE REVERTING THE UNSCAN OF ARTICLE $receiptRowId");
|
||||
debugPrint(e.toString());
|
||||
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
|
||||
});
|
||||
|
||||
debugPrint("BODY: ${response.body!}");
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
|
||||
// let it throw, if the values are invalid
|
||||
return DiscountAddResponseDTO.fromJson(responseJson);
|
||||
} catch (e, st) {
|
||||
debugPrint("ERROR while adding discount");
|
||||
debugPrint(e.toString());
|
||||
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,
|
||||
});
|
||||
|
||||
debugPrint("${response.body!}");
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
|
||||
// let it throw, if the values are invalid
|
||||
return DiscountRemoveResponseDTO.fromJson(responseJson);
|
||||
} catch (e, st) {
|
||||
debugPrint("ERROR while removing discount");
|
||||
debugPrint(e.toString());
|
||||
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
|
||||
});
|
||||
|
||||
Map<String, dynamic> responseJson = jsonDecode(response.body!);
|
||||
|
||||
// let it throw, if the values are invalid
|
||||
return DiscountUpdateResponseDTO.fromJson(responseJson);
|
||||
} catch (e, st) {
|
||||
debugPrint("ERROR while retrieving allowed payment methods");
|
||||
debugPrint(e.toString());
|
||||
debugPrint(st.toString());
|
||||
|
||||
rethrow;
|
||||
} finally {
|
||||
await logout(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user