From 34867df7286e188f589c15cef44d758ea5c95a5a Mon Sep 17 00:00:00 2001 From: Damien Bouvy Date: Mon, 18 May 2020 07:50:33 +0000 Subject: [PATCH] [FIX] payment,sale: company information in payment wizard link When generating a payment link through the payment link wizard, the company was previously not included. This could cause accounting issues if the acquirer displayed to the customer were not part of the same company as the underlying document that generated the link. This commit add a new computed field 'company_id' on the wizard model that gets computed based on the underlying model; this field will be included in links generated by the wizard to limit the acquirers displayed to those of the that company, preventing extra acocunting steps (interco reconciliation). opw-2254011 closes odoo/odoo#51441 X-original-commit: a6fcd7e0cfec8d4c62620a53b5fa806561e239af Signed-off-by: Damien Bouvy (dbo) --- addons/payment/controllers/portal.py | 15 ++++++++++++-- addons/payment/wizards/payment_link_wizard.py | 20 ++++++++++++++++++- addons/sale/wizard/sale_payment_link.py | 15 +++++++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/addons/payment/controllers/portal.py b/addons/payment/controllers/portal.py index 23a6fe3b69e..ece7f11dcd6 100644 --- a/addons/payment/controllers/portal.py +++ b/addons/payment/controllers/portal.py @@ -198,12 +198,23 @@ class WebsitePayment(http.Controller): # Check acquirer acquirers = None + if order_id and order: + cid = order.company_id.id + elif kw.get('company_id'): + try: + cid = int(kw.get('company_id')) + except: + cid = user.company_id.id + else: + cid = user.company_id.id + acquirer_domain = [('state', 'in', ['enabled', 'test']), ('company_id', '=', cid)] + if acquirer_id: acquirers = env['payment.acquirer'].browse(int(acquirer_id)) if order_id: - acquirers = env['payment.acquirer'].search([('state', 'in', ['enabled', 'test']), ('company_id', '=', order.company_id.id)]) + acquirers = env['payment.acquirer'].search(acquirer_domain) if not acquirers: - acquirers = env['payment.acquirer'].search([('state', 'in', ['enabled', 'test']), ('company_id', '=', user.company_id.id)]) + acquirers = env['payment.acquirer'].search(acquirer_domain) # Check partner if not user._is_public(): diff --git a/addons/payment/wizards/payment_link_wizard.py b/addons/payment/wizards/payment_link_wizard.py index 7dd543d997b..dd75560dd57 100644 --- a/addons/payment/wizards/payment_link_wizard.py +++ b/addons/payment/wizards/payment_link_wizard.py @@ -41,6 +41,7 @@ class PaymentLinkWizard(models.TransientModel): link = fields.Char(string='Payment Link', compute='_compute_values') description = fields.Char('Payment Ref') access_token = fields.Char(compute='_compute_values') + company_id = fields.Many2one('res.company', compute='_compute_company') @api.onchange('amount', 'description') def _onchange_amount(self): @@ -58,10 +59,27 @@ class PaymentLinkWizard(models.TransientModel): # must be called after token generation, obvsly - the link needs an up-to-date token self._generate_link() + @api.depends('res_model', 'res_id') + def _compute_company(self): + for link in self: + record = self.env[link.res_model].browse(link.res_id) + link.company_id = record.company_id if 'company_id' in record else False + def _generate_link(self): base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') for payment_link in self: - payment_link.link = '%s/website_payment/pay?reference=%s&amount=%s¤cy_id=%s&partner_id=%s&access_token=%s' % (base_url, urls.url_quote(payment_link.description), payment_link.amount, payment_link.currency_id.id, payment_link.partner_id.id, payment_link.access_token) + link = ('%s/website_payment/pay?reference=%s&amount=%s¤cy_id=%s' + '&partner_id=%s&access_token=%s') % ( + base_url, + urls.url_quote(payment_link.description), + payment_link.amount, + payment_link.currency_id.id, + payment_link.partner_id.id, + payment_link.access_token + ) + if payment_link.company_id: + link += '&company_id=%s' % payment_link.company_id.id + payment_link.link = link @api.model def check_token(self, access_token, partner_id, amount, currency_id): diff --git a/addons/sale/wizard/sale_payment_link.py b/addons/sale/wizard/sale_payment_link.py index c86072b2187..c3733af7ae7 100644 --- a/addons/sale/wizard/sale_payment_link.py +++ b/addons/sale/wizard/sale_payment_link.py @@ -30,7 +30,20 @@ class SalePaymentLink(models.TransientModel): for payment_link in self: # only add order_id for SOs, # otherwise the controller might try to link it with an unrelated record + # NOTE: company_id is not necessary here, we have it in order_id + # however, should parsing of the id fail in the controller, let's include + # it anyway if payment_link.res_model == 'sale.order': - payment_link.link = '%s/website_payment/pay?reference=%s&amount=%s¤cy_id=%s&partner_id=%s&order_id=%s&access_token=%s' % (base_url, urls.url_quote(payment_link.description), payment_link.amount, payment_link.currency_id.id, payment_link.partner_id.id, payment_link.res_id, payment_link.access_token) + payment_link.link = ('%s/website_payment/pay?reference=%s&amount=%s¤cy_id=%s' + '&partner_id=%s&order_id=%s&company_id=%s&access_token=%s') % ( + base_url, + urls.url_quote(payment_link.description), + payment_link.amount, + payment_link.currency_id.id, + payment_link.partner_id.id, + payment_link.res_id, + payment_link.company_id.id, + payment_link.access_token + ) else: super(SalePaymentLink, payment_link)._generate_link() \ No newline at end of file