mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
Initial commit of account_invoice_margin for 11.0
This commit is contained in:
1
account_invoice_margin/__init__.py
Normal file
1
account_invoice_margin/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import models
|
||||||
23
account_invoice_margin/__manifest__.py
Executable file
23
account_invoice_margin/__manifest__.py
Executable file
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
'name': 'Invoice Margin',
|
||||||
|
'author': 'Hibou Corp. <hello@hibou.io>',
|
||||||
|
'version': '11.0.1.0.0',
|
||||||
|
'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,
|
||||||
|
}
|
||||||
1
account_invoice_margin/models/__init__.py
Normal file
1
account_invoice_margin/models/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import account_invoice
|
||||||
62
account_invoice_margin/models/account_invoice.py
Normal file
62
account_invoice_margin/models/account_invoice.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
from odoo import api, fields, models
|
||||||
|
from odoo.addons import decimal_precision as dp
|
||||||
|
|
||||||
|
|
||||||
|
class AccountInvoiceLine(models.Model):
|
||||||
|
_inherit = "account.invoice.line"
|
||||||
|
|
||||||
|
margin = fields.Float(compute='_product_margin', digits=dp.get_precision('Product Price'), store=True)
|
||||||
|
purchase_price = fields.Float(string='Cost', digits=dp.get_precision('Product Price'))
|
||||||
|
|
||||||
|
def _compute_margin(self, invoice_id, product_id, product_uom_id, sale_line_ids):
|
||||||
|
# if sale_line_ids and don't re-browse
|
||||||
|
for line in sale_line_ids:
|
||||||
|
return line.purchase_price
|
||||||
|
frm_cur = invoice_id.company_currency_id
|
||||||
|
to_cur = invoice_id.currency_id
|
||||||
|
purchase_price = product_id.standard_price
|
||||||
|
if product_uom_id != product_id.uom_id:
|
||||||
|
purchase_price = product_id.uom_id._compute_price(purchase_price, product_uom_id)
|
||||||
|
ctx = self.env.context.copy()
|
||||||
|
ctx['date'] = invoice_id.date if invoice_id.date else fields.Date.context_today(invoice_id)
|
||||||
|
price = frm_cur.with_context(ctx).compute(purchase_price, to_cur, round=False)
|
||||||
|
return price
|
||||||
|
|
||||||
|
@api.onchange('product_id', 'uom_id')
|
||||||
|
def product_id_change_margin(self):
|
||||||
|
if not self.product_id or not self.uom_id:
|
||||||
|
return
|
||||||
|
self.purchase_price = self._compute_margin(self.invoice_id, self.product_id, self.uom_id, self.sale_line_ids)
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def create(self, vals):
|
||||||
|
line = super(AccountInvoiceLine, self).create(vals)
|
||||||
|
line.product_id_change_margin()
|
||||||
|
return line
|
||||||
|
|
||||||
|
@api.depends('product_id', 'purchase_price', 'quantity', 'price_unit', 'price_subtotal')
|
||||||
|
def _product_margin(self):
|
||||||
|
for line in self:
|
||||||
|
currency = line.invoice_id.currency_id
|
||||||
|
price = line.purchase_price
|
||||||
|
if line.product_id and not price:
|
||||||
|
date = line.invoice_id.date if line.invoice_id.date else fields.Date.context_today(line.invoice_id)
|
||||||
|
from_cur = line.invoice_id.company_currency_id.with_context(date=date)
|
||||||
|
price = from_cur.compute(line.product_id.standard_price, currency, round=False)
|
||||||
|
|
||||||
|
line.margin = currency.round(line.price_subtotal - (price * line.quantity))
|
||||||
|
|
||||||
|
|
||||||
|
class AccountInvoice(models.Model):
|
||||||
|
_inherit = "account.invoice"
|
||||||
|
|
||||||
|
margin = fields.Monetary(compute='_product_margin',
|
||||||
|
help="It gives profitability by calculating the difference between the Unit Price and the cost.",
|
||||||
|
currency_field='currency_id',
|
||||||
|
digits=dp.get_precision('Product Price'),
|
||||||
|
store=True)
|
||||||
|
|
||||||
|
@api.depends('invoice_line_ids.margin')
|
||||||
|
def _product_margin(self):
|
||||||
|
for invoice in self:
|
||||||
|
invoice.margin = sum(invoice.invoice_line_ids.mapped('margin'))
|
||||||
1
account_invoice_margin/tests/__init__.py
Normal file
1
account_invoice_margin/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import test_invoice_margin
|
||||||
64
account_invoice_margin/tests/test_invoice_margin.py
Normal file
64
account_invoice_margin/tests/test_invoice_margin.py
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
from odoo.addons.sale_margin.tests.test_sale_margin import TestSaleMargin
|
||||||
|
|
||||||
|
class TestInvoiceMargin(TestSaleMargin):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super(TestInvoiceMargin, self).setUp()
|
||||||
|
self.AccountInvoice = self.env['account.invoice']
|
||||||
|
|
||||||
|
def test_invoice_margin(self):
|
||||||
|
""" Test the sale_margin module in Odoo. """
|
||||||
|
# Create a sales order for product Graphics Card.
|
||||||
|
sale_order_so11 = self.SaleOrder.create({
|
||||||
|
'name': 'Test_SO011',
|
||||||
|
'order_line': [
|
||||||
|
(0, 0, {
|
||||||
|
'name': '[CARD] Graphics Card',
|
||||||
|
'purchase_price': 700.0,
|
||||||
|
'price_unit': 1000.0,
|
||||||
|
'product_uom': self.product_uom_id,
|
||||||
|
'product_uom_qty': 10.0,
|
||||||
|
'state': 'draft',
|
||||||
|
'product_id': self.product_id}),
|
||||||
|
(0, 0, {
|
||||||
|
'name': 'Line without product_uom',
|
||||||
|
'price_unit': 1000.0,
|
||||||
|
'purchase_price': 700.0,
|
||||||
|
'product_uom_qty': 10.0,
|
||||||
|
'state': 'draft',
|
||||||
|
'product_id': self.product_id})
|
||||||
|
],
|
||||||
|
'partner_id': self.partner_id,
|
||||||
|
'partner_invoice_id': self.partner_invoice_address_id,
|
||||||
|
'partner_shipping_id': self.partner_invoice_address_id,
|
||||||
|
'pricelist_id': self.pricelist_id})
|
||||||
|
# Confirm the sales order.
|
||||||
|
sale_order_so11.action_confirm()
|
||||||
|
# Verify that margin field gets bind with the value.
|
||||||
|
self.assertEqual(sale_order_so11.margin, 6000.00, "Sales order margin should be 6000.00")
|
||||||
|
|
||||||
|
# Invoice the sales order.
|
||||||
|
inv_id = sale_order_so11.action_invoice_create()
|
||||||
|
inv = self.AccountInvoice.browse(inv_id)
|
||||||
|
self.assertEqual(inv.margin, sale_order_so11.margin)
|
||||||
|
|
||||||
|
account = self.env['account.account'].search([('internal_type', '=', 'other')], limit=1)
|
||||||
|
inv = self.AccountInvoice.create({
|
||||||
|
'partner_id': self.partner_id,
|
||||||
|
'invoice_line_ids': [
|
||||||
|
(0, 0, {
|
||||||
|
'account_id': account.id,
|
||||||
|
'name': '[CARD] Graphics Card',
|
||||||
|
'purchase_price': 600.0,
|
||||||
|
'price_unit': 1000.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(inv.margin, 6000.0)
|
||||||
18
account_invoice_margin/views/account_invoice_views.xml
Normal file
18
account_invoice_margin/views/account_invoice_views.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?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.invoice</field>
|
||||||
|
<field name="inherit_id" ref="account.invoice_form"/>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<xpath expr="//field[@name='residual']" position="after">
|
||||||
|
<field name="margin" groups="base.group_user"/>
|
||||||
|
</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>
|
||||||
Reference in New Issue
Block a user