Debian Codegraph @

addons/website_sale_collect/models/payment_provider.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo import _, api, fields, models
4
5 from odoo.addons.payment import utils as payment_utils
6 from odoo.addons.website_sale_collect import const
7
8
9 class PaymentProvider(models.Model):
10 _inherit = 'payment.provider'
11
12 custom_mode = fields.Selection(selection_add=[('on_site', "Pay on site")])
13
14 # === CRUD METHODS === #
15
16 def _get_default_payment_method_codes(self):
17 """ Override of `payment` to return the default payment method codes. """
18 self.ensure_one()
19 if self.custom_mode != 'on_site':
20 return super()._get_default_payment_method_codes()
21 return const.DEFAULT_PAYMENT_METHOD_CODES
22
23 # === BUSINESS METHODS === #
24
25 @api.model
26 def _get_compatible_providers(
27 self, company_id, *args, sale_order_id=None, website_id=None, report=None, **kwargs
28 ):
29 """ Override of payment to exclude on-site payment providers if the delivery method is not
30 pick up in store.
31
32 :param int company_id: The company to which providers must belong, as a `res.company` id
33 :param int sale_order_id: The sale order to be paid, if any, as a `sale.order` id
34 :param int website_id: The provided website, as a `website` id
35 :param dict report: The availability report.
36 :return: The compatible providers
37 :rtype: recordset of `payment.provider`
38 """
39 compatible_providers = super()._get_compatible_providers(
40 company_id,
41 *args,
42 sale_order_id=sale_order_id,
43 website_id=website_id,
44 report=report,
45 **kwargs,
46 )
47 order = self.env['sale.order'].browse(sale_order_id).exists()
48
49 # Show on-site payment providers only if in-store delivery methods exist and the order
50 # contains physical products.
51 if order.carrier_id.delivery_type != 'in_store' or not any(
52 product.type == 'consu' for product in order.order_line.product_id
53 ):
54 unfiltered_providers = compatible_providers
55 compatible_providers = compatible_providers.filtered(
56 lambda p: p.code != 'custom' or p.custom_mode != 'on_site'
57 )
58 payment_utils.add_to_report(
59 report,
60 unfiltered_providers - compatible_providers,
61 available=False,
62 reason=_("no in-store delivery methods available"),
63 )
64
65 return compatible_providers