# PDF Editor - Database Schema & Data Flow
## Database Table: `annotations`
### Location
- **Migration File**: `database/migrations/2025_09_29_104240_create_annotations_table.php`
- **Updated Migration**: `database/migrations/2025_10_16_131634_update_annotations_shape_type_enum.php`
### Table Structure
```sql
CREATE TABLE annotations (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
pdf_file VARCHAR(255),
page_number INT,
shape_type ENUM('rect', 'circle', 'triangle', 'spool', 'weld', 'shop_weld', 'leader_line', 'text'),
x_coordinate DECIMAL(10,2),
y_coordinate DECIMAL(10,2),
width DECIMAL(10,2),
height DECIMAL(10,2),
shape_id VARCHAR(100),
spool_no VARCHAR(100),
joint_type TEXT,
classification VARCHAR(50),
created_by BIGINT,
created_at TIMESTAMP,
updated_at TIMESTAMP
);
```
### Field Descriptions
| Field | Type | Description | Used For |
|-------|------|-------------|----------|
| `id` | BIGINT | Primary key | Unique identifier |
| `pdf_file` | VARCHAR(255) | PDF file path | Linking annotations to specific PDFs |
| `page_number` | INT | Page number | Multi-page PDF support |
| `shape_type` | ENUM | Type of annotation | rect, circle, triangle, spool, weld, shop_weld, leader_line, text |
| `x_coordinate` | DECIMAL(10,2) | X position | Canvas coordinate (for shapes: top-left, for lines: start point) |
| `y_coordinate` | DECIMAL(10,2) | Y position | Canvas coordinate |
| `width` | DECIMAL(10,2) | Width/Delta X | Shape width or line X distance |
| `height` | DECIMAL(10,2) | Height/Delta Y | Shape height or line Y distance |
| `shape_id` | VARCHAR(100) | Unique shape ID | Cross-referencing (leader lines to shapes, QR codes) |
| `spool_no` | VARCHAR(100) | Spool number | Business data for welding |
| `joint_type` | TEXT | Joint type or text content | Business data for welding OR free text content |
| `classification` | VARCHAR(50) | Annotation category | 'shape_text', 'free_text', etc. |
| `created_by` | BIGINT | User ID | Audit trail |
| `created_at` | TIMESTAMP | Creation time | Audit trail |
| `updated_at` | TIMESTAMP | Update time | Audit trail |
### Special Field Usage
#### Leader Lines
- `shape_type`: 'leader_line'
- `x_coordinate`, `y_coordinate`: Start point (click point)
- `width`: Delta X (x2 - x1)
- `height`: Delta Y (y2 - y1)
- Additional metadata stored in backend:
- `connectedShapeId`: The shape this line connects to
- `originalStartPoint`: Original click coordinates for reconnection
#### Text Annotations
- `shape_type`: 'text'
- `joint_type`: Contains the actual text content
- `classification`:
- 'shape_text': Text inside shapes (red, bold, 28px)
- 'free_text': Standalone notes (black, normal, 16px)
#### Shapes (rect, circle, triangle, etc.)
- Standard coordinate and dimension fields
- `spool_no`: **Business identifier - populated from Spool Number dropdown**
- Rectangle shapes: Display spool_no as text inside shape
- Source: MTO (Material Take-Off) data or manually generated
- Dropdown ID: `#spool-select`
- `joint_type`: **Joint/Weld classification - populated from Weld Number dropdown**
- Triangle/Circle shapes: Display joint_type (weld number) as text inside shape
- Source: MTO welders data or manually entered
- Dropdown ID: `#weld-select`
- `shape_id`: Used for QR code generation and leader line connection
**Shape Type → Data Mapping:**
- **Rectangle** → Uses `spool_no` for text content
- **Triangle** → Uses `joint_type` (weld number) for text content
- **Circle** → Uses `joint_type` (weld number) for text content
---
## Data Population System
### Spool Number (`spool_no` field)
#### What is it?
Spool number is a unique identifier for a spool in piping/welding systems. In the PDF Editor, this value is used to label rectangle shapes.
#### How it's populated:
1. **From MTO Data** (Material Take-Off):
```javascript
// Function: populateMtoForms()
// Populates dropdown from mtoData.spools array
spoolSelect.innerHTML = '';
mtoData.spools.forEach(spool => {
option.value = spool.spool_number; // e.g., "SP001"
option.textContent = spool.spool_number;
});
```
2. **Auto-Generate**:
- Button: "Generate" next to Spool Number dropdown
- API Call: `GET /api/mto/spools/generate-number`
- Returns: New unique spool number (e.g., "SP123")
3. **Manual Selection**:
- User selects from dropdown: `