Update app.py
Browse files
app.py
CHANGED
|
@@ -663,7 +663,7 @@ def chuan_hoa_ngay(value):
|
|
| 663 |
continue
|
| 664 |
return None
|
| 665 |
|
| 666 |
-
@
|
| 667 |
async def cap_nhat_hoa_chat(request: Request):
|
| 668 |
try:
|
| 669 |
du_lieu = await request.json()
|
|
@@ -678,6 +678,7 @@ async def cap_nhat_hoa_chat(request: Request):
|
|
| 678 |
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.")
|
| 679 |
|
| 680 |
today = datetime.today()
|
|
|
|
| 681 |
for i, row in df.iterrows():
|
| 682 |
ngay_str = row[idx_ngay_het_han]
|
| 683 |
dt = chuan_hoa_ngay(ngay_str)
|
|
@@ -692,8 +693,18 @@ async def cap_nhat_hoa_chat(request: Request):
|
|
| 692 |
else:
|
| 693 |
status = f"CÒN: {diff} NGÀY"
|
| 694 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
df.iat[i, idx_tinh_trang] = status
|
| 696 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 697 |
# Ghi file Excel tạm
|
| 698 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 699 |
temp_path = tmp.name
|
|
@@ -715,19 +726,6 @@ async def cap_nhat_hoa_chat(request: Request):
|
|
| 715 |
status_code=500,
|
| 716 |
content={"error": str(e), "trace": traceback.format_exc()}
|
| 717 |
)
|
| 718 |
-
def chuan_hoa_ngay(value):
|
| 719 |
-
if pd.isna(value):
|
| 720 |
-
return None
|
| 721 |
-
if isinstance(value, datetime):
|
| 722 |
-
return value
|
| 723 |
-
if isinstance(value, str):
|
| 724 |
-
for fmt in ("%d/%m/%Y", "%d-%m-%Y", "%Y-%m-%d", "%Y/%m/%d"):
|
| 725 |
-
try:
|
| 726 |
-
return datetime.strptime(value.strip(), fmt)
|
| 727 |
-
except:
|
| 728 |
-
continue
|
| 729 |
-
return None
|
| 730 |
-
|
| 731 |
@app.get("/quan_ly_hoa_chat")
|
| 732 |
async def quan_ly_hoa_chat():
|
| 733 |
try:
|
|
|
|
| 663 |
continue
|
| 664 |
return None
|
| 665 |
|
| 666 |
+
@router.post("/cap_nhat_hoa_chat")
|
| 667 |
async def cap_nhat_hoa_chat(request: Request):
|
| 668 |
try:
|
| 669 |
du_lieu = await request.json()
|
|
|
|
| 678 |
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.")
|
| 679 |
|
| 680 |
today = datetime.today()
|
| 681 |
+
|
| 682 |
for i, row in df.iterrows():
|
| 683 |
ngay_str = row[idx_ngay_het_han]
|
| 684 |
dt = chuan_hoa_ngay(ngay_str)
|
|
|
|
| 693 |
else:
|
| 694 |
status = f"CÒN: {diff} NGÀY"
|
| 695 |
|
| 696 |
+
# Cập nhật lại ngày hết hạn theo định dạng chuẩn
|
| 697 |
+
df.iat[i, idx_ngay_het_han] = dt.strftime('%d/%m/%Y')
|
| 698 |
+
else:
|
| 699 |
+
df.iat[i, idx_ngay_het_han] = ""
|
| 700 |
+
|
| 701 |
df.iat[i, idx_tinh_trang] = status
|
| 702 |
|
| 703 |
+
# Chuẩn hóa các cột datetime còn lại nếu có
|
| 704 |
+
for col in df.columns:
|
| 705 |
+
if df[col].dtype == 'datetime64[ns]':
|
| 706 |
+
df[col] = df[col].dt.strftime('%d/%m/%Y')
|
| 707 |
+
|
| 708 |
# Ghi file Excel tạm
|
| 709 |
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
| 710 |
temp_path = tmp.name
|
|
|
|
| 726 |
status_code=500,
|
| 727 |
content={"error": str(e), "trace": traceback.format_exc()}
|
| 728 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
@app.get("/quan_ly_hoa_chat")
|
| 730 |
async def quan_ly_hoa_chat():
|
| 731 |
try:
|