Price Book
Price Book Auto Cost Calculation
How component costs roll up into price book item pricing automatically.
6 min readPrice Book Auto Cost Calculation from Pending Components
Summary
Enhancement: Added automatic cost calculation for pending inventory components when creating a new price book item. The cost field now updates in real-time as components are added, matching the behavior when editing existing items.
Problem
When adding inventory components during price book item creation (new feature), the cost and unit price fields were not automatically calculated from the component costs. This was inconsistent with the behavior when editing existing items.
Solution
Implemented automatic cost calculation that:
- Calculates total cost from all pending components
- Updates the cost field automatically in the Basic Information tab
- Auto-calculates unit price if markup is set
- Displays visual feedback showing the cost is from components
- Disables manual cost editing when components are present
Changes Made
1. Added Cost Calculation Logic
Calculate Total Cost from Pending Components
// Calculate total cost from pending components
const pendingComponentsTotalCost = useMemo(() => {
return pendingComponents.reduce((total, component) => {
const unitCost = component.unitCost || 0
const quantity = component.quantity || 1
return total + (unitCost * quantity)
}, 0)
}, [pendingComponents])
Features:
- Uses
useMemofor performance optimization - Recalculates whenever components or quantities change
- Sums up: (unit cost Ć quantity) for each component
Auto-Update Form Fields
// Update form cost field when pending components change
useEffect(() => {
if (!editingItem && pendingComponents.length > 0) {
form.setFieldsValue({ cost: pendingComponentsTotalCost })
// Auto-calculate unit price if markup is set
const markup = form.getFieldValue('markup')
if (markup && markup > 0) {
const calculatedPrice = pendingComponentsTotalCost * (1 + markup / 100)
form.setFieldsValue({ unitPrice: calculatedPrice })
}
}
}, [pendingComponentsTotalCost, pendingComponents.length, form, editingItem])
Features:
- Automatically updates
costfield when components change - Automatically calculates
unitPriceif markup is set - Only applies to new items (not editing existing items)
2. Visual Cost Summary in Components Tab
Added a green summary card showing:
- Total Cost - Large, prominent display with $ formatting
- Component Count - Number of selected components
- Info Message - Explains cost will auto-update in Basic Information tab
<Card size="small" style={{ marginBottom: 16, backgroundColor: '#f6ffed', borderColor: '#b7eb8f' }}>
<Row gutter={16}>
<Col span={6}>
<Typography.Text type="secondary">Total Cost</Typography.Text>
<Typography.Text strong style={{ fontSize: 24, color: '#52c41a' }}>
${pendingComponentsTotalCost.toFixed(2)}
</Typography.Text>
</Col>
<Col span={6}>
<Typography.Text type="secondary">Components</Typography.Text>
<Typography.Text strong style={{ fontSize: 24 }}>
{pendingComponents.length}
</Typography.Text>
</Col>
<Col span={12}>
<InboxOutlined style={{ marginRight: 8, color: '#52c41a', fontSize: 20 }} />
<Typography.Text type="secondary">
Cost will be automatically calculated...
</Typography.Text>
</Col>
</Row>
</Card>
3. Enhanced Cost Field in Basic Information Tab
Before: Cost field was always editable
After: Cost field is disabled and auto-populated when components exist
After: Cost field is disabled and auto-populated when components exist
<Form.Item name="cost" label="Cost">
<InputNumber
min={0}
precision={2}
style={{ width: '100%' }}
placeholder="0.00"
addonBefore="quot;
disabled={hasComponentCost}
value={hasComponentCost ? componentCost : getFieldValue('cost')}
onChange={(value) => {
if (!hasComponentCost) {
form.setFieldValue('cost', value)
}
}}
/>
{(calculatedCost > 0 || pendingComponentsTotalCost > 0) && (
<Alert
message={`Cost auto-calculated from ${calculatedCost > 0 ? 'saved' : 'pending'} components`}
type="success"
showIcon
/>
)}
</Form.Item>
Features:
- Shows green success alert when cost is from components
- Differentiates between "saved" and "pending" components
- Field is disabled to prevent manual override
- Automatically displays calculated cost
4. Enhanced Unit Price Feedback
Updated the unit price alert to show:
- Cost source (component cost vs manual cost)
- Markup percentage
- Calculation formula
<Alert
message={`Price calculated from ${hasComponentCost ? 'component cost' : 'cost'} + ${markup}% markup`}
type="success"
showIcon
style={{ marginTop: 4, fontSize: 11 }}
/>
User Experience
Workflow
- Create new price book item
- Go to "Inventory Components" tab
- Click "Add Components"
- Select inventory items with quantities
- See cost summary immediately in green card
- Return to "Basic Information" tab
- Cost field is auto-populated and disabled
- If markup is set, unit price auto-calculates
- Visual alerts show what's being calculated
Visual Feedback
In Inventory Components Tab:
- š¢ Green summary card with total cost
- š Real-time component count
- š” Info message explaining auto-calculation
In Basic Information Tab:
- š Disabled cost field (can't manually edit)
- ā Green success alert: "Cost auto-calculated from pending components"
- ā Price alert: "Price calculated from component cost + X% markup"
Benefits
- ā” Real-time calculation - See costs update as you add components
- š¢ Accurate pricing - No manual calculation errors
- š Visual clarity - Clear indicators of where costs come from
- š Consistent behavior - Matches editing existing items
- š”ļø Prevents errors - Can't accidentally override calculated costs
- š° Automatic markup - Unit price auto-calculates with markup
Examples
Example 1: Simple Component Addition
- Add 3 screws @ $0.50 each = $1.50
- Add 1 bracket @ $5.00 = $5.00
- Total Cost: $6.50 (automatically shown)
- Set markup to 50%
- Unit Price: $9.75 (automatically calculated)
Example 2: Editing Quantities
- Add 10 units @ $2.00 each = $20.00
- Change quantity to 15
- Cost updates to $30.00 immediately
- If markup is 100%, price updates to $60.00
Example 3: Multiple Components
- Component A: 5 Ć $1.00 = $5.00
- Component B: 2 Ć $10.00 = $20.00
- Component C: 1 Ć $15.00 = $15.00
- Total: $40.00 (shown in green card)
- With 25% markup ā Price: $50.00
Edge Cases Handled
ā
No components - Cost field is editable
ā Components removed - Cost recalculates
ā Quantity changed - Cost updates immediately
ā Zero cost items - Handled gracefully
ā Editing existing items - Uses saved components
ā Switching tabs - Cost persists correctly
ā Modal cancelled - Pending components cleared
ā Components removed - Cost recalculates
ā Quantity changed - Cost updates immediately
ā Zero cost items - Handled gracefully
ā Editing existing items - Uses saved components
ā Switching tabs - Cost persists correctly
ā Modal cancelled - Pending components cleared
Files Modified
apps/web/src/pages/priceBook/PriceBook.tsx- Added
pendingComponentsTotalCostcalculation (useMemo) - Added useEffect to auto-update form fields
- Enhanced cost field with disabled state and alerts
- Enhanced unit price alerts with better messaging
- Added cost summary card in components tab
Testing Checklist
- Add components and verify cost auto-calculates
- Change component quantities and verify cost updates
- Set markup and verify unit price auto-calculates
- Remove components and verify cost clears
- Verify cost field is disabled when components exist
- Verify cost field is editable when no components
- Check green alerts display correctly
- Verify cost summary card shows correct totals
- Test with zero-cost components
- Test with decimal quantities (e.g., 0.5)
- Verify editing existing items still works
Future Enhancements
- Add profit margin calculator
- Show total cost vs selling price comparison
- Add suggested pricing based on market rates
- Batch update markup for multiple items
- Export cost breakdown report
Created: October 18, 2025
Status: ā Complete - Ready for Testing Related: PRICE_BOOK_COMPONENTS_CREATE_FEATURE.md
Status: ā Complete - Ready for Testing Related: PRICE_BOOK_COMPONENTS_CREATE_FEATURE.md