Merge branch 'mig/17.0/account_invoice_margin' into '17.0'

WIP: mig/17.0/account_invoice_margin into 17.0

See merge request hibou-io/hibou-odoo/suite!1686
This commit is contained in:
Cedric Collins
2023-11-03 20:51:06 +00:00
9 changed files with 259 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
******************************
Hibou - Account Invoice Margin
******************************
Include a margin calculation on invoices.
For more information and add-ons, visit `Hibou.io <https://hibou.io/docs/hibou-odoo-suite-1/invoice-margin-156>`_.
=============
Main Features
=============
* Adds computed field `margin` to invoices to give the profitability by calculating the difference between the Unit Price and the Cost.
.. image:: https://user-images.githubusercontent.com/15882954/45578631-880c0000-b837-11e8-9c4d-d2f15c3c0592.png
:alt: 'Customer Invoice'
:width: 988
:align: left
=======
License
=======
Please see `LICENSE <https://github.com/hibou-io/hibou-odoo-suite/blob/16.0/LICENSE>`_.
Copyright Hibou Corp. 2023

View File

@@ -0,0 +1 @@
from . import models

View File

@@ -0,0 +1,24 @@
{
'name': 'Invoice Margin',
'version': '17.0.1.1.0',
'author': 'Hibou Corp.',
'license': 'AGPL-3',
'category': 'Accounting',
'sequence': 95,
'summary': 'Invoices include margin calculation.',
'description': """
Invoices include margin calculation.
If the invoice line comes from a sale order line, the cost will come
from the sale order line.
""",
'website': 'https://hibou.io/',
'depends': [
'account',
'sale_margin',
],
'data': [
'views/account_invoice_views.xml',
],
'installable': True,
'application': False,
}

View File

@@ -0,0 +1,48 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * account_invoice_margin
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-10-11 16:27+0000\n"
"PO-Revision-Date: 2021-10-11 16:27+0000\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: account_invoice_margin
#: model:ir.model.fields,field_description:account_invoice_margin.field_account_move_line__purchase_price
msgid "Cost"
msgstr "Costo"
#. module: account_invoice_margin
#: model:ir.model.fields,help:account_invoice_margin.field_account_bank_statement_line__margin
#: model:ir.model.fields,help:account_invoice_margin.field_account_move__margin
#: model:ir.model.fields,help:account_invoice_margin.field_account_payment__margin
msgid ""
"It gives profitability by calculating the difference between the Unit Price"
"and the cost."
msgstr "Da la rentabilidad calculando la diferencia entre el precio unitario y costo del producto"
#. module: account_invoice_margin
#: model:ir.model,name:account_invoice_margin.model_account_move
msgid "Journal Entry"
msgstr "Asiento contable"
#. module: account_invoice_margin
#: model:ir.model,name:account_invoice_margin.model_account_move_line
msgid "Journal Item"
msgstr "Apunte contable"
#. module: account_invoice_margin
#: model:ir.model.fields,field_description:account_invoice_margin.field_account_bank_statement_line__margin
#: model:ir.model.fields,field_description:account_invoice_margin.field_account_move__margin
#: model:ir.model.fields,field_description:account_invoice_margin.field_account_move_line__margin
#: model:ir.model.fields,field_description:account_invoice_margin.field_account_payment__margin
msgid "Margin"
msgstr "Margen"

View File

@@ -0,0 +1 @@
from . import account_invoice

View File

