Debian Codegraph @

addons/point_of_sale/static/src/app/models/product_template.js

raw
1 import { registry } from "@web/core/registry";
2 import { markup } from "@odoo/owl";
3 import { ProductTemplateAccounting } from "./accounting/product_template_accounting";
4 import { normalize } from "@web/core/l10n/utils";
5
6 /**
7 * ProductProduct, shadow of product.product in python.
8 * To works properly, this model needs to be registered in the registry
9 * with the key "pos_available_models". And to be instanciated with the
10 * method createRelatedModels from related_models.js
11 *
12 * Models to load: product.product, uom.uom
13 */
14
15 export class ProductTemplate extends ProductTemplateAccounting {
16 static pythonModel = "product.template";
17
18 isAllowOnlyOneLot() {
19 return this.tracking === "lot" || !this.uom_id || !this.uom_id.is_pos_groupable;
20 }
21
22 isTracked() {
23 const pickingType = this.models["stock.picking.type"].readAll()[0];
24
25 return (
26 ["serial", "lot"].includes(this.tracking) &&
27 (pickingType.use_create_lots || pickingType.use_existing_lots)
28 );
29 }
30
31 async _onScaleNotAvailable() {}
32
33 isConfigurable() {
34 return this.attribute_line_ids.find(
35 (l) =>
36 l.product_template_value_ids.length > 1 ||
37 l.product_template_value_ids.some((v) => v.is_custom)
38 );
39 }
40
41 needToConfigure() {
42 return (
43 this.isConfigurable() &&
44 this.attribute_line_ids.length > 0 &&
45 this.attribute_line_ids.some((l) => l.attribute_id.create_variant === "no_variant")
46 );
47 }
48
49 isCombo() {
50 return this.combo_ids.length;
51 }
52
53 get isScaleAvailable() {
54 return true;
55 }
56
57 get parentCategories() {
58 const categories = [];
59 let category = this.categ_id;
60
61 while (category) {
62 categories.push(category.id);
63 category = category.parent_id;
64 }
65
66 return categories;
67 }
68
69 get parentPosCategIds() {
70 const current = [];
71 const categories = this.pos_categ_ids;
72
73 const getParent = (categ) => {
74 if (categ.parent_id) {
75 current.push(categ.parent_id.id);
76 getParent(categ.parent_id);
77 }
78 };
79
80 for (const category of categories) {
81 current.push(category.id);
82 getParent(category);
83 }
84
85 return current;
86 }
87
88 getImageUrl() {
89 return `/web/image?model=product.template&field=image_128&id=${this.id}&unique=${this.write_date}`;
90 }
91
92 _isArchivedCombination(attributeValueIds) {
93 if (!this._archived_combinations) {
94 return false;
95 }
96 const excludedPTAV = new Set();
97 let isCombinationArchived = false;
98 for (const archivedCombination of this._archived_combinations) {
99 const ptavCommon = archivedCombination.filter((ptav) =>
100 attributeValueIds.includes(ptav)
101 );
102 if (ptavCommon.length === attributeValueIds.length) {
103 // all attributes must be disabled from each other
104 archivedCombination.forEach((ptav) => excludedPTAV.add(ptav));
105 } else if (ptavCommon.length === attributeValueIds.length - 1) {
106 // In this case we only need to disable the remaining ptav
107 const disablePTAV = archivedCombination.find(
108 (ptav) => !attributeValueIds.includes(ptav)
109 );
110 excludedPTAV.add(disablePTAV);
111 }
112 if (ptavCommon.length === attributeValueIds.length) {
113 isCombinationArchived = true;
114 }
115 }
116 this.attribute_line_ids.forEach((attribute_line) => {
117 attribute_line.product_template_value_ids.forEach((ptav) => {
118 ptav["excluded"] = excludedPTAV.has(ptav.id);
119 });
120 });
121 return isCombinationArchived;
122 }
123
124 get productDescriptionMarkup() {
125 return this.public_description ? markup(this.public_description) : "";
126 }
127
128 get canBeDisplayed() {
129 return this.active && this.available_in_pos;
130 }
131
132 get searchString() {
133 const fields = ["name", "default_code", "barcode"];
134 const raw = fields
135 .map((field) => this[field] || "")
136 .filter(Boolean)
137 .join(" ");
138
139 const templateContent = normalize(raw);
140 const variantContent = this.product_variant_ids.map((v) => v.searchString).join(" ");
141
142 return variantContent ? templateContent + " " + variantContent : templateContent;
143 }
144
145 get normalizedName() {
146 return normalize(this.name || "");
147 }
148
149 get displayName() {
150 return this.default_code
151 ? `[${this.default_code}] ${this.display_name}`
152 : this.display_name;
153 }
154 }
155 registry.category("pos_available_models").add(ProductTemplate.pythonModel, ProductTemplate);