From d78f23df84840b151c20cc3bfccd2104d9ff0ace Mon Sep 17 00:00:00 2001 From: Toufik Benjaa Date: Fri, 7 Sep 2018 17:20:47 +0200 Subject: [PATCH] [FIX] payment,sale_payment: extract code to overridable methods - We extract some code logic to methods to allow to override them. This is needed for the subscription module where a payment token need to be copied from a transaction to a subscription. The subscriptions is created from a sale order and before the payment.transaction holding the payment.token id is validated. Since the payment.transaction is not validated, the subscription module doesn't find the correct transaction. By extracting this code logic, we can override exactly when it should be. --- addons/payment/models/payment_acquirer.py | 9 ++++++--- addons/sale_payment/models/payment.py | 8 ++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/addons/payment/models/payment_acquirer.py b/addons/payment/models/payment_acquirer.py index 5a57a90cb3c..14d4bc55f77 100644 --- a/addons/payment/models/payment_acquirer.py +++ b/addons/payment/models/payment_acquirer.py @@ -725,6 +725,12 @@ class PaymentTransaction(models.Model): if any(trans.state not in ('draft', 'authorized') for trans in self): raise ValidationError(_('Only draft/authorized transaction can be posted.')) + self.write({'state': 'done', 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)}) + self._reconcile_after_transaction_done() + self._log_payment_transaction_received() + + @api.multi + def _reconcile_after_transaction_done(self): # Validate invoices automatically upon the transaction is posted. invoices = self.mapped('invoice_ids').filtered(lambda inv: inv.state == 'draft') invoices.action_invoice_open() @@ -746,9 +752,6 @@ class PaymentTransaction(models.Model): for company in payments: payments[company].with_context(force_company=company, company_id=company).post() - self.write({'state': 'done', 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)}) - self._log_payment_transaction_received() - @api.multi def _set_transaction_cancel(self): '''Move the transaction's payment to the cancel state(e.g. Paypal).''' diff --git a/addons/sale_payment/models/payment.py b/addons/sale_payment/models/payment.py index 9c2afc96b72..1cc47bf45d5 100644 --- a/addons/sale_payment/models/payment.py +++ b/addons/sale_payment/models/payment.py @@ -59,14 +59,19 @@ class PaymentTransaction(models.Model): so.action_confirm() @api.multi - def _set_transaction_done(self): + def _reconcile_after_transaction_done(self): # Override of '_set_transaction_done' in the 'payment' module # to confirm the quotations automatically and to generate the invoices if needed. sales_orders = self.mapped('sale_order_ids').filtered(lambda so: so.state == 'draft') sales_orders.force_quotation_send() sales_orders = self.mapped('sale_order_ids').filtered(lambda so: so.state == 'sent') sales_orders.action_confirm() + # invoice the sale orders if needed + self._invoice_sale_orders() + return super(PaymentTransaction, self)._reconcile_after_transaction_done() + @api.multi + def _invoice_sale_orders(self): if self.env['ir.config_parameter'].sudo().get_param('website_sale.automatic_invoice'): for trans in self.filtered(lambda t: t.sale_order_ids): ctx_company = {'company_id': self.acquirer_id.company_id.id, @@ -75,7 +80,6 @@ class PaymentTransaction(models.Model): trans.sale_order_ids._force_lines_to_invoice_policy_order() invoices = trans.sale_order_ids.action_invoice_create() trans.invoice_ids = [(6, 0, invoices)] - return super(PaymentTransaction, self)._set_transaction_done() @api.model def _compute_reference_prefix(self, values):