[MIG] sale_margin_no_onchange from v10 to v12
This commit is contained in:
@@ -1,4 +1,2 @@
|
|||||||
# -*- encoding: utf-8 -*-
|
|
||||||
|
|
||||||
from . import sale
|
from . import sale
|
||||||
from . import sale_report
|
from . import sale_report
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# Copyright (C) 2015-2019 Akretion (http://www.akretion.com)
|
||||||
# Copyright (C) 2015-2018 Akretion (http://www.akretion.com)
|
|
||||||
# @author Alexis de Lattre <alexis.delattre@akretion.com>
|
# @author Alexis de Lattre <alexis.delattre@akretion.com>
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
'name': 'Sale Margin No Onchange',
|
'name': 'Sale Margin No Onchange',
|
||||||
'version': '10.0.1.0.0',
|
'version': '12.0.1.0.0',
|
||||||
'category': 'Accounting',
|
'category': 'Sales',
|
||||||
'license': 'AGPL-3',
|
'license': 'AGPL-3',
|
||||||
'summary': 'Copy standard price on sale order line and compute margins',
|
'summary': 'Copy standard price on sale order line and compute margins',
|
||||||
'description': """
|
'description': """
|
||||||
This module copies the field *standard_price* of the product on the sale order line when the sale order line is created. The allows the computation of the margin of the sale order.
|
This module copies the field *standard_price* of the product on the sale order line when the sale order line is created and then computes the margin of the sale order and the sale order line (in the currency of the quotation, in the currency of the company and the margin rate).
|
||||||
|
|
||||||
|
I decided to develop this module as an alternative to the OCA sale margin modules because I wanted a small and simple module. The module *account_invoice_margin*, available in the same Github repository, do the same thing on customer invoices.
|
||||||
|
|
||||||
This module has been written by Alexis de Lattre from Akretion
|
This module has been written by Alexis de Lattre from Akretion
|
||||||
<alexis.delattre@akretion.com>.
|
<alexis.delattre@akretion.com>.
|
||||||
@@ -19,8 +20,6 @@ This module has been written by Alexis de Lattre from Akretion
|
|||||||
'author': 'Akretion',
|
'author': 'Akretion',
|
||||||
'website': 'http://www.akretion.com',
|
'website': 'http://www.akretion.com',
|
||||||
'depends': ['sale'],
|
'depends': ['sale'],
|
||||||
'data': [
|
'data': ['sale_view.xml'],
|
||||||
'sale_view.xml',
|
|
||||||
],
|
|
||||||
'installable': True,
|
'installable': True,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# Copyright (C) 2015-2019 Akretion (http://www.akretion.com)
|
||||||
# Copyright (C) 2015-2018 Akretion (http://www.akretion.com)
|
|
||||||
# @author Alexis de Lattre <alexis.delattre@akretion.com>
|
# @author Alexis de Lattre <alexis.delattre@akretion.com>
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
|
||||||
from odoo import models, fields, api
|
from odoo import api, fields, models
|
||||||
import odoo.addons.decimal_precision as dp
|
import odoo.addons.decimal_precision as dp
|
||||||
|
|
||||||
|
|
||||||
@@ -14,7 +13,7 @@ class SaleOrderLine(models.Model):
|
|||||||
# Also defined in bi_sale_company_currency
|
# Also defined in bi_sale_company_currency
|
||||||
company_currency_id = fields.Many2one(
|
company_currency_id = fields.Many2one(
|
||||||
related='order_id.company_id.currency_id',
|
related='order_id.company_id.currency_id',
|
||||||
readonly=True, store=True, string='Company Currency')
|
store=True, string='Company Currency')
|
||||||
standard_price_company_currency = fields.Float(
|
standard_price_company_currency = fields.Float(
|
||||||
string='Cost Price in Company Currency', readonly=True,
|
string='Cost Price in Company Currency', readonly=True,
|
||||||
digits=dp.get_precision('Product Price'),
|
digits=dp.get_precision('Product Price'),
|
||||||
@@ -48,17 +47,19 @@ class SaleOrderLine(models.Model):
|
|||||||
margin_comp_cur = 0.0
|
margin_comp_cur = 0.0
|
||||||
margin_rate = 0.0
|
margin_rate = 0.0
|
||||||
order_cur = line.order_id.pricelist_id.currency_id
|
order_cur = line.order_id.pricelist_id.currency_id
|
||||||
company_cur = line.order_id.company_id.currency_id
|
company = line.order_id.company_id
|
||||||
|
company_cur = company.currency_id
|
||||||
if order_cur and company_cur:
|
if order_cur and company_cur:
|
||||||
date = line.order_id.date_order
|
date = line.order_id.date_order
|
||||||
standard_price_sale_cur =\
|
standard_price_sale_cur =\
|
||||||
company_cur.with_context(date=date).compute(
|
company_cur._convert(
|
||||||
line.standard_price_company_currency, order_cur)
|
line.standard_price_company_currency, order_cur,
|
||||||
|
company, date)
|
||||||
margin_sale_cur =\
|
margin_sale_cur =\
|
||||||
line.price_subtotal\
|
line.price_subtotal\
|
||||||
- line.product_uom_qty * standard_price_sale_cur
|
- line.product_uom_qty * standard_price_sale_cur
|
||||||
margin_comp_cur = order_cur.with_context(date=date).compute(
|
margin_comp_cur = order_cur._convert(
|
||||||
margin_sale_cur, company_cur)
|
margin_sale_cur, company_cur, company, date)
|
||||||
if line.price_subtotal:
|
if line.price_subtotal:
|
||||||
margin_rate = 100 * margin_sale_cur / line.price_subtotal
|
margin_rate = 100 * margin_sale_cur / line.price_subtotal
|
||||||
line.standard_price_sale_currency = standard_price_sale_cur
|
line.standard_price_sale_currency = standard_price_sale_cur
|
||||||
@@ -74,7 +75,7 @@ class SaleOrderLine(models.Model):
|
|||||||
std_price = pp.standard_price
|
std_price = pp.standard_price
|
||||||
sale_uom_id = vals.get('product_uom')
|
sale_uom_id = vals.get('product_uom')
|
||||||
if sale_uom_id and sale_uom_id != pp.uom_id.id:
|
if sale_uom_id and sale_uom_id != pp.uom_id.id:
|
||||||
sale_uom = self.env['product.uom'].browse(sale_uom_id)
|
sale_uom = self.env['uom.uom'].browse(sale_uom_id)
|
||||||
# convert from product UoM to sale UoM
|
# convert from product UoM to sale UoM
|
||||||
std_price = pp.uom_id._compute_price(
|
std_price = pp.uom_id._compute_price(
|
||||||
pp.standard_price, sale_uom)
|
pp.standard_price, sale_uom)
|
||||||
@@ -92,7 +93,7 @@ class SaleOrderLine(models.Model):
|
|||||||
else:
|
else:
|
||||||
pp = sol.product_id
|
pp = sol.product_id
|
||||||
if 'product_uom' in vals:
|
if 'product_uom' in vals:
|
||||||
sale_uom = self.env['product.uom'].browse(
|
sale_uom = self.env['uom.uom'].browse(
|
||||||
vals['product_uom'])
|
vals['product_uom'])
|
||||||
else:
|
else:
|
||||||
sale_uom = sol.product_uom
|
sale_uom = sol.product_uom
|
||||||
@@ -108,7 +109,7 @@ class SaleOrder(models.Model):
|
|||||||
|
|
||||||
# Also defined in bi_sale_company_currency
|
# Also defined in bi_sale_company_currency
|
||||||
company_currency_id = fields.Many2one(
|
company_currency_id = fields.Many2one(
|
||||||
related='company_id.currency_id', readonly=True, store=True,
|
related='company_id.currency_id', store=True,
|
||||||
string="Company Currency")
|
string="Company Currency")
|
||||||
margin_sale_currency = fields.Monetary(
|
margin_sale_currency = fields.Monetary(
|
||||||
string='Margin in Sale Currency',
|
string='Margin in Sale Currency',
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# Copyright 2018-2019 Akretion (http://www.akretion.com)
|
||||||
# Copyright 2018 Akretion (http://www.akretion.com)
|
|
||||||
# @author Alexis de Lattre <alexis.delattre@akretion.com>
|
# @author Alexis de Lattre <alexis.delattre@akretion.com>
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
@@ -11,7 +10,10 @@ class SaleReport(models.Model):
|
|||||||
|
|
||||||
margin = fields.Float(string='Margin', readonly=True)
|
margin = fields.Float(string='Margin', readonly=True)
|
||||||
|
|
||||||
def _select(self):
|
def _query(self, with_clause='', fields={}, groupby='', from_clause=''):
|
||||||
select_str = super(SaleReport, self)._select()
|
fields['margin_company_currency'] =\
|
||||||
select_str += ", SUM(l.margin_company_currency) AS margin"
|
", SUM(l.margin_company_currency) AS margin"
|
||||||
return select_str
|
res = super(SaleReport, self)._query(
|
||||||
|
with_clause=with_clause, fields=fields, groupby=groupby,
|
||||||
|
from_clause=from_clause)
|
||||||
|
return res
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<!--
|
<!--
|
||||||
Copyright (C) 2015-2018 Akretion (http://www.akretion.com/)
|
Copyright (C) 2015-2019 Akretion (http://www.akretion.com/)
|
||||||
@author: Alexis de Lattre <alexis.delattre@akretion.com>
|
@author: Alexis de Lattre <alexis.delattre@akretion.com>
|
||||||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
-->
|
-->
|
||||||
@@ -13,13 +13,13 @@
|
|||||||
<field name="model">sale.order</field>
|
<field name="model">sale.order</field>
|
||||||
<field name="inherit_id" ref="sale.view_order_form"/>
|
<field name="inherit_id" ref="sale.view_order_form"/>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<field name="origin" position="after">
|
<group name="technical" position="inside">
|
||||||
<field name="margin_sale_currency" string="Margin"
|
<field name="margin_sale_currency" string="Margin"
|
||||||
groups="account.group_account_user"/>
|
groups="account.group_account_user"/>
|
||||||
<field name="margin_company_currency"
|
<field name="margin_company_currency"
|
||||||
groups="account.group_account_user"/>
|
groups="account.group_account_user"/>
|
||||||
<field name="company_currency_id" invisible="1"/>
|
<field name="company_currency_id" invisible="1"/>
|
||||||
</field>
|
</group>
|
||||||
<xpath expr="//field[@name='order_line']/form//field[@name='analytic_tag_ids']/.." position="after">
|
<xpath expr="//field[@name='order_line']/form//field[@name='analytic_tag_ids']/.." position="after">
|
||||||
<field name="standard_price_sale_currency" groups="base.group_no_one"/>
|
<field name="standard_price_sale_currency" groups="base.group_no_one"/>
|
||||||
<field name="standard_price_company_currency" groups="base.group_no_one"/>
|
<field name="standard_price_company_currency" groups="base.group_no_one"/>
|
||||||
@@ -41,12 +41,13 @@
|
|||||||
<field name="inherit_id" ref="sale.view_order_line_tree" />
|
<field name="inherit_id" ref="sale.view_order_line_tree" />
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
<field name="price_subtotal" position="after">
|
<field name="price_subtotal" position="after">
|
||||||
<field name="standard_price_company_currency"/>
|
<field name="standard_price_company_currency" groups="account.group_account_user"/>
|
||||||
<field name="margin_company_currency"/>
|
<field name="margin_company_currency" groups="account.group_account_user"/>
|
||||||
<field name="margin_rate"/>
|
<field name="margin_rate" groups="account.group_account_user"/>
|
||||||
<field name="company_currency_id" invisible="1"/>
|
<field name="company_currency_id" invisible="1"/>
|
||||||
</field>
|
</field>
|
||||||
</field>
|
</field>
|
||||||
</record>
|
</record>
|
||||||
|
|
||||||
|
|
||||||
</odoo>
|
</odoo>
|
||||||
|
|||||||
Reference in New Issue
Block a user