[FIX] Loop on the statement lines and make sql request to gain efficiency + change company field for a related one

This commit is contained in:
Florian da Costa
2014-02-04 12:27:00 +01:00
parent d0ba25d6e2
commit 6f64c0c102

View File

@@ -78,27 +78,30 @@ class AccountStatementCompletionRule(orm.Model):
label_obj = self.pool.get('account.statement.label') label_obj = self.pool.get('account.statement.label')
statement = st_obj.browse(cr, uid, st_line['statement_id'][0], context=context) statement = st_obj.browse(cr, uid, st_line['statement_id'][0], context=context)
res = {} res = {}
# As we have to iterate on each label for each line,
# we memorize the pair to avoid
# to redo computation for each line.
# Following code can be done by a single SQL query
# but this option is not really maintanable
if not context.get('label_memorizer'): if not context.get('label_memorizer'):
context['label_memorizer'] = defaultdict(list) context['label_memorizer'] = defaultdict(list)
label_ids = label_obj.search(cr, uid, for line in statement.line_ids:
['|', print "ll***"
('profile_id', '=', statement.profile_id.id), sub_query = "SELECT st_l.name FROM account_bank_statement_line as st_l WHERE st_l.id = %s" % (line.id)
('profile_id', '=', False)], sign = "'%'"
context=context) cr.execute("""
for label in label_obj.browse(cr, uid, label_ids, context=context): SELECT l.partner_id,
line_ids = st_line_obj.search(cr, uid, l.account_id
[('statement_id', '=', statement.id), FROM account_statement_label as l
('name', 'ilike', label.label), WHERE (
('already_completed', '=', False)], SELECT st_l.name
context=context) FROM account_bank_statement_line as st_l
for line_id in line_ids: WHERE st_l.id = %s) ILIKE %s || l.label || %s
context['label_memorizer'][line_id].append({'partner_id': label.partner_id.id, AND l.profile_id = (
'account_id': label.account_id.id}) SELECT s.profile_id
FROM account_bank_statement as s
LEFT JOIN account_bank_statement_line st_l
ON st_l.statement_id = s.id
WHERE st_l.id = %s)
""" % (line.id, sign, sign, line.id))
for partner, account in cr.fetchall():
context['label_memorizer'][line.id].append({'partner_id': partner,
'account_id': account})
if st_line['id'] in context['label_memorizer']: if st_line['id'] in context['label_memorizer']:
label_info = context['label_memorizer'][st_line['id']] label_info = context['label_memorizer'][st_line['id']]
if len(label_info) > 1: if len(label_info) > 1:
@@ -126,7 +129,12 @@ class AccountStatementLabel(orm.Model):
required = True, required = True,
help='Account corresponding to the label ' help='Account corresponding to the label '
'for a given partner'), 'for a given partner'),
'company_id': fields.many2one('res.company', 'Company'), 'company_id': fields.related('account_id', 'company_id',
type='many2one',
reation='res.company',
string='Company',
store=True,
readonly=True),
'profile_id': fields.many2one('account.statement.profile', 'profile_id': fields.many2one('account.statement.profile',
'Account Profile'), 'Account Profile'),
} }