diff --git a/lib/data/datasources/order_remote_datasource.dart b/lib/data/datasources/order_remote_datasource.dart index 36fd408..6f14843 100644 --- a/lib/data/datasources/order_remote_datasource.dart +++ b/lib/data/datasources/order_remote_datasource.dart @@ -302,6 +302,38 @@ class OrderRemoteDatasource { } } + Future> getOrderById( + {required String orderId}) async { + try { + final authData = await AuthLocalDataSource().getAuthData(); + final response = await dio.get( + '${Variables.baseUrl}/api/v1/orders/$orderId', + options: Options( + headers: { + 'Authorization': 'Bearer ${authData.token}', + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + ), + ); + + if (response.statusCode == 200) { + return Right(OrderDetailResponseModel.fromMap(response.data)); + } else { + log("❌ OrderDetailResponseModel API call failed - Status: ${response.statusCode}"); + return const Left("Failed Load Data"); + } + } on DioException catch (e) { + final errorMessage = 'Something went wrong'; + log("💥 Dio error: ${e.message}"); + log("💥 Dio response: ${e.response?.data}"); + return Left(errorMessage); + } catch (e) { + log("💥 Unexpected error: $e"); + return Left("Unexpected Error: $e"); + } + } + Future> createOrderWithPayment( OrderRequestModel orderModel, PaymentMethod payment, diff --git a/lib/presentation/home/dialog/payment_add_order_dialog.dart b/lib/presentation/home/dialog/payment_add_order_dialog.dart index e7f837f..b832562 100644 --- a/lib/presentation/home/dialog/payment_add_order_dialog.dart +++ b/lib/presentation/home/dialog/payment_add_order_dialog.dart @@ -136,7 +136,7 @@ class _PaymentAddOrderDialogState extends State { context.pushReplacement( SuccessSaveOrderPage( productQuantity: widget.items, - order: data, + orderId: selectOrder?.id ?? "", ), ); }, diff --git a/lib/presentation/home/dialog/payment_save_dialog.dart b/lib/presentation/home/dialog/payment_save_dialog.dart index e4f6532..199f712 100644 --- a/lib/presentation/home/dialog/payment_save_dialog.dart +++ b/lib/presentation/home/dialog/payment_save_dialog.dart @@ -146,7 +146,7 @@ class _PaymentSaveDialogState extends State { success: (data) { context.pushReplacement(SuccessSaveOrderPage( productQuantity: widget.items, - order: data, + orderId: data.id ?? "", )); }, error: (message) => AppFlushbar.showError(context, message), diff --git a/lib/presentation/home/models/order_item.dart b/lib/presentation/home/models/order_item.dart index 4e81869..b8e9420 100644 --- a/lib/presentation/home/models/order_item.dart +++ b/lib/presentation/home/models/order_item.dart @@ -1,7 +1,5 @@ import 'package:enaklo_pos/data/models/response/product_response_model.dart'; -import 'product_model.dart'; - class OrderItem { final Product product; int quantity; diff --git a/lib/presentation/sales/blocs/order_loader/order_loader_bloc.dart b/lib/presentation/sales/blocs/order_loader/order_loader_bloc.dart index ce4b95a..6621187 100644 --- a/lib/presentation/sales/blocs/order_loader/order_loader_bloc.dart +++ b/lib/presentation/sales/blocs/order_loader/order_loader_bloc.dart @@ -23,5 +23,14 @@ class OrderLoaderBloc extends Bloc { )), ); }); + on<_GetById>((event, emit) async { + emit(const _Loading()); + final result = + await _orderRemoteDatasource.getOrderById(orderId: event.id); + result.fold( + (l) => emit(_Error(l)), + (r) => emit(_LoadedDetail(r.data!)), + ); + }); } } diff --git a/lib/presentation/sales/blocs/order_loader/order_loader_bloc.freezed.dart b/lib/presentation/sales/blocs/order_loader/order_loader_bloc.freezed.dart index 927ee79..a15a071 100644 --- a/lib/presentation/sales/blocs/order_loader/order_loader_bloc.freezed.dart +++ b/lib/presentation/sales/blocs/order_loader/order_loader_bloc.freezed.dart @@ -16,45 +16,44 @@ final _privateConstructorUsedError = UnsupportedError( /// @nodoc mixin _$OrderLoaderEvent { - String get status => throw _privateConstructorUsedError; @optionalTypeArgs TResult when({ required TResult Function(String status) getByStatus, + required TResult Function(String id) getById, }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult? whenOrNull({ TResult? Function(String status)? getByStatus, + TResult? Function(String id)? getById, }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult maybeWhen({ TResult Function(String status)? getByStatus, + TResult Function(String id)? getById, required TResult orElse(), }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult map({ required TResult Function(_GetByStatus value) getByStatus, + required TResult Function(_GetById value) getById, }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult? mapOrNull({ TResult? Function(_GetByStatus value)? getByStatus, + TResult? Function(_GetById value)? getById, }) => throw _privateConstructorUsedError; @optionalTypeArgs TResult maybeMap({ TResult Function(_GetByStatus value)? getByStatus, + TResult Function(_GetById value)? getById, required TResult orElse(), }) => throw _privateConstructorUsedError; - - /// Create a copy of OrderLoaderEvent - /// with the given fields replaced by the non-null parameter values. - @JsonKey(includeFromJson: false, includeToJson: false) - $OrderLoaderEventCopyWith get copyWith => - throw _privateConstructorUsedError; } /// @nodoc @@ -62,8 +61,6 @@ abstract class $OrderLoaderEventCopyWith<$Res> { factory $OrderLoaderEventCopyWith( OrderLoaderEvent value, $Res Function(OrderLoaderEvent) then) = _$OrderLoaderEventCopyWithImpl<$Res, OrderLoaderEvent>; - @useResult - $Res call({String status}); } /// @nodoc @@ -78,27 +75,13 @@ class _$OrderLoaderEventCopyWithImpl<$Res, $Val extends OrderLoaderEvent> /// Create a copy of OrderLoaderEvent /// with the given fields replaced by the non-null parameter values. - @pragma('vm:prefer-inline') - @override - $Res call({ - Object? status = null, - }) { - return _then(_value.copyWith( - status: null == status - ? _value.status - : status // ignore: cast_nullable_to_non_nullable - as String, - ) as $Val); - } } /// @nodoc -abstract class _$$GetByStatusImplCopyWith<$Res> - implements $OrderLoaderEventCopyWith<$Res> { +abstract class _$$GetByStatusImplCopyWith<$Res> { factory _$$GetByStatusImplCopyWith( _$GetByStatusImpl value, $Res Function(_$GetByStatusImpl) then) = __$$GetByStatusImplCopyWithImpl<$Res>; - @override @useResult $Res call({String status}); } @@ -163,6 +146,7 @@ class _$GetByStatusImpl implements _GetByStatus { @optionalTypeArgs TResult when({ required TResult Function(String status) getByStatus, + required TResult Function(String id) getById, }) { return getByStatus(status); } @@ -171,6 +155,7 @@ class _$GetByStatusImpl implements _GetByStatus { @optionalTypeArgs TResult? whenOrNull({ TResult? Function(String status)? getByStatus, + TResult? Function(String id)? getById, }) { return getByStatus?.call(status); } @@ -179,6 +164,7 @@ class _$GetByStatusImpl implements _GetByStatus { @optionalTypeArgs TResult maybeWhen({ TResult Function(String status)? getByStatus, + TResult Function(String id)? getById, required TResult orElse(), }) { if (getByStatus != null) { @@ -191,6 +177,7 @@ class _$GetByStatusImpl implements _GetByStatus { @optionalTypeArgs TResult map({ required TResult Function(_GetByStatus value) getByStatus, + required TResult Function(_GetById value) getById, }) { return getByStatus(this); } @@ -199,6 +186,7 @@ class _$GetByStatusImpl implements _GetByStatus { @optionalTypeArgs TResult? mapOrNull({ TResult? Function(_GetByStatus value)? getByStatus, + TResult? Function(_GetById value)? getById, }) { return getByStatus?.call(this); } @@ -207,6 +195,7 @@ class _$GetByStatusImpl implements _GetByStatus { @optionalTypeArgs TResult maybeMap({ TResult Function(_GetByStatus value)? getByStatus, + TResult Function(_GetById value)? getById, required TResult orElse(), }) { if (getByStatus != null) { @@ -219,17 +208,155 @@ class _$GetByStatusImpl implements _GetByStatus { abstract class _GetByStatus implements OrderLoaderEvent { const factory _GetByStatus(final String status) = _$GetByStatusImpl; - @override String get status; /// Create a copy of OrderLoaderEvent /// with the given fields replaced by the non-null parameter values. - @override @JsonKey(includeFromJson: false, includeToJson: false) _$$GetByStatusImplCopyWith<_$GetByStatusImpl> get copyWith => throw _privateConstructorUsedError; } +/// @nodoc +abstract class _$$GetByIdImplCopyWith<$Res> { + factory _$$GetByIdImplCopyWith( + _$GetByIdImpl value, $Res Function(_$GetByIdImpl) then) = + __$$GetByIdImplCopyWithImpl<$Res>; + @useResult + $Res call({String id}); +} + +/// @nodoc +class __$$GetByIdImplCopyWithImpl<$Res> + extends _$OrderLoaderEventCopyWithImpl<$Res, _$GetByIdImpl> + implements _$$GetByIdImplCopyWith<$Res> { + __$$GetByIdImplCopyWithImpl( + _$GetByIdImpl _value, $Res Function(_$GetByIdImpl) _then) + : super(_value, _then); + + /// Create a copy of OrderLoaderEvent + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = null, + }) { + return _then(_$GetByIdImpl( + null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as String, + )); + } +} + +/// @nodoc + +class _$GetByIdImpl implements _GetById { + const _$GetByIdImpl(this.id); + + @override + final String id; + + @override + String toString() { + return 'OrderLoaderEvent.getById(id: $id)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$GetByIdImpl && + (identical(other.id, id) || other.id == id)); + } + + @override + int get hashCode => Object.hash(runtimeType, id); + + /// Create a copy of OrderLoaderEvent + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + @override + @pragma('vm:prefer-inline') + _$$GetByIdImplCopyWith<_$GetByIdImpl> get copyWith => + __$$GetByIdImplCopyWithImpl<_$GetByIdImpl>(this, _$identity); + + @override + @optionalTypeArgs + TResult when({ + required TResult Function(String status) getByStatus, + required TResult Function(String id) getById, + }) { + return getById(id); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult? Function(String status)? getByStatus, + TResult? Function(String id)? getById, + }) { + return getById?.call(id); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function(String status)? getByStatus, + TResult Function(String id)? getById, + required TResult orElse(), + }) { + if (getById != null) { + return getById(id); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(_GetByStatus value) getByStatus, + required TResult Function(_GetById value) getById, + }) { + return getById(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult? Function(_GetByStatus value)? getByStatus, + TResult? Function(_GetById value)? getById, + }) { + return getById?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_GetByStatus value)? getByStatus, + TResult Function(_GetById value)? getById, + required TResult orElse(), + }) { + if (getById != null) { + return getById(this); + } + return orElse(); + } +} + +abstract class _GetById implements OrderLoaderEvent { + const factory _GetById(final String id) = _$GetByIdImpl; + + String get id; + + /// Create a copy of OrderLoaderEvent + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + _$$GetByIdImplCopyWith<_$GetByIdImpl> get copyWith => + throw _privateConstructorUsedError; +} + /// @nodoc mixin _$OrderLoaderState { @optionalTypeArgs @@ -238,6 +365,7 @@ mixin _$OrderLoaderState { required TResult Function() loading, required TResult Function(List orders, int totalOrder) loaded, required TResult Function(String message) error, + required TResult Function(Order order) loadedDetail, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -246,6 +374,7 @@ mixin _$OrderLoaderState { TResult? Function()? loading, TResult? Function(List orders, int totalOrder)? loaded, TResult? Function(String message)? error, + TResult? Function(Order order)? loadedDetail, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -254,6 +383,7 @@ mixin _$OrderLoaderState { TResult Function()? loading, TResult Function(List orders, int totalOrder)? loaded, TResult Function(String message)? error, + TResult Function(Order order)? loadedDetail, required TResult orElse(), }) => throw _privateConstructorUsedError; @@ -263,6 +393,7 @@ mixin _$OrderLoaderState { required TResult Function(_Loading value) loading, required TResult Function(_Loaded value) loaded, required TResult Function(_Error value) error, + required TResult Function(_LoadedDetail value) loadedDetail, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -271,6 +402,7 @@ mixin _$OrderLoaderState { TResult? Function(_Loading value)? loading, TResult? Function(_Loaded value)? loaded, TResult? Function(_Error value)? error, + TResult? Function(_LoadedDetail value)? loadedDetail, }) => throw _privateConstructorUsedError; @optionalTypeArgs @@ -279,6 +411,7 @@ mixin _$OrderLoaderState { TResult Function(_Loading value)? loading, TResult Function(_Loaded value)? loaded, TResult Function(_Error value)? error, + TResult Function(_LoadedDetail value)? loadedDetail, required TResult orElse(), }) => throw _privateConstructorUsedError; @@ -350,6 +483,7 @@ class _$InitialImpl implements _Initial { required TResult Function() loading, required TResult Function(List orders, int totalOrder) loaded, required TResult Function(String message) error, + required TResult Function(Order order) loadedDetail, }) { return initial(); } @@ -361,6 +495,7 @@ class _$InitialImpl implements _Initial { TResult? Function()? loading, TResult? Function(List orders, int totalOrder)? loaded, TResult? Function(String message)? error, + TResult? Function(Order order)? loadedDetail, }) { return initial?.call(); } @@ -372,6 +507,7 @@ class _$InitialImpl implements _Initial { TResult Function()? loading, TResult Function(List orders, int totalOrder)? loaded, TResult Function(String message)? error, + TResult Function(Order order)? loadedDetail, required TResult orElse(), }) { if (initial != null) { @@ -387,6 +523,7 @@ class _$InitialImpl implements _Initial { required TResult Function(_Loading value) loading, required TResult Function(_Loaded value) loaded, required TResult Function(_Error value) error, + required TResult Function(_LoadedDetail value) loadedDetail, }) { return initial(this); } @@ -398,6 +535,7 @@ class _$InitialImpl implements _Initial { TResult? Function(_Loading value)? loading, TResult? Function(_Loaded value)? loaded, TResult? Function(_Error value)? error, + TResult? Function(_LoadedDetail value)? loadedDetail, }) { return initial?.call(this); } @@ -409,6 +547,7 @@ class _$InitialImpl implements _Initial { TResult Function(_Loading value)? loading, TResult Function(_Loaded value)? loaded, TResult Function(_Error value)? error, + TResult Function(_LoadedDetail value)? loadedDetail, required TResult orElse(), }) { if (initial != null) { @@ -467,6 +606,7 @@ class _$LoadingImpl implements _Loading { required TResult Function() loading, required TResult Function(List orders, int totalOrder) loaded, required TResult Function(String message) error, + required TResult Function(Order order) loadedDetail, }) { return loading(); } @@ -478,6 +618,7 @@ class _$LoadingImpl implements _Loading { TResult? Function()? loading, TResult? Function(List orders, int totalOrder)? loaded, TResult? Function(String message)? error, + TResult? Function(Order order)? loadedDetail, }) { return loading?.call(); } @@ -489,6 +630,7 @@ class _$LoadingImpl implements _Loading { TResult Function()? loading, TResult Function(List orders, int totalOrder)? loaded, TResult Function(String message)? error, + TResult Function(Order order)? loadedDetail, required TResult orElse(), }) { if (loading != null) { @@ -504,6 +646,7 @@ class _$LoadingImpl implements _Loading { required TResult Function(_Loading value) loading, required TResult Function(_Loaded value) loaded, required TResult Function(_Error value) error, + required TResult Function(_LoadedDetail value) loadedDetail, }) { return loading(this); } @@ -515,6 +658,7 @@ class _$LoadingImpl implements _Loading { TResult? Function(_Loading value)? loading, TResult? Function(_Loaded value)? loaded, TResult? Function(_Error value)? error, + TResult? Function(_LoadedDetail value)? loadedDetail, }) { return loading?.call(this); } @@ -526,6 +670,7 @@ class _$LoadingImpl implements _Loading { TResult Function(_Loading value)? loading, TResult Function(_Loaded value)? loaded, TResult Function(_Error value)? error, + TResult Function(_LoadedDetail value)? loadedDetail, required TResult orElse(), }) { if (loading != null) { @@ -628,6 +773,7 @@ class _$LoadedImpl implements _Loaded { required TResult Function() loading, required TResult Function(List orders, int totalOrder) loaded, required TResult Function(String message) error, + required TResult Function(Order order) loadedDetail, }) { return loaded(orders, totalOrder); } @@ -639,6 +785,7 @@ class _$LoadedImpl implements _Loaded { TResult? Function()? loading, TResult? Function(List orders, int totalOrder)? loaded, TResult? Function(String message)? error, + TResult? Function(Order order)? loadedDetail, }) { return loaded?.call(orders, totalOrder); } @@ -650,6 +797,7 @@ class _$LoadedImpl implements _Loaded { TResult Function()? loading, TResult Function(List orders, int totalOrder)? loaded, TResult Function(String message)? error, + TResult Function(Order order)? loadedDetail, required TResult orElse(), }) { if (loaded != null) { @@ -665,6 +813,7 @@ class _$LoadedImpl implements _Loaded { required TResult Function(_Loading value) loading, required TResult Function(_Loaded value) loaded, required TResult Function(_Error value) error, + required TResult Function(_LoadedDetail value) loadedDetail, }) { return loaded(this); } @@ -676,6 +825,7 @@ class _$LoadedImpl implements _Loaded { TResult? Function(_Loading value)? loading, TResult? Function(_Loaded value)? loaded, TResult? Function(_Error value)? error, + TResult? Function(_LoadedDetail value)? loadedDetail, }) { return loaded?.call(this); } @@ -687,6 +837,7 @@ class _$LoadedImpl implements _Loaded { TResult Function(_Loading value)? loading, TResult Function(_Loaded value)? loaded, TResult Function(_Error value)? error, + TResult Function(_LoadedDetail value)? loadedDetail, required TResult orElse(), }) { if (loaded != null) { @@ -782,6 +933,7 @@ class _$ErrorImpl implements _Error { required TResult Function() loading, required TResult Function(List orders, int totalOrder) loaded, required TResult Function(String message) error, + required TResult Function(Order order) loadedDetail, }) { return error(message); } @@ -793,6 +945,7 @@ class _$ErrorImpl implements _Error { TResult? Function()? loading, TResult? Function(List orders, int totalOrder)? loaded, TResult? Function(String message)? error, + TResult? Function(Order order)? loadedDetail, }) { return error?.call(message); } @@ -804,6 +957,7 @@ class _$ErrorImpl implements _Error { TResult Function()? loading, TResult Function(List orders, int totalOrder)? loaded, TResult Function(String message)? error, + TResult Function(Order order)? loadedDetail, required TResult orElse(), }) { if (error != null) { @@ -819,6 +973,7 @@ class _$ErrorImpl implements _Error { required TResult Function(_Loading value) loading, required TResult Function(_Loaded value) loaded, required TResult Function(_Error value) error, + required TResult Function(_LoadedDetail value) loadedDetail, }) { return error(this); } @@ -830,6 +985,7 @@ class _$ErrorImpl implements _Error { TResult? Function(_Loading value)? loading, TResult? Function(_Loaded value)? loaded, TResult? Function(_Error value)? error, + TResult? Function(_LoadedDetail value)? loadedDetail, }) { return error?.call(this); } @@ -841,6 +997,7 @@ class _$ErrorImpl implements _Error { TResult Function(_Loading value)? loading, TResult Function(_Loaded value)? loaded, TResult Function(_Error value)? error, + TResult Function(_LoadedDetail value)? loadedDetail, required TResult orElse(), }) { if (error != null) { @@ -861,3 +1018,161 @@ abstract class _Error implements OrderLoaderState { _$$ErrorImplCopyWith<_$ErrorImpl> get copyWith => throw _privateConstructorUsedError; } + +/// @nodoc +abstract class _$$LoadedDetailImplCopyWith<$Res> { + factory _$$LoadedDetailImplCopyWith( + _$LoadedDetailImpl value, $Res Function(_$LoadedDetailImpl) then) = + __$$LoadedDetailImplCopyWithImpl<$Res>; + @useResult + $Res call({Order order}); +} + +/// @nodoc +class __$$LoadedDetailImplCopyWithImpl<$Res> + extends _$OrderLoaderStateCopyWithImpl<$Res, _$LoadedDetailImpl> + implements _$$LoadedDetailImplCopyWith<$Res> { + __$$LoadedDetailImplCopyWithImpl( + _$LoadedDetailImpl _value, $Res Function(_$LoadedDetailImpl) _then) + : super(_value, _then); + + /// Create a copy of OrderLoaderState + /// with the given fields replaced by the non-null parameter values. + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? order = null, + }) { + return _then(_$LoadedDetailImpl( + null == order + ? _value.order + : order // ignore: cast_nullable_to_non_nullable + as Order, + )); + } +} + +/// @nodoc + +class _$LoadedDetailImpl implements _LoadedDetail { + const _$LoadedDetailImpl(this.order); + + @override + final Order order; + + @override + String toString() { + return 'OrderLoaderState.loadedDetail(order: $order)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$LoadedDetailImpl && + (identical(other.order, order) || other.order == order)); + } + + @override + int get hashCode => Object.hash(runtimeType, order); + + /// Create a copy of OrderLoaderState + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + @override + @pragma('vm:prefer-inline') + _$$LoadedDetailImplCopyWith<_$LoadedDetailImpl> get copyWith => + __$$LoadedDetailImplCopyWithImpl<_$LoadedDetailImpl>(this, _$identity); + + @override + @optionalTypeArgs + TResult when({ + required TResult Function() initial, + required TResult Function() loading, + required TResult Function(List orders, int totalOrder) loaded, + required TResult Function(String message) error, + required TResult Function(Order order) loadedDetail, + }) { + return loadedDetail(order); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult? Function()? initial, + TResult? Function()? loading, + TResult? Function(List orders, int totalOrder)? loaded, + TResult? Function(String message)? error, + TResult? Function(Order order)? loadedDetail, + }) { + return loadedDetail?.call(order); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function()? initial, + TResult Function()? loading, + TResult Function(List orders, int totalOrder)? loaded, + TResult Function(String message)? error, + TResult Function(Order order)? loadedDetail, + required TResult orElse(), + }) { + if (loadedDetail != null) { + return loadedDetail(order); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(_Initial value) initial, + required TResult Function(_Loading value) loading, + required TResult Function(_Loaded value) loaded, + required TResult Function(_Error value) error, + required TResult Function(_LoadedDetail value) loadedDetail, + }) { + return loadedDetail(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult? Function(_Initial value)? initial, + TResult? Function(_Loading value)? loading, + TResult? Function(_Loaded value)? loaded, + TResult? Function(_Error value)? error, + TResult? Function(_LoadedDetail value)? loadedDetail, + }) { + return loadedDetail?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_Initial value)? initial, + TResult Function(_Loading value)? loading, + TResult Function(_Loaded value)? loaded, + TResult Function(_Error value)? error, + TResult Function(_LoadedDetail value)? loadedDetail, + required TResult orElse(), + }) { + if (loadedDetail != null) { + return loadedDetail(this); + } + return orElse(); + } +} + +abstract class _LoadedDetail implements OrderLoaderState { + const factory _LoadedDetail(final Order order) = _$LoadedDetailImpl; + + Order get order; + + /// Create a copy of OrderLoaderState + /// with the given fields replaced by the non-null parameter values. + @JsonKey(includeFromJson: false, includeToJson: false) + _$$LoadedDetailImplCopyWith<_$LoadedDetailImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/lib/presentation/sales/blocs/order_loader/order_loader_event.dart b/lib/presentation/sales/blocs/order_loader/order_loader_event.dart index b100df1..d28bfcb 100644 --- a/lib/presentation/sales/blocs/order_loader/order_loader_event.dart +++ b/lib/presentation/sales/blocs/order_loader/order_loader_event.dart @@ -3,4 +3,5 @@ part of 'order_loader_bloc.dart'; @freezed class OrderLoaderEvent with _$OrderLoaderEvent { const factory OrderLoaderEvent.getByStatus(String status) = _GetByStatus; + const factory OrderLoaderEvent.getById(String id) = _GetById; } diff --git a/lib/presentation/sales/blocs/order_loader/order_loader_state.dart b/lib/presentation/sales/blocs/order_loader/order_loader_state.dart index 64445d7..40a4907 100644 --- a/lib/presentation/sales/blocs/order_loader/order_loader_state.dart +++ b/lib/presentation/sales/blocs/order_loader/order_loader_state.dart @@ -7,4 +7,5 @@ class OrderLoaderState with _$OrderLoaderState { const factory OrderLoaderState.loaded(List orders, int totalOrder) = _Loaded; const factory OrderLoaderState.error(String message) = _Error; + const factory OrderLoaderState.loadedDetail(Order order) = _LoadedDetail; } diff --git a/lib/presentation/sales/dialog/payment_dialog.dart b/lib/presentation/sales/dialog/payment_dialog.dart index 46462f8..6c9932f 100644 --- a/lib/presentation/sales/dialog/payment_dialog.dart +++ b/lib/presentation/sales/dialog/payment_dialog.dart @@ -11,7 +11,9 @@ import 'package:enaklo_pos/core/extensions/string_ext.dart'; import 'package:enaklo_pos/data/models/request/payment_request.dart'; import 'package:enaklo_pos/data/models/response/order_response_model.dart'; import 'package:enaklo_pos/data/models/response/payment_methods_response_model.dart'; +import 'package:enaklo_pos/data/models/response/product_response_model.dart'; import 'package:enaklo_pos/presentation/home/bloc/payment_methods/payment_methods_bloc.dart'; +import 'package:enaklo_pos/presentation/home/models/product_quantity.dart'; import 'package:enaklo_pos/presentation/sales/blocs/payment_form/payment_form_bloc.dart'; import 'package:enaklo_pos/presentation/success/pages/success_payment_page.dart'; import 'package:flutter/material.dart'; @@ -328,6 +330,18 @@ class _PaymentDialogState extends State { orElse: () {}, success: (data) { context.pushReplacement(SuccessPaymentPage( + productQuantity: widget.order.orderItems + ?.map( + (item) => ProductQuantity( + product: Product( + name: item.productName, + price: item.unitPrice, + ), + quantity: item.quantity ?? 0, + ), + ) + .toList() ?? + [], payment: data, )); }, diff --git a/lib/presentation/success/pages/success_payment_page.dart b/lib/presentation/success/pages/success_payment_page.dart index 6346f08..c50a37a 100644 --- a/lib/presentation/success/pages/success_payment_page.dart +++ b/lib/presentation/success/pages/success_payment_page.dart @@ -1,3 +1,5 @@ +import 'dart:developer'; + import 'package:enaklo_pos/core/components/buttons.dart'; import 'package:enaklo_pos/core/components/dashed_divider.dart'; import 'package:enaklo_pos/core/components/spaces.dart'; @@ -5,13 +7,34 @@ import 'package:enaklo_pos/core/constants/colors.dart'; import 'package:enaklo_pos/core/extensions/build_context_ext.dart'; import 'package:enaklo_pos/core/extensions/date_time_ext.dart'; import 'package:enaklo_pos/core/extensions/string_ext.dart'; +import 'package:enaklo_pos/core/utils/printer_service.dart'; +import 'package:enaklo_pos/data/dataoutputs/print_dataoutputs.dart'; +import 'package:enaklo_pos/data/datasources/product_local_datasource.dart'; import 'package:enaklo_pos/data/models/response/payment_response_model.dart'; +import 'package:enaklo_pos/presentation/home/models/product_quantity.dart'; import 'package:enaklo_pos/presentation/home/pages/dashboard_page.dart'; +import 'package:enaklo_pos/presentation/sales/blocs/order_loader/order_loader_bloc.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; -class SuccessPaymentPage extends StatelessWidget { +class SuccessPaymentPage extends StatefulWidget { + final List productQuantity; final PaymentData payment; - const SuccessPaymentPage({super.key, required this.payment}); + const SuccessPaymentPage( + {super.key, required this.payment, required this.productQuantity}); + + @override + State createState() => _SuccessPaymentPageState(); +} + +class _SuccessPaymentPageState extends State { + @override + void initState() { + super.initState(); + context + .read() + .add(OrderLoaderEvent.getById(widget.payment.orderId ?? "")); + } @override Widget build(BuildContext context) { @@ -25,137 +48,249 @@ class SuccessPaymentPage extends StatelessWidget { color: AppColors.white, borderRadius: const BorderRadius.all(Radius.circular(12.0)), ), - child: Column( - children: [ - Padding( - padding: const EdgeInsets.all(16.0), - child: Column( - children: [ - Text( - 'Pembayaran!', - style: const TextStyle( - fontSize: 18, fontWeight: FontWeight.bold), - ), - Text('Pembayaran berhasil dilalukan', - style: const TextStyle(fontSize: 14)), - ], + child: BlocBuilder( + builder: (context, state) { + return state.maybeWhen( + orElse: () => Center( + child: CircularProgressIndicator(), ), - ), - DashedDivider( - color: AppColors.grey, - ), - SpaceHeight(24), - Padding( - padding: const EdgeInsets.symmetric(horizontal: 16.0), - child: Icon( - Icons.check_circle_outline, - size: 64, - color: Colors.green, + loading: () => Center( + child: CircularProgressIndicator(), ), - ), - Spacer(), - Padding( - padding: const EdgeInsets.all(16.0).copyWith(top: 24), - child: Column( - children: [ - // Row( - // mainAxisAlignment: MainAxisAlignment.spaceBetween, - // children: [ - // Text( - // 'No. Pesanan', - // ), - // Text( - // order.orderNumber ?? "-", - // style: const TextStyle(fontWeight: FontWeight.bold), - // ), - // ], - // ), - SpaceHeight(4), - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text( - 'Waktu', - ), - Text( - (payment.createdAt ?? DateTime.now()) - .toFormattedDate3(), - style: const TextStyle(fontWeight: FontWeight.bold), - ), - ], - ), - ], + error: (message) => Center( + child: Text(message), ), - ), - DashedDivider( - color: AppColors.grey, - ), - Padding( - padding: const EdgeInsets.all(16.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, + loadedDetail: (order) => Column( children: [ - Text( - 'Status Pembayaran', - ), - Text( - 'Lunas', - style: const TextStyle( - fontWeight: FontWeight.bold, color: Colors.green), - ), - ], - ), - ), - DashedDivider( - color: AppColors.grey, - ), - Padding( - padding: const EdgeInsets.all(16.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text( - 'Total Pembayaran', - ), - Text( - (payment.amount ?? 0).toString().currencyFormatRpV2, - style: const TextStyle(fontWeight: FontWeight.bold), - ), - ], - ), - ), - DashedDivider( - color: AppColors.grey, - ), - Spacer(), - Padding( - padding: const EdgeInsets.all(16.0), - child: Row( - children: [ - Expanded( - child: Button.outlined( - onPressed: () => - context.pushReplacement(DashboardPage()), - label: 'Kembali', - height: 44, + Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + children: [ + Text( + 'Pembayaran!', + style: const TextStyle( + fontSize: 18, fontWeight: FontWeight.bold), + ), + Text('Pembayaran berhasil dilalukan', + style: const TextStyle(fontSize: 14)), + ], ), ), - SpaceWidth(12), - Expanded( - child: Button.filled( - onPressed: () {}, - label: 'Cetak', - icon: Icon( - Icons.print, - color: AppColors.white, - ), - height: 44, + DashedDivider( + color: AppColors.grey, + ), + SpaceHeight(24), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 16.0), + child: Icon( + Icons.check_circle_outline, + size: 64, + color: Colors.green, + ), + ), + Spacer(), + Padding( + padding: const EdgeInsets.all(16.0).copyWith(top: 24), + child: Column( + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'No. Pesanan', + ), + Text( + order.orderNumber ?? "-", + style: const TextStyle( + fontWeight: FontWeight.bold), + ), + ], + ), + SpaceHeight(4), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'Waktu', + ), + Text( + (widget.payment.createdAt ?? DateTime.now()) + .toFormattedDate3(), + style: const TextStyle( + fontWeight: FontWeight.bold), + ), + ], + ), + ], + ), + ), + DashedDivider( + color: AppColors.grey, + ), + Padding( + padding: const EdgeInsets.all(16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'Status Pembayaran', + ), + Text( + 'Lunas', + style: const TextStyle( + fontWeight: FontWeight.bold, + color: Colors.green), + ), + ], + ), + ), + DashedDivider( + color: AppColors.grey, + ), + Padding( + padding: const EdgeInsets.all(16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'Total Pembayaran', + ), + Text( + (widget.payment.amount ?? 0) + .toString() + .currencyFormatRpV2, + style: const TextStyle(fontWeight: FontWeight.bold), + ), + ], + ), + ), + DashedDivider( + color: AppColors.grey, + ), + Spacer(), + Padding( + padding: const EdgeInsets.all(16.0), + child: Row( + children: [ + Expanded( + child: Button.outlined( + onPressed: () => + context.pushReplacement(DashboardPage()), + label: 'Kembali', + height: 44, + ), + ), + SpaceWidth(12), + Expanded( + child: Button.filled( + onPressed: () async { + final checkerPrinter = + await ProductLocalDatasource.instance + .getPrinterByCode('checker'); + final kitchenPrinter = + await ProductLocalDatasource.instance + .getPrinterByCode('kitchen'); + final barPrinter = await ProductLocalDatasource + .instance + .getPrinterByCode('bar'); + + log("Checker printer: ${checkerPrinter?.toMap()}"); + log("Kitchen printer: ${kitchenPrinter?.toMap()}"); + log("Bar printer: ${barPrinter?.toMap()}"); + + // Checker printer + if (checkerPrinter != null) { + try { + final printValue = await PrintDataoutputs + .instance + .printChecker( + widget.productQuantity, + order.tableNumber ?? "", + order.orderNumber ?? "", + 'kasir', + checkerPrinter.paper.toIntegerFromText, + order.orderType ?? "", + ); + + await PrinterService().printWithPrinter( + checkerPrinter, printValue, context); + } catch (e) { + log("Error printing checker: $e"); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Error printing checker: $e')), + ); + } + } + + // Kitchen printer + if (kitchenPrinter != null) { + try { + final printValue = await PrintDataoutputs + .instance + .printKitchen( + widget.productQuantity, + order.tableNumber!, + order.orderNumber ?? "", + 'kasir', + kitchenPrinter.paper.toIntegerFromText, + order.orderType ?? "", + ); + + await PrinterService().printWithPrinter( + kitchenPrinter, printValue, context); + } catch (e) { + log("Error printing kitchen order: $e"); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Error printing kitchen order: $e')), + ); + } + } + + // Bar printer + if (barPrinter != null) { + try { + final printValue = await PrintDataoutputs + .instance + .printBar( + widget.productQuantity, + order.tableNumber ?? "", + order.orderNumber ?? "", + 'kasir', + barPrinter.paper.toIntegerFromText, + order.orderType ?? "", + ); + + await PrinterService().printWithPrinter( + barPrinter, printValue, context); + } catch (e) { + log("Error printing bar order: $e"); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Error printing bar order: $e')), + ); + } + } + }, + label: 'Cetak', + icon: Icon( + Icons.print, + color: AppColors.white, + ), + height: 44, + ), + ), + ], ), ), ], ), - ), - ], + ); + }, ), ), ), diff --git a/lib/presentation/success/pages/success_save_order_page.dart b/lib/presentation/success/pages/success_save_order_page.dart index 9680c47..9e0d889 100644 --- a/lib/presentation/success/pages/success_save_order_page.dart +++ b/lib/presentation/success/pages/success_save_order_page.dart @@ -9,16 +9,33 @@ import 'package:enaklo_pos/core/extensions/string_ext.dart'; import 'package:enaklo_pos/core/utils/printer_service.dart'; import 'package:enaklo_pos/data/dataoutputs/print_dataoutputs.dart'; import 'package:enaklo_pos/data/datasources/product_local_datasource.dart'; -import 'package:enaklo_pos/data/models/response/order_response_model.dart'; import 'package:enaklo_pos/presentation/home/models/product_quantity.dart'; import 'package:enaklo_pos/presentation/home/pages/dashboard_page.dart'; +import 'package:enaklo_pos/presentation/sales/blocs/order_loader/order_loader_bloc.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; -class SuccessSaveOrderPage extends StatelessWidget { +class SuccessSaveOrderPage extends StatefulWidget { final List productQuantity; - final Order order; - const SuccessSaveOrderPage( - {super.key, required this.order, required this.productQuantity}); + final String orderId; + const SuccessSaveOrderPage({ + super.key, + required this.orderId, + required this.productQuantity, + }); + + @override + State createState() => _SuccessSaveOrderPageState(); +} + +class _SuccessSaveOrderPageState extends State { + @override + void initState() { + super.initState(); + context + .read() + .add(OrderLoaderEvent.getById(widget.orderId)); + } @override Widget build(BuildContext context) { @@ -32,244 +49,267 @@ class SuccessSaveOrderPage extends StatelessWidget { color: AppColors.white, borderRadius: const BorderRadius.all(Radius.circular(12.0)), ), - child: Column( - children: [ - Padding( - padding: const EdgeInsets.all(16.0), - child: Column( - children: [ - Text( - 'Pesanan!', - style: const TextStyle( - fontSize: 18, fontWeight: FontWeight.bold), - ), - Text('Pesanan Berhasil disimpan', - style: const TextStyle(fontSize: 14)), - ], + child: BlocBuilder( + builder: (context, state) { + return state.maybeWhen( + orElse: () => Center( + child: CircularProgressIndicator(), ), - ), - DashedDivider( - color: AppColors.grey, - ), - SpaceHeight(24), - Padding( - padding: const EdgeInsets.symmetric(horizontal: 16.0), - child: Text( - order.metadata?['customer_name'] ?? "-", - style: const TextStyle( - fontSize: 24, - fontWeight: FontWeight.bold, - color: AppColors.primary, - ), - textAlign: TextAlign.center, - maxLines: 2, - overflow: TextOverflow.ellipsis, + loading: () => Center( + child: CircularProgressIndicator(), ), - ), - Padding( - padding: const EdgeInsets.all(16.0).copyWith(top: 24), - child: Column( - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text( - 'No. Pesanan', - ), - Text( - order.orderNumber ?? "-", - style: const TextStyle(fontWeight: FontWeight.bold), - ), - ], - ), - SpaceHeight(4), - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text( - 'No. Meja', - ), - Text( - order.tableNumber ?? "-", - style: const TextStyle(fontWeight: FontWeight.bold), - ), - ], - ), - SpaceHeight(4), - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text( - 'Waktu', - ), - Text( - (order.createdAt ?? DateTime.now()) - .toFormattedDate3(), - style: const TextStyle(fontWeight: FontWeight.bold), - ), - ], - ), - ], + error: (message) => Center( + child: Text(message), ), - ), - Spacer(), - DashedDivider( - color: AppColors.grey, - ), - Padding( - padding: const EdgeInsets.all(16.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, + loadedDetail: (orderx) => Column( children: [ - Text( - 'Status Pembayaran', - ), - Text( - 'Belum Bayar', - style: const TextStyle( - fontWeight: FontWeight.bold, color: Colors.red), - ), - ], - ), - ), - DashedDivider( - color: AppColors.grey, - ), - Padding( - padding: const EdgeInsets.all(16.0), - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text( - 'Total Pembayaran', - ), - Text( - (order.totalAmount ?? 0).toString().currencyFormatRpV2, - style: const TextStyle(fontWeight: FontWeight.bold), - ), - ], - ), - ), - DashedDivider( - color: AppColors.grey, - ), - Spacer(), - Padding( - padding: const EdgeInsets.all(16.0), - child: Row( - children: [ - Expanded( - child: Button.outlined( - onPressed: () => context.push(DashboardPage()), - label: 'Kembali', - height: 44, + Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + children: [ + Text( + 'Pesanan!', + style: const TextStyle( + fontSize: 18, fontWeight: FontWeight.bold), + ), + Text('Pesanan Berhasil disimpan', + style: const TextStyle(fontSize: 14)), + ], ), ), - SpaceWidth(12), - Expanded( - child: Button.filled( - onPressed: () async { - final checkerPrinter = await ProductLocalDatasource - .instance - .getPrinterByCode('checker'); - final kitchenPrinter = await ProductLocalDatasource - .instance - .getPrinterByCode('kitchen'); - final barPrinter = await ProductLocalDatasource - .instance - .getPrinterByCode('bar'); - - log("Checker printer: ${checkerPrinter?.toMap()}"); - log("Kitchen printer: ${kitchenPrinter?.toMap()}"); - log("Bar printer: ${barPrinter?.toMap()}"); - - // Checker printer - if (checkerPrinter != null) { - try { - final printValue = - await PrintDataoutputs.instance.printChecker( - productQuantity, - order.tableNumber ?? "", - order.orderNumber ?? "", - 'kasir', - checkerPrinter.paper.toIntegerFromText, - order.orderType ?? "", - ); - - await PrinterService().printWithPrinter( - checkerPrinter, printValue, context); - } catch (e) { - log("Error printing checker: $e"); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: - Text('Error printing checker: $e')), - ); - } - } - - // Kitchen printer - if (kitchenPrinter != null) { - try { - final printValue = - await PrintDataoutputs.instance.printKitchen( - productQuantity, - order.tableNumber!, - order.orderNumber ?? "", - 'kasir', - kitchenPrinter.paper.toIntegerFromText, - order.orderType ?? "", - ); - - await PrinterService().printWithPrinter( - kitchenPrinter, printValue, context); - } catch (e) { - log("Error printing kitchen order: $e"); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text( - 'Error printing kitchen order: $e')), - ); - } - } - - // Bar printer - if (barPrinter != null) { - try { - final printValue = - await PrintDataoutputs.instance.printBar( - productQuantity, - order.tableNumber ?? "", - order.orderNumber ?? "", - 'kasir', - barPrinter.paper.toIntegerFromText, - order.orderType ?? "", - ); - - await PrinterService().printWithPrinter( - barPrinter, printValue, context); - } catch (e) { - log("Error printing bar order: $e"); - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: - Text('Error printing bar order: $e')), - ); - } - } - }, - label: 'Cetak', - icon: Icon( - Icons.print, - color: AppColors.white, + DashedDivider( + color: AppColors.grey, + ), + SpaceHeight(24), + Padding( + padding: const EdgeInsets.symmetric(horizontal: 16.0), + child: Text( + orderx.metadata?['customer_name'] ?? "-", + style: const TextStyle( + fontSize: 24, + fontWeight: FontWeight.bold, + color: AppColors.primary, ), - height: 44, + textAlign: TextAlign.center, + maxLines: 2, + overflow: TextOverflow.ellipsis, + ), + ), + Padding( + padding: const EdgeInsets.all(16.0).copyWith(top: 24), + child: Column( + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'No. Pesanan', + ), + Text( + orderx.orderNumber ?? "-", + style: const TextStyle( + fontWeight: FontWeight.bold), + ), + ], + ), + SpaceHeight(4), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'No. Meja', + ), + Text( + orderx.tableNumber ?? "-", + style: const TextStyle( + fontWeight: FontWeight.bold), + ), + ], + ), + SpaceHeight(4), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'Waktu', + ), + Text( + (orderx.createdAt ?? DateTime.now()) + .toFormattedDate3(), + style: const TextStyle( + fontWeight: FontWeight.bold), + ), + ], + ), + ], + ), + ), + Spacer(), + DashedDivider( + color: AppColors.grey, + ), + Padding( + padding: const EdgeInsets.all(16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'Status Pembayaran', + ), + Text( + 'Belum Bayar', + style: const TextStyle( + fontWeight: FontWeight.bold, color: Colors.red), + ), + ], + ), + ), + DashedDivider( + color: AppColors.grey, + ), + Padding( + padding: const EdgeInsets.all(16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text( + 'Total Pembayaran', + ), + Text( + (orderx.totalAmount ?? 0) + .toString() + .currencyFormatRpV2, + style: const TextStyle(fontWeight: FontWeight.bold), + ), + ], + ), + ), + DashedDivider( + color: AppColors.grey, + ), + Spacer(), + Padding( + padding: const EdgeInsets.all(16.0), + child: Row( + children: [ + Expanded( + child: Button.outlined( + onPressed: () => context.push(DashboardPage()), + label: 'Kembali', + height: 44, + ), + ), + SpaceWidth(12), + Expanded( + child: Button.filled( + onPressed: () async { + final checkerPrinter = + await ProductLocalDatasource.instance + .getPrinterByCode('checker'); + final kitchenPrinter = + await ProductLocalDatasource.instance + .getPrinterByCode('kitchen'); + final barPrinter = await ProductLocalDatasource + .instance + .getPrinterByCode('bar'); + + log("Checker printer: ${checkerPrinter?.toMap()}"); + log("Kitchen printer: ${kitchenPrinter?.toMap()}"); + log("Bar printer: ${barPrinter?.toMap()}"); + + // Checker printer + if (checkerPrinter != null) { + try { + final printValue = await PrintDataoutputs + .instance + .printChecker( + widget.productQuantity, + orderx.tableNumber ?? "", + orderx.orderNumber ?? "", + 'kasir', + checkerPrinter.paper.toIntegerFromText, + orderx.orderType ?? "", + ); + + await PrinterService().printWithPrinter( + checkerPrinter, printValue, context); + } catch (e) { + log("Error printing checker: $e"); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Error printing checker: $e')), + ); + } + } + + // Kitchen printer + if (kitchenPrinter != null) { + try { + final printValue = await PrintDataoutputs + .instance + .printKitchen( + widget.productQuantity, + orderx.tableNumber!, + orderx.orderNumber ?? "", + 'kasir', + kitchenPrinter.paper.toIntegerFromText, + orderx.orderType ?? "", + ); + + await PrinterService().printWithPrinter( + kitchenPrinter, printValue, context); + } catch (e) { + log("Error printing kitchen order: $e"); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Error printing kitchen order: $e')), + ); + } + } + + // Bar printer + if (barPrinter != null) { + try { + final printValue = await PrintDataoutputs + .instance + .printBar( + widget.productQuantity, + orderx.tableNumber ?? "", + orderx.orderNumber ?? "", + 'kasir', + barPrinter.paper.toIntegerFromText, + orderx.orderType ?? "", + ); + + await PrinterService().printWithPrinter( + barPrinter, printValue, context); + } catch (e) { + log("Error printing bar order: $e"); + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text( + 'Error printing bar order: $e')), + ); + } + } + }, + label: 'Cetak', + icon: Icon( + Icons.print, + color: AppColors.white, + ), + height: 44, + ), + ), + ], ), ), ], ), - ), - ], + ); + }, ), ), ),