Attach OCR Document to Existing Invoice
POST/api/purchase-invoices/ocr/:purchase_extraction_id/attach-to-invoice
Attach the OCR'd PDF to an existing PurchaseInvoice that does not yet have a document. Used when the OCR extraction's PO number matches a PO that has already been invoiced — instead of creating a new invoice, the user can retroactively attach the extracted PDF to the prior invoice as the supporting document.
This is the primary resolution path for duplicate_detected extractions: the OCR detected the same supplier + invoice number combination already exists, and the user wants to attach the new PDF to that prior invoice.
Uploads the original PDF (still in S3 from the upload step) as a PurchaseInvoiceAttachment, marks the extraction as 'confirmed', and links the extraction to the target invoice.
Request body fields:
| Field | Type | Required | Notes |
|---|---|---|---|
| purchase_invoice_id | integer | yes | Must exist in purchase_invoices. The invoice's supplier_id must match the extraction's matched supplier and the invoice must not already have an attachment. |
Allowed only when:
- Extraction status is
pending_revieworduplicate_detected - Target invoice's supplier matches the extraction's matched supplier
- Target invoice has no existing attachments (i.e.
can_attach_document: truefrom the candidate_purchase_orders block)
Response 201:
{
"data": {
"purchase_invoice_id": 211,
"extraction_id": 7,
"attachment_id": 559
},
"message": "OCR document attached to existing invoice."
}
Response 422 — wrong extraction status (e.g. confirmed or failed):
{ "message": "Only extractions pending review can be attached." }
Response 422 — supplier mismatch:
{ "message": "The selected invoice's supplier does not match the extraction's matched supplier." }
Response 422 — invoice already has a document:
{ "message": "The selected invoice already has an attached document." }
Response 404 — invoice not found (route model + findOrFail failure).
Request
Responses
- 200
Successful response