Skip to content

Commit 1ccdd2b

Browse files
committed
modif import product
1 parent 44bbc0e commit 1ccdd2b

File tree

4 files changed

+831
-435
lines changed

4 files changed

+831
-435
lines changed

import_helper_generic/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"import_helper_base",
1414
"phone_validation", # would be nice to avoid depending on it ?
1515
"partner_import_helper",
16-
# "product_import_helper",
16+
"product_import_helper",
1717
# "account_import_helper",
1818
],
1919
"installable": True,

import_helper_generic/views/technical_view_menu_generic.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</group>
1111
<group name="main" col="3">
1212
<button name="partner_import_generic" type="object" string="Partner import"/>
13+
<button name="product_import_generic" type="object" string="Product import"/>
1314
</group>
1415
</form>
1516
</field>

import_helper_generic/wizards/import_helper_generic.py

Lines changed: 191 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
"delivery_phone",
3535
"delivery_mobile",
3636
"delivery_lang",
37+
"attributes",
38+
"ref_supplier",
39+
"ref_template",
3740
]
3841

3942

@@ -43,16 +46,23 @@ class ImportHelpergeneric(models.TransientModel):
4346

4447
file_import = fields.Binary(string="File to import")
4548

46-
def speedy_categori_id(self):
49+
def speedy_partner_categori_id(self):
4750
categ_id = {}
4851
categs = self.env["res.partner.category"].search([])
4952
for c in categs:
5053
categ_id[c.name] = c.id
5154
return categ_id
5255

