Bug 41297

Summary: Add duplicate invoice number detection on EDI invoice import
Product: Koha Reporter: Martin Renvoize (ashimema) <martin.renvoize>
Component: AcquisitionsAssignee: Martin Renvoize (ashimema) <martin.renvoize>
Status: Needs Signoff --- QA Contact: Testopia <testopia>
Severity: enhancement    
Priority: P5 - low CC: hdunne-howrie
Version: Main   
Hardware: All   
OS: All   
GIT URL: Initiative type: ---
Sponsorship status: Sponsored Comma delimited list of Sponsors:
Crowdfunding goal: 0 Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:
Bug Depends on: 40383    
Bug Blocks: 38195    
Attachments: Bug 41297: Block duplicate EDIFACT invoice processing
Bug 41297: Block duplicate EDIFACT invoice processing
Bug 41297: Block duplicate EDIFACT invoice processing

Description Martin Renvoize (ashimema) 2025-11-25 10:25:10 UTC
EDIFACT invoice processing should detect and block duplicate invoice numbers, notify the supplier (possibly via an EDI CONTRL message), and alert library staff.

While Koha has the AcqWarnOnDuplicateInvoice system preference for manual invoice creation (implemented in Bug 10366), this check is not applied to invoices created automatically via EDIFACT/EDI processing in Koha/EDI.pm.
Comment 1 Martin Renvoize (ashimema) 2025-11-26 15:45:19 UTC
Created attachment 189962 [details] [review]
Bug 41297: Block duplicate EDIFACT invoice processing

This patch adds duplicate invoice detection and blocking for EDIFACT
invoice processing, with email notifications to library staff and
vendors.

Key features:
1. System preferences to enable/disable duplicate blocking and email
   notifications
2. Database index on aqinvoices (invoicenumber, booksellerid) for
   performance
3. Email notifications using the message_queue (GetPreparedLetter →
   EnqueueLetter pattern)
4. Vendor contact-based notifications via a new
   aqcontacts.edi_error_notification flag
5. Two notice templates (EDI_DUP_INV_LIBRARY and EDI_DUP_INV_VENDOR)

Changes:
- Add EdiBlockDuplicateInvoice system preference
- Add EdiBlockDuplicateInvoiceEmailNotice system preference
- Add EdiBlockDuplicateInvoiceEmailAddresses system preference
- Add edi_error_notification column to aqcontacts table
- Add duplicate invoice detection logic to Koha::EDI::process_invoice
- Add email notification support using standard Koha messaging queue
- Add "Contact about EDI errors" checkbox to vendor contacts UI
- Add notice templates to sample_notices.yml for fresh installations
- Add test coverage in t/db_dependent/Koha/EDI.t

Test plan:
1. Run database update: perl installer/data/mysql/updatedatabase.pl
2. Enable system preferences:
   - Set EdiBlockDuplicateInvoice to "Block"
   - Set EdiBlockDuplicateInvoiceEmailNotice to "Send"
   - Set EdiBlockDuplicateInvoiceEmailAddresses to valid email(s)
3. Edit a vendor and add a contact with "Contact about EDI errors" enabled
4. Process an EDIFACT invoice
5. Attempt to process the same invoice again - should be blocked
6. Check message_queue table for queued notifications
7. Run tests: prove t/db_dependent/Koha/EDI.t :: duplicate_invoice_blocking
Comment 2 Martin Renvoize (ashimema) 2025-11-26 15:46:44 UTC Comment hidden (obsolete)
Comment 3 Martin Renvoize (ashimema) 2025-11-26 15:47:20 UTC Comment hidden (obsolete)