From f922c7c1fefeb5bac915ba8b4858c065fe99f170 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roc=C3=ADo=20Vega?= Date: Tue, 17 Dec 2024 13:02:49 -0300 Subject: [PATCH] [IMP] new module: account_currency_reports_ux --- account_currency_reports_ux/__init__.py | 5 + account_currency_reports_ux/__manifest__.py | 45 +++++++++ account_currency_reports_ux/hooks.py | 17 ++++ account_currency_reports_ux/monkey_patches.py | 98 +++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 account_currency_reports_ux/__init__.py create mode 100644 account_currency_reports_ux/__manifest__.py create mode 100644 account_currency_reports_ux/hooks.py create mode 100644 account_currency_reports_ux/monkey_patches.py diff --git a/account_currency_reports_ux/__init__.py b/account_currency_reports_ux/__init__.py new file mode 100644 index 000000000..8a3896d1e --- /dev/null +++ b/account_currency_reports_ux/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from .hooks import uninstall_hook +from .monkey_patches import * diff --git a/account_currency_reports_ux/__manifest__.py b/account_currency_reports_ux/__manifest__.py new file mode 100644 index 000000000..14d7b0f51 --- /dev/null +++ b/account_currency_reports_ux/__manifest__.py @@ -0,0 +1,45 @@ +############################################################################## +# +# Copyright (C) 2024 ADHOC SA (http://www.adhoc.com.ar) +# All Rights Reserved. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +{ + 'name': 'Account Currency Reports UX', + 'version': "16.0.1.0.0", + 'category': 'Accounting', + 'sequence': 14, + 'summary': 'Restrict the use of certain journals to certain users', + 'author': 'ADHOC SA', + 'website': 'www.adhoc.com.ar', + 'license': 'AGPL-3', + 'images': [ + ], + 'depends': [ + 'account', + ], + 'data': [ + ], + 'demo': [ + ], + 'test': [ + ], + 'installable': True, + 'auto_install': False, + 'application': False, + 'post_load': 'monkey_patches', + 'uninstall_hook': 'uninstall_hook' +} diff --git a/account_currency_reports_ux/hooks.py b/account_currency_reports_ux/hooks.py new file mode 100644 index 000000000..e72478f2a --- /dev/null +++ b/account_currency_reports_ux/hooks.py @@ -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') diff --git a/account_currency_reports_ux/monkey_patches.py b/account_currency_reports_ux/monkey_patches.py new file mode 100644 index 000000000..aff158439 --- /dev/null +++ b/account_currency_reports_ux/monkey_patches.py @@ -0,0 +1,98 @@ +from odoo import api +from odoo.addons.account.report.account_invoice_report import AccountInvoiceReport + +def monkey_patches(): + + # monkey patch + @api.model + def _select_patch(self): + return ''' + WITH currency_rate AS MATERIALIZED ( + SELECT + r.currency_id, + COALESCE(r.company_id, c.id) as company_id, + 1/r.rate as rate, + r.name AS date_start, + (SELECT name FROM res_currency_rate r2 + WHERE r2.name > r.name AND + r2.currency_id = r.currency_id AND + (r2.company_id = c.id) + ORDER BY r2.name ASC + LIMIT 1) AS date_end + FROM res_currency_rate r + JOIN res_company c ON (r.company_id = c.id) + WHERE c.id = %s + ) + SELECT + line.id, + line.move_id, + line.product_id, + line.account_id, + line.journal_id, + line.company_id, + line.company_currency_id, + line.partner_id AS commercial_partner_id, + account.account_type AS user_type, + move.state, + move.move_type, + move.partner_id, + move.invoice_user_id, + move.fiscal_position_id, + move.payment_state, + move.invoice_date, + move.invoice_date_due, + uom_template.id AS product_uom_id, + template.categ_id AS product_categ_id, + 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) + AS quantity, + CASE WHEN rc.currency_id <> line.company_currency_id THEN -line.balance * currency_table.rate ELSE -line.balance END AS price_subtotal, + line.price_total * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END) + AS price_total, + CASE WHEN rc.currency_id <> line.company_currency_id THEN + -COALESCE( + -- Average line price + (line.balance / NULLIF(line.quantity, 0.0)) * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END) + -- convert to template uom + * (NULLIF(COALESCE(uom_line.factor, 1), 0.0) / NULLIF(COALESCE(uom_template.factor, 1), 0.0)), + 0.0) * currency_table.rate + ELSE -COALESCE( + -- Average line price + (line.balance / NULLIF(line.quantity, 0.0)) * (CASE WHEN move.move_type IN ('in_invoice','out_refund','in_receipt') THEN -1 ELSE 1 END) + -- convert to template uom + * (NULLIF(COALESCE(uom_line.factor, 1), 0.0) / NULLIF(COALESCE(uom_template.factor, 1), 0.0)), + 0.0) END + AS price_average, + COALESCE(partner.country_id, commercial_partner.country_id) AS country_id, + line.currency_id AS currency_id + ''' % self.env.company.id + + @api.model + def _from_patch(self): + return ''' + FROM account_move_line line + LEFT JOIN res_partner partner ON partner.id = line.partner_id + LEFT JOIN product_product product ON product.id = line.product_id + LEFT JOIN account_account account ON account.id = line.account_id + LEFT JOIN product_template template ON template.id = product.product_tmpl_id + LEFT JOIN uom_uom uom_line ON uom_line.id = line.product_uom_id + LEFT JOIN uom_uom uom_template ON uom_template.id = template.uom_id + 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.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=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)