print order add item

This commit is contained in:
efrilm 2025-11-07 16:45:36 +07:00
parent 93560b85cb
commit 0886e8c912
4 changed files with 83 additions and 40 deletions

View File

@ -161,6 +161,17 @@ class Order with _$Order {
paymentCount: 1,
splitType: 'Single',
);
// For Add Item Order
factory Order.fromLastOrder(Order order, List<ProductQuantity> 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,
);
}

View File

@ -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';

View File

@ -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,7 +16,20 @@ class SuccessAddItemOrderPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
return BlocListener<PrintStruckBloc, PrintStruckState>(
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<CheckoutFormBloc, CheckoutFormState>(
@ -42,6 +57,7 @@ class SuccessAddItemOrderPage extends StatelessWidget {
},
),
),
),
);
}
}

View File

@ -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<PrintStruckBloc>().add(
PrintStruckEvent.order(
Order.fromLastOrder(
checkoutState.orderAdded!,
checkoutState.items,
),
),
);
},
label: 'Cetak Struk',
icon: Icon(Icons.print_rounded, color: AppColor.white),