MIG account_invoice_margin For Odoo 13.0 ( Test Case have error. Module works perfect).

This commit is contained in:
Bhoomi
2019-09-26 12:27:32 -04:00
parent 41c3f8f5e4
commit 22c96bb9d3
4 changed files with 25 additions and 25 deletions

View File

@@ -1,7 +1,7 @@
{
'name': 'Invoice Margin',
'author': 'Hibou Corp. <hello@hibou.io>',
'version': '12.0.1.0.0',
'version': '13.0.1.0.0',
'category': 'Accounting',
'sequence': 95,
'summary': 'Invoices include margin calculation.',

View File

@@ -2,53 +2,53 @@ from odoo import api, fields, models
from odoo.addons import decimal_precision as dp
class AccountInvoiceLine(models.Model):
_inherit = "account.invoice.line"
class AccountMoveLine(models.Model):
_inherit = "account.move.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):
def _compute_margin(self, move_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
frm_cur = move_id.company_currency_id
to_cur = move_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)._convert(purchase_price, to_cur, invoice_id.company_id, ctx['date'], round=False)
ctx['date'] = move_id.date if move_id.date else fields.Date.context_today(move_id)
price = frm_cur.with_context(ctx)._convert(purchase_price, to_cur, move_id.company_id, ctx['date'], round=False)
return price
@api.onchange('product_id', 'uom_id')
@api.onchange('product_id', 'product_uom_id')
def product_id_change_margin(self):
if not self.product_id or not self.uom_id:
if not self.product_id or not self.product_uom_id:
return
self.purchase_price = self._compute_margin(self.invoice_id, self.product_id, self.uom_id, self.sale_line_ids)
self.purchase_price = self._compute_margin(self.move_id, self.product_id, self.product_uom_id, self.sale_line_ids)
@api.model
@api.model_create_multi
def create(self, vals):
line = super(AccountInvoiceLine, self).create(vals)
line = super(AccountMoveLine, 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
currency = line.move_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)
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)
line.margin = currency.round(line.price_subtotal - (price * line.quantity))
class AccountInvoice(models.Model):
_inherit = "account.invoice"
class AccountMove(models.Model):
_inherit = "account.move"
margin = fields.Monetary(compute='_product_margin',
help="It gives profitability by calculating the difference between the Unit Price and the cost.",

View File

@@ -6,7 +6,7 @@ class TestInvoiceMargin(TestSaleMargin):
def setUp(self):
super(TestInvoiceMargin, self).setUp()
self.AccountInvoice = self.env['account.invoice']
self.AccountMove = self.env['account.move']
def test_invoice_margin(self):
""" Test the sale_margin module in Odoo. """
@@ -42,12 +42,12 @@ class TestInvoiceMargin(TestSaleMargin):
sale_order_so11.order_line.write({'qty_delivered': 10.0})
# Invoice the sales order.
inv_id = sale_order_so11.action_invoice_create()
inv = self.AccountInvoice.browse(inv_id)
inv_id = sale_order_so11._create_invoices()
inv = self.AccountMove.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({
inv = self.AccountMove.create({
'partner_id': self.partner_id,
'invoice_line_ids': [
(0, 0, {

View File

@@ -3,10 +3,10 @@
<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="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='residual']" position="after">
<xpath expr="//field[@name='amount_residual']" position="after">
<field name="margin" groups="base.group_user"/>
</xpath>
<xpath expr="//field[@name='invoice_line_ids']//field[@name='price_unit']" position="after">