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.

100 lines
2.7 KiB

  1. # Copyright 2020 Tecnativa - David Vidal
  2. # License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0.html
  3. from odoo import api, fields, models
  4. from dateutil.relativedelta import relativedelta
  5. class PartnerRoute(models.Model):
  6. _name = "partner.route"
  7. _description = "Partner Routes"
  8. _inherit = ['mail.thread', 'mail.activity.mixin']
  9. name = fields.Char(required=True)
  10. active = fields.Boolean(default=True)
  11. user_id = fields.Many2one(
  12. comodel_name="res.users",
  13. string="Responsible",
  14. help="Which user is in charge of the route",
  15. )
  16. route_type = fields.Selection(
  17. selection=[
  18. ("crm", "CRM"),
  19. ("sale", "Sales"),
  20. ("delivery", "Delivery"),
  21. ("repair", "Repair"),
  22. ],
  23. )
  24. interval_unit = fields.Selection(
  25. string="Interval",
  26. selection=[
  27. ("days", "Day(s)"),
  28. ("weeks", "Week(s)"),
  29. ("months", "Month(s)"),
  30. ("years", "Year(s)"),
  31. ],
  32. default="weeks",
  33. required=True,
  34. )
  35. recurring_interval = fields.Integer(
  36. string="Repeat Every",
  37. required=True,
  38. default=1,
  39. )
  40. next_date = fields.Date(
  41. string="Next route date",
  42. compute="_compute_next_date",
  43. required=True,
  44. readonly=False,
  45. store=True,
  46. default=fields.Date.today(),
  47. )
  48. route_day = fields.Boolean(
  49. string="Applies today",
  50. compute="_compute_route_day",
  51. readonly=True,
  52. )
  53. route_partner_ids = fields.One2many(
  54. comodel_name="partner.route.item",
  55. inverse_name="route_id",
  56. string="Partners",
  57. )
  58. @api.depends("interval_unit", "recurring_interval")
  59. def _compute_next_date(self):
  60. if self.next_date == fields.Date.today():
  61. return
  62. delta = relativedelta(**{self.interval_unit: self.recurring_interval})
  63. self.next_date = fields.Date.today() + delta
  64. @api.depends("next_date")
  65. def _compute_route_day(self):
  66. self.route_day = self.next_date == fields.Date.today()
  67. class PartnerRouteItem(models.Model):
  68. _name = "partner.route.item"
  69. _description = "Route Partners"
  70. _order = "sequence, partner_id"
  71. sequence = fields.Integer()
  72. route_id = fields.Many2one(
  73. comodel_name="partner.route",
  74. )
  75. partner_id = fields.Many2one(
  76. comodel_name="res.partner",
  77. )
  78. street = fields.Char(
  79. related="partner_id.street",
  80. )
  81. city = fields.Char(
  82. related="partner_id.city",
  83. )
  84. zip = fields.Char(
  85. related="partner_id.zip",
  86. )
  87. state_id = fields.Many2one(
  88. related="partner_id.state_id",
  89. )
  90. country_id = fields.Many2one(
  91. related="partner_id.country_id",
  92. )