odoo/odoo#204022

Created by fw-bot
Merged at dba3e5be062849fbd6c94032a870d8cab67c6541

Statuses:

label
odoo-dev:saas-18.1-17.0-purchase-fix-match-only-invoice-lines-loug-426410-fw
head
6b70febee371fbe4289c52c3e40429abd2b6d439
merged
7 months ago by Training, Louis Gobert (loug)
odoo/odoo
17.0 #197637
saas-17.4 #203981
18.0 #204005
saas-18.1 #204022
saas-18.2 #204068
saas-18.3
saas-18.4
19.0
master #204071

[FW][FIX] purchase: match only invoice lines in matching process

Currently, an error can occur in _find_matching_po_and_inv_lines when some invoice lines have a price_unit of 0. This happens because the matching logic sorts and matches lines by (price_unit, qty), leading to "Journal Items" (line_ids) being incorrectly matched with purchase order lines.

The issue arises because the _find_matching_po_and_inv_lines function checks all line_ids, instead of only invoice_line_ids. When a PO line has a price_unit of 0, it might match with a line that isn't an actual invoice line. As a result, unmatched_lines becomes incorrect, and it could remove a valid invoice line. Then, later, when calculating inv_and_po_lines, the process only considers invoice_line_ids. Since no matching line is found, an error occurs when attempting to delete the line (id is False).

Steps to reproduce:
1. Create a purchase order with:
- 1 product A with price_unit = 0
2. Confirm the purchase order.
3. Upload an EDI XML bill (e.g., IT localization) with:
- A reference to the purchase order
- 1 product B with price_unit = 100

Since all bill lines are processed, a tax line or a payment term line might incorrectly match with the PO line, causing the error.

opw-4513869

Forward-Port-Of: #197637