diff --git a/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py b/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py
index fd5201b62bc8a63fb678c14f2c0f7a7a9340f0ff..0d2c7120503fb0f4ab6f4c3534cca4d7e376052d 100644
--- a/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py
+++ b/addons/hr_timesheet_invoice/wizard/hr_timesheet_invoice_create.py
@@ -128,10 +128,13 @@ class invoice_create(wizard.interface):
                 #
                 # Compute for lines
                 #
-                str_ids=""
-                for x in data['ids']:
-                    str_ids += ','.join(str(x)) 
-                cr.execute("SELECT * FROM account_analytic_line WHERE account_id = %s and id IN (%s) AND product_id=%s and to_invoice=%s", (account.id, str_ids, product_id, factor_id))
+                cr.execute("SELECT * "  # TODO optimize this
+                           "  FROM account_analytic_line" 
+                           " WHERE account_id=%%s"
+                           "   AND id IN (%s)"
+                           "   AND product_id=%%s"
+                           "   AND to_invoice=%%s" % ','.join(['%s']*len(data['ids'])), 
+                           (account.id, data['ids'], product_id, factor_id))
                 line_ids = cr.dictfetchall()
                 note = []
                 for line in line_ids: