feat: auto-fill empty item names in manual labels from sku master database table

This commit is contained in:
Rafhan Mazaya Fathurrahman committed 2026-07-02 16:46:13 +07:00
1 parent 170cd586d9
commit 6666ecd599
3 files changed
+72 -8

No files matched your search

+64
View File
@@ -0,0 +1,64 @@
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()
Binary file not shown.
+8 -8
View File
@@ -12,31 +12,31 @@
"items": [
{
"kodeBarang": "11310014",
"namaBarang": "",
"namaBarang": "AYAM SIZE 2 FROZEN (0.8-0.9)KG(*)",
"banyak": "2 KRG",
"jumlah": "40 PC"
},
{
"kodeBarang": "11310024",
"namaBarang": "",
"namaBarang": "AYAM SIZE A FROZEN (0.9-1)KG/PC(*)",
"banyak": "2 KRG",
"jumlah": "40 PC"
},
{
"kodeBarang": "11620056",
"namaBarang": "",
"namaBarang": "SBL (FILLET PAHA) 1 KG(*)",
"banyak": "1 BOX",
"jumlah": "10 KG"
},
{
"kodeBarang": "11690053",
"namaBarang": "",
"namaBarang": "SBB (FILLET DADA )1 KG(*)",
"banyak": "1 BOX",
"jumlah": "10 KG"
},
{
"kodeBarang": "11720055",
"namaBarang": "",
"namaBarang": "FULL WING FROZ PACK 1 KG(*)",
"banyak": "1 BOX",
"jumlah": "10 KG"
}
@@ -67,19 +67,19 @@
},
{
"kodeBarang": "11620056",
"namaBarang": "",
"namaBarang": "SBL (FILLET PAHA) 1 KG(*)",
"banyak": "1 BOX",
"jumlah": "10 KG"
},
{
"kodeBarang": "11690053",
"namaBarang": "",
"namaBarang": "SBB (FILLET DADA )1 KG(*)",
"banyak": "1 BOX",
"jumlah": "10 KG"
},
{
"kodeBarang": "11720055",
"namaBarang": "",
"namaBarang": "FULL WING FROZ PACK 1 KG(*)",
"banyak": "1 BOX",
"jumlah": "10 KG"
}