account_payment_register.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from odoo import api, Command, fields, models
  2. class AccountPaymentRegister(models.TransientModel):
  3. _inherit = 'account.payment.register'
  4. # == Business fields ==
  5. payment_token_id = fields.Many2one(
  6. comodel_name='payment.token',
  7. string="Saved payment token",
  8. store=True, readonly=False,
  9. compute='_compute_payment_token_id',
  10. domain='''[
  11. ('id', 'in', suitable_payment_token_ids),
  12. ]''',
  13. help="Note that tokens from providers set to only authorize transactions (instead of capturing the amount) are "
  14. "not available.")
  15. # == Display purpose fields ==
  16. suitable_payment_token_ids = fields.Many2many(
  17. comodel_name='payment.token',
  18. compute='_compute_suitable_payment_token_ids'
  19. )
  20. # Technical field used to hide or show the payment_token_id if needed
  21. use_electronic_payment_method = fields.Boolean(
  22. compute='_compute_use_electronic_payment_method',
  23. )
  24. payment_method_code = fields.Char(
  25. related='payment_method_line_id.code')
  26. # -------------------------------------------------------------------------
  27. # COMPUTE METHODS
  28. # -------------------------------------------------------------------------
  29. @api.depends('payment_method_line_id')
  30. def _compute_suitable_payment_token_ids(self):
  31. for wizard in self:
  32. if wizard.can_edit_wizard and wizard.use_electronic_payment_method:
  33. related_partner_ids = (
  34. wizard.partner_id
  35. | wizard.partner_id.commercial_partner_id
  36. | wizard.partner_id.commercial_partner_id.child_ids
  37. )._origin
  38. wizard.suitable_payment_token_ids = self.env['payment.token'].sudo().search([
  39. ('company_id', '=', wizard.company_id.id),
  40. ('provider_id.capture_manually', '=', False),
  41. ('partner_id', 'in', related_partner_ids.ids),
  42. ('provider_id', '=', wizard.payment_method_line_id.payment_provider_id.id),
  43. ])
  44. else:
  45. wizard.suitable_payment_token_ids = [Command.clear()]
  46. @api.depends('payment_method_line_id')
  47. def _compute_use_electronic_payment_method(self):
  48. for wizard in self:
  49. # Get a list of all electronic payment method codes.
  50. # These codes are comprised of the codes of each payment provider.
  51. codes = [key for key in dict(self.env['payment.provider']._fields['code']._description_selection(self.env))]
  52. wizard.use_electronic_payment_method = wizard.payment_method_code in codes
  53. @api.onchange('can_edit_wizard', 'payment_method_line_id', 'journal_id')
  54. def _compute_payment_token_id(self):
  55. codes = [key for key in dict(self.env['payment.provider']._fields['code']._description_selection(self.env))]
  56. for wizard in self:
  57. related_partner_ids = (
  58. wizard.partner_id
  59. | wizard.partner_id.commercial_partner_id
  60. | wizard.partner_id.commercial_partner_id.child_ids
  61. )._origin
  62. if wizard.can_edit_wizard \
  63. and wizard.payment_method_line_id.code in codes \
  64. and wizard.journal_id \
  65. and related_partner_ids:
  66. wizard.payment_token_id = self.env['payment.token'].sudo().search([
  67. ('company_id', '=', wizard.company_id.id),
  68. ('partner_id', 'in', related_partner_ids.ids),
  69. ('provider_id.capture_manually', '=', False),
  70. ('provider_id', '=', wizard.payment_method_line_id.payment_provider_id.id),
  71. ], limit=1)
  72. else:
  73. wizard.payment_token_id = False
  74. # -------------------------------------------------------------------------
  75. # BUSINESS METHODS
  76. # -------------------------------------------------------------------------
  77. def _create_payment_vals_from_wizard(self, batch_result):
  78. # OVERRIDE
  79. payment_vals = super()._create_payment_vals_from_wizard(batch_result)
  80. payment_vals['payment_token_id'] = self.payment_token_id.id
  81. return payment_vals