ربط Odoo مع زاتكا
Odoo (نظام ERP) يبلغك لما الطلب يندفع، وماثل يحوّل هالدفعة لفاتورة زاتكا مرسلة ومقبولة، مع PDF عربي وإنجليزي فيه رمز QR.
نسخة Odoo السعودية فيها ربط مع الهيئة جاهز. ماثل يناسبك لو نسختك ما فيها هذا الربط أو تبي الـ PDF/A-3 والجهاز بدون تعقيد.
موديول أودو مجاني: موديول ماثل لأودو 17 و18 (Community وEnterprise): كل فاتورة تُرحّلها تنرسل للهيئة تلقائياً، مع QR وPDF والـXML مرفقين بالفاتورة. ثبّته وسجّل الجهاز من الإعدادات، وما تحتاج الكود تحت.
الحدث اللي تستمع له
Automation rule on account.move (customer invoices) with a 'Values Updated' trigger plus a condition payment_state = 'paid' (values: not_paid, in_payment, paid, partial, reversed, blocked, invoicing_legacy) and the action 'Send Webhook Notification'
التسجيل
Studio > Automations: model account.move, trigger + condition, Action 'Send Webhook Notification' with URL and Fields (the Sample Payload previews the request)
التحقق إن الطلب جاي من Odoo
None: Odoo POSTs plain JSON with no signature or secret (ir.actions.server _run_action_webhook). Put a long random token in the URL and check it
المبلغ والبنود
- المبلغ:
account.move: amount_untaxed, amount_tax, amount_total; account.move.line: price_subtotal ('Subtotal', without tax), price_total, price_unit - البنود:
Not in the webhook (it sends ids). Read account.move.line by move_id with display_type = 'product': name, quantity, price_subtotal - إيميل العميل:
partner_id -> res.partner.email (read with a second call)
لو المبلغ شامل الضريبة (وهو الغالب)، أرسله زي ما هو مع "prices_include_vat": true.
الكود
يستقبل الحدث، يتحقق منه، ويستدعي matheel_invoice(items): الدالة الجاهزة اللي تقفل صف الجهاز وترسل الفاتورة وتحفظ المفتاح الجديد.
import hmac, os, xmlrpc.client
from flask import Flask, request, abort
app = Flask(__name__)
URL, DB, USER, KEY = (os.environ[k] for k in ("ODOO_URL", "ODOO_DB", "ODOO_USER", "ODOO_API_KEY"))
# Odoo's webhook is unsigned: the URL carries ?token=... and we check it.
@app.post("/odoo/invoice-paid")
def invoice_paid():
if not hmac.compare_digest(request.args.get("token", ""), os.environ["ODOO_HOOK_TOKEN"]):
abort(401)
move_id = request.get_json()["_id"] # account.move id sent by Send Webhook Notification
uid = xmlrpc.client.ServerProxy(f"{URL}/xmlrpc/2/common").authenticate(DB, USER, KEY, {})
models = xmlrpc.client.ServerProxy(f"{URL}/xmlrpc/2/object")
lines = models.execute_kw(DB, uid, KEY, "account.move.line", "search_read",
[[["move_id", "=", move_id], ["display_type", "=", "product"]]],
{"fields": ["name", "quantity", "price_subtotal"]})
items = [{"name": l["name"], "quantity": str(l["quantity"]),
"price": f'{l["price_subtotal"] / l["quantity"]:.2f}'} for l in lines]
matheel_invoice(items)
return "", 200
ملاحظات
The webhook body is {_id, _model, _action} plus the fields you pick (record.read(load=None)), so relational fields arrive as ids only, hence the XML-RPC read. 'in_payment' may come before 'paid' depending on bank reconciliation: choose which one counts as paid. Odoo's Saudi Arabia localization has its own ZATCA e-invoicing (ZATCA API integration), so Matheel fits Odoo setups that don't use it. The docs and code here are for Odoo 18.0; whether newer versions still offer XML-RPC was not checked. Odoo docs give no payload schema, only 'POST API request with the values of the selected Fields'; the shape is from the 18.0 source.
أسئلة شائعة
هل Odoo يرسل الفواتير لزاتكا بنفسه؟
نسخة Odoo السعودية فيها ربط مع الهيئة جاهز. ماثل يناسبك لو نسختك ما فيها هذا الربط أو تبي الـ PDF/A-3 والجهاز بدون تعقيد.
لازم أعدّل الأسعار عشان الضريبة؟
لا. أرسل المبلغ اللي دفعه العميل مع "prices_include_vat": true، وماثل يطلّع الضريبة ويخلّي المبلغ المستحق في الفاتورة نفس المدفوع.
وش يصير لو جت دفعتين بنفس الثانية؟
فواتير الجهاز تمشي ورا بعض. اقفل صف الجهاز وقت الإرسال (موجود في الدالة الجاهزة)، وماثل يرفض أي نسخة قديمة من مفتاح الجهاز بـ 409 عشان السلسلة ما تنكسر.