Skip to content

Commit 5cf88fb

Browse files
committed
[IMP] new module: account_currency_reports_ux
1 parent 9194357 commit 5cf88fb

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
# Part of Odoo. See LICENSE file for full copyright and licensing details.
3+
4+
from .hooks import uninstall_hook
5+
from .monkey_patches import *
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
##############################################################################
2+
#
3+
# Copyright (C) 2024 ADHOC SA (http://www.adhoc.com.ar)
4+
# All Rights Reserved.
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Affero General Public License as
8+
# published by the Free Software Foundation, either version 3 of the
9+
# License, or (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Affero General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Affero General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
19+
##############################################################################
20+
{
21+
'name': 'Account Currency Reports UX',
22+
'version': "18.0.1.0.0",
23+
'category': 'Accounting',
24+
'sequence': 14,
25+
'summary': 'Restrict the use of certain journals to certain users',
26+
'author': 'ADHOC SA',
27+
'website': 'www.adhoc.com.ar',
28+
'license': 'AGPL-3',
29+
'images': [
30+
],
31+
'depends': [
32+
'account',
33+
],
34+
'data': [
35+
],
36+
'demo': [
37+
],
38+
'test': [
39+
],
40+
'installable': True,
41+
'auto_install': False,
42+
'application': False,
43+
'post_load': 'monkey_patches',
44+
'uninstall_hook': 'uninstall_hook'
45+
}

account_currency_reports_ux/hooks.py

Lines changed: 17 additions & 0 deletions
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')
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
from odoo import api
2+
from odoo.addons.account.report.account_invoice_report import AccountInvoiceReport
3+
4+
def monkey_patches():
5+
6+
# monkey patch
7+
@api.model
8+
def _select_patch(self):
9+
return '''
10+
WITH currency_rate AS MATERIALIZED (
11+
SELECT
12+
r.currency_id,
13+
COALESCE(r.company_id, c.id) as company_id,
14+
1/r.rate as rate,
15+
r.name AS date_start,
16+
(SELECT name FROM res_currency_rate r2
17+
WHERE r2.name > r.name AND
18+
r2.currency_id = r.currency_id AND
19+
(r2.company_id = c.id)
20+
ORDER BY r2.name ASC
21+
LIMIT 1) AS date_end
22+
FROM res_currency_rate r
23+
JOIN res_company c ON (r.company_id = c.id)
24+
WHERE c.id = %s
25+
)
26+
SELECT
27+
line.id,
28+
line.move_id,
29+
line.product_id,
30+
line.account_id,
31+
line.journal_id,
32+
line.company_id,
33+
line.company_currency_id,
34+
line.partner_id AS commercial_partner_id,
35+
account.account_type AS user_type,
36+
move.state,
37+
move.move_type,
38+
move.partner_id,
39+
move.invoice_user_id,
40+
move.fiscal_position_id,
41+
move.payment_state,
42+
move.invoice_date,
43+
move.invoice_date_due,
44+
uom_template.id AS product_uom_id,
45+
template.categ_id AS product_categ_id,
46+
line.quantity / NULLIF(COALESCE(uom_line.factor, 1) / COALESCE(uom_template.factor, 1), 0.0) * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END)
47+
AS quantity,
48+
CASE WHEN line.currency_id <> rc.currency_id THEN -line.balance * currency_table.rate ELSE -line.balance END AS price_subtotal,
49+
line.price_total * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END)
50+
AS price_total,
51+
CASE WHEN line.currency_id <> rc.currency_id THEN
52+
-COALESCE(
53+
-- Average line price
54+
(line.balance / NULLIF(line.quantity, 0.0)) * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END)
55+
-- convert to template uom
56+
* (NULLIF(COALESCE(uom_line.factor, 1), 0.0) / NULLIF(COALESCE(uom_template.factor, 1), 0.0)),
57+
0.0) * currency_table.rate
58+
ELSE -COALESCE(
59+
-- Average line price
60+
(line.balance / NULLIF(line.quantity, 0.0)) * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END)
61+
-- convert to template uom
62+
* (NULLIF(COALESCE(uom_line.factor, 1), 0.0) / NULLIF(COALESCE(uom_template.factor, 1), 0.0)),
63+
0.0) END
64+
AS price_average,
65+
COALESCE(partner.country_id, commercial_partner.country_id) AS country_id,
66+
line.currency_id AS currency_id
67+
''' % self.env.company.id
68+
69+
@api.model
70+
def _from_patch(self):
71+
return '''
72+
FROM account_move_line line
73+
LEFT JOIN res_partner partner ON partner.id = line.partner_id
74+
LEFT JOIN product_product product ON product.id = line.product_id
75+
LEFT JOIN account_account account ON account.id = line.account_id
76+
LEFT JOIN product_template template ON template.id = product.product_tmpl_id
77+
LEFT JOIN uom_uom uom_line ON uom_line.id = line.product_uom_id
78+
LEFT JOIN uom_uom uom_template ON uom_template.id = template.uom_id
79+
INNER JOIN account_move move ON move.id = line.move_id
80+
LEFT JOIN res_partner commercial_partner ON commercial_partner.id = move.commercial_partner_id
81+
lEFT JOIN currency_rate currency_table on
82+
(currency_table.currency_id = line.currency_id and
83+
currency_table.date_start <= COALESCE(line.date, NOW()) and
84+
(currency_table.date_end IS NULL OR currency_table.date_end > COALESCE(line.date, NOW())))
85+
LEFT JOIN res_company rc on rc.id=currency_table.company_id
86+
'''
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)

0 commit comments

Comments
 (0)