Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] account_invoice_margin #2982

Draft
wants to merge 3 commits into
base: 14.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions account_invoice_margin/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/avanzosc/odoo-addons/issues>`_.

Credits
=======

Contributors
------------

* Unai Beristain <unaiberistain@avanzosc.es>
* Ana Juaristi <anajuaristi@avanzosc.es>

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 <https://opensource.org/licenses/LGPL-3.0>.
1 change: 1 addition & 0 deletions account_invoice_margin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
15 changes: 15 additions & 0 deletions account_invoice_margin/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 2 additions & 0 deletions account_invoice_margin/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import account_invoice
from . import account_invoice_line
46 changes: 46 additions & 0 deletions account_invoice_margin/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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,
)
total_margin_percent = fields.Float(
string="Total Margin Percent",
compute="_compute_margin_percent",
store=True,
)

@api.depends("invoice_line_ids.subtotal_cost")
def _compute_total_product_cost(self):
for invoice in self:
invoice.total_product_cost = sum(
invoice.invoice_line_ids.mapped("product_cost")
)

@api.depends("invoice_line_ids.subtotal_cost", "invoice_line_ids.price_total")
def _compute_total_margin(self):
for invoice in self:
total_cost = sum(invoice.invoice_line_ids.mapped("product_cost"))
total_sales = sum(invoice.invoice_line_ids.mapped("price_total"))
invoice.total_margin = total_sales - total_cost

@api.depends("total_margin", "invoice_line_ids.price_total")
def _compute_margin_percent(self):
for invoice in self:
total_sales = sum(invoice.invoice_line_ids.mapped("price_total"))
if total_sales != 0:
invoice.total_margin_percent = (
invoice.total_margin / total_sales
) * 100
else:
invoice.total_margin_percent = 0
44 changes: 44 additions & 0 deletions account_invoice_margin/models/account_invoice_line.py
Original file line number Diff line number Diff line change
@@ -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
186 changes: 186 additions & 0 deletions account_invoice_margin/views/account_invoice_line_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<record id="view_invoice_line_form_margin" model="ir.ui.view">
<field name="name">account.move.line.form.margin</field>
<field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_form" />
<field name="arch" type="xml">
<xpath expr="//sheet/group" position="inside">
<field name="product_cost" />
<field name="subtotal_cost" />
<field name="margin" />
<field name="margin_percent" />
</xpath>
</field>
</record>

<record id="view_invoice_tree_margin_new" model="ir.ui.view">
<field name="name">account.move.tree.margin</field>
<field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_tree" />
<field name="arch" type="xml">
<xpath expr="//tree" position="inside">
<field name="product_cost" optional="hide" />
<field name="subtotal_cost" optional="show" />
<field name="margin" optional="show" />
<field name="margin_percent" optional="show" />
</xpath>
</field>
</record>

<record id="view_invoice_line_pivot_margin" model="ir.ui.view">
<field name="name">account.move.line.pivot.margin</field>
<field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_pivot" />
<field name="arch" type="xml">
<xpath expr="//pivot" position="inside">
<field name="product_cost" />
<field name="subtotal_cost" />
<field name="margin" />
<field name="margin_percent" />
</xpath>
</field>
</record>

<record id="view_invoice_filter_margin" model="ir.ui.view">
<field name="name">account.move.line.filter.margin</field>
<field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_account_move_line_filter" />
<field name="arch" type="xml">
<xpath expr="//filter" position="inside">
<field name="product_cost" />
<field name="subtotal_cost" />
<field name="margin" />
<field name="margin_percent" />
</xpath>
</field>
</record>

<record id="view_invoice_line_pivot_margin_inherit" model="ir.ui.view">
<field name="name">account.move.line.pivot.margin.inherit</field>
<field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_pivot" />
<field name="arch" type="xml">
<xpath expr="//pivot" position="inside">
<field name="subtotal_cost" type="measure" widget="float" />
<field name="margin" type="measure" widget="float" />
<field name="margin_percent" type="measure" widget="float" />
</xpath>
</field>
</record>


<!-- CREATE TREE VIEW -->


<record id="view_invoice_tree_margin" model="ir.ui.view">
<field name="name">account.move.line.tree.margin</field>
<field name="model">account.move.line</field>
<field name="inherit_id" ref="account.view_move_line_tree" />
<field name="arch" type="xml">
<xpath expr="//tree" position="inside">
<field name="sequence" widget="handle" />
<field name="move_name" />
<field
name="product_id"
optional="show"
domain="context.get('default_move_type') in ('out_invoice', 'out_refund', 'out_receipt') and [('sale_ok', '=', True), '|', ('company_id', '=', False), ('company_id', '=', parent.company_id)] or [('purchase_ok', '=', True), '|', ('company_id', '=', False), ('company_id', '=', parent.company_id)] "
/>
<field name="name" widget="section_and_note_text" />
<field
name="account_id"
groups="account.group_account_readonly"
options="{'no_create': True}"
domain="[('deprecated', '=', False), ('user_type_id.type', 'not in', ('receivable', 'payable')), ('company_id', '=', parent.company_id), ('is_off_balance', '=', False)]"
attrs="{'required': [('display_type', '=', False)]}"
/>
<field
name="analytic_account_id"
domain="['|', ('company_id', '=', False), ('company_id', '=', parent.company_id)]"
groups="analytic.group_analytic_accounting"
optional="show"
/>
<field
name="analytic_tag_ids"
domain="['|', ('company_id', '=', False), ('company_id', '=', parent.company_id)]"
groups="analytic.group_analytic_tags"
optional="show"
widget="many2many_tags"
/>
<field name="quantity" />
<field name="product_uom_category_id" invisible="1" />
<field
name="product_uom_id"
string="UoM"
groups="uom.group_uom"
optional="show"
/>
<field name="price_unit" string="Price" />
<field name="discount" string="Disc.%" optional="hide" />
<field
name="tax_ids"
widget="many2many_tags"
domain="[('type_tax_use', '=?', parent.invoice_filter_type_domain), ('company_id', '=', parent.company_id)]"
context="{'append_type_to_tax_name': not parent.invoice_filter_type_domain}"
options="{'no_create': True}"
optional="show"
/>
<field
name="price_subtotal"
string="Subtotal"
groups="account.group_show_line_subtotals_tax_excluded"
/>
<field
name="price_total"
string="Total"
groups="account.group_show_line_subtotals_tax_included"
/>

<!-- Others fields -->
<field name="partner_id" invisible="1" />
<field name="amount_currency" invisible="1" />
<field name="currency_id" invisible="1" />
<field name="debit" invisible="1" />
<field name="credit" invisible="1" />
<field name="date" invisible="1" />
<field name="date_maturity" invisible="1" />

<field name="tax_line_id" invisible="1" />
<field name="tax_repartition_line_id" invisible="1" />
<field name="tax_tag_ids" invisible="1" />
<field name="tax_base_amount" invisible="1" />
<field name="tax_exigible" invisible="1" />
<field name="company_id" invisible="1" />
<field name="company_currency_id" invisible="1" />
<field name="recompute_tax_line" invisible="1" force_save="1" />
<field name="display_type" force_save="1" invisible="1" />
<field name="is_rounding_line" invisible="1" />
<field name="exclude_from_invoice_tab" invisible="1" />
<field name="account_internal_type" invisible="1" />
<field name="account_internal_group" invisible="1" />


<field name="product_cost" optional="hide" />
<field name="subtotal_cost" optional="show" />
<field name="margin" optional="show" />
<field name="margin_percent" optional="show" />
</xpath>
</field>
</record>

<record id="action_account_move_line_margin" model="ir.actions.act_window">
<field name="name">Invoice Lines (with Margin)</field>
<field name="res_model">account.move.line</field>
<field name="view_mode">tree</field>
<field name="view_id" ref="view_invoice_tree_margin" />
<field name="target">current</field>
<field name="domain">[]</field>
</record>

<record id="account_move_line_menu_margin" model="ir.ui.menu">
<field name="name">Invoice Lines with Margin</field>
<field name="parent_id" ref="account.menu_finance_entries" />
<field name="action" ref="action_account_move_line_margin" />
<field name="sequence">10</field>
</record>
</odoo>
Loading
Loading