# Estimates Pivot Enterprise Blueprint (Final)

## 1) Tujuan
- Menutup gap laporan pivot agar setara kebutuhan enterprise, dengan memaksimalkan data yang **sudah ada**.
- Tidak menambah tabel baru pada fase ini.
- Menjaga kompatibilitas dengan pivot yang sudah berjalan di endpoint:
  - `/estimates/pivot_report`

## 2) Prinsip desain
- Satu sumber data inti (base dataset) dipakai ulang untuk banyak pivot.
- Semua pivot wajib konsisten terhadap filter global.
- KPI harus punya rumus baku agar angka tidak berbeda antar role.
- Pisahkan konsumsi:
  - Eksekutif (ringkas, high-level)
  - Operasional (drill-down detail)

## 3) Filter global wajib
- Owner
- Role profile
- Status estimate
- Time frame preset: `This week`, `This month`, `This quarter`, `YTD`, `Rolling 30D`, `Rolling 90D`
- Custom date range: `date_from` - `date_to`
- Date basis selector: `estimate_date` / `created_at` / `accepted_at`
- Source / Sub source
- Entity type: `Lead` / `Client`

## 4) KPI standar (rumus baku)
- `Estimate count` = jumlah dokumen estimate.
- `Estimate value` = SUM nilai estimate.
- `Accepted` = jumlah estimate status accepted.
- `Declined` = jumlah estimate status declined.
- `Accepted rate (%)` = `Accepted / (Accepted + Declined) * 100`.
- `Lead count` = jumlah estimate yang terkait lead.
- `Client count` = jumlah estimate yang terkait client.
- `Item lines` = total baris item estimate.
- `Item qty` = SUM qty item estimate.
- `Avg deal size` = `Estimate value / Estimate count`.

## 5) Struktur tab final (per role)

### Tab A: Executive Summary (Direksi)
- KPI cards: Total estimate, Total value, Accepted rate, Accepted/Declined, Avg deal size.
- Pivot 1: `Month x Status`.
- Pivot 2: `Channel (Source/Sub source) x Status`.
- Pivot 3: Top 10 Owner by value.
- Pivot 4: Top 10 Client by value.

### Tab B: Sales Manager
- KPI cards + tren mingguan.
- Pivot 1: `Owner x Week x Status`.
- Pivot 2: `Owner x Channel`.
- Pivot 3: `Owner x Lead/Client`.
- Pivot 4: Aging bucket (`0-3`, `4-7`, `8-14`, `>14` hari).

### Tab C: Sales Ops
- Fokus kualitas data dan throughput.
- Pivot 1: `Source x Sub source x Status`.
- Pivot 2: `Client x Status`.
- Pivot 3: `Lead x Status`.
- Pivot 4: daftar exception (draft tua, nilai nol, item qty ekstrem).

### Tab D: Detail Explorer (opsional)
- Tabel detail untuk export (Excel) dengan filter global yang sama.
- Digunakan untuk audit angka pivot.

## 6) Mapping ke modul yang sudah ada (tanpa tabel baru)

## Endpoint yang sudah ada
- View utama pivot:
  - `/estimates/pivot_report`

## Reuse query/dataset existing
- Dataset yang saat ini sudah mengisi:
  - `Multi-dimensional pivot by role`
  - `Owner x Week x Status`
- Dataset ini dijadikan **base dataset** untuk tab baru dengan tambahan dimensi:
  - `source`, `sub source`, `lead/client`, `client name`, `accepted_at`, `created_at`.

## Strategi endpoint
- Tetap satu endpoint page:
  - `/estimates/pivot_report`
- Tambah endpoint data terstruktur (JSON) per blok:
  - `/estimates/pivot_summary_data`
  - `/estimates/pivot_owner_week_status_data`
  - `/estimates/pivot_channel_data`
  - `/estimates/pivot_client_data`
  - `/estimates/pivot_lead_data`
  - `/estimates/pivot_aging_data`
- Semua endpoint wajib menerima filter global yang sama.

