
Refactors the reception process to provide clearer control over data synchronization and validation. - Replaces the `isDraft` property with `synchronized` in `StockPickingRecordEntity` to accurately represent the local data's sync status with the backend. - Introduces a new API endpoint and corresponding logic for `validateStockPicking`, allowing finalization of receptions. - Splits the "Save" action into "Synchronize data" and "Validate reception" on the details page. "Validate" now implicitly performs a synchronization first. - Updates UI elements (cards, quick actions) to reflect the new synchronization state and expose the new actions. - Corrects a typo in the `updateAllMoveLineOnStockPicking` API method name. - Improves local data update logic for scanned items, marking them as unsynchronized.
115 lines
3.7 KiB
Dart
115 lines
3.7 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(saveLoading: 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.updateAllMoveLineOnStockPicking(
|
|
stockPickingId: receptionId,
|
|
moveLineDto: moveLinesDto,
|
|
);
|
|
if (res) {
|
|
stockPikingEntity?.synchronized = true;
|
|
stockPickingRecords.put(stockPikingEntity!);
|
|
getReceptionById(id: receptionId);
|
|
onSuccess?.call();
|
|
} else {
|
|
onError?.call();
|
|
}
|
|
state = state.copyWith(saveLoading: false);
|
|
} catch (e) {
|
|
onError?.call();
|
|
state = state.copyWith(saveLoading: false);
|
|
}
|
|
}
|
|
|
|
Future validate({
|
|
required int receptionId,
|
|
VoidCallback? onSuccess,
|
|
VoidCallback? onError,
|
|
}) async {
|
|
await save(receptionId: receptionId);
|
|
try {
|
|
final stockPickingRecords = objectboxManager.store
|
|
.box<StockPickingRecordEntity>();
|
|
final stockPikingEntity = stockPickingRecords.get(receptionId);
|
|
state = state.copyWith(validateLoading: true);
|
|
final res = await ApiCalls.validateStockPicking(
|
|
stockPickingId: receptionId,
|
|
);
|
|
if (res) {
|
|
onSuccess?.call();
|
|
stockPikingEntity?.synchronized = true;
|
|
stockPikingEntity?.state = 'done';
|
|
stockPickingRecords.put(stockPikingEntity!);
|
|
getReceptionById(id: receptionId);
|
|
} 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,
|
|
@Default(false) bool saveLoading,
|
|
}) = _ReceptionDetailsPageState;
|
|
}
|