[IMP] pre-commit: first run on whole repo

This commit is contained in:
Kevin Khao
2021-11-26 18:54:38 +03:00
parent a04b8980e1
commit 167aefee13
289 changed files with 6020 additions and 4170 deletions

View File

@@ -3,22 +3,22 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Account Invoice Margin',
'version': '12.0.1.0.0',
'category': 'Invoicing Management',
'license': 'AGPL-3',
'summary': 'Copy standard price on invoice line and compute margins',
'description': """
"name": "Account Invoice Margin",
"version": "12.0.1.0.0",
"category": "Invoicing Management",
"license": "AGPL-3",
"summary": "Copy standard price on invoice line and compute margins",
"description": """
This module copies the field *standard_price* of the product on the invoice line when the invoice line is created. The allows the computation of the margin of the invoice.
This module has been written by Alexis de Lattre from Akretion
<alexis.delattre@akretion.com>.
""",
'author': 'Akretion',
'website': 'http://www.akretion.com',
'depends': ['account'],
'data': [
'account_invoice_view.xml',
"author": "Akretion",
"website": "https://github.com/OCA/odoo-usability",
"depends": ["account"],
"data": [
"account_invoice_view.xml",
],
'installable': False,
"installable": False,
}

View File

@@ -3,39 +3,62 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
import odoo.addons.decimal_precision as dp
class AccountInvoiceLine(models.Model):
_inherit = 'account.invoice.line'
_inherit = "account.invoice.line"
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 invoice line (which may be different from the unit "
"of measure of the product).")
"of measure of the product).",
)
standard_price_invoice_currency = fields.Float(
string='Cost Price in Invoice Currency', readonly=True,
compute='_compute_margin', store=True,
digits=dp.get_precision('Product Price'),
string="Cost Price in Invoice Currency",
readonly=True,
compute="_compute_margin",
store=True,
digits=dp.get_precision("Product Price"),
help="Cost price in invoice currency in the unit of measure "
"of the invoice line")
"of the invoice line",
)
margin_invoice_currency = fields.Monetary(
string='Margin in Invoice Currency', readonly=True, store=True,
compute='_compute_margin', currency_field='currency_id')
string="Margin in Invoice 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', 'invoice_id.currency_id',
'invoice_id.type', 'invoice_id.company_id',
'invoice_id.date_invoice', 'quantity', 'price_subtotal')
"standard_price_company_currency",
"invoice_id.currency_id",
"invoice_id.type",
"invoice_id.company_id",
"invoice_id.date_invoice",
"quantity",
"price_subtotal",
)
def _compute_margin(self):
for il in self:
standard_price_inv_cur = 0.0
@@ -43,27 +66,27 @@ class AccountInvoiceLine(models.Model):
margin_comp_cur = 0.0
margin_rate = 0.0
inv = il.invoice_id
if inv and inv.type in ('out_invoice', 'out_refund'):
if inv and inv.type in ("out_invoice", "out_refund"):
# it works in _get_current_rate
# even if we set date = False in context
# standard_price_inv_cur is in the UoM of the invoice line
date = inv._get_currency_rate_date() or\
fields.Date.context_today(self)
date = inv._get_currency_rate_date() or fields.Date.context_today(self)
company = inv.company_id
company_currency = company.currency_id
standard_price_inv_cur =\
company_currency._convert(
il.standard_price_company_currency,
inv.currency_id, company, date)
margin_inv_cur =\
standard_price_inv_cur = company_currency._convert(
il.standard_price_company_currency, inv.currency_id, company, date
)
margin_inv_cur = (
il.price_subtotal - il.quantity * standard_price_inv_cur
)
margin_comp_cur = inv.currency_id._convert(
margin_inv_cur, company_currency, company, date)
margin_inv_cur, company_currency, company, date
)
if il.price_subtotal:
margin_rate = 100 * margin_inv_cur / il.price_subtotal
# for a refund, margin should be negative
# but margin rate should stay positive
if inv.type == 'out_refund':
if inv.type == "out_refund":
margin_inv_cur *= -1
margin_comp_cur *= -1
il.standard_price_invoice_currency = standard_price_inv_cur
@@ -79,35 +102,32 @@ class AccountInvoiceLine(models.Model):
# because we don't have access to the 'type' of the invoice
@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
inv_uom_id = vals.get('uom_id')
inv_uom_id = vals.get("uom_id")
if inv_uom_id and inv_uom_id != pp.uom_id.id:
inv_uom = self.env['uom.uom'].browse(inv_uom_id)
std_price = pp.uom_id._compute_price(
std_price, inv_uom)
vals['standard_price_company_currency'] = std_price
inv_uom = self.env["uom.uom"].browse(inv_uom_id)
std_price = pp.uom_id._compute_price(std_price, inv_uom)
vals["standard_price_company_currency"] = std_price
return super(AccountInvoiceLine, self).create(vals)
def write(self, vals):
if not vals:
vals = {}
if 'product_id' in vals or 'uom_id' in vals:
if "product_id" in vals or "uom_id" in vals:
for il in self:
if 'product_id' in vals:
if vals.get('product_id'):
pp = self.env['product.product'].browse(
vals['product_id'])
if "product_id" in vals:
if vals.get("product_id"):
pp = self.env["product.product"].browse(vals["product_id"])
else:
pp = False
else:
pp = il.product_id or False
# uom_id is NOT a required field
if 'uom_id' in vals:
if vals.get('uom_id'):
inv_uom = self.env['uom.uom'].browse(
vals['uom_id'])
if "uom_id" in vals:
if vals.get("uom_id"):
inv_uom = self.env["uom.uom"].browse(vals["uom_id"])
else:
inv_uom = False
else:
@@ -116,37 +136,43 @@ class AccountInvoiceLine(models.Model):
if pp:
std_price = pp.standard_price
if inv_uom and inv_uom != pp.uom_id:
std_price = pp.uom_id._compute_price(
std_price, inv_uom)
il.write({'standard_price_company_currency': std_price})
std_price = pp.uom_id._compute_price(std_price, inv_uom)
il.write({"standard_price_company_currency": std_price})
return super(AccountInvoiceLine, self).write(vals)
class AccountInvoice(models.Model):
_inherit = 'account.invoice'
_inherit = "account.invoice"
margin_invoice_currency = fields.Monetary(
string='Margin in Invoice Currency',
compute='_compute_margin', store=True, readonly=True,
currency_field='currency_id')
string="Margin in Invoice Currency",
compute="_compute_margin",
store=True,
readonly=True,
currency_field="currency_id",
)
margin_company_currency = fields.Monetary(
string='Margin in Company Currency',
compute='_compute_margin', store=True, readonly=True,
currency_field='company_currency_id')
string="Margin in Company Currency",
compute="_compute_margin",
store=True,
readonly=True,
currency_field="company_currency_id",
)
@api.depends(
'type',
'invoice_line_ids.margin_invoice_currency',
'invoice_line_ids.margin_company_currency')
"type",
"invoice_line_ids.margin_invoice_currency",
"invoice_line_ids.margin_company_currency",
)
def _compute_margin(self):
res = self.env['account.invoice.line'].read_group(
[('invoice_id', 'in', self.ids)],
['invoice_id', 'margin_invoice_currency',
'margin_company_currency'],
['invoice_id'])
res = self.env["account.invoice.line"].read_group(
[("invoice_id", "in", self.ids)],
["invoice_id", "margin_invoice_currency", "margin_company_currency"],
["invoice_id"],
)
for re in res:
if re['invoice_id']:
inv = self.browse(re['invoice_id'][0])
if inv.type in ('out_invoice', 'out_refund'):
inv.margin_invoice_currency = re['margin_invoice_currency']
inv.margin_company_currency = re['margin_company_currency']
if re["invoice_id"]:
inv = self.browse(re["invoice_id"][0])
if inv.type in ("out_invoice", "out_refund"):
inv.margin_invoice_currency = re["margin_invoice_currency"]
inv.margin_company_currency = re["margin_company_currency"]

View File

@@ -6,47 +6,70 @@ from odoo import api, fields, models
class AccountInvoiceReport(models.Model):
_inherit = 'account.invoice.report'
_inherit = "account.invoice.report"
margin = fields.Float(string='Margin', readonly=True)
margin = fields.Float(string="Margin", readonly=True)
# why digits=0 ??? Why is it like that in the native "account" module
user_currency_margin = fields.Float(
string="Margin", compute='_compute_user_currency_margin', digits=0)
string="Margin", compute="_compute_user_currency_margin", digits=0
)
_depends = {
'account.invoice': [
'account_id', 'amount_total_company_signed',
'commercial_partner_id', 'company_id',
'currency_id', 'date_due', 'date_invoice', 'fiscal_position_id',
'journal_id', 'number', 'partner_bank_id', 'partner_id',
'payment_term_id', 'residual', 'state', 'type', 'user_id',
"account.invoice": [
"account_id",
"amount_total_company_signed",
"commercial_partner_id",
"company_id",
"currency_id",
"date_due",
"date_invoice",
"fiscal_position_id",
"journal_id",
"number",
"partner_bank_id",
"partner_id",
"payment_term_id",
"residual",
"state",
"type",
"user_id",
],
'account.invoice.line': [
'account_id', 'invoice_id', 'price_subtotal', 'product_id',
'quantity', 'uom_id', 'account_analytic_id',
'margin_company_currency',
"account.invoice.line": [
"account_id",
"invoice_id",
"price_subtotal",
"product_id",
"quantity",
"uom_id",
"account_analytic_id",
"margin_company_currency",
],
'product.product': ['product_tmpl_id'],
'product.template': ['categ_id'],
'uom.uom': ['category_id', 'factor', 'name', 'uom_type'],
'res.currency.rate': ['currency_id', 'name'],
'res.partner': ['country_id'],
"product.product": ["product_tmpl_id"],
"product.template": ["categ_id"],
"uom.uom": ["category_id", "factor", "name", "uom_type"],
"res.currency.rate": ["currency_id", "name"],
"res.partner": ["country_id"],
}
@api.depends('currency_id', 'date', 'margin')
@api.depends("currency_id", "date", "margin")
def _compute_user_currency_margin(self):
user_currency = self.env.user.company_id.currency_id
currency_rate = self.env['res.currency.rate'].search([
('rate', '=', 1),
'|',
('company_id', '=', self.env.user.company_id.id),
('company_id', '=', False)], limit=1)
currency_rate = self.env["res.currency.rate"].search(
[
("rate", "=", 1),
"|",
("company_id", "=", self.env.user.company_id.id),
("company_id", "=", False),
],
limit=1,
)
base_currency = currency_rate.currency_id
for record in self:
date = record.date or fields.Date.today()
company = record.company_id
record.user_currency_margin = base_currency._convert(
record.margin, user_currency, company, date)
record.margin, user_currency, company, date
)
# TODO check for refunds
def _sub_select(self):

View File

@@ -1,33 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!--
© 2015-2017 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_invoice_line_form" model="ir.ui.view">
<field name="name">margin.account.invoice.line.form</field>
<field name="model">account.invoice.line</field>
<field name="inherit_id" ref="account.view_invoice_line_form"/>
<field name="inherit_id" ref="account.view_invoice_line_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='analytic_tag_ids']/.." position="inside">
<field name="standard_price_company_currency"
string="Cost Price in Comp. Cur."
groups="base.group_no_one"/>
<field name="standard_price_invoice_currency"
string="Cost Price in Inv. Cur."
groups="base.group_no_one"/>
<field name="margin_invoice_currency"
string="Margin in Inv. Cur."
groups="base.group_no_one"/>
<field name="margin_company_currency"
string="Margin in Comp. Cur."
groups="base.group_no_one"/>
<label for="margin_rate" groups="base.group_no_one"/>
<field
name="standard_price_company_currency"
string="Cost Price in Comp. Cur."
groups="base.group_no_one"
/>
<field
name="standard_price_invoice_currency"
string="Cost Price in Inv. Cur."
groups="base.group_no_one"
/>
<field
name="margin_invoice_currency"
string="Margin in Inv. Cur."
groups="base.group_no_one"
/>
<field
name="margin_company_currency"
string="Margin in Comp. Cur."
groups="base.group_no_one"
/>
<label for="margin_rate" groups="base.group_no_one" />
<div name="margin_rate" groups="base.group_no_one">
<field name="margin_rate" class="oe_inline"/> %
<field name="margin_rate" class="oe_inline" /> %
</div>
</xpath>
</field>
@@ -36,13 +43,19 @@
<record id="invoice_form" model="ir.ui.view">
<field name="name">margin.account.invoice.form</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="inherit_id" ref="account.invoice_form" />
<field name="arch" type="xml">
<field name="move_id" position="after">
<field name="margin_invoice_currency"
string="Margin in Inv. Cur." groups="base.group_no_one"/>
<field name="margin_company_currency"
string="Margin in Comp. Cur." groups="base.group_no_one"/>
<field
name="margin_invoice_currency"
string="Margin in Inv. Cur."
groups="base.group_no_one"
/>
<field
name="margin_company_currency"
string="Margin in Comp. Cur."
groups="base.group_no_one"
/>
</field>
</field>
</record>