@@ -0,0 +1,74 @@
from odoo import api, fields, models
class AccountMoveLine(models.Model):
_inherit = "account.move.line"
margin = fields.Monetary(compute='_compute_product_margin', digits='Product Price', store=True,
string='Margin', groups='base.group_user')
margin_percent = fields.Float(compute='_compute_product_margin', store=True, string='Margin (%)',
groups='base.group_user')
purchase_price = fields.Monetary(string='Cost', digits='Product Price',
groups='base.group_user')
# Note we are keeping this API because it is easy to customize and extend the purchase price/margin calculation
def _compute_margin(self, move, product, product_uom, sale_lines):
# if sale_line_ids and don't re-browse
for line in sale_lines:
return line.purchase_price
frm_cur = move.company_currency_id
to_cur = move.currency_id
purchase_price = product.standard_price
if product_uom and product_uom != product.uom_id:
purchase_price = product.uom_id._compute_price(purchase_price, product_uom)
ctx = self.env.context.copy()
ctx['date'] = move.date if move.date else fields.Date.context_today(move)
price = frm_cur.with_context(ctx)._convert(purchase_price, to_cur, move.company_id, ctx['date'], round=False)
return price
@api.onchange('product_id', 'product_uom_id')
def product_id_change_margin(self):
for line in self:
if not line.product_id:
line.purchase_price = 0.0
else:
line.purchase_price = line._compute_margin(line.move_id, line.product_id, line.product_uom_id, line.sale_line_ids)
@api.model_create_multi
def create(self, vals):
lines = super(AccountMoveLine, self).create(vals)
if vals and 'purchase_price' not in vals[0]:
lines.product_id_change_margin()
return lines
@api.depends('product_id', 'purchase_price', 'quantity', 'price_unit', 'price_subtotal')
def _compute_product_margin(self):
for line in self:
currency = line.move_id.currency_id
price = line.purchase_price
margin = line.price_subtotal - (price * line.quantity)
if line.product_id and not price:
date = line.move_id.date if line.move_id.date else fields.Date.context_today(line.move_id)
from_cur = line.move_id.company_currency_id.with_context(date=date)
price = from_cur._convert(line.product_id.standard_price, currency, line.company_id, date, round=False)
margin = line.price_subtotal - (price * line.quantity)
line.margin = currency.round(margin) if currency else margin
line.margin_percent = 1.0 if not line.price_subtotal else line.margin / line.price_subtotal
class AccountMove(models.Model):
_inherit = "account.move"
margin = fields.Monetary(compute='_compute_product_margin', store=True, digits='Product Price',
help="Profitability by calculating the difference between the Unit Price and the cost.",
groups='base.group_user')
margin_percent = fields.Float(compute='_compute_product_margin', store=True, string='Margin (%)',
groups='base.group_user')
@api.depends('invoice_line_ids.margin', 'invoice_line_ids.price_subtotal')
def _compute_product_margin(self):
for invoice in self:
invoice.margin = sum(invoice.invoice_line_ids.mapped('margin'))
total_price_subtotal = sum(invoice.invoice_line_ids.mapped('price_subtotal'))
invoice.margin_percent = 1.0 if not total_price_subtotal else invoice.margin / total_price_subtotal

View File

@@ -0,0 +1 @@
from . import test_invoice_margin

View File

@@ -0,0 +1,60 @@
from odoo.fields import Command
from odoo.addons.sale_margin.tests.test_sale_margin import TestSaleMargin
from datetime import datetime
class TestInvoiceMargin(TestSaleMargin):
def setUp(self):
super(TestInvoiceMargin, self).setUp()
self.AccountMove = self.env['account.move']
self.SaleOrder = self.env['sale.order']
def test_invoice_margin(self):
self.product.standard_price = 700.0
order = self.empty_order
order.order_line = [
Command.create({
'price_unit': 1000.0,
'product_uom_qty': 10.0,
'product_id': self.product.id,
}),
]
# Confirm the sales order.
order.action_confirm()
# Verify that margin field gets bind with the value.
self.assertEqual(order.margin, 3000.00, "Sales order profit should be 6000.00")
self.assertEqual(order.margin_percent, 0.3, "Sales order margin should be 30%")
order.order_line.write({'qty_delivered': 10.0})
# Invoice the sales order.
inv = order._create_invoices()
self.assertEqual(inv.margin, order.margin)
self.assertEqual(inv.margin_percent, order.margin_percent)
account = self.env['account.account'].search([('account_type', '=', 'expense')], limit=1)
self.assertTrue(account)
inv = self.AccountMove.create({
'move_type': 'in_invoice',
'partner_id': order.partner_id.id,
'invoice_line_ids': [
(0, 0, {
'account_id': account.id,
'name': '[CARD] Graphics Card',
'price_unit': 1000.0,
'purchase_price': 600.0,
'quantity': 10.0,
'product_id': self.product.id}),
(0, 0, {
'account_id': account.id,
'name': 'Line without product_uom',
'price_unit': 1000.0,
'purchase_price': 800.0,
'quantity': 10.0,})
],
})
self.assertEqual(len(inv.invoice_line_ids), 2)
self.assertEqual(inv.margin, 6000.0)
self.assertEqual(inv.margin_percent, 0.3)

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record model="ir.ui.view" id="invoice_margin_form">
<field name="name">account.invoice.margin.view.form</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='amount_residual']" position="after">
<span class="oe_inline oe_right" groups="base.group_user" colspan="2">
<span>Margin:</span>
<field name="margin" class="oe_inline"/>
(<field name="margin_percent" nolabel="1" class="oe_inline" widget="percentage"/>)
</span>
</xpath>
<xpath expr="//field[@name='invoice_line_ids']//field[@name='price_unit']" position="after">
<field name="purchase_price" groups="base.group_user"/>
</xpath>
</field>
</record>
</odoo>