diff --git a/account_move_order_partner/README.rst b/account_move_order_partner/README.rst new file mode 100644 index 00000000..4163a3a4 --- /dev/null +++ b/account_move_order_partner/README.rst @@ -0,0 +1,108 @@ +========================== +Account Move Order Partner +========================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:4c8bc67b9ae34cc79ec93ccac1970fe0244fee5a288405a3f4cc964b08dd0c7e + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github + :target: https://github.com/OCA/account-invoicing/tree/18.0/account_move_order_partner + :alt: OCA/account-invoicing +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/account-invoicing-18-0/account-invoicing-18-0-account_move_order_partner + :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/account-invoicing&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Adds the order partner (sold-to partner) to invoices and prints it on +the report. If multiple partners are involved, the sale partner defaults +to the invoice partner. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To make sure each invoice is linked to a single order partner: + +1. Navigate to Sales ▸ Configuration ▸ Settings. +2. Enable the option Group invoices by order partner. + +When enabled, invoices will only be grouped if the order partner is the +same across all sale orders. Even if the invoice partner is the same, +sale orders with different order partners will result in separate +invoices. + +If you'd like to control the grouping behavior per invoice partner, +consider disabling this configuration and installing the +sale_order_invoicing_grouping_criteria module. + +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 +------- + +* Quartile + +Contributors +------------ + +- Quartile + + - Aung Ko Ko Lin + +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. + +.. |maintainer-yostashiro| image:: https://github.com/yostashiro.png?size=40px + :target: https://github.com/yostashiro + :alt: yostashiro +.. |maintainer-aungkokolin1997| image:: https://github.com/aungkokolin1997.png?size=40px + :target: https://github.com/aungkokolin1997 + :alt: aungkokolin1997 + +Current `maintainers `__: + +|maintainer-yostashiro| |maintainer-aungkokolin1997| + +This module is part of the `OCA/account-invoicing `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/account_move_order_partner/__init__.py b/account_move_order_partner/__init__.py new file mode 100644 index 00000000..6d58305f --- /dev/null +++ b/account_move_order_partner/__init__.py @@ -0,0 +1,2 @@ +from . import models +from .hooks import pre_init_hook diff --git a/account_move_order_partner/__manifest__.py b/account_move_order_partner/__manifest__.py new file mode 100644 index 00000000..476756b3 --- /dev/null +++ b/account_move_order_partner/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Account Move Order Partner", + "category": "Invoice", + "version": "18.0.1.0.0", + "author": "Quartile, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/account-invoicing", + "license": "AGPL-3", + "depends": ["sale"], + "data": [ + "reports/report_invoice_document.xml", + "views/account_move_views.xml", + "views/res_config_settings_views.xml", + ], + "pre_init_hook": "pre_init_hook", + "maintainers": ["yostashiro", "aungkokolin1997"], + "installable": True, +} diff --git a/account_move_order_partner/hooks.py b/account_move_order_partner/hooks.py new file mode 100644 index 00000000..7aef9dae --- /dev/null +++ b/account_move_order_partner/hooks.py @@ -0,0 +1,45 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tools.sql import column_exists + + +def pre_init_hook(env): + if not column_exists(env.cr, "account_move", "order_partner_id"): + env.cr.execute( + """ + ALTER TABLE account_move + ADD COLUMN order_partner_id INTEGER + REFERENCES res_partner(id) + ON DELETE SET NULL; + """ + ) + env.cr.execute( + """ + WITH spc AS ( + SELECT + am.id AS move_id, + COUNT(DISTINCT sol.order_partner_id) AS cnt, + MIN(sol.order_partner_id) AS single_partner_id + FROM account_move am + LEFT JOIN account_move_line aml + ON aml.move_id = am.id + LEFT JOIN sale_order_line_invoice_rel rel + ON rel.invoice_line_id = aml.id + LEFT JOIN sale_order_line sol + ON sol.id = rel.order_line_id + WHERE am.move_type IN ('out_invoice', 'out_refund') + GROUP BY am.id + ) + UPDATE account_move am + SET order_partner_id = CASE + WHEN spc.cnt = 1 AND spc.single_partner_id IS NOT NULL + THEN spc.single_partner_id + ELSE am.partner_id + END + FROM spc + WHERE am.id = spc.move_id + AND am.move_type IN ('out_invoice', 'out_refund') + AND am.order_partner_id IS NULL; + """ + ) diff --git a/account_move_order_partner/models/__init__.py b/account_move_order_partner/models/__init__.py new file mode 100644 index 00000000..be074503 --- /dev/null +++ b/account_move_order_partner/models/__init__.py @@ -0,0 +1,4 @@ +from . import account_move +from . import res_company +from . import res_config_settings +from . import sale_order diff --git a/account_move_order_partner/models/account_move.py b/account_move_order_partner/models/account_move.py new file mode 100644 index 00000000..41cdd31e --- /dev/null +++ b/account_move_order_partner/models/account_move.py @@ -0,0 +1,28 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class AccountMove(models.Model): + _inherit = "account.move" + + order_partner_id = fields.Many2one( + "res.partner", + string="Sold-to Partner", + compute="_compute_order_partner_id", + store=True, + ) + + @api.depends( + "move_type", "partner_id", "invoice_line_ids.sale_line_ids.order_partner_id" + ) + def _compute_order_partner_id(self): + for move in self: + move.order_partner_id = move.partner_id + sale_partners = move.move_type in [ + "out_invoice", + "out_refund", + ] and move.invoice_line_ids.mapped("sale_line_ids.order_partner_id") + if sale_partners and len(sale_partners) == 1: + move.order_partner_id = sale_partners.id diff --git a/account_move_order_partner/models/res_company.py b/account_move_order_partner/models/res_company.py new file mode 100644 index 00000000..1ecce9f1 --- /dev/null +++ b/account_move_order_partner/models/res_company.py @@ -0,0 +1,10 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = "res.company" + + invoice_group_by_order_partner = fields.Boolean() diff --git a/account_move_order_partner/models/res_config_settings.py b/account_move_order_partner/models/res_config_settings.py new file mode 100644 index 00000000..0d7fb392 --- /dev/null +++ b/account_move_order_partner/models/res_config_settings.py @@ -0,0 +1,12 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + invoice_group_by_order_partner = fields.Boolean( + related="company_id.invoice_group_by_order_partner", readonly=False + ) diff --git a/account_move_order_partner/models/sale_order.py b/account_move_order_partner/models/sale_order.py new file mode 100644 index 00000000..ae6a0f8f --- /dev/null +++ b/account_move_order_partner/models/sale_order.py @@ -0,0 +1,20 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + def _prepare_invoice(self): + vals = super()._prepare_invoice() + # Set for use in _get_invoice_grouping_keys + vals["order_partner_id"] = self.partner_id.id + return vals + + def _get_invoice_grouping_keys(self): + keys = super()._get_invoice_grouping_keys() + if self.env.company.invoice_group_by_order_partner: + keys.append("order_partner_id") + return keys diff --git a/account_move_order_partner/pyproject.toml b/account_move_order_partner/pyproject.toml new file mode 100644 index 00000000..4231d0cc --- /dev/null +++ b/account_move_order_partner/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/account_move_order_partner/readme/CONFIGURE.md b/account_move_order_partner/readme/CONFIGURE.md new file mode 100644 index 00000000..56d1860e --- /dev/null +++ b/account_move_order_partner/readme/CONFIGURE.md @@ -0,0 +1,11 @@ +To make sure each invoice is linked to a single order partner: + +1. Navigate to Sales ▸ Configuration ▸ Settings. +2. Enable the option Group invoices by order partner. + +When enabled, invoices will only be grouped if the order partner is the same across all sale +orders. Even if the invoice partner is the same, sale orders with different order partners will +result in separate invoices. + +If you'd like to control the grouping behavior per invoice partner, consider disabling this +configuration and installing the sale_order_invoicing_grouping_criteria module. diff --git a/account_move_order_partner/readme/CONTRIBUTORS.md b/account_move_order_partner/readme/CONTRIBUTORS.md new file mode 100644 index 00000000..a8a2dfb2 --- /dev/null +++ b/account_move_order_partner/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- Quartile \<\> + - Aung Ko Ko Lin diff --git a/account_move_order_partner/readme/DESCRIPTION.md b/account_move_order_partner/readme/DESCRIPTION.md new file mode 100644 index 00000000..190974d4 --- /dev/null +++ b/account_move_order_partner/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +Adds the order partner (sold-to partner) to invoices and prints it on the report. If +multiple partners are involved, the sale partner defaults to the invoice partner. diff --git a/account_move_order_partner/reports/report_invoice_document.xml b/account_move_order_partner/reports/report_invoice_document.xml new file mode 100644 index 00000000..f9207cb8 --- /dev/null +++ b/account_move_order_partner/reports/report_invoice_document.xml @@ -0,0 +1,46 @@ + + + + diff --git a/account_move_order_partner/static/description/index.html b/account_move_order_partner/static/description/index.html new file mode 100644 index 00000000..250aa506 --- /dev/null +++ b/account_move_order_partner/static/description/index.html @@ -0,0 +1,446 @@ + + + + + +Account Move Order Partner + + + +
+

