277 lines
9.5 KiB
Markdown
277 lines
9.5 KiB
Markdown
|
|
---
|
|||
|
|
proyek: Mito
|
|||
|
|
modul: mito_base
|
|||
|
|
odoo: "18.0"
|
|||
|
|
kategori: Discount & Loyalty
|
|||
|
|
status: Fix plan — sebagian sudah dikerjakan (performa cascade fix 2026-06-08)
|
|||
|
|
sumber: snk_mito/config/fixing_discount_loyalty.md
|
|||
|
|
tags:
|
|||
|
|
- mito
|
|||
|
|
- discount
|
|||
|
|
- loyalty
|
|||
|
|
- bug-fixing
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
> Index: [[Mito — Index]] · Kategori: **Discount & Loyalty**
|
|||
|
|
>
|
|||
|
|
> `_apply_tiered_discount` hanya membaca 2 field dari `loyalty.reward`, sisanya diabaikan —
|
|||
|
|
> 4 skenario gagal + fix plan 4 langkah. Termasuk catatan performa cascade `_compute_snk_discounts`.
|
|||
|
|
> Kasus turunan: [[Mito — Discount Kebagi Dua saat Ada Ongkir]]
|
|||
|
|
|
|||
|
|
# Fixing: Diskon Bertingkat — Loyalty Reward Tidak Bisa Diaplikasikan
|
|||
|
|
|
|||
|
|
**File terkait:**
|
|||
|
|
- `mito/mito_base/wizards/sale/sale_order_discount.py` → method `_apply_tiered_discount`
|
|||
|
|
- `mito/mito_base/wizards/sale/sale_order_discount_tier.py` → field `snk_discount_so`
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Root Cause
|
|||
|
|
|
|||
|
|
Method `_apply_tiered_discount` mengambil data dari `loyalty.reward` tapi **hanya membaca field `discount` dan `discount_line_product_id`**. Banyak properti penting dari reward yang diabaikan.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Field `loyalty.reward` yang Diabaikan (Sumber Masalah)
|
|||
|
|
|
|||
|
|
| Field | Fungsi | Kondisi di Code Sekarang |
|
|||
|
|
|-------|--------|--------------------------|
|
|||
|
|
| `discount_applicability` | `order` / `cheapest` / `specific` | ❌ Tidak dicek — discount dihitung pada semua lines |
|
|||
|
|
| `discount_product_ids` | Produk spesifik yang didiskon (jika `applicability=specific`) | ❌ Diabaikan — discount berlaku ke semua produk |
|
|||
|
|
| `all_discount_product_ids` | Computed: gabungan product IDs dari domain/category/tag | ❌ Tidak dipakai |
|
|||
|
|
| `discount_max_amount` | Cap maksimum nominal diskon | ❌ Tidak ada capping |
|
|||
|
|
| `discount_mode` | `percent` / `per_point` / `per_order` | ⚠️ Diasumsikan `percent`, bisa salah |
|
|||
|
|
| `reward_type` | Harus `discount` agar bisa dihitung | ⚠️ Tidak divalidasi sebelum apply |
|
|||
|
|
| `discount` | % diskon (hanya valid jika `discount_mode=percent`) | ✅ Sudah dibaca |
|
|||
|
|
| `discount_line_product_id` | Produk yang muncul di SO line sebagai baris diskon | ✅ Sudah dibaca |
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Scenario yang Gagal
|
|||
|
|
|
|||
|
|
### Scenario 1: `discount_applicability = 'specific'`
|
|||
|
|
Reward hanya berlaku untuk produk tertentu (misal: FG-000096, FG-000098).
|
|||
|
|
|
|||
|
|
**Yang terjadi sekarang:**
|
|||
|
|
```python
|
|||
|
|
# Code menghitung subtotal dari SEMUA lines
|
|||
|
|
for line in lines:
|
|||
|
|
total_price_per_tax_groups[taxes] += line.price_unit * line.product_uom_qty
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Yang seharusnya:**
|
|||
|
|
```python
|
|||
|
|
# Filter hanya produk yang ada di discount_product_ids
|
|||
|
|
if reward.discount_applicability == 'specific':
|
|||
|
|
discount_products = reward.all_discount_product_ids
|
|||
|
|
lines = lines.filtered(lambda l: l.product_id in discount_products)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### Scenario 2: `discount_max_amount` terlewat
|
|||
|
|
Reward punya cap Rp 46,760, tapi code langsung pakai nominal penuh.
|
|||
|
|
|
|||
|
|
**Yang seharusnya:**
|
|||
|
|
```python
|
|||
|
|
if reward.discount_max_amount and tier_discount_amount > reward.discount_max_amount:
|
|||
|
|
tier_discount_amount = reward.discount_max_amount
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### Scenario 3: `discount_mode != 'percent'`
|
|||
|
|
Jika reward memakai mode `per_order` (nominal flat per order) atau `per_point`, field `discount` tidak merepresentasikan persentase.
|
|||
|
|
|
|||
|
|
**Yang seharusnya:**
|
|||
|
|
```python
|
|||
|
|
if reward.discount_mode == 'percent':
|
|||
|
|
discount_pct = reward.discount # ini persentase
|
|||
|
|
elif reward.discount_mode == 'per_order':
|
|||
|
|
# discount adalah nominal flat, bukan pct
|
|||
|
|
tier_discount_amount = min(reward.discount, reward.discount_max_amount or reward.discount)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### Scenario 4: `reward_type != 'discount'`
|
|||
|
|
Jika user memilih reward bertipe `free_product` atau `promotion`, field `discount` bernilai 0 atau tidak relevan.
|
|||
|
|
|
|||
|
|
**Yang seharusnya:**
|
|||
|
|
```python
|
|||
|
|
if tier.snk_discount_so.reward_type != 'discount':
|
|||
|
|
_logger.warning(f"Tier {tier.snk_discount_seq}: reward type bukan discount, skip")
|
|||
|
|
continue
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Fix Plan
|
|||
|
|
|
|||
|
|
### Step 1 — Validasi `reward_type` sebelum apply
|
|||
|
|
Di `_apply_tiered_discount`, tambahkan guard di loop tier:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
reward = tier.snk_discount_so
|
|||
|
|
if reward.reward_type != 'discount':
|
|||
|
|
_logger.warning(f"Tier {tier.snk_discount_seq}: reward_type={reward.reward_type}, skip")
|
|||
|
|
continue
|
|||
|
|
if reward.discount_mode != 'percent':
|
|||
|
|
_logger.warning(f"Tier {tier.snk_discount_seq}: discount_mode={reward.discount_mode} belum didukung")
|
|||
|
|
continue
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2 — Filter lines berdasarkan `discount_applicability`
|
|||
|
|
Di bagian kalkulasi subtotal, filter lines sesuai reward:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
def _get_applicable_lines(self, lines, reward):
|
|||
|
|
if reward.discount_applicability == 'order':
|
|||
|
|
return lines
|
|||
|
|
elif reward.discount_applicability == 'cheapest':
|
|||
|
|
cheapest = min(lines, key=lambda l: l.price_unit, default=False)
|
|||
|
|
return cheapest if cheapest else lines.browse()
|
|||
|
|
elif reward.discount_applicability == 'specific':
|
|||
|
|
discount_products = reward.all_discount_product_ids
|
|||
|
|
return lines.filtered(lambda l: l.product_id in discount_products)
|
|||
|
|
return lines
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
Lalu di `_apply_tiered_discount`:
|
|||
|
|
```python
|
|||
|
|
applicable_lines = self._get_applicable_lines(lines, tier.snk_discount_so)
|
|||
|
|
if not applicable_lines:
|
|||
|
|
_logger.warning(f"Tier {tier.snk_discount_seq}: tidak ada produk yang match")
|
|||
|
|
continue
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 3 — Terapkan `discount_max_amount`
|
|||
|
|
Setelah hitung `tier_discount_amount`:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
max_amount = tier.snk_discount_so.discount_max_amount
|
|||
|
|
if max_amount and tier_discount_amount > max_amount:
|
|||
|
|
_logger.info(f"Tier {tier.snk_discount_seq}: capped dari {tier_discount_amount} ke {max_amount}")
|
|||
|
|
tier_discount_amount = max_amount
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 4 — Error message ke user jika reward tidak kompatibel
|
|||
|
|
Gunakan `ValidationError` atau `UserError` yang informatif:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
from odoo.exceptions import UserError
|
|||
|
|
|
|||
|
|
incompatible = [
|
|||
|
|
tier for tier in self.snk_discount_tier_ids
|
|||
|
|
if tier.snk_discount_so.discount_mode != 'percent'
|
|||
|
|
or tier.snk_discount_so.reward_type != 'discount'
|
|||
|
|
]
|
|||
|
|
if incompatible:
|
|||
|
|
names = ', '.join(t.snk_discount_so.display_name for t in incompatible)
|
|||
|
|
raise UserError(_(
|
|||
|
|
"Reward berikut tidak didukung untuk Diskon Bertingkat (harus tipe Discount, mode Percent):\n%s"
|
|||
|
|
) % names)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Priority
|
|||
|
|
|
|||
|
|
| No | Fix | Impact | Effort |
|
|||
|
|
|----|-----|--------|--------|
|
|||
|
|
| 1 | Validasi `reward_type != 'discount'` | Mencegah silent error | Rendah |
|
|||
|
|
| 2 | Filter lines by `discount_applicability` | Fix bug utama | Sedang |
|
|||
|
|
| 3 | Cap `discount_max_amount` | Koreksi nominal | Rendah |
|
|||
|
|
| 4 | Support `discount_mode = 'per_order'` | Fitur tambahan | Sedang |
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Catatan Domain Field
|
|||
|
|
|
|||
|
|
Di `sale_order_discount_tier.py`, domain saat ini:
|
|||
|
|
```python
|
|||
|
|
domain="[('program_id.snk_state', '=', 'posted')]"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
Pertimbangkan tambahkan filter `reward_type = 'discount'` dan `discount_mode = 'percent'` agar user hanya bisa pilih reward yang didukung:
|
|||
|
|
```python
|
|||
|
|
domain="[('program_id.snk_state', '=', 'posted'), ('reward_type', '=', 'discount'), ('discount_mode', '=', 'percent')]"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Performance Issue: `_compute_snk_discounts` Cascade
|
|||
|
|
|
|||
|
|
**File:** `mito/mito_base/models/sale/sale_order_line.py`
|
|||
|
|
|
|||
|
|
### Status Fix (2026-06-08)
|
|||
|
|
|
|||
|
|
**Fix yang sudah diterapkan** — hapus `'order_id.order_line'` dari `@api.depends`:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# SEBELUM (menyebabkan cascade 30×+ saat save)
|
|||
|
|
@api.depends(
|
|||
|
|
'order_id.order_line', # ← DIHAPUS
|
|||
|
|
'order_id.order_line.price_subtotal',
|
|||
|
|
'product_id', 'product_uom_qty', 'price_unit', 'tax_id'
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# SESUDAH (fix)
|
|||
|
|
@api.depends(
|
|||
|
|
'order_id.order_line.price_subtotal',
|
|||
|
|
'product_id', 'product_uom_qty', 'price_unit', 'tax_id',
|
|||
|
|
'discount', # ← DITAMBAHKAN
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Hasil setelah fix** (diverifikasi dari log `2026-06-08 05:32`):
|
|||
|
|
|
|||
|
|
| Event | Sebelum Fix | Sesudah Fix |
|
|||
|
|
|-------|-------------|-------------|
|
|||
|
|
| `web_save` (DB write) | 30×+ DISCOUNT SUM | **1× DISCOUNT SUM** ✅ |
|
|||
|
|
| `onchange` (in-memory) | ~30× | ~18× (17ms, tidak terasa) |
|
|||
|
|
|
|||
|
|
### Sisa Issue: Onchange 18×
|
|||
|
|
|
|||
|
|
Onchange `sale.order(['order_line'])` masih memanggil `_compute_snk_discounts` **18 kali**, tapi **bukan masalah performa** karena:
|
|||
|
|
- Semua 18 call selesai dalam **17ms** (in-memory, tidak ada DB write)
|
|||
|
|
- Total response onchange: **39ms**
|
|||
|
|
- Tidak ada cascade ke DB
|
|||
|
|
|
|||
|
|
**Root cause onchange 18×:**
|
|||
|
|
|
|||
|
|
`_compute_snk_discounts` menghasilkan **18 `store=True` fields**. Dalam onchange context, ORM Odoo 18 mengevaluasi setiap stored computed field secara individual. Setiap kali satu dari 18 field "ditulis" ke virtual record cache, framework re-check dependencies → trigger compute lagi.
|
|||
|
|
|
|||
|
|
Pola cascade onchange (ilustrasi):
|
|||
|
|
```
|
|||
|
|
price_subtotal berubah
|
|||
|
|
→ _compute_snk_discounts() call #1 → tulis snk_diskon_area (field 1/18)
|
|||
|
|
→ ORM: field berubah → re-check deps → call #2 → tulis snk_diskon_cash (field 2/18)
|
|||
|
|
→ ... × 18
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Fix Onchange (Opsional, P2)
|
|||
|
|
|
|||
|
|
Jika onchange 18× perlu dieliminasi (misalnya saat SO punya 20+ lines → menjadi 360 calls):
|
|||
|
|
|
|||
|
|
**Opsi A — Hapus `store=True` dari semua discount fields** (breaking change, butuh migrasi):
|
|||
|
|
```python
|
|||
|
|
snk_diskon_area = fields.Monetary(
|
|||
|
|
compute='_compute_snk_discounts',
|
|||
|
|
currency_field='currency_id',
|
|||
|
|
# store=True ← hapus ini
|
|||
|
|
)
|
|||
|
|
```
|
|||
|
|
Konsekuensi: field tidak bisa diquery via SQL (BI report rusak).
|
|||
|
|
|
|||
|
|
**Opsi B — Tambah guard di awal method** untuk skip jika nilai tidak berubah:
|
|||
|
|
```python
|
|||
|
|
def _compute_snk_discounts(self):
|
|||
|
|
# Skip lines yang tidak masuk all_goods_lines sebelum mulai loop besar
|
|||
|
|
# ... (early return untuk virtual records)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Opsi C — Pisahkan dependency** dari `order_id.order_line.price_subtotal` ke field yang lebih spesifik, tapi ini tidak mudah karena fungsi distribusi butuh total semua lines.
|
|||
|
|
|
|||
|
|
**Rekomendasi:** Biarkan 18× onchange untuk sekarang (17ms = tidak terasa). Monitor jika SO dengan 10+ lines mulai lambat saat edit.
|