[FIX] sale_order_type_invoice_policy_invoice_link: replace process-wide monkey patch with model inheritance - #992
Open
gal-adhoc wants to merge 1 commit into
Open
Conversation
…de monkey patch with model inheritance The module patched stock_picking_invoice_link's StockMove.write at import time, which mutates the class for the whole Odoo process instead of for the databases where this module is installed. On a database that has stock_picking_invoice_link installed but not sale_order_type_invoice_policy, the patched write still ran and crashed on picking validation with AttributeError: 'sale.order.type' object has no attribute 'invoice_policy'. Use a regular _inherit = "stock.move" override instead, so the code can only run in the registries where the module is actually installed. The duplicated "You can not modify an invoiced stock move" check is dropped: it now runs through super() from stock_picking_invoice_link, which the monkey patch was replacing. Business logic is unchanged.
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
models/stock_move.pypatchedstock_picking_invoice_link'sStockMove.writeat import time:That mutates the class object for the whole Odoo process, and nothing ever undoes it. Uninstalling this module removes its models from the registry but does not un-import the Python module nor restore the original
write. The patched body readssale_line_id.order_id.type_id.invoice_policy, a field contributed bysale_order_type_invoice_policy, so once that field is gone every picking validation crashes until the worker is restarted.Reproduced on a production database:
sale_order_type_invoice_policy—sale_order_type_invoice_policy_invoice_linkauto-installs and the patch is applied to the class.sale.order.type.invoice_policy, but the patchedwriteis still in place.23 consecutive failures over ~50 minutes, from a module that was not installed on that database.
Fix
Use a regular
_inherit = "stock.move"override. The code then lives in the registry instead of in the class object, so it is present exactly when the module is installed and gone as soon as it is uninstalled. That removes the whole class of failure rather than guarding against this one symptom.Also drops the duplicated
You can not modify an invoiced stock movecheck: the monkey patch had to reimplement it because it was replacingstock_picking_invoice_link.write. With normal inheritance that check runs throughsuper()again.Business logic is unchanged — invoice lines are still linked when the sale order type policy is
order, or when it isby_productand the product policy isorder.Test plan
sale_order_type_invoice_policyand this module installed: create asale.order.typewithinvoice_policy = 'order', confirm a SO with that type, invoice it, validate the delivery, and checkstock.move.invoice_line_idsis populated. Same behaviour as before.AttributeError. This is the regression that motivated the change.product_uom_qtyon a done move that has linked invoice lines still raises theUserError.Ref: https://www.adhoc.inc/odoo/helpdesk.ticket/124767