From e11c0a45f230f66ee9bd9f8730ecd165e47c0a72 Mon Sep 17 00:00:00 2001 From: Toufik Benjaa Date: Thu, 6 Sep 2018 11:18:51 +0200 Subject: [PATCH] [FIX] payment: Fix reference computation to avoid duplicates - The method to compute the reference of a payment.transaction and avoid duplicate was missing some edge cases (for example if there was a '-' in the reference). - The creation of transactions in the route /website_payment/pay was not checking for duplicate payment.transaction. --- addons/payment/controllers/portal.py | 2 ++ addons/payment/models/payment_acquirer.py | 5 ++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/addons/payment/controllers/portal.py b/addons/payment/controllers/portal.py index ae9863c4ffc..29a8d8568fc 100644 --- a/addons/payment/controllers/portal.py +++ b/addons/payment/controllers/portal.py @@ -109,6 +109,8 @@ class WebsitePayment(http.Controller): if order_id: values['sale_order_ids'] = [(6, 0, [order_id])] + reference_values = order_id and {'sale_order_ids': [(4, order_id)]} or {} + values['reference'] = request.env['payment.transaction']._compute_reference(values=reference_values, prefix=reference) tx = request.env['payment.transaction'].sudo().with_context(lang=None).create(values) render_values = { diff --git a/addons/payment/models/payment_acquirer.py b/addons/payment/models/payment_acquirer.py index de4b8bab581..5a57a90cb3c 100644 --- a/addons/payment/models/payment_acquirer.py +++ b/addons/payment/models/payment_acquirer.py @@ -792,11 +792,10 @@ class PaymentTransaction(models.Model): # Fetch the last reference # E.g. If the last reference is SO42-5, this query will return '-5' self._cr.execute(''' - SELECT CAST(SUBSTRING(reference FROM '-\d+') AS INTEGER) AS suffix + SELECT CAST(SUBSTRING(reference FROM '-\d+$') AS INTEGER) AS suffix FROM payment_transaction WHERE reference LIKE %s ORDER BY suffix - ''', [prefix + '%']) + ''', [prefix + '-%']) query_res = self._cr.fetchone() - if query_res: # Increment the last reference by one suffix = '%s' % (-query_res[0] + 1)