import csv from pathlib import Path from ee.logging import log from ee.project import Project from ee.tools import parse_bool def restructure(in_path: Path, in_format, out_path: Path, out_format): log.info("Creating {}".format(out_path)) if in_format == "digikey-order": with in_path.open("r") as f: data = list(csv.reader(f)) header = {k: index for index, k in enumerate(data[0])} description_i = header["Description"] pn_i = header["Part Number"] mpn_i = header["Manufacturer Part Number"] unit_price_i = header["Unit Price"] extended_price_i = header["Extended Price"] # If you search in Odoo for Digikey this is the correct name. digikey_name = "Digi-Key Electronics" rows = ([row[description_i], row[pn_i], row[mpn_i], row[unit_price_i], row[extended_price_i]] for row in data[1:-1]) with out_path.open("w") as f: out = csv.writer(f) header = ["External ID", "Can be Purchased", "Product Type", # "Description", # Internal notes "Name", "Internal Reference", "Cost", "Product Category", # "Vendors / External ID", # "seller_ids/name", # "Vendors / Vendor Product Name", # "Vendors / Vendor Product Code", # "Vendors / Price", ] out.writerow(header) for description, pn, mpn, price, e_price in rows: external_id = "ee/mpn:" + mpn vendor_external_id = "ee/spn:{}".format(pn) row = [external_id, "yes", "Storable Product", description, mpn, price, "All / Electronics"] # row.extend([vendor_external_id, digikey_name, description, pn, price]) out.writerow(row) # break def init_project(project: Project): odoo = project.get_or_create_section("odoo") odoo["enabled"] = "yes" def generate_ninja(project: Project): enabled = parse_bool(project.get_or_create_section("odoo").get("enabled")) if not enabled: return head = """ rule odoo-restructure command = ee odoo-restructure --in $in --in-format $in_format --out $out --out-format $out_format """.strip() orders_dir = project.orders_dir files = [] targets = [] # print("orders_dir={}".format(orders_dir)) if orders_dir.is_dir(): for file in sorted(orders_dir.iterdir()): if file.name.endswith(".csv"): with file.open("r") as f: data = list(csv.reader(f)) if len(data) < 1: continue in_format = _classify(data) if in_format is None: continue out_format = "product-list" target = "$public_dir/odoo/{}/{}".format(out_format, file.name) files.append("build {}: odoo-restructure $orders_dir/{}\n" " in_format = {}\n" " out_format = {}\n".format(target, file.name, in_format, out_format)) # This was an attempt to get ninja to run "ee ninja" when the input csv files went away. # files.append("build $orders_dir/{}: ee-ninja".format(file.name)) targets.append(target) lines = [] lines.extend(head.splitlines()) lines.append("") lines.extend(files) lines.append("build odoo-restructure: phony $orders_dir ee.ninja $\n" + " $\n".join([" " + t for t in targets])) lines.append("default odoo-restructure") return "\n".join(lines) def _classify(data): header = data[0] if len(header) < 5: return None last_row = data[-1] print(last_row) print(last_row[-2]) if last_row[-2] == "Subtotal": return "digikey-order"