Update app.py
Browse files
app.py
CHANGED
|
@@ -728,57 +728,43 @@ def chuan_hoa_ngay(value):
|
|
| 728 |
continue
|
| 729 |
return None
|
| 730 |
|
| 731 |
-
@app.
|
| 732 |
-
async def
|
| 733 |
try:
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
if idx_ngay_het_han is None or idx_tinh_trang is None:
|
| 743 |
-
raise ValueError("Không tìm thấy cột 'NGÀY HẾT HẠN' hoặc 'TÌNH TRẠNG' trong dữ liệu.")
|
| 744 |
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
ngay_str = row[idx_ngay_het_han]
|
| 748 |
-
dt = chuan_hoa_ngay(ngay_str)
|
| 749 |
-
status = "KHÔNG RÕ"
|
| 750 |
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
|
|
|
|
|
|
|
|
|
| 757 |
else:
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
|
| 762 |
-
|
| 763 |
-
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 764 |
-
temp_path = tmp.name
|
| 765 |
-
df.to_excel(temp_path, sheet_name="HOA CHAT SU DUNG", index=False)
|
| 766 |
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
path_or_fileobj=temp_path,
|
| 770 |
-
path_in_repo="Quan_ly_hoa_chat.xlsx",
|
| 771 |
-
repo_id="doluong6/Quatest1",
|
| 772 |
-
repo_type="dataset",
|
| 773 |
-
token=os.environ.get("HF_TOKEN"),
|
| 774 |
-
)
|
| 775 |
|
| 776 |
-
return
|
| 777 |
|
| 778 |
except Exception as e:
|
| 779 |
-
|
| 780 |
-
status_code=500,
|
| 781 |
-
content={"error": str(e), "trace": traceback.format_exc()}
|
| 782 |
-
)
|
| 783 |
|
| 784 |
|
|
|
|
| 728 |
continue
|
| 729 |
return None
|
| 730 |
|
| 731 |
+
@app.get("/quan_ly_hoa_chat")
|
| 732 |
+
async def quan_ly_hoa_chat():
|
| 733 |
try:
|
| 734 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 735 |
+
file_path = hf_hub_download(
|
| 736 |
+
repo_id="doluong6/Quatest1",
|
| 737 |
+
filename="Quan_ly_hoa_chat.xlsx",
|
| 738 |
+
repo_type="dataset",
|
| 739 |
+
token=HF_TOKEN,
|
| 740 |
+
cache_dir="/tmp"
|
| 741 |
+
)
|
|
|
|
|
|
|
| 742 |
|
| 743 |
+
df = pd.read_excel(file_path, sheet_name="HOA CHAT SU DUNG")
|
| 744 |
+
df = df.fillna("")
|
|
|
|
|
|
|
|
|
|
| 745 |
|
| 746 |
+
# Cột Ngày nhập kho và Ngày hết hạn có thể là datetime
|
| 747 |
+
def xu_ly_tinh_trang(row):
|
| 748 |
+
try:
|
| 749 |
+
ngay_nhap = pd.to_datetime(row['NGÀY NHẬP KHO'])
|
| 750 |
+
het_han = pd.to_datetime(row['NGÀY HẾT HẠN'])
|
| 751 |
+
hom_nay = pd.Timestamp.today()
|
| 752 |
+
con_lai = (het_han - hom_nay).days
|
| 753 |
+
if con_lai < 0:
|
| 754 |
+
return f"HẾT HẠN ({ngay_nhap.date()} - {het_han.date()})"
|
| 755 |
else:
|
| 756 |
+
return f"CÒN: {con_lai} NGÀY ({ngay_nhap.date()} - {het_han.date()})"
|
| 757 |
+
except:
|
| 758 |
+
return row.get("TÌNH TRẠNG", "") # fallback
|
| 759 |
|
| 760 |
+
df["TÌNH TRẠNG"] = df.apply(xu_ly_tinh_trang, axis=1)
|
|
|
|
|
|
|
|
|
|
| 761 |
|
| 762 |
+
columns = df.columns.tolist()
|
| 763 |
+
rows = df.astype(str).values.tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 764 |
|
| 765 |
+
return {"columns": columns, "rows": rows}
|
| 766 |
|
| 767 |
except Exception as e:
|
| 768 |
+
raise HTTPException(status_code=500, detail=f"Lỗi đọc danh sách hóa chất: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 769 |
|
| 770 |
|