Merge pull request #78 from guewen/8.0-account_compute_tax_amount-migr

Migrate account_compute_tax_amount to 8.0
This commit is contained in:
Pedro M. Baeza
2015-01-14 12:22:48 +01:00
8 changed files with 70 additions and 84 deletions

View File

@@ -1,81 +0,0 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2013 Camptocamp (http://www.camptocamp.com)
# All Right Reserved
#
# Author : Vincent Renaville (Camptocamp)
#
# 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.osv import orm, fields
import openerp.addons.decimal_precision as dp
class account_move_line(orm.Model):
_inherit = "account.move.line"
# We set the tax_amount invisible, because we recompute it in every case.
_columns = {
'tax_amount': fields.float(
'Tax/Base Amount',
digits_compute=dp.get_precision('Account'),
invisible=True,
select=True,
help="If the Tax account is a tax code account, "
"this field will contain the taxed amount. "
"If the tax account is base tax code, "
"this field will contain the basic amount (without tax)."
),
}
def create(self, cr, uid, vals, context=None, check=True):
result = super(account_move_line, self).create(cr, uid, vals,
context=context,
check=check)
if result:
move_line = self.read(cr, uid, result,
['credit', 'debit', 'tax_code_id'],
context=context)
if move_line['tax_code_id']:
tax_amount = move_line['credit'] - move_line['debit']
self.write(cr, uid, [result],
{'tax_amount': tax_amount},
context=context)
return result
def write(self, cr, uid, ids, vals, context=None, check=True,
update_check=True):
result = super(account_move_line, self).write(
cr, uid, ids, vals,
context=context,
check=check,
update_check=update_check
)
if result:
if ('debit' in vals) or ('credit' in vals):
move_lines = self.read(cr, uid, ids,
['credit', 'debit', 'tax_code_id'],
context=context)
for move_line in move_lines:
if move_line['tax_code_id']:
tax_amount = move_line['credit'] - move_line['debit']
self.write(cr, uid,
[move_line['id']],
{'tax_amount': tax_amount},
context=context)
return result

View File

@@ -0,0 +1,13 @@
Recompute tax amount
====================
The way the tax amount is computed in journal entries in the core
``account`` module is prone to input errors.
This module forces the the tax amount to always be: ``credit - debit``
whatever the configuration of the tax is and whatever the user types in
the tax amount.
**Warning**: there is no guarantee that this module will work for every
country, at least it works for Switzerland and France where the tax
amount is always ``credit - debit``.

View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author Vincent Renaville. Copyright 2013 Camptocamp SA
# Author Vincent Renaville. Copyright 2013-2014 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -23,9 +23,8 @@
"depends": ["base",
"account"],
"author": "Camptocamp",
"description": """Recompute tax_amount to avoid sign problem""",
'website': 'http://www.camptocamp.com',
'data': [],
'installable': False,
'installable': True,
'active': False,
}

View File

@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2013-2014 Camptocamp (http://www.camptocamp.com)
#
# Author : Vincent Renaville (Camptocamp)
#
# 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
class AccountMoveLine(models.Model):
_inherit = "account.move.line"
# We set the tax_amount readonly, because we recompute it in every case.
tax_amount = fields.Float(readonly=True)
@api.one
def force_compute_tax_amount(self):
if self.tax_code_id:
self.tax_amount = self.credit - self.debit
@api.cr_uid_context
def create(self, cr, uid, vals, context=None, check=True):
record_id = super(AccountMoveLine, self).create(cr, uid, vals,
context=context,
check=check)
self.force_compute_tax_amount(cr, uid, [record_id], context=context)
return record_id
@api.cr_uid_ids_context
def write(self, cr, uid, ids, vals, context=None, check=True,
update_check=True):
result = super(AccountMoveLine, self).write(cr, uid, ids, vals,
context=context,
check=check,
update_check=update_check)
if ('debit' in vals) or ('credit' in vals):
self.force_compute_tax_amount(cr, uid, ids, context=context)
return result