mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[MIG] product_route_profile: Migration to 17.0
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
{
|
||||
"name": "Product Route Profile",
|
||||
"summary": "Add Route profile concept on product",
|
||||
"version": "15.0.1.0.0",
|
||||
"version": "17.0.1.0.0",
|
||||
"category": "Warehouse",
|
||||
"website": "https://github.com/OCA/stock-logistics-warehouse",
|
||||
"author": "Akretion, Odoo Community Association (OCA)",
|
||||
|
||||
@@ -4,18 +4,14 @@
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
from odoo import SUPERUSER_ID, api
|
||||
|
||||
|
||||
def post_init_hook(cr, registry):
|
||||
def post_init_hook(env):
|
||||
def get_profile(route_ids):
|
||||
route_ids = tuple(set(route_ids))
|
||||
profile = route2profile.get(route_ids)
|
||||
if not profile:
|
||||
profile_name = ""
|
||||
route_names = [
|
||||
rec.name for rec in env["stock.location.route"].browse(route_ids)
|
||||
]
|
||||
route_names = [rec.name for rec in env["stock.route"].browse(route_ids)]
|
||||
profile_name = " / ".join(route_names)
|
||||
profile = env["route.profile"].create(
|
||||
{
|
||||
@@ -26,13 +22,12 @@ def post_init_hook(cr, registry):
|
||||
route2profile[route_ids] = profile
|
||||
return profile
|
||||
|
||||
env = api.Environment(cr, SUPERUSER_ID, {})
|
||||
query = """
|
||||
SELECT product_id, array_agg(route_id)
|
||||
FROM stock_route_product group by product_id;
|
||||
"""
|
||||
cr.execute(query)
|
||||
results = cr.fetchall()
|
||||
env.cr.execute(query)
|
||||
results = env.cr.fetchall()
|
||||
route2profile = {}
|
||||
profile2product = defaultdict(lambda: env["product.template"])
|
||||
for row in results:
|
||||
|
||||
@@ -64,11 +64,24 @@ class ProductTemplate(models.Model):
|
||||
"route_ids": [(6, 0, self.route_ids.ids)],
|
||||
}
|
||||
|
||||
@api.model
|
||||
def create(self, vals):
|
||||
route_profile_id = vals.get("route_profile_id", False)
|
||||
if route_profile_id:
|
||||
route_profile = self.env["route.profile"].browse(route_profile_id)
|
||||
vals["route_ids"] = [(6, 0, route_profile.route_ids.ids)]
|
||||
self = self.with_context(skip_inverse_route_ids=True)
|
||||
return super(ProductTemplate, self).create(vals)
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
vals_with_profile = []
|
||||
vals_without_profile = []
|
||||
for vals in vals_list:
|
||||
route_profile_id = vals.get("route_profile_id")
|
||||
if route_profile_id:
|
||||
vals = vals.copy()
|
||||
route_profile = self.env["route.profile"].browse(route_profile_id)
|
||||
vals["route_ids"] = [(6, 0, route_profile.route_ids.ids)]
|
||||
vals_with_profile.append(vals)
|
||||
else:
|
||||
vals_without_profile.append(vals)
|
||||
res = self.env["product.template"]
|
||||
if vals_without_profile:
|
||||
res += super().create(vals_without_profile)
|
||||
if vals_with_profile:
|
||||
res += super(
|
||||
ProductTemplate, self.with_context(skip_inverse_route_ids=True)
|
||||
).create(vals_with_profile)
|
||||
return res
|
||||
|
||||
@@ -14,10 +14,9 @@ class RouteProfile(models.Model):
|
||||
comodel_name="res.company",
|
||||
default=lambda self: self.env.company.id,
|
||||
required=False,
|
||||
string="Company",
|
||||
)
|
||||
route_ids = fields.Many2many(
|
||||
"stock.location.route",
|
||||
comodel_name="stock.route",
|
||||
string="Routes",
|
||||
domain=[("product_selectable", "=", True)],
|
||||
)
|
||||
|
||||
@@ -8,7 +8,7 @@ from odoo.tests.common import TransactionCase
|
||||
class TestProductRouteProfile(TransactionCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(TestProductRouteProfile, cls).setUpClass()
|
||||
super().setUpClass()
|
||||
|
||||
cls.company_bis = cls.env["res.company"].create(
|
||||
{
|
||||
|
||||
@@ -9,16 +9,16 @@
|
||||
<field name="inherit_id" ref="product.product_template_form_view" />
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='route_ids']/parent::div" position="attributes">
|
||||
<attribute name="attrs">{'invisible': True}</attribute>
|
||||
<attribute name="invisible">True</attribute>
|
||||
</xpath>
|
||||
<xpath expr="//group[@name='operations']" position="inside">
|
||||
<field
|
||||
name="route_profile_id"
|
||||
attrs="{'invisible': [('type', 'in', ['service', 'digital'])]}"
|
||||
invisible="type not in ['product', 'consu']"
|
||||
/>
|
||||
<field
|
||||
name="force_route_profile_id"
|
||||
attrs="{'invisible': [('type', 'in', ['service', 'digital'])]}"
|
||||
invisible="type not in ['product', 'consu']"
|
||||
/>
|
||||
</xpath>
|
||||
</field>
|
||||
|
||||
Reference in New Issue
Block a user