Catatan:
- Bila tim memilih minimal perubahan, seluruh blok bisa tetap ditarik dari satu endpoint data yang ada sekarang, lalu dipivot di server-side per `report_type`.

## 7) Mapping kebutuhan vs gap saat ini
- Sudah ada:
  - Month x Status
  - Owner x Week x Status
  - KPI dasar accepted/declined/value/count
- Belum lengkap:
  - Laporan per user dengan ranking + kontribusi
  - Tujuan submit / channel (source-sub source)
  - Konsumen (client-centric)
  - Lead-centric
  - Time frame preset konsisten
  - Custom range waktu + date basis selector

## 8) Prioritas implementasi (tanpa ubah skema DB)
- P1:
  - Filter global lengkap (time frame + date range + date basis).
  - Tab role profile otomatis dari role user (dengan override untuk admin).
  - Laporan per user + channel.
- P2:
  - Laporan client + lead.
  - Aging dan exception table.
- P3:
  - Executive summary polish + optimasi performa query.

## 9) UAT checklist ringkas
- Filter global mempengaruhi semua blok secara konsisten.
- Angka card = agregasi tabel detail.
- Accepted rate konsisten rumus di semua tab.
- Role profile membatasi data sesuai hak akses.
- Export Excel sesuai filter aktif.

## 10) Definisi selesai
- Semua gap utama (1-6) ter-cover di UI pivot.
- Tidak ada perbedaan angka antar blok untuk filter yang sama.
- Direksi, Sales Manager, Sales Ops masing-masing punya view yang relevan.

## 11) KPI Contract (wajib untuk enterprise)
- Setiap KPI wajib punya kontrak formal dengan komponen:
- `kpi_id`
- `business_definition`
- `sql_formula_reference`
- `grain` (harian, mingguan, bulanan, per owner, per status)
- `allowed_dimensions`
- `filter_behavior`
- `null_zero_rule`
- `inclusion_exclusion_rule`
- `data_owner`
- `approval_signoff` (Direksi, Sales Ops, Finance)

Contoh standar:
- `accepted_rate`:
- definisi: rasio keberhasilan keputusan final.
- rumus: `accepted / (accepted + declined)`.
- rule: jika denominator `0`, hasil `0.00%`.
- grain: per filter aktif.

## 12) Data Governance & Semantic Layer
- Tetapkan satu semantic definition layer untuk semua endpoint pivot.
- Wajib ada data dictionary untuk kolom yang dipakai di laporan.
- Wajib ada lineage: dari tabel sumber sampai KPI final.
- Wajib ada reconciliation rule:
- nilai kartu KPI harus sama dengan agregasi tabel detail untuk filter yang sama.

## 13) Security & Access Control
- Terapkan row-level security berdasarkan role dan struktur atasan-bawahan.
- Terapkan scope akses:
- Direksi: all
- Sales Manager: timnya
- Sales Ops: operasi sesuai domain yang diizinkan
- Audit trail wajib untuk:
- akses laporan
- export
- perubahan definisi KPI

## 14) Non-Functional Requirements (NFR)
- Data freshness SLA:
- near real-time: <= 15 menit atau batch harian (pilih satu dan tetapkan).
- Response time target:
- p95 <= 3 detik untuk filter umum.
- p99 <= 8 detik untuk rentang besar.
- Concurrency target:
- minimal 50 user bersamaan tanpa degradasi kritis.

## 15) Arsitektur Performa
- Gunakan pre-aggregation untuk dimensi berat (owner, month, status, source).
- Gunakan cache terkontrol per kombinasi filter.
- Gunakan indeks query untuk kolom:
- date basis
- owner
- status
- source/sub source
- lead/client flag
- Untuk data besar, siapkan materialized summary layer bertahap.

## 16) Quality Assurance (wajib otomatis)
- Unit test untuk rumus KPI inti.
- Integration test untuk endpoint data pivot.
- Reconciliation test:
- bandingkan output pivot vs SQL baseline.
- Regression test untuk mencegah perubahan angka saat release.
- UAT evidence template harus berisi screenshot + filter + hasil numerik.

