1
# -*- encoding: utf-8 -*-
2
##############################################################################
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>
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.
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.
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/>.
24
##############################################################################
26
from osv import fields, osv
27
from tools.translate import _
29
class account_move(osv.osv):
30
_inherit = "account.move"
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),
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):
40
Create the reversal of a move
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
49
@return: Returns the id of the created reversal move
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
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
62
reversal_ref = ''.join([x for x in [move_prefix, move.ref] if x])
63
reversal_move_id = self.copy(cr, uid, move.id,
65
'date': reversal_date,
66
'period_id': reversal_period_id,
68
'journal_id': reversal_journal_id,
69
'to_be_reversed': False,
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
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],
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,
89
check=True, update_check=True
92
self.validate(cr, uid, [reversal_move_id], context=context)
93
return reversal_move_id
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):
98
Create the reversal of one or multiple moves
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
106
@return: Returns a list of ids of the created reversal moves
108
if isinstance(ids, (int, long)):
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
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)
120
reversed_move_ids.append(reversal_move_id)
122
return reversed_move_ids