Component-Level Cost Roll Design
Overview
This design outlines a modular system for performing component-level cost rolls in a manufacturing ERP context. It handles BOM (Bill of Materials) and routing changes, overhead allocation, and full audit trails. The system recalculates costs bottom-up from raw materials/components to finished goods, propagating changes dynamically.
Key Features:
- Granularity: Costs tracked per component/labor step.
- Change Handling: Versioned BOM/routings with delta detection.
- Overhead: Activity-based or rate-based allocation.
- Audit: Immutable logs with before/after snapshots.
- Tech Assumptions: Relational DB (e.g., PostgreSQL), event-driven architecture (e.g., Kafka for change events), microservices (e.g., Node.js/Python).
System Architecture
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ UI/API │ │ Cost Roll Engine │ │ Audit Service │
│ (Change Input) │───▶│ (Core Logic) │───▶│ (Logging) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│
┌─────────────┼─────────────┐
│ │ │
┌──────▼──────┐ ┌───▼──────┐ ┌───▼──────┐
│ BOM/Routing │ │Overhead │ │ Cost Roll│
│ Updater │ │Allocator │ │ Up │
└──────┬──────┘ └───┬──────┘ └────┬──────┘
│ │ │
└─────────────┼─────────────┘
│
┌──────▼──────┐
│ Database │
│ (BOM, Costs,│
│ Audit Log) │
└──────────────┘
1. Data Models (Schema)
Core tables (simplified SQL DDL):
-- BOM: Hierarchical components
CREATE TABLE bom (
id SERIAL PRIMARY KEY,
product_id VARCHAR(50), -- FG or semi-finished
component_id VARCHAR(50),
qty_per DECIMAL(10,4),
valid_from DATE,
valid_to DATE,
version INT,
change_reason TEXT
);
-- Routing: Labor/operations
CREATE TABLE routing (
id SERIAL PRIMARY KEY,
product_id VARCHAR(50),
operation VARCHAR(100), -- e.g., 'MILL', 'ASSEMBLE'
labor_hrs DECIMAL(8,2),
machine_hrs DECIMAL(8,2),
cost_center VARCHAR(50),
valid_from DATE,
valid_to DATE,
version INT
);
-- Material Costs (component-level)
CREATE TABLE material_costs (
id SERIAL PRIMARY KEY,
component_id VARCHAR(50) UNIQUE,
std_cost DECIMAL(12,4), -- Standard cost/unit
last_updated TIMESTAMP,
source VARCHAR(50) -- Vendor/PO
);
-- Overhead Rates (by cost center/activity)
CREATE TABLE overhead_rates (
id SERIAL PRIMARY KEY,
cost_center VARCHAR(50),
activity_type VARCHAR(50), -- e.g., 'LABOR', 'MACHINE'
rate DECIMAL(10,4), -- $/hr
valid_from DATE,
valid_to DATE
);
-- Rolled Costs (post-rollup output)
CREATE TABLE rolled_costs (
id SERIAL PRIMARY KEY,
product_id VARCHAR(50),
component_id VARCHAR(50) NULL, -- NULL for FG total
mat_cost DECIMAL(12,4),
labor_cost DECIMAL(12,4),
overhead_cost DECIMAL(12,4),
total_cost DECIMAL(12,4),
roll_date TIMESTAMP,
version INT
);
-- Audit Log (immutable)
CREATE TABLE audit_log (
id SERIAL PRIMARY KEY,
entity_type VARCHAR(20), -- 'BOM', 'ROUTING', 'COST_ROLL'
entity_id INT,
old_snapshot JSONB, -- Before state
new_snapshot JSONB, -- After state
user_id VARCHAR(50),
timestamp TIMESTAMP DEFAULT NOW(),
change_type VARCHAR(50) -- INSERT/UPDATE/DELETE/ROLL
);
2. BOM/Routing Change Handler
- Trigger: API/UI submits changes (e.g., add component, update qty).
- Process:
- Validate changes (e.g., no negative qty).
- Create new version: Insert with
valid_from = NOW(), set old valid_to = NOW() - 1s.
- Detect deltas: Compare JSON snapshots of old/new.
- Emit event:
BOM_CHANGE or ROUTING_CHANGE.
- Queue cost roll: Trigger async job.
- Pseudo-code:
def handle_bom_change(product_id, new_bom_data):
old_snapshot = bom_to_json(product_id, active_version=True)
new_bom_id = insert_new_bom_version(product_id, new_bom_data)
new_snapshot = bom_to_json(product_id, new_bom_id)
audit_log_insert('BOM', product_id, old_snapshot, new_snapshot)
queue_cost_roll(product_id)
3. Cost Roll Engine
- Input: Product ID + trigger event.
- Algorithm: Recursive bottom-up rollup.
- Fetch active BOM/routing for product (latest
valid_from <= NOW() <= valid_to).
- Material Costs: Sum
qty_per * std_cost for components (recurse if sub-assemblies).
- Direct Labor: Sum
labor_hrs * wage_rate (wage from cost center).
- Trigger Overhead Allocation.
- Total: Mat + Labor + Overhead.
- Store in
rolled_costs; emit COST_ROLL_COMPLETE.
- Recursion Handling: Use CTE or stack to avoid cycles (cycle detection via graph traversal).
- Pseudo-code:
def roll_costs(product_id):
bom = fetch_active_bom(product_id)
routing = fetch_active_routing(product_id)
# Material rollup
mat_cost = 0
for comp in bom.components:
if is_sub_assembly(comp):
mat_cost += roll_costs(comp.id) * comp.qty_per # Recurse
else:
mat_cost += fetch_std_cost(comp.id) * comp.qty_per
# Labor
labor_cost = sum(op.labor_hrs * fetch_wage(op.cost_center) for op in routing.ops)
# Overhead (from module below)
oh_cost = allocate_overhead(routing)
total = mat_cost + labor_cost + oh_cost
insert_rolled_cost(product_id, mat_cost, labor_cost, oh_cost, total)
return total
4. Overhead Allocation Module
5. Audit Service
- Immutable Logging: Every change/roll logs JSON snapshots to
audit_log.
- Query API:
GET /audit/product/{id}?from_date=... → Timeline view.
GET /audit/diffs/{log_id} → Side-by-side old/new.
- Retention: 7 years; GDPR-compliant purge.
- Alerts: Notify on high-impact changes (e.g., >10% cost variance).
Process Flow (End-to-End)
- User updates BOM/routing → Change Handler → Audit + Queue.
- Cost Roll Job picks queue → Fetches data → Rolls costs (recursive) → Allocates OH → Stores → Audit.
- UI queries
rolled_costs for variances (e.g., old vs. new total).
- Scheduler: Nightly full-roll for all active products.
Edge Cases & Validation
- Cycles in BOM: Detect via DFS; reject.
- Cost Gaps: Flag if
std_cost missing.
- Version Conflicts: Lock during roll.
- Performance: Cache rates; parallelize independent sub-assemblies.
- Testing: Unit (rollup fn), Integration (full flow), E2E (change → audit).
Implementation Notes
- Scalability: Use job queue (Celery/RabbitMQ) for large BOMs.
- API Endpoints:
| Method | Endpoint | Desc |
|---|
| POST | /bom/{product} | Update BOM |
| POST | /routing/{product} | Update Routing |
| GET | /costs/{product} | Latest rolled costs |
| GET | /roll/{product} | Trigger manual roll |
| GET | /audit/{entity}/{id} | Audit trail |
- Cost: ~O(N log N) per roll (N=components, log for recursion depth).
This design ensures accuracy, traceability, and efficiency. Questions or refinements?