barcode_scanner/lib/pages/operation/reception/reception_details_page_model.dart
your-name 61252a3aa9 feat: Implements reception validation process
Introduces the ability to validate a stock reception by sending updated move line quantities to the backend.

This includes:
- Adding a new API call to update stock picking move lines.
- Integrating a "Validate Reception" button within the quick actions component on the reception details page.
- Implementing the logic to gather move line data and call the new API endpoint.
- Enhancing error messages on the scan page for products not expected in the current reception.
- Improving type safety for API response data.
2025-07-31 01:07:48 +03:00

81 lines
2.6 KiB
Dart

import 'package:e_scan/backend/api/api_calls.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:e_scan/backend/schema/stock_picking/update_move_line_dto.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'reception_details_page_model.freezed.dart';
final receptionDetailsPageModelProvider =
StateNotifierProvider.autoDispose<
ReceptionDetailsPageModel,
ReceptionDetailsPageState
>((ref) {
return ReceptionDetailsPageModel();
});
class ReceptionDetailsPageModel
extends StateNotifier<ReceptionDetailsPageState> {
ReceptionDetailsPageModel() : super(const ReceptionDetailsPageState());
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);
}
}
Future save({
required int receptionId,
VoidCallback? onSuccess,
VoidCallback? onError,
}) async {
try {
state = state.copyWith(validateLoading: true);
final stockPickingRecords = objectboxManager.store
.box<StockPickingRecordEntity>();
final stockPikingEntity = stockPickingRecords.get(receptionId);
final moveLinesDto =
stockPikingEntity?.moveLineIdsWithoutPackage
.map(
(m) => UpdateMoveLineDto(
operation: 1,
moveLineId: m.id,
values: {"quantity": m.quantity},
),
)
.toList() ??
[];
final res = await ApiCalls.updateAllMoveLineOnStockPiking(
stockPickingId: receptionId,
moveLineDto: moveLinesDto,
);
if (res) {
onSuccess?.call();
} else {
onError?.call();
}
state = state.copyWith(validateLoading: false);
} catch (e) {
onError?.call();
state = state.copyWith(validateLoading: false);
}
}
}
@freezed
abstract class ReceptionDetailsPageState with _$ReceptionDetailsPageState {
const factory ReceptionDetailsPageState({
StockPickingRecordEntity? reception,
@Default(false) bool loading,
@Default(false) bool validateLoading,
}) = _ReceptionDetailsPageState;
}