[IMP] pre-commit: first run on whole repo
This commit is contained in:
@@ -4,12 +4,12 @@
|
||||
|
||||
|
||||
{
|
||||
'name': 'Sale Margin No Onchange',
|
||||
'version': '12.0.1.0.0',
|
||||
'category': 'Sales',
|
||||
'license': 'AGPL-3',
|
||||
'summary': 'Copy standard price on sale order line and compute margins',
|
||||
'description': """
|
||||
"name": "Sale Margin No Onchange",
|
||||
"version": "12.0.1.0.0",
|
||||
"category": "Sales",
|
||||
"license": "AGPL-3",
|
||||
"summary": "Copy standard price on sale order line and compute margins",
|
||||
"description": """
|
||||
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.
|
||||
@@ -17,9 +17,9 @@ I decided to develop this module as an alternative to the OCA sale margin module
|
||||
This module has been written by Alexis de Lattre from Akretion
|
||||
<alexis.delattre@akretion.com>.
|
||||
""",
|
||||
'author': 'Akretion',
|
||||
'website': 'http://www.akretion.com',
|
||||
'depends': ['sale'],
|
||||
'data': ['sale_view.xml'],
|
||||
'installable': False,
|
||||
"author": "Akretion",
|
||||
"website": "https://github.com/OCA/odoo-usability",
|
||||
"depends": ["sale"],
|
||||
"data": ["sale_view.xml"],
|
||||
"installable": False,
|
||||
}
|
||||
|
||||
@@ -4,42 +4,64 @@
|
||||
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
import odoo.addons.decimal_precision as dp
|
||||
|
||||
|
||||
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',
|
||||
store=True, string='Company Currency')
|
||||
related="order_id.company_id.currency_id", store=True, string="Company Currency"
|
||||
)
|
||||
standard_price_company_currency = fields.Float(
|
||||
string='Cost Price in Company Currency', readonly=True,
|
||||
digits=dp.get_precision('Product Price'),
|
||||
string="Cost Price in Company Currency",
|
||||
readonly=True,
|
||||
digits=dp.get_precision("Product Price"),
|
||||
help="Cost price in company currency in the unit of measure "
|
||||
"of the sale order line")
|
||||
"of the sale order line",
|
||||
)
|
||||
standard_price_sale_currency = fields.Float(
|
||||
string='Cost Price in Sale Currency', readonly=True,
|
||||
compute='_compute_margin', store=True,
|
||||
digits=dp.get_precision('Product Price'),
|
||||
string="Cost Price in Sale Currency",
|
||||
readonly=True,
|
||||
compute="_compute_margin",
|
||||
store=True,
|
||||
digits=dp.get_precision("Product Price"),
|
||||
help="Cost price in sale currency in the unit of measure "
|
||||
"of the sale order line")
|
||||
"of the sale order line",
|
||||
)
|
||||
margin_sale_currency = fields.Monetary(
|
||||
string='Margin in Sale Currency', readonly=True, store=True,
|
||||
compute='_compute_margin', currency_field='currency_id')
|
||||
string="Margin in Sale Currency",
|
||||
readonly=True,
|
||||
store=True,
|
||||
compute="_compute_margin",
|
||||
currency_field="currency_id",
|
||||
)
|
||||
margin_company_currency = fields.Monetary(
|
||||
string='Margin in Company Currency', readonly=True, store=True,
|
||||
compute='_compute_margin', currency_field='company_currency_id')
|
||||
string="Margin in Company Currency",
|
||||
readonly=True,
|
||||
store=True,
|
||||
compute="_compute_margin",
|
||||
currency_field="company_currency_id",
|
||||
)
|
||||
margin_rate = fields.Float(
|
||||
string="Margin Rate", readonly=True, store=True,
|
||||
compute='_compute_margin',
|
||||
digits=(16, 2), help="Margin rate in percentage of the sale price")
|
||||
string="Margin Rate",
|
||||
readonly=True,
|
||||
store=True,
|
||||
compute="_compute_margin",
|
||||
digits=(16, 2),
|
||||
help="Margin rate in percentage of the sale price",
|
||||
)
|
||||
|
||||
@api.depends(
|
||||
'standard_price_company_currency', 'order_id.pricelist_id.currency_id',
|
||||
'order_id.date_order', 'product_uom_qty', 'price_subtotal',
|
||||
'order_id.company_id')
|
||||
"standard_price_company_currency",
|
||||
"order_id.pricelist_id.currency_id",
|
||||
"order_id.date_order",
|
||||
"product_uom_qty",
|
||||
"price_subtotal",
|
||||
"order_id.company_id",
|
||||
)
|
||||
def _compute_margin(self):
|
||||
for line in self:
|
||||
standard_price_sale_cur = 0.0
|
||||
@@ -51,15 +73,15 @@ class SaleOrderLine(models.Model):
|
||||
company_cur = company.currency_id
|
||||
if order_cur and company_cur:
|
||||
date = line.order_id.date_order
|
||||
standard_price_sale_cur =\
|
||||
company_cur._convert(
|
||||
line.standard_price_company_currency, order_cur,
|
||||
company, date)
|
||||
margin_sale_cur =\
|
||||
line.price_subtotal\
|
||||
- line.product_uom_qty * standard_price_sale_cur
|
||||
standard_price_sale_cur = company_cur._convert(
|
||||
line.standard_price_company_currency, order_cur, company, date
|
||||
)
|
||||
margin_sale_cur = (
|
||||
line.price_subtotal - line.product_uom_qty * standard_price_sale_cur
|
||||
)
|
||||
margin_comp_cur = order_cur._convert(
|
||||
margin_sale_cur, company_cur, company, date)
|
||||
margin_sale_cur, company_cur, company, date
|
||||
)
|
||||
if line.price_subtotal:
|
||||
margin_rate = 100 * margin_sale_cur / line.price_subtotal
|
||||
line.standard_price_sale_currency = standard_price_sale_cur
|
||||
@@ -70,59 +92,63 @@ class SaleOrderLine(models.Model):
|
||||
# We want to copy standard_price on sale order line
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
if vals.get('product_id'):
|
||||
pp = self.env['product.product'].browse(vals['product_id'])
|
||||
if vals.get("product_id"):
|
||||
pp = self.env["product.product"].browse(vals["product_id"])
|
||||
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:
|
||||
sale_uom = self.env['uom.uom'].browse(sale_uom_id)
|
||||
sale_uom = self.env["uom.uom"].browse(sale_uom_id)
|
||||
# convert from product UoM to sale UoM
|
||||
std_price = pp.uom_id._compute_price(
|
||||
pp.standard_price, sale_uom)
|
||||
vals['standard_price_company_currency'] = std_price
|
||||
std_price = pp.uom_id._compute_price(pp.standard_price, sale_uom)
|
||||
vals["standard_price_company_currency"] = std_price
|
||||
return super(SaleOrderLine, self).create(vals)
|
||||
|
||||
def write(self, vals):
|
||||
if not 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:
|
||||
# product_uom and product_id are required fields
|
||||
if 'product_id' in vals:
|
||||
pp = self.env['product.product'].browse(vals['product_id'])
|
||||
if "product_id" in vals:
|
||||
pp = self.env["product.product"].browse(vals["product_id"])
|
||||
else:
|
||||
pp = sol.product_id
|
||||
if 'product_uom' in vals:
|
||||
sale_uom = self.env['uom.uom'].browse(
|
||||
vals['product_uom'])
|
||||
if "product_uom" in vals:
|
||||
sale_uom = self.env["uom.uom"].browse(vals["product_uom"])
|
||||
else:
|
||||
sale_uom = sol.product_uom
|
||||
std_price = pp.standard_price
|
||||
if sale_uom != pp.uom_id:
|
||||
std_price = pp.uom_id._compute_price(std_price, sale_uom)
|
||||
sol.write({'standard_price_company_currency': std_price})
|
||||
sol.write({"standard_price_company_currency": std_price})
|
||||
return super(SaleOrderLine, self).write(vals)
|
||||
|
||||
|
||||
class SaleOrder(models.Model):
|
||||
_inherit = 'sale.order'
|
||||
_inherit = "sale.order"
|
||||
|
||||
# Also defined in bi_sale_company_currency
|
||||
company_currency_id = fields.Many2one(
|
||||
related='company_id.currency_id', store=True,
|
||||
string="Company Currency")
|
||||
related="company_id.currency_id", store=True, string="Company Currency"
|
||||
)
|
||||
margin_sale_currency = fields.Monetary(
|
||||
string='Margin in Sale Currency',
|
||||
currency_field='currency_id',
|
||||
readonly=True, compute='_compute_margin', store=True)
|
||||
string="Margin in Sale Currency",
|
||||
currency_field="currency_id",
|
||||
readonly=True,
|
||||
compute="_compute_margin",
|
||||
store=True,
|
||||
)
|
||||
margin_company_currency = fields.Monetary(
|
||||
string='Margin in Company Currency',
|
||||
currency_field='company_currency_id',
|
||||
readonly=True, compute='_compute_margin', store=True)
|
||||
string="Margin in Company Currency",
|
||||
currency_field="company_currency_id",
|
||||
readonly=True,
|
||||
compute="_compute_margin",
|
||||
store=True,
|
||||
)
|
||||
|
||||
@api.depends(
|
||||
'order_line.margin_sale_currency',
|
||||
'order_line.margin_company_currency')
|
||||
"order_line.margin_sale_currency", "order_line.margin_company_currency"
|
||||
)
|
||||
def _compute_margin(self):
|
||||
for order in self:
|
||||
margin_sale_cur = 0.0
|
||||
|
||||
@@ -6,14 +6,16 @@ from odoo import fields, models
|
||||
|
||||
|
||||
class SaleReport(models.Model):
|
||||
_inherit = 'sale.report'
|
||||
_inherit = "sale.report"
|
||||
|
||||
margin = fields.Float(string='Margin', readonly=True)
|
||||
margin = fields.Float(string="Margin", readonly=True)
|
||||
|
||||
def _query(self, with_clause='', fields={}, groupby='', from_clause=''):
|
||||
fields['margin_company_currency'] =\
|
||||
", SUM(l.margin_company_currency) AS margin"
|
||||
def _query(self, with_clause="", fields={}, groupby="", from_clause=""):
|
||||
fields["margin_company_currency"] = ", SUM(l.margin_company_currency) AS margin"
|
||||
res = super(SaleReport, self)._query(
|
||||
with_clause=with_clause, fields=fields, groupby=groupby,
|
||||
from_clause=from_clause)
|
||||
with_clause=with_clause,
|
||||
fields=fields,
|
||||
groupby=groupby,
|
||||
from_clause=from_clause,
|
||||
)
|
||||
return res
|
||||
|
||||
@@ -1,36 +1,44 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<!--
|
||||
Copyright (C) 2015-2019 Akretion (http://www.akretion.com/)
|
||||
@author: Alexis de Lattre <alexis.delattre@akretion.com>
|
||||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
-->
|
||||
|
||||
<odoo>
|
||||
|
||||
|
||||
<record id="view_order_form" model="ir.ui.view">
|
||||
<field name="name">margin.sale.order.form</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">
|
||||
<group name="technical" position="inside">
|
||||
<field name="margin_sale_currency" string="Margin"
|
||||
groups="account.group_account_user"/>
|
||||
<field name="margin_company_currency"
|
||||
groups="account.group_account_user"/>
|
||||
<field name="company_currency_id" invisible="1"/>
|
||||
<field
|
||||
name="margin_sale_currency"
|
||||
string="Margin"
|
||||
groups="account.group_account_user"
|
||||
/>
|
||||
<field name="margin_company_currency" groups="account.group_account_user" />
|
||||
<field name="company_currency_id" invisible="1" />
|
||||
</group>
|
||||
<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_company_currency" groups="base.group_no_one"/>
|
||||
<field name="margin_sale_currency" groups="base.group_no_one"/>
|
||||
<field name="margin_company_currency" groups="base.group_no_one"/>
|
||||
<label for="margin_rate"/>
|
||||
<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_company_currency" groups="base.group_no_one" />
|
||||
<field name="margin_sale_currency" groups="base.group_no_one" />
|
||||
<field name="margin_company_currency" groups="base.group_no_one" />
|
||||
<label for="margin_rate" />
|
||||
<div name="margin_rate">
|
||||
<field name="margin_rate" groups="base.group_no_one" class="oe_inline"/> %
|
||||
<field
|
||||
name="margin_rate"
|
||||
groups="base.group_no_one"
|
||||
class="oe_inline"
|
||||
/> %
|
||||
</div>
|
||||
<field name="company_currency_id" invisible="1"/>
|
||||
<field name="currency_id" invisible="1"/>
|
||||
<field name="company_currency_id" invisible="1" />
|
||||
<field name="currency_id" invisible="1" />
|
||||
</xpath>
|
||||
</field>
|
||||
</record>
|
||||
@@ -41,10 +49,13 @@
|
||||
<field name="inherit_id" ref="sale.view_order_line_tree" />
|
||||
<field name="arch" type="xml">
|
||||
<field name="price_subtotal" position="after">
|
||||
<field name="standard_price_company_currency" groups="account.group_account_user"/>
|
||||
<field name="margin_company_currency" groups="account.group_account_user"/>
|
||||
<field name="margin_rate" groups="account.group_account_user"/>
|
||||
<field name="company_currency_id" invisible="1"/>
|
||||
<field
|
||||
name="standard_price_company_currency"
|
||||
groups="account.group_account_user"
|
||||
/>
|
||||
<field name="margin_company_currency" groups="account.group_account_user" />
|
||||
<field name="margin_rate" groups="account.group_account_user" />
|
||||
<field name="company_currency_id" invisible="1" />
|
||||
</field>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
Reference in New Issue
Block a user