2025-07-30 22:38:44 +07:00
|
|
|
// ignore_for_file: public_member_api_docs, sort_constructors_first
|
|
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
|
|
|
|
|
|
import 'package:enaklo_pos/core/extensions/int_ext.dart';
|
|
|
|
|
import 'package:enaklo_pos/core/extensions/string_ext.dart';
|
|
|
|
|
import 'package:enaklo_pos/data/datasources/product_local_datasource.dart';
|
|
|
|
|
import 'package:enaklo_pos/data/models/response/table_model.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/bloc/checkout/checkout_bloc.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/bloc/get_table_status/get_table_status_bloc.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/bloc/order/order_bloc.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/bloc/payment_methods/payment_methods_bloc.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/bloc/status_table/status_table_bloc.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/models/product_quantity.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/models/order_type.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/widgets/order_menu.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/widgets/success_payment_dialog.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/home/widgets/order_type_selector.dart';
|
|
|
|
|
import 'package:enaklo_pos/presentation/table/models/draft_order_model.dart';
|
|
|
|
|
import 'package:enaklo_pos/data/models/response/payment_methods_response_model.dart';
|
|
|
|
|
|
|
|
|
|
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
|
|
|
|
|
|
|
|
|
|
import '../../../core/components/buttons.dart';
|
|
|
|
|
import '../../../core/components/spaces.dart';
|
|
|
|
|
import '../../../core/constants/colors.dart';
|
|
|
|
|
|
|
|
|
|
class PaymentTablePage extends StatefulWidget {
|
|
|
|
|
final DraftOrderModel? draftOrder;
|
|
|
|
|
final TableModel? table;
|
|
|
|
|
const PaymentTablePage({
|
2025-08-03 00:47:49 +07:00
|
|
|
super.key,
|
2025-07-30 22:38:44 +07:00
|
|
|
this.draftOrder,
|
|
|
|
|
this.table,
|
2025-08-03 00:47:49 +07:00
|
|
|
});
|
2025-07-30 22:38:44 +07:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<PaymentTablePage> createState() => _PaymentTablePageState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _PaymentTablePageState extends State<PaymentTablePage> {
|
|
|
|
|
final totalPriceController = TextEditingController();
|
|
|
|
|
final customerController = TextEditingController();
|
|
|
|
|
PaymentMethod? selectedPaymentMethod;
|
|
|
|
|
int totalPriceFinal = 0;
|
|
|
|
|
int discountAmountFinal = 0;
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
// Helper method to handle post-payment cleanup
|
|
|
|
|
Future<void> _handlePostPaymentCleanup() async {
|
|
|
|
|
if (widget.table != null && widget.draftOrder?.id != null) {
|
|
|
|
|
// Update table status to available
|
|
|
|
|
final newTable = TableModel(
|
|
|
|
|
id: widget.table!.id,
|
|
|
|
|
tableName: widget.table!.tableName,
|
|
|
|
|
status: 'available',
|
|
|
|
|
orderId: 0,
|
|
|
|
|
paymentAmount: 0,
|
|
|
|
|
startTime: DateTime.now().toIso8601String(),
|
|
|
|
|
position: widget.table!.position,
|
|
|
|
|
);
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
// Update table status
|
|
|
|
|
await ProductLocalDatasource.instance.updateStatusTable(newTable);
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
// Remove draft order
|
2025-08-03 00:35:00 +07:00
|
|
|
await ProductLocalDatasource.instance
|
|
|
|
|
.removeDraftOrderById(widget.draftOrder!.id!);
|
|
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
// Refresh table status
|
|
|
|
|
context.read<GetTableStatusBloc>().add(
|
2025-08-03 00:35:00 +07:00
|
|
|
GetTableStatusEvent.getTablesStatus('all'),
|
|
|
|
|
);
|
|
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
log("Table ${widget.table!.tableName} freed up and draft order removed");
|
|
|
|
|
}
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
// Safely navigate back - pop multiple times to get to table management
|
|
|
|
|
// Pop the success dialog first, then the payment page
|
|
|
|
|
if (Navigator.of(context).canPop()) {
|
|
|
|
|
Navigator.of(context).pop(); // Pop success dialog
|
|
|
|
|
if (Navigator.of(context).canPop()) {
|
|
|
|
|
Navigator.of(context).pop(); // Pop payment page
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
context
|
|
|
|
|
.read<GetTableStatusBloc>()
|
|
|
|
|
.add(GetTableStatusEvent.getTablesStatus('available'));
|
|
|
|
|
context
|
|
|
|
|
.read<PaymentMethodsBloc>()
|
|
|
|
|
.add(PaymentMethodsEvent.fetchPaymentMethods());
|
|
|
|
|
super.initState();
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
// Set a default payment method in case API fails
|
|
|
|
|
selectedPaymentMethod = PaymentMethod(
|
2025-08-03 00:47:49 +07:00
|
|
|
id: "4b1c0d21-c98a-4fc0-a2f9-8d90a0c9d905",
|
|
|
|
|
organizationId: "3e8b1793-d18b-40c4-a03d-0c6480b630c7",
|
|
|
|
|
name: "CASH",
|
|
|
|
|
type: "cash",
|
2025-07-30 22:38:44 +07:00
|
|
|
isActive: true,
|
2025-08-03 00:47:49 +07:00
|
|
|
createdAt: DateTime.tryParse('2025-07-18T03:43:13.857048+07:00'),
|
|
|
|
|
updatedAt: DateTime.tryParse('2025-07-18T03:43:13.857048+07:00'),
|
2025-07-30 22:38:44 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
totalPriceController.dispose();
|
|
|
|
|
customerController.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return SafeArea(
|
|
|
|
|
child: Hero(
|
|
|
|
|
tag: 'table_payment_screen',
|
|
|
|
|
child: Scaffold(
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
title: Text('Payment - Table ${widget.table?.tableName}'),
|
|
|
|
|
backgroundColor: AppColors.primary,
|
|
|
|
|
foregroundColor: Colors.white,
|
|
|
|
|
leading: IconButton(
|
|
|
|
|
icon: const Icon(Icons.arrow_back),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: const Text('Kembali'),
|
2025-08-03 00:35:00 +07:00
|
|
|
content: const Text(
|
|
|
|
|
'Apakah Anda yakin ingin kembali? Order akan tetap tersimpan.'),
|
2025-07-30 22:38:44 +07:00
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.of(context).pop(); // Close dialog
|
|
|
|
|
},
|
|
|
|
|
child: const Text('Tidak'),
|
|
|
|
|
),
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
Navigator.of(context).pop(); // Close dialog
|
2025-08-03 00:35:00 +07:00
|
|
|
Navigator.of(context)
|
|
|
|
|
.pop(); // Go back to previous page
|
2025-07-30 22:38:44 +07:00
|
|
|
},
|
|
|
|
|
child: const Text('Ya'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
child: const Text(
|
|
|
|
|
'Kembali',
|
2025-08-03 00:35:00 +07:00
|
|
|
style: TextStyle(
|
|
|
|
|
color: Colors.white, fontWeight: FontWeight.bold),
|
2025-07-30 22:38:44 +07:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
body: Row(
|
|
|
|
|
children: [
|
|
|
|
|
// LEFT CONTENT
|
|
|
|
|
Expanded(
|
|
|
|
|
flex: 2,
|
|
|
|
|
child: Align(
|
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
padding: const EdgeInsets.all(24.0),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
'Konfirmasi',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontSize: 20,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Text(
|
|
|
|
|
'Orders Table ${widget.table?.tableName}',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const Divider(),
|
|
|
|
|
const SpaceHeight(24.0),
|
|
|
|
|
const Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
'Item',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
width: 160,
|
|
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
width: 50.0,
|
|
|
|
|
child: Text(
|
|
|
|
|
'Qty',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
child: Text(
|
|
|
|
|
'Price',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(8),
|
|
|
|
|
const Divider(),
|
|
|
|
|
const SpaceHeight(8),
|
|
|
|
|
BlocBuilder<CheckoutBloc, CheckoutState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
return state.maybeWhen(
|
|
|
|
|
orElse: () => const Center(
|
|
|
|
|
child: Text('No Items'),
|
|
|
|
|
),
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) {
|
2025-07-30 22:38:44 +07:00
|
|
|
if (products.isEmpty) {
|
|
|
|
|
return const Center(
|
|
|
|
|
child: Text('No Items'),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return ListView.separated(
|
|
|
|
|
shrinkWrap: true,
|
|
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
|
itemBuilder: (context, index) =>
|
|
|
|
|
OrderMenu(data: products[index]),
|
|
|
|
|
separatorBuilder: (context, index) =>
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
itemCount: products.length,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const Divider(),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
const Text(
|
|
|
|
|
'Sub total',
|
|
|
|
|
style: TextStyle(color: AppColors.grey),
|
|
|
|
|
),
|
|
|
|
|
BlocBuilder<CheckoutBloc, CheckoutState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
final price = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
products.fold(
|
|
|
|
|
0,
|
|
|
|
|
(previousValue, element) =>
|
|
|
|
|
previousValue +
|
2025-08-03 00:35:00 +07:00
|
|
|
(element.product.price! *
|
2025-07-30 22:38:44 +07:00
|
|
|
element.quantity),
|
|
|
|
|
));
|
|
|
|
|
return Text(
|
|
|
|
|
price.currencyFormatRp,
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
'Diskon',
|
|
|
|
|
style: TextStyle(color: AppColors.grey),
|
|
|
|
|
),
|
|
|
|
|
BlocBuilder<CheckoutBloc, CheckoutState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
final discount = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
|
|
|
|
draftName,
|
|
|
|
|
orderType) {
|
|
|
|
|
log("discountAmount: $discountAmount");
|
|
|
|
|
return discountAmount;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
discountAmountFinal = discount.toInt();
|
|
|
|
|
|
|
|
|
|
return Text(
|
|
|
|
|
discount.toInt().currencyFormatRp,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
const Text(
|
|
|
|
|
'Pajak PB1',
|
|
|
|
|
style: TextStyle(color: AppColors.grey),
|
|
|
|
|
),
|
|
|
|
|
BlocBuilder<CheckoutBloc, CheckoutState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
final tax = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
tax,
|
|
|
|
|
);
|
|
|
|
|
final price = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
products.fold(
|
|
|
|
|
0,
|
|
|
|
|
(previousValue, element) =>
|
|
|
|
|
previousValue +
|
2025-08-03 00:35:00 +07:00
|
|
|
(element.product.price! *
|
2025-07-30 22:38:44 +07:00
|
|
|
element.quantity),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final discount = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) {
|
2025-07-30 22:38:44 +07:00
|
|
|
return discountAmount;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final subTotal = price - discount;
|
|
|
|
|
final finalTax = subTotal * (tax / 100);
|
|
|
|
|
return Text(
|
|
|
|
|
'$tax % (${finalTax.toInt().currencyFormatRp})',
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
const Text(
|
|
|
|
|
'Biaya Layanan',
|
|
|
|
|
style: TextStyle(color: AppColors.grey),
|
|
|
|
|
),
|
|
|
|
|
BlocBuilder<CheckoutBloc, CheckoutState>(
|
|
|
|
|
builder: (context, state) {
|
2025-08-03 00:47:49 +07:00
|
|
|
state.maybeWhen(
|
2025-07-30 22:38:44 +07:00
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
tax,
|
|
|
|
|
);
|
|
|
|
|
final price = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
products.fold(
|
|
|
|
|
0,
|
|
|
|
|
(previousValue, element) =>
|
|
|
|
|
previousValue +
|
2025-08-03 00:35:00 +07:00
|
|
|
(element.product.price! *
|
2025-07-30 22:38:44 +07:00
|
|
|
element.quantity),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final discount = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) {
|
2025-07-30 22:38:44 +07:00
|
|
|
return discountAmount;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final serviceCharge = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
serviceCharge,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final subTotal = price - discount;
|
|
|
|
|
final finalServiceCharge =
|
|
|
|
|
subTotal * (serviceCharge / 100);
|
|
|
|
|
|
|
|
|
|
return Text(
|
|
|
|
|
'$serviceCharge % (${finalServiceCharge.toInt().currencyFormatRp}) ',
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
const Text(
|
|
|
|
|
'Total',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.grey,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
fontSize: 16),
|
|
|
|
|
),
|
|
|
|
|
BlocBuilder<CheckoutBloc, CheckoutState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
final price = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
products.fold(
|
|
|
|
|
0,
|
|
|
|
|
(previousValue, element) =>
|
|
|
|
|
previousValue +
|
2025-08-03 00:35:00 +07:00
|
|
|
(element.product.price! *
|
2025-07-30 22:38:44 +07:00
|
|
|
element.quantity),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final discount = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) {
|
2025-07-30 22:38:44 +07:00
|
|
|
return discountAmount;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final serviceCharge = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
serviceCharge,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final tax = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
2025-07-30 22:38:44 +07:00
|
|
|
tax,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final subTotal = price - discount;
|
|
|
|
|
final finalTax = subTotal * (tax / 100);
|
|
|
|
|
final service =
|
|
|
|
|
subTotal * (serviceCharge / 100);
|
|
|
|
|
final total = subTotal + finalTax + service;
|
|
|
|
|
totalPriceFinal = total.ceil();
|
|
|
|
|
totalPriceController.text =
|
|
|
|
|
total.ceil().toString();
|
|
|
|
|
return Text(
|
|
|
|
|
total.ceil().currencyFormatRp,
|
|
|
|
|
style: const TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
// const SpaceHeight(20.0),
|
|
|
|
|
// Button.filled(
|
|
|
|
|
// onPressed: () {},
|
|
|
|
|
// label: 'Lanjutkan Pembayaran',
|
|
|
|
|
// ),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
// RIGHT CONTENT
|
|
|
|
|
Expanded(
|
|
|
|
|
flex: 3,
|
|
|
|
|
child: Align(
|
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
|
child: Stack(
|
|
|
|
|
children: [
|
|
|
|
|
SingleChildScrollView(
|
|
|
|
|
padding: const EdgeInsets.all(24.0),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
const Text(
|
|
|
|
|
'Pembayaran',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontSize: 20,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
const Divider(),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const Text(
|
|
|
|
|
'Customer',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(12.0),
|
|
|
|
|
BlocBuilder<CheckoutBloc, CheckoutState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
return state.maybeWhen(
|
|
|
|
|
orElse: () {
|
|
|
|
|
return SizedBox.shrink();
|
|
|
|
|
},
|
|
|
|
|
loaded: (items,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
2025-08-03 00:35:00 +07:00
|
|
|
draftName,
|
|
|
|
|
orderType) {
|
2025-07-30 22:38:44 +07:00
|
|
|
customerController.text = draftName;
|
|
|
|
|
return TextFormField(
|
|
|
|
|
readOnly: true,
|
|
|
|
|
controller: customerController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
border: OutlineInputBorder(
|
|
|
|
|
borderRadius:
|
|
|
|
|
BorderRadius.circular(8.0),
|
|
|
|
|
),
|
|
|
|
|
hintText: 'Nama Customer',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const Divider(),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const OrderTypeSelector(),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const Divider(),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const Text(
|
|
|
|
|
'Metode Bayar',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontSize: 20,
|
|
|
|
|
fontWeight: FontWeight.w600,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(12.0),
|
2025-08-03 00:35:00 +07:00
|
|
|
BlocBuilder<PaymentMethodsBloc,
|
|
|
|
|
PaymentMethodsState>(
|
2025-07-30 22:38:44 +07:00
|
|
|
builder: (context, state) {
|
|
|
|
|
return state.maybeWhen(
|
|
|
|
|
orElse: () => const Center(
|
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
|
),
|
|
|
|
|
loading: () => const Center(
|
|
|
|
|
child: Column(
|
|
|
|
|
children: [
|
|
|
|
|
CircularProgressIndicator(),
|
|
|
|
|
SizedBox(height: 8.0),
|
|
|
|
|
Text('Loading payment methods...'),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
error: (message) => Column(
|
|
|
|
|
children: [
|
|
|
|
|
Center(
|
2025-08-03 00:35:00 +07:00
|
|
|
child: Text(
|
|
|
|
|
'Error loading payment methods: $message'),
|
2025-07-30 22:38:44 +07:00
|
|
|
),
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
Button.filled(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
context
|
|
|
|
|
.read<PaymentMethodsBloc>()
|
2025-08-03 00:35:00 +07:00
|
|
|
.add(PaymentMethodsEvent
|
|
|
|
|
.fetchPaymentMethods());
|
2025-07-30 22:38:44 +07:00
|
|
|
},
|
|
|
|
|
label: 'Retry',
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
loaded: (paymentMethods) {
|
|
|
|
|
log("Loaded ${paymentMethods.length} payment methods");
|
|
|
|
|
paymentMethods.forEach((method) {
|
|
|
|
|
log("Payment method: ${method.name} (ID: ${method.id})");
|
|
|
|
|
});
|
|
|
|
|
if (paymentMethods.isEmpty) {
|
|
|
|
|
return Column(
|
|
|
|
|
children: [
|
|
|
|
|
const Center(
|
2025-08-03 00:35:00 +07:00
|
|
|
child: Text(
|
|
|
|
|
'No payment methods available'),
|
2025-07-30 22:38:44 +07:00
|
|
|
),
|
|
|
|
|
const SpaceHeight(16.0),
|
|
|
|
|
Button.filled(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
context
|
|
|
|
|
.read<PaymentMethodsBloc>()
|
2025-08-03 00:35:00 +07:00
|
|
|
.add(PaymentMethodsEvent
|
|
|
|
|
.fetchPaymentMethods());
|
2025-07-30 22:38:44 +07:00
|
|
|
},
|
|
|
|
|
label: 'Retry',
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
// Set default selected payment method if none selected or if current selection is not in the list
|
2025-08-03 00:35:00 +07:00
|
|
|
if (selectedPaymentMethod == null ||
|
|
|
|
|
!paymentMethods.any((method) =>
|
|
|
|
|
method.id ==
|
|
|
|
|
selectedPaymentMethod?.id)) {
|
|
|
|
|
selectedPaymentMethod =
|
|
|
|
|
paymentMethods.first;
|
2025-07-30 22:38:44 +07:00
|
|
|
}
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
return Wrap(
|
|
|
|
|
spacing: 12.0,
|
|
|
|
|
runSpacing: 8.0,
|
|
|
|
|
children: paymentMethods.map((method) {
|
2025-08-03 00:35:00 +07:00
|
|
|
final isSelected =
|
|
|
|
|
selectedPaymentMethod?.id ==
|
|
|
|
|
method.id;
|
2025-07-30 22:38:44 +07:00
|
|
|
return Container(
|
|
|
|
|
constraints: const BoxConstraints(
|
|
|
|
|
minWidth: 120.0,
|
|
|
|
|
),
|
|
|
|
|
decoration: isSelected
|
|
|
|
|
? BoxDecoration(
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
width: 2.0,
|
|
|
|
|
),
|
2025-08-03 00:35:00 +07:00
|
|
|
borderRadius:
|
|
|
|
|
BorderRadius.circular(
|
|
|
|
|
8.0),
|
2025-07-30 22:38:44 +07:00
|
|
|
)
|
|
|
|
|
: null,
|
2025-08-03 00:47:49 +07:00
|
|
|
child: isSelected
|
|
|
|
|
? Button.filled(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
height: 50.0,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
setState(() {
|
|
|
|
|
selectedPaymentMethod =
|
|
|
|
|
method;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
label: method.name
|
|
|
|
|
?.isNotEmpty ==
|
|
|
|
|
true
|
|
|
|
|
? method.name!
|
|
|
|
|
: 'Unknown',
|
|
|
|
|
)
|
|
|
|
|
: Button.outlined(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
height: 50.0,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
setState(() {
|
|
|
|
|
selectedPaymentMethod =
|
|
|
|
|
method;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
label: method.name
|
|
|
|
|
?.isNotEmpty ==
|
|
|
|
|
true
|
|
|
|
|
? method.name!
|
|
|
|
|
: 'Unknown',
|
|
|
|
|
),
|
2025-07-30 22:38:44 +07:00
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const Divider(),
|
|
|
|
|
const SpaceHeight(8.0),
|
|
|
|
|
const Text(
|
|
|
|
|
'Total Bayar',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: AppColors.primary,
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: FontWeight.w500,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(12.0),
|
|
|
|
|
TextFormField(
|
|
|
|
|
controller: totalPriceController,
|
|
|
|
|
keyboardType: TextInputType.number,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
border: OutlineInputBorder(
|
|
|
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
|
|
|
),
|
|
|
|
|
hintText: 'Total harga',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(45.0),
|
|
|
|
|
Row(
|
|
|
|
|
children: [
|
|
|
|
|
Button.filled(
|
|
|
|
|
width: 150.0,
|
|
|
|
|
onPressed: () {},
|
|
|
|
|
label: 'UANG PAS',
|
|
|
|
|
),
|
|
|
|
|
const SpaceWidth(20.0),
|
|
|
|
|
Button.filled(
|
|
|
|
|
width: 150.0,
|
|
|
|
|
onPressed: () {},
|
|
|
|
|
label: 'Rp 250.000',
|
|
|
|
|
),
|
|
|
|
|
const SpaceWidth(20.0),
|
|
|
|
|
Button.filled(
|
|
|
|
|
width: 150.0,
|
|
|
|
|
onPressed: () {},
|
|
|
|
|
label: 'Rp 300.000',
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SpaceHeight(100.0),
|
|
|
|
|
]),
|
|
|
|
|
),
|
|
|
|
|
Align(
|
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
|
child: ColoredBox(
|
|
|
|
|
color: AppColors.white,
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: 24.0, vertical: 16.0),
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Button.outlined(
|
|
|
|
|
onPressed: () => context.pop(),
|
|
|
|
|
label: 'Kembali',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceWidth(8.0),
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Button.outlined(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
// Show void confirmation dialog
|
|
|
|
|
showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
title: Row(
|
|
|
|
|
children: [
|
2025-08-03 00:35:00 +07:00
|
|
|
Icon(Icons.warning,
|
|
|
|
|
color: AppColors.red),
|
2025-07-30 22:38:44 +07:00
|
|
|
SizedBox(width: 8),
|
2025-08-03 00:35:00 +07:00
|
|
|
Text('Batalkan Pesanan?'),
|
2025-07-30 22:38:44 +07:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
content: Text(
|
|
|
|
|
'Apakah anda yakin ingin membatalkan pesanan untuk meja ${widget.table?.tableName ?? "ini"}?\n\nPesanan akan dihapus secara permanen.'),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
2025-08-03 00:35:00 +07:00
|
|
|
onPressed: () =>
|
|
|
|
|
Navigator.pop(context),
|
|
|
|
|
child: Text('Tidak',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color:
|
|
|
|
|
AppColors.primary)),
|
2025-07-30 22:38:44 +07:00
|
|
|
),
|
2025-08-03 00:35:00 +07:00
|
|
|
BlocListener<StatusTableBloc,
|
|
|
|
|
StatusTableState>(
|
2025-07-30 22:38:44 +07:00
|
|
|
listener: (context, state) {
|
|
|
|
|
state.maybeWhen(
|
|
|
|
|
orElse: () {},
|
|
|
|
|
success: () {
|
2025-08-03 00:35:00 +07:00
|
|
|
Navigator.pop(
|
|
|
|
|
context); // Close void dialog
|
|
|
|
|
Navigator.pop(
|
|
|
|
|
context); // Close payment page
|
|
|
|
|
ScaffoldMessenger.of(
|
|
|
|
|
context)
|
|
|
|
|
.showSnackBar(
|
2025-07-30 22:38:44 +07:00
|
|
|
const SnackBar(
|
2025-08-03 00:35:00 +07:00
|
|
|
content: Text(
|
|
|
|
|
'Pesanan berhasil dibatalkan'),
|
|
|
|
|
backgroundColor:
|
|
|
|
|
AppColors.primary,
|
2025-07-30 22:38:44 +07:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
child: ElevatedButton(
|
|
|
|
|
style: ElevatedButton.styleFrom(
|
2025-08-03 00:35:00 +07:00
|
|
|
backgroundColor:
|
|
|
|
|
AppColors.red,
|
2025-07-30 22:38:44 +07:00
|
|
|
),
|
|
|
|
|
onPressed: () {
|
|
|
|
|
// Void the order
|
|
|
|
|
if (widget.table != null) {
|
|
|
|
|
final newTable = TableModel(
|
|
|
|
|
id: widget.table!.id,
|
2025-08-03 00:35:00 +07:00
|
|
|
tableName: widget
|
|
|
|
|
.table!.tableName,
|
2025-07-30 22:38:44 +07:00
|
|
|
status: 'available',
|
|
|
|
|
orderId: 0,
|
|
|
|
|
paymentAmount: 0,
|
2025-08-03 00:35:00 +07:00
|
|
|
startTime: DateTime.now()
|
|
|
|
|
.toIso8601String(),
|
|
|
|
|
position: widget
|
|
|
|
|
.table!.position,
|
2025-07-30 22:38:44 +07:00
|
|
|
);
|
2025-08-03 00:35:00 +07:00
|
|
|
context
|
|
|
|
|
.read<StatusTableBloc>()
|
|
|
|
|
.add(
|
|
|
|
|
StatusTableEvent
|
|
|
|
|
.statusTabel(
|
|
|
|
|
newTable),
|
2025-07-30 22:38:44 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// Remove draft order from local storage
|
2025-08-03 00:35:00 +07:00
|
|
|
if (widget.draftOrder?.id !=
|
|
|
|
|
null) {
|
|
|
|
|
ProductLocalDatasource
|
|
|
|
|
.instance
|
|
|
|
|
.removeDraftOrderById(
|
|
|
|
|
widget.draftOrder!
|
|
|
|
|
.id!);
|
2025-07-30 22:38:44 +07:00
|
|
|
}
|
|
|
|
|
log("Voided order from payment page");
|
|
|
|
|
},
|
|
|
|
|
child: const Text(
|
|
|
|
|
"Ya, Batalkan",
|
2025-08-03 00:35:00 +07:00
|
|
|
style: TextStyle(
|
|
|
|
|
color: Colors.white),
|
2025-07-30 22:38:44 +07:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
label: 'Batalkan',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SpaceWidth(8.0),
|
|
|
|
|
BlocListener<OrderBloc, OrderState>(
|
|
|
|
|
listener: (context, state) {
|
|
|
|
|
final newTable = TableModel(
|
|
|
|
|
id: widget.table!.id,
|
|
|
|
|
tableName: widget.table!.tableName,
|
|
|
|
|
status: 'available',
|
|
|
|
|
orderId: 0,
|
|
|
|
|
paymentAmount: 0,
|
|
|
|
|
startTime:
|
|
|
|
|
DateTime.now().toIso8601String(),
|
|
|
|
|
position: widget.table!.position,
|
|
|
|
|
);
|
|
|
|
|
context.read<StatusTableBloc>().add(
|
|
|
|
|
StatusTableEvent.statusTabel(
|
|
|
|
|
newTable,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
ProductLocalDatasource.instance
|
|
|
|
|
.removeDraftOrderById(
|
|
|
|
|
widget.draftOrder!.id!);
|
|
|
|
|
},
|
|
|
|
|
child:
|
|
|
|
|
BlocBuilder<CheckoutBloc, CheckoutState>(
|
|
|
|
|
builder: (context, state) {
|
|
|
|
|
final discount = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
|
|
|
|
draftName,
|
|
|
|
|
orderType) {
|
|
|
|
|
if (discountModel == null) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2025-08-03 00:47:49 +07:00
|
|
|
return discountModel.value!
|
2025-07-30 22:38:44 +07:00
|
|
|
.replaceAll('.00', '')
|
|
|
|
|
.toIntegerFromText;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final price = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
|
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
|
|
|
|
products.fold(
|
|
|
|
|
0,
|
|
|
|
|
(previousValue, element) =>
|
|
|
|
|
previousValue +
|
2025-08-03 00:35:00 +07:00
|
|
|
(element.product.price! *
|
2025-07-30 22:38:44 +07:00
|
|
|
element.quantity),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final tax = state.maybeWhen(
|
|
|
|
|
orElse: () => 0,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
|
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
|
|
|
|
tax,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final subTotal =
|
|
|
|
|
price - (discount / 100 * price);
|
|
|
|
|
final totalDiscount =
|
|
|
|
|
discount / 100 * price;
|
|
|
|
|
final finalTax = subTotal * (tax / 100);
|
|
|
|
|
|
|
|
|
|
List<ProductQuantity> items =
|
|
|
|
|
state.maybeWhen(
|
|
|
|
|
orElse: () => [],
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
|
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
|
|
|
|
products,
|
|
|
|
|
);
|
|
|
|
|
final totalQty = items.fold(
|
|
|
|
|
0,
|
|
|
|
|
(previousValue, element) =>
|
|
|
|
|
previousValue + element.quantity,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final orderType = state.maybeWhen(
|
|
|
|
|
orElse: () => OrderType.dineIn,
|
|
|
|
|
loaded: (products,
|
|
|
|
|
discountModel,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmount,
|
|
|
|
|
tax,
|
|
|
|
|
serviceCharge,
|
|
|
|
|
totalQuantity,
|
|
|
|
|
totalPrice,
|
|
|
|
|
draftName,
|
|
|
|
|
orderType) =>
|
|
|
|
|
orderType,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Flexible(
|
|
|
|
|
child: Button.filled(
|
|
|
|
|
onPressed: () async {
|
|
|
|
|
if (selectedPaymentMethod == null) {
|
2025-08-03 00:35:00 +07:00
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
|
.showSnackBar(
|
2025-07-30 22:38:44 +07:00
|
|
|
const SnackBar(
|
2025-08-03 00:35:00 +07:00
|
|
|
content: Text(
|
|
|
|
|
'Please select a payment method'),
|
2025-07-30 22:38:44 +07:00
|
|
|
backgroundColor: Colors.red,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-03 00:35:00 +07:00
|
|
|
|
|
|
|
|
final paymentMethodName =
|
|
|
|
|
selectedPaymentMethod?.name
|
|
|
|
|
?.toLowerCase() ??
|
|
|
|
|
'';
|
2025-07-30 22:38:44 +07:00
|
|
|
log("Selected payment method: ${selectedPaymentMethod?.name} (normalized: $paymentMethodName)");
|
2025-08-03 00:35:00 +07:00
|
|
|
|
|
|
|
|
if (paymentMethodName == 'cash' ||
|
2025-07-30 22:38:44 +07:00
|
|
|
paymentMethodName == 'tunai' ||
|
2025-08-03 00:35:00 +07:00
|
|
|
paymentMethodName ==
|
|
|
|
|
'uang tunai' ||
|
|
|
|
|
paymentMethodName ==
|
|
|
|
|
'cash payment') {
|
2025-07-30 22:38:44 +07:00
|
|
|
context.read<OrderBloc>().add(
|
|
|
|
|
OrderEvent.order(
|
|
|
|
|
items,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmountFinal,
|
|
|
|
|
finalTax.toInt(),
|
|
|
|
|
0,
|
|
|
|
|
totalPriceController.text
|
|
|
|
|
.toIntegerFromText,
|
|
|
|
|
customerController.text,
|
|
|
|
|
widget.table?.id ?? 0,
|
|
|
|
|
'completed',
|
|
|
|
|
'paid',
|
2025-08-03 00:35:00 +07:00
|
|
|
selectedPaymentMethod
|
|
|
|
|
?.name ??
|
|
|
|
|
'Cash',
|
2025-07-30 22:38:44 +07:00
|
|
|
totalPriceFinal,
|
|
|
|
|
orderType));
|
|
|
|
|
|
|
|
|
|
await showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
barrierDismissible: false,
|
|
|
|
|
builder: (context) =>
|
|
|
|
|
SuccessPaymentDialog(
|
|
|
|
|
isTablePaymentPage: true,
|
|
|
|
|
data: items,
|
|
|
|
|
totalQty: totalQty,
|
|
|
|
|
totalPrice:
|
|
|
|
|
totalPriceFinal.toInt(),
|
|
|
|
|
totalTax: finalTax.toInt(),
|
|
|
|
|
totalDiscount:
|
|
|
|
|
totalDiscount.toInt(),
|
|
|
|
|
subTotal: subTotal.toInt(),
|
|
|
|
|
normalPrice: price,
|
|
|
|
|
totalService: 0,
|
|
|
|
|
draftName:
|
|
|
|
|
customerController.text,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
await _handlePostPaymentCleanup();
|
|
|
|
|
} else {
|
|
|
|
|
log("Processing non-cash payment: ${selectedPaymentMethod?.name}");
|
|
|
|
|
context.read<OrderBloc>().add(
|
|
|
|
|
OrderEvent.order(
|
|
|
|
|
items,
|
|
|
|
|
discount,
|
|
|
|
|
discountAmountFinal,
|
|
|
|
|
finalTax.toInt(),
|
|
|
|
|
0,
|
|
|
|
|
totalPriceController.text
|
|
|
|
|
.toIntegerFromText,
|
|
|
|
|
customerController.text,
|
|
|
|
|
widget.table?.id ?? 0,
|
|
|
|
|
'completed',
|
|
|
|
|
'paid',
|
2025-08-03 00:35:00 +07:00
|
|
|
selectedPaymentMethod
|
|
|
|
|
?.name ??
|
|
|
|
|
'Unknown Payment Method',
|
2025-07-30 22:38:44 +07:00
|
|
|
totalPriceFinal,
|
|
|
|
|
orderType));
|
|
|
|
|
|
|
|
|
|
await showDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
barrierDismissible: false,
|
|
|
|
|
builder: (context) =>
|
|
|
|
|
SuccessPaymentDialog(
|
|
|
|
|
isTablePaymentPage: true,
|
|
|
|
|
data: items,
|
|
|
|
|
totalQty: totalQty,
|
|
|
|
|
totalPrice:
|
|
|
|
|
totalPriceFinal.toInt(),
|
|
|
|
|
totalTax: finalTax.toInt(),
|
|
|
|
|
totalDiscount:
|
|
|
|
|
totalDiscount.toInt(),
|
|
|
|
|
subTotal: subTotal.toInt(),
|
|
|
|
|
normalPrice: price,
|
|
|
|
|
totalService: 0,
|
|
|
|
|
draftName:
|
|
|
|
|
customerController.text,
|
|
|
|
|
),
|
|
|
|
|
);
|
2025-08-03 00:35:00 +07:00
|
|
|
|
2025-07-30 22:38:44 +07:00
|
|
|
// Handle post-payment cleanup
|
|
|
|
|
await _handlePostPaymentCleanup();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
label: 'Bayar',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|