Account Move Order Partner

+ + +

Beta License: AGPL-3 OCA/account-invoicing Translate me on Weblate Try me on Runboat

+

Adds the order partner (sold-to partner) to invoices and prints it on +the report. If multiple partners are involved, the sale partner defaults +to the invoice partner.

+

Table of contents

+ +
+

Configuration

+

To make sure each invoice is linked to a single order partner:

+
    +
  1. Navigate to Sales ▸ Configuration ▸ Settings.
  2. +
  3. Enable the option Group invoices by order partner.
  4. +
+

When enabled, invoices will only be grouped if the order partner is the +same across all sale orders. Even if the invoice partner is the same, +sale orders with different order partners will result in separate +invoices.

+

If you’d like to control the grouping behavior per invoice partner, +consider disabling this configuration and installing the +sale_order_invoicing_grouping_criteria module.

+
+
+

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

+
    +
  • Quartile
  • +
+
+
+

Contributors

+ +
+
+

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.

+

Current maintainers:

+

yostashiro aungkokolin1997

+

This module is part of the OCA/account-invoicing project on GitHub.

+

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

+
+
+
+ + diff --git a/account_move_order_partner/tests/__init__.py b/account_move_order_partner/tests/__init__.py new file mode 100644 index 00000000..34f5488f --- /dev/null +++ b/account_move_order_partner/tests/__init__.py @@ -0,0 +1 @@ +from . import test_account_move_order_partner diff --git a/account_move_order_partner/tests/test_account_move_order_partner.py b/account_move_order_partner/tests/test_account_move_order_partner.py new file mode 100644 index 00000000..2979cc4c --- /dev/null +++ b/account_move_order_partner/tests/test_account_move_order_partner.py @@ -0,0 +1,64 @@ +# Copyright 2025 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import Command +from odoo.tests.common import TransactionCase + + +class TestAccountMoveOrderPartner(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner1 = cls.env["res.partner"].create({"name": "Customer A"}) + cls.partner2 = cls.env["res.partner"].create({"name": "Customer B"}) + cls.invoice_partner = cls.env["res.partner"].create({"name": "Invoice Partner"}) + cls.product = cls.env["product.product"].create({"name": "Test Product"}) + cls.env.company.invoice_group_by_order_partner = True + + def _create_sale_order(self, customer, invoice_partner): + order = self.env["sale.order"].create( + { + "partner_id": customer.id, + "partner_invoice_id": invoice_partner.id, + "order_line": [ + Command.create( + { + "product_id": self.product.id, + "product_uom_qty": 1, + "price_unit": 100.0, + } + ) + ], + } + ) + order.action_confirm() + return order + + def test_single_sale_order_invoice(self): + so = self._create_sale_order(self.partner1, self.invoice_partner) + so._create_invoices() + self.assertEqual(len(so.invoice_ids), 1) + self.assertEqual(so.invoice_ids.order_partner_id, self.partner1) + + def test_two_sales_different_customer_same_invoice_partner(self): + so1 = self._create_sale_order(self.partner1, self.invoice_partner) + so2 = self._create_sale_order(self.partner2, self.invoice_partner) + (so1 + so2)._create_invoices() + self.assertEqual(so1.invoice_ids.order_partner_id, self.partner1) + self.assertEqual(so2.invoice_ids.order_partner_id, self.partner2) + + def test_two_sales_same_customer_and_invoice_partner(self): + so1 = self._create_sale_order(self.partner1, self.invoice_partner) + so2 = self._create_sale_order(self.partner1, self.invoice_partner) + (so1 + so2)._create_invoices() + self.assertEqual(so1.invoice_ids.order_partner_id, self.partner1) + self.assertEqual(so2.invoice_ids.order_partner_id, self.partner1) + self.assertEqual(so1.invoice_ids, so2.invoice_ids) + + def test_invoice_group_by_order_partner(self): + self.env.company.invoice_group_by_order_partner = False + so1 = self._create_sale_order(self.partner1, self.invoice_partner) + so2 = self._create_sale_order(self.partner2, self.invoice_partner) + (so1 + so2)._create_invoices() + self.assertEqual(so1.invoice_ids, so2.invoice_ids) + self.assertEqual(so1.invoice_ids.order_partner_id, self.invoice_partner) diff --git a/account_move_order_partner/views/account_move_views.xml b/account_move_order_partner/views/account_move_views.xml new file mode 100644 index 00000000..c0e0a44b --- /dev/null +++ b/account_move_order_partner/views/account_move_views.xml @@ -0,0 +1,39 @@ + + + + account.invoice.filter + account.move + + + + + + + + + + + + account.move.form + account.move + + + + + + + + diff --git a/account_move_order_partner/views/res_config_settings_views.xml b/account_move_order_partner/views/res_config_settings_views.xml new file mode 100644 index 00000000..8e60c108 --- /dev/null +++ b/account_move_order_partner/views/res_config_settings_views.xml @@ -0,0 +1,19 @@ + + + + res.config.settings + res.config.settings + + + + + + + + + +