
Refreshes the list of receptions on the main page after returning from the details view, ensuring the displayed data is up-to-date. Improves the product scanning logic by ensuring product lookups are specific to the current reception. This prevents misidentification of products across different receptions. Adds an error toast message when a scanned product is not found within the active reception's product list, providing immediate user feedback. Refactors product retrieval and existence checks for clarity and robustness.
83 lines
3.1 KiB
Dart
83 lines
3.1 KiB
Dart
import 'package:e_scan/backend/objectbox/entities/product/product_entity.dart';
|
|
import 'package:e_scan/backend/objectbox/entities/stock_picking/stock_picking_record_entity.dart';
|
|
import 'package:e_scan/backend/objectbox/objectbox_manager.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
import 'package:collection/collection.dart';
|
|
part 'reception_scan_page_model.freezed.dart';
|
|
|
|
final receptionScanPageModelProvider =
|
|
StateNotifierProvider.autoDispose<
|
|
ReceptionScanPageModel,
|
|
ReceptionScanPageModelState
|
|
>((ref) {
|
|
return ReceptionScanPageModel();
|
|
});
|
|
|
|
class ReceptionScanPageModel
|
|
extends StateNotifier<ReceptionScanPageModelState> {
|
|
ReceptionScanPageModel() : super(const ReceptionScanPageModelState());
|
|
|
|
Future getReceptionById({required int id}) async {
|
|
try {
|
|
final stockPickingRecords = objectboxManager.store
|
|
.box<StockPickingRecordEntity>();
|
|
state = state.copyWith(loading: true);
|
|
final entity = stockPickingRecords.get(id);
|
|
state = state.copyWith(loading: false, reception: entity);
|
|
} catch (e) {
|
|
state = state.copyWith(loading: false);
|
|
}
|
|
}
|
|
|
|
ProductEntity? getProduct({
|
|
required int receptionId,
|
|
required String barcode,
|
|
}) {
|
|
final stockPickingRecordBox = objectboxManager.store
|
|
.box<StockPickingRecordEntity>();
|
|
final stockPickingRecord = stockPickingRecordBox.get(receptionId);
|
|
final moveEntity = stockPickingRecord?.moveIdsWithoutPackage
|
|
.firstWhereOrNull((e) => e.productId.target?.barcode == barcode);
|
|
return moveEntity?.productId.target;
|
|
}
|
|
|
|
void incrementMoveLineQuantity({
|
|
required String barcode,
|
|
required int receptionId,
|
|
VoidCallback? onError,
|
|
}) {
|
|
final moveLineBox = objectboxManager.store
|
|
.box<MoveLineWithoutPackageEntity>();
|
|
final stockPickingRecordBox = objectboxManager.store
|
|
.box<StockPickingRecordEntity>();
|
|
final moveBox = objectboxManager.store.box<MoveWithoutPackageEntity>();
|
|
final stockPickingRecord = stockPickingRecordBox.get(receptionId);
|
|
final moveLineEntity = stockPickingRecord?.moveLineIdsWithoutPackage
|
|
.firstWhereOrNull((e) => e.productId.target?.barcode == barcode);
|
|
final moveEntity = stockPickingRecord?.moveIdsWithoutPackage
|
|
.firstWhereOrNull((e) => e.productId.target?.barcode == barcode);
|
|
if (moveLineEntity != null &&
|
|
moveEntity != null &&
|
|
stockPickingRecord != null) {
|
|
moveLineEntity.quantity = (moveLineEntity.quantity ?? 0) + 1;
|
|
moveEntity.quantity = (moveEntity.quantity ?? 0) + 1;
|
|
stockPickingRecord.synchronized = false;
|
|
moveLineBox.put(moveLineEntity);
|
|
moveBox.put(moveEntity);
|
|
stockPickingRecordBox.put(stockPickingRecord);
|
|
} else {
|
|
onError?.call();
|
|
}
|
|
}
|
|
}
|
|
|
|
@freezed
|
|
abstract class ReceptionScanPageModelState with _$ReceptionScanPageModelState {
|
|
const factory ReceptionScanPageModelState({
|
|
StockPickingRecordEntity? reception,
|
|
@Default(false) bool loading,
|
|
}) = _ReceptionScanPageModelState;
|
|
}
|