feat: delete item

This commit is contained in:
efrilm
2025-08-02 11:06:41 +07:00
parent 08df1b6384
commit 79d606a08e
4 changed files with 354 additions and 19 deletions
@@ -3,7 +3,6 @@ import 'dart:developer';
import 'package:bloc/bloc.dart';
import 'package:enaklo_pos/data/datasources/product_local_datasource.dart';
import 'package:enaklo_pos/data/models/response/discount_response_model.dart';
import 'package:enaklo_pos/presentation/home/models/order_item.dart';
import 'package:enaklo_pos/presentation/table/models/draft_order_item.dart';
import 'package:enaklo_pos/presentation/table/models/draft_order_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
@@ -20,8 +19,9 @@ part 'checkout_bloc.freezed.dart';
class CheckoutBloc extends Bloc<CheckoutEvent, CheckoutState> {
final SettingsLocalDatasource settingsLocalDatasource;
CheckoutBloc({required this.settingsLocalDatasource}) : super(const CheckoutState.initial()) {
CheckoutBloc({required this.settingsLocalDatasource})
: super(const CheckoutState.initial()) {
on<_AddItem>((event, emit) {
var currentState = state as _Loaded;
List<ProductQuantity> items = [...currentState.items];
@@ -74,14 +74,37 @@ class CheckoutBloc extends Bloc<CheckoutEvent, CheckoutState> {
currentState.orderType));
});
on<_DeleteItem>((event, emit) {
var currentState = state as _Loaded;
List<ProductQuantity> items = [...currentState.items];
var index =
items.indexWhere((element) => element.product.id == event.product.id);
emit(const _Loading());
items.removeAt(index);
emit(_Loaded(
items,
currentState.discountModel,
currentState.discount,
currentState.discountAmount,
currentState.tax,
currentState.serviceCharge,
currentState.totalQuantity,
currentState.totalPrice,
currentState.draftName,
currentState.orderType));
});
on<_Started>((event, emit) async {
emit(const _Loading());
try {
// Load tax and service charge from settings
final tax = await settingsLocalDatasource.getTax();
final serviceCharge = await settingsLocalDatasource.getServiceCharge();
emit(_Loaded([], null, 0, 0, tax.value, serviceCharge, 0, 0, '', OrderType.dineIn));
emit(_Loaded([], null, 0, 0, tax.value, serviceCharge, 0, 0, '',
OrderType.dineIn));
} catch (e) {
// If loading fails, use default values
log('Failed to load settings: $e');
@@ -199,7 +222,8 @@ class CheckoutBloc extends Bloc<CheckoutEvent, CheckoutState> {
on<_UpdateItemNotes>((event, emit) {
var currentState = state as _Loaded;
List<ProductQuantity> items = [...currentState.items];
var index = items.indexWhere((element) => element.product.id == event.product.id);
var index =
items.indexWhere((element) => element.product.id == event.product.id);
if (index != -1) {
items[index] = items[index].copyWith(notes: event.notes);
}