[RFR] Unify _get_period methods on statement and statement line. As a result,

always return a non-opening period. Small API change:
      date is always passed as argument, not in context.
[RFR] Override of _check_company_id (unrelated to the fix):
      Don't call super, but integrate its check (while being tolerant when
      there is no period).
      Refresh statement so as not to overwrite the period for every line
[RFR] PEP8 in affected code and unrelated _get_period method on voucher
      Remove obsolete class instantiations
This commit is contained in:
Stefan Rijnhart
2014-03-20 16:12:21 +01:00
parent f39a74d44c
commit e58f8306ec
2 changed files with 37 additions and 42 deletions

View File

@@ -298,17 +298,24 @@ class account_bank_statement(orm.Model):
def _check_company_id(self, cr, uid, ids, context=None):
"""
Adapt this constraint method from the account module to reflect the
move of period_id to the statement line
move of period_id to the statement line: also check the periods of the
lines. Update the statement period if it does not have one yet.
Don't call super, because its check is integrated below and
it will break if a statement does not have any lines yet and
therefore may not have a period.
"""
for statement in self.browse(cr, uid, ids, context=context):
if (statement.period_id and
statement.company_id != statement.period_id.company_id):
return False
for line in statement.line_ids:
if (line.period_id and
statement.company_id.id != line.period_id.company_id.id):
statement.company_id != line.period_id.company_id):
return False
if not statement.period_id:
statement.write({'period_id': line.period_id.id})
return super(account_bank_statement, self)._check_company_id(
cr, uid, ids, context=context)
statement.refresh()
return True
# Redefine the constraint, or it still refer to the original method
_constraints = [
@@ -317,13 +324,22 @@ class account_bank_statement(orm.Model):
['journal_id','period_id']),
]
def _get_period(self, cr, uid, date, context=None):
'''
Find matching period for date, not meant for _defaults.
'''
period_obj = self.pool.get('account.period')
periods = period_obj.find(cr, uid, dt=date, context=context)
return periods and periods[0] or False
def _get_period(self, cr, uid, date=False, context=None):
"""
Used in statement line's _defaults, so it is always triggered
on installation or module upgrade even if there are no records
without a value. For that reason, we need
to be tolerant and allow for the situation in which no period
exists for the current date (i.e. when no date is specified).
"""
local_ctx = dict(context or {}, account_period_prefer_normal=True)
try:
return self.pool.get('account.period').find(
cr, uid, dt=date, context=local_ctx)[0]
except except_osv:
if date:
raise
return False
def _prepare_move(
self, cr, uid, st_line, st_line_number, context=None):
@@ -368,7 +384,7 @@ class account_bank_statement(orm.Model):
# Take period from statement line and write to context
# this will be picked up by the _prepare_move* methods
period_id = self._get_period(
cr, uid, st_line.date, context=context)
cr, uid, date=st_line.date, context=context)
localctx = context.copy()
localctx['period_id'] = period_id
@@ -424,7 +440,8 @@ class account_bank_statement(orm.Model):
context=context)
for st in self.browse(cr, uid, noname_ids, context=context):
if st.journal_id.sequence_id:
period_id = self._get_period(cr, uid, st.date)
period_id = self._get_period(
cr, uid, date=st.date, context=context)
year = self.pool.get('account.period').browse(
cr, uid, period_id, context=context).fiscalyear_id.id
c = {'fiscalyear_id': year}
@@ -436,8 +453,6 @@ class account_bank_statement(orm.Model):
return super(account_bank_statement, self).button_confirm_bank(
cr, uid, ids, context)
account_bank_statement()
class account_voucher(orm.Model):
_inherit = 'account.voucher'
@@ -446,12 +461,11 @@ class account_voucher(orm.Model):
if context is None:
context = {}
if not context.get('period_id') and context.get('move_line_ids'):
return self.pool.get('account.move.line').browse(
cr, uid , context.get('move_line_ids'), context=context)[0].period_id.id
move_line = self.pool.get('account.move.line').browse(
cr, uid , context.get('move_line_ids')[0], context=context)
return move_line.period_id.id
return super(account_voucher, self)._get_period(cr, uid, context)
account_voucher()
class account_bank_statement_line(orm.Model):
'''
@@ -464,28 +478,9 @@ class account_bank_statement_line(orm.Model):
_inherit = 'account.bank.statement.line'
_description = 'Bank Transaction'
def _get_period(self, cr, uid, context=None):
"""
Get a non-opening period for today or a date specified in
the context.
Used in this model's _defaults, so it is always triggered
on installation or module upgrade. For that reason, we need
to be tolerant and allow for the situation in which no period
exists for the current date (i.e. when no date is specified).
"""
if context is None:
context = {}
date = context.get('date', False)
local_ctx = dict(context)
local_ctx['account_period_prefer_normal'] = True
try:
return self.pool.get('account.period').find(
cr, uid, dt=date, context=local_ctx)[0]
except except_osv:
if date:
raise
return False
def _get_period(self, cr, uid, date=False, context=None):
return self.pool['account.bank.statement']._get_period(
cr, uid, date=date, context=context)
def _get_currency(self, cr, uid, context=None):
'''

View File

@@ -1576,7 +1576,7 @@ class account_bank_statement_line(orm.Model):
self.write(
cr, uid, [st_line.id], {
'period_id': self._get_period(
cr, uid, {'date': st_line.date})
cr, uid, date=st_line.date, context=context)
})
st_line.refresh()
# Generate the statement number, if it is not already done