Create tao_baogia.py
Browse files- routes/tao_baogia.py +75 -0
routes/tao_baogia.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Form
|
| 2 |
+
from huggingface_hub import hf_hub_download, upload_file
|
| 3 |
+
from openpyxl import load_workbook
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import os
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from bao_gia import dien_file_baogia, tao_so_bao_gia
|
| 8 |
+
|
| 9 |
+
router = APIRouter()
|
| 10 |
+
|
| 11 |
+
REPO_ID = "doluong6/Quatest1"
|
| 12 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 13 |
+
|
| 14 |
+
@router.post("/tao_baogia_tu_file")
|
| 15 |
+
async def tao_baogia_tu_file(ten_file: str = Form(...)):
|
| 16 |
+
try:
|
| 17 |
+
# ✅ Tải file PYC từ Hugging Face Dataset
|
| 18 |
+
path = hf_hub_download(
|
| 19 |
+
repo_id=REPO_ID,
|
| 20 |
+
repo_type="dataset",
|
| 21 |
+
token=HF_TOKEN,
|
| 22 |
+
filename=f"Phieu_yeu_cau/{ten_file}",
|
| 23 |
+
cache_dir="/tmp"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# ✅ Đọc nội dung Excel
|
| 27 |
+
wb = load_workbook(path, data_only=True)
|
| 28 |
+
ws = wb.active
|
| 29 |
+
|
| 30 |
+
ten_khach = str(ws["I8"].value or "Khach_hang_khong_ten").strip()
|
| 31 |
+
df_data = []
|
| 32 |
+
row = 24
|
| 33 |
+
while True:
|
| 34 |
+
ten_tb = ws[f"D{row}"].value
|
| 35 |
+
so_luong = ws[f"BM{row}"].value
|
| 36 |
+
don_gia = ws[f"BP{row}"].value
|
| 37 |
+
if not ten_tb:
|
| 38 |
+
break
|
| 39 |
+
df_data.append({
|
| 40 |
+
"Ten_thiet_bi": str(ten_tb).strip(),
|
| 41 |
+
"Số lượng": float(so_luong or 0),
|
| 42 |
+
"Don_gia": float(don_gia or 0)
|
| 43 |
+
})
|
| 44 |
+
row += 1
|
| 45 |
+
|
| 46 |
+
if not df_data:
|
| 47 |
+
raise HTTPException(status_code=400, detail="❌ Không có thiết bị nào trong PYC.")
|
| 48 |
+
|
| 49 |
+
df = pd.DataFrame(df_data)
|
| 50 |
+
|
| 51 |
+
# ✅ Tạo số báo giá và điền file
|
| 52 |
+
sobg = tao_so_bao_gia()
|
| 53 |
+
file_bg = dien_file_baogia("Baogia.xlsx", ten_khach, df, sobg)
|
| 54 |
+
|
| 55 |
+
# ✅ Ghi file và upload lên HF
|
| 56 |
+
bg_filename = sobg.replace("/", "_") + ".xlsx"
|
| 57 |
+
tmp_path = f"/tmp/{bg_filename}"
|
| 58 |
+
with open(tmp_path, "wb") as f:
|
| 59 |
+
f.write(file_bg.getbuffer())
|
| 60 |
+
|
| 61 |
+
# ✅ Upload lên thư mục tương ứng
|
| 62 |
+
path_repo = f"Khach_hang/{ten_khach.replace(' ', '_')}/Bao_gia/{bg_filename}"
|
| 63 |
+
upload_file(
|
| 64 |
+
path_or_fileobj=tmp_path,
|
| 65 |
+
path_in_repo=path_repo,
|
| 66 |
+
repo_id=REPO_ID,
|
| 67 |
+
repo_type="dataset",
|
| 68 |
+
token=HF_TOKEN,
|
| 69 |
+
commit_message=f"Tạo báo giá {bg_filename}"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
return {"message": f"✅ Đã tạo báo giá {bg_filename} cho file `{ten_file}`"}
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
raise HTTPException(status_code=500, detail=str(e))
|