Port account_invoice_margin to v10

This commit is contained in:
Alexis de Lattre
2017-10-17 00:11:51 +02:00
parent e6076c3f76
commit fcecfcc4e1
4 changed files with 81 additions and 126 deletions

View File

@@ -1,3 +1,3 @@
# -*- encoding: utf-8 -*- # -*- coding: utf-8 -*-
from . import account_invoice from . import account_invoice

View File

@@ -1,29 +1,10 @@
# -*- encoding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################## # © 2015-2017 Akretion (http://www.akretion.com)
# # @author Alexis de Lattre <alexis.delattre@akretion.com>
# Account Invoice Margin module for Odoo
# Copyright (C) 2015 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# 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': 'Account Invoice Margin', 'name': 'Account Invoice Margin',
'version': '0.1', 'version': '10.0.1.0.0',
'category': 'Accounting & Finance', 'category': 'Accounting & Finance',
'license': 'AGPL-3', 'license': 'AGPL-3',
'summary': 'Copy standard price on invoice line and compute margins', 'summary': 'Copy standard price on invoice line and compute margins',
@@ -39,5 +20,5 @@ This module has been written by Alexis de Lattre from Akretion
'data': [ 'data': [
'account_invoice_view.xml', 'account_invoice_view.xml',
], ],
'installable': False, 'installable': True,
} }

View File

