split bill

This commit is contained in:
efrilm
2025-10-31 20:43:37 +07:00
parent 6b42cd86ff
commit 35f02e6b76
18 changed files with 1763 additions and 27 deletions
@@ -3,6 +3,7 @@ import 'package:dartz/dartz.dart' hide Order;
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart' hide Order;
import '../../../common/types/split_type.dart';
import '../../../domain/order/order.dart';
import '../../../domain/payment_method/payment_method.dart';
@@ -64,6 +65,39 @@ class PaymentFormBloc extends Bloc<PaymentFormEvent, PaymentFormState> {
),
);
},
submittedSplitBill: (e) async {
Either<OrderFailure, Payment> failureOrPayment;
emit(
state.copyWith(isSubmitting: true, failureOrPaymentSplitBill: none()),
);
final request = PaymentSplitBillRequest(
orderId: state.order.id,
paymentMethodId: state.paymentMethod?.id ?? '',
amount: state.order.totalAmount,
customerId: e.customerId ?? '',
type: e.splitType ?? SplitType.unknown,
customerName: e.customerName ?? '',
items: state.pendingItems
.map(
(item) => PaymentItemSplitBillRequest(
orderItemId: item.id,
quantity: item.quantity,
),
)
.toList(),
);
failureOrPayment = await _repository.createSplitBill(request);
emit(
state.copyWith(
isSubmitting: false,
failureOrPaymentSplitBill: optionOf(failureOrPayment),
),
);
},
);
}
}
@@ -22,18 +22,36 @@ mixin _$PaymentFormEvent {
required TResult Function(Order order) setOrder,
required TResult Function(PaymentMethod paymentMethod) setPaymentMethod,
required TResult Function() submitted,
required TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)
submittedSplitBill,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(Order order)? setOrder,
TResult? Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult? Function()? submitted,
TResult? Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function(Order order)? setOrder,
TResult Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult Function()? submitted,
TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
required TResult orElse(),
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
@@ -41,18 +59,21 @@ mixin _$PaymentFormEvent {
required TResult Function(_SetOrder value) setOrder,
required TResult Function(_SetPayment value) setPaymentMethod,
required TResult Function(_Submitted value) submitted,
required TResult Function(_SubmittedSplitBill value) submittedSplitBill,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
TResult? Function(_SetOrder value)? setOrder,
TResult? Function(_SetPayment value)? setPaymentMethod,
TResult? Function(_Submitted value)? submitted,
TResult? Function(_SubmittedSplitBill value)? submittedSplitBill,
}) => throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>({
TResult Function(_SetOrder value)? setOrder,
TResult Function(_SetPayment value)? setPaymentMethod,
TResult Function(_Submitted value)? submitted,
TResult Function(_SubmittedSplitBill value)? submittedSplitBill,
required TResult orElse(),
}) => throw _privateConstructorUsedError;
}
@@ -164,6 +185,12 @@ class _$SetOrderImpl implements _SetOrder {
required TResult Function(Order order) setOrder,
required TResult Function(PaymentMethod paymentMethod) setPaymentMethod,
required TResult Function() submitted,
required TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)
submittedSplitBill,
}) {
return setOrder(order);
}
@@ -174,6 +201,12 @@ class _$SetOrderImpl implements _SetOrder {
TResult? Function(Order order)? setOrder,
TResult? Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult? Function()? submitted,
TResult? Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
}) {
return setOrder?.call(order);
}
@@ -184,6 +217,12 @@ class _$SetOrderImpl implements _SetOrder {
TResult Function(Order order)? setOrder,
TResult Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult Function()? submitted,
TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
required TResult orElse(),
}) {
if (setOrder != null) {
@@ -198,6 +237,7 @@ class _$SetOrderImpl implements _SetOrder {
required TResult Function(_SetOrder value) setOrder,
required TResult Function(_SetPayment value) setPaymentMethod,
required TResult Function(_Submitted value) submitted,
required TResult Function(_SubmittedSplitBill value) submittedSplitBill,
}) {
return setOrder(this);
}
@@ -208,6 +248,7 @@ class _$SetOrderImpl implements _SetOrder {
TResult? Function(_SetOrder value)? setOrder,
TResult? Function(_SetPayment value)? setPaymentMethod,
TResult? Function(_Submitted value)? submitted,
TResult? Function(_SubmittedSplitBill value)? submittedSplitBill,
}) {
return setOrder?.call(this);
}
@@ -218,6 +259,7 @@ class _$SetOrderImpl implements _SetOrder {
TResult Function(_SetOrder value)? setOrder,
TResult Function(_SetPayment value)? setPaymentMethod,
TResult Function(_Submitted value)? submitted,
TResult Function(_SubmittedSplitBill value)? submittedSplitBill,
required TResult orElse(),
}) {
if (setOrder != null) {
@@ -325,6 +367,12 @@ class _$SetPaymentImpl implements _SetPayment {
required TResult Function(Order order) setOrder,
required TResult Function(PaymentMethod paymentMethod) setPaymentMethod,
required TResult Function() submitted,
required TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)
submittedSplitBill,
}) {
return setPaymentMethod(paymentMethod);
}
@@ -335,6 +383,12 @@ class _$SetPaymentImpl implements _SetPayment {
TResult? Function(Order order)? setOrder,
TResult? Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult? Function()? submitted,
TResult? Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
}) {
return setPaymentMethod?.call(paymentMethod);
}
@@ -345,6 +399,12 @@ class _$SetPaymentImpl implements _SetPayment {
TResult Function(Order order)? setOrder,
TResult Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult Function()? submitted,
TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
required TResult orElse(),
}) {
if (setPaymentMethod != null) {
@@ -359,6 +419,7 @@ class _$SetPaymentImpl implements _SetPayment {
required TResult Function(_SetOrder value) setOrder,
required TResult Function(_SetPayment value) setPaymentMethod,
required TResult Function(_Submitted value) submitted,
required TResult Function(_SubmittedSplitBill value) submittedSplitBill,
}) {
return setPaymentMethod(this);
}
@@ -369,6 +430,7 @@ class _$SetPaymentImpl implements _SetPayment {
TResult? Function(_SetOrder value)? setOrder,
TResult? Function(_SetPayment value)? setPaymentMethod,
TResult? Function(_Submitted value)? submitted,
TResult? Function(_SubmittedSplitBill value)? submittedSplitBill,
}) {
return setPaymentMethod?.call(this);
}
@@ -379,6 +441,7 @@ class _$SetPaymentImpl implements _SetPayment {
TResult Function(_SetOrder value)? setOrder,
TResult Function(_SetPayment value)? setPaymentMethod,
TResult Function(_Submitted value)? submitted,
TResult Function(_SubmittedSplitBill value)? submittedSplitBill,
required TResult orElse(),
}) {
if (setPaymentMethod != null) {
@@ -447,6 +510,12 @@ class _$SubmittedImpl implements _Submitted {
required TResult Function(Order order) setOrder,
required TResult Function(PaymentMethod paymentMethod) setPaymentMethod,
required TResult Function() submitted,
required TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)
submittedSplitBill,
}) {
return submitted();
}
@@ -457,6 +526,12 @@ class _$SubmittedImpl implements _Submitted {
TResult? Function(Order order)? setOrder,
TResult? Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult? Function()? submitted,
TResult? Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
}) {
return submitted?.call();
}
@@ -467,6 +542,12 @@ class _$SubmittedImpl implements _Submitted {
TResult Function(Order order)? setOrder,
TResult Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult Function()? submitted,
TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
required TResult orElse(),
}) {
if (submitted != null) {
@@ -481,6 +562,7 @@ class _$SubmittedImpl implements _Submitted {
required TResult Function(_SetOrder value) setOrder,
required TResult Function(_SetPayment value) setPaymentMethod,
required TResult Function(_Submitted value) submitted,
required TResult Function(_SubmittedSplitBill value) submittedSplitBill,
}) {
return submitted(this);
}
@@ -491,6 +573,7 @@ class _$SubmittedImpl implements _Submitted {
TResult? Function(_SetOrder value)? setOrder,
TResult? Function(_SetPayment value)? setPaymentMethod,
TResult? Function(_Submitted value)? submitted,
TResult? Function(_SubmittedSplitBill value)? submittedSplitBill,
}) {
return submitted?.call(this);
}
@@ -501,6 +584,7 @@ class _$SubmittedImpl implements _Submitted {
TResult Function(_SetOrder value)? setOrder,
TResult Function(_SetPayment value)? setPaymentMethod,
TResult Function(_Submitted value)? submitted,
TResult Function(_SubmittedSplitBill value)? submittedSplitBill,
required TResult orElse(),
}) {
if (submitted != null) {
@@ -514,12 +598,218 @@ abstract class _Submitted implements PaymentFormEvent {
const factory _Submitted() = _$SubmittedImpl;
}
/// @nodoc
abstract class _$$SubmittedSplitBillImplCopyWith<$Res> {
factory _$$SubmittedSplitBillImplCopyWith(
_$SubmittedSplitBillImpl value,
$Res Function(_$SubmittedSplitBillImpl) then,
) = __$$SubmittedSplitBillImplCopyWithImpl<$Res>;
@useResult
$Res call({SplitType? splitType, String? customerId, String? customerName});
}
/// @nodoc
class __$$SubmittedSplitBillImplCopyWithImpl<$Res>
extends _$PaymentFormEventCopyWithImpl<$Res, _$SubmittedSplitBillImpl>
implements _$$SubmittedSplitBillImplCopyWith<$Res> {
__$$SubmittedSplitBillImplCopyWithImpl(
_$SubmittedSplitBillImpl _value,
$Res Function(_$SubmittedSplitBillImpl) _then,
) : super(_value, _then);
/// Create a copy of PaymentFormEvent
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? splitType = freezed,
Object? customerId = freezed,
Object? customerName = freezed,
}) {
return _then(
_$SubmittedSplitBillImpl(
splitType: freezed == splitType
? _value.splitType
: splitType // ignore: cast_nullable_to_non_nullable
as SplitType?,
customerId: freezed == customerId
? _value.customerId
: customerId // ignore: cast_nullable_to_non_nullable
as String?,
customerName: freezed == customerName
? _value.customerName
: customerName // ignore: cast_nullable_to_non_nullable
as String?,
),
);
}
}
/// @nodoc
class _$SubmittedSplitBillImpl implements _SubmittedSplitBill {
const _$SubmittedSplitBillImpl({
this.splitType,
this.customerId,
this.customerName,
});
@override
final SplitType? splitType;
@override
final String? customerId;
@override
final String? customerName;
@override
String toString() {
return 'PaymentFormEvent.submittedSplitBill(splitType: $splitType, customerId: $customerId, customerName: $customerName)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$SubmittedSplitBillImpl &&
(identical(other.splitType, splitType) ||
other.splitType == splitType) &&
(identical(other.customerId, customerId) ||
other.customerId == customerId) &&
(identical(other.customerName, customerName) ||
other.customerName == customerName));
}
@override
int get hashCode =>
Object.hash(runtimeType, splitType, customerId, customerName);
/// Create a copy of PaymentFormEvent
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$SubmittedSplitBillImplCopyWith<_$SubmittedSplitBillImpl> get copyWith =>
__$$SubmittedSplitBillImplCopyWithImpl<_$SubmittedSplitBillImpl>(
this,
_$identity,
);
@override
@optionalTypeArgs
TResult when<TResult extends Object?>({
required TResult Function(Order order) setOrder,
required TResult Function(PaymentMethod paymentMethod) setPaymentMethod,
required TResult Function() submitted,
required TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)
submittedSplitBill,
}) {
return submittedSplitBill(splitType, customerId, customerName);
}
@override
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>({
TResult? Function(Order order)? setOrder,
TResult? Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult? Function()? submitted,
TResult? Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
}) {
return submittedSplitBill?.call(splitType, customerId, customerName);
}
@override
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>({
TResult Function(Order order)? setOrder,
TResult Function(PaymentMethod paymentMethod)? setPaymentMethod,
TResult Function()? submitted,
TResult Function(
SplitType? splitType,
String? customerId,
String? customerName,
)?
submittedSplitBill,
required TResult orElse(),
}) {
if (submittedSplitBill != null) {
return submittedSplitBill(splitType, customerId, customerName);
}
return orElse();
}
@override
@optionalTypeArgs
TResult map<TResult extends Object?>({
required TResult Function(_SetOrder value) setOrder,
required TResult Function(_SetPayment value) setPaymentMethod,
required TResult Function(_Submitted value) submitted,
required TResult Function(_SubmittedSplitBill value) submittedSplitBill,
}) {
return submittedSplitBill(this);
}
@override
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>({
TResult? Function(_SetOrder value)? setOrder,
TResult? Function(_SetPayment value)? setPaymentMethod,
TResult? Function(_Submitted value)? submitted,
TResult? Function(_SubmittedSplitBill value)? submittedSplitBill,
}) {
return submittedSplitBill?.call(this);
}
@override
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>({
TResult Function(_SetOrder value)? setOrder,
TResult Function(_SetPayment value)? setPaymentMethod,
TResult Function(_Submitted value)? submitted,
TResult Function(_SubmittedSplitBill value)? submittedSplitBill,
required TResult orElse(),
}) {
if (submittedSplitBill != null) {
return submittedSplitBill(this);
}
return orElse();
}
}
abstract class _SubmittedSplitBill implements PaymentFormEvent {
const factory _SubmittedSplitBill({
final SplitType? splitType,
final String? customerId,
final String? customerName,
}) = _$SubmittedSplitBillImpl;
SplitType? get splitType;
String? get customerId;
String? get customerName;
/// Create a copy of PaymentFormEvent
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
_$$SubmittedSplitBillImplCopyWith<_$SubmittedSplitBillImpl> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
mixin _$PaymentFormState {
Order get order => throw _privateConstructorUsedError;
List<OrderItem> get pendingItems => throw _privateConstructorUsedError;
Option<Either<OrderFailure, Payment>> get failureOrPayment =>
throw _privateConstructorUsedError;
Option<Either<OrderFailure, Payment>> get failureOrPaymentSplitBill =>
throw _privateConstructorUsedError;
PaymentMethod? get paymentMethod => throw _privateConstructorUsedError;
bool get isSubmitting => throw _privateConstructorUsedError;
@@ -541,6 +831,7 @@ abstract class $PaymentFormStateCopyWith<$Res> {
Order order,
List<OrderItem> pendingItems,
Option<Either<OrderFailure, Payment>> failureOrPayment,
Option<Either<OrderFailure, Payment>> failureOrPaymentSplitBill,
PaymentMethod? paymentMethod,
bool isSubmitting,
});
@@ -567,6 +858,7 @@ class _$PaymentFormStateCopyWithImpl<$Res, $Val extends PaymentFormState>
Object? order = null,
Object? pendingItems = null,
Object? failureOrPayment = null,
Object? failureOrPaymentSplitBill = null,
Object? paymentMethod = freezed,
Object? isSubmitting = null,
}) {
@@ -584,6 +876,10 @@ class _$PaymentFormStateCopyWithImpl<$Res, $Val extends PaymentFormState>
? _value.failureOrPayment
: failureOrPayment // ignore: cast_nullable_to_non_nullable
as Option<Either<OrderFailure, Payment>>,
failureOrPaymentSplitBill: null == failureOrPaymentSplitBill
? _value.failureOrPaymentSplitBill
: failureOrPaymentSplitBill // ignore: cast_nullable_to_non_nullable
as Option<Either<OrderFailure, Payment>>,
paymentMethod: freezed == paymentMethod
? _value.paymentMethod
: paymentMethod // ignore: cast_nullable_to_non_nullable
@@ -635,6 +931,7 @@ abstract class _$$PaymentFormStateImplCopyWith<$Res>
Order order,
List<OrderItem> pendingItems,
Option<Either<OrderFailure, Payment>> failureOrPayment,
Option<Either<OrderFailure, Payment>> failureOrPaymentSplitBill,
PaymentMethod? paymentMethod,
bool isSubmitting,
});
@@ -662,6 +959,7 @@ class __$$PaymentFormStateImplCopyWithImpl<$Res>
Object? order = null,
Object? pendingItems = null,
Object? failureOrPayment = null,
Object? failureOrPaymentSplitBill = null,
Object? paymentMethod = freezed,
Object? isSubmitting = null,
}) {
@@ -679,6 +977,10 @@ class __$$PaymentFormStateImplCopyWithImpl<$Res>
? _value.failureOrPayment
: failureOrPayment // ignore: cast_nullable_to_non_nullable
as Option<Either<OrderFailure, Payment>>,
failureOrPaymentSplitBill: null == failureOrPaymentSplitBill
? _value.failureOrPaymentSplitBill
: failureOrPaymentSplitBill // ignore: cast_nullable_to_non_nullable
as Option<Either<OrderFailure, Payment>>,
paymentMethod: freezed == paymentMethod
? _value.paymentMethod
: paymentMethod // ignore: cast_nullable_to_non_nullable
@@ -699,6 +1001,7 @@ class _$PaymentFormStateImpl implements _PaymentFormState {
required this.order,
required final List<OrderItem> pendingItems,
required this.failureOrPayment,
required this.failureOrPaymentSplitBill,
this.paymentMethod,
this.isSubmitting = false,
}) : _pendingItems = pendingItems;
@@ -716,6 +1019,8 @@ class _$PaymentFormStateImpl implements _PaymentFormState {
@override
final Option<Either<OrderFailure, Payment>> failureOrPayment;
@override
final Option<Either<OrderFailure, Payment>> failureOrPaymentSplitBill;
@override
final PaymentMethod? paymentMethod;
@override
@JsonKey()
@@ -723,7 +1028,7 @@ class _$PaymentFormStateImpl implements _PaymentFormState {
@override
String toString() {
return 'PaymentFormState(order: $order, pendingItems: $pendingItems, failureOrPayment: $failureOrPayment, paymentMethod: $paymentMethod, isSubmitting: $isSubmitting)';
return 'PaymentFormState(order: $order, pendingItems: $pendingItems, failureOrPayment: $failureOrPayment, failureOrPaymentSplitBill: $failureOrPaymentSplitBill, paymentMethod: $paymentMethod, isSubmitting: $isSubmitting)';
}
@override
@@ -738,6 +1043,11 @@ class _$PaymentFormStateImpl implements _PaymentFormState {
) &&
(identical(other.failureOrPayment, failureOrPayment) ||
other.failureOrPayment == failureOrPayment) &&
(identical(
other.failureOrPaymentSplitBill,
failureOrPaymentSplitBill,
) ||
other.failureOrPaymentSplitBill == failureOrPaymentSplitBill) &&
(identical(other.paymentMethod, paymentMethod) ||
other.paymentMethod == paymentMethod) &&
(identical(other.isSubmitting, isSubmitting) ||
@@ -750,6 +1060,7 @@ class _$PaymentFormStateImpl implements _PaymentFormState {
order,
const DeepCollectionEquality().hash(_pendingItems),
failureOrPayment,
failureOrPaymentSplitBill,
paymentMethod,
isSubmitting,
);
@@ -771,6 +1082,8 @@ abstract class _PaymentFormState implements PaymentFormState {
required final Order order,
required final List<OrderItem> pendingItems,
required final Option<Either<OrderFailure, Payment>> failureOrPayment,
required final Option<Either<OrderFailure, Payment>>
failureOrPaymentSplitBill,
final PaymentMethod? paymentMethod,
final bool isSubmitting,
}) = _$PaymentFormStateImpl;
@@ -782,6 +1095,8 @@ abstract class _PaymentFormState implements PaymentFormState {
@override
Option<Either<OrderFailure, Payment>> get failureOrPayment;
@override
Option<Either<OrderFailure, Payment>> get failureOrPaymentSplitBill;
@override
PaymentMethod? get paymentMethod;
@override
bool get isSubmitting;
@@ -6,4 +6,9 @@ class PaymentFormEvent with _$PaymentFormEvent {
const factory PaymentFormEvent.setPaymentMethod(PaymentMethod paymentMethod) =
_SetPayment;
const factory PaymentFormEvent.submitted() = _Submitted;
const factory PaymentFormEvent.submittedSplitBill({
SplitType? splitType,
String? customerId,
String? customerName,
}) = _SubmittedSplitBill;
}
@@ -6,6 +6,7 @@ class PaymentFormState with _$PaymentFormState {
required Order order,
required List<OrderItem> pendingItems,
required Option<Either<OrderFailure, Payment>> failureOrPayment,
required Option<Either<OrderFailure, Payment>> failureOrPaymentSplitBill,
PaymentMethod? paymentMethod,
@Default(false) bool isSubmitting,
}) = _PaymentFormState;
@@ -14,5 +15,6 @@ class PaymentFormState with _$PaymentFormState {
order: Order.empty(),
pendingItems: [],
failureOrPayment: none(),
failureOrPaymentSplitBill: none(),
);
}