[MRG] fix lookup on partner bank_statement_label

This commit is contained in:
unknown
2013-03-21 16:09:37 +01:00
parent 45245eb04d
commit be201a42c7

View File

@@ -18,6 +18,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
############################################################################## ##############################################################################
from collections import defaultdict
from tools.translate import _ from tools.translate import _
from openerp.osv.orm import Model, fields from openerp.osv.orm import Model, fields
from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
@@ -269,35 +271,46 @@ class AccountStatementCompletionRule(Model):
""" """
partner_obj = self.pool.get('res.partner') partner_obj = self.pool.get('res.partner')
st_obj = self.pool.get('account.bank.statement.line') st_obj = self.pool.get('account.bank.statement.line')
st_line = st_obj.browse(cr, uid, line_id, context=context)
res = {} res = {}
compt = 0 # As we have to iterate on each partner for each line,
if st_line: # we memoize the pair to avoid
ids = partner_obj.search( # to redo computation for each line.
cr, # Follwing code can be done by a single SQL query
uid, # but this option is not really maintanable
[('bank_statement_label', '!=', False)], if not context.get('label_memoizer'):
context=context) context['label_memoizer'] = defaultdict(list)
for partner in partner_obj.browse(cr, uid, ids, context=context): partner_ids = partner_obj.search(cr,
for partner_label in partner.bank_statement_label.split(';'): uid,
if partner_label in st_line.label: [('bank_statement_label', '!=', False)])
compt += 1 line_ids = tuple(x.id for x in context.get('line_ids', []))
res['partner_id'] = partner.id for partner in partner_obj.browse(cr, uid, partner_ids, context=context):
if compt > 1: vals = '|'.join(x.strip() for x in partner.bank_statement_label.split(';'))
raise ErrorTooManyPartner( or_regex = ".*%s*." % vals
_('Line named "%s" (Ref:%s) was matched by ' sql = ("SELECT id from account_bank_statement_line"
'more than one partner.') % " WHERE id in %s"
(st_line.name, st_line.ref)) " AND name ~* %s")
if res: cr.execute(sql, (line_ids, or_regex))
st_vals = st_obj.get_values_for_line( pairs = cr.fetchall()
cr, if not pairs:
uid, continue
profile_id=st_line.statement_id.profile_id.id, for pair in pairs:
partner_id=res.get('partner_id', False), context['label_memoizer'][pair[0]].append(partner)
line_type=st_line.type, st_line = st_obj.browse(cr, uid, line_id, context=context)
amount=st_line.amount, if st_line and st_line.id in context['label_memoizer']:
context=context) found_partner = context['label_memoizer'][st_line.id]
res.update(st_vals) if len(found_partner) > 1:
raise ErrorTooManyPartner(_('Line named "%s" (Ref:%s) was matched by '
'more than one partner.') %
(st_line.name, st_line.ref))
res['partner_id'] = found_partner[0].id
st_vals = st_obj.get_values_for_line(cr,
uid,
profile_id=st_line.statement_id.profile_id.id,
partner_id=found_partner[0].id,
line_type=st_line.type,
amount=st_line.amount,
context=context)
res.update(st_vals)
return res return res
def get_from_label_and_partner_name(self, cr, uid, line_id, context=None): def get_from_label_and_partner_name(self, cr, uid, line_id, context=None):
@@ -332,14 +345,13 @@ class AccountStatementCompletionRule(Model):
_('Line named "%s" (Ref:%s) was matched by more ' _('Line named "%s" (Ref:%s) was matched by more '
'than one partner.') % 'than one partner.') %
(st_line.name, st_line.ref)) (st_line.name, st_line.ref))
for id in result[0]: res['partner_id'] = result[0][0] if result else False
res['partner_id'] = id
if res: if res:
st_vals = st_obj.get_values_for_line( st_vals = st_obj.get_values_for_line(
cr, cr,
uid, uid,
profile_id=st_line.statement_id.profile_id.id, profile_id=st_line.statement_id.profile_id.id,
partner_id=res.get('partner_id', False), partner_id=res.get['partner_id'],
line_type=st_line.type, line_type=st_line.type,
amount=st_line.amount, amount=st_line.amount,
context=context) context=context)
@@ -475,6 +487,7 @@ class AccountBankSatement(Model):
for stat in self.browse(cr, uid, ids, context=context): for stat in self.browse(cr, uid, ids, context=context):
msg_lines = [] msg_lines = []
ctx = context.copy() ctx = context.copy()
ctx['line_ids'] = stat.line_ids
for line in stat.line_ids: for line in stat.line_ids:
res = {} res = {}
try: try: