diff --git a/partner_tag_smart_assignation/__manifest__.py b/partner_tag_smart_assignation/__manifest__.py
index dd7ead9e6c9..831c1977316 100644
--- a/partner_tag_smart_assignation/__manifest__.py
+++ b/partner_tag_smart_assignation/__manifest__.py
@@ -8,7 +8,7 @@
"author": "Compassion CH, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/partner-contact",
- "depends": ["base"],
+ "depends": ["base", "hr"],
"data": ["cron/update_cron.xml", "views/smart_tagger_view.xml"],
"installable": True,
"auto_install": False,
diff --git a/partner_tag_smart_assignation/models/__init__.py b/partner_tag_smart_assignation/models/__init__.py
index 5d00c57a590..e87be07028d 100644
--- a/partner_tag_smart_assignation/models/__init__.py
+++ b/partner_tag_smart_assignation/models/__init__.py
@@ -1,2 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from . import res_partner_category
+from . import hr_department
diff --git a/partner_tag_smart_assignation/models/hr_department.py b/partner_tag_smart_assignation/models/hr_department.py
new file mode 100644
index 00000000000..2b20a5244df
--- /dev/null
+++ b/partner_tag_smart_assignation/models/hr_department.py
@@ -0,0 +1,9 @@
+# Copyright (C) 2019 Compassion CH (http://www.compassion.ch)
+# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
+from odoo import fields, models
+
+
+class ResPartnerCategoryExtension(models.Model):
+ _inherit = "hr.department"
+
+ tag_ids = fields.Many2many("res.partner.category")
diff --git a/partner_tag_smart_assignation/models/res_partner_category.py b/partner_tag_smart_assignation/models/res_partner_category.py
index 2c0946f877b..a3565ae5ff0 100644
--- a/partner_tag_smart_assignation/models/res_partner_category.py
+++ b/partner_tag_smart_assignation/models/res_partner_category.py
@@ -43,6 +43,13 @@ class ResPartnerCategory(models.Model):
tagged_partner_count = fields.Integer(compute="_compute_number_tags", stored=True)
+ author_id = fields.Many2one(
+ "res.users", string="Author", default=lambda x: x.env.user
+ )
+ department_ids = fields.Many2many("hr.department", string="Department")
+ description = fields.Text()
+ valid_until = fields.Date()
+
@api.model
def create(self, vals):
record = super().create(vals)
@@ -134,8 +141,19 @@ def get_partners_from_sql(self):
@api.model
def update_all_smart_tags(self):
+ self._check_validity_dates()
return self.search([("smart", "=", True)]).update_partner_tags()
+ @api.model
+ def _check_validity_dates(self):
+ """Scheduled method to deactivate records past their validity date"""
+ today = fields.Date.today()
+ records_to_deactivate = self.search(
+ [("valid_until", "<", today), ("active", "=", True), ("smart", "=", True)]
+ )
+ # Archive the tag and unlink the partner
+ records_to_deactivate.write({"partner_ids": [(5, 0, 0)], "active": False})
+
@api.depends("partner_ids")
def _compute_number_tags(self):
for category in self:
diff --git a/partner_tag_smart_assignation/tests/test_smart_tagger.py b/partner_tag_smart_assignation/tests/test_smart_tagger.py
index fdf0fd5fbfe..d3fada1de28 100644
--- a/partner_tag_smart_assignation/tests/test_smart_tagger.py
+++ b/partner_tag_smart_assignation/tests/test_smart_tagger.py
@@ -1,5 +1,7 @@
import logging
+from datetime import timedelta
+from odoo import fields
from odoo.tests.common import SavepointCase
logger = logging.getLogger(__name__)
@@ -52,6 +54,10 @@ def create_tag(self):
"active": True,
"smart": True,
"parent_id": False,
+ "author_id": False,
+ "department_ids": False,
+ "description": "",
+ "valid_until": fields.Date.today(),
"tag_filter_partner_field": "partner_id",
"tag_filter_condition_id": self.create_condition().id,
}
@@ -119,3 +125,37 @@ def test_smart_tag_sql(self):
for partner in smart_tag.partner_ids:
self.assertTrue("o" in partner.name)
+
+ def test_check_validity_dates(self):
+ """
+ Test if the valid_until functionality works correctly
+ """
+
+ # Create a new tag with a 'valid_until' date set to yesterday
+ yesterday = fields.Date.to_string(fields.Date.today() - timedelta(days=1))
+
+ expired_tag = self.create_tag()
+ expired_tag.update({"valid_until": yesterday})
+ expired_tag.update_partner_tags()
+
+ # Reload the tags from the database
+ expired_tag.invalidate_cache()
+
+ # Check that the expired tag is now inactive
+ self.assertFalse(expired_tag.active)
+ # Check that the partner aren't tagged
+ self.assertFalse(self.david_simpson in expired_tag.partner_ids)
+
+ # Modify the tag with a 'valid_until' date set to tomorrow
+ tomorrow = fields.Date.to_string(fields.Date.today() + timedelta(days=1))
+ active_tag = expired_tag
+ active_tag.update({"valid_until": tomorrow, "active": True})
+
+ # Run the method which is supposed to deactivate expired tags
+ self.env["res.partner.category"]._check_validity_dates()
+
+ # Reload the tags from the database
+ active_tag.invalidate_cache()
+
+ # Check that the active tag is still active
+ self.assertTrue(active_tag.active)
diff --git a/partner_tag_smart_assignation/views/smart_tagger_view.xml b/partner_tag_smart_assignation/views/smart_tagger_view.xml
index 4465da71dd7..ab9d2333ca7 100644
--- a/partner_tag_smart_assignation/views/smart_tagger_view.xml
+++ b/partner_tag_smart_assignation/views/smart_tagger_view.xml
@@ -6,6 +6,13 @@
+
+
+
+
@@ -34,6 +41,13 @@
+
+
+
+