res_company.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. from odoo import api, fields, models
  3. class ResCompany(models.Model):
  4. _inherit = 'res.company'
  5. payment_provider_onboarding_state = fields.Selection(
  6. string="State of the onboarding payment provider step",
  7. selection=[('not_done', "Not done"), ('just_done', "Just done"), ('done', "Done")],
  8. default='not_done')
  9. payment_onboarding_payment_method = fields.Selection(
  10. string="Selected onboarding payment method",
  11. selection=[
  12. ('paypal', "PayPal"),
  13. ('stripe', "Stripe"),
  14. ('manual', "Manual"),
  15. ('other', "Other"),
  16. ])
  17. def _run_payment_onboarding_step(self, menu_id):
  18. """ Install the suggested payment modules and configure the providers.
  19. It's checked that the current company has a Chart of Account.
  20. :param int menu_id: The menu from which the user started the onboarding step, as an
  21. `ir.ui.menu` id
  22. :return: The action returned by `action_stripe_connect_account`
  23. :rtype: dict
  24. """
  25. self.env.company.get_chart_of_accounts_or_fail()
  26. self._install_modules(['payment_stripe', 'account_payment'])
  27. # Create a new env including the freshly installed module(s)
  28. new_env = api.Environment(self.env.cr, self.env.uid, self.env.context)
  29. # Configure Stripe
  30. default_journal = new_env['account.journal'].search(
  31. [('type', '=', 'bank'), ('company_id', '=', new_env.company.id)], limit=1
  32. )
  33. stripe_provider = new_env['payment.provider'].search(
  34. [('company_id', '=', self.env.company.id), ('code', '=', 'stripe')], limit=1
  35. )
  36. if not stripe_provider:
  37. base_provider = self.env.ref('payment.payment_provider_stripe')
  38. # Use sudo to access payment provider record that can be in different company.
  39. stripe_provider = base_provider.sudo().copy(default={'company_id': self.env.company.id})
  40. stripe_provider.journal_id = stripe_provider.journal_id or default_journal
  41. return stripe_provider.action_stripe_connect_account(menu_id=menu_id)
  42. def _install_modules(self, module_names):
  43. modules_sudo = self.env['ir.module.module'].sudo().search([('name', 'in', module_names)])
  44. STATES = ['installed', 'to install', 'to upgrade']
  45. modules_sudo.filtered(lambda m: m.state not in STATES).button_immediate_install()
  46. def _mark_payment_onboarding_step_as_done(self):
  47. """ Mark the payment onboarding step as done.
  48. :return: None
  49. """
  50. self.set_onboarding_step_done('payment_provider_onboarding_state')
  51. def get_account_invoice_onboarding_steps_states_names(self):
  52. """ Override of account. """
  53. steps = super().get_account_invoice_onboarding_steps_states_names()
  54. return steps + ['payment_provider_onboarding_state']