sale_margin_no_onchange port to v10

This commit is contained in:
Alexis de Lattre
2018-02-09 17:52:25 +01:00
parent d84ef2e8c5
commit 52eff801b6
3 changed files with 97 additions and 130 deletions

View File

@@ -1,30 +1,13 @@
# -*- encoding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################## # Copyright (C) 2015-2018 Akretion (http://www.akretion.com)
#
# Sale Margin No Onchange module for Odoo
# Copyright (C) 2015 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).
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{ {
'name': 'Sale Margin No Onchange', 'name': 'Sale Margin No Onchange',
'version': '0.1', 'version': '10.0.1.0.0',
'category': 'Accounting & Finance', 'category': 'Accounting',
'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': """
@@ -39,5 +22,5 @@ This module has been written by Alexis de Lattre from Akretion
'data': [ 'data': [
'sale_view.xml', 'sale_view.xml',
], ],
'installable': False, 'installable': True,
} }

View File

@@ -1,81 +1,70 @@
# -*- encoding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################## # Copyright (C) 2015-2018 Akretion (http://www.akretion.com)
#
# Sale Margin No Onchange module for Odoo
# Copyright (C) 2015 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).
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp import models, fields, api
import openerp.addons.decimal_precision as dp from odoo import models, fields, api
import odoo.addons.decimal_precision as dp
class SaleOrderLine(models.Model): class SaleOrderLine(models.Model):
_inherit = 'sale.order.line' _inherit = 'sale.order.line'
# Also defined in bi_sale_company_currency
company_currency_id = fields.Many2one(
related='order_id.company_id.currency_id',
readonly=True, 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'),
help="Cost price in company currency in the unit of measure "
"of the sale order line")
standard_price_sale_currency = fields.Float( standard_price_sale_currency = fields.Float(
string='Cost Price in Sale Currency', readonly=True, string='Cost Price in Sale Currency', readonly=True,
compute='_compute_margin', store=True, compute='_compute_margin', store=True,
digits=dp.get_precision('Product Price')) digits=dp.get_precision('Product Price'),
margin_sale_currency = fields.Float( help="Cost price in sale currency in the unit of measure "
"of the sale order line")
margin_sale_currency = fields.Monetary(
string='Margin in Sale Currency', readonly=True, store=True, string='Margin in Sale Currency', readonly=True, store=True,
compute='_compute_margin', compute='_compute_margin', currency_field='currency_id')
digits=dp.get_precision('Account')) margin_company_currency = fields.Monetary(
margin_company_currency = fields.Float(
string='Margin in Company Currency', readonly=True, store=True, string='Margin in Company Currency', readonly=True, store=True,
compute='_compute_margin', compute='_compute_margin', currency_field='company_currency_id')
digits=dp.get_precision('Account'))
margin_rate = fields.Float( margin_rate = fields.Float(
string="Margin Rate", readonly=True, store=True, string="Margin (%)", readonly=True, store=True,
compute='_compute_margin', compute='_compute_margin',
digits=(16, 2), help="Margin rate in percentage of the sale price") digits=(16, 2), help="Margin rate in percentage of the sale price")
@api.one
@api.depends( @api.depends(
'standard_price_company_currency', 'order_id.pricelist_id', 'standard_price_company_currency', 'order_id.pricelist_id.currency_id',
'order_id.date_order', 'product_uom_qty', 'price_subtotal') 'order_id.date_order', 'product_uom_qty', 'price_subtotal',
'order_id.company_id')
def _compute_margin(self): def _compute_margin(self):
for line in self:
standard_price_sale_cur = 0.0 standard_price_sale_cur = 0.0
margin_sale_cur = 0.0 margin_sale_cur = 0.0
margin_comp_cur = 0.0 margin_comp_cur = 0.0
margin_rate = 0.0 margin_rate = 0.0
if self.order_id and self.order_id.currency_id: order_cur = line.order_id.pricelist_id.currency_id
# it works in _get_current_rate company_cur = line.order_id.company_id.currency_id
# even if we set date = False in context if order_cur and company_cur:
date = line.order_id.date_order
standard_price_sale_cur =\ standard_price_sale_cur =\
self.order_id.company_id.currency_id.with_context( company_cur.with_context(date=date).compute(
date=self.order_id.date_order).compute( line.standard_price_company_currency, order_cur)
self.standard_price_company_currency,
self.order_id.currency_id)
margin_sale_cur =\ margin_sale_cur =\
self.price_subtotal\ line.price_subtotal\
- self.product_uom_qty * standard_price_sale_cur - line.product_uom_qty * standard_price_sale_cur
margin_comp_cur = self.order_id.currency_id.with_context( margin_comp_cur = order_cur.with_context(date=date).compute(
date=self.order_id.date_order).compute( margin_sale_cur, company_cur)
margin_sale_cur, self.order_id.company_id.currency_id) if line.price_subtotal:
if self.price_subtotal: margin_rate = 100 * margin_sale_cur / line.price_subtotal
margin_rate = 100 * margin_sale_cur / self.price_subtotal line.standard_price_sale_currency = standard_price_sale_cur
self.standard_price_sale_currency = standard_price_sale_cur line.margin_sale_currency = margin_sale_cur
self.margin_sale_currency = margin_sale_cur line.margin_company_currency = margin_comp_cur
self.margin_company_currency = margin_comp_cur line.margin_rate = margin_rate
self.margin_rate = margin_rate
# We want to copy standard_price on sale order line # We want to copy standard_price on sale order line
@api.model @api.model
@@ -85,38 +74,31 @@ 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:
std_price = self.env['product.uom']._compute_price( sale_uom = self.env['product.uom'].browse(sale_uom_id)
pp.uom_id.id, std_price, sale_uom_id) # convert from product UoM to sale UoM
std_price = pp.uom_id._compute_price(
standard_price, sale_uom)
vals['standard_price_company_currency'] = std_price vals['standard_price_company_currency'] = std_price
return super(SaleOrderLine, self).create(vals) return super(SaleOrderLine, self).create(vals)
@api.multi
def write(self, vals): def write(self, vals):
if not vals: if not vals:
vals = {} vals = {}
if 'product_id' in vals or 'product_uom' in vals: if 'product_id' in vals or 'product_uom' in vals:
for sol in self: for sol in self:
# product_uom and product_id are required fields
if 'product_id' in vals: if 'product_id' in vals:
if vals.get('product_id'): pp = self.env['product.product'].browse(vals['product_id'])
pp = self.env['product.product'].browse(
vals['product_id'])
else: else:
pp = False pp = sol.product_id
else:
pp = sol.product_id or False
# product_uom is a required field,
# so it's different from product_id
if 'product_uom' in vals: if 'product_uom' in vals:
sale_uom = self.env['product.uom'].browse( sale_uom = self.env['product.uom'].browse(
vals['product_uom']) vals['product_uom'])
else: else:
sale_uom = sol.product_uom sale_uom = sol.product_uom
std_price = 0.0
if pp:
std_price = pp.standard_price std_price = pp.standard_price
if sale_uom != pp.uom_id: if sale_uom != pp.uom_id:
std_price = self.env['product.uom']._compute_price( std_price = pp.uom_id._compute_price(std_price, sale_uom)
pp.uom_id.id, std_price, sale_uom.id)
sol.write({'standard_price_company_currency': std_price}) sol.write({'standard_price_company_currency': std_price})
return super(SaleOrderLine, self).write(vals) return super(SaleOrderLine, self).write(vals)
@@ -124,24 +106,28 @@ class SaleOrderLine(models.Model):
class SaleOrder(models.Model): class SaleOrder(models.Model):
_inherit = 'sale.order' _inherit = 'sale.order'
margin_sale_currency = fields.Float( # Also defined in bi_sale_company_currency
company_currency_id = fields.Many2one(
related='company_id.currency_id', readonly=True, store=True,
string="Company Currency")
margin_sale_currency = fields.Monetary(
string='Margin in Sale Currency', string='Margin in Sale Currency',
readonly=True, compute='_compute_margin', store=True, currency_field='currency_id',
digits=dp.get_precision('Account')) readonly=True, compute='_compute_margin', store=True)
margin_company_currency = fields.Float( margin_company_currency = fields.Monetary(
string='Margin in Company Currency', string='Margin in Company Currency',
readonly=True, compute='_compute_margin', store=True, currency_field='company_currency_id',
digits=dp.get_precision('Account')) readonly=True, compute='_compute_margin', store=True)
@api.one
@api.depends( @api.depends(
'order_line.margin_sale_currency', 'order_line.margin_sale_currency',
'order_line.margin_company_currency') 'order_line.margin_company_currency')
def _compute_margin(self): def _compute_margin(self):
for order in self:
margin_sale_cur = 0.0 margin_sale_cur = 0.0
margin_comp_cur = 0.0 margin_comp_cur = 0.0
for sol in self.order_line: for sol in order.order_line:
margin_sale_cur += sol.margin_sale_currency margin_sale_cur += sol.margin_sale_currency
margin_comp_cur += sol.margin_company_currency margin_comp_cur += sol.margin_company_currency
self.margin_sale_currency = margin_sale_cur order.margin_sale_currency = margin_sale_cur
self.margin_company_currency = margin_comp_cur order.margin_company_currency = margin_comp_cur

View File

@@ -1,12 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
Copyright (C) 2015 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>
The licence is in the file __openerp__.py License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
--> -->
<openerp> <odoo>
<data>
<record id="view_order_form" model="ir.ui.view"> <record id="view_order_form" model="ir.ui.view">
@@ -14,21 +13,20 @@
<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="company_id" position="after"> <field name="origin" position="after">
<field name="margin_sale_currency" <field name="margin_sale_currency" string="Margin"
string="Margin"
widget="monetary"
options="{'currency_field': 'currency_id'}"
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> </field>
<xpath expr="//field[@name='order_line']/tree/field[@name='price_subtotal']" position="after"> <xpath expr="//field[@name='order_line']/tree/field[@name='price_subtotal']" position="after">
<field name="standard_price_sale_currency" groups="base.group_no_one"/> <field name="standard_price_sale_currency" groups="base.group_no_one" string="Cost Price Sale Cur."/>
<field name="standard_price_company_currency" groups="base.group_no_one"/> <field name="standard_price_company_currency" groups="base.group_no_one" string="Cost Price Comp. Cur."/>
<field name="margin_sale_currency" groups="base.group_no_one"/> <field name="margin_sale_currency" groups="base.group_no_one" string="Margin Sale Cur."/>
<field name="margin_company_currency" groups="base.group_no_one"/> <field name="margin_company_currency" groups="base.group_no_one" string="Margin Comp. Cur."/>
<field name="margin_rate" groups="base.group_no_one"/> <field name="margin_rate" groups="base.group_no_one"/>
<field name="company_currency_id" invisible="1"/>
</xpath> </xpath>
</field> </field>
</record> </record>
@@ -42,9 +40,9 @@
<field name="standard_price_company_currency"/> <field name="standard_price_company_currency"/>
<field name="margin_company_currency"/> <field name="margin_company_currency"/>
<field name="margin_rate"/> <field name="margin_rate"/>
<field name="company_currency_id" invisible="1"/>
</field> </field>
</field> </field>
</record> </record>
</data> </odoo>
</openerp>