[IMP] python formatting, OpenERP standards

This commit is contained in:
Guewen Baconnier
2013-01-04 12:11:41 +01:00
parent 1baac8e00d
commit 31e0f499ed
2 changed files with 129 additions and 82 deletions

View File

@@ -5,7 +5,7 @@
# Copyright (C) 2011 Akretion (http://www.akretion.com). All Rights Reserved # Copyright (C) 2011 Akretion (http://www.akretion.com). All Rights Reserved
# @author Alexis de Lattre <alexis.delattre@akretion.com> # @author Alexis de Lattre <alexis.delattre@akretion.com>
# with the kind advice of Nicolas Bessi from Camptocamp # with the kind advice of Nicolas Bessi from Camptocamp
# Copyright (C) 2012 Camptocamp SA (http://www.camptocamp.com). All Rights Reserved # Copyright (C) 2012-2013 Camptocamp SA (http://www.camptocamp.com)
# @author Guewen Baconnier <guewen.baconnier@camptocamp.com> # @author Guewen Baconnier <guewen.baconnier@camptocamp.com>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
@@ -23,39 +23,52 @@
# #
############################################################################## ##############################################################################
from osv import fields, osv from openerp.osv import fields, orm
from tools.translate import _
class account_move(osv.osv):
class account_move(orm.Model):
_inherit = "account.move" _inherit = "account.move"
_columns = { _columns = {
'to_be_reversed': fields.boolean('To Be Reversed', help='Check this box if your entry has to be reversed at the end of period.'), 'to_be_reversed': fields.boolean(
'reversal_id': fields.many2one('account.move', 'Reversal Entry', ondelete='set null', readonly=True), 'To Be Reversed',
help='Check this box if your entry has to be'
'reversed at the end of period.'),
'reversal_id': fields.many2one(
'account.move',
'Reversal Entry',
ondelete='set null',
readonly=True),
} }
def _move_reversal(self, cr, uid, move, reversal_date, reversal_period_id=False, reversal_journal_id=False, def _move_reversal(self, cr, uid, move, reversal_date,
move_prefix=False, move_line_prefix=False, context=None): reversal_period_id=False, reversal_journal_id=False,
move_prefix=False, move_line_prefix=False,
context=None):
""" """
Create the reversal of a move Create the reversal of a move
@param move: browse instance of the move to reverse :param move: browse instance of the move to reverse
@param reversal_date: when the reversal must be input :param reversal_date: when the reversal must be input
@param reversal_period_id: facultative period to write on the move (use the period of the date if empty :param reversal_period_id: facultative period to write on the move
@param reversal_journal_id: facultative journal in which create the move (use the period of the date if empty
@param move_prefix: prefix for the move's name :param reversal_journal_id: facultative journal on which create
@param move_line_prefix: prefix for the move line's names the move
:param move_prefix: prefix for the move's name
:param move_line_prefix: prefix for the move line's names
@return: Returns the id of the created reversal move :return: Returns the id of the created reversal move
""" """
if context is None: context = {} if context is None:
context = {}
move_line_obj = self.pool.get('account.move.line') move_line_obj = self.pool.get('account.move.line')
period_obj = self.pool.get('account.period') period_obj = self.pool.get('account.period')
period_ctx = context.copy() period_ctx = context.copy()
period_ctx['company_id'] = move.company_id.id period_ctx['company_id'] = move.company_id.id
if not reversal_period_id: if not reversal_period_id:
reversal_period_id = period_obj.find(cr, uid, reversal_date, context=period_ctx)[0] reversal_period_id = period_obj.find(
cr, uid, reversal_date, context=period_ctx)[0]
if not reversal_journal_id: if not reversal_journal_id:
reversal_journal_id = move.journal_id.id reversal_journal_id = move.journal_id.id
@@ -72,38 +85,47 @@ class account_move(osv.osv):
self.write(cr, uid, [move.id], self.write(cr, uid, [move.id],
{'reversal_id': reversal_move_id, {'reversal_id': reversal_move_id,
'to_be_reversed': True}, # ensure to_be_reversed is true if ever it was not # ensure to_be_reversed is true if ever it was not
'to_be_reversed': True},
context=context) context=context)
reversal_move = self.browse(cr, uid, reversal_move_id, context=context) reversal_move = self.browse(cr, uid, reversal_move_id, context=context)
for reversal_move_line in reversal_move.line_id: for reversal_move_line in reversal_move.line_id:
reversal_ml_name = ' '.join([x for x in [move_line_prefix, reversal_move_line.name] if x]) reversal_ml_name = ' '.join(
move_line_obj.write(cr, uid, [reversal_move_line.id], [x for x
{ in [move_line_prefix, reversal_move_line.name]
'debit': reversal_move_line.credit, if x])
'credit': reversal_move_line.debit, move_line_obj.write(
'amount_currency': reversal_move_line.amount_currency * -1, cr,
'name': reversal_ml_name, uid,
}, [reversal_move_line.id],
context=context, {'debit': reversal_move_line.credit,
check=True, update_check=True 'credit': reversal_move_line.debit,
) 'amount_currency': reversal_move_line.amount_currency * -1,
'name': reversal_ml_name},
context=context,
check=True,
update_check=True)
self.validate(cr, uid, [reversal_move_id], context=context) self.validate(cr, uid, [reversal_move_id], context=context)
return reversal_move_id return reversal_move_id
def create_reversals(self, cr, uid, ids, reversal_date, reversal_period_id=False, reversal_journal_id=False, def create_reversals(self, cr, uid, ids, reversal_date,
move_prefix=False, move_line_prefix=False, context=None): reversal_period_id=False, reversal_journal_id=False,
move_prefix=False, move_line_prefix=False,
context=None):
""" """
Create the reversal of one or multiple moves Create the reversal of one or multiple moves
@param reversal_date: when the reversal must be input :param reversal_date: when the reversal must be input
@param reversal_period_id: facultative period to write on the move (use the period of the date if empty :param reversal_period_id: facultative period to write on the move
@param reversal_journal_id: facultative journal in which create the move (use the period of the date if empty
@param move_prefix: prefix for the move's name :param reversal_journal_id: facultative journal on which create
@param move_line_prefix: prefix for the move line's names the move
:param move_prefix: prefix for the move's name
:param move_line_prefix: prefix for the move line's names
@return: Returns a list of ids of the created reversal moves :return: Returns a list of ids of the created reversal moves
""" """
if isinstance(ids, (int, long)): if isinstance(ids, (int, long)):
ids = [ids] ids = [ids]
@@ -113,12 +135,17 @@ class account_move(osv.osv):
if src_move.reversal_id: if src_move.reversal_id:
continue # skip the reversal creation if already done continue # skip the reversal creation if already done
reversal_move_id = self._move_reversal(cr, uid, src_move, reversal_date, reversal_period_id=reversal_period_id, reversal_move_id = self._move_reversal(
reversal_journal_id=reversal_journal_id, move_prefix=move_prefix, cr, uid,
move_line_prefix=move_line_prefix, context=context) src_move,
reversal_date,
reversal_period_id=reversal_period_id,
reversal_journal_id=reversal_journal_id,
move_prefix=move_prefix,
move_line_prefix=move_line_prefix,
context=context)
if reversal_move_id: if reversal_move_id:
reversed_move_ids.append(reversal_move_id) reversed_move_ids.append(reversal_move_id)
return reversed_move_ids return reversed_move_ids
account_move()

View File

@@ -23,68 +23,88 @@
# #
############################################################################## ##############################################################################
from osv import osv, fields from openerp.osv import orm, fields
from tools.translate import _ from openerp.tools.translate import _
class account_move_reversal(osv.osv_memory): class account_move_reversal(orm.TransientModel):
_name = "account.move.reverse" _name = "account.move.reverse"
_description = "Create reversal of account moves" _description = "Create reversal of account moves"
_columns = { _columns = {
'date': fields.date('Reversal Date', required=True, help="Enter the date of the reversal account entries. By default, OpenERP proposes the first day of the next period."), 'date': fields.date(
'period_id': fields.many2one('account.period', 'Reversal Period', help="If empty, take the period of the date."), 'Reversal Date',
'journal_id': fields.many2one('account.journal', 'Reversal Journal', help='If empty, uses the journal of the journal entry to be reversed.'), required=True,
'move_prefix': fields.char('Entries Ref. Prefix', size=32, help="Prefix that will be added to the 'Ref' of the journal entry to be reversed to create the 'Ref' of the reversal journal entry (no space added after the prefix)."), help="Enter the date of the reversal account entries. "
'move_line_prefix': fields.char('Items Name Prefix', size=32, help="Prefix that will be added to the name of the journal item to be reversed to create the name of the reversal journal item (a space is added after the prefix)."), "By default, OpenERP proposes the first day of "
"the next period."),
'period_id': fields.many2one(
'account.period',
'Reversal Period',
help="If empty, take the period of the date."),
'journal_id': fields.many2one(
'account.journal',
'Reversal Journal',
help='If empty, uses the journal of the journal entry '
'to be reversed.'),
'move_prefix': fields.char(
'Entries Ref. Prefix',
size=32,
help="Prefix that will be added to the 'Ref' of the journal "
"entry to be reversed to create the 'Ref' of the "
"reversal journal entry (no space added after the prefix)."),
'move_line_prefix': fields.char(
'Items Name Prefix',
size=32,
help="Prefix that will be added to the name of the journal "
"item to be reversed to create the name of the reversal "
"journal item (a space is added after the prefix)."),
} }
def _next_period_first_date(self, cr, uid, context=None): def _next_period_first_date(self, cr, uid, context=None):
if context is None:
context = {}
period_obj = self.pool.get('account.period') period_obj = self.pool.get('account.period')
current_period_id = period_obj.find(cr, uid, context=context)[0] current_period_id = period_obj.find(cr, uid, context=context)[0]
current_period = period_obj.browse(cr, uid, current_period_id, context=context) current_period = period_obj.browse(
next_period_id = period_obj.next(cr, uid, current_period, 1, context=context) cr, uid, current_period_id, context=context)
next_period = period_obj.browse(cr, uid, next_period_id, context=context) next_period_id = period_obj.next(
cr, uid, current_period, 1, context=context)
next_period = period_obj.browse(
cr, uid, next_period_id, context=context)
return next_period.date_start return next_period.date_start
_defaults = { _defaults = {
'date': _next_period_first_date, 'date': _next_period_first_date,
'move_line_prefix': lambda *a: 'REV -', 'move_line_prefix': 'REV -',
} }
def action_reverse(self, cr, uid, ids, context=None): def action_reverse(self, cr, uid, ids, context=None):
if context is None: if context is None:
context = {} context = {}
assert 'active_ids' in context, "active_ids missing in context"
form = self.read(cr, uid, ids, context=context)[0] form = self.read(cr, uid, ids, context=context)[0]
action = {'type': 'ir.actions.act_window_close'} mod_obj = self.pool.get('ir.model.data')
act_obj = self.pool.get('ir.actions.act_window')
move_obj = self.pool.get('account.move')
move_ids = context['active_ids']
if context.get('active_ids', False): period_id = form['period_id'][0] if form.get('period_id') else False
mod_obj = self.pool.get('ir.model.data') journal_id = form['journal_id'][0] if form.get('journal_id') else False
act_obj = self.pool.get('ir.actions.act_window') reversed_move_ids = move_obj.create_reversals(
move_obj = self.pool.get('account.move') cr, uid,
move_ids = context['active_ids'] move_ids,
form['date'],
reversal_period_id=period_id,
reversal_journal_id=journal_id,
move_prefix=form['move_prefix'],
move_line_prefix=form['move_line_prefix'],
context=context)
period_id = form.get('period_id', False) and form['period_id'][0] or False __, action_id = mod_obj.get_object_reference(
journal_id = form.get('journal_id', False) and form['journal_id'][0] or False cr, uid, 'account', 'action_move_journal_line')
reversed_move_ids = move_obj.create_reversals( action = act_obj.read(cr, uid, [action_id], context=context)[0]
cr, uid, action['domain'] = unicode([('id', 'in', reversed_move_ids)])
move_ids, action['name'] = _('Reversal Entries')
form['date'], action['context'] = unicode({'search_default_to_be_reversed': 0})
reversal_period_id=period_id,
reversal_journal_id=journal_id,
move_prefix=form['move_prefix'],
move_line_prefix=form['move_line_prefix'],
context=context)
action_ref = mod_obj.get_object_reference(cr, uid, 'account', 'action_move_journal_line')
action_id = action_ref and action_ref[1] or False
action = act_obj.read(cr, uid, [action_id], context=context)[0]
action['domain'] = str([('id', 'in', reversed_move_ids)])
action['name'] = _('Reversal Entries')
action['context'] = unicode({'search_default_to_be_reversed': 0})
return action return action
account_move_reversal()