You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
4.0 KiB

  1. # Copyright 2017 LasLabs Inc.
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, fields, models
  4. def _context_mail_templates(env):
  5. return env['contract.abstract.contract']._context_mail_templates()
  6. class AbstractContract(models.AbstractModel):
  7. _inherit = 'contract.abstract.contract'
  8. invoice_mail_template_id = fields.Many2one(
  9. string='Invoice Message',
  10. comodel_name='mail.template',
  11. default=lambda s: s._default_invoice_mail_template_id(),
  12. domain="[('model', '=', 'account.invoice')]",
  13. context=_context_mail_templates,
  14. help="During the automatic payment process, an invoice will be "
  15. "created and validated. If this template is selected, it will "
  16. "automatically be sent to the customer during this process "
  17. "using the defined template.",
  18. )
  19. pay_retry_mail_template_id = fields.Many2one(
  20. string='Payment Retry Message',
  21. comodel_name='mail.template',
  22. default=lambda s: s._default_pay_retry_mail_template_id(),
  23. domain="[('model', '=', 'account.invoice')]",
  24. context=_context_mail_templates,
  25. help="If automatic payment fails for some reason, but will be "
  26. "re-attempted later, this message will be sent to the billed "
  27. "partner.",
  28. )
  29. pay_fail_mail_template_id = fields.Many2one(
  30. string='Payment Failed Message',
  31. comodel_name='mail.template',
  32. default=lambda s: s._default_pay_fail_mail_template_id(),
  33. domain="[('model', '=', 'account.invoice')]",
  34. context=_context_mail_templates,
  35. help="If automatic payment fails for some reason, this message "
  36. "will be sent to the billed partner.",
  37. )
  38. is_auto_pay = fields.Boolean(
  39. string='Auto Pay?',
  40. default=False,
  41. help="Check this to enable automatic payment for invoices that are "
  42. "created for this contract.",
  43. )
  44. auto_pay_retries = fields.Integer(
  45. default=lambda s: s._default_auto_pay_retries(),
  46. help="Amount times to retry failed/declined automatic payment "
  47. "before giving up."
  48. )
  49. auto_pay_retry_hours = fields.Integer(
  50. default=lambda s: s._default_auto_pay_retry_hours(),
  51. help="Amount of hours that should lapse until a failed automatic "
  52. "is retried.",
  53. )
  54. @api.model
  55. def _default_invoice_mail_template_id(self):
  56. return self.env.ref(
  57. 'account.email_template_edi_invoice',
  58. raise_if_not_found=False,
  59. )
  60. @api.model
  61. def _default_pay_retry_mail_template_id(self):
  62. return self.env.ref(
  63. 'contract_payment_auto.mail_template_auto_pay_retry',
  64. raise_if_not_found=False,
  65. )
  66. @api.model
  67. def _default_pay_fail_mail_template_id(self):
  68. return self.env.ref(
  69. 'contract_payment_auto.mail_template_auto_pay_fail',
  70. raise_if_not_found=False,
  71. )
  72. @api.model
  73. def _default_auto_pay_retries(self):
  74. return 3
  75. @api.model
  76. def _default_auto_pay_retry_hours(self):
  77. return 24
  78. @api.model
  79. def _context_mail_templates(self):
  80. """ Return a context for use in mail templates. """
  81. default_model = self.env.ref('account.model_account_invoice')
  82. report_template = self.env.ref('account.account_invoices')
  83. return {
  84. 'default_model_id': default_model.id,
  85. 'default_email_from': "${(object.user_id.email and '%s <%s>' % "
  86. "(object.user_id.name, object.user_id.email)"
  87. " or '')|safe}",
  88. 'default_partner_to': '${object.partner_id.id}',
  89. 'default_lang': '${object.partner_id.lang}',
  90. 'default_auto_delete': True,
  91. 'report_template': report_template.id,
  92. 'report_name': "Invoice_${(object.number or '').replace('/','_')}"
  93. "_${object.state == 'draft' and 'draft' or ''}",
  94. }