diff --git a/account_invoice_margin/README.rst b/account_invoice_margin/README.rst new file mode 100644 index 0000000000..09d04b5449 --- /dev/null +++ b/account_invoice_margin/README.rst @@ -0,0 +1,75 @@ +.. image:: https://img.shields.io/badge/license-LGPL--3-blue.svg + :target: https://opensource.org/licenses/LGPL-3.0 + :alt: License: LGPL-3 + +=========================================== +Account Invoice Margin +=========================================== + +Overview +======== + +The **Account Invoice Margin** module adds margin calculation fields to invoice lines and invoice totals. It allows businesses to track and analyze the margin on individual invoice lines and the overall invoice margin, as well as the margin percentage. + +Features +======== + +- **Margin Fields on Invoice Lines**: Adds fields for calculating product cost, subtotal cost, margin, and margin percentage on invoice lines. + +- **Margin Calculation on Invoice**: Computes the total margin, total subtotal cost, and average margin percentage at the invoice level. + +Usage +===== + +1. **Install the Module**: + + - Install the **Account Invoice Margin** module via the Apps menu. + +2. **View Margin Information**: + + - On the **Invoice Line** form, you will see the calculated fields for **Product Cost**, **Subtotal Cost**, **Margin**, and **Margin Percent**. + + - The **Invoice Form** will display the **Total Subtotal Cost**, **Total Margin**, and **Average Margin Percent**. + +3. **Analyze Invoice Margins**: + + - Use the **Invoice Pivot** view to analyze the total margin, subtotal cost, and average margin percentage for invoices. + +Configuration +============= + +No additional configuration is required. Simply install the module to start using the margin calculation features. + +Testing +======= + +Test the following scenarios to ensure the module functions as expected: + +- **Test Margin Calculation**: + + - Create an invoice and check that the **Subtotal Cost**, **Margin**, and **Margin Percent** are calculated correctly for each invoice line. + +- **Test Total and Average Margin**: + + - Verify that the **Total Subtotal Cost**, **Total Margin**, and **Average Margin Percent** are correctly calculated at the invoice level. + +Bug Tracker +=========== + +If you encounter any issues, please report them on the GitHub repository at `GitHub Issues `_. + +Credits +======= + +Contributors +------------ + +* Unai Beristain +* Ana Juaristi + +For specific questions regarding this module, please contact the contributors. For support, please use the official issue tracker. + +License +======= + +This project is licensed under the LGPL-3 License. For more details, refer to the LICENSE file or visit . diff --git a/account_invoice_margin/__init__.py b/account_invoice_margin/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/account_invoice_margin/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/account_invoice_margin/__manifest__.py b/account_invoice_margin/__manifest__.py new file mode 100644 index 0000000000..f75f9f44b4 --- /dev/null +++ b/account_invoice_margin/__manifest__.py @@ -0,0 +1,15 @@ +{ + "name": "Account Invoice Margin", + "version": "14.0.1.0.0", + "author": "Avanzosc", + "summary": "Adds margin calculation fields to invoice lines and invoice totals.", + "website": "https://github.com/avanzosc/odoo-addons", + "license": "LGPL-3", + "depends": ["account"], + "data": [ + "views/account_invoice_line_views.xml", + "views/account_invoice_views.xml", + ], + "installable": True, + "application": False, +} diff --git a/account_invoice_margin/models/__init__.py b/account_invoice_margin/models/__init__.py new file mode 100644 index 0000000000..114ef8e753 --- /dev/null +++ b/account_invoice_margin/models/__init__.py @@ -0,0 +1,2 @@ +from . import account_invoice +from . import account_invoice_line diff --git a/account_invoice_margin/models/account_invoice.py b/account_invoice_margin/models/account_invoice.py new file mode 100644 index 0000000000..202be7b579 --- /dev/null +++ b/account_invoice_margin/models/account_invoice.py @@ -0,0 +1,44 @@ +from odoo import api, fields, models + + +class AccountInvoice(models.Model): + _inherit = "account.move" + + total_product_cost = fields.Float( + string="Total Product Cost", + compute="_compute_total_product_cost", + store=True, + ) + total_margin = fields.Float( + string="Total Margin", + compute="_compute_total_margin", + store=True, + ) + average_margin_percent = fields.Float( + string="Average Margin Percent", + compute="_compute_average_margin_percent", + store=True, + ) + + @api.depends("invoice_line_ids.subtotal_cost") + def _compute_total_product_cost(self): + for invoice in self: + invoice.total_subtotal_cost = sum( + invoice.invoice_line_ids.mapped("product_cost") + ) + + @api.depends("invoice_line_ids.margin") + def _compute_total_margin(self): + for invoice in self: + invoice.total_margin = sum(invoice.invoice_line_ids.mapped("margin")) + + @api.depends("invoice_line_ids.margin_percent") + def _compute_average_margin_percent(self): + for invoice in self: + margin_percent_values = invoice.invoice_line_ids.mapped("margin_percent") + if margin_percent_values: + invoice.average_margin_percent = sum(margin_percent_values) / len( + margin_percent_values + ) + else: + invoice.average_margin_percent = 0 diff --git a/account_invoice_margin/models/account_invoice_line.py b/account_invoice_margin/models/account_invoice_line.py new file mode 100644 index 0000000000..d3cf2452e0 --- /dev/null +++ b/account_invoice_margin/models/account_invoice_line.py @@ -0,0 +1,44 @@ +from odoo import api, fields, models + + +class AccountInvoiceLine(models.Model): + _inherit = "account.move.line" + + product_cost = fields.Float( + string="Product Cost", + related="product_id.standard_price", + store=True, + ) + subtotal_cost = fields.Float( + string="Subtotal Cost", + compute="_compute_subtotal_cost", + store=True, + ) + margin = fields.Float( + string="Margin", + compute="_compute_margin", + store=True, + ) + margin_percent = fields.Float( + string="Margin Percent", + compute="_compute_margin_percent", + store=True, + ) + + @api.depends("product_cost", "quantity") + def _compute_subtotal_cost(self): + for line in self: + line.subtotal_cost = line.product_cost * line.quantity + + @api.depends("subtotal_cost", "price_subtotal") + def _compute_margin(self): + for line in self: + line.margin = line.price_subtotal - line.subtotal_cost + + @api.depends("margin", "price_subtotal") + def _compute_margin_percent(self): + for line in self: + if line.price_subtotal != 0: + line.margin_percent = (line.margin / line.price_subtotal) * 100 + else: + line.margin_percent = 0 diff --git a/account_invoice_margin/views/account_invoice_line_views.xml b/account_invoice_margin/views/account_invoice_line_views.xml new file mode 100644 index 0000000000..2b3d3df87a --- /dev/null +++ b/account_invoice_margin/views/account_invoice_line_views.xml @@ -0,0 +1,58 @@ + + + + account.move.line.form.margin + account.move.line + + + + + + + + + + + + + account.move.tree.margin + account.move.line + + + + + + + + + + + + + account.move.line.pivot.margin + account.move.line + + + + + + + + + + + + + account.move.line.filter.margin + account.move.line + + + + + + + + + + + diff --git a/account_invoice_margin/views/account_invoice_views.xml b/account_invoice_margin/views/account_invoice_views.xml new file mode 100644 index 0000000000..eb17a9b6e3 --- /dev/null +++ b/account_invoice_margin/views/account_invoice_views.xml @@ -0,0 +1,67 @@ + + + + account.move.tree.margin + account.move + + + + + + + + + + + + account.move.tree.margin + account.move + + + + + + + + + + + + account.move.kanban.margin + account.move + + + + + + + + + + + + account.move.form.margin + account.move + + + + + + + + + + + + account.move.filter.margin + account.move + + + + + + + + + + diff --git a/setup/account_invoice_margin/odoo/addons/account_invoice_margin b/setup/account_invoice_margin/odoo/addons/account_invoice_margin new file mode 120000 index 0000000000..d31e24ea96 --- /dev/null +++ b/setup/account_invoice_margin/odoo/addons/account_invoice_margin @@ -0,0 +1 @@ +../../../../account_invoice_margin \ No newline at end of file diff --git a/setup/account_invoice_margin/setup.py b/setup/account_invoice_margin/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/account_invoice_margin/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)