|
| 1 | +from typing import dataclass_transform |
| 2 | +from decorator import append |
| 3 | +from openpyxl.workbook import child |
| 4 | +from odoo import api, fields, models |
| 5 | +from tempfile import NamedTemporaryFile |
| 6 | +import base64 |
| 7 | + |
| 8 | +from odoo.fields import Command |
| 9 | + |
| 10 | +import openpyxl as opx |
| 11 | + |
| 12 | +import logging |
| 13 | +from stdnum.iban import is_valid as iban_is_valid |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | +LIST_FR = ["FR", "France", "Martinique - France", "REUNION", "Paris"] |
| 18 | +LIST_COL_POP = [ |
| 19 | + "invoice_name", |
| 20 | + "invoice_title_code", |
| 21 | + "invoice_street", |
| 22 | + "invoice_street2", |
| 23 | + "invoice_city", |
| 24 | + "invoice_zip", |
| 25 | + "invoice_phone", |
| 26 | + "invoice_mobile", |
| 27 | + "invoice_lang", |
| 28 | + "delivery_name", |
| 29 | + "delivery_title_code", |
| 30 | + "delivery_street", |
| 31 | + "delivery_street2", |
| 32 | + "delivery_city", |
| 33 | + "delivery_zip", |
| 34 | + "delivery_phone", |
| 35 | + "delivery_mobile", |
| 36 | + "delivery_lang", |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +class ImportHelpergeneric(models.TransientModel): |
| 41 | + _name = "import.helper.generic" |
| 42 | + _description = "Import helper generic for importing information with template" |
| 43 | + |
| 44 | + file_import = fields.Binary(string="File to import") |
| 45 | + |
| 46 | + def speedy_categori_id(self): |
| 47 | + categ_id = {} |
| 48 | + categs = self.env["res.partner.category"].search([]) |
| 49 | + for c in categs: |
| 50 | + categ_id[c.name] = c.id |
| 51 | + return categ_id |
| 52 | + |
| 53 | + def check_vals(self, vals): |
| 54 | + if "category_id" in vals: |
| 55 | + categ_ids = self.speedy_categori_id() |
| 56 | + list_categ = [] |
| 57 | + for ctname in vals["category_id"].split("/"): |
| 58 | + if ctname and vals["category_id"] in categ_ids: |
| 59 | + list_categ.append(categ_ids[ctname]) |
| 60 | + vals["category_id"] = [Command.set(list_categ)] |
| 61 | + if "invoice_name" in vals: |
| 62 | + child_val = {"type": "invoice"} |
| 63 | + vals["child_ids"] = [] |
| 64 | + for v in vals: |
| 65 | + if "invoice_" in v: |
| 66 | + child_val[v.replace("invoice_", "")] = vals[v] |
| 67 | + vals["child_ids"].append((0, 0, child_val)) |
| 68 | + if "delivery_name" in vals: |
| 69 | + child_val = {"type": "delivery"} |
| 70 | + for v in vals: |
| 71 | + if "delivery_" in v: |
| 72 | + child_val[v.replace("delivery_", "")] = vals[v] |
| 73 | + if "child_ids" in vals: |
| 74 | + vals["child_ids"].append((0, 0, child_val)) |
| 75 | + else: |
| 76 | + vals["child_ids"] = [(0, 0, child_val)] |
| 77 | + for i in LIST_COL_POP: |
| 78 | + if vals.get(i): |
| 79 | + vals.pop(i) |
| 80 | + return vals |
| 81 | + |
| 82 | + def partner_import_generic(self): |
| 83 | + fileobj = NamedTemporaryFile( |
| 84 | + "wb+", prefix="odoo-import_helper-", suffix=".xlsx" |
| 85 | + ) |
| 86 | + file_bytes = base64.b64decode(self.file_import) |
| 87 | + fileobj.write(file_bytes) |
| 88 | + fileobj.seek(0) |
| 89 | + dataframe = opx.load_workbook(fileobj.name, read_only=True) |
| 90 | + reader = dataframe.active |
| 91 | + import_obj = self.env["import.helper"] |
| 92 | + speedy = import_obj._prepare_speedy(aiengine="NONE") |
| 93 | + line = 0 |
| 94 | + 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): |
| 97 | + vals = {} |
| 98 | + if row[0] == "Colonnes:": |
| 99 | + for c in range(len(row)): |
| 100 | + if row[c]: |
| 101 | + colonnes.append(row[c]) |
| 102 | + else: |
| 103 | + colonnes.append("empty") |
| 104 | + continue |
| 105 | + |
| 106 | + if row[2]: |
| 107 | + line += 1 |
| 108 | + vals["line"] = line |
| 109 | + for c in range(len(row)): |
| 110 | + if row[c] and colonnes[c] != "empty": |
| 111 | + vals[colonnes[c]] = str(row[c]) |
| 112 | + vals = self.check_vals(vals) |
| 113 | + import_obj._create_partner(vals, speedy) |
| 114 | + action = import_obj._result_action(speedy) |
| 115 | + return action |
0 commit comments