feat: add project requisites and agreement cancellation
This commit is contained in:
@@ -13,6 +13,7 @@ KS3_NUMBER_UNIQUE_MIGRATION = "20260927_ks3_number_unique"
|
||||
BOQ_DOCUMENT_REQUISITES_MIGRATION = "20260928_boq_document_requisites"
|
||||
DOCUMENT_ARTIFACTS_MIGRATION = "20260928_document_artifacts"
|
||||
AGREEMENT_STATUS_CANCELLED_MIGRATION = "20260930_agreement_status_cancelled"
|
||||
PROJECT_REQUISITES_MIGRATION = "20260930_project_requisites"
|
||||
|
||||
|
||||
def run_migrations(engine):
|
||||
@@ -279,6 +280,34 @@ def run_migrations(engine):
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_agreements_boq_document_id ON agreements (boq_document_id)")
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(AGREEMENT_STATUS_CANCELLED_MIGRATION, datetime.now(timezone.utc).isoformat()))
|
||||
if PROJECT_REQUISITES_MIGRATION not in applied:
|
||||
proj_cols = {row[1] for row in connection.execute("PRAGMA table_info(projects)")}
|
||||
if "investor_name" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN investor_name VARCHAR")
|
||||
if "customer_name" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN customer_name VARCHAR")
|
||||
if "contractor_name" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN contractor_name VARCHAR")
|
||||
if "construction_name" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN construction_name VARCHAR")
|
||||
if "object_name" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN object_name VARCHAR")
|
||||
if "contract_amount" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN contract_amount NUMERIC(18,2)")
|
||||
if "contractor_signer_name" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN contractor_signer_name VARCHAR")
|
||||
if "contractor_signer_position" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN contractor_signer_position VARCHAR")
|
||||
if "customer_signer_name" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN customer_signer_name VARCHAR")
|
||||
if "customer_signer_position" not in proj_cols:
|
||||
connection.execute("ALTER TABLE projects ADD COLUMN customer_signer_position VARCHAR")
|
||||
# Normalize legacy customer → customer_name for new domain field
|
||||
connection.execute(
|
||||
"UPDATE projects SET customer_name = customer WHERE customer_name IS NULL AND customer IS NOT NULL"
|
||||
)
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(PROJECT_REQUISITES_MIGRATION, datetime.now(timezone.utc).isoformat()))
|
||||
violations = connection.execute("PRAGMA foreign_key_check").fetchall()
|
||||
if violations:
|
||||
raise RuntimeError(f"Foreign key violations after agreement migration: {violations[:10]}")
|
||||
|
||||
@@ -28,6 +28,17 @@ class KS2ExportHeader:
|
||||
period_to: date | None
|
||||
comment: str
|
||||
status: str
|
||||
# Official requisites (DOC-B10)
|
||||
investor_name: str = ""
|
||||
customer_name: str = ""
|
||||
contractor_name: str = ""
|
||||
construction_name: str = ""
|
||||
object_name: str = ""
|
||||
contract_amount: str = ""
|
||||
contractor_signer_name: str = ""
|
||||
contractor_signer_position: str = ""
|
||||
customer_signer_name: str = ""
|
||||
customer_signer_position: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -60,7 +71,7 @@ def build_ks2_export_dto(db, ks2_document) -> KS2ExportDocument:
|
||||
|
||||
header = KS2ExportHeader(
|
||||
project_name=project.name if project else "",
|
||||
customer=project.customer if project and project.customer else "",
|
||||
customer=project.effective_customer_name if project else "",
|
||||
contract_number=project.contract_number if project and project.contract_number else "",
|
||||
contract_date=project.contract_date if project else None,
|
||||
currency=project.currency if project and project.currency else "",
|
||||
@@ -70,5 +81,15 @@ def build_ks2_export_dto(db, ks2_document) -> KS2ExportDocument:
|
||||
period_to=ks2_document.reporting_period_to,
|
||||
comment=ks2_document.comment or "",
|
||||
status=ks2_document.status or "",
|
||||
investor_name=project.investor_name if project and project.investor_name else "",
|
||||
customer_name=project.effective_customer_name if project else "",
|
||||
contractor_name=project.contractor_name if project and project.contractor_name else "",
|
||||
construction_name=project.construction_name if project and project.construction_name else "",
|
||||
object_name=project.object_name if project and project.object_name else "",
|
||||
contract_amount=str(project.contract_amount) if project and project.contract_amount is not None else "",
|
||||
contractor_signer_name=project.contractor_signer_name if project and project.contractor_signer_name else "",
|
||||
contractor_signer_position=project.contractor_signer_position if project and project.contractor_signer_position else "",
|
||||
customer_signer_name=project.customer_signer_name if project and project.customer_signer_name else "",
|
||||
customer_signer_position=project.customer_signer_position if project and project.customer_signer_position else "",
|
||||
)
|
||||
return KS2ExportDocument(header=header, items=items, total=total)
|
||||
|
||||
@@ -27,6 +27,17 @@ class KS3ExportHeader:
|
||||
period_to: date | None
|
||||
comment: str
|
||||
status: str
|
||||
# Official requisites (DOC-B10)
|
||||
investor_name: str = ""
|
||||
customer_name: str = ""
|
||||
contractor_name: str = ""
|
||||
construction_name: str = ""
|
||||
object_name: str = ""
|
||||
contract_amount: str = ""
|
||||
contractor_signer_name: str = ""
|
||||
contractor_signer_position: str = ""
|
||||
customer_signer_name: str = ""
|
||||
customer_signer_position: str = ""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -60,7 +71,7 @@ def build_ks3_export_dto(db, ks3_document) -> KS3ExportDocument:
|
||||
|
||||
header = KS3ExportHeader(
|
||||
project_name=project.name if project else "",
|
||||
customer=project.customer if project and project.customer else "",
|
||||
customer=project.effective_customer_name if project else "",
|
||||
contract_number=project.contract_number if project and project.contract_number else "",
|
||||
contract_date=project.contract_date if project else None,
|
||||
currency=project.currency if project and project.currency else "",
|
||||
@@ -70,5 +81,15 @@ def build_ks3_export_dto(db, ks3_document) -> KS3ExportDocument:
|
||||
period_to=ks3_document.reporting_period_to,
|
||||
comment=ks3_document.comment or "",
|
||||
status=ks3_document.status or "",
|
||||
investor_name=project.investor_name if project and project.investor_name else "",
|
||||
customer_name=project.effective_customer_name if project else "",
|
||||
contractor_name=project.contractor_name if project and project.contractor_name else "",
|
||||
construction_name=project.construction_name if project and project.construction_name else "",
|
||||
object_name=project.object_name if project and project.object_name else "",
|
||||
contract_amount=str(project.contract_amount) if project and project.contract_amount is not None else "",
|
||||
contractor_signer_name=project.contractor_signer_name if project and project.contractor_signer_name else "",
|
||||
contractor_signer_position=project.contractor_signer_position if project and project.contractor_signer_position else "",
|
||||
customer_signer_name=project.customer_signer_name if project and project.customer_signer_name else "",
|
||||
customer_signer_position=project.customer_signer_position if project and project.customer_signer_position else "",
|
||||
)
|
||||
return KS3ExportDocument(header=header, items=items, total=total)
|
||||
|
||||
@@ -375,6 +375,16 @@ def serialize_project(graph: dict[str, Any]) -> dict[str, list | dict]:
|
||||
"status": project.status,
|
||||
"created_at": _iso(project.created_at),
|
||||
"updated_at": _iso(project.updated_at),
|
||||
"investor_name": project.investor_name,
|
||||
"customer_name": project.customer_name or project.customer,
|
||||
"contractor_name": project.contractor_name,
|
||||
"construction_name": project.construction_name,
|
||||
"object_name": project.object_name,
|
||||
"contract_amount": _dec(project.contract_amount),
|
||||
"contractor_signer_name": project.contractor_signer_name,
|
||||
"contractor_signer_position": project.contractor_signer_position,
|
||||
"customer_signer_name": project.customer_signer_name,
|
||||
"customer_signer_position": project.customer_signer_position,
|
||||
}
|
||||
|
||||
def record(kind: str, obj: Any, fields: dict[str, Any]) -> dict[str, Any]:
|
||||
|
||||
@@ -489,6 +489,16 @@ def _import_entities(db: Session, payload: dict[str, Any]) -> Project:
|
||||
start_date=_parse_date(src.get("start_date"), "start_date"),
|
||||
end_date=_parse_date(src.get("end_date"), "end_date"),
|
||||
status=src.get("status") or "active",
|
||||
investor_name=src.get("investor_name"),
|
||||
customer_name=src.get("customer_name") or src.get("customer"),
|
||||
contractor_name=src.get("contractor_name"),
|
||||
construction_name=src.get("construction_name"),
|
||||
object_name=src.get("object_name"),
|
||||
contract_amount=_parse_decimal(src.get("contract_amount"), "contract_amount"),
|
||||
contractor_signer_name=src.get("contractor_signer_name"),
|
||||
contractor_signer_position=src.get("contractor_signer_position"),
|
||||
customer_signer_name=src.get("customer_signer_name"),
|
||||
customer_signer_position=src.get("customer_signer_position"),
|
||||
)
|
||||
db.add(project)
|
||||
db.flush()
|
||||
|
||||
+19
-2
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy import Column, Integer, String, Date, DateTime, Text, Enum
|
||||
from sqlalchemy import Column, Integer, String, Date, DateTime, Text, Numeric
|
||||
from sqlalchemy.sql import func
|
||||
from app.database.database import Base
|
||||
from datetime import datetime
|
||||
@@ -12,7 +12,7 @@ class Project(Base):
|
||||
name = Column(String, index=True, nullable=False)
|
||||
contract_number = Column(String)
|
||||
contract_date = Column(Date)
|
||||
customer = Column(String)
|
||||
customer = Column(String) # domain: customer_name
|
||||
currency = Column(String, default="RUB")
|
||||
start_date = Column(Date)
|
||||
end_date = Column(Date)
|
||||
@@ -20,10 +20,27 @@ class Project(Base):
|
||||
created_at = Column(DateTime, default=func.now())
|
||||
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
||||
|
||||
# Official document requisites (DOC-B10)
|
||||
investor_name = Column(String)
|
||||
customer_name = Column(String) # preferred domain name for customer
|
||||
contractor_name = Column(String)
|
||||
construction_name = Column(String)
|
||||
object_name = Column(String)
|
||||
contract_amount = Column(Numeric(18, 2))
|
||||
contractor_signer_name = Column(String)
|
||||
contractor_signer_position = Column(String)
|
||||
customer_signer_name = Column(String)
|
||||
customer_signer_position = Column(String)
|
||||
|
||||
# Relationships
|
||||
boq_documents = relationship("BoqDocument", back_populates="project")
|
||||
systems = relationship("System", back_populates="project")
|
||||
agreements = relationship("Agreement", back_populates="project", cascade="all, delete-orphan")
|
||||
|
||||
@property
|
||||
def effective_customer_name(self) -> str:
|
||||
"""Return customer_name if set, else fall back to legacy customer field."""
|
||||
return self.customer_name or self.customer or ""
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Project(id={self.id}, name='{self.name}')>"
|
||||
|
||||
+30
-1
@@ -348,12 +348,23 @@ async def update_project(
|
||||
start_date: str = Form(None),
|
||||
end_date: str = Form(None),
|
||||
status: str = Form("active"),
|
||||
investor_name: str = Form(None),
|
||||
customer_name: str = Form(None),
|
||||
contractor_name: str = Form(None),
|
||||
construction_name: str = Form(None),
|
||||
object_name: str = Form(None),
|
||||
contract_amount: str = Form(None),
|
||||
contractor_signer_name: str = Form(None),
|
||||
contractor_signer_position: str = Form(None),
|
||||
customer_signer_name: str = Form(None),
|
||||
customer_signer_position: str = Form(None),
|
||||
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")
|
||||
from datetime import datetime
|
||||
from decimal import Decimal, InvalidOperation
|
||||
try:
|
||||
db_project.contract_date = datetime.strptime(contract_date, "%Y-%m-%d").date() if contract_date else None
|
||||
db_project.start_date = datetime.strptime(start_date, "%Y-%m-%d").date() if start_date else None
|
||||
@@ -362,9 +373,27 @@ async def update_project(
|
||||
raise HTTPException(status_code=400, detail="Dates must use YYYY-MM-DD format") from exc
|
||||
db_project.name = name.strip()
|
||||
db_project.contract_number = contract_number.strip() if contract_number else None
|
||||
db_project.customer = customer.strip() if customer else None
|
||||
# customer_name is the domain field; customer is kept in sync for compatibility
|
||||
resolved_customer = (customer_name or customer or "").strip() or None
|
||||
db_project.customer_name = resolved_customer
|
||||
db_project.customer = resolved_customer
|
||||
db_project.currency = currency.strip() if currency else "RUB"
|
||||
db_project.status = status
|
||||
db_project.investor_name = investor_name.strip() if investor_name else None
|
||||
db_project.contractor_name = contractor_name.strip() if contractor_name else None
|
||||
db_project.construction_name = construction_name.strip() if construction_name else None
|
||||
db_project.object_name = object_name.strip() if object_name else None
|
||||
db_project.contractor_signer_name = contractor_signer_name.strip() if contractor_signer_name else None
|
||||
db_project.contractor_signer_position = contractor_signer_position.strip() if contractor_signer_position else None
|
||||
db_project.customer_signer_name = customer_signer_name.strip() if customer_signer_name else None
|
||||
db_project.customer_signer_position = customer_signer_position.strip() if customer_signer_position else None
|
||||
if contract_amount:
|
||||
try:
|
||||
db_project.contract_amount = Decimal(contract_amount.strip().replace(",", "."))
|
||||
except InvalidOperation:
|
||||
raise HTTPException(status_code=400, detail="Сумма договора должна быть числом")
|
||||
else:
|
||||
db_project.contract_amount = None
|
||||
db.commit()
|
||||
return RedirectResponse(url=f"/projects/{project_id}", status_code=303)
|
||||
|
||||
|
||||
@@ -61,12 +61,30 @@
|
||||
<div class="row g-3 g-md-4 align-items-start mb-2">
|
||||
<div class="col-6 col-sm-4 col-md-auto">
|
||||
<div class="text-muted small mb-1">Договор</div>
|
||||
<div class="fw-semibold text-dark">{{ project.contract_number or 'не указан' }}</div>
|
||||
<div class="fw-semibold text-dark">{{ project.contract_number or '—' }}{% if project.contract_date %} от {{ project.contract_date.strftime('%d.%m.%Y') }}{% endif %}</div>
|
||||
</div>
|
||||
{% if project.contract_amount is not none %}
|
||||
<div class="col-6 col-sm-4 col-md-auto">
|
||||
<div class="text-muted small mb-1">Сумма</div>
|
||||
<div class="fw-semibold text-dark money-value">{{ project.contract_amount|money(project.currency) }}</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="col-6 col-sm-4 col-md-auto">
|
||||
<div class="text-muted small mb-1">Заказчик</div>
|
||||
<div class="fw-semibold text-dark">{{ project.customer or 'не указан' }}</div>
|
||||
<div class="fw-semibold text-dark">{{ project.effective_customer_name or '—' }}</div>
|
||||
</div>
|
||||
{% if project.contractor_name %}
|
||||
<div class="col-6 col-sm-4 col-md-auto">
|
||||
<div class="text-muted small mb-1">Подрядчик</div>
|
||||
<div class="fw-semibold text-dark">{{ project.contractor_name }}</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if project.object_name %}
|
||||
<div class="col-6 col-sm-4 col-md-auto">
|
||||
<div class="text-muted small mb-1">Объект</div>
|
||||
<div class="fw-semibold text-dark">{{ project.object_name }}</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="col-6 col-sm-4 col-md-auto">
|
||||
<div class="text-muted small mb-1">Валюта</div>
|
||||
<div class="fw-semibold text-dark">{{ project.currency or 'RUB' }}</div>
|
||||
|
||||
@@ -11,12 +11,9 @@
|
||||
<h1 class="h2 mb-4">Редактировать проект</h1>
|
||||
<form method="post" action="/projects/{{ project.id }}/edit" class="card border-0 shadow-sm">
|
||||
<div class="card-body">
|
||||
<div class="row g-3">
|
||||
<h2 class="h5 mb-3">Проект</h2>
|
||||
<div class="row g-3 mb-4">
|
||||
<div class="col-md-6"><label class="form-label" for="name">Название проекта *</label><input class="form-control" id="name" name="name" value="{{ project.name }}" required></div>
|
||||
<div class="col-md-6"><label class="form-label" for="contract_number">Номер договора</label><input class="form-control" id="contract_number" name="contract_number" value="{{ project.contract_number or '' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="contract_date">Дата договора</label><input class="form-control" type="date" id="contract_date" name="contract_date" value="{{ project.contract_date.isoformat() if project.contract_date else '' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="customer">Заказчик</label><input class="form-control" id="customer" name="customer" value="{{ project.customer or '' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="currency">Валюта</label><input class="form-control" id="currency" name="currency" value="{{ project.currency or 'RUB' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="status">Статус</label><select class="form-select" id="status" name="status">
|
||||
{% for value, label in [('active','Активный'),('completed','Завершён'),('on_hold','Приостановлен')] %}<option value="{{ value }}" {% if project.status == value %}selected{% endif %}>{{ label }}</option>{% endfor %}
|
||||
{% if project.status and project.status not in ['active','completed','on_hold'] %}<option value="{{ project.status }}" selected>{{ project.status }}</option>{% endif %}
|
||||
@@ -24,6 +21,49 @@
|
||||
<div class="col-md-6"><label class="form-label" for="start_date">Дата начала</label><input class="form-control" type="date" id="start_date" name="start_date" value="{{ project.start_date.isoformat() if project.start_date else '' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="end_date">Дата окончания</label><input class="form-control" type="date" id="end_date" name="end_date" value="{{ project.end_date.isoformat() if project.end_date else '' }}"></div>
|
||||
</div>
|
||||
|
||||
<h2 class="h5 mb-3">Договор</h2>
|
||||
<div class="row g-3 mb-4">
|
||||
<div class="col-md-6"><label class="form-label" for="contract_number">Номер</label><input class="form-control" id="contract_number" name="contract_number" value="{{ project.contract_number or '' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="contract_date">Дата</label><input class="form-control" type="date" id="contract_date" name="contract_date" value="{{ project.contract_date.isoformat() if project.contract_date else '' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="contract_amount">Сумма</label><input class="form-control" id="contract_amount" name="contract_amount" type="number" step="0.01" value="{{ project.contract_amount if project.contract_amount is not none else '' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="currency">Валюта</label><input class="form-control" id="currency" name="currency" value="{{ project.currency or 'RUB' }}"></div>
|
||||
</div>
|
||||
|
||||
<h2 class="h5 mb-3">Участники</h2>
|
||||
<div class="row g-3 mb-4">
|
||||
<div class="col-md-4"><label class="form-label" for="investor_name">Инвестор</label><input class="form-control" id="investor_name" name="investor_name" value="{{ project.investor_name or '' }}"></div>
|
||||
<div class="col-md-4"><label class="form-label" for="customer_name">Заказчик</label><input class="form-control" id="customer_name" name="customer_name" value="{{ project.effective_customer_name }}"></div>
|
||||
<div class="col-md-4"><label class="form-label" for="contractor_name">Подрядчик</label><input class="form-control" id="contractor_name" name="contractor_name" value="{{ project.contractor_name or '' }}"></div>
|
||||
</div>
|
||||
|
||||
<h2 class="h5 mb-3">Объект</h2>
|
||||
<div class="row g-3 mb-4">
|
||||
<div class="col-md-6"><label class="form-label" for="construction_name">Наименование строительства</label><input class="form-control" id="construction_name" name="construction_name" value="{{ project.construction_name or '' }}"></div>
|
||||
<div class="col-md-6"><label class="form-label" for="object_name">Наименование объекта</label><input class="form-control" id="object_name" name="object_name" value="{{ project.object_name or '' }}"></div>
|
||||
</div>
|
||||
|
||||
<h2 class="h5 mb-3">Подписанты</h2>
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6">
|
||||
<div class="card bg-light border-0"><div class="card-body">
|
||||
<h3 class="h6 mb-3">Заказчик</h3>
|
||||
<div class="row g-3">
|
||||
<div class="col-12"><label class="form-label" for="customer_signer_name">ФИО</label><input class="form-control" id="customer_signer_name" name="customer_signer_name" value="{{ project.customer_signer_name or '' }}"></div>
|
||||
<div class="col-12"><label class="form-label" for="customer_signer_position">Должность</label><input class="form-control" id="customer_signer_position" name="customer_signer_position" value="{{ project.customer_signer_position or '' }}"></div>
|
||||
</div>
|
||||
</div></div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="card bg-light border-0"><div class="card-body">
|
||||
<h3 class="h6 mb-3">Подрядчик</h3>
|
||||
<div class="row g-3">
|
||||
<div class="col-12"><label class="form-label" for="contractor_signer_name">ФИО</label><input class="form-control" id="contractor_signer_name" name="contractor_signer_name" value="{{ project.contractor_signer_name or '' }}"></div>
|
||||
<div class="col-12"><label class="form-label" for="contractor_signer_position">Должность</label><input class="form-control" id="contractor_signer_position" name="contractor_signer_position" value="{{ project.contractor_signer_position or '' }}"></div>
|
||||
</div>
|
||||
</div></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-footer bg-white d-flex justify-content-end gap-2"><a href="/projects/{{ project.id }}" class="btn btn-outline-secondary">Отмена</a><button class="btn btn-primary" type="submit">Сохранить</button></div>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user