⛔ CRITICAL |
Category: Odoo Tips |
ID: T13 |
Owner: CEO
T13 — Automation Rule + Batch Script = Duplicate Risk
Tóm tắt
Khi Odoo có Automated Action active trên 1 model (ví dụ: auto-invoice onpos.order state change), và ta chạy batch script gọi cùng method (action_pos_order_invoice) → cả rule VÀ script đều fire → tạo duplicate.
Sự cố 1 (2026-03-14) — Automation + Batch Script
backfill_pos_invoices.py gọi action_pos_order_invoice() cho 189 ordersSự cố 2 (2026-03-15) — Automation + Manual Session Close
action_pos_order_invoice() trực tiếpon_state_set trigger conflict với POS session close flowscripts/check_pos_automation.py (--disable/--enable/--check-dupes)Quy tắc cứng
TRƯỚC khi chạy batch script:
1. CHECK automation rules active trên model sẽ chạm 2. TẮT automation rules:base.automation.write([[rule_id], {'active': False}])
3. CHẠY batch script
4. BẬT LẠI automation rules
Code pattern an toàn:
Disable rule
models.execute_kw(db, uid, api_key, 'base.automation', 'write',
[[rule_id], {'active': False}])
try:
# Run batch
for order in orders:
models.execute_kw(...)
finally:
# Re-enable rule
models.execute_kw(db, uid, api_key, 'base.automation', 'write',
[[rule_id], {'active': True}])
Automation rule code nên có guard:
ĐÚNG — check trước khi tạo
if not record.account_move:
record.action_pos_order_invoice()
SAI — không check
record.action_pos_order_invoice() # Có thể tạo duplicate
Ảnh hưởng
Quy tắc bổ sung — Automation vs UI Manual Action
on_state_set trigger trên pos.order sẽ conflict với POS session close flowaction_pos_order_invoice() trực tiếp → KHÔNG qua guard trong automation codeif not record.account_move chỉ bảo vệ phía automation, không chặn UI sideChecklist
if not record.field trước khi tạo?
📚 Published from Company Knowledge Base — T13
Last updated: 2026-03-14
Review by: 2026-06-12