~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to account_reversal/account_reversal.py

  • Committer: Guewen Baconnier @ Camptocamp
  • Date: 2012-01-31 12:53:19 UTC
  • Revision ID: guewen.baconnier@camptocamp.com-20120131125319-t1tyraqlb4f2u3l6
[ADD] port account_reversal from v6.0 and add features : to_be_reversed flag and reversal_id many2one

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    Account reversal module for OpenERP
 
5
#    Copyright (C) 2011 Akretion (http://www.akretion.com). All Rights Reserved
 
6
#    @author Alexis de Lattre <alexis.delattre@akretion.com>
 
7
#    with the kind advice of Nicolas Bessi from Camptocamp
 
8
#    Copyright (C) 2012 Camptocamp SA (http://www.camptocamp.com). All Rights Reserved
 
9
#    @author Guewen Baconnier <guewen.baconnier@camptocamp.com>
 
10
#
 
11
#    This program is free software: you can redistribute it and/or modify
 
12
#    it under the terms of the GNU Affero General Public License as
 
13
#    published by the Free Software Foundation, either version 3 of the
 
14
#    License, or (at your option) any later version.
 
15
#
 
16
#    This program is distributed in the hope that it will be useful,
 
17
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
#    GNU Affero General Public License for more details.
 
20
#
 
21
#    You should have received a copy of the GNU Affero General Public License
 
22
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
23
#
 
24
##############################################################################
 
25
 
 
26
from osv import fields, osv
 
27
from tools.translate import _
 
28
 
 
29
class account_move(osv.osv):
 
30
    _inherit = "account.move"
 
31
 
 
32
    _columns = {
 
33
        'to_be_reversed': fields.boolean('To Be Reversed', help='Check this box if your entry has to be reversed at the end of period.'),
 
34
        'reversal_id': fields.many2one('account.move', 'Reversal Entry', ondelete='set null', readonly=True),
 
35
    }
 
36
 
 
37
    def _move_reversal(self, cr, uid, move, reversal_date, reversal_period_id=False, reversal_journal_id=False,
 
38
                       move_prefix=False, move_line_prefix=False, context=None):
 
39
        """
 
40
        Create the reversal of a move
 
41
 
 
42
        @param move: browse instance of the move to reverse
 
43
        @param reversal_date: when the reversal must be input
 
44
        @param reversal_period_id: facultative period to write on the move (use the period of the date if empty
 
45
        @param reversal_journal_id: facultative journal in which create the move
 
46
        @param move_prefix: prefix for the move's name
 
47
        @param move_line_prefix: prefix for the move line's names
 
48
 
 
49
        @return: Returns the id of the created reversal move
 
50
        """
 
51
        if context is None: context = {}
 
52
        move_line_obj = self.pool.get('account.move.line')
 
53
        period_obj = self.pool.get('account.period')
 
54
        period_ctx = context.copy()
 
55
        period_ctx['company_id'] = move.company_id.id
 
56
 
 
57
        if not reversal_period_id:
 
58
            reversal_period_id = period_obj.find(cr, uid, reversal_date, context=period_ctx)[0]
 
59
        if not reversal_journal_id:
 
60
            reversal_journal_id = move.journal_id.id
 
61
 
 
62
        reversal_ref = ''.join([x for x in [move_prefix, move.ref] if x])
 
63
        reversal_move_id = self.copy(cr, uid, move.id,
 
64
                                     default={
 
65
                                         'date': reversal_date,
 
66
                                         'period_id': reversal_period_id,
 
67
                                         'ref': reversal_ref,
 
68
                                         'journal_id': reversal_journal_id,
 
69
                                         'to_be_reversed': False,
 
70
                                     },
 
71
                                     context=context)
 
72
 
 
73
        self.write(cr, uid, [move.id],
 
74
                   {'reversal_id': reversal_move_id,
 
75
                    'to_be_reversed': True},  # ensure to_be_reversed is true if ever it was not
 
76
                   context=context)
 
77
 
 
78
        reversal_move = self.browse(cr, uid, reversal_move_id, context=context)
 
79
        for reversal_move_line in reversal_move.line_id:
 
80
            reversal_ml_name = ' '.join([x for x in [move_line_prefix, reversal_move_line.name] if x])
 
81
            move_line_obj.write(cr, uid, [reversal_move_line.id],
 
82
                                {
 
83
                                    'debit': reversal_move_line.credit,
 
84
                                    'credit': reversal_move_line.debit,
 
85
                                    'amount_currency': reversal_move_line.amount_currency * -1,
 
86
                                    'name': reversal_ml_name,
 
87
                                },
 
88
                                context=context,
 
89
                                check=True, update_check=True
 
90
                                )
 
91
 
 
92
        self.validate(cr, uid, [reversal_move_id], context=context)
 
93
        return reversal_move_id
 
94
 
 
95
    def create_reversals(self, cr, uid, ids, reversal_date, reversal_period_id=False, reversal_journal_id=False,
 
96
                         move_prefix=False, move_line_prefix=False, context=None):
 
97
        """
 
98
        Create the reversal of one or multiple moves
 
99
 
 
100
        @param reversal_date: when the reversal must be input
 
101
        @param reversal_period_id: facultative period to write on the move (use the period of the date if empty
 
102
        @param reversal_journal_id: facultative journal in which create the move
 
103
        @param move_prefix: prefix for the move's name
 
104
        @param move_line_prefix: prefix for the move line's names
 
105
 
 
106
        @return: Returns a list of ids of the created reversal moves
 
107
        """
 
108
        if isinstance(ids, (int, long)):
 
109
            ids = [ids]
 
110
 
 
111
        reversed_move_ids = []
 
112
        for src_move in self.browse(cr, uid, ids, context=context):
 
113
            if src_move.reversal_id:
 
114
                continue  # skip the reversal creation if already done
 
115
 
 
116
            reversal_move_id = self._move_reversal(cr, uid, src_move, reversal_date, reversal_period_id=reversal_period_id,
 
117
                                                   reversal_journal_id=reversal_journal_id, move_prefix=move_prefix,
 
118
                                                   move_line_prefix=move_line_prefix, context=context)
 
119
            if reversal_move_id:
 
120
                reversed_move_ids.append(reversal_move_id)
 
121
 
 
122
        return reversed_move_ids
 
123
 
 
124
account_move()