diff --git a/account_invoice_sale_attachment/README.rst b/account_invoice_sale_attachment/README.rst new file mode 100644 index 00000000000..07791cbdc5e --- /dev/null +++ b/account_invoice_sale_attachment/README.rst @@ -0,0 +1,95 @@ +=============================== +Account Invoice Sale Attachment +=============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:6338887aa40094db146e7a57cdf37bf46f6f98e562efa611c85e6848ea491e08 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fsale--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/sale-workflow/tree/15.0/account_invoice_sale_attachment + :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-15-0/sale-workflow-15-0-account_invoice_sale_attachment + :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=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows the automatic attachment of related sale order documents when an +invoice is sent. + +This module is especially useful for businesses that require the inclusion of +sale order details when sending invoices, ensuring that customers have all +necessary documentation at their fingertips. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +1. Select a partner. +2. In the Sales/Purchases tab, check the box "Allow Joining Sale Order Documents". + +Usage +===== + +1. Create an invoice linked to one or more sale orders. +2. Click the Send by Email. +3. The system will automatically generate and attach the sale order documents to the invoice email. +4. The email sent to the customer will include the invoice and all attached sale order documents. + +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 +~~~~~~~ + +* ACSONE SA/NV + +Contributors +~~~~~~~~~~~~ + +* Souheil Bejaoui + +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/account_invoice_sale_attachment/__init__.py b/account_invoice_sale_attachment/__init__.py new file mode 100644 index 00000000000..aee8895e7a3 --- /dev/null +++ b/account_invoice_sale_attachment/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizards diff --git a/account_invoice_sale_attachment/__manifest__.py b/account_invoice_sale_attachment/__manifest__.py new file mode 100644 index 00000000000..8a6fda8211f --- /dev/null +++ b/account_invoice_sale_attachment/__manifest__.py @@ -0,0 +1,12 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Account Invoice Sale Attachment", + "version": "15.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/sale-workflow", + "depends": ["sale"], + "data": ["views/res_partner.xml"], +} diff --git a/account_invoice_sale_attachment/models/__init__.py b/account_invoice_sale_attachment/models/__init__.py new file mode 100644 index 00000000000..54dbd3679f8 --- /dev/null +++ b/account_invoice_sale_attachment/models/__init__.py @@ -0,0 +1,3 @@ +from . import res_partner +from . import account_move +from . import sale_order diff --git a/account_invoice_sale_attachment/models/account_move.py b/account_invoice_sale_attachment/models/account_move.py new file mode 100644 index 00000000000..f9838f95786 --- /dev/null +++ b/account_invoice_sale_attachment/models/account_move.py @@ -0,0 +1,21 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class AccountMove(models.Model): + + _inherit = "account.move" + + def action_invoice_sent(self): + res = super().action_invoice_sent() + if not self.partner_id.allow_invoice_sale_order_attachment: + return res + sale_orders = self.invoice_line_ids.sale_line_ids.mapped("order_id") + if not sale_orders: + return res + res["context"][ + "sale_order_attachments" + ] = sale_orders._get_sale_document_attachments().ids + return res diff --git a/account_invoice_sale_attachment/models/res_partner.py b/account_invoice_sale_attachment/models/res_partner.py new file mode 100644 index 00000000000..bd3e1b6ebd8 --- /dev/null +++ b/account_invoice_sale_attachment/models/res_partner.py @@ -0,0 +1,20 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models + + +class ResPartner(models.Model): + + _inherit = "res.partner" + + allow_invoice_sale_order_attachment = fields.Boolean( + string="Allow Joining Sale Order Documents", + default=False, + help="If enabled, sale order documents will be attached to the invoice email.", + ) + + @api.model + def _commercial_fields(self): + res = super()._commercial_fields() + return res + ["allow_invoice_sale_order_attachment"] diff --git a/account_invoice_sale_attachment/models/sale_order.py b/account_invoice_sale_attachment/models/sale_order.py new file mode 100644 index 00000000000..aa5960a9dfc --- /dev/null +++ b/account_invoice_sale_attachment/models/sale_order.py @@ -0,0 +1,44 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import base64 + +from odoo import fields, models + + +class SaleOrder(models.Model): + + _inherit = "sale.order" + + sale_document_attachment = fields.Binary(copy=False, attachment=True, readonly=True) + + def _prepare_sale_document_attachment_vals(self): + pdf_data = self.env.ref("sale.action_report_saleorder")._render_qweb_pdf( + self.ids + )[0] + return { + "name": self.name, + "type": "binary", + "datas": base64.b64encode(pdf_data).decode("utf-8"), + "res_model": self._name, + "res_field": "sale_document_attachment", + "res_id": self.id, + } + + def _create_sale_document_attachment(self): + self.ensure_one() + self.env["ir.attachment"].create( + [rec._prepare_sale_document_attachment_vals() for rec in self] + ) + + def _get_sale_document_attachments(self): + for rec in self: + if not rec.sale_document_attachment: + rec._create_sale_document_attachment() + return self.env["ir.attachment"].search( + [ + ("res_id", "in", self.ids), + ("res_model", "=", self._name), + ("res_field", "=", "sale_document_attachment"), + ] + ) diff --git a/account_invoice_sale_attachment/readme/CONFIGURE.rst b/account_invoice_sale_attachment/readme/CONFIGURE.rst new file mode 100644 index 00000000000..572890888c9 --- /dev/null +++ b/account_invoice_sale_attachment/readme/CONFIGURE.rst @@ -0,0 +1,2 @@ +1. Select a partner. +2. In the Sales/Purchases tab, check the box "Allow Joining Sale Order Documents". diff --git a/account_invoice_sale_attachment/readme/CONTRIBUTORS.rst b/account_invoice_sale_attachment/readme/CONTRIBUTORS.rst new file mode 100644 index 00000000000..50e6298db56 --- /dev/null +++ b/account_invoice_sale_attachment/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Souheil Bejaoui diff --git a/account_invoice_sale_attachment/readme/DESCRIPTION.rst b/account_invoice_sale_attachment/readme/DESCRIPTION.rst new file mode 100644 index 00000000000..5ee4263247b --- /dev/null +++ b/account_invoice_sale_attachment/readme/DESCRIPTION.rst @@ -0,0 +1,6 @@ +This module allows the automatic attachment of related sale order documents when an +invoice is sent. + +This module is especially useful for businesses that require the inclusion of +sale order details when sending invoices, ensuring that customers have all +necessary documentation at their fingertips. diff --git a/account_invoice_sale_attachment/readme/USAGE.rst b/account_invoice_sale_attachment/readme/USAGE.rst new file mode 100644 index 00000000000..6282ccd6fc5 --- /dev/null +++ b/account_invoice_sale_attachment/readme/USAGE.rst @@ -0,0 +1,4 @@ +1. Create an invoice linked to one or more sale orders. +2. Click the Send by Email. +3. The system will automatically generate and attach the sale order documents to the invoice email. +4. The email sent to the customer will include the invoice and all attached sale order documents. diff --git a/account_invoice_sale_attachment/static/description/index.html b/account_invoice_sale_attachment/static/description/index.html new file mode 100644 index 00000000000..5bfac77ba04 --- /dev/null +++ b/account_invoice_sale_attachment/static/description/index.html @@ -0,0 +1,442 @@ + + + + + +Account Invoice Sale Attachment + + + +
+

