中国本土应用
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.

114 lines
3.8KB

  1. # Copyright 2016 ACSONE SA/NV (<http://acsone.eu>)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. class DateRange(models.Model):
  6. _name = "date.range"
  7. _description = "Date Range"
  8. _order = "type_name,date_start"
  9. @api.model
  10. def _default_company(self):
  11. return self.env.company
  12. name = fields.Char(required=True, translate=True)
  13. date_start = fields.Date(string="Start date", required=True)
  14. date_end = fields.Date(string="End date", required=True)
  15. type_id = fields.Many2one(
  16. comodel_name="date.range.type",
  17. string="Type",
  18. index=1,
  19. required=True,
  20. ondelete="restrict",
  21. domain="['|', ('company_id', '=', company_id), " "('company_id', '=', False)]",
  22. store=True,
  23. compute="_compute_type_id",
  24. readonly=False,
  25. )
  26. type_name = fields.Char(related="type_id.name", store=True, string="Type Name")
  27. company_id = fields.Many2one(
  28. comodel_name="res.company", string="Company", index=1, default=_default_company
  29. )
  30. active = fields.Boolean(
  31. help="The active field allows you to hide the date range without "
  32. "removing it.",
  33. default=True,
  34. )
  35. _sql_constraints = [
  36. (
  37. "date_range_uniq",
  38. "unique (name,type_id, company_id)",
  39. "A date range must be unique per company !",
  40. )
  41. ]
  42. @api.depends("company_id", "type_id.company_id")
  43. def _compute_type_id(self):
  44. """Enforce check of company consistency when changing company, here
  45. or in the type.
  46. """
  47. self._check_company_id_type_id()
  48. @api.constrains("company_id", "type_id")
  49. def _check_company_id_type_id(self):
  50. for rec in self.sudo():
  51. if (
  52. rec.company_id
  53. and rec.type_id.company_id
  54. and rec.company_id != rec.type_id.company_id
  55. ):
  56. raise ValidationError(
  57. _(
  58. "The Company in the Date Range and in "
  59. "Date Range Type must be the same."
  60. )
  61. )
  62. @api.constrains("type_id", "date_start", "date_end", "company_id")
  63. def _validate_range(self):
  64. for this in self:
  65. if this.date_start > this.date_end:
  66. raise ValidationError(
  67. _("%s is not a valid range (%s > %s)")
  68. % (this.name, this.date_start, this.date_end)
  69. )
  70. if this.type_id.allow_overlap:
  71. continue
  72. # here we use a plain SQL query to benefit of the daterange
  73. # function available in PostgresSQL
  74. # (http://www.postgresql.org/docs/current/static/rangetypes.html)
  75. SQL = """
  76. SELECT
  77. id
  78. FROM
  79. date_range dt
  80. WHERE
  81. DATERANGE(dt.date_start, dt.date_end, '[]') &&
  82. DATERANGE(%s::date, %s::date, '[]')
  83. AND dt.id != %s
  84. AND dt.active
  85. AND dt.company_id = %s
  86. AND dt.type_id=%s;"""
  87. self.env.cr.execute(
  88. SQL,
  89. (
  90. this.date_start,
  91. this.date_end,
  92. this.id,
  93. this.company_id.id or None,
  94. this.type_id.id,
  95. ),
  96. )
  97. res = self.env.cr.fetchall()
  98. if res:
  99. dt = self.browse(res[0][0])
  100. raise ValidationError(_("%s overlaps %s") % (this.name, dt.name))
  101. def get_domain(self, field_name):
  102. self.ensure_one()
  103. return [(field_name, ">=", self.date_start), (field_name, "<=", self.date_end)]
上海开阖软件有限公司 沪ICP备12045867号-1