Skip to content

Commit ccdf727

Browse files
committed
[MIG] pos_remove_pos_category: migration 18.0
1 parent 427aaf5 commit ccdf727

15 files changed

+151
-130
lines changed

pos_remove_pos_category/__manifest__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111
],
1212
"website": "https://github.com/OCA/pos",
1313
"data": [
14-
"views/assets.xml",
1514
"views/pos_view.xml",
1615
"views/pos_category_view.xml",
1716
],
17+
"assets": {
18+
"point_of_sale._assets_pos": [
19+
"/pos_remove_pos_category/static/src/overrides/components/**"
20+
],
21+
},
1822
"installable": True,
1923
"license": "AGPL-3",
2024
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
from . import module
22
from . import product
3+
from . import pos_session
4+
from . import product_category
5+
from . import res_config_settings
6+
from . import pos_config

pos_remove_pos_category/models/module.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@ class Module(models.Model):
1010
def module_uninstall(self):
1111
for module in self:
1212
if module.name == "pos_remove_pos_category":
13-
# As we have loose previous POS categs restore them
14-
# in a sane empty state
15-
16-
self._cr.execute("UPDATE product_template SET pos_categ_id=NULL")
17-
# And restore original constraint
18-
self._cr.execute(
19-
"""
20-
ALTER TABLE product_template
21-
DROP CONSTRAINT IF EXISTS
22-
product_template_pos_categ_id_fkey
23-
"""
24-
)
25-
26-
self._cr.execute(
27-
"""
28-
ALTER TABLE product_template ADD CONSTRAINT
29-
"product_template_pos_categ_id_fkey"
30-
FOREIGN KEY (pos_categ_id)
31-
REFERENCES pos_category(id) ON DELETE SET NULL;
32-
"""
33-
)
34-
3513
# Restore POS category menu action
3614
# in SQL because pool/env is not available here
3715
self._cr.execute(
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (C) 2015-TODAY Akretion (<http://www.akretion.com>).
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
4+
from odoo import fields, models
5+
6+
7+
class PosConfig(models.Model):
8+
_inherit = "pos.config"
9+
10+
iface_available_categ_ids = fields.Many2many("product.category")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (C) 2015-TODAY Akretion (<http://www.akretion.com>).
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
4+
from odoo import models
5+
6+
7+
class PosSession(models.Model):
8+
_inherit = "pos.session"
9+
10+
def load_data(self, models_to_load, only_data=False):
11+
res = super().load_data(models_to_load, only_data=only_data)
12+
res["pos.category"] = res.pop("product.category")
13+
return res
14+
15+
def _load_pos_data_models(self, config_id):
16+
res = super()._load_pos_data_models(config_id)
17+
res.append("product.category")
18+
return res
19+
20+
def _load_pos_data_relations(self, model, response):
21+
super()._load_pos_data_relations(model, response)
22+
if model == "product.category":
23+
response[model]["relations"]["parent_id"]["relation"] = "pos.category"
24+
response[model]["relations"]["child_ids"]["relation"] = "pos.category"
25+
if model == "product.product":
26+
response[model]["relations"]["pos_categ_ids"]["relation"] = "pos.category"
27+
return

pos_remove_pos_category/models/product.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,40 @@
77
class ProductTemplate(models.Model):
88
_inherit = "product.template"
99

10-
pos_categ_id = fields.Many2one(
10+
pos_categ_ids = fields.Many2many(
1111
"product.category",
1212
store=False,
13-
related="categ_id",
14-
search="_search_pos_categ_id",
13+
compute="_compute_pos_categ_ids",
14+
search="_search_pos_categ_ids",
1515
)
1616

17-
def _search_pos_categ_id(self, operator, value):
17+
def _compute_pos_categ_ids(self):
18+
for product in self:
19+
if product.categ_id and product.categ_id.available_in_pos:
20+
product.pos_categ_ids = [(6, 0, [product.categ_id.id])]
21+
else:
22+
product.pos_categ_ids = False
23+
24+
def _search_pos_categ_ids(self, operator, value):
1825
return [("categ_id", operator, value)]
1926

20-
@api.model
21-
def create(self, vals):
22-
if "categ_id" in vals:
23-
vals["pos_categ_id"] = vals["categ_id"]
24-
return super().create(vals)
27+
@api.model_create_multi
28+
def create(self, vals_list):
29+
for vals in vals_list:
30+
if "categ_id" in vals:
31+
vals["pos_categ_ids"] = [(6, 0, [vals["categ_id"]])]
32+
return super().create(vals_list)
2533

2634
def write(self, vals):
27-
if "pos_categ_id" in vals and not vals["pos_categ_id"]:
28-
del vals["pos_categ_id"]
35+
if "pos_categ_ids" in vals and not vals["pos_categ_ids"]:
36+
del vals["pos_categ_ids"]
2937
return super().write(vals)
3038

3139

3240
class ProductCategory(models.Model):
3341
_inherit = "product.category"
3442

35-
image_128 = fields.Image("Image 128", max_width=128, max_height=128)
43+
image_128 = fields.Image(max_width=128, max_height=128)
3644

3745
available_in_pos = fields.Boolean(
3846
string="Available in the Point of Sale",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (C) 2015-TODAY Akretion (<http://www.akretion.com>).
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
4+
import random
5+
6+
from odoo import api, fields, models
7+
8+
9+
class ProductCategory(models.Model):
10+
_inherit = ["product.category", "pos.load.mixin"]
11+
_name = "product.category"
12+
13+
def get_default_color(self):
14+
return random.randint(0, 10)
15+
16+
child_ids = fields.One2many(string="POS child categories", related="child_id")
17+
has_image = fields.Boolean(compute="_compute_has_image")
18+
color = fields.Integer(required=False, default=get_default_color)
19+
sequence = fields.Integer(
20+
help="Gives the sequence order when displaying a list of product categories."
21+
)
22+
23+
@api.model
24+
def _load_pos_data_domain(self, data):
25+
config_id = self.env["pos.config"].browse(data["pos.config"]["data"][0]["id"])
26+
domain = (
27+
[("id", "in", config_id._get_available_categories().ids)]
28+
if config_id.limit_categories and config_id.iface_available_categ_ids
29+
else []
30+
)
31+
return domain
32+
33+
@api.model
34+
def _load_pos_data_fields(self, config_id):
35+
return [
36+
"id",
37+
"name",
38+
"parent_id",
39+
"child_ids",
40+
"write_date",
41+
"has_image",
42+
"color",
43+
"sequence",
44+
]
45+
46+
@api.depends("has_image")
47+
def _compute_has_image(self):
48+
for category in self:
49+
category.has_image = bool(category.image_128)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (C) 2015-TODAY Akretion (<http://www.akretion.com>).
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
3+
4+
from odoo import fields, models
5+
6+
7+
class ResConfigSettings(models.TransientModel):
8+
_inherit = "res.config.settings"
9+
10+
pos_iface_available_categ_ids = fields.Many2many("product.category")

pos_remove_pos_category/static/src/js/CategoryButton.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

pos_remove_pos_category/static/src/js/Chrome.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

pos_remove_pos_category/static/src/js/models.js

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {ProductScreen} from "@point_of_sale/app/screens/product_screen/product_screen";
2+
import {patch} from "@web/core/utils/patch";
3+
import {pick} from "@web/core/utils/objects";
4+
5+
patch(ProductScreen.prototype, {
6+
getChildCategoriesInfo(category) {
7+
return {
8+
...pick(category, "id", "name", "color"),
9+
imgSrc:
10+
this.pos.config.show_category_images && category.has_image
11+
? `/web/image?model=product.category&field=image_128&id=${category.id}`
12+
: undefined,
13+
isSelected: this.getAncestorsAndCurrent().includes(category),
14+
isChildren: this.getChildCategories(this.pos.selectedCategory).includes(
15+
category
16+
),
17+
};
18+
},
19+
});

pos_remove_pos_category/views/assets.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

pos_remove_pos_category/views/pos_category_view.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<group>
1111
<field name="available_in_pos" />
1212
</group>
13+
<group>
14+
<field name="color" widget="color_picker" />
15+
</group>
1316
</field>
1417
<xpath expr="//field[@name='name']/../.." position="before">
1518
<field name="image_128" widget="image" class="oe_avatar" />

pos_remove_pos_category/views/pos_view.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<odoo>
33
<record id="product_template_form_view" model="ir.ui.view">
4-
<field name="name">hide.pos_categ_id.product.template.form</field>
4+
<field name="name">hide.pos_categ_ids.product.template.form</field>
55
<field name="model">product.template</field>
66
<field name="inherit_id" ref="point_of_sale.product_template_form_view" />
77
<field name="arch" type="xml">
8-
<field name="pos_categ_id" position="attributes">
9-
<attribute name="invisible">1</attribute>
8+
<field name="pos_categ_ids" position="attributes">
9+
<attribute name="invisible">0</attribute>
10+
<attribute name="options">{}</attribute>
1011
</field>
1112
</field>
1213
</record>

0 commit comments

Comments
 (0)