diff --git a/pandoc-3.4-1-amd64.deb b/pandoc-3.4-1-amd64.deb new file mode 100644 index 00000000000..35ac940a480 Binary files /dev/null and b/pandoc-3.4-1-amd64.deb differ diff --git a/sale_order_invoice_amount/README.rst b/sale_order_invoice_amount/README.rst new file mode 100644 index 00000000000..d86f895517a --- /dev/null +++ b/sale_order_invoice_amount/README.rst @@ -0,0 +1,80 @@ +========================= +Sale Order Invoice Amount +========================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:adda86ddd28b95f77e0604ae2d8fcc9e48351fb5567a49001111cf3c6edd5488 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/sale-workflow/tree/17.0/sale_order_invoice_amount + :alt: OCA/sale-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/sale-workflow-17-0/sale-workflow-17-0-sale_order_invoice_amount + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/sale-workflow&target_branch=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +The purpose of this module is to add two fields in the sale order model: + +- invoiced_amount: total invoiced amount. +- uninvoiced_amount: total uninvoiced amount. + +**Table of contents** + +.. contents:: + :local: + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* ForgeFlow + +Contributors +------------ + +- Mateu Griful +- Lois Rilo + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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/sale-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/sale_order_invoice_amount/__init__.py b/sale_order_invoice_amount/__init__.py new file mode 100644 index 00000000000..6d58305f5dd --- /dev/null +++ b/sale_order_invoice_amount/__init__.py @@ -0,0 +1,2 @@ +from . import models +from .hooks import pre_init_hook diff --git a/sale_order_invoice_amount/__manifest__.py b/sale_order_invoice_amount/__manifest__.py new file mode 100644 index 00000000000..3ef85320792 --- /dev/null +++ b/sale_order_invoice_amount/__manifest__.py @@ -0,0 +1,26 @@ +# Copyright (C) 2021 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +{ + "name": "Sale Order Invoice Amount", + "version": "17.0.1.0.0", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/sale-workflow", + "category": "Sales", + "license": "LGPL-3", + "summary": "Display the invoiced and uninvoiced total in the sale order", + "depends": [ + "sale", + "account", + ], + "data": [ + "views/sale_order_view.xml", + ], + "installable": True, + "pre_init_hook": "pre_init_hook", + "assets": { + "web.assets_backend": [ + "sale_order_invoice_amount/static/src/xml/*", + ], + }, +} diff --git a/sale_order_invoice_amount/hooks.py b/sale_order_invoice_amount/hooks.py new file mode 100644 index 00000000000..4cc41b63954 --- /dev/null +++ b/sale_order_invoice_amount/hooks.py @@ -0,0 +1,92 @@ +def _add_new_columns(env): + env.cr.execute( + """ +ALTER TABLE + sale_order +ADD COLUMN IF NOT EXISTS + invoiced_amount numeric, +ADD COLUMN IF NOT EXISTS + uninvoiced_amount numeric + """ + ) + + +def _update_amounts_for_cancel_invoices(env): + env.cr.execute( + """ +UPDATE + sale_order +SET + invoiced_amount = 0.0, + uninvoiced_amount = sale_order.amount_total +WHERE + sale_order.state = 'cancel' + """ + ) + + +def _update_amounts_for_non_cancel_invoices(env): + env.cr.execute( + """ +WITH amt AS( + SELECT + sale_order_id, + COALESCE(SUM(amount_total_in_currency_signed), 0) AS invoiced_amount, + CASE + WHEN SUM(amount_total_in_currency_signed) IS NULL + THEN amount_total + WHEN amount_total - SUM( + amount_total_in_currency_signed + ) > 0.0 THEN amount_total - SUM( + amount_total_in_currency_signed) + ELSE 0.0 + END AS uninvoiced_amount + FROM ( + SELECT DISTINCT + sale_order.id AS sale_order_id, + sale_order.amount_total AS amount_total, + account_move.id AS account_move_id, + account_move.amount_total_in_currency_signed + as amount_total_in_currency_signed + FROM + sale_order + LEFT JOIN sale_order_line + ON sale_order_line.order_id = sale_order.id + LEFT JOIN sale_order_line_invoice_rel + ON sale_order_line_invoice_rel.order_line_id = sale_order_line.id + LEFT JOIN account_move_line + ON sale_order_line_invoice_rel.invoice_line_id = account_move_line.id + LEFT JOIN account_move + ON account_move_line.move_id = account_move.id + WHERE + sale_order.state != 'cancel' + AND ( + account_move IS NULL + OR ( + account_move.move_type IN ('out_invoice', 'out_refund') + AND account_move.state != 'cancel' + ) + ) + ) AS distinct_account_move + GROUP BY sale_order_id, amount_total +) +UPDATE sale_order + SET invoiced_amount = amt.invoiced_amount, + uninvoiced_amount = amt.uninvoiced_amount +FROM amt +WHERE sale_order.id = amt.sale_order_id + """ + ) + + +def _update_amounts(env): + _update_amounts_for_cancel_invoices(env) + _update_amounts_for_non_cancel_invoices(env) + + +def pre_init_hook(env): + """ + Add columns to avoid Memory error on an existing Odoo instance with lots of data + """ + _add_new_columns(env) + _update_amounts(env) diff --git a/sale_order_invoice_amount/i18n/es.po b/sale_order_invoice_amount/i18n/es.po new file mode 100644 index 00000000000..3558c1f4522 --- /dev/null +++ b/sale_order_invoice_amount/i18n/es.po @@ -0,0 +1,48 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_order_invoice_amount +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: sale_order_invoice_amount +#. odoo-javascript +#: code:addons/sale_order_invoice_amount/static/src/xml/tax_totals.xml:0 +#: model:ir.model.fields,field_description:sale_order_invoice_amount.field_sale_order__invoiced_amount +#: model_terms:ir.ui.view,arch_db:sale_order_invoice_amount.view_order_tree_invoiced_amount +#, python-format +msgid "Invoiced Amount" +msgstr "Importe facturado" + +#. module: sale_order_invoice_amount +#: model:ir.model.fields,help:sale_order_invoice_amount.field_sale_order__invoiced_amount +msgid "Order amount already invoiced." +msgstr "Importe del pedido ya facturado." + +#. module: sale_order_invoice_amount +#: model:ir.model.fields,help:sale_order_invoice_amount.field_sale_order__uninvoiced_amount +msgid "Order amount to be invoiced" +msgstr "Importe del pedido pendiente de facturar" + +#. module: sale_order_invoice_amount +#: model:ir.model,name:sale_order_invoice_amount.model_sale_order +msgid "Sales Order" +msgstr "Pedidos de venta" + +#. module: sale_order_invoice_amount +#. odoo-javascript +#: code:addons/sale_order_invoice_amount/static/src/xml/tax_totals.xml:0 +#: model:ir.model.fields,field_description:sale_order_invoice_amount.field_sale_order__uninvoiced_amount +#: model_terms:ir.ui.view,arch_db:sale_order_invoice_amount.view_order_tree_invoiced_amount +#, python-format +msgid "Uninvoiced Amount" +msgstr "Importe no facturado" diff --git a/sale_order_invoice_amount/i18n/fr.po b/sale_order_invoice_amount/i18n/fr.po new file mode 100644 index 00000000000..a6a4af8753a --- /dev/null +++ b/sale_order_invoice_amount/i18n/fr.po @@ -0,0 +1,50 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_order_invoice_amount +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-10-28 13:16+0000\n" +"Last-Translator: Nicolas JEUDY \n" +"Language-Team: none\n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n > 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: sale_order_invoice_amount +#. odoo-javascript +#: code:addons/sale_order_invoice_amount/static/src/xml/tax_totals.xml:0 +#: model:ir.model.fields,field_description:sale_order_invoice_amount.field_sale_order__invoiced_amount +#: model_terms:ir.ui.view,arch_db:sale_order_invoice_amount.view_order_tree_invoiced_amount +#, python-format +msgid "Invoiced Amount" +msgstr "Montant facturé" + +#. module: sale_order_invoice_amount +#: model:ir.model.fields,help:sale_order_invoice_amount.field_sale_order__invoiced_amount +msgid "Order amount already invoiced." +msgstr "Montant de commande déjà facturé." + +#. module: sale_order_invoice_amount +#: model:ir.model.fields,help:sale_order_invoice_amount.field_sale_order__uninvoiced_amount +msgid "Order amount to be invoiced" +msgstr "Montant de commande à facturer" + +#. module: sale_order_invoice_amount +#: model:ir.model,name:sale_order_invoice_amount.model_sale_order +msgid "Sales Order" +msgstr "Bon de commande" + +#. module: sale_order_invoice_amount +#. odoo-javascript +#: code:addons/sale_order_invoice_amount/static/src/xml/tax_totals.xml:0 +#: model:ir.model.fields,field_description:sale_order_invoice_amount.field_sale_order__uninvoiced_amount +#: model_terms:ir.ui.view,arch_db:sale_order_invoice_amount.view_order_tree_invoiced_amount +#, python-format +msgid "Uninvoiced Amount" +msgstr "Montant à facturer" diff --git a/sale_order_invoice_amount/i18n/it.po b/sale_order_invoice_amount/i18n/it.po new file mode 100644 index 00000000000..75ee2256b79 --- /dev/null +++ b/sale_order_invoice_amount/i18n/it.po @@ -0,0 +1,50 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_order_invoice_amount +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-12-04 09:34+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: sale_order_invoice_amount +#. odoo-javascript +#: code:addons/sale_order_invoice_amount/static/src/xml/tax_totals.xml:0 +#: model:ir.model.fields,field_description:sale_order_invoice_amount.field_sale_order__invoiced_amount +#: model_terms:ir.ui.view,arch_db:sale_order_invoice_amount.view_order_tree_invoiced_amount +#, python-format +msgid "Invoiced Amount" +msgstr "Importo fatturato" + +#. module: sale_order_invoice_amount +#: model:ir.model.fields,help:sale_order_invoice_amount.field_sale_order__invoiced_amount +msgid "Order amount already invoiced." +msgstr "Importo ordine già fatturato." + +#. module: sale_order_invoice_amount +#: model:ir.model.fields,help:sale_order_invoice_amount.field_sale_order__uninvoiced_amount +msgid "Order amount to be invoiced" +msgstr "Importo ordine da fatturare" + +#. module: sale_order_invoice_amount +#: model:ir.model,name:sale_order_invoice_amount.model_sale_order +msgid "Sales Order" +msgstr "Ordine di vendita" + +#. module: sale_order_invoice_amount +#. odoo-javascript +#: code:addons/sale_order_invoice_amount/static/src/xml/tax_totals.xml:0 +#: model:ir.model.fields,field_description:sale_order_invoice_amount.field_sale_order__uninvoiced_amount +#: model_terms:ir.ui.view,arch_db:sale_order_invoice_amount.view_order_tree_invoiced_amount +#, python-format +msgid "Uninvoiced Amount" +msgstr "Importo non fatturato" diff --git a/sale_order_invoice_amount/i18n/sale_order_invoice_amount.pot b/sale_order_invoice_amount/i18n/sale_order_invoice_amount.pot new file mode 100644 index 00000000000..b97f75a6e4f --- /dev/null +++ b/sale_order_invoice_amount/i18n/sale_order_invoice_amount.pot @@ -0,0 +1,47 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * sale_order_invoice_amount +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: sale_order_invoice_amount +#. odoo-javascript +#: code:addons/sale_order_invoice_amount/static/src/xml/tax_totals.xml:0 +#: model:ir.model.fields,field_description:sale_order_invoice_amount.field_sale_order__invoiced_amount +#: model_terms:ir.ui.view,arch_db:sale_order_invoice_amount.view_order_tree_invoiced_amount +#, python-format +msgid "Invoiced Amount" +msgstr "" + +#. module: sale_order_invoice_amount +#: model:ir.model.fields,help:sale_order_invoice_amount.field_sale_order__invoiced_amount +msgid "Order amount already invoiced." +msgstr "" + +#. module: sale_order_invoice_amount +#: model:ir.model.fields,help:sale_order_invoice_amount.field_sale_order__uninvoiced_amount +msgid "Order amount to be invoiced" +msgstr "" + +#. module: sale_order_invoice_amount +#: model:ir.model,name:sale_order_invoice_amount.model_sale_order +msgid "Sales Order" +msgstr "" + +#. module: sale_order_invoice_amount +#. odoo-javascript +#: code:addons/sale_order_invoice_amount/static/src/xml/tax_totals.xml:0 +#: model:ir.model.fields,field_description:sale_order_invoice_amount.field_sale_order__uninvoiced_amount +#: model_terms:ir.ui.view,arch_db:sale_order_invoice_amount.view_order_tree_invoiced_amount +#, python-format +msgid "Uninvoiced Amount" +msgstr "" diff --git a/sale_order_invoice_amount/models/__init__.py b/sale_order_invoice_amount/models/__init__.py new file mode 100644 index 00000000000..6aacb753131 --- /dev/null +++ b/sale_order_invoice_amount/models/__init__.py @@ -0,0 +1 @@ +from . import sale_order diff --git a/sale_order_invoice_amount/models/sale_order.py b/sale_order_invoice_amount/models/sale_order.py new file mode 100644 index 00000000000..e4e3d09e18c --- /dev/null +++ b/sale_order_invoice_amount/models/sale_order.py @@ -0,0 +1,95 @@ +# Copyright (C) 2021 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) +from odoo import api, fields, models +from odoo.tools.misc import formatLang + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + invoiced_amount = fields.Monetary( + compute="_compute_invoice_amount", + store=True, + help="Order amount already invoiced.", + ) + + uninvoiced_amount = fields.Monetary( + compute="_compute_invoice_amount", + store=True, + help="Order amount to be invoiced", + ) + + @api.depends( + "state", + "invoice_ids", + "invoice_ids.amount_total_in_currency_signed", + "amount_total", + "invoice_ids.state", + ) + def _compute_invoice_amount(self): + for rec in self: + if rec.state != "cancel" and rec.invoice_ids: + rec.invoiced_amount = 0.0 + for invoice in rec.invoice_ids: + if invoice.state != "cancel": + if ( + invoice.currency_id != rec.currency_id + and rec.currency_id != invoice.company_currency_id + ): + rec.invoiced_amount += invoice.currency_id._convert( + invoice.amount_total_signed, + rec.currency_id, + invoice.company_id, + invoice.invoice_date or fields.Date.today(), + ) + else: + rec.invoiced_amount += invoice.amount_total_signed + # Uninvoiced amount could not be equal to total - invoiced amount. + # For example if the amount invoiced does not match with the price unit. + rec.uninvoiced_amount = max( + 0, + sum( + (line.product_uom_qty - line.qty_invoiced) + * (line.price_total / line.product_uom_qty) + for line in rec.order_line.filtered( + lambda sl: sl.product_uom_qty > 0 + ) + ), + ) + else: + rec.invoiced_amount = 0.0 + if rec.state in ["draft", "sent", "cancel"]: + rec.uninvoiced_amount = 0.0 + else: + rec.uninvoiced_amount = rec.amount_total + + @api.depends( + "order_line.tax_id", + "order_line.price_unit", + "amount_total", + "amount_untaxed", + "state", + "invoice_ids", + "invoice_ids.amount_total_in_currency_signed", + "amount_total", + "invoice_ids.state", + ) + def _compute_tax_totals(self): + res = super()._compute_tax_totals() + for order in self: + lang_env = order.with_context(lang=order.partner_id.lang).env + order.tax_totals.update( + { + "invoiced_amount": order.invoiced_amount, + "formatted_invoiced_amount": formatLang( + lang_env, order.invoiced_amount, currency_obj=order.currency_id + ), + "uninvoiced_amount": order.uninvoiced_amount, + "formatted_uninvoiced_amount": formatLang( + lang_env, + order.uninvoiced_amount, + currency_obj=order.currency_id, + ), + } + ) + return res diff --git a/sale_order_invoice_amount/pyproject.toml b/sale_order_invoice_amount/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /dev/null +++ b/sale_order_invoice_amount/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/sale_order_invoice_amount/readme/CONTRIBUTORS.md b/sale_order_invoice_amount/readme/CONTRIBUTORS.md new file mode 100644 index 00000000000..ec3dfeec24a --- /dev/null +++ b/sale_order_invoice_amount/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Mateu Griful \<\> +- Lois Rilo \<\> diff --git a/sale_order_invoice_amount/readme/DESCRIPTION.md b/sale_order_invoice_amount/readme/DESCRIPTION.md new file mode 100644 index 00000000000..944ec1733d1 --- /dev/null +++ b/sale_order_invoice_amount/readme/DESCRIPTION.md @@ -0,0 +1,4 @@ +The purpose of this module is to add two fields in the sale order model: + +- invoiced_amount: total invoiced amount. +- uninvoiced_amount: total uninvoiced amount. diff --git a/sale_order_invoice_amount/static/description/icon.png b/sale_order_invoice_amount/static/description/icon.png new file mode 100644 index 00000000000..3a0328b516c Binary files /dev/null and b/sale_order_invoice_amount/static/description/icon.png differ diff --git a/sale_order_invoice_amount/static/description/index.html b/sale_order_invoice_amount/static/description/index.html new file mode 100644 index 00000000000..bc14afd1b1b --- /dev/null +++ b/sale_order_invoice_amount/static/description/index.html @@ -0,0 +1,428 @@ + + + + + +Sale Order Invoice Amount + + + +
+

