65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
import json
|
|
import os
|
|
import glob
|
|
import csv
|
|
|
|
def main():
|
|
uploads_dir = "backend/uploads"
|
|
if not os.path.exists(uploads_dir):
|
|
uploads_dir = "uploads"
|
|
|
|
csv_path = os.path.join(uploads_dir, "sku_master.csv")
|
|
if not os.path.exists(csv_path):
|
|
print(f"Error: sku_master.csv not found at {csv_path}")
|
|
return
|
|
|
|
# Load SKU map
|
|
sku_map = {}
|
|
with open(csv_path, "r", encoding="utf-8") as f:
|
|
reader = csv.reader(f)
|
|
header = next(reader, None) # skip header
|
|
for row in reader:
|
|
if len(row) >= 2:
|
|
sku_map[row[0].strip()] = row[1].strip()
|
|
|
|
print(f"Loaded {len(sku_map)} SKU mappings from master list.")
|
|
|
|
manual_pattern = os.path.join(uploads_dir, "manual_label_*.json")
|
|
manual_files = glob.glob(manual_pattern)
|
|
modified_files = 0
|
|
filled_items = 0
|
|
|
|
for mf in manual_files:
|
|
try:
|
|
with open(mf, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
is_modified = False
|
|
items = data.get("items", [])
|
|
for item in items:
|
|
sku = item.get("kodeBarang", "").strip()
|
|
name = item.get("namaBarang", "").strip()
|
|
|
|
# If name is empty, try to fill it
|
|
if not name and sku:
|
|
master_name = sku_map.get(sku)
|
|
if master_name:
|
|
item["namaBarang"] = master_name
|
|
is_modified = True
|
|
filled_items += 1
|
|
|
|
if is_modified:
|
|
with open(mf, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, indent=2, ensure_ascii=False)
|
|
modified_files += 1
|
|
|
|
except Exception as e:
|
|
print(f"Error processing manual label {mf}: {e}")
|
|
|
|
print(f"Finished item name fill process.")
|
|
print(f"Files updated: {modified_files}")
|
|
print(f"Item names filled: {filled_items}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|