Calculate construction costs using DDC CWICR resource-based methodology. Break down costs into labor, materials, equipment with transparent pricing."
python
import pandas as pd
import numpy as np
from typing import Dict, Any, List, Optional, Tuple
from dataclasses import dataclass, field
from enum import Enum
from datetime import datetime
class CostComponent(Enum):
成本分解组成部分。
LABOR = labor
MATERIAL = material
EQUIPMENT = equipment
OVERHEAD = overhead
PROFIT = profit
TOTAL = total
class CostStatus(Enum):
成本计算状态。
CALCULATED = calculated
ESTIMATED = estimated
MISSINGDATA = missingdata
ERROR = error
@dataclass
class CostBreakdown:
工作项的详细成本分解。
workitemcode: str
description: str
unit: str
quantity: float
labor_cost: float = 0.0
material_cost: float = 0.0
equipment_cost: float = 0.0
overhead_cost: float = 0.0
profit_cost: float = 0.0
unit_price: float = 0.0
total_cost: float = 0.0
labor_hours: float = 0.0
labor_rate: float = 0.0
resources: List[Dict[str, Any]] = field(default_factory=list)
status: CostStatus = CostStatus.CALCULATED
def to_dict(self) -> Dict[str, Any]:
return {
workitemcode: self.workitemcode,
description: self.description,
unit: self.unit,
quantity: self.quantity,
laborcost: self.laborcost,
materialcost: self.materialcost,
equipmentcost: self.equipmentcost,
overheadcost: self.overheadcost,
profitcost: self.profitcost,
totalcost: self.totalcost,
status: self.status.value
}
@dataclass
class CostSummary:
成本估算摘要。
total_cost: float
labor_total: float
material_total: float
equipment_total: float
overhead_total: float
profit_total: float
item_count: int
currency: str
calculated_at: datetime
breakdownbycategory: Dict[str, float] = field(default_factory=dict)
class CWICRCostCalculator:
使用CWICR方法的基于资源的成本计算器。
DEFAULTOVERHEADRATE = 0.15 # 15% 管理费
DEFAULTPROFITRATE = 0.10 # 10% 利润
def init(self, cwicr_data: pd.DataFrame,
overhead_rate: float = None,
profit_rate: float = None,
currency: str = USD):
使用CWICR数据初始化计算器。
self.data = cwicr_data
self.overheadrate = overheadrate or self.DEFAULTOVERHEADRATE
self.profitrate = profitrate or self.DEFAULTPROFITRATE
self.currency = currency
# 为快速查找建立索引
self.indexdata()
def indexdata(self):
为快速工作项查找创建索引。
if workitemcode in self.data.columns:
self.codeindex = self.data.setindex(workitem_code)
else:
self.codeindex = None
def calculateitemcost(self, workitemcode: str,
quantity: float,
price_overrides: Dict[str, float] = None) -> CostBreakdown:
计算单个工作项的成本。
# 在数据库中查找工作项
if self.codeindex is not None and workitemcode in self.codeindex.index:
item = self.codeindex.loc[workitemcode]
else:
# 尝试部分匹配
matches = self.data[
self.data[workitemcode].str.contains(workitemcode, case=False, na=False)
]
if matches.empty:
return CostBreakdown(
workitemcode=workitemcode,
description=NOT FOUND,
unit=,
quantity=quantity,
status=CostStatus.MISSING_DATA
)
item = matches.iloc[0]
# 获取基础成本
laborunit = float(item.get(laborcost, 0) or 0)
materialunit = float(item.get(materialcost, 0) or 0)
equipmentunit = float(item.get(equipmentcost, 0) or 0)
# 如果提供了价格覆盖,则应用
if price_overrides:
if laborrate in priceoverrides:
labornorm = float(item.get(labornorm, 0) or 0)
laborunit = labornorm * priceoverrides[laborrate]
if materialfactor in priceoverrides:
materialunit *= priceoverrides[material_factor]
if equipmentfactor in priceoverrides:
equipmentunit *= priceoverrides[equipment_factor]
# 计算组成部分成本
laborcost = laborunit * quantity
materialcost = materialunit * quantity
equipmentcost = equipmentunit * quantity
# 直接成本
directcost = laborcost + materialcost + equipmentcost
# 管理费和利润
overheadcost = directcost * self.overhead_rate
profitcost = (directcost + overheadcost) * self.profitrate
# 总计
totalcost = directcost + overheadcost + profitcost
# 单价
unitprice = totalcost / quantity if quantity > 0 else 0
return CostBreakdown(
workitemcode=workitemcode,
description=str(item.get(description, )),
unit=str(item.get(unit, )),
quantity=quantity,
laborcost=laborcost,
materialcost=materialcost,
equipmentcost=equipmentcost,
overheadcost=overheadcost,
profitcost=profitcost,
unitprice=unitprice,
totalcost=totalcost,
laborhours=float(item.get(labornorm, 0) or 0) * quantity,
laborrate=float(item.get(laborrate, 0) or 0),
status=CostStatus.CALCULATED
)
def calculate_estimate(self, items: List[Dict[str, Any]],
groupbycategory: bool = True) -> CostSummary:
计算多个项目的成本估算。
breakdowns = []
for item in items:
code = item.get(workitemcode) or item.get(code)
qty = item.get(quantity, 0)
overrides = item.get(price_overrides)
breakdown = self.calculateitemcost(code, qty, overrides)
breakdowns.append(breakdown)
# 汇总总计
labortotal = sum(b.laborcost for b in breakdowns)
materialtotal = sum(b.materialcost for b in breakdowns)
equipmenttotal = sum(b.equipmentcost for b in breakdowns)
overheadtotal = sum(b.overheadcost for b in breakdowns)
profittotal = sum(b.profitcost for b in breakdowns)
totalcost = sum(b.totalcost for b in breakdowns)
# 如果请求,按类别分组
breakdownbycategory = {}
if groupbycategory:
for b in breakdowns:
# 从工作项代码前缀提取类别
category = b.workitemcode.split(-)[0] if - in b.workitemcode else Other
if category not in breakdownbycategory:
breakdownbycategory[category] = 0
breakdownbycategory[category] += b.total_cost
return CostSummary(
totalcost=totalcost,
labortotal=labortotal,
materialtotal=materialtotal,
equipmenttotal=equipmenttotal,
overheadtotal=overheadtotal,
profittotal=profittotal,
item_count=len(breakdowns),
currency=self.currency,
calculated_at=datetime.now(),
breakdownbycategory=breakdownbycategory
)
def calculatefromqto(self, qto_df: pd.DataFrame,
codecolumn: str = workitem_code,
quantity_column:
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 cwicr-cost-calculator-1776345205 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 cwicr-cost-calculator-1776345205 技能
skillhub install cwicr-cost-calculator-1776345205
文件大小: 6.25 KB | 发布时间: 2026-4-17 15:42