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.

108 lines
4.0 KiB

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