~account-core-editors/account-financial-tools/7.0

« back to all changes in this revision

Viewing changes to account_compute_tax_amount/account_move_line.py

  • Committer: nicolas.bessi at camptocamp
  • Author(s): vincent.renaville at camptocamp
  • Date: 2013-08-28 10:04:49 UTC
  • mfrom: (113.3.9 7.0_add_account_compute_tax_amount)
  • Revision ID: nicolas.bessi@camptocamp.com-20130828100449-3apzp50rmd7demrt
[ADD] account_compute_tax_amount module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
# Copyright (c) 2013 Camptocamp (http://www.camptocamp.com)
 
5
# All Right Reserved
 
6
#
 
7
# Author : Vincent Renaville (Camptocamp)
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU Affero General Public License as
 
11
#    published by the Free Software Foundation, either version 3 of the
 
12
#    License, or (at your option) any later version.
 
13
#
 
14
#    This program is distributed in the hope that it will be useful,
 
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#    GNU Affero General Public License for more details.
 
18
#
 
19
#    You should have received a copy of the GNU Affero General Public License
 
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
#
 
22
##############################################################################
 
23
 
 
24
from openerp.osv import orm, fields
 
25
import openerp.addons.decimal_precision as dp
 
26
 
 
27
 
 
28
class account_move_line(orm.Model):
 
29
    _inherit = "account.move.line"
 
30
 
 
31
    # We set the tax_amount invisible, because we recompute it in every case.
 
32
    _columns = {
 
33
        'tax_amount': fields.float('Tax/Base Amount',
 
34
                                   digits_compute=dp.get_precision('Account'),
 
35
                                   invisible=True,
 
36
                                   select=True,
 
37
                                   help="If the Tax account is a tax code account, "
 
38
                                        "this field will contain the taxed amount. "
 
39
                                        "If the tax account is base tax code, "
 
40
                                        "this field will contain the basic amount (without tax)."),
 
41
    }
 
42
 
 
43
    def create(self, cr, uid, vals, context=None, check=True):
 
44
        result = super(account_move_line, self).create(cr, uid, vals,
 
45
                                                       context=context,
 
46
                                                       check=check)
 
47
        if result:
 
48
            move_line = self.read(cr, uid, result,
 
49
                                  ['credit', 'debit', 'tax_code_id'],
 
50
                                  context=context)
 
51
            if move_line['tax_code_id']:
 
52
                tax_amount = move_line['credit'] - move_line['debit']
 
53
                self.write(cr, uid, [result],
 
54
                           {'tax_amount': tax_amount},
 
55
                           context=context)
 
56
        return result
 
57
 
 
58
    def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
 
59
        result = super(account_move_line,self).write(cr, uid, ids, vals,
 
60
                                                     context=context,
 
61
                                                     check=check,
 
62
                                                     update_check=update_check)
 
63
        if result:
 
64
            if ('debit' in vals) or ('credit' in vals):
 
65
                move_lines = self.read(cr, uid, ids,
 
66
                                       ['credit', 'debit', 'tax_code_id'],
 
67
                                       context=context)
 
68
                for move_line in move_lines:
 
69
                    if move_line['tax_code_id']:
 
70
                        tax_amount = move_line['credit'] - move_line['debit']
 
71
                        self.write(cr, uid,
 
72
                                   [move_line['id']],
 
73
                                   {'tax_amount': tax_amount},
 
74
                                   context=context)
 
75
 
 
76
        return result