Spaces:
Sleeping

doluong6 commited on
Commit
deffb9b
·
verified ·
1 Parent(s): 51ce01d

Create pyc_chua_hoan_thanh

Browse files
Files changed (1) hide show
  1. routes/pyc_chua_hoan_thanh +86 -0
routes/pyc_chua_hoan_thanh ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter
2
+ from huggingface_hub import list_repo_files, hf_hub_download
3
+ from openpyxl import load_workbook
4
+ import os
5
+
6
+ router = APIRouter()
7
+
8
+ REPO_ID = "doluong6/Quatest1"
9
+ HF_TOKEN = os.environ.get("HF_TOKEN")
10
+
11
+ @router.get("/pyc_chua_hoan_thanh")
12
+ def pyc_chua_hoan_thanh():
13
+ try:
14
+ all_files = list_repo_files(repo_id=REPO_ID, repo_type="dataset", token=HF_TOKEN)
15
+
16
+ # File GCN trong Scan
17
+ gcn_files = [
18
+ f for f in all_files
19
+ if f.startswith("Khach_hang/")
20
+ and "/Scan/" in f
21
+ and "GCN" in os.path.basename(f).upper()
22
+ ]
23
+ khach_co_gcn = set(f.split("/")[1] for f in gcn_files if len(f.split("/")) >= 3)
24
+
25
+ # File phiếu yêu cầu
26
+ pyc_files = [
27
+ f for f in all_files
28
+ if f.startswith("Khach_hang/")
29
+ and "/Phieu_yeu_cau/" in f
30
+ and f.endswith(".xlsx")
31
+ ]
32
+
33
+ ket_qua = []
34
+ for file in pyc_files:
35
+ parts = file.split("/")
36
+ if len(parts) < 3:
37
+ continue
38
+ ten_khach = parts[1]
39
+ if ten_khach in khach_co_gcn:
40
+ continue # bỏ qua khách đã có GCN
41
+
42
+ try:
43
+ local = hf_hub_download(
44
+ repo_id=REPO_ID,
45
+ repo_type="dataset",
46
+ filename=file,
47
+ token=HF_TOKEN,
48
+ cache_dir="/tmp"
49
+ )
50
+ wb = load_workbook(local, data_only=True)
51
+ sheet = wb.active
52
+
53
+ # Lấy mã PYC từ D3:O3
54
+ pyc_code = "".join(str(sheet[f"{col}3"].value or "") for col in "DEFGHIJKLMNO").strip()
55
+
56
+ # Lấy tên khách hàng từ I8:CD8
57
+ cols_kh = [
58
+ "I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X",
59
+ "Y","Z","AA","AB","AC","AD","AE","AF","AG","AH","AI","AJ","AK",
60
+ "AL","AM","AN","AO","AP","AQ","AR","AS","AT","AU","AV","AW","AX",
61
+ "AY","AZ","BA","BB","BC","BD","BE","BF","BG","BH","BI","BJ","BK",
62
+ "BL","BM","BN","BO","BP","BQ","BR","BS","BT","BU","BV","BW","BX",
63
+ "BY","BZ","CA","CB","CC","CD"
64
+ ]
65
+ khach_hang = "".join(str(sheet[f"{col}8"].value or "") for col in cols_kh).strip()
66
+
67
+ # Lấy ngày tạo từ BE3:CA3
68
+ cols_ngay = ["BE","BF","BG","BH","BI","BJ","BK","BL","BM","BN",
69
+ "BO","BP","BQ","BR","BS","BT","BU","BV","BW","BX",
70
+ "BY","BZ","CA"]
71
+ ngay_tao = " ".join(str(sheet[f"{col}3"].value or "") for col in cols_ngay).strip()
72
+
73
+ ket_qua.append({
74
+ "phieu_yeu_cau": pyc_code or "Không rõ",
75
+ "khach_hang": khach_hang or "Không rõ",
76
+ "ngay_tao_phieu": ngay_tao or "Không rõ"
77
+ })
78
+
79
+ except Exception as e:
80
+ print(f"Lỗi đọc file {file}: {e}")
81
+ continue
82
+
83
+ return ket_qua
84
+
85
+ except Exception as e:
86
+ return {"error": str(e)}