chore: baseline before agreements

This commit is contained in:
Александр Арсентьев
2026-09-25 17:53:33 +03:00
commit 2376778828
37 changed files with 2870 additions and 0 deletions
+176
View File
@@ -0,0 +1,176 @@
from fastapi import APIRouter, Depends, HTTPException, Request, Form, UploadFile, File
from sqlalchemy.orm import Session
from app.database import database
from app.models import project, boq
from app.importer import excel_importer
from typing import List
import os
from decimal import Decimal
from pathlib import Path
router = APIRouter()
UPLOAD_DIR = Path(__file__).resolve().parents[1] / "uploads"
# Dependency
def get_db():
db = database.SessionLocal()
try:
yield db
finally:
db.close()
@router.get("/{project_id}")
async def list_project_boq(request: Request, project_id: int, db: Session = Depends(get_db)):
db_project = db.query(project.Project).filter(project.Project.id == project_id).first()
if db_project is None:
raise HTTPException(status_code=404, detail="Project not found")
documents = db.query(boq.BoqDocument).filter(
boq.BoqDocument.project_id == project_id
).order_by(boq.BoqDocument.created_at.desc(), boq.BoqDocument.id.desc()).all()
return request.app.templates.TemplateResponse("projects/boq_list.html", {
"request": request, "project": db_project, "documents": documents,
})
@router.get("/{project_id}/documents/{document_id}")
async def view_boq_document(request: Request, project_id: int, document_id: int,
db: Session = Depends(get_db)):
db_project = db.query(project.Project).filter(project.Project.id == project_id).first()
if db_project is None:
raise HTTPException(status_code=404, detail="Project not found")
document = db.query(boq.BoqDocument).filter(
boq.BoqDocument.id == document_id,
boq.BoqDocument.project_id == project_id,
).first()
if document is None:
raise HTTPException(status_code=404, detail="BOQ document not found")
document_title = document.name.replace("BOQ Import - ", "Импорт ведомости — ").replace(
"BOQ Import — ", "Импорт ведомости — "
)
items = db.query(boq.WorkItem).filter(
boq.WorkItem.boq_document_id == document_id,
).order_by(boq.WorkItem.id).all()
system_ids = {item.system_id for item in items if item.system_id is not None}
systems = db.query(boq.System).filter(
boq.System.project_id == project_id,
boq.System.id.in_(system_ids) if system_ids else False,
).order_by(boq.System.sort_order, boq.System.id).all()
tree = []
total = Decimal("0.00")
for system in systems:
system_sections = db.query(boq.Section).filter(
boq.Section.system_id == system.id,
).order_by(boq.Section.sort_order, boq.Section.id).all()
section_nodes = []
system_total = Decimal("0.00")
for section in system_sections:
section_items = [item for item in items if item.section_id == section.id]
section_total = sum((item.amount or Decimal("0.00") for item in section_items), Decimal("0.00"))
section_nodes.append({"section": section, "items": section_items, "total": section_total})
system_total += section_total
# Keep system rows represented even if currently empty.
tree.append({"system": system, "sections": section_nodes, "total": system_total})
total += system_total
return request.app.templates.TemplateResponse("projects/boq_document.html", {
"request": request, "project": db_project, "document": document,
"document_title": document_title,
"systems": tree, "total": total,
"unassigned_items": [item for item in items if item.system_id is None or item.section_id is None],
})
@router.post("/{project_id}/import/upload")
async def upload_excel_file(
request: Request,
project_id: int,
file: UploadFile = File(...),
db: Session = Depends(get_db)
):
db_project = db.query(project.Project).filter(project.Project.id == project_id).first()
if db_project is None:
raise HTTPException(status_code=404, detail="Project not found")
# Save the uploaded file
filename = Path(file.filename or "upload.xlsx").name
UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
file_location = UPLOAD_DIR / filename
with open(file_location, "wb+") as file_object:
file_object.write(file.file.read())
# Get the sheets from the Excel file
sheets = excel_importer.get_excel_sheets(file_location)
return request.app.templates.TemplateResponse("projects/import_sheet_selection.html", {
"request": request,
"project": db_project,
"project_id": project_id,
"filename": filename,
"sheets": sheets
})
@router.post("/{project_id}/import/preview")
async def preview_import(
request: Request,
project_id: int,
filename: str = Form(...),
sheet_name: str = Form(...),
header_row: int = Form(...),
start_row: int = Form(...),
db: Session = Depends(get_db)
):
# Get the file path
file_path = UPLOAD_DIR / Path(filename).name
# Import and preview data
try:
preview_data = excel_importer.preview_excel_data(file_path, sheet_name, header_row, start_row)
except (ValueError, OSError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
# Get project
db_project = db.query(project.Project).filter(project.Project.id == project_id).first()
if db_project is None:
raise HTTPException(status_code=404, detail="Project not found")
return request.app.templates.TemplateResponse("projects/import_preview.html", {
"request": request,
"project": db_project,
"preview_data": preview_data,
"filename": filename,
"sheet_name": sheet_name,
"header_row": header_row,
"start_row": start_row
})
@router.post("/{project_id}/import/confirm")
async def confirm_import(
request: Request,
project_id: int,
filename: str = Form(...),
sheet_name: str = Form(...),
header_row: int = Form(...),
start_row: int = Form(...),
db: Session = Depends(get_db)
):
# Get the file path
file_path = UPLOAD_DIR / Path(filename).name
# Import data into database
db_project = db.query(project.Project).filter(project.Project.id == project_id).first()
if db_project is None:
raise HTTPException(status_code=404, detail="Project not found")
try:
result = excel_importer.import_excel_data(db, file_path, sheet_name, header_row, start_row, project_id)
except (ValueError, OSError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
# Get project
return request.app.templates.TemplateResponse("projects/import_confirm.html", {
"request": request,
"project": db_project,
"import_result": result,
"filename": filename,
"sheet_name": sheet_name
})