Account Invoice Sale Attachment

+ + +

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

+

This module allows the automatic attachment of related sale order documents when an +invoice is sent.

+

This module is especially useful for businesses that require the inclusion of +sale order details when sending invoices, ensuring that customers have all +necessary documentation at their fingertips.

+

Table of contents

+ +
+

Configuration

+
    +
  1. Select a partner.
  2. +
  3. In the Sales/Purchases tab, check the box “Allow Joining Sale Order Documents”.
  4. +
+
+
+

Usage

+
    +
  1. Create an invoice linked to one or more sale orders.
  2. +
  3. Click the Send by Email.
  4. +
  5. The system will automatically generate and attach the sale order documents to the invoice email.
  6. +
  7. The email sent to the customer will include the invoice and all attached sale order documents.
  8. +
+
+
+

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

+
    +
  • ACSONE SA/NV
  • +
+
+
+

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.

+

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/account_invoice_sale_attachment/tests/__init__.py b/account_invoice_sale_attachment/tests/__init__.py new file mode 100644 index 00000000000..b305f2b27f9 --- /dev/null +++ b/account_invoice_sale_attachment/tests/__init__.py @@ -0,0 +1 @@ +from . import test_account_invoice_sale_attachment diff --git a/account_invoice_sale_attachment/tests/test_account_invoice_sale_attachment.py b/account_invoice_sale_attachment/tests/test_account_invoice_sale_attachment.py new file mode 100644 index 00000000000..d47dda3ad90 --- /dev/null +++ b/account_invoice_sale_attachment/tests/test_account_invoice_sale_attachment.py @@ -0,0 +1,101 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import Command +from odoo.tests.common import Form, TransactionCase + + +class TestAccountInvoiceSaleAttachment(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, discard_logo_check=True)) + cls.partner = cls.env["res.partner"].create({"name": "Test Partner"}) + cls.product = cls.env.ref("product.product_product_1") + cls.product.invoice_policy = "order" + cls.sale_order = cls.env["sale.order"].create( + { + "partner_id": cls.partner.id, + "state": "sale", + "order_line": [ + Command.create( + { + "product_id": cls.product.id, + "product_uom_qty": 2, + "price_unit": 100, + } + ) + ], + } + ) + cls.sale_order.action_confirm() + cls.invoice = cls.sale_order._create_invoices() + + def _get_sale_document_attachment(self, sale_order): + return self.env["ir.attachment"].search( + [ + ("res_id", "=", sale_order.id), + ("res_model", "=", sale_order._name), + ("res_field", "=", "sale_document_attachment"), + ] + ) + + def _create_invoice_send_wizard_form(self): + action = self.invoice.action_invoice_sent() + return Form( + self.env[action.get("res_model")].with_context(**action.get("context")) + ) + + def test_0(self): + """if the option is not disabled at the partner level, nothing happens""" + self.partner.allow_invoice_sale_order_attachment = False + action = self.invoice.action_invoice_sent() + self.assertFalse(self._get_sale_document_attachment(self.sale_order)) + wizard_form = Form( + self.env[action.get("res_model")].with_context(**action.get("context")) + ) + self.assertEqual(len(wizard_form.attachment_ids), 1) + + def test_1(self): + """if the option is not enabled at the partner level, the document of the + linked sale order will be added to the email send wizard""" + self.partner.allow_invoice_sale_order_attachment = True + wizard_form = self._create_invoice_send_wizard_form() + self.assertTrue(self._get_sale_document_attachment(self.sale_order)) + self.assertEqual(len(wizard_form.attachment_ids), 2) + wizard = wizard_form.save() + self.assertIn( + self._get_sale_document_attachment(self.sale_order), wizard.attachment_ids + ) + + def test_2(self): + """check it works even for multiple orders linked to one invoice""" + self.partner.allow_invoice_sale_order_attachment = True + sale_order_2 = self.sale_order.copy() + sale_order_2.action_confirm() + self.invoice.write( + { + "invoice_line_ids": [ + Command.create( + { + "product_id": self.product.id, + "quantity": 2, + "price_unit": 100, + "sale_line_ids": [Command.link(sale_order_2.order_line.id)], + } + ) + ] + } + ) + self.assertEqual( + len(self.invoice.invoice_line_ids.sale_line_ids.mapped("order_id")), 2 + ) + wizard_form = self._create_invoice_send_wizard_form() + self.assertEqual(len(wizard_form.attachment_ids), 3) + wizard = wizard_form.save() + self.assertIn( + self._get_sale_document_attachment(self.sale_order), wizard.attachment_ids + ) + self.assertIn( + self._get_sale_document_attachment(sale_order_2), wizard.attachment_ids + ) diff --git a/account_invoice_sale_attachment/views/res_partner.xml b/account_invoice_sale_attachment/views/res_partner.xml new file mode 100644 index 00000000000..5bff057bec2 --- /dev/null +++ b/account_invoice_sale_attachment/views/res_partner.xml @@ -0,0 +1,16 @@ + + + + + + res.partner + + + + + + + + + diff --git a/account_invoice_sale_attachment/wizards/__init__.py b/account_invoice_sale_attachment/wizards/__init__.py new file mode 100644 index 00000000000..edaf0442f27 --- /dev/null +++ b/account_invoice_sale_attachment/wizards/__init__.py @@ -0,0 +1 @@ +from . import account_invoice_send diff --git a/account_invoice_sale_attachment/wizards/account_invoice_send.py b/account_invoice_sale_attachment/wizards/account_invoice_send.py new file mode 100644 index 00000000000..1110b76349f --- /dev/null +++ b/account_invoice_sale_attachment/wizards/account_invoice_send.py @@ -0,0 +1,20 @@ +# Copyright 2024 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import api, models + + +class AccountInvoiceSend(models.TransientModel): + + _inherit = "account.invoice.send" + + @api.onchange("template_id") + def onchange_template_id(self): + res = super().onchange_template_id() + if self.env.context.get("sale_order_attachments"): + attachment_ids = self.attachment_ids.ids + self.env.context.get( + "sale_order_attachments" + ) + attachments = self.env["ir.attachment"].browse(attachment_ids) + self.attachment_ids = attachments + return res diff --git a/setup/account_invoice_sale_attachment/odoo/addons/account_invoice_sale_attachment b/setup/account_invoice_sale_attachment/odoo/addons/account_invoice_sale_attachment new file mode 120000 index 00000000000..2915fb56191 --- /dev/null +++ b/setup/account_invoice_sale_attachment/odoo/addons/account_invoice_sale_attachment @@ -0,0 +1 @@ +../../../../account_invoice_sale_attachment \ No newline at end of file diff --git a/setup/account_invoice_sale_attachment/setup.py b/setup/account_invoice_sale_attachment/setup.py new file mode 100644 index 00000000000..28c57bb6403 --- /dev/null +++ b/setup/account_invoice_sale_attachment/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)