Skip to content

Commit

Permalink
[FIX] account_currency_report_ux: approach of using monkey_patches
Browse files Browse the repository at this point in the history
  • Loading branch information
rov-adhoc committed Dec 18, 2024
1 parent 0e6335c commit 042354f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
3 changes: 2 additions & 1 deletion account_currency_reports_ux/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import report
from .hooks import uninstall_hook
from .monkey_patches import *
2 changes: 2 additions & 0 deletions account_currency_reports_ux/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@
'installable': True,
'auto_install': False,
'application': False,
'post_load': 'monkey_patches',
'uninstall_hook': 'uninstall_hook'
}
17 changes: 17 additions & 0 deletions account_currency_reports_ux/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##############################################################################
# For copyright and license notices, see __manifest__.py file in module root
# directory
##############################################################################
from odoo.addons.account.report.account_invoice_report import AccountInvoiceReport

def _revert_method(cls, name):
""" Revert the original method called ``name`` in the given class.
See :meth:`~._patch_method`.
"""
method = getattr(cls, name)
setattr(cls, name, method.origin)


def uninstall_hook(cr, registry):
_revert_method(AccountInvoiceReport, '_select')
_revert_method(AccountInvoiceReport, '_from')
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from odoo import models, api
from odoo import api
from odoo.addons.account.report.account_invoice_report import AccountInvoiceReport

class AccountInvoiceReport(models.Model):
_inherit = "account.invoice.report"
def monkey_patches():

# monkey patch
@api.model
def _select(self):
def _select_patch(self):
return '''
WITH currency_rate AS MATERIALIZED (
SELECT
Expand Down Expand Up @@ -66,7 +67,7 @@ def _select(self):
''' % self.env.company.id

@api.model
def _from(self):
def _from_patch(self):
return '''
FROM account_move_line line
LEFT JOIN res_partner partner ON partner.id = line.partner_id
Expand All @@ -78,9 +79,19 @@ def _from(self):
INNER JOIN account_move move ON move.id = line.move_id
LEFT JOIN res_partner commercial_partner ON commercial_partner.id = move.commercial_partner_id
lEFT JOIN currency_rate currency_table on
(currency_table.company_id = line.company_id and
currency_table.currency_id = line.currency_id and
(currency_table.currency_id = line.currency_id and
currency_table.date_start <= COALESCE(line.date, NOW()) and
(currency_table.date_end IS NULL OR currency_table.date_end > COALESCE(line.date, NOW())))
LEFT JOIN res_company rc on rc.id=line.company_id
LEFT JOIN res_company rc on rc.id=currency_table.company_id
'''

def _patch_method(cls, name, method):
origin = getattr(cls, name)
method.origin = origin
# propagate decorators from origin to method, and apply api decorator
wrapped = api.propagate(origin, method)
wrapped.origin = origin
setattr(cls, name, wrapped)

_patch_method(AccountInvoiceReport, '_select', _select_patch)
_patch_method(AccountInvoiceReport, '_from', _from_patch)
4 changes: 0 additions & 4 deletions account_currency_reports_ux/report/__init__.py

This file was deleted.

0 comments on commit 042354f

Please sign in to comment.