# Task D.3: Lead / Enquiry Ownership Refinement

## Goal

ปรับ CRM Pipeline ให้สอดคล้องกับ Business Process ล่าสุด

Current:

```txt
Lead (unassigned enquiry)
↓
Opportunity (assigned enquiry)
↓
Quotation
↓
Won / Lost
```

New Business Flow:

```txt
Lead
↓ Assign To Sales
Enquiry
↓
Quotation
↓
Won / Lost
```

Important:

Task D.3 focuses on ownership, visibility, permissions, and lifecycle only.

Lead and Enquiry continue using the same underlying business fields and form structure.

Do NOT split the Lead form and Enquiry form yet.

Future form differences will be handled in a separate task if business requirements diverge.

---

# Business Rules

## Lead

Owner:

```txt
Marketing (MK)
```

Purpose:

```txt
Lead collection
Lead qualification
Lead nurturing
```

Status examples:

```txt
new
contacted
qualified
disqualified
assigned
```

Lead may exist without sales assignment.

---

## Enquiry

Owner:

```txt
Sales
```

Purpose:

```txt
Project follow-up
Opportunity development
Competitor tracking
Quotation preparation
```

Enquiry begins when:

```txt
Lead assigned to Sales
```

or

```txt
Sales creates enquiry directly
```

---

# Scope D3.1 Pipeline Terminology Refinement

Replace current KPI and workflow terminology.

Old:

```txt
Lead = enquiry without assignment
Opportunity = enquiry with assignment
```

New:

```txt
Lead = marketing-owned pipeline stage

Enquiry = sales-owned pipeline stage
```

Rules:

* Stop using Opportunity terminology in UI where practical
* Use Lead and Enquiry terminology consistently
* Preserve existing data compatibility

---

# Scope D3.2 Enquiry Pipeline Stage

Add field:

```txt
pipelineStage
```

Allowed values:

```txt
lead
enquiry
closed_won
closed_lost
```

Default:

```txt
lead
```

Rules:

### Marketing-created lead

```txt
pipelineStage = lead
assignedToUserId = null
```

### Assign to sales

Automatically:

```txt
pipelineStage = enquiry
assignedToUserId = salesUserId
assignedAt = now()
```

### Reassign

Keep:

```txt
pipelineStage = enquiry
```

### Won

```txt
pipelineStage = closed_won
```

### Lost

```txt
pipelineStage = closed_lost
```

---

# Scope D3.3 Assign Flow Refinement

Current assign flow already exists.

Update behavior:

Assign:

```txt
Lead
↓
Enquiry
```

When assign executes:

```txt
assignedToUserId
assignedAt
assignedBy
pipelineStage = enquiry
```

Audit:

```txt
assign
lead_to_enquiry
```

---

# Scope D3.4 MK Visibility Policy

Marketing users must be able to monitor progress.

MK can view:

```txt
Lead
Enquiry
Follow-ups
Status
Pipeline Stage
Assigned Sales
Won/Lost Result
```

MK cannot view:

```txt
Quotation Amount
Quotation Item
Quotation Cost
Discount
Margin
Approval Data
```

MK cannot:

```txt
Edit enquiry owned by sales
Create quotation
Modify quotation
Approve quotation
```

---

# Scope D3.5 Sales Visibility Policy

Sales can:

```txt
View assigned enquiries
Edit assigned enquiries
Create quotations
Manage follow-ups
View quotation pricing
```

Rules remain compatible with existing assignment ownership.

---

# Scope D3.6 Dashboard KPI Alignment

Update KPI service layer.

New definitions:

### Lead Count

```txt
pipelineStage = lead
```

### Enquiry Count

```txt
pipelineStage = enquiry
```

### Won Count

```txt
pipelineStage = closed_won
```

### Lost Count

```txt
pipelineStage = closed_lost
```

Remove dependency on:

```txt
assignedToUserId == null
```

for lead calculation.

---

# Scope D3.7 Revenue Rules

No revenue logic change.

Still use:

```txt
Quotation Amount
Revenue Attribution
Project Party Governance
```

from Task D.2.1.

---

# Scope D3.8 UI Labels

Update CRM wording.

Examples:

```txt
Assign Sales
→ Convert To Enquiry

Assigned Sales
→ Enquiry Owner

Open Opportunities
→ Active Enquiries
```

Do not rename database tables.

Only update business-facing labels.

---

# Scope D3.9 Backward Compatibility

Do NOT:

```txt
Create crm_leads table
Split enquiry form
Split enquiry schema
Split enquiry API
```

Continue using:

```txt
crm_enquiries
```

as the single source of truth.

Lead vs Enquiry is determined by:

```txt
pipelineStage
```

and ownership.

---

# Scope D3.10 ADR

Create:

```txt
docs/adr/0011-lead-enquiry-ownership-model.md
```

Document:

```txt
Lead ownership
Enquiry ownership
Assignment flow
MK visibility restrictions
Won/Lost lifecycle
```

This ADR supersedes the Opportunity terminology frozen in Task J.0.

---

# Explicit Non-Scope

Do NOT implement:

```txt
crm_leads table
Lead form
Lead API
Lead import
Campaign module
Marketing automation
Lead scoring
New dashboard widgets
```

---

# Output

1. Files Added
2. Files Modified
3. Pipeline Changes
4. Ownership Changes
5. Visibility Rules Added
6. KPI Changes
7. ADR Added
8. Migration Notes
9. Remaining Risks

---

# Definition of Done

✔ Lead and Enquiry terminology updated

✔ Assign converts Lead → Enquiry

✔ MK can monitor enquiries

✔ MK cannot see quotation pricing

✔ Sales owns enquiries

✔ Dashboard KPI follows pipelineStage

✔ No duplicate Lead module created

✔ Existing enquiry form reused

✔ Existing data remains compatible

✔ ADR created
