From ad04b737910ac3d40cf635e8f916eb9e27820d94 Mon Sep 17 00:00:00 2001 From: LorenzoC0 Date: Thu, 21 Nov 2024 12:06:57 +0100 Subject: [PATCH 1/2] [IMP] l10n_it_reverse_charge: Improved currency amount calculation for self-invoices, now considers original invoice date --- l10n_it_reverse_charge/models/__init__.py | 2 +- l10n_it_reverse_charge/models/account_move.py | 20 ++++++++++++++----- l10n_it_reverse_charge/models/res_currency.py | 10 ++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 l10n_it_reverse_charge/models/res_currency.py diff --git a/l10n_it_reverse_charge/models/__init__.py b/l10n_it_reverse_charge/models/__init__.py index af27e9de8e36..169b2a3c59ec 100644 --- a/l10n_it_reverse_charge/models/__init__.py +++ b/l10n_it_reverse_charge/models/__init__.py @@ -3,4 +3,4 @@ # Copyright 2017 Lorenzo Battistini - Agile Business Group # Copyright 2017 Marco Calcagni - Dinamiche Aziendali srl -from . import account_fiscal_position, account_move, account_rc_type +from . import account_fiscal_position, account_move, account_rc_type, res_currency diff --git a/l10n_it_reverse_charge/models/account_move.py b/l10n_it_reverse_charge/models/account_move.py index 22bfe9a2217d..8532825880d6 100644 --- a/l10n_it_reverse_charge/models/account_move.py +++ b/l10n_it_reverse_charge/models/account_move.py @@ -413,11 +413,17 @@ def generate_self_invoice(self): for line in rc_invoice.line_ids: line.remove_move_reconcile() rc_invoice.invoice_line_ids.unlink() - rc_invoice.write(inv_vals) + rc_invoice.with_context( + force_conversion_date=self.rc_original_purchase_invoice_ids[0].invoice_date, + ).write(inv_vals) else: - rc_invoice = self.create(inv_vals) + rc_invoice = self.with_context( + force_conversion_date=self.rc_original_purchase_invoice_ids[0].invoice_date, + ).create(inv_vals) self.rc_self_invoice_id = rc_invoice.id - rc_invoice.action_post() + rc_invoice.with_context( + force_conversion_date=self.invoice_date, + ).action_post() if self.amount_total: # No need to reconcile invoices with total = 0 @@ -468,10 +474,14 @@ def generate_supplier_self_invoice(self): if inv_line.account_id: line_vals["account_id"] = rc_type.transitory_account_id.id invoice_line_vals.append((0, 0, line_vals)) - supplier_invoice.write({"invoice_line_ids": invoice_line_vals}) + supplier_invoice.with_context( + force_conversion_date=self.invoice_date, + ).write({"invoice_line_ids": invoice_line_vals}) self.rc_self_purchase_invoice_id = supplier_invoice.id - supplier_invoice.action_post() + supplier_invoice.with_context( + force_conversion_date=self.invoice_date, + ).action_post() supplier_invoice.fiscal_position_id = self.fiscal_position_id.id def action_post(self): diff --git a/l10n_it_reverse_charge/models/res_currency.py b/l10n_it_reverse_charge/models/res_currency.py new file mode 100644 index 000000000000..a18d5fd79026 --- /dev/null +++ b/l10n_it_reverse_charge/models/res_currency.py @@ -0,0 +1,10 @@ +from odoo import models + +class ResCurrency(models.BaseModel): + _inherit = 'res.currency' + + def _convert(self, from_amount, to_currency, company, date, round=True): + if self.env.context.get('force_conversion_date'): + return super(ResCurrency, self)._convert(from_amount, to_currency, company, date=self.env.context['force_conversion_date'], round=round) + + return super(ResCurrency, self)._convert(from_amount, to_currency, company, date, round) \ No newline at end of file From 1a115c318de8787f87eedfc81278f532985cb46b Mon Sep 17 00:00:00 2001 From: eLBati Date: Tue, 26 Nov 2024 15:16:12 +0100 Subject: [PATCH 2/2] FIX tests File "/opt/l10n-italy/l10n_it_reverse_charge/models/account_move.py", line 421, in generate_self_invoice force_conversion_date=self.rc_original_purchase_invoice_ids[0].invoice_date, File "/opt/odoo/odoo/models.py", line 5731, in __getitem__ return self.browse((self._ids[key],)) IndexError: tuple index out of range + pre-commit --- l10n_it_reverse_charge/models/account_move.py | 12 ++++++++++-- l10n_it_reverse_charge/models/res_currency.py | 17 +++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/l10n_it_reverse_charge/models/account_move.py b/l10n_it_reverse_charge/models/account_move.py index 8532825880d6..8487b1abae8e 100644 --- a/l10n_it_reverse_charge/models/account_move.py +++ b/l10n_it_reverse_charge/models/account_move.py @@ -414,11 +414,19 @@ def generate_self_invoice(self): line.remove_move_reconcile() rc_invoice.invoice_line_ids.unlink() rc_invoice.with_context( - force_conversion_date=self.rc_original_purchase_invoice_ids[0].invoice_date, + force_conversion_date=self.rc_original_purchase_invoice_ids[ + 0 + ].invoice_date + if self.rc_original_purchase_invoice_ids + else self.rc_purchase_invoice_id.invoice_date, ).write(inv_vals) else: rc_invoice = self.with_context( - force_conversion_date=self.rc_original_purchase_invoice_ids[0].invoice_date, + force_conversion_date=self.rc_original_purchase_invoice_ids[ + 0 + ].invoice_date + if self.rc_original_purchase_invoice_ids + else self.rc_purchase_invoice_id.invoice_date, ).create(inv_vals) self.rc_self_invoice_id = rc_invoice.id rc_invoice.with_context( diff --git a/l10n_it_reverse_charge/models/res_currency.py b/l10n_it_reverse_charge/models/res_currency.py index a18d5fd79026..ecea37709d20 100644 --- a/l10n_it_reverse_charge/models/res_currency.py +++ b/l10n_it_reverse_charge/models/res_currency.py @@ -1,10 +1,19 @@ from odoo import models + class ResCurrency(models.BaseModel): - _inherit = 'res.currency' + _inherit = "res.currency" def _convert(self, from_amount, to_currency, company, date, round=True): - if self.env.context.get('force_conversion_date'): - return super(ResCurrency, self)._convert(from_amount, to_currency, company, date=self.env.context['force_conversion_date'], round=round) + if self.env.context.get("force_conversion_date"): + return super(ResCurrency, self)._convert( + from_amount, + to_currency, + company, + date=self.env.context["force_conversion_date"], + round=round, + ) - return super(ResCurrency, self)._convert(from_amount, to_currency, company, date, round) \ No newline at end of file + return super(ResCurrency, self)._convert( + from_amount, to_currency, company, date, round + )