Skip to content

Commit 042354f

Browse files
committed
[FIX] account_currency_report_ux: approach of using monkey_patches
1 parent 0e6335c commit 042354f

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
22
# Part of Odoo. See LICENSE file for full copyright and licensing details.
33

4-
from . import report
4+
from .hooks import uninstall_hook
5+
from .monkey_patches import *

account_currency_reports_ux/__manifest__.py

+2
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@
4040
'installable': True,
4141
'auto_install': False,
4242
'application': False,
43+
'post_load': 'monkey_patches',
44+
'uninstall_hook': 'uninstall_hook'
4345
}

account_currency_reports_ux/hooks.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
##############################################################################
2+
# For copyright and license notices, see __manifest__.py file in module root
3+
# directory
4+
##############################################################################
5+
from odoo.addons.account.report.account_invoice_report import AccountInvoiceReport
6+
7+
def _revert_method(cls, name):
8+
""" Revert the original method called ``name`` in the given class.
9+
See :meth:`~._patch_method`.
10+
"""
11+
method = getattr(cls, name)
12+
setattr(cls, name, method.origin)
13+
14+
15+
def uninstall_hook(cr, registry):
16+
_revert_method(AccountInvoiceReport, '_select')
17+
_revert_method(AccountInvoiceReport, '_from')

account_currency_reports_ux/report/account_invoice_report.py account_currency_reports_ux/monkey_patches.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from odoo import models, api
1+
from odoo import api
2+
from odoo.addons.account.report.account_invoice_report import AccountInvoiceReport
23

3-
class AccountInvoiceReport(models.Model):
4-
_inherit = "account.invoice.report"
4+
def monkey_patches():
55

6+
# monkey patch
67
@api.model
7-
def _select(self):
8+
def _select_patch(self):
89
return '''
910
WITH currency_rate AS MATERIALIZED (
1011
SELECT
@@ -66,7 +67,7 @@ def _select(self):
6667
''' % self.env.company.id
6768

6869
@api.model
69-
def _from(self):
70+
def _from_patch(self):
7071
return '''
7172
FROM account_move_line line
7273
LEFT JOIN res_partner partner ON partner.id = line.partner_id
@@ -78,9 +79,19 @@ def _from(self):
7879
INNER JOIN account_move move ON move.id = line.move_id
7980
LEFT JOIN res_partner commercial_partner ON commercial_partner.id = move.commercial_partner_id
8081
lEFT JOIN currency_rate currency_table on
81-
(currency_table.company_id = line.company_id and
82-
currency_table.currency_id = line.currency_id and
82+
(currency_table.currency_id = line.currency_id and
8383
currency_table.date_start <= COALESCE(line.date, NOW()) and
8484
(currency_table.date_end IS NULL OR currency_table.date_end > COALESCE(line.date, NOW())))
85-
LEFT JOIN res_company rc on rc.id=line.company_id
85+
LEFT JOIN res_company rc on rc.id=currency_table.company_id
8686
'''
87+
88+
def _patch_method(cls, name, method):
89+
origin = getattr(cls, name)
90+
method.origin = origin
91+
# propagate decorators from origin to method, and apply api decorator
92+
wrapped = api.propagate(origin, method)
93+
wrapped.origin = origin
94+
setattr(cls, name, wrapped)
95+
96+
_patch_method(AccountInvoiceReport, '_select', _select_patch)
97+
_patch_method(AccountInvoiceReport, '_from', _from_patch)

account_currency_reports_ux/report/__init__.py

-4
This file was deleted.

0 commit comments

Comments
 (0)