Skip to content

Commit

Permalink
[FIX] crm_salesperson_planner: Set the correct date of visits in spec…
Browse files Browse the repository at this point in the history
…ific days of week

TT48238
  • Loading branch information
victoralmau committed Mar 8, 2024
1 parent 923e37c commit f554b12
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,27 @@ def _get_max_date(self):
return self.until or self._increase_date(self.start_date, self.count)

def _increase_date(self, date, value):
week_fields_map = {
0: "mon",
1: "tue",
2: "wed",
3: "thu",
4: "fri",
5: "sat",
6: "sun",
}
if self.rrule_type == "daily":
date += timedelta(days=value)
elif self.rrule_type == "weekly":
date += timedelta(weeks=value)
weekdays = []
for weekday in list(week_fields_map.keys()):
week_field_name = week_fields_map[weekday]
if self[week_field_name]:
weekdays.append(weekday)
if len(weekdays) > 0:
while date.weekday() not in weekdays:
date += timedelta(days=1)
elif self.rrule_type == "monthly":
date += timedelta(months=value)
elif self.rrule_type == "yearly":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2021 Sygel - Valentin Vinagre
# Copyright 2021 Sygel - Manuel Regidor
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from datetime import timedelta

from odoo import exceptions, fields
Expand Down Expand Up @@ -183,3 +182,31 @@ def test_04_cancel_visit(self):
self.assertFalse(first_visit.calendar_event_id)
first_visit.unlink()
self.assertEqual(len(visit_template.visit_ids), 9)

def test_05_repeat_weeks(self):
self.visit_template_base.write(
{
"start_date": "2024-03-08",
"interval": 1,
"rrule_type": "weekly",
"tue": True,
"end_type": "end_date",
"until": "2024-07-02",
}
)
self.visit_template_base.action_validate()
self.assertFalse(self.visit_template_base.visit_ids)
create_model = self.env["crm.salesperson.planner.visit.template.create"]
create_item = create_model.with_context(
active_id=self.visit_template_base.id
).create({"date_to": "2024-07-02"})
create_item.create_visits()
self.assertEqual(self.visit_template_base.state, "done")
self.assertEqual(
self.visit_template_base.visit_ids[0].date,
fields.Date.from_string("2024-03-19"),
)
self.assertEqual(
self.visit_template_base.last_visit_date,
fields.Date.from_string("2024-07-02"),
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ def create_visits(self):
days = (self.date_to - fields.Date.context_today(self)).days
if days < 0:
raise ValidationError(_("The date can't be earlier than today"))
visits = self.env["crm.salesperson.planner.visit"].create(
template._create_visits(days=days)
)
if visits and template.auto_validate:
visits.action_confirm()
# Create visits + auto-confirm + auto-done
template.create_visits(days=days)
return {"type": "ir.actions.act_window_close"}

0 comments on commit f554b12

Please sign in to comment.