Doc_chelvyn/Mito/Discount & Loyalty/Mito — Free Product Wizard.md

5.2 KiB

proyek modul odoo kategori status sumber tags
Mito snk_jobsheet_operation 18.0 Discount & Loyalty Selesai — wizard manual (Plan B), version 18.0.1.0.1 snk_mito/config/free_product_wizard.md
mito
discount
loyalty
wizard

Index: Mito — Index · Kategori: Discount & Loyalty

Free product (buy_x_get_y) tidak bisa diandalkan lewat reward standar Odoo — mismatch poin dan discount ke-reset saat save. Plan B: wizard manual, line dibuat tanpa reward_id sehingga mesin loyalty tidak pernah menyentuhnya.

Free Product Wizard — Diskon Free Product via Wizard Manual

Modul: snk_jobsheet_operation (version 18.0.1.0.1)

File terkait:

  • mito/snk_jobsheet_operation/wizards/snk_free_product.py → model snk.free.product + snk.free.product.line
  • mito/snk_jobsheet_operation/wizards/snk_free_product_views.xml → form wizard + act_window
  • mito/snk_jobsheet_operation/views/sale_order_views.xml → button Free Product di SO
  • mito/snk_jobsheet_operation/security/ir.model.access.csv → akses 2 model transient
  • mito/snk_jobsheet_operation/wizards/__init__.py, __manifest__.py → wiring + version

Background

Free product (program buy_x_get_y) tidak bisa diandalkan lewat mekanisme reward standar Odoo karena beberapa masalah berantai:

  1. Mismatch poin — rule grant 1.00 Credit per order (reward_point_mode = order), tapi reward butuh 2.00 Credits. Karena _get_claimable_rewards hanya memunculkan reward jika points >= required_points, maka 1 < 2 → reward tidak pernah muncul di wizard reward. (Mode per spent kebetulan jalan karena credit menumpuk ≥ 2.)
  2. Discount ke-reset — kalau free product dipasang sebagai reward line (reward_id keisi), loyalty re-apply nilai reward (discount = 100) saat save & confirm, sehingga discount yang di-override user manual (mis. 0%) balik ke 100%.

Plan B: bikin wizard manual untuk pilih free product, bypass total mesin poin loyalty.


Root Cause (kenapa Plan B beres di akar)

Free product line dibuat sebagai line NORMAL (sale.order.line biasa) — reward_id sengaja dikosongkan.

Masalah Kenapa lenyap di Plan B
Mismatch poin (1 < 2) Tidak ada cek points >= required_points sama sekali — user pilih langsung.
Discount ke-reset saat save/confirm Mesin loyalty (_update_programs_and_rewards) mencari line via reward_id. Tanpa reward_id, line tidak pernah disentuh loyalty → discount tidak recalculate.

Konsekuensi: tidak perlu hack save/restore discount. Requirement FSD "tidak recalculate kembali ketika input diskon & confirm" terpenuhi otomatis.


Alur Penggunaan

Path: Sales > Orders > Quotation/Order (state draft / sent)

  1. Klik button Free Product (di sebelah kanan button Reward).
  2. Klik Add a line:
    • Urutan (sequence)
    • Free Product (reward_id) — dropdown sudah difilter (lihat tabel filter)
    • Product & Quantity auto-keisi dari reward, qty editable
  3. Klik Apply → line ditambahkan ke SO:
    • product_id ← reward.reward_product_id
    • product_uom_qty ← reward.reward_product_qty (editable)
    • name = "Free Product - <product>"
    • discount = 100%
    • tanpa reward_id (line normal)

Filter Reward yang Muncul

Domain reward_id (via computed allowed_reward_ids):

Kondisi Nilai
reward_type product
program_id.program_type buy_x_get_y
program_id.snk_state posted
AP Form Kalau SO punya snk_ap_form → batasi ke program_id.snk_jobsheet_id in snk_ap_form. Kalau tidak ada AP Form → tampilkan semua yang posted.

Keputusan Desain

  • Line tanpa reward_id (bukan sekadar is_reward_line = False). Yang dipakai loyalty untuk recompute adalah reward_id, bukan is_reward_line. Jadi mengosongkan reward_id = satu-satunya cara line benar-benar lepas dari mesin loyalty.
  • Wizard ditaruh di snk_jobsheet_operation (bukan mito_base) karena butuh snk_ap_form (M2M ke jobsheet.operation) dan loyalty.program.snk_jobsheet_id, yang keduanya hidup di modul ini.
  • Tidak butuh migration script — model snk.free.product & snk.free.product.line adalah TransientModel (tabel auto-dibuat, auto-vacuum). Cukup naikkan version di manifest agar file baru ter-load saat upgrade.

Catatan Upgrade

Button Free Product di-anchor ke //button[@name='action_open_reward_wizard'] (button Reward standar sale_loyalty). Jika saat -u snk_jobsheet_operation muncul error "Element ... cannot be located", ganti anchor ke action_open_discount_wizard (button Discount, pasti ada).


Test Script

Step Ekspektasi
1. SO draft → isi customer, AP Form, order line (mis. WM-002 qty 5) —
2. Klik Free Product → Add a line → urutan 1 → pilih reward free product Dropdown hanya menampilkan reward sesuai filter
3. Qty auto-keisi dari reward (editable) → Apply Line free product nongol di SO, discount 100%
4. Ubah discount line free product, Save Discount tidak balik ke 100%
5. Confirm SO Discount tetap sesuai input user (tidak recalculate)