From 4a89ec1e8d21c618134ff67112f59b7f9165fe62 Mon Sep 17 00:00:00 2001 From: Steven Tang Date: Fri, 3 Jul 2026 21:36:37 -0400 Subject: [PATCH] Improve scan stock item in Issue 617 (#841) * Support transfer location into sublocation. Fix notification override. Support continous scanning. * Perform linting as suggested * Reuse http client and cache the https setting (#845) This makes the app way faster by not having to redo the TCP and TLS handshake all the time and removing a disk read on each http request. * Bump version and release notes (#846) --------- Co-authored-by: Jorropo Co-authored-by: Oliver --- lib/barcode/stock.dart | 77 +++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/lib/barcode/stock.dart b/lib/barcode/stock.dart index 09826f7d..4e3368c9 100644 --- a/lib/barcode/stock.dart +++ b/lib/barcode/stock.dart @@ -75,23 +75,20 @@ class BarcodeScanStockItemHandler extends BarcodeHandler { if (data.containsKey("stockitem")) { int _item = (data["stockitem"]?["pk"] ?? -1) as int; - // A valid stock location! if (_item > 0) { barcodeSuccessTone(); - bool result = await onItemScanned(_item); + await onItemScanned(_item); + } + } else if (data.containsKey("stocklocation")) { + int _loc = (data["stocklocation"]?["pk"] ?? -1) as int; - if (result && OneContext.hasContext) { - OneContext().pop(); - return; - } + if (_loc > 0) { + barcodeSuccessTone(); + + await onStockLocationScanned(_loc); } } - - // If we get to this point, something went wrong during the scan process - barcodeFailureTone(); - - showSnackIcon(L10().invalidStockItem, success: false); } // Callback function which runs when a valid StockItem is scanned @@ -99,6 +96,12 @@ class BarcodeScanStockItemHandler extends BarcodeHandler { // Re-implement this for particular subclass return false; } + + // Callback function which runs when a valid StockLocation is scanned + Future onStockLocationScanned(int locationId) async { + // Re-implement this for particular subclass + return false; + } } /* @@ -171,6 +174,56 @@ class StockLocationScanInItemsHandler extends BarcodeScanStockItemHandler { @override String getOverlayText(BuildContext context) => L10().barcodeScanItem; + @override + Future onStockLocationScanned(int locationId) async { + if (locationId == location.pk) { + barcodeFailureTone(); + showSnackIcon(L10().invalidStockLocation, success: false); + return false; + } + + final scannedLocation = + await InvenTreeStockLocation().get(locationId) + as InvenTreeStockLocation?; + + if (scannedLocation != null) { + if (scannedLocation.parentId == location.pk) { + barcodeFailureTone(); + showSnackIcon(L10().itemInLocation, success: false); + return false; + } + + final response = await scannedLocation.update( + values: {"parent": location.pk.toString()}, + expectedStatusCode: null, + ); + + switch (response.statusCode) { + case 200: + case 201: + barcodeSuccess(L10().barcodeScanIntoLocationSuccess); + return true; + case 400: + barcodeFailureTone(); + showSnackIcon(L10().invalidStockLocation, success: false); + return false; + default: + barcodeFailureTone(); + showSnackIcon( + L10().barcodeScanIntoLocationFailure, + success: false, + actionText: L10().details, + onAction: () { + showErrorDialog(L10().barcodeError, response: response); + }, + ); + return false; + } + } + + return false; + } + @override Future onItemScanned(int itemId) async { final InvenTreeStockItem? item = @@ -186,7 +239,7 @@ class StockLocationScanInItemsHandler extends BarcodeScanStockItemHandler { // Item is already *in* the specified location if (item.locationId == location.pk) { barcodeFailureTone(); - showSnackIcon(L10().itemInLocation, success: true); + showSnackIcon(L10().itemInLocation, success: false); return false; } else { if (confirm) {