3434 "delivery_phone" ,
3535 "delivery_mobile" ,
3636 "delivery_lang" ,
37+ "attribues" ,
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,7 +102,7 @@ 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 ()
105+ partner_ids_list = self .speedy_partner_id ()
96106 for row in reader .iter_rows (min_row = 4 , max_col = 39 , values_only = True ):
97107 vals = {}
98108 if row [0 ] == "Colonnes:" :
@@ -109,7 +119,143 @@ 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 ():
140+ speedy_attribute_value = {}
141+ attribute_value = self .env ["product.attribue.value" ].search ([])
142+ for att in attribute_value :
143+ speedy_attribute_value [att .fullname ] = {
144+ "id" : att .id ,
145+ "attribue_id" : att .product_attribute_id .id ,
146+ }
147+ return speedy_attribute_value
148+
149+ def check_vals_product (self , vals ):
150+ variant_att = ()
151+ if "attributes" in vals :
152+ speedy_attribute_value = self .prepare_speedy_attribute_value ()
153+ variant_att = vals ["attributes" ].split ("/" )
154+ list_attribute_ids = []
155+ for v in variant_att :
156+ if speedy_attribute_value .get (v ):
157+ list_attribute_ids .append (
158+ (
159+ speedy_attribute_value [v ]["attribue_id" ],
160+ speedy_attribute_value [v ]["id" ],
161+ )
162+ )
163+ if "ref_supplier" in vals :
164+ speedy_partner_id = self .speedy_partner_id ()
165+ if vals ["ref_supplier" ] in speedy_partner_id :
166+ vals ["supplier_id" ] = speedy_partner_id [vals ["ref_supplier" ]]
167+ for i in LIST_COL_POP :
168+ if vals .get (i ):
169+ vals .pop (i )
170+ return vals , variant_att , list_attribute_ids
171+
172+ def product_import_generic (self ):
173+ fileobj = NamedTemporaryFile (
174+ "wb+" , prefix = "odoo-import_helper-" , suffix = ".xlsx"
175+ )
176+ file_bytes = base64 .b64decode (self .file_import )
177+ fileobj .write (file_bytes )
178+ fileobj .seek (0 )
179+ dataframe = opx .load_workbook (fileobj .name , read_only = True )
180+ reader = dataframe .active
181+ import_obj = self .env ["import.helper" ]
182+ speedy = import_obj ._prepare_speedy (aiengine = "NONE" )
183+ line = 0
184+ colonnes = []
185+ product_ids = self .env ["product.product" ].search ([])
186+ speedy_product_list = {}
187+ for p in product_ids :
188+ speedy_product_list [p .default_code ] = p .id
189+ product_template_ids = self .env ["product.template" ].search ([])
190+ speedy_product_template_list = {}
191+ for p in product_template_ids :
192+ speedy_product_template_list [p .default_code ] = p .id
193+ for row in reader .iter_rows (min_row = 4 , values_only = True ):
194+ vals = {}
195+ if row [0 ] == "Colonnes:" :
196+ for c in range (len (row )):
197+ if row [c ]:
198+ colonnes .append (row [c ])
199+ else :
200+ colonnes .append ("empty" )
201+ continue
202+
203+ if row [0 ]:
204+ line += 1
205+ vals ["line" ] = line
206+ for c in range (len (row )):
207+ if row [c ] and colonnes [c ] != "empty" :
208+ vals [colonnes [c ]] = str (row [c ])
209+ if vals .get ("ref_template" ):
210+ vals ["product_template_id" ] = speedy_product_template_list [
211+ vals ["ref_template" ]
212+ ]
213+ vals , variant_att = self .check_vals_product (vals )
214+ if (
215+ not vals ["product_template_id" ]
216+ and vals ["default_code" ] in speedy_product_template_list
217+ ):
218+ import_obj ._prepare_product_vals (vals , speedy )
219+ res = (
220+ self .env ["product.template" ]
221+ .browse (speedy_product_list [vals ["default_code" ]])
222+ .write (vals )
223+ )
224+ if res :
225+ logger .info (
226+ f"{ res .display_name } ,id { res .id } has been update with line { line } "
227+ )
228+ else :
229+ logger .warning (f"line { line } have done nothing" )
230+ elif vals ["default_code" ] in speedy_product_list :
231+ import_obj ._prepare_product_vals (vals , speedy )
232+ res = (
233+ self .env ["product.product" ]
234+ .browse (speedy_product_list [vals ["default_code" ]])
235+ .write (vals )
236+ )
237+ if res :
238+ logger .info (
239+ f"{ res .display_name } , id { res .id } has been update with line { line } "
240+ )
241+ else :
242+ logger .warning (f"line { line } have done nothing" )
243+ elif vals ["product_template_id" ]:
244+ import_obj ._prepare_product_vals (vals , speedy )
245+ template = self .env ["product.template" ].browse (
246+ speedy_product_template_list [vals ["template_id" ]]
247+ )
248+ for p in template .product_variant_ids :
249+ if p .product_template_attribute_value_ids :
250+ for v in p .product_template_attribute_value_ids :
251+ if v .product_attribute_value_id .fullname in variant_att :
252+ p .write (vals )
253+ else :
254+ p_tmpl = self .env ["product.template" ].create ({vals })
255+ for att in list_attribute_ids :
256+ p_tmpl .product_template_attribute_line = Command .create (
257+ {"attribue_id" : att [0 ], "value_id" : att [1 ]}
258+ )
259+ import_obj ._create_partner (vals , speedy )
114260 action = import_obj ._result_action (speedy )
115261 return action
0 commit comments