Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] ASN from QR code is not added to document metadata although barcode is read successfully
### Description

When I scan a document with an ASN barcode/QR code, the ASN is not added to the document metadata, even though the logs show that the code has been read. This happens on single documents as well as multiple documents split via PATCHT pages. It does not matter whether the ASN is already in the database or not. Detection of the QR codes and recognition as ASN and insertion of the ASN into the document metadata worked fine before the update from 2.5.1 to 2.5.2 (with the exception of the error fixed in [5745](https://github.com/paperless-ngx/paperless-ngx/pull/5745).

I would be expecting some additional lines in the log (from 2.5.1 last night) that show that the barcode was not only read, but recognized as an ASN:

[2024-02-14 20:56:23,657] [DEBUG] [paperless.barcodes] Found ASN Barcode: ASN0000015

[2024-02-14 20:56:23,658] [INFO] [paperless.barcodes] Found ASN in barcode: 15

But these do not show up.

### Steps to reproduce

1. Add a scanned pdf with a ASN QR code to the consume folder.
2. Document is consumed
3. Document in inbox does not have its ASN set in metadata.
4. Logs show the barcode was read, but does not show recognition as an ASN. Duplicate ASNs are not detected, either.

### Webserver logs

```bash
[2024-02-15 10:57:25,933] [DEBUG] [paperless.tasks] Skipping plugin CollatePlugin

[2024-02-15 10:57:25,934] [DEBUG] [paperless.tasks] Executing plugin BarcodePlugin

[2024-02-15 10:57:25,934] [DEBUG] [paperless.barcodes] Scanning for barcodes using ZXING

[2024-02-15 10:57:26,841] [DEBUG] [paperless.barcodes] Barcode of type BarcodeFormat.QRCode found: ASN0000022

[2024-02-15 10:57:26,844] [INFO] [paperless.tasks] BarcodePlugin completed with: No pages to split on!

[2024-02-15 10:57:26,850] [DEBUG] [paperless.tasks] Executing plugin WorkflowTriggerPlugin

[2024-02-15 10:57:26,853] [INFO] [paperless.tasks] WorkflowTriggerPlugin completed with no message

[2024-02-15 10:57:26,876] [INFO] [paperless.consumer] Consuming 2024-02-15.105714 scan_ocr.pdf

[2024-02-15 10:57:26,880] [DEBUG] [paperless.consumer] Detected mime type: application/pdf

[2024-02-15 10:57:26,891] [DEBUG] [paperless.consumer] Parser: RasterisedDocumentParser

[2024-02-15 10:57:26,896] [DEBUG] [paperless.consumer] Parsing 2024-02-15.105714 scan_ocr.pdf...

[2024-02-15 10:57:27,243] [DEBUG] [paperless.parsing.tesseract] Calling OCRmyPDF with args: {'input_file': PosixPath('/tmp/paperless/paperless-ngxafs4pekd/2024-02-15.105714 scan_ocr.pdf'), 'output_file': PosixPath('/tmp/paperless/paperless-nv1oe3ig/archive.pdf'), 'use_threads': True, 'jobs': 2, 'language': 'deu+eng', 'output_type': 'pdfa', 'progress_bar': False, 'color_conversion_strategy': 'RGB', 'skip_text': True, 'clean': True, 'deskew': True, 'rotate_pages': True, 'rotate_pages_threshold': 12.0, 'sidecar': PosixPath('/tmp/paperless/paperless-nv1oe3ig/sidecar.txt'), 'continue_on_soft_render_error': True, 'invalidate_digital_signatures': True}

[2024-02-15 10:57:48,347] [DEBUG] [paperless.parsing.tesseract] Using text from sidecar file

[2024-02-15 10:57:48,347] [DEBUG] [paperless.consumer] Generating thumbnail for 2024-02-15.105714 scan_ocr.pdf...

[2024-02-15 10:57:48,352] [DEBUG] [paperless.parsing] Execute: convert -density 300 -scale 500x5000> -alpha remove -strip -auto-orient -define pdf:use-cropbox=true /tmp/paperless/paperless-nv1oe3ig/archive.pdf[0] /tmp/paperless/paperless-nv1oe3ig/convert.webp

[2024-02-15 10:57:51,260] [DEBUG] [paperless.consumer] Saving record to database

[2024-02-15 10:57:51,261] [DEBUG] [paperless.consumer] Creation date from parse_date: 2024-02-15 00:00:00+01:00

[2024-02-15 10:57:52,229] [INFO] [paperless.handlers] Assigning correspondent XXXXX to 2024-02-15 2024-02-15.105714 scan_ocr

[2024-02-15 10:57:52,243] [INFO] [paperless.handlers] Assigning document type XXXXX to 2024-02-15 XXXXX 2024-02-15.105714 scan_ocr

[2024-02-15 10:57:52,268] [INFO] [paperless.handlers] Tagging "2024-02-15 XXXXX 2024-02-15.105714 scan_ocr" with "XXXXX, XXXXX, 2022, XXXXX"

[2024-02-15 10:57:52,585] [DEBUG] [paperless.consumer] Deleting file /tmp/paperless/paperless-ngxafs4pekd/2024-02-15.105714 scan_ocr.pdf

[2024-02-15 10:57:52,620] [DEBUG] [paperless.parsing.tesseract] Deleting directory /tmp/paperless/paperless-nv1oe3ig

[2024-02-15 10:57:52,621] [INFO] [paperless.consumer] Document 2024-02-15 XXXXX 2024-02-15.105714 scan_ocr consumption finished
```


### Browser logs

_No response_

### Paperless-ngx version

2.5.2

### Host OS

Ubuntu 22.04, x86_64

### Installation method

Docker - official image

### Browser

Firefox

### Configuration changes

docker-compose.env: PAPERLESS_URL=http://xxxx USERMAP_UID=xxxx USERMAP_GID=xxxx PAPERLESS_TIME_ZONE=Europe/Berlin PAPERLESS_OCR_LANGUAGE=deu+eng PAPERLESS_OCR_USER_ARGS='{"continue_on_soft_render_error": true, "invalidate_digital_signatures": true}' PAPERLESS_SECRET_KEY=xxxxxxx PAPERLESS_FILENAME_FORMAT={correspondent}/{created} {title} PAPERLESS_FILENAME_FORMAT_REMOVE_NONE=True PAPERLESS_FILENAME_DATE_ORDER=YMD PAPERLESS_NUMBER_OF_SUGGESTED_DATES=5 PAPERLESS_IGNORE_DATES=xxxxx PAPERLESS_CONSUMER_RECURSIVE=true PAPERLESS_CONSUMER_SUBDIRS_AS_TAGS=True PAPERLESS_CONSUMER_ENABLE_COLLATE_DOUBLE_SIDED=True PAPERLESS_CONSUMER_COLLATE_DOUBLE_SIDED_SUBDIR_NAME=double-sided PAPERLESS_CONSUMER_COLLATE_DOUBLE_SIDED_TIFF_SUPPORT=True PAPERLESS_CONSUMER_ENABLE_BARCODES=True PAPERLESS_CONSUMER_BARCODE_TIFF_SUPPORT=True PAPERLESS_CONSUMER_ENABLE_ASN_BARCODE=True PAPERLESS_CONSUMER_BARCODE_SCANNER=ZXING 

docker-compose.yml: gotenberg: # image: docker.io/gotenberg/gotenberg:7.8 image: docker.io/gotenberg/gotenberg:latest restart: unless-stopped # The gotenberg chromium route is used to convert .eml files. We do not # want to allow external content like tracking pixels or even javascript. command: - "gotenberg" - "--chromium-disable-javascript=true" - "--chromium-allow-list=file:///tmp/.*" - "--api-timeout=120s"

### Other

_No response_

### Please confirm the following

- [X] I believe this issue is a bug that affects all users of Paperless-ngx, not something specific to my installation.
- [X] I have already searched for relevant existing issues and discussions before opening this report.
- [X] I have updated the title field above with a concise description.


Discussion:
Sample file?
With the sample file https://github.com/paperless-ngx/paperless-ngx/blob/dev/src/documents/tests/samples/barcodes/barcode-39-asn-123.pdf I cannot reproduce this.  The ASN is read an assigned as expected.

Resolving patch:
diff --git a/src/documents/barcodes.py b/src/documents/barcodes.py
--- a/src/documents/barcodes.py
+++ b/src/documents/barcodes.py
@@ -100,11 +100,9 @@ def run(self) -> Optional[str]:
             logger.info(f"Found tags in barcode: {tags}")
 
         # Lastly attempt to split documents
-        if settings.CONSUMER_ENABLE_BARCODES:
-
-            separator_pages = self.get_separation_pages()
-            if not separator_pages:
-                return "No pages to split on!"
+        if settings.CONSUMER_ENABLE_BARCODES and (
+            separator_pages := self.get_separation_pages()
+        ):
 
             # We have pages to split against
 


Test patch:
diff --git a/src/documents/tests/test_barcodes.py b/src/documents/tests/test_barcodes.py
--- a/src/documents/tests/test_barcodes.py
+++ b/src/documents/tests/test_barcodes.py
@@ -15,6 +15,7 @@
 from documents.data_models import DocumentMetadataOverrides
 from documents.data_models import DocumentSource
 from documents.models import Tag
+from documents.plugins.base import StopConsumeTaskError
 from documents.tests.utils import DirectoriesMixin
 from documents.tests.utils import DocumentConsumeDelayMixin
 from documents.tests.utils import DummyProgressManager
@@ -415,7 +416,10 @@ def test_separate_pages_no_list(self):
         test_file = self.SAMPLE_DIR / "simple.pdf"
 
         with self.get_reader(test_file) as reader:
-            self.assertEqual("No pages to split on!", reader.run())
+            try:
+                reader.run()
+            except StopConsumeTaskError:
+                self.fail("Barcode reader split pages unexpectedly")
 
     @override_settings(
         CONSUMER_ENABLE_BARCODES=True,