diff --git a/lib/domain/order/entities/order_entity.dart b/lib/domain/order/entities/order_entity.dart index 277baf4..929100c 100644 --- a/lib/domain/order/entities/order_entity.dart +++ b/lib/domain/order/entities/order_entity.dart @@ -161,6 +161,17 @@ class Order with _$Order { paymentCount: 1, splitType: 'Single', ); + + // For Add Item Order + factory Order.fromLastOrder(Order order, List products) => + order.copyWith( + orderItems: products + .map((e) => OrderItem.fromProductQuantity(e)) + .toList(), + totalAmount: products + .map((e) => e.product.price.toInt() * e.quantity) + .reduce((value, element) => value + element), + ); } @freezed @@ -202,4 +213,25 @@ class OrderItem with _$OrderItem { printerType: '', paidQuantity: 0, ); + + factory OrderItem.fromProductQuantity(ProductQuantity productQuantity) => + OrderItem( + id: '', + orderId: '', + productId: productQuantity.product.id, + productName: productQuantity.product.name, + productVariantId: productQuantity.variant?.id ?? '', + productVariantName: productQuantity.variant?.name ?? '', + quantity: productQuantity.quantity, + unitPrice: productQuantity.product.price.toInt(), + totalPrice: (productQuantity.product.price * productQuantity.quantity) + .toInt(), + modifiers: [], + notes: productQuantity.notes, + status: 'pending', + createdAt: DateTime.now(), + updatedAt: DateTime.now(), + printerType: productQuantity.product.printerType, + paidQuantity: 0, + ); } diff --git a/lib/domain/order/order.dart b/lib/domain/order/order.dart index 53f6d29..a3f8f4d 100644 --- a/lib/domain/order/order.dart +++ b/lib/domain/order/order.dart @@ -3,6 +3,7 @@ import 'package:freezed_annotation/freezed_annotation.dart'; import '../../common/api/api_failure.dart'; import '../../common/types/split_type.dart'; +import '../product/product.dart'; part 'order.freezed.dart'; diff --git a/lib/presentation/pages/order/pages/success_add_item_order/success_add_item_order_page.dart b/lib/presentation/pages/order/pages/success_add_item_order/success_add_item_order_page.dart index ef44ace..9f844e2 100644 --- a/lib/presentation/pages/order/pages/success_add_item_order/success_add_item_order_page.dart +++ b/lib/presentation/pages/order/pages/success_add_item_order/success_add_item_order_page.dart @@ -3,8 +3,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../../../../../application/checkout/checkout_form/checkout_form_bloc.dart'; +import '../../../../../application/printer/print_struck/print_struck_bloc.dart'; import '../../../../../common/theme/theme.dart'; import '../../../../components/spaces/space.dart'; +import '../../../../components/toast/flushbar.dart'; import 'widgets/success_add_item_order_left_panel.dart'; import 'widgets/success_add_item_order_right_panel.dart'; @@ -14,32 +16,46 @@ class SuccessAddItemOrderPage extends StatelessWidget { @override Widget build(BuildContext context) { - return Scaffold( - backgroundColor: AppColor.background, - body: SafeArea( - child: BlocBuilder( - builder: (context, checkoutState) { - return Padding( - padding: const EdgeInsets.all(24.0), - child: Row( - children: [ - Expanded( - flex: 35, - child: SuccessAddItemOrderLeftPanel( - checkoutState: checkoutState, + return BlocListener( + listenWhen: (p, c) => p.failureOrPrintStruck != c.failureOrPrintStruck, + listener: (context, state) { + state.failureOrPrintStruck.fold( + () {}, + (either) => either.fold( + (f) => AppFlushbar.showPrinterFailureToast(context, f), + (success) { + AppFlushbar.showSuccess(context, "Struck berhasil dicetak"); + }, + ), + ); + }, + child: Scaffold( + backgroundColor: AppColor.background, + body: SafeArea( + child: BlocBuilder( + builder: (context, checkoutState) { + return Padding( + padding: const EdgeInsets.all(24.0), + child: Row( + children: [ + Expanded( + flex: 35, + child: SuccessAddItemOrderLeftPanel( + checkoutState: checkoutState, + ), ), - ), - SpaceWidth(16), - Expanded( - flex: 65, - child: SuccessAddItemOrderRightPanel( - checkoutState: checkoutState, + SpaceWidth(16), + Expanded( + flex: 65, + child: SuccessAddItemOrderRightPanel( + checkoutState: checkoutState, + ), ), - ), - ], - ), - ); - }, + ], + ), + ); + }, + ), ), ), ); diff --git a/lib/presentation/pages/order/pages/success_add_item_order/widgets/success_add_item_order_left_panel.dart b/lib/presentation/pages/order/pages/success_add_item_order/widgets/success_add_item_order_left_panel.dart index 06811c7..14f5fda 100644 --- a/lib/presentation/pages/order/pages/success_add_item_order/widgets/success_add_item_order_left_panel.dart +++ b/lib/presentation/pages/order/pages/success_add_item_order/widgets/success_add_item_order_left_panel.dart @@ -3,8 +3,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../../../../../../application/checkout/checkout_form/checkout_form_bloc.dart'; +import '../../../../../../application/printer/print_struck/print_struck_bloc.dart'; import '../../../../../../common/extension/extension.dart'; import '../../../../../../common/theme/theme.dart'; +import '../../../../../../domain/order/order.dart'; import '../../../../../components/button/button.dart'; import '../../../../../components/spaces/space.dart'; import '../../../../../router/app_router.gr.dart'; @@ -186,22 +188,14 @@ class SuccessAddItemOrderLeftPanel extends StatelessWidget { Expanded( child: AppElevatedButton.filled( onPressed: () { - // onPrintRecipt( - // context, - // order: widget.order, - // paymentMethod: widget.paymentMethod, - // nominalBayar: widget.paymentMethod == "Cash" - // ? widget.nominalBayar - // : widget.order.totalAmount ?? 0, - // kembalian: widget.nominalBayar - - // (widget.order.totalAmount ?? 0), - // productQuantity: widget.productQuantity, - // ); - // onPrint( - // context, - // productQuantity: widget.productQuantity, - // order: widget.order, - // ); + context.read().add( + PrintStruckEvent.order( + Order.fromLastOrder( + checkoutState.orderAdded!, + checkoutState.items, + ), + ), + ); }, label: 'Cetak Struk', icon: Icon(Icons.print_rounded, color: AppColor.white),