[IMP] sale_margin: clean module
* Split python file by classes * Remove empty ir.model.access.csv file * Correct name for views file * Use setUpClass instead of setUp to avoid multiple setups during tests Part-of: odoo/odoo#85001
This commit is contained in:
@@ -13,7 +13,11 @@ This gives the profitability by calculating the difference between the Unit
|
||||
Price and Cost Price.
|
||||
""",
|
||||
'depends':['sale_management'],
|
||||
'demo':['data/sale_margin_demo.xml'],
|
||||
'data':['security/ir.model.access.csv','views/sale_margin_view.xml'],
|
||||
'demo':[
|
||||
'data/sale_margin_demo.xml',
|
||||
],
|
||||
'data':[
|
||||
'views/sale_order_views.xml',
|
||||
],
|
||||
'license': 'LGPL-3',
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<odoo>
|
||||
<data noupdate="1">
|
||||
<odoo noupdate="1">
|
||||
|
||||
<record id="sale.sale_order_line_1" model="sale.order.line">
|
||||
<field name="purchase_price">2870.00</field>
|
||||
@@ -34,5 +33,4 @@
|
||||
<field name="purchase_price">13.00</field>
|
||||
</record>
|
||||
|
||||
</data>
|
||||
</odoo>
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from . import sale_order
|
||||
from . import sale_order_line
|
||||
|
||||
@@ -1,63 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class SaleOrderLine(models.Model):
|
||||
_inherit = "sale.order.line"
|
||||
|
||||
margin = fields.Float(
|
||||
"Margin", compute='_compute_margin',
|
||||
digits='Product Price', store=True, groups="base.group_user", precompute=True)
|
||||
margin_percent = fields.Float(
|
||||
"Margin (%)", compute='_compute_margin', store=True, groups="base.group_user", precompute=True)
|
||||
purchase_price = fields.Float(
|
||||
string='Cost', compute="_compute_purchase_price",
|
||||
digits='Product Price', store=True, readonly=False, precompute=True,
|
||||
groups="base.group_user")
|
||||
|
||||
@api.depends('product_id', 'company_id', 'currency_id', 'product_uom')
|
||||
def _compute_purchase_price(self):
|
||||
for line in self:
|
||||
if not line.product_id:
|
||||
line.purchase_price = 0.0
|
||||
continue
|
||||
line = line.with_company(line.company_id)
|
||||
product_cost = line.product_id.standard_price
|
||||
line.purchase_price = line._convert_price(product_cost, line.product_id.uom_id)
|
||||
|
||||
@api.depends('price_subtotal', 'product_uom_qty', 'purchase_price')
|
||||
def _compute_margin(self):
|
||||
for line in self:
|
||||
line.margin = line.price_subtotal - (line.purchase_price * line.product_uom_qty)
|
||||
line.margin_percent = line.price_subtotal and line.margin/line.price_subtotal
|
||||
|
||||
def _convert_price(self, product_cost, from_uom):
|
||||
self.ensure_one()
|
||||
if not product_cost:
|
||||
# If the standard_price is 0
|
||||
# Avoid unnecessary computations
|
||||
# and currency conversions
|
||||
if not self.purchase_price:
|
||||
return product_cost
|
||||
from_currency = self.product_id.cost_currency_id
|
||||
to_cur = self.currency_id or self.order_id.currency_id
|
||||
to_uom = self.product_uom
|
||||
if to_uom and to_uom != from_uom:
|
||||
product_cost = from_uom._compute_price(
|
||||
product_cost,
|
||||
to_uom,
|
||||
)
|
||||
return from_currency._convert(
|
||||
from_amount=product_cost,
|
||||
to_currency=to_cur,
|
||||
company=self.company_id or self.env.company,
|
||||
date=self.order_id.date_order or fields.Date.today(),
|
||||
round=False,
|
||||
) if to_cur and product_cost else product_cost
|
||||
# The pricelist may not have been set, therefore no conversion
|
||||
# is needed because we don't know the target currency..
|
||||
|
||||
class SaleOrder(models.Model):
|
||||
_inherit = "sale.order"
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class SaleOrderLine(models.Model):
|
||||
_inherit = "sale.order.line"
|
||||
|
||||
margin = fields.Float(
|
||||
"Margin", compute='_compute_margin',
|
||||
digits='Product Price', store=True, groups="base.group_user", precompute=True)
|
||||
margin_percent = fields.Float(
|
||||
"Margin (%)", compute='_compute_margin', store=True, groups="base.group_user", precompute=True)
|
||||
purchase_price = fields.Float(
|
||||
string='Cost', compute="_compute_purchase_price",
|
||||
digits='Product Price', store=True, readonly=False, precompute=True,
|
||||
groups="base.group_user")
|
||||
|
||||
@api.depends('product_id', 'company_id', 'currency_id', 'product_uom')
|
||||
def _compute_purchase_price(self):
|
||||
for line in self:
|
||||
if not line.product_id:
|
||||
line.purchase_price = 0.0
|
||||
continue
|
||||
line = line.with_company(line.company_id)
|
||||
product_cost = line.product_id.standard_price
|
||||
line.purchase_price = line._convert_price(product_cost, line.product_id.uom_id)
|
||||
|
||||
@api.depends('price_subtotal', 'product_uom_qty', 'purchase_price')
|
||||
def _compute_margin(self):
|
||||
for line in self:
|
||||
line.margin = line.price_subtotal - (line.purchase_price * line.product_uom_qty)
|
||||
line.margin_percent = line.price_subtotal and line.margin/line.price_subtotal
|
||||
|
||||
def _convert_price(self, product_cost, from_uom):
|
||||
self.ensure_one()
|
||||
if not product_cost:
|
||||
# If the standard_price is 0
|
||||
# Avoid unnecessary computations
|
||||
# and currency conversions
|
||||
if not self.purchase_price:
|
||||
return product_cost
|
||||
from_currency = self.product_id.cost_currency_id
|
||||
to_cur = self.currency_id or self.order_id.currency_id
|
||||
to_uom = self.product_uom
|
||||
if to_uom and to_uom != from_uom:
|
||||
product_cost = from_uom._compute_price(
|
||||
product_cost,
|
||||
to_uom,
|
||||
)
|
||||
return from_currency._convert(
|
||||
from_amount=product_cost,
|
||||
to_currency=to_cur,
|
||||
company=self.company_id or self.env.company,
|
||||
date=self.order_id.date_order or fields.Date.today(),
|
||||
round=False,
|
||||
) if to_cur and product_cost else product_cost
|
||||
# The pricelist may not have been set, therefore no conversion
|
||||
# is needed because we don't know the target currency..
|
||||
@@ -1 +0,0 @@
|
||||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||
|
@@ -7,20 +7,21 @@ from datetime import datetime
|
||||
|
||||
class TestSaleMargin(common.TransactionCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestSaleMargin, self).setUp()
|
||||
self.SaleOrder = self.env['sale.order']
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.SaleOrder = cls.env['sale.order']
|
||||
|
||||
self.product_uom_id = self.ref('uom.product_uom_unit')
|
||||
self.product = self.env['product.product'].create({'name': 'Individual Workplace'})
|
||||
self.product_id = self.product.id
|
||||
self.partner_id = self.env['res.partner'].create({'name': 'A test partner'}).id
|
||||
self.partner_invoice_address_id = self.env['res.partner'].create({
|
||||
cls.product_uom_id = cls.env.ref('uom.product_uom_unit').id
|
||||
cls.product = cls.env['product.product'].create({'name': 'Individual Workplace'})
|
||||
cls.product_id = cls.product.id
|
||||
cls.partner_id = cls.env['res.partner'].create({'name': 'A test partner'}).id
|
||||
cls.partner_invoice_address_id = cls.env['res.partner'].create({
|
||||
'name': 'A test partner address',
|
||||
'parent_id': self.partner_id,
|
||||
'parent_id': cls.partner_id,
|
||||
}).id
|
||||
self.pricelist_id = self.ref('product.list0')
|
||||
self.pricelist = self.env.ref('product.list0')
|
||||
cls.pricelist = cls.env.ref('product.list0')
|
||||
cls.pricelist_id = cls.pricelist.id
|
||||
|
||||
def test_sale_margin(self):
|
||||
""" Test the sale_margin module in Odoo. """
|
||||
|
||||
Reference in New Issue
Block a user