prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  [14.0] account_payment_purchase: changing many2one resets payment_mode
  Hi,
  We've seen that when the field purchase_vendor_bill_id is changed and a purchase is selected from it, the payment_mode_id is always reseted because it is using the reference purchase_id.
  `new_mode = self.purchase_id.payment_mode_id.id or False`
  We've made this change, and it seems to work as it should.
  `new_mode = self.purchase_vendor_bill_id.purchase_order_id.payment_mode_id.id or False`
  The same goes for the partner_bank_id field.
  @MiquelRForgeFlow 
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `account_payment_purchase/models/account_invoice.py`
  Content:
  ```
  1 # Copyright 2016 Akretion (<http://www.akretion.com>).
  2 # Copyright 2017 Tecnativa - Vicent Cubells.
  3 # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4 
  5 from odoo import _, api, models
  6 
  7 
  8 class AccountMove(models.Model):
  9     _inherit = "account.move"
  10 
  11     @api.onchange("purchase_vendor_bill_id", "purchase_id")
  12     def _onchange_purchase_auto_complete(self):
  13         new_mode = self.purchase_id.payment_mode_id.id or False
  14         new_bank = self.purchase_id.supplier_partner_bank_id.id or False
  15         res = super()._onchange_purchase_auto_complete() or {}
  16         if self.payment_mode_id and new_mode and self.payment_mode_id.id != new_mode:
  17             res["warning"] = {
  18                 "title": _("Warning"),
  19                 "message": _("Selected purchase order have different payment mode."),
  20             }
  21             return res
  22         self.payment_mode_id = new_mode
  23         if self.partner_bank_id and new_bank and self.partner_bank_id.id != new_bank:
  24             res["warning"] = {
  25                 "title": _("Warning"),
  26                 "message": _("Selected purchase order have different supplier bank."),
  27             }
  28             return res
  29         self.partner_bank_id = new_bank
  30         return res
  31
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/account_payment_purchase/models/account_invoice.py b/account_payment_purchase/models/account_invoice.py
  --- a/account_payment_purchase/models/account_invoice.py
  +++ b/account_payment_purchase/models/account_invoice.py
  @@ -10,8 +10,16 @@
   
       @api.onchange("purchase_vendor_bill_id", "purchase_id")
       def _onchange_purchase_auto_complete(self):
  -        new_mode = self.purchase_id.payment_mode_id.id or False
  -        new_bank = self.purchase_id.supplier_partner_bank_id.id or False
  +
  +        new_mode = (
  +            self.purchase_vendor_bill_id.purchase_order_id.payment_mode_id.id
  +            or self.purchase_id.payment_mode_id.id
  +        )
  +        new_bank = (
  +            self.purchase_vendor_bill_id.purchase_order_id.supplier_partner_bank_id.id
  +            or self.purchase_id.supplier_partner_bank_id.id
  +        )
  +
           res = super()._onchange_purchase_auto_complete() or {}
           if self.payment_mode_id and new_mode and self.payment_mode_id.id != new_mode:
               res["warning"] = {
  
verification_info: {"golden_diff": "diff --git a/account_payment_purchase/models/account_invoice.py b/account_payment_purchase/models/account_invoice.py\n--- a/account_payment_purchase/models/account_invoice.py\n+++ b/account_payment_purchase/models/account_invoice.py\n@@ -10,8 +10,16 @@\n \n     @api.onchange(\"purchase_vendor_bill_id\", \"purchase_id\")\n     def _onchange_purchase_auto_complete(self):\n-        new_mode = self.purchase_id.payment_mode_id.id or False\n-        new_bank = self.purchase_id.supplier_partner_bank_id.id or False\n+\n+        new_mode = (\n+            self.purchase_vendor_bill_id.purchase_order_id.payment_mode_id.id\n+            or self.purchase_id.payment_mode_id.id\n+        )\n+        new_bank = (\n+            self.purchase_vendor_bill_id.purchase_order_id.supplier_partner_bank_id.id\n+            or self.purchase_id.supplier_partner_bank_id.id\n+        )\n+\n         res = super()._onchange_purchase_auto_complete() or {}\n         if self.payment_mode_id and new_mode and self.payment_mode_id.id != new_mode:\n             res[\"warning\"] = {\n", "issue": "[14.0] account_payment_purchase: changing many2one resets payment_mode\nHi,\r\nWe've seen that when the field purchase_vendor_bill_id is changed and a purchase is selected from it, the payment_mode_id is always reseted because it is using the reference purchase_id.\r\n`new_mode = self.purchase_id.payment_mode_id.id or False`\r\nWe've made this change, and it seems to work as it should.\r\n`new_mode = self.purchase_vendor_bill_id.purchase_order_id.payment_mode_id.id or False`\r\nThe same goes for the partner_bank_id field.\r\n@MiquelRForgeFlow \n", "before_files": [{"content": "# Copyright 2016 Akretion (<http://www.akretion.com>).\n# Copyright 2017 Tecnativa - Vicent Cubells.\n# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).\n\nfrom odoo import _, api, models\n\n\nclass AccountMove(models.Model):\n    _inherit = \"account.move\"\n\n    @api.onchange(\"purchase_vendor_bill_id\", \"purchase_id\")\n    def _onchange_purchase_auto_complete(self):\n        new_mode = self.purchase_id.payment_mode_id.id or False\n        new_bank = self.purchase_id.supplier_partner_bank_id.id or False\n        res = super()._onchange_purchase_auto_complete() or {}\n        if self.payment_mode_id and new_mode and self.payment_mode_id.id != new_mode:\n            res[\"warning\"] = {\n                \"title\": _(\"Warning\"),\n                \"message\": _(\"Selected purchase order have different payment mode.\"),\n            }\n            return res\n        self.payment_mode_id = new_mode\n        if self.partner_bank_id and new_bank and self.partner_bank_id.id != new_bank:\n            res[\"warning\"] = {\n                \"title\": _(\"Warning\"),\n                \"message\": _(\"Selected purchase order have different supplier bank.\"),\n            }\n            return res\n        self.partner_bank_id = new_bank\n        return res\n", "path": "account_payment_purchase/models/account_invoice.py"}], "after_files": [{"content": "# Copyright 2016 Akretion (<http://www.akretion.com>).\n# Copyright 2017 Tecnativa - Vicent Cubells.\n# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).\n\nfrom odoo import _, api, models\n\n\nclass AccountMove(models.Model):\n    _inherit = \"account.move\"\n\n    @api.onchange(\"purchase_vendor_bill_id\", \"purchase_id\")\n    def _onchange_purchase_auto_complete(self):\n\n        new_mode = (\n            self.purchase_vendor_bill_id.purchase_order_id.payment_mode_id.id\n            or self.purchase_id.payment_mode_id.id\n        )\n        new_bank = (\n            self.purchase_vendor_bill_id.purchase_order_id.supplier_partner_bank_id.id\n            or self.purchase_id.supplier_partner_bank_id.id\n        )\n\n        res = super()._onchange_purchase_auto_complete() or {}\n        if self.payment_mode_id and new_mode and self.payment_mode_id.id != new_mode:\n            res[\"warning\"] = {\n                \"title\": _(\"Warning\"),\n                \"message\": _(\"Selected purchase order have different payment mode.\"),\n            }\n            return res\n        self.payment_mode_id = new_mode\n        if self.partner_bank_id and new_bank and self.partner_bank_id.id != new_bank:\n            res[\"warning\"] = {\n                \"title\": _(\"Warning\"),\n                \"message\": _(\"Selected purchase order have different supplier bank.\"),\n            }\n            return res\n        self.partner_bank_id = new_bank\n        return res\n", "path": "account_payment_purchase/models/account_invoice.py"}]}