|
| 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)]}) |
0 commit comments