@@ -1,27 +1,10 @@
# -*- encoding: utf-8 -*- # -*- coding: utf-8 -*-
############################################################################## # © 2015-2017 Akretion (http://www.akretion.com)
# # @author Alexis de Lattre <alexis.delattre@akretion.com>
# Account Invoice Margin module for Odoo # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
# Copyright (C) 2015 Akretion (http://www.akretion.com)
# @author Alexis de Lattre <alexis.delattre@akretion.com>
#
# 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 from odoo import models, fields, api
import openerp.addons.decimal_precision as dp import odoo.addons.decimal_precision as dp
class AccountInvoiceLine(models.Model): class AccountInvoiceLine(models.Model):
@@ -39,56 +22,53 @@ class AccountInvoiceLine(models.Model):
digits=dp.get_precision('Product Price'), digits=dp.get_precision('Product Price'),
help="Cost price in invoice currency in the unit of measure " help="Cost price in invoice currency in the unit of measure "
"of the invoice line") "of the invoice line")
margin_invoice_currency = fields.Float( margin_invoice_currency = fields.Monetary(
string='Margin in Invoice Currency', readonly=True, store=True, string='Margin in Invoice 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 Rate", 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', 'invoice_id.currency_id', 'standard_price_company_currency', 'invoice_id.currency_id',
'invoice_id.type', 'invoice_id.company_id', 'invoice_id.type', 'invoice_id.company_id',
'invoice_id.date_invoice', 'quantity', 'price_subtotal') 'invoice_id.date_invoice', 'quantity', 'price_subtotal')
def _compute_margin(self): def _compute_margin(self):
standard_price_inv_cur = 0.0 for il in self:
margin_inv_cur = 0.0 standard_price_inv_cur = 0.0
margin_comp_cur = 0.0 margin_inv_cur = 0.0
margin_rate = 0.0 margin_comp_cur = 0.0
if ( margin_rate = 0.0
self.invoice_id and inv = il.invoice_id
self.invoice_id.type in ('out_invoice', 'out_refund')): if inv and inv.type in ('out_invoice', 'out_refund'):
# it works in _get_current_rate # it works in _get_current_rate
# even if we set date = False in context # even if we set date = False in context
# standard_price_inv_cur is in the UoM of the invoice line # standard_price_inv_cur is in the UoM of the invoice line
standard_price_inv_cur =\ standard_price_inv_cur =\
self.invoice_id.company_id.currency_id.with_context( inv.company_id.currency_id.with_context(
date=self.invoice_id.date_invoice).compute( date=inv.date_invoice).compute(
self.standard_price_company_currency, il.standard_price_company_currency,
self.invoice_id.currency_id) inv.currency_id)
margin_inv_cur =\ margin_inv_cur =\
self.price_subtotal - self.quantity * standard_price_inv_cur il.price_subtotal - il.quantity * standard_price_inv_cur
margin_comp_cur = self.invoice_id.currency_id.with_context( margin_comp_cur = inv.currency_id.with_context(
date=self.invoice_id.date_invoice).compute( date=inv.date_invoice).compute(
margin_inv_cur, self.invoice_id.company_id.currency_id) margin_inv_cur, inv.company_id.currency_id)
if self.price_subtotal: if il.price_subtotal:
margin_rate = 100 * margin_inv_cur / self.price_subtotal margin_rate = 100 * margin_inv_cur / il.price_subtotal
# for a refund, margin should be negative # for a refund, margin should be negative
# but margin rate should stay positive # but margin rate should stay positive
if self.invoice_id.type == 'out_refund': if inv.type == 'out_refund':
margin_inv_cur *= -1 margin_inv_cur *= -1
margin_comp_cur *= -1 margin_comp_cur *= -1
self.standard_price_invoice_currency = standard_price_inv_cur il.standard_price_invoice_currency = standard_price_inv_cur
self.margin_invoice_currency = margin_inv_cur il.margin_invoice_currency = margin_inv_cur
self.margin_company_currency = margin_comp_cur il.margin_company_currency = margin_comp_cur
self.margin_rate = margin_rate il.margin_rate = margin_rate
# We want to copy standard_price on invoice line for customer # We want to copy standard_price on invoice line for customer
# invoice/refunds. We can't do that via on_change of product_id, # invoice/refunds. We can't do that via on_change of product_id,
@@ -101,7 +81,7 @@ class AccountInvoiceLine(models.Model):
if vals.get('product_id'): if vals.get('product_id'):
pp = self.env['product.product'].browse(vals['product_id']) pp = self.env['product.product'].browse(vals['product_id'])
std_price = pp.standard_price std_price = pp.standard_price
inv_uom_id = vals.get('uos_id') inv_uom_id = vals.get('uom_id')
if inv_uom_id and inv_uom_id != pp.uom_id.id: if inv_uom_id and inv_uom_id != pp.uom_id.id:
std_price = self.env['product.uom']._compute_price( std_price = self.env['product.uom']._compute_price(
pp.uom_id.id, std_price, inv_uom_id) pp.uom_id.id, std_price, inv_uom_id)
@@ -112,7 +92,7 @@ class AccountInvoiceLine(models.Model):
def write(self, vals): def write(self, vals):
if not vals: if not vals:
vals = {} vals = {}
if 'product_id' in vals or 'uos_id' in vals: if 'product_id' in vals or 'uom_id' in vals:
for il in self: for il in self:
if 'product_id' in vals: if 'product_id' in vals:
if vals.get('product_id'): if vals.get('product_id'):
@@ -122,21 +102,21 @@ class AccountInvoiceLine(models.Model):
pp = False pp = False
else: else:
pp = il.product_id or False pp = il.product_id or False
# uos_id is NOT a required field # uom_id is NOT a required field
if 'uos_id' in vals: if 'uom_id' in vals:
if vals.get('uos_id'): if vals.get('uom_id'):
inv_uom = self.env['product.uom'].browse( inv_uom = self.env['product.uom'].browse(
vals['uos_id']) vals['uom_id'])
else: else:
inv_uom = False inv_uom = False
else: else:
inv_uom = il.uos_id or False inv_uom = il.uom_id or False
std_price = 0.0 std_price = 0.0
if pp: if pp:
std_price = pp.standard_price std_price = pp.standard_price
if inv_uom and inv_uom != pp.uom_id: if inv_uom and inv_uom != pp.uom_id:
std_price = self.env['product.uom']._compute_price( std_price = pp.uom_id._compute_price(
pp.uom_id.id, std_price, inv_uom.id) std_price, inv_uom)
il.write({'standard_price_company_currency': std_price}) il.write({'standard_price_company_currency': std_price})
return super(AccountInvoiceLine, self).write(vals) return super(AccountInvoiceLine, self).write(vals)
@@ -144,26 +124,26 @@ class AccountInvoiceLine(models.Model):
class AccountInvoice(models.Model): class AccountInvoice(models.Model):
_inherit = 'account.invoice' _inherit = 'account.invoice'
margin_invoice_currency = fields.Float( margin_invoice_currency = fields.Monetary(
string='Margin in Invoice Currency', string='Margin in Invoice Currency',
readonly=True, compute='_compute_margin', store=True, readonly=True, compute='_compute_margin', store=True,
digits=dp.get_precision('Account')) currency_field='currency_id')
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, readonly=True, compute='_compute_margin', store=True,
digits=dp.get_precision('Account')) currency_field='company_currency_id')
@api.one
@api.depends( @api.depends(
'type', 'type',
'invoice_line.margin_invoice_currency', 'invoice_line_ids.margin_invoice_currency',
'invoice_line.margin_company_currency') 'invoice_line_ids.margin_company_currency')
def _compute_margin(self): def _compute_margin(self):
margin_inv_cur = 0.0 for inv in self:
margin_comp_cur = 0.0 margin_inv_cur = 0.0
if self.type in ('out_invoice', 'out_refund'): margin_comp_cur = 0.0
for il in self.invoice_line: if inv.type in ('out_invoice', 'out_refund'):
margin_inv_cur += il.margin_invoice_currency for il in inv.invoice_line_ids:
margin_comp_cur += il.margin_company_currency margin_inv_cur += il.margin_invoice_currency
self.margin_invoice_currency = margin_inv_cur margin_comp_cur += il.margin_company_currency
self.margin_company_currency = margin_comp_cur inv.margin_invoice_currency = margin_inv_cur
inv.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/) © 2015-2017 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_invoice_line_form" model="ir.ui.view"> <record id="view_invoice_line_form" model="ir.ui.view">
<field name="name">margin.account.invoice.line.form</field> <field name="name">margin.account.invoice.line.form</field>
@@ -21,8 +20,6 @@
options="{'currency_field': 'currency_id'}" options="{'currency_field': 'currency_id'}"
groups="account.group_account_user"/> groups="account.group_account_user"/>
<field name="margin_invoice_currency" <field name="margin_invoice_currency"
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"/>
@@ -37,23 +34,20 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="move_id" position="after"> <field name="move_id" position="after">
<field name="margin_invoice_currency" <field name="margin_invoice_currency"
string="Margin" string="Margin" groups="account.group_account_user"/>
widget="monetary"
options="{'currency_field': 'currency_id'}"
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> </field>
<xpath expr="//field[@name='invoice_line']/tree/field[@name='price_subtotal']" position="after"> <xpath expr="//field[@name='invoice_line_ids']/tree/field[@name='price_subtotal']" position="after">
<field name="standard_price_invoice_currency" groups="base.group_no_one"/> <field name="standard_price_invoice_currency" groups="base.group_no_one" widget="monetary" options="{'currency_field': 'currency_id'}"/>
<field name="standard_price_company_currency" groups="base.group_no_one"/> <field name="standard_price_company_currency" groups="base.group_no_one" widget="monetary" options="{'currency_field': 'company_currency_id'}"/>
<field name="margin_invoice_currency" groups="base.group_no_one"/> <field name="margin_invoice_currency" groups="base.group_no_one"/>
<field name="margin_company_currency" groups="base.group_no_one"/> <field name="margin_company_currency" groups="base.group_no_one"/>
<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>
</data> </odoo>
</openerp>