## 17) Monitoring, Alerting, Runbook
- Monitor:
- error rate endpoint pivot
- latency p95/p99
- freshness data
- Alert wajib:
- refresh gagal
- latency melampaui threshold
- mismatch angka rekonsiliasi
- Siapkan runbook incident:
- langkah diagnosa
- fallback
- eskalasi PIC

## 18) Compliance, Privacy, Retention
- Masking data sensitif pada level role tertentu (jika ada PII).
- Kebijakan retensi data laporan ditetapkan resmi.
- Kontrol export:
- siapa boleh export
- watermark jika perlu
- log aktivitas export wajib tersimpan.

## 19) Change Management & Versioning
- Version KPI dengan format `KPI_VERSION`.
- Setiap perubahan rumus wajib:
- change note
- impact analysis
- backtest perbandingan sebelum-sesudah
- approval lintas fungsi
- Sediakan rollback plan untuk setiap release pivot.

## 20) Operating Model (RACI minimum)
- Product owner laporan: Sales Ops Head.
- Data owner: tim data/BI.
- Technical owner: engineering owner modul Estimates.
- Validator bisnis: Sales Manager + Finance.
- Approver final: Direksi.

## 21) Enterprise Go-Live Checklist
- KPI contract disetujui lintas fungsi.
- Security scope diuji untuk semua role.
- UAT lulus untuk seluruh tab dan filter global.
- Reconciliation pass untuk periode historis utama.
- Monitoring dan alert aktif.
- Dokumentasi runbook dan SOP tersedia.

## 22) Status Blueprint
- Dokumen ini sekarang mencakup:
- blueprint fungsional pivot
- hardening governance enterprise
- kontrol kualitas dan operasi produksi
- Langkah berikut:
- implementasi bertahap P1, P2, P3 dengan sign-off per fase.

## 23) Progress Snapshot (Update: 2026-04-19)

### Sudah selesai (implementasi kode)
- P1 core selesai:
  - Filter global konsisten: `owner_id`, `role_profile`, `status`, `time_frame`, `date_from`, `date_to`, `date_basis`, `source_id`, `sub_source_id`, `entity_type`.
  - Endpoint aktif:
    - `/estimates/pivot_summary_data`
    - `/estimates/pivot_owner_performance_data`
    - `/estimates/pivot_channel_performance_data`
  - UI pivot sudah memanggil endpoint P1 via AJAX.
- P2 core selesai:
  - Endpoint aktif:
    - `/estimates/pivot_client_data`
    - `/estimates/pivot_lead_data`
    - `/estimates/pivot_aging_data`
    - `/estimates/pivot_exception_data`
  - UI tabel tambahan sudah aktif:
    - Client performance
    - Lead performance
    - Aging bucket
    - Exception list
- I18n update:
  - Label pivot baru sudah dipindah ke `app_lang` (EN/ID).
- Bugfix terakhir:
  - Perbaikan filter `All time` agar tidak mewarisi `date_from/date_to/month` dari state sebelumnya.

### Belum selesai (untuk sesi lanjutan)
- UAT komprehensif per role (Direksi, Sales Manager, Sales Ops).
- SQL baseline reconciliation untuk semua blok P1+P2.
- Hardening validasi input:
  - `time_frame=custom` harus valid range.
  - Guardrail rentang tanggal terlalu panjang.
- Hardening security enterprise:
  - audit log akses/report/export.
  - verifikasi row-level policy lintas role.
- P3:
  - optimasi performa query (indexing/caching/pre-aggregation).
  - final executive polish.

### Next action yang disarankan saat lanjut
1. Jalankan UAT + reconciliation numerik per blok (summary/owner/channel/client/lead/aging/exception).
2. Tutup gap validasi input + guardrail rentang tanggal.
3. Lanjut P3 (performance hardening + monitoring).

Referensi eksekusi teknis P1:
- [ESTIMATES_PIVOT_P1_TECH_IMPLEMENTATION_CHECKLIST.md](\\192.168.5.14\web\san_ibb_master\files\ESTIMATES_PIVOT_P1_TECH_IMPLEMENTATION_CHECKLIST.md)