Sale Order Invoice Amount

+ + +

Beta License: LGPL-3 OCA/sale-workflow Translate me on Weblate Try me on Runboat

+

The purpose of this module is to add two fields in the sale order model:

+
    +
  • invoiced_amount: total invoiced amount.
  • +
  • uninvoiced_amount: total uninvoiced amount.
  • +
+

Table of contents

+ +
+

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 to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ForgeFlow
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

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/sale-workflow project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/sale_order_invoice_amount/static/src/xml/tax_totals.xml b/sale_order_invoice_amount/static/src/xml/tax_totals.xml new file mode 100644 index 00000000000..ed40a34d20b --- /dev/null +++ b/sale_order_invoice_amount/static/src/xml/tax_totals.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sale_order_invoice_amount/tests/__init__.py b/sale_order_invoice_amount/tests/__init__.py new file mode 100644 index 00000000000..15963146727 --- /dev/null +++ b/sale_order_invoice_amount/tests/__init__.py @@ -0,0 +1 @@ +from . import test_sale_order_invoice_amount diff --git a/sale_order_invoice_amount/tests/test_sale_order_invoice_amount.py b/sale_order_invoice_amount/tests/test_sale_order_invoice_amount.py new file mode 100644 index 00000000000..6996c8496a6 --- /dev/null +++ b/sale_order_invoice_amount/tests/test_sale_order_invoice_amount.py @@ -0,0 +1,401 @@ +# Copyright (C) 2021 ForgeFlow S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo import fields +from odoo.tests import common, tagged + + +@tagged("post_install", "-at_install") +class TestSaleOrderInvoiceAmount(common.TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + + # Partners + partner_model = cls.env["res.partner"] + cls.res_partner_1 = partner_model.create({"name": "Wood Corner"}) + cls.res_partner_address_1 = partner_model.create( + {"name": "Willie Burke", "parent_id": cls.res_partner_1.id} + ) + cls.res_partner_2 = partner_model.create({"name": "Partner 12"}) + # Products + product_model = cls.env["product.product"] + cls.product_1 = product_model.create( + {"name": "Desk Combination", "type": "consu"} + ) + cls.product_2 = product_model.create( + {"name": "Conference Chair", "type": "consu"} + ) + cls.product_3 = product_model.create( + {"name": "Repair Services", "type": "service"} + ) + cls.currency_eur = cls.env.ref("base.EUR") + cls.currency_cad = cls.env.ref("base.CAD") + cls.currency_cad.active = True + cls.currency_eur.active = True + + cls.res_partner_2 = cls.env["res.partner"].create({"name": "Partner 12"}) + # Sale Order + cls.tax = cls.env["account.tax"].create( + {"name": "Tax 15", "type_tax_use": "sale", "amount": 21} + ) + cls.sale_order_1 = cls.env["sale.order"].create( + {"partner_id": cls.res_partner_1.id} + ) + sale_order_line_model = cls.env["sale.order.line"] + cls.order_line_1 = sale_order_line_model.create( + { + "order_id": cls.sale_order_1.id, + "product_id": cls.product_1.id, + "product_uom": cls.product_1.uom_id.id, + "product_uom_qty": 10.0, + "price_unit": 10.0, + "tax_id": cls.tax, + } + ) + cls.order_line_2 = sale_order_line_model.create( + { + "order_id": cls.sale_order_1.id, + "product_id": cls.product_2.id, + "product_uom": cls.product_2.uom_id.id, + "product_uom_qty": 25.0, + "price_unit": 4.0, + "tax_id": cls.tax, + } + ) + cls.order_line_3 = sale_order_line_model.create( + { + "order_id": cls.sale_order_1.id, + "product_id": cls.product_3.id, + "product_uom": cls.product_3.uom_id.id, + "product_uom_qty": 20.0, + "price_unit": 5.0, + "tax_id": cls.tax, + } + ) + + def test_01_sale_order_invoiced_amount(self): + self.assertEqual( + self.sale_order_1.invoiced_amount, + 0.0, + "Invoiced Amount should be 0.0", + ) + + self.sale_order_1.action_confirm() + aml1 = self.order_line_1._prepare_invoice_line() + aml2 = self.order_line_2._prepare_invoice_line() + test_invoice = self.env["account.move"].create( + { + "move_type": "out_invoice", + "invoice_date": fields.Date.from_string("2024-01-01"), + "date": fields.Date.from_string("2024-01-01"), + "partner_id": self.res_partner_1.id, + "line_ids": [ + ( + 0, + 0, + aml1, + ), + ( + 0, + 0, + aml2, + ), + ], + } + ) + test_invoice.action_post() + self.assertEqual( + self.sale_order_1.invoiced_amount, + 242.0, + "Invoiced Amount should be 242", + ) + self.assertEqual( + self.sale_order_1.uninvoiced_amount, + 121.0, + "Uninvoiced Amount should be 121, as the lines keep uninvoiced.", + ) + test_invoice.button_cancel() + self.sale_order_1._create_invoices(final=True) + self.assertEqual( + self.sale_order_1.invoiced_amount, + 363.0, + "Invoiced Amount should be calculated.", + ) + self.assertEqual( + self.sale_order_1.uninvoiced_amount, + 0.0, + "Uninvoiced Amount should be calculated.", + ) + tax_totals = self.sale_order_1.tax_totals + self.assertEqual( + tax_totals["invoiced_amount"], + 363.0, + ) + self.assertEqual( + tax_totals["uninvoiced_amount"], + 0.0, + ) + self.assertEqual( + tax_totals["formatted_invoiced_amount"], + "$\xa0363.00", + ) + self.assertEqual( + tax_totals["formatted_uninvoiced_amount"], + "$\xa00.00", + ) + + def test_02_sale_order_invoiced_amount_different_currencies_invoice(self): + self.assertEqual( + self.sale_order_1.invoiced_amount, + 0.0, + "Invoiced Amount should be 0.0", + ) + self.sale_order_1.action_confirm() + price_foreign_currency_1 = self.sale_order_1.currency_id._convert( + 10.0, + self.currency_eur, + self.sale_order_1.company_id, + fields.Date.from_string("2024-01-01"), + ) + price_foreign_currency_2 = self.sale_order_1.currency_id._convert( + 4.0, + self.currency_eur, + self.sale_order_1.company_id, + fields.Date.from_string("2024-01-01"), + ) + aml1 = self.order_line_1._prepare_invoice_line( + **{ + "price_unit": price_foreign_currency_1, + "currency_id": self.currency_eur.id, + } + ) + aml2 = self.order_line_2._prepare_invoice_line( + **{ + "price_unit": price_foreign_currency_2, + "currency_id": self.currency_eur.id, + } + ) + test_invoice = self.env["account.move"].create( + { + "move_type": "out_invoice", + "invoice_date": fields.Date.from_string("2024-01-01"), + "date": fields.Date.from_string("2024-01-01"), + "partner_id": self.res_partner_1.id, + "line_ids": [ + ( + 0, + 0, + aml1, + ), + ( + 0, + 0, + aml2, + ), + ], + "currency_id": self.currency_eur.id, + } + ) + test_invoice.action_post() + self.assertAlmostEqual( + self.sale_order_1.invoiced_amount, + 242.0, + delta=1, + ) + self.assertEqual( + self.sale_order_1.uninvoiced_amount, + 121.0, + "Uninvoiced Amount should be 121, as the lines keep uninvoiced.", + ) + test_invoice.button_cancel() + self.sale_order_1._create_invoices(final=True) + self.assertEqual( + self.sale_order_1.invoiced_amount, + 363.0, + "Invoiced Amount should be calculated.", + ) + self.assertEqual( + self.sale_order_1.uninvoiced_amount, + 0.0, + "Uninvoiced Amount should be calculated.", + ) + + def test_03_sale_order_invoiced_amount_different_currencies_sale(self): + self.currency_cad.active = True + self.sale_order_1 = self.env["sale.order"].create( + {"partner_id": self.res_partner_1.id, "currency_id": self.currency_eur.id} + ) + self.order_line_1 = self.env["sale.order.line"].create( + { + "order_id": self.sale_order_1.id, + "product_id": self.product_1.id, + "product_uom": self.product_1.uom_id.id, + "product_uom_qty": 10.0, + "price_unit": 10.0, + "tax_id": self.tax, + "currency_id": self.currency_eur.id, + } + ) + self.order_line_2 = self.env["sale.order.line"].create( + { + "order_id": self.sale_order_1.id, + "product_id": self.product_2.id, + "product_uom": self.product_2.uom_id.id, + "product_uom_qty": 25.0, + "price_unit": 4.0, + "tax_id": self.tax, + "currency_id": self.currency_eur.id, + } + ) + self.order_line_3 = self.env["sale.order.line"].create( + { + "order_id": self.sale_order_1.id, + "product_id": self.product_3.id, + "product_uom": self.product_3.uom_id.id, + "product_uom_qty": 20.0, + "price_unit": 5.0, + "tax_id": self.tax, + "currency_id": self.currency_eur.id, + } + ) + + self.assertEqual( + self.sale_order_1.invoiced_amount, + 0.0, + "Invoiced Amount should be 0.0", + ) + self.sale_order_1.action_confirm() + self.sale_order_1.currency_id = self.currency_eur + price_foreign_currency_1 = self.sale_order_1.currency_id._convert( + 10.0, + self.currency_cad, + self.sale_order_1.company_id, + fields.Date.from_string("2024-01-01"), + ) + price_foreign_currency_2 = self.sale_order_1.currency_id._convert( + 4.0, + self.currency_cad, + self.sale_order_1.company_id, + fields.Date.from_string("2024-01-01"), + ) + aml1 = self.order_line_1._prepare_invoice_line( + **{ + "price_unit": price_foreign_currency_1, + "currency_id": self.currency_cad.id, + } + ) + aml2 = self.order_line_2._prepare_invoice_line( + **{ + "price_unit": price_foreign_currency_2, + "currency_id": self.currency_cad.id, + } + ) + test_invoice = self.env["account.move"].create( + { + "move_type": "out_invoice", + "invoice_date": fields.Date.from_string("2024-01-01"), + "date": fields.Date.from_string("2024-01-01"), + "partner_id": self.res_partner_1.id, + "line_ids": [ + ( + 0, + 0, + aml1, + ), + ( + 0, + 0, + aml2, + ), + ], + "currency_id": self.currency_cad.id, + } + ) + test_invoice.action_post() + self.assertAlmostEqual( + self.sale_order_1.invoiced_amount, + 242.0, + delta=70, + ) + self.assertEqual( + self.sale_order_1.uninvoiced_amount, + 121.0, + "Uninvoiced Amount should be 121, as the lines keep uninvoiced.", + ) + test_invoice.button_cancel() + price_foreign_currency_1 = self.sale_order_1.currency_id._convert( + 10.0, + self.currency_cad, + self.sale_order_1.company_id, + fields.Date.from_string("2024-01-01"), + ) + price_foreign_currency_2 = self.sale_order_1.currency_id._convert( + 4.0, + self.currency_cad, + self.sale_order_1.company_id, + fields.Date.from_string("2024-01-01"), + ) + price_foreign_currency_3 = self.sale_order_1.currency_id._convert( + 5.0, + self.currency_cad, + self.sale_order_1.company_id, + fields.Date.from_string("2024-01-01"), + ) + aml1 = self.order_line_1._prepare_invoice_line( + **{ + "price_unit": price_foreign_currency_1, + "currency_id": self.currency_cad.id, + } + ) + aml2 = self.order_line_2._prepare_invoice_line( + **{ + "price_unit": price_foreign_currency_2, + "currency_id": self.currency_cad.id, + } + ) + aml3 = self.order_line_3._prepare_invoice_line( + **{ + "price_unit": price_foreign_currency_3, + "currency_id": self.currency_cad.id, + } + ) + test_invoice = self.env["account.move"].create( + [ + { + "move_type": "out_invoice", + "invoice_date": fields.Date.from_string("2024-01-01"), + "date": fields.Date.from_string("2024-01-01"), + "partner_id": self.res_partner_1.id, + "line_ids": [ + ( + 0, + 0, + aml1, + ), + ( + 0, + 0, + aml2, + ), + ( + 0, + 0, + aml3, + ), + ], + "currency_id": self.currency_cad.id, + } + ] + ) + test_invoice.action_post() + self.assertAlmostEqual( + self.sale_order_1.invoiced_amount, + 363.0, + delta=100, + ) + self.assertEqual( + self.sale_order_1.uninvoiced_amount, + 0.0, + "Uninvoiced Amount should be calculated.", + ) diff --git a/sale_order_invoice_amount/views/sale_order_view.xml b/sale_order_invoice_amount/views/sale_order_view.xml new file mode 100644 index 00000000000..6be26d7335e --- /dev/null +++ b/sale_order_invoice_amount/views/sale_order_view.xml @@ -0,0 +1,18 @@ + + + + sale.order.tree.invoiced_amount + sale.order + + + + + + + + +