diff --git a/addons/account/account.py b/addons/account/account.py index b7c76ac892a..57e53aa55c5 100644 --- a/addons/account/account.py +++ b/addons/account/account.py @@ -1047,13 +1047,15 @@ class account_period(osv.osv): else: company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id args.append(('company_id', '=', company_id)) - ids = self.search(cr, uid, args, context=context) - if not ids: + result = [] + if context.get('account_period_prefer_normal'): + # look for non-special periods first, and fallback to all if no result is found + result = self.search(cr, uid, args + [('special', '=', False)], context=context) + if not result: + result = self.search(cr, uid, args, context=context) + if not result: raise osv.except_osv(_('Error !'), _('No period defined for this date: %s !\nPlease create one.')%dt) - for period in self.browse(cr, uid, ids, context=context): - if period.date_start == period.date_stop: - ids.remove(period.id) - return ids + return result def action_draft(self, cr, uid, ids, *args): mode = 'draft' @@ -1226,10 +1228,9 @@ class account_move(osv.osv): return res def _get_period(self, cr, uid, context=None): - periods = self.pool.get('account.period').find(cr, uid) - if periods: - return periods[0] - return False + ctx = dict(context or {}, account_period_prefer_normal=True) + period_ids = self.pool.get('account.period').find(cr, uid, context=ctx) + return period_ids[0] def _amount_compute(self, cr, uid, ids, name, args, context, where =''): if not ids: return {} diff --git a/addons/account/account_invoice.py b/addons/account/account_invoice.py index b7def8efde5..bbb002c232e 100644 --- a/addons/account/account_invoice.py +++ b/addons/account/account_invoice.py @@ -933,7 +933,8 @@ class account_invoice(osv.osv): 'narration':inv.comment } period_id = inv.period_id and inv.period_id.id or False - ctx.update({'company_id': inv.company_id.id}) + ctx.update(company_id=inv.company_id.id, + account_period_prefer_normal=True) if not period_id: period_ids = period_obj.find(cr, uid, inv.date_invoice, context=ctx) period_id = period_ids and period_ids[0] or False diff --git a/addons/account/account_move_line.py b/addons/account/account_move_line.py index c6182d9e61b..48726b9e522 100644 --- a/addons/account/account_move_line.py +++ b/addons/account/account_move_line.py @@ -952,7 +952,8 @@ class account_move_line(osv.osv): if context is None: context = {} period_pool = self.pool.get('account.period') - pids = period_pool.search(cr, user, [('date_start','<=',date), ('date_stop','>=',date)]) + ctx = dict(context, account_period_prefer_normal=True) + pids = period_pool.find(cr, user, date, context=ctx) if pids: res.update({ 'period_id':pids[0]