Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] restAPI ignores archive_serial_number 
### Description

Hi, when I upload a document via RestAPI and use "archive_serial_number" as form-data, the specified number is not entered in the document.


https://docs.paperless-ngx.com/api/

POSTing documents
The API provides a special endpoint for file uploads:

/api/documents/post_document/

POST a multipart form to this endpoint, where the form field document contains the document that you want to upload to paperless. The filename is sanitized and then used to store the document in a temporary directory, and the consumer will be instructed to consume the document from there.

The endpoint supports the following optional form fields:
...
**archive_serial_number: An optional archive serial number to set.**

Thank you for the support!

### Steps to reproduce

Sending RestAPI Post request to http://xxxx:8808/api/documents/post_document/ with "archive_serial_number:12"

### Webserver logs

```bash
Protocoll from paperlessNGX:

[2023-07-08 17:09:54,288] [WARNING] [paperless.barcodes] Unsupported file format for barcode reader: image/jpeg

[2023-07-08 17:09:54,290] [DEBUG] [paperless.barcodes] Scanning for barcodes using PYZBAR

[2023-07-08 17:09:54,291] [WARNING] [paperless.barcodes] Exception during barcode scanning: 'NoneType' object has no attribute 'name'

[2023-07-08 17:09:54,356] [INFO] [paperless.consumer] Consuming test.jpg

[2023-07-08 17:09:54,369] [DEBUG] [paperless.consumer] Detected mime type: image/jpeg

[2023-07-08 17:09:54,373] [DEBUG] [paperless.consumer] Parser: RasterisedDocumentParser

[2023-07-08 17:09:54,382] [DEBUG] [paperless.consumer] Parsing test.jpg...

[2023-07-08 17:09:54,679] [DEBUG] [paperless.parsing.tesseract] Estimated DPI 206 based on image width 1704

[2023-07-08 17:09:54,680] [DEBUG] [paperless.parsing.tesseract] Detected DPI for image /tmp/paperless/paperless-ngxfylpx4xe/test.jpg: 72

[2023-07-08 17:09:54,681] [DEBUG] [paperless.parsing.tesseract] Calling OCRmyPDF with args: {'input_file': PosixPath('/tmp/paperless/paperless-ngxfylpx4xe/test.jpg'), 'output_file': PosixPath('/tmp/paperless/paperless-pz649c1e/archive.pdf'), 'use_threads': True, 'jobs': 4, 'language': 'deu', 'output_type': 'pdfa', 'progress_bar': False, 'skip_text': True, 'clean': True, 'deskew': True, 'rotate_pages': True, 'rotate_pages_threshold': 12.0, 'sidecar': PosixPath('/tmp/paperless/paperless-pz649c1e/sidecar.txt'), 'image_dpi': 72}

[2023-07-08 17:10:42,376] [DEBUG] [paperless.parsing.tesseract] Using text from sidecar file

[2023-07-08 17:10:42,379] [DEBUG] [paperless.consumer] Generating thumbnail for test.jpg...

[2023-07-08 17:10:42,392] [DEBUG] [paperless.parsing] Execute: convert -density 300 -scale 500x5000> -alpha remove -strip -auto-orient /tmp/paperless/paperless-pz649c1e/archive.pdf[0] /tmp/paperless/paperless-pz649c1e/convert.webp

[2023-07-08 17:12:05,958] [DEBUG] [paperless.handlers] Deleted file /usr/src/paperless/media/documents/originals/Rechnungen/Rechnung (RE)/RE-2023-24-0000041_1688665491709.jpg.

[2023-07-08 17:12:05,961] [DEBUG] [paperless.handlers] Deleted file /usr/src/paperless/media/documents/archive/Rechnungen/Rechnung (RE)/RE-2023-24-0000041_1688665491709.pdf.

[2023-07-08 17:12:05,964] [DEBUG] [paperless.handlers] Deleted file /usr/src/paperless/media/documents/thumbnails/0000037.webp.

[2023-07-08 17:12:07,279] [DEBUG] [paperless.consumer] Saving record to database

[2023-07-08 17:12:07,280] [DEBUG] [paperless.consumer] Creation date from post_documents parameter: 2023-01-24 00:00:00+01:00

[2023-07-08 17:12:08,173] [DEBUG] [paperless.consumer] Deleting file /tmp/paperless/paperless-ngxfylpx4xe/test.jpg

[2023-07-08 17:12:08,179] [DEBUG] [paperless.parsing.tesseract] Deleting directory /tmp/paperless/paperless-pz649c1e

[2023-07-08 17:12:08,181] [INFO] [paperless.consumer] Document 2023-01-24 test consumption finished
```


### Browser logs

```bash
POST /api/documents/post_document/ HTTP/1.1
Authorization: Basic xxxx=
User-Agent: PostmanRuntime/7.32.3
Accept: */*
Cache-Control: no-cache
Postman-Token: b02fe41b-b6e4-4784-8117-8452d51069d4
Host: 192.168.2.6:8808
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------271215561418376712309544
Content-Length: 871130
 
----------------------------271215561418376712309544
Content-Disposition: form-data; name="archive_serial_number"
12
----------------------------271215561418376712309544
Content-Disposition: form-data; name="created"
2023-01-24
----------------------------271215561418376712309544
Content-Disposition: form-data; name="title"
test
----------------------------271215561418376712309544
Content-Disposition: form-data; name="document"; filename="test.jpg"
<test.jpg>
----------------------------271215561418376712309544--
 
HTTP/1.1 200 OK
date: Sat, 08 Jul 2023 15:09:52 GMT
server: uvicorn
content-type: application/json
vary: Accept, Accept-Language, origin
allow: POST, OPTIONS
x-frame-options: SAMEORIGIN
x-api-version: 3
x-version: 1.16.5
content-length: 38
content-language: en-us
x-content-type-options: nosniff
referrer-policy: same-origin
cross-origin-opener-policy: same-origin
 
"7de8f6b1-5ee8-4dec-a0a4-bac26d462f9d"
```


### Paperless-ngx version

1.16.5

### Host OS

RaspberryPI ARM Docker Container

### Installation method

Docker - official image

### Browser

Postman

### Configuration changes

      PAPERLESS_CONSUMER_ENABLE_BARCODES: "true"       PAPERLESS_CONSUMER_ENABLE_ASN_BARCODE: "true"

### Other

_No response_


Discussion:
I was able to solve it myself. Unfortunately, the call only works if PAPERLESS_CONSUMER_ENABLE_BARCODES: "false".
I want to look at this one.  Maybe it's just docs not being clear on the order of priorities, but I can kind of imagine how this would happen

Resolving patch:
diff --git a/src/documents/tasks.py b/src/documents/tasks.py
--- a/src/documents/tasks.py
+++ b/src/documents/tasks.py
@@ -124,10 +124,11 @@ def consume_file(
                 return "File successfully split"
 
             # try reading the ASN from barcode
-            if settings.CONSUMER_ENABLE_ASN_BARCODE:
+            if settings.CONSUMER_ENABLE_ASN_BARCODE and reader.asn is not None:
+                # Note this will take precedence over an API provided ASN
+                # But it's from a physical barcode, so that's good
                 overrides.asn = reader.asn
-                if overrides.asn:
-                    logger.info(f"Found ASN in barcode: {overrides.asn}")
+                logger.info(f"Found ASN in barcode: {overrides.asn}")
 
     # continue with consumption if no barcode was found
     document = Consumer().try_consume_file(


Test patch: