feat: add agreement workflow and project-scoped work items
This commit is contained in:
@@ -46,20 +46,24 @@ def _validate_agreement_scope(db: Session, agreement: Agreement):
|
||||
return doc
|
||||
|
||||
|
||||
def _item_scope_document_id(db: Session, item: boq.WorkItem):
|
||||
source_doc = db.query(boq.BoqDocument).filter(boq.BoqDocument.id == item.boq_document_id).first()
|
||||
if source_doc is None or source_doc.project_id != item.project_id:
|
||||
raise BaseBoqConfigurationError(f"WorkItem {item.id} has no valid source BOQ")
|
||||
if source_doc.agreement_id is None:
|
||||
return source_doc.id
|
||||
def work_item_scope_document_id(db: Session, item: boq.WorkItem):
|
||||
origin = db.query(AgreementItem, Agreement).join(
|
||||
Agreement, Agreement.id == AgreementItem.agreement_id
|
||||
).filter(
|
||||
AgreementItem.work_item_id == item.id,
|
||||
AgreementItem.change_type == "new_position",
|
||||
Agreement.status == "approved",
|
||||
).first()
|
||||
return origin[1].boq_document_id if origin else None
|
||||
).order_by(AgreementItem.id).first()
|
||||
if item.boq_document_id is None:
|
||||
return origin[0].source_boq_document_id if origin else None
|
||||
source_doc = db.query(boq.BoqDocument).filter(boq.BoqDocument.id == item.boq_document_id).first()
|
||||
if source_doc is None or source_doc.project_id != item.project_id:
|
||||
raise BaseBoqConfigurationError(f"WorkItem {item.id} has an invalid source BOQ")
|
||||
if source_doc.agreement_id is None:
|
||||
return source_doc.id
|
||||
if origin:
|
||||
return origin[0].source_boq_document_id or origin[1].boq_document_id
|
||||
return None
|
||||
|
||||
|
||||
def build_effective_plan(db: Session, project_id: int):
|
||||
@@ -95,7 +99,7 @@ def build_effective_plan(db: Session, project_id: int):
|
||||
if item is None:
|
||||
raise BaseBoqConfigurationError(f"Agreement item {line.id} references a WorkItem outside its project")
|
||||
if scope_doc and line.change_type != "new_position":
|
||||
if _item_scope_document_id(db, item) != scope_doc.id:
|
||||
if work_item_scope_document_id(db, item) != scope_doc.id:
|
||||
raise BaseBoqConfigurationError(
|
||||
f"Agreement item {line.id} targets a WorkItem outside BOQ scope {scope_doc.id}"
|
||||
)
|
||||
@@ -103,10 +107,30 @@ def build_effective_plan(db: Session, project_id: int):
|
||||
deltas[line.work_item_id][0] += _decimal(line.quantity_delta)
|
||||
deltas[line.work_item_id][1] += _decimal(line.amount_delta)
|
||||
if line.change_type == "new_position":
|
||||
if item.boq_document.agreement_id != agreement.id:
|
||||
raise BaseBoqConfigurationError(
|
||||
f"New agreement item {line.id} is not backed by its agreement source document"
|
||||
)
|
||||
if item.boq_document_id is not None:
|
||||
source_doc = db.query(boq.BoqDocument).filter(
|
||||
boq.BoqDocument.id == item.boq_document_id,
|
||||
boq.BoqDocument.project_id == project_id,
|
||||
boq.BoqDocument.agreement_id == agreement.id,
|
||||
).first()
|
||||
if source_doc is None:
|
||||
raise BaseBoqConfigurationError(
|
||||
f"New agreement item {line.id} references an invalid source document"
|
||||
)
|
||||
if line.source_boq_document_id is not None:
|
||||
source_boq = db.query(boq.BoqDocument).filter(
|
||||
boq.BoqDocument.id == line.source_boq_document_id,
|
||||
boq.BoqDocument.project_id == project_id,
|
||||
boq.BoqDocument.agreement_id.is_(None),
|
||||
).first()
|
||||
if source_boq is None:
|
||||
raise BaseBoqConfigurationError(
|
||||
f"New agreement item {line.id} references a BOQ outside its project"
|
||||
)
|
||||
if scope_doc and source_boq.id != scope_doc.id:
|
||||
raise BaseBoqConfigurationError(
|
||||
f"New agreement item {line.id} references a BOQ outside agreement scope"
|
||||
)
|
||||
new_item_ids.add(line.work_item_id)
|
||||
|
||||
present = {item.id for item in items}
|
||||
@@ -192,13 +216,24 @@ def approve_agreement(db: Session, agreement_id: int):
|
||||
if item is None:
|
||||
raise ValueError(f"Agreement item {line.id} does not reference a project WorkItem")
|
||||
if line.change_type == "new_position":
|
||||
source_doc = db.query(boq.BoqDocument).filter(
|
||||
boq.BoqDocument.id == item.boq_document_id,
|
||||
boq.BoqDocument.agreement_id == agreement.id,
|
||||
boq.BoqDocument.project_id == agreement.project_id,
|
||||
).first()
|
||||
if source_doc is None:
|
||||
raise ValueError(f"New position line {line.id} must reference an item from its agreement document")
|
||||
if item.boq_document_id is not None:
|
||||
source_doc = db.query(boq.BoqDocument).filter(
|
||||
boq.BoqDocument.id == item.boq_document_id,
|
||||
boq.BoqDocument.agreement_id == agreement.id,
|
||||
boq.BoqDocument.project_id == agreement.project_id,
|
||||
).first()
|
||||
if source_doc is None:
|
||||
raise ValueError(f"New position line {line.id} references an invalid source document")
|
||||
if line.source_boq_document_id is not None:
|
||||
source_boq = db.query(boq.BoqDocument).filter(
|
||||
boq.BoqDocument.id == line.source_boq_document_id,
|
||||
boq.BoqDocument.project_id == agreement.project_id,
|
||||
boq.BoqDocument.agreement_id.is_(None),
|
||||
).first()
|
||||
if source_boq is None:
|
||||
raise ValueError(f"New position line {line.id} references a BOQ outside its project")
|
||||
if agreement.boq_document_id is not None and line.source_boq_document_id not in (None, agreement.boq_document_id):
|
||||
raise ValueError(f"New position line {line.id} references a BOQ outside agreement scope")
|
||||
if item.quantity not in (None, ZERO) or item.amount not in (None, ZERO):
|
||||
raise ValueError(f"New position WorkItem {item.id} must start with zero base quantity and amount")
|
||||
all_project_items[item.id] = item
|
||||
@@ -206,7 +241,7 @@ def approve_agreement(db: Session, agreement_id: int):
|
||||
canonical = all_project_items.get(item.id)
|
||||
if canonical is None:
|
||||
raise ValueError(f"Agreement item {line.id} does not reference an existing canonical project item")
|
||||
if scope_doc and _item_scope_document_id(db, item) != scope_doc.id:
|
||||
if scope_doc and work_item_scope_document_id(db, item) != scope_doc.id:
|
||||
raise ValueError(f"Agreement item {line.id} targets a WorkItem outside its BOQ scope")
|
||||
|
||||
totals.setdefault(item.id, [ZERO, ZERO])
|
||||
|
||||
Reference in New Issue
Block a user