barcode_scanner/lib/pages/operation/reception/reception_scan_page_model.dart
your-name 46b93d7fa4 enhance: Enhances reception details with refresh and syncs scan quantities
Adds pull-to-refresh functionality to the reception details page, allowing users to manually update the displayed information.

Ensures that when a product is scanned, both the move line and the main move entities have their quantities correctly incremented, improving data consistency.
2025-07-30 20:00:03 +03:00

95 lines
3.3 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:mobile_scanner/mobile_scanner.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);
}
}
bool isProductExist({required String barcode}) {
final productBox = objectboxManager.store.box<ProductEntity>();
return productBox
.query(ProductEntity_.barcode.equals(barcode))
.build()
.findFirst() !=
null;
}
ProductEntity? getProduct({required String barcode}) {
final productBox = objectboxManager.store.box<ProductEntity>();
return productBox
.query(ProductEntity_.barcode.equals(barcode))
.build()
.findFirst();
}
void incrementMoveLineQuantity({required String barcode}) {
final moveLineBox = objectboxManager.store
.box<MoveLineWithoutPackageEntity>();
final moveBox = objectboxManager.store.box<MoveWithoutPackageEntity>();
final productBox = objectboxManager.store.box<ProductEntity>();
final productEntity = productBox
.query(ProductEntity_.barcode.equals(barcode))
.build()
.findFirst();
final productId = productEntity?.id;
if (productId != null) {
final moveLineEntity = moveLineBox
.query(MoveLineWithoutPackageEntity_.productId.equals(productId))
.build()
.findFirst();
final moveEntity = moveBox
.query(MoveWithoutPackageEntity_.productId.equals(productId))
.build()
.findFirst();
if (moveLineEntity != null && moveEntity != null) {
moveLineEntity.quantity = (moveLineEntity.quantity ?? 0) + 1;
moveEntity.quantity = (moveEntity.quantity ?? 0) + 1;
moveLineBox.put(moveLineEntity);
moveBox.put(moveEntity);
}
}
}
Future handleBarcode({required BarcodeCapture barcodeCapture}) async {
try {
final stockPickingRecords = objectboxManager.store
.box<StockPickingRecordEntity>();
} catch (e) {}
}
}
@freezed
abstract class ReceptionScanPageModelState with _$ReceptionScanPageModelState {
const factory ReceptionScanPageModelState({
StockPickingRecordEntity? reception,
@Default(false) bool loading,
}) = _ReceptionScanPageModelState;
}