[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
# @author Alexis de Lattre <alexis.delattre@akretion.com>
# 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>
#
# This program is free software: you can redistribute it and/or modify
@@ -23,39 +23,52 @@
#
##############################################################################
from osv import fields, osv
from tools.translate import _
from openerp.osv import fields, orm
class account_move(osv.osv):
class account_move(orm.Model):
_inherit = "account.move"
_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.'),
'reversal_id': fields.many2one('account.move', 'Reversal Entry', ondelete='set null', readonly=True),
'to_be_reversed': fields.boolean(
'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,
move_prefix=False, move_line_prefix=False, context=None):
def _move_reversal(self, cr, uid, move, reversal_date,
reversal_period_id=False, reversal_journal_id=False,
move_prefix=False, move_line_prefix=False,
context=None):
"""
Create the reversal of a move
@param move: browse instance of the move to reverse
@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_journal_id: facultative journal in which create the move
@param move_prefix: prefix for the move's name
@param move_line_prefix: prefix for the move line's names
:param move: browse instance of the move to reverse
: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_journal_id: facultative journal on which create
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')
period_obj = self.pool.get('account.period')
period_ctx = context.copy()
period_ctx['company_id'] = move.company_id.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:
reversal_journal_id = move.journal_id.id
@@ -72,38 +85,47 @@ class account_move(osv.osv):
self.write(cr, uid, [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)
reversal_move = self.browse(cr, uid, reversal_move_id, context=context)
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])
move_line_obj.write(cr, uid, [reversal_move_line.id],
{
'debit': reversal_move_line.credit,
reversal_ml_name = ' '.join(
[x for x
in [move_line_prefix, reversal_move_line.name]
if x])
move_line_obj.write(
cr,
uid,
[reversal_move_line.id],
{'debit': reversal_move_line.credit,
'credit': reversal_move_line.debit,
'amount_currency': reversal_move_line.amount_currency * -1,
'name': reversal_ml_name,
},
'name': reversal_ml_name},
context=context,
check=True, update_check=True
)
check=True,
update_check=True)
self.validate(cr, uid, [reversal_move_id], context=context)
return reversal_move_id
def create_reversals(self, cr, uid, ids, reversal_date, reversal_period_id=False, reversal_journal_id=False,
move_prefix=False, move_line_prefix=False, context=None):
def create_reversals(self, cr, uid, ids, reversal_date,
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
@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_journal_id: facultative journal in which create the move
@param move_prefix: prefix for the move's name
@param move_line_prefix: prefix for the move line's names
: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_journal_id: facultative journal on which create
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)):
ids = [ids]
@@ -113,12 +135,17 @@ class account_move(osv.osv):
if src_move.reversal_id:
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_journal_id=reversal_journal_id, move_prefix=move_prefix,
move_line_prefix=move_line_prefix, context=context)
reversal_move_id = self._move_reversal(
cr, uid,
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:
reversed_move_ids.append(reversal_move_id)
return reversed_move_ids
account_move()

View File

@@ -23,52 +23,74 @@
#
##############################################################################
from osv import osv, fields
from tools.translate import _
from openerp.osv import orm, fields
from openerp.tools.translate import _
class account_move_reversal(osv.osv_memory):
class account_move_reversal(orm.TransientModel):
_name = "account.move.reverse"
_description = "Create reversal of account moves"
_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."),
'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)."),
'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."),
'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):
if context is None:
context = {}
period_obj = self.pool.get('account.period')
current_period_id = period_obj.find(cr, uid, context=context)[0]
current_period = period_obj.browse(cr, uid, current_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)
current_period = period_obj.browse(
cr, uid, current_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
_defaults = {
'date': _next_period_first_date,
'move_line_prefix': lambda *a: 'REV -',
'move_line_prefix': 'REV -',
}
def action_reverse(self, cr, uid, ids, context=None):
if context is None:
context = {}
assert 'active_ids' in context, "active_ids missing in context"
form = self.read(cr, uid, ids, context=context)[0]
action = {'type': 'ir.actions.act_window_close'}
if context.get('active_ids', False):
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']
period_id = form.get('period_id', False) and form['period_id'][0] or False
journal_id = form.get('journal_id', False) and form['journal_id'][0] or False
period_id = form['period_id'][0] if form.get('period_id') else False
journal_id = form['journal_id'][0] if form.get('journal_id') else False
reversed_move_ids = move_obj.create_reversals(
cr, uid,
move_ids,
@@ -79,12 +101,10 @@ class account_move_reversal(osv.osv_memory):
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_id = mod_obj.get_object_reference(
cr, uid, 'account', 'action_move_journal_line')
action = act_obj.read(cr, uid, [action_id], context=context)[0]
action['domain'] = str([('id', 'in', reversed_move_ids)])
action['domain'] = unicode([('id', 'in', reversed_move_ids)])
action['name'] = _('Reversal Entries')
action['context'] = unicode({'search_default_to_be_reversed': 0})
return action
account_move_reversal()