inventree / inventree-app

InvenTree mobile app
https://docs.inventree.org/en/latest/app/app/
MIT License
57 stars 48 forks source link

Scanning a stock item into a location results in both success and failure feedback to the user #520

Open awnz opened 2 months ago

awnz commented 2 months ago

Hi all,

Awesome application! I've just deployed InvenTree to our little family business which was previously losing things regularly.

I'm encountering one issue with the InvenTree mobile app where the following happens when trying to scan a stock item into a location:

  1. Open InvenTree app (Android)
  2. Tap "Scan" icon, then scan a location QR code
  3. Tap "Scan Stock Item" and scan a stockitem
  4. The app reports "Scanned into location" with a barcode success beep
  5. The app then immediately also shows "Invalid Stock Item" with a barcode failure beep, misleading the user into thinking there's a problem

(The item is successfully scanned into the location, verified via the InvenTree web UI, so the error message is false).

This is my first dig into the source which I'm not familiar with, but I think the problem is in lib/barcode/stock.dart line 97, where the return statement shouldn't be inside that if block - I've added comments below:

class BarcodeScanStockItemHandler extends BarcodeHandler {

  @override
  String getOverlayText(BuildContext context) => L10().barcodeScanItem;

  @override
  Future<void> onBarcodeMatched(Map<String, dynamic> data) async {
    // We expect that the barcode points to a 'stockitem'
    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);

        if (result && OneContext.hasContext) {
          OneContext().pop();
          return; // I think this...
        }
        // ...should be here?
      }
    }

    // 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
  Future<bool> onItemScanned(int itemId) async {
    // Re-implement this for particular subclass
    return false;
  }
}

(edit: fixed indentation)

Changing the above so that the return line is moved to where I commented "should be here?" looks to resolve the issue for me. I've tested both expected success (scan a stock item into a location - now doesn't produce the false error) and expected failure (try to scan a random grocery item that InvenTree doesn't know about into a location, correctly returns "No match found for barcode data" as expected).

Am I on the right track? If yes, let me know and I'll submit a PR.

SchrodingersGat commented 2 months ago

@awnz looks like you're on the right track! Please submit a PR and I'll be happy to review :)

awnz commented 2 months ago

Thanks - created PR #521