# Task J.0: KPI Definition Freeze

## Goal

Freeze KPI Definitions และ Sales Pipeline Definitions ก่อนเริ่มสร้าง CRM Dashboard KPI

หลัง Task J.0 ผ่านแล้ว ห้ามเปลี่ยนนิยาม KPI โดยไม่มี ADR ใหม่

---

# Sales Pipeline Definition

## Lead

Definition:

```txt
ยังไม่ได้ Assign Sales
```

Rules:

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

KPI Impact:

```txt
Lead Count
Lead Aging
Lead Source
New Leads
```

---

## Opportunity

Definition:

```txt
Assign Sales แล้ว
```

Rules:

```txt
assignedToUserId != null
pipelineStage = opportunity
```

Auto Transition:

```txt
Lead
↓ Assign Sales
Opportunity
```

KPI Impact:

```txt
Opportunity Count
Opportunity Value
Hot Opportunities
Opportunity Aging
```

---

## Closed Won

Definition:

```txt
ลูกค้ายืนยันรับงาน
หรือโครงการได้รับ Award
```

Rules:

```txt
pipelineStage = closed_won
```

Recommendation:

Add future field:

```txt
closedWonAt
```

---

## Closed Lost

Definition:

```txt
แพ้คู่แข่ง
งบประมาณไม่ผ่าน
ลูกค้ายกเลิก
โครงการไม่เดินต่อ
```

Rules:

```txt
pipelineStage = closed_lost
```

Recommendation:

Add future field:

```txt
closedLostAt
lostReason
```

---

# Opportunity Lifecycle

```txt
Lead
↓ Assign Sales
Opportunity
↓ Create Quotation
Opportunity
↓ Approved Quotation
Opportunity
↓ Award
Closed Won
```

---

# Customer Party Definition

## Billing Customer

Definition:

```txt
บริษัทที่ใช้ในการออกใบเสนอราคา
หรือวางบิล
```

Examples:

```txt
ALLA
Italian Thai
SCG
```

---

## End Customer

Definition:

```txt
เจ้าของโครงการตัวจริง
```

Examples:

```txt
PTT
CP
SCG Chemicals
```

---

## Contractor

Definition:

```txt
ผู้รับเหมาก่อสร้าง
```

---

## Consultant

Definition:

```txt
ที่ปรึกษาโครงการ
```

---

## Customer

Definition:

```txt
คู่ค้าหรือผู้เกี่ยวข้องทั่วไป
```

---

# KPI Definitions

## Lead Metrics

```txt
Lead Count
New Leads
Unassigned Leads
Lead Aging
```

Lead Count:

```txt
pipelineStage = lead
```

---

## Opportunity Metrics

```txt
Opportunity Count
Open Opportunities
Hot Opportunities
```

Opportunity Count:

```txt
pipelineStage = opportunity
```

---

## Conversion Metrics

### Lead → Opportunity

Formula:

```txt
Opportunity Created
/
Total Leads
```

---

### Opportunity → Quotation

Formula:

```txt
Opportunity With Quotation
/
Total Opportunities
```

---

### Quotation → Won

Formula:

```txt
Closed Won
/
Total Opportunities With Quotation
```

---

# Revenue Metrics

## Opportunity Value

Source:

```txt
crm_enquiries.estimatedValue
```

Condition:

```txt
pipelineStage = opportunity
```

---

## Quotation Value

Source:

```txt
crm_quotations.totalAmount
```

Condition:

```txt
quotation status != cancelled
```

---

## Won Value

Source:

```txt
crm_quotations.totalAmount
```

Condition:

```txt
pipelineStage = closed_won
```

---

## Lost Value

Source:

```txt
crm_quotations.totalAmount
```

Condition:

```txt
pipelineStage = closed_lost
```

---

# Sales Ownership

Dashboard must use:

```txt
assignedToUserId
```

for:

```txt
Lead
Opportunity
```

and

```txt
salesmanId
```

for:

```txt
Quotation
```

---

# Future Fields (Not In Scope)

Do NOT implement now.

Track as technical debt:

```txt
convertedToOpportunityAt
closedWonAt
closedLostAt
lostReason
defaultPartyRole
```

---

# Output

1. KPI Definitions Frozen
2. Sales Pipeline Frozen
3. Customer Party Definitions Frozen
4. Revenue Rules Frozen
5. Dashboard Ready

---

# Definition of Done

✔ Lead definition frozen

✔ Opportunity definition frozen

✔ Won/Lost definition frozen

✔ Billing Customer definition frozen

✔ Project Party role definition frozen

✔ KPI formulas frozen

✔ Revenue formulas frozen

✔ Ready for Task J
