From 4cc56ba6cf827fd66d6608275b2d0cb4a55e9c58 Mon Sep 17 00:00:00 2001 From: Borruso Date: Fri, 16 Jun 2023 08:45:02 +0200 Subject: [PATCH] [MIG] l10n_it_split_payment: Migration to 16.0 --- l10n_it_split_payment/README.rst | 22 +- l10n_it_split_payment/__manifest__.py | 3 +- .../migrations/14.0.1.0.0/post-migration.py | 33 --- .../14.0.1.0.0/pre-move_split_amount.py | 23 --- l10n_it_split_payment/models/account.py | 194 ++++++++---------- .../static/description/index.html | 16 +- .../tests/test_splitpayment.py | 19 +- l10n_it_split_payment/views/account_view.xml | 15 +- 8 files changed, 115 insertions(+), 210 deletions(-) delete mode 100644 l10n_it_split_payment/migrations/14.0.1.0.0/post-migration.py delete mode 100644 l10n_it_split_payment/migrations/14.0.1.0.0/pre-move_split_amount.py diff --git a/l10n_it_split_payment/README.rst b/l10n_it_split_payment/README.rst index cd6b44d533b..992830f0a4b 100644 --- a/l10n_it_split_payment/README.rst +++ b/l10n_it_split_payment/README.rst @@ -14,14 +14,14 @@ ITA - Scissione pagamenti :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fl10n--italy-lightgray.png?logo=github - :target: https://github.com/OCA/l10n-italy/tree/14.0/l10n_it_split_payment + :target: https://github.com/OCA/l10n-italy/tree/16.0/l10n_it_split_payment :alt: OCA/l10n-italy .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/l10n-italy-14-0/l10n-italy-14-0-l10n_it_split_payment + :target: https://translation.odoo-community.org/projects/l10n-italy-16-0/l10n-italy-16-0-l10n_it_split_payment :alt: Translate me on Weblate -.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png - :target: https://runbot.odoo-community.org/runbot/122/14.0 - :alt: Try me on Runbot +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/l10n-italy&target_branch=16.0 + :alt: Try me on Runboat |badge1| |badge2| |badge3| |badge4| |badge5| @@ -57,7 +57,7 @@ To configure this module, you need to: * go to Accounting → Configuration → Settings and configure 'Split Payment Write-off account' (like 'IVA n/debito sospesa SP'). Write-off account should be different from standard debit VAT, in order to separately add it in VAT statement. -.. figure:: https://raw.githubusercontent.com/OCA/l10n-italy/14.0/l10n_it_split_payment/static/settings.png +.. figure:: https://raw.githubusercontent.com/OCA/l10n-italy/16.0/l10n_it_split_payment/static/settings.png :alt: Configuration :width: 600 px @@ -72,11 +72,11 @@ To configure this module, you need to: * add a new tax (Accounting → Configuration → Accounting → Taxes). IVA al 22% SPL should be configured like the following: -.. figure:: https://raw.githubusercontent.com/OCA/l10n-italy/14.0/l10n_it_split_payment/static/SP.png +.. figure:: https://raw.githubusercontent.com/OCA/l10n-italy/16.0/l10n_it_split_payment/static/SP.png :alt: 22SPL :width: 600 px -.. figure:: https://raw.githubusercontent.com/OCA/l10n-italy/14.0/l10n_it_split_payment/static/SP2.png +.. figure:: https://raw.githubusercontent.com/OCA/l10n-italy/16.0/l10n_it_split_payment/static/SP2.png :alt: 22SPL :width: 600 px @@ -91,7 +91,7 @@ To configure this module, you need to: * configure the fiscal position (Accounting → Configuration → Accounting → Fiscal Positions) used for split payment, setting 'Split Payment' flag. In fiscal position, map standard VAT with SP VAT, like the following: -.. figure:: https://raw.githubusercontent.com/OCA/l10n-italy/14.0/l10n_it_split_payment/static/fiscal_position.png +.. figure:: https://raw.githubusercontent.com/OCA/l10n-italy/16.0/l10n_it_split_payment/static/fiscal_position.png :alt: Fiscal position :width: 600 px @@ -112,7 +112,7 @@ Bug Tracker Bugs are tracked on `GitHub Issues `_. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us smashing it by providing a detailed and welcomed -`feedback `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -153,6 +153,6 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use. -This module is part of the `OCA/l10n-italy `_ project on GitHub. +This module is part of the `OCA/l10n-italy `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/l10n_it_split_payment/__manifest__.py b/l10n_it_split_payment/__manifest__.py index 8f0c4efdaa0..57e904e78de 100644 --- a/l10n_it_split_payment/__manifest__.py +++ b/l10n_it_split_payment/__manifest__.py @@ -6,12 +6,13 @@ { "name": "ITA - Scissione pagamenti", - "version": "14.0.1.0.7", + "version": "16.0.1.0.0", "category": "Localization/Italy", "summary": "Scissione pagamenti", "author": "Abstract, Agile Business Group, Odoo Community Association (OCA)", "website": "https://github.com/OCA/l10n-italy", "license": "AGPL-3", + "maintainers": ["Borruso"], "depends": [ "account", ], diff --git a/l10n_it_split_payment/migrations/14.0.1.0.0/post-migration.py b/l10n_it_split_payment/migrations/14.0.1.0.0/post-migration.py deleted file mode 100644 index b28b920131c..00000000000 --- a/l10n_it_split_payment/migrations/14.0.1.0.0/post-migration.py +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright 2021 Simone Vanin - Agile Business Group -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from openupgradelib import openupgrade - -from odoo import SUPERUSER_ID, api - - -def migrate(cr, installed_version): - env = api.Environment(cr, SUPERUSER_ID, {}) - MoveLine = env["account.move.line"] - Translations = env["ir.translation"] - source = "Split Payment Write Off" - text_vals = {source} - langs = Translations._get_languages() - for lang, _lang_name in langs: - text_val = env["ir.translation"]._get_source( - False, "code", lang=lang, source=source - ) - text_vals.add(text_val) - text_vals = list(text_vals) - line_ids = MoveLine.search([("name", "in", text_vals)]).ids - if line_ids: - openupgrade.logged_query( - cr, - """ - update account_move_line - set is_split_payment = True - where id in {line_ids} - """.format( - line_ids=tuple(line_ids) - ), - ) diff --git a/l10n_it_split_payment/migrations/14.0.1.0.0/pre-move_split_amount.py b/l10n_it_split_payment/migrations/14.0.1.0.0/pre-move_split_amount.py deleted file mode 100644 index 75edce9d789..00000000000 --- a/l10n_it_split_payment/migrations/14.0.1.0.0/pre-move_split_amount.py +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2021 Simone Rubino - Agile Business Group -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - -from openupgradelib import openupgrade - - -@openupgrade.migrate() -def migrate(env, version): - openupgrade.add_fields( - env, - [("amount_sp", "account.move", False, "float", False, "l10n_it_split_payment")], - ) - openupgrade.logged_query( - env.cr, - """ - update account_move - set - amount_sp = inv.amount_sp - from account_invoice inv - where - account_move.id = inv.move_id; - """, - ) diff --git a/l10n_it_split_payment/models/account.py b/l10n_it_split_payment/models/account.py index 60dbd3346cb..e280ef117f0 100644 --- a/l10n_it_split_payment/models/account.py +++ b/l10n_it_split_payment/models/account.py @@ -3,7 +3,7 @@ # Copyright 2016 Alessio Gerace - Agile Business Group # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import _, fields, models +from odoo import _, api, fields, models from odoo.exceptions import UserError from odoo.tools import float_compare @@ -11,7 +11,7 @@ class AccountFiscalPosition(models.Model): _inherit = "account.fiscal.position" - split_payment = fields.Boolean("Split Payment") + split_payment = fields.Boolean() class AccountMove(models.Model): @@ -29,20 +29,35 @@ class AccountMove(models.Model): ) def _compute_amount(self): - super()._compute_amount() + res = super()._compute_amount() for move in self: if move.split_payment: if move.is_purchase_document(): continue - move.amount_sp = move.amount_tax - move.amount_tax = 0.0 + if move.tax_totals: + move.amount_sp = ( + move.tax_totals["amount_total"] + - move.tax_totals["amount_untaxed"] + ) + move.amount_residual -= move.amount_tax + move.amount_tax = 0.0 + else: + move.amount_sp = 0.0 move.amount_total = move.amount_untaxed - move._compute_split_payments() else: move.amount_sp = 0.0 + return res + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + is_split_payment = fields.Boolean() def _build_debit_line(self): - if not self.company_id.sp_account_id: + self.ensure_one() + + if not self.move_id.company_id.sp_account_id: raise UserError( _( "Please set 'Split Payment Write-off Account' field in" @@ -51,117 +66,70 @@ def _build_debit_line(self): ) vals = { "name": _("Split Payment Write Off"), - "partner_id": self.partner_id.id, - "account_id": self.company_id.sp_account_id.id, - "journal_id": self.journal_id.id, - "date": self.invoice_date, - "price_unit": -self.amount_sp, - "amount_currency": self.amount_sp, - "debit": self.amount_sp, - "credit": 0.0, - "exclude_from_invoice_tab": True, + "partner_id": self.move_id.partner_id.id, + "account_id": self.move_id.company_id.sp_account_id.id, + "journal_id": self.move_id.journal_id.id, + "date": self.move_id.invoice_date, + "date_maturity": self.move_id.invoice_date, + "price_unit": -self.credit, + "amount_currency": self.credit, + "debit": self.credit, + "credit": self.debit, + "display_type": "tax", "is_split_payment": True, } - if self.move_type == "out_refund": - vals["amount_currency"] = -self.amount_sp - vals["debit"] = 0.0 - vals["credit"] = self.amount_sp + if self.move_id.move_type == "out_refund": + vals["amount_currency"] = -self.debit + vals["debit"] = self.credit + vals["credit"] = self.debit return vals - def set_receivable_line_ids(self): - """Recompute all account move lines by _recompute_dynamic_lines() - and set correct receivable lines - """ - self._recompute_dynamic_lines() - line_client_ids = self.line_ids.filtered( - lambda l: l.account_id.id - == self.partner_id.property_account_receivable_id.id - ) - if self.move_type == "out_invoice": - for line_client in line_client_ids: - inv_total = self.amount_sp + self.amount_total - if inv_total: - receivable_line_amount = ( - self.amount_total * line_client.debit - ) / inv_total - else: - receivable_line_amount = 0.0 - line_client.with_context(check_move_validity=False).update( - { - "price_unit": -receivable_line_amount, - "amount_currency": receivable_line_amount, - "debit": receivable_line_amount, - "credit": 0.0, - } - ) - elif self.move_type == "out_refund": - for line_client in line_client_ids: - inv_total = self.amount_sp + self.amount_total - if inv_total: - receivable_line_amount = ( - self.amount_total * line_client.credit - ) / inv_total - else: - receivable_line_amount = 0.0 - line_client.with_context(check_move_validity=False).update( - { - "price_unit": -receivable_line_amount, - "amount_currency": -receivable_line_amount, - "debit": 0.0, - "credit": receivable_line_amount, - } - ) - - def _compute_split_payments(self): - write_off_line_vals = self._build_debit_line() - line_sp = self.line_ids.filtered(lambda l: l.is_split_payment) - if line_sp: - line_sp = line_sp[0].with_context(check_move_validity=False) + @api.model_create_multi + def create(self, vals_list): + lines = super().create(vals_list) + for line in lines: if ( - self.move_type == "out_invoice" - and float_compare( - line_sp.price_unit, - write_off_line_vals["price_unit"], - precision_rounding=self.currency_id.rounding, - ) - != 0 - ): - line_sp.update( - { - "price_unit": 0.0, - "amount_currency": 0.0, - "debit": 0.0, - "credit": 0.0, - } - ) - self.set_receivable_line_ids() - line_sp.write(write_off_line_vals) - elif ( - self.move_type == "out_refund" - and float_compare( - line_sp.price_unit, - write_off_line_vals["price_unit"], - precision_rounding=self.currency_id.rounding, - ) - != 0 + line.display_type == "tax" + and line.move_id.split_payment + and not line.is_split_payment + and not any(ml.is_split_payment for ml in line.move_id.line_ids) ): - line_sp.update( - { - "price_unit": 0.0, - "amount_currency": 0.0, - "debit": 0.0, - "credit": 0.0, - } + write_off_line_vals = line._build_debit_line() + line.move_id.line_ids = [(0, 0, write_off_line_vals)] + line.move_id._sync_dynamic_lines( + container={"records": line.move_id, "self": line.move_id} ) - self.set_receivable_line_ids() - line_sp.write(write_off_line_vals) - else: - self.set_receivable_line_ids() - if self.amount_sp: - self.invoice_line_ids = [(0, 0, write_off_line_vals)] - + return lines -class AccountMoveLine(models.Model): - _inherit = "account.move.line" - - is_split_payment = fields.Boolean(string="Is Split Payment") + def write(self, vals): + res = super().write(vals) + for line in self: + if ( + line.move_id.split_payment + and line.display_type == "tax" + and not line.is_split_payment + ): + write_off_line_vals = line._build_debit_line() + if any(aml.is_split_payment for aml in line.move_id.line_ids): + line_sp = line.move_id.line_ids.filtered( + lambda l: l.is_split_payment + ) + if ( + float_compare( + line_sp[0].price_unit, + write_off_line_vals["price_unit"], + precision_rounding=line.move_id.currency_id.rounding, + ) + != 0 + ): + line_sp[0].write(write_off_line_vals) + line.move_id._sync_dynamic_lines( + container={"records": line.move_id, "self": line.move_id} + ) + else: + if line.move_id.amount_sp: + line.move_id.line_ids = [(0, 0, write_off_line_vals)] + line.move_id._sync_dynamic_lines( + container={"records": line.move_id, "self": line.move_id} + ) + return res diff --git a/l10n_it_split_payment/static/description/index.html b/l10n_it_split_payment/static/description/index.html index 90f5985a87a..b1a29d4c464 100644 --- a/l10n_it_split_payment/static/description/index.html +++ b/l10n_it_split_payment/static/description/index.html @@ -3,7 +3,7 @@ - + ITA - Scissione pagamenti