53-
def check_vals(self, vals):
56+
def speedy_partner_id(self):
57+
partner_list = {}
58+
partner_ids = self.env["res.partner"].search([])
59+
for p in partner_ids:
60+
partner_list[p.ref] = p.id
61+
return partner_list
62+
63+
def check_vals_partner(self, vals):
5464
if "category_id" in vals:
55-
categ_ids = self.speedy_categori_id()
65+
categ_ids = self.speedy_partner_categori_id()
5666
list_categ = []
5767
for ctname in vals["category_id"].split("/"):
5868
if ctname and vals["category_id"] in categ_ids:
@@ -92,8 +102,8 @@ def partner_import_generic(self):
92102
speedy = import_obj._prepare_speedy(aiengine="NONE")
93103
line = 0
94104
colonnes = []
95-
speedy_categ_id = self.speedy_categori_id()
96-
for row in reader.iter_rows(min_row=4, max_col=39, values_only=True):
105+
partner_ids_list = self.speedy_partner_id()
106+
for row in reader.iter_rows(min_row=4, values_only=True):
97107
vals = {}
98108
if row[0] == "Colonnes:":
99109
for c in range(len(row)):
@@ -109,7 +119,181 @@ def partner_import_generic(self):
109119
for c in range(len(row)):
110120
if row[c] and colonnes[c] != "empty":
111121
vals[colonnes[c]] = str(row[c])
112-
vals = self.check_vals(vals)
113-
import_obj._create_partner(vals, speedy)
122+
vals = self.check_vals_partner(vals)
123+
if vals["ref"] in partner_ids_list:
124+
import_obj._prepare_partner_vals(vals, speedy)
125+
res = (
126+
self.env["res.partner"]
127+
.browse(partner_ids_list[vals["ref"]])
128+
.write(vals)
129+
)
130+
if res:
131+
logger.info(
132+
f"{res.display_name},id {res.id} has been update with line {line}"
133+
)
134+
else:
135+
import_obj._create_partner(vals, speedy)
136+
action = import_obj._result_action(speedy)
137+
return action
138+
139+
def prepare_speedy_attribute_value(self):
140+
speedy_attribute_value = {}
141+
attribute_value = self.env["product.attribute.value"].search([])
142+
for att in attribute_value:
143+
speedy_attribute_value[att.fullname] = {
144+
"id": att.id,
145+
"attribute_id": att.attribute_id.id,
146+
}
147+
return speedy_attribute_value
148+
149+
def check_vals_product(self, vals):
150+
variant_att = ()
151+
list_attribute_ids = {}
152+
if "attributes" in vals:
153+
speedy_attribute_value = self.prepare_speedy_attribute_value()
154+
variant_att = vals["attributes"].split("/")
155+
for v in variant_att:
156+
if speedy_attribute_value.get(v):
157+
if speedy_attribute_value[v]["attribute_id"] in list_attribute_ids:
158+
list_attribute_ids[
159+
speedy_attribute_value[v]["attribute_id"]
160+
].append(
161+
speedy_attribute_value[v]["id"],
162+
)
163+
else:
164+
list_attribute_ids[
165+
speedy_attribute_value[v]["attribute_id"]
166+
] = [speedy_attribute_value[v]["id"]]
167+
168+
if "ref_supplier" in vals:
169+
speedy_partner_id = self.speedy_partner_id()
170+
if vals["ref_supplier"] in speedy_partner_id:
171+
vals["supplier_id"] = speedy_partner_id[vals["ref_supplier"]]
172+
for i in LIST_COL_POP:
173+
if vals.get(i):
174+
vals.pop(i)
175+
return vals, variant_att, list_attribute_ids
176+
177+
def product_import_generic(self):
178+
fileobj = NamedTemporaryFile(
179+
"wb+", prefix="odoo-import_helper-", suffix=".xlsx"
180+
)
181+
file_bytes = base64.b64decode(self.file_import)
182+
fileobj.write(file_bytes)
183+
fileobj.seek(0)
184+
dataframe = opx.load_workbook(fileobj.name, read_only=True)
185+
reader = dataframe.active
186+
import_obj = self.env["import.helper"]
187+
speedy = import_obj._prepare_speedy(aiengine="NONE")
188+
line = 0
189+
colonnes = []
190+
product_ids = self.env["product.product"].search([])
191+
speedy_product_list = {}
192+
for p in product_ids:
193+
speedy_product_list[p.default_code] = p.id
194+
product_template_ids = self.env["product.template"].search([])
195+
speedy_product_template_list = {}
196+
for p in product_template_ids:
197+
speedy_product_template_list[p.default_code] = p.id
198+
list_product_create = {}
199+
for row in reader.iter_rows(min_row=4, values_only=True):
200+
vals = {}
201+
if row[0] == "Colonnes:":
202+
for c in range(len(row)):
203+
if row[c]:
204+
colonnes.append(row[c])
205+
else:
206+
colonnes.append("empty")
207+
continue
208+
209+
if row[1]:
210+
line += 1
211+
vals["line"] = line
212+
for c in range(len(row)):
213+
if row[c] and colonnes[c] != "empty":
214+
vals[colonnes[c]] = str(row[c])
215+
if (
216+
vals.get("ref_template")
217+
and vals["ref_template"] in speedy_product_template_list
218+
):
219+
vals["product_tmpl_id"] = speedy_product_template_list[
220+
vals["ref_template"]
221+
]
222+
vals, variant_att, list_attribue_ids = self.check_vals_product(vals)
223+
if (
224+
not vals.get("product_tmpl_id")
225+
and vals["default_code"] in speedy_product_template_list
226+
):
227+
location_id = vals.get("location_id") or speedy.get(
228+
"default_location_id"
229+
)
230+
vals = import_obj._prepare_product_vals(vals, location_id, speedy)
231+
res = (
232+
self.env["product.template"]
233+
.browse(speedy_product_list[vals["default_code"]])
234+
.write(vals)
235+
)
236+
if res:
237+
logger.info(
238+
f"{res.display_name},id {res.id} has been update with line {line}"
239+
)
240+
else:
241+
logger.warning(f"line {line} have done nothing")
242+
elif vals["default_code"] in speedy_product_list:
243+
location_id = vals.get("location_id") or speedy.get(
244+
"default_location_id"
245+
)
246+
vals = import_obj._prepare_product_vals(vals, location_id, speedy)
247+
res = (
248+
self.env["product.product"]
249+
.browse(speedy_product_list[vals["default_code"]])
250+
.write(vals)
251+
)
252+
if res:
253+
logger.info(
254+
f"{res.display_name}, id {res.id} has been update with line {line}"
255+
)
256+
else:
257+
logger.warning(f"line {line} have done nothing")
258+
elif vals.get("product_tmpl_id"):
259+
location_id = vals.get("location_id") or speedy.get(
260+
"default_location_id"
261+
)
262+
vals = import_obj._prepare_product_vals(vals, location_id, speedy)
263+
if vals["product_tmpl_id"] in list_product_create:
264+
template = list_product_create[vals["product_tmpl_id"]]
265+
else:
266+
template = self.env["product.template"].browse(
267+
vals["product_tmpl_id"]
268+
)
269+
for p in template.product_variant_ids:
270+
if p.product_template_attribute_value_ids:
271+
for v in p.product_template_attribute_value_ids:
272+
if v.product_attribute_value_id.fullname in variant_att:
273+
vals["standard_price"] = float(
274+
vals["standard_price"]
275+
)
276+
p.write(vals)
277+
else:
278+
location_id = vals.get("location_id") or speedy.get(
279+
"default_location_id"
280+
)
281+
vals = import_obj._prepare_product_vals(vals, location_id, speedy)
282+
p_tmpl = self.env["product.template"].create(vals)
283+
speedy_product_template_list[p_tmpl.default_code] = p_tmpl.id
284+
list_product_create[p_tmpl.id] = p_tmpl
285+
if p_tmpl:
286+
for att in list_attribue_ids:
287+
b = list_attribue_ids[att]
288+
p_tmpl.attribute_line_ids = [
289+
Command.create(
290+
{"attribute_id": att, "value_ids": [Command.set(b)]}
291+
)
292+
]
293+
logger.info(f"{p_tmpl.id} has been create")
294+
else:
295+
logger.warning("nothing")
296+
else:
297+
break
114298
action = import_obj._result_action(speedy)
115299
return action

0 commit comments

Comments
 (0)