Skip to content

Commit 2d9fd4f

Browse files
author
Michael Michot
committed
[ADD] module phs_product_auto_tags
1 parent 1f017aa commit 2d9fd4f

File tree

10 files changed

+181
-0
lines changed

10 files changed

+181
-0
lines changed

phs_product_auto_tags/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models

phs_product_auto_tags/__manifest__.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2020 Pharmasimple (https://www.pharmasimple.be)
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
{
4+
"name": "Product Auto Tags",
5+
"summary": """This module allow to set and unset automatically tags
6+
(new,top100delivered) on products
7+
""",
8+
"version": "14.0.1.0.0",
9+
"category": "Product",
10+
"website": "https://github.com/akretion/phs-addons",
11+
"author": " Akretion",
12+
"license": "AGPL-3",
13+
"application": False,
14+
"installable": True,
15+
"depends": [
16+
"product",
17+
"product_template_tags",
18+
],
19+
"data": [
20+
"data/ir_cron_data.xml",
21+
"data/product_template_data.xml",
22+
],
23+
"demo": [],
24+
"qweb": [],
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<odoo>
3+
<data>
4+
<record id="ir_cron_new_tag_product" model="ir.cron">
5+
<field name="name">Product; Del the tag 'new' on product</field>
6+
<field name="interval_number">1</field>
7+
<field name="interval_type">days</field>
8+
<field name="numbercall">-1</field>
9+
<field
10+
name="nextcall"
11+
eval="(DateTime.now().replace(hour=2, minute=0) + timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')"
12+
/>
13+
<field name="doall" eval="False" />
14+
<field name="model_id" ref="product.model_product_template" />
15+
<field name="code">model.untag_product()</field>
16+
<field name="state">code</field>
17+
</record>
18+
19+
<record id="ir_cron_tag_top_100_delivered" model="ir.cron">
20+
<field name="name">Product; Update tag top 100 for delivered product</field>
21+
<field name="interval_number">1</field>
22+
<field name="interval_type">days</field>
23+
<field name="numbercall">-1</field>
24+
<field
25+
name="nextcall"
26+
eval="(DateTime.now().replace(hour=2, minute=0) + timedelta(days=1)).strftime('%Y-%m-%d %H:%M:%S')"
27+
/>
28+
<field name="doall" eval="False" />
29+
<field name="model_id" ref="product.model_product_template" />
30+
<field name="code">model.top_100_delivered_product()</field>
31+
<field name="state">code</field>
32+
</record>
33+
</data>
34+
</odoo>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo noupdate="1">
3+
<record id="tag_new" model="product.template.tag">
4+
<field name="name">new</field>
5+
<field name="company_id" ref="base.main_company" />
6+
</record>
7+
<record id="tag_top_100_delivered" model="product.template.tag">
8+
<field name="name">top100 delivered</field>
9+
<field name="company_id" ref="base.main_company" />
10+
</record>
11+
<record id="nbr_days_tag_new" model="ir.config_parameter">
12+
<field name="key">nbr_days_tag_new</field>
13+
<field name="value">15</field>
14+
</record>
15+
</odoo>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import product
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from datetime import datetime, timedelta
2+
3+
from odoo import api, models
4+
5+
6+
class ProductTemplate(models.Model):
7+
_inherit = "product.template"
8+
9+
@api.model
10+
def create(self, vals):
11+
tag_new_id = self.env.ref("phs_product_auto_tags.tag_new")
12+
if "tag_ids" in vals and tag_new_id.id not in vals["tag_ids"]:
13+
vals["tag_ids"].append((4, tag_new_id.id))
14+
15+
return super(ProductTemplate, self).create(vals)
16+
17+
def update_tag_new(self):
18+
nbr_days_tag_new = (
19+
self.env["ir.config_parameter"].sudo().get_param("nbr_days_tag_new", 10)
20+
)
21+
date_new_product = datetime.today() - timedelta(days=int(nbr_days_tag_new))
22+
new_tag_id = self.env.ref("phs_product_auto_tags.tag_new")
23+
self.search(
24+
[("tag_ids", "=", new_tag_id.id), ("create_date", ">", date_new_product)]
25+
).write({"tag_ids": [(3, new_tag_id.id)]})
26+
27+
def top_100_delivered_product(self):
28+
nbr_days_tag_top100 = (
29+
self.env["ir.config_parameter"]
30+
.sudo()
31+
.get_param("nbr_days_tag_top100_delivered", 10)
32+
)
33+
date_nbr_days = datetime.today() - timedelta(days=int(nbr_days_tag_top100))
34+
tag_top_100_delivered = self.env.ref(
35+
"phs_product_auto_tags.tag_top_100_delivered"
36+
)
37+
product_list = self.env["sale.order.line"].read_group(
38+
[("create_date", ">", date_nbr_days)],
39+
fields=["qty_delivered"],
40+
groupby=["product_id", "qty_delivered"],
41+
limit=100,
42+
orderby="qty_delivered desc",
43+
)
44+
new_top_100_ids = [r for r in map(lambda r: r["product_id"][0], product_list)]
45+
new_top_100_ids = (
46+
self.env["product.product"]
47+
.search(["id", "in", new_top_100_ids])
48+
.mapped("product_tmpl_id")
49+
)
50+
self.search([("tag_ids", "=", tag_top_100_delivered.id)]).write(
51+
{"tag_ids": [(3, tag_top_100_delivered.id)]}
52+
)
53+
self.search(
54+
[
55+
("id", "in", new_top_100_ids),
56+
]
57+
).write({"tag_ids": [(4, tag_top_100_delivered.id)]})
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_product
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from dateutil.relativedelta import relativedelta
2+
3+
from odoo.tests.common import TransactionCase
4+
5+
6+
class TestProductAutoTags(TransactionCase):
7+
"""Tests tags on product"""
8+
9+
def setUp(self):
10+
super(TestProductAutoTags, self).setUp()
11+
self.product_template = self.env["product.template"]
12+
self.product_categ_id = self.env["product.category"].search(
13+
[("name", "=", "All")]
14+
)
15+
self.tag_new = self.env.ref("phs_product_auto_tags.tag_new")
16+
17+
def test_new_tag_product(self):
18+
"""Test if when product is created, the tag 'new' is set on the product """
19+
vals = {
20+
"name": "test_product",
21+
"type": "consu",
22+
"categ_id": self.product_categ_id.id,
23+
"tag_ids": [],
24+
}
25+
new_product = self.product_template.create(vals)
26+
self.assertTrue(self.tag_new.id in new_product.tag_ids.ids)
27+
28+
def test_untagged_new_on_product(self):
29+
"""Test for check if the tag new is del after few days"""
30+
vals = {
31+
"name": "test_product",
32+
"type": "consu",
33+
"categ_id": self.product_categ_id.id,
34+
"tag_ids": [],
35+
# "create_date": datetime.now() - timedelta(days=14)
36+
}
37+
new_product = self.product_template.create(vals)
38+
new_product.create_date += relativedelta(days=-14)
39+
self.product_template.update_tag_new()
40+
self.assertFalse(self.tag_new.id in new_product.tag_ids.ids)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../phs_product_auto_tags

setup/phs_product_auto_tags/setup.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import setuptools
2+
3+
setuptools.setup(
4+
setup_requires=['setuptools-odoo'],
5+
odoo_addon=True,
6+
)

0 commit comments

Comments
 (0)