feat(pos): migrate CFDI timbrado from Horux to Facturapi
- Add Facturapi REST service (invoices, customers, orgs, cancel, downloads) - Add JSON payload builder for ingreso/egreso/pago/global invoices - Replace XML queue with Facturapi JSON queue (payload_unsigned, external_id) - Update invoicing blueprint with Facturapi config and download endpoints - Update global invoice service to use Facturapi payloads - Add migration v4.3_facturapi.sql and tenant rollout script - Update invoicing UI: payload preview, PDF/XML downloads, PAC status panel - Add FACTURAPI_USER_KEY to .env.example
This commit is contained in:
243
pos/services/cfdi_facturapi_builder.py
Normal file
243
pos/services/cfdi_facturapi_builder.py
Normal file
@@ -0,0 +1,243 @@
|
||||
# /home/Autopartes/pos/services/cfdi_facturapi_builder.py
|
||||
"""Build Facturapi invoice payloads from Nexus sales data.
|
||||
|
||||
Facturapi expects a JSON payload instead of an unsigned XML. This module
|
||||
generates those payloads for:
|
||||
- Ingreso (sale invoice)
|
||||
- Egreso (credit note)
|
||||
- Pago (payment complement)
|
||||
- Factura global mensual
|
||||
"""
|
||||
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
from datetime import datetime
|
||||
|
||||
# SAT defaults
|
||||
RFC_PUBLICO_GENERAL = "XAXX010101000"
|
||||
RFC_EXTRANJERO = "XEXX010101000"
|
||||
|
||||
# Forma de pago mapping (Nexus internal -> SAT code)
|
||||
FORMA_PAGO_MAP = {
|
||||
"efectivo": "01",
|
||||
"transferencia": "03",
|
||||
"tarjeta": "04",
|
||||
"cheque": "02",
|
||||
"credito": "99",
|
||||
"mixto": "99",
|
||||
"99": "99",
|
||||
}
|
||||
|
||||
# Metodo de pago
|
||||
METODO_PAGO_MAP = {
|
||||
"PUE": "PUE",
|
||||
"PPD": "PPD",
|
||||
}
|
||||
|
||||
TWO = Decimal("0.01")
|
||||
SIX = Decimal("0.000001")
|
||||
|
||||
|
||||
def _to_dec(val):
|
||||
if val is None:
|
||||
return Decimal("0")
|
||||
return Decimal(str(val))
|
||||
|
||||
|
||||
def _fmt2(val):
|
||||
return float(_to_dec(val).quantize(TWO, ROUND_HALF_UP))
|
||||
|
||||
|
||||
def _fmt6(val):
|
||||
return float(_to_dec(val).quantize(SIX, ROUND_HALF_UP))
|
||||
|
||||
|
||||
def _resolve_forma_pago(sale):
|
||||
method = (sale.get("payment_method") or "").lower().strip()
|
||||
fp = (sale.get("forma_pago_sat") or "").strip()
|
||||
if fp:
|
||||
return fp
|
||||
return FORMA_PAGO_MAP.get(method, "99")
|
||||
|
||||
|
||||
def _resolve_metodo_pago(sale):
|
||||
mp = (sale.get("metodo_pago_sat") or "").upper().strip()
|
||||
if mp in ("PUE", "PPD"):
|
||||
return mp
|
||||
# Default: credit sales are PPD, cash sales are PUE
|
||||
if sale.get("sale_type") == "credit" or sale.get("payment_method") == "credito":
|
||||
return "PPD"
|
||||
return "PUE"
|
||||
|
||||
|
||||
def _build_items(sale_items):
|
||||
items = []
|
||||
for item in sale_items or []:
|
||||
qty = int(item.get("quantity", 1))
|
||||
unit_price = _to_dec(item.get("unit_price", 0))
|
||||
discount = _to_dec(item.get("discount_amount", 0))
|
||||
tax_rate = _to_dec(item.get("tax_rate", "0.16"))
|
||||
|
||||
# Facturapi price is unit price before taxes and discounts
|
||||
product = {
|
||||
"description": item.get("name") or "Autoparte",
|
||||
"product_key": item.get("clave_prod_serv") or "25174800",
|
||||
"unit_key": item.get("clave_unidad") or "H87",
|
||||
"unit_name": "Pieza",
|
||||
"price": _fmt2(unit_price),
|
||||
"tax_included": False,
|
||||
"taxes": [
|
||||
{
|
||||
"type": "IVA",
|
||||
"rate": _fmt6(tax_rate),
|
||||
"factor": "Tasa",
|
||||
}
|
||||
],
|
||||
}
|
||||
if discount > 0:
|
||||
product["discount"] = _fmt2(discount / qty) if qty > 0 else _fmt2(discount)
|
||||
|
||||
items.append({"quantity": qty, "product": product})
|
||||
return items
|
||||
|
||||
|
||||
def _build_customer_payload(customer, tenant_cp):
|
||||
if not customer or not customer.get("rfc"):
|
||||
# Publico en general
|
||||
return {
|
||||
"tax_id": RFC_PUBLICO_GENERAL,
|
||||
"legal_name": "PUBLICO EN GENERAL",
|
||||
"tax_system": "616",
|
||||
"address": {"zip": tenant_cp or "00000"},
|
||||
}
|
||||
|
||||
rfc = (customer.get("rfc") or "").upper().strip()
|
||||
return {
|
||||
"tax_id": rfc,
|
||||
"legal_name": customer.get("razon_social") or customer.get("name") or rfc,
|
||||
"tax_system": customer.get("regimen_fiscal") or "616",
|
||||
"email": customer.get("email"),
|
||||
"address": {"zip": customer.get("cp") or tenant_cp or "00000"},
|
||||
}
|
||||
|
||||
|
||||
def build_ingreso_payload(sale, tenant_config, customer=None):
|
||||
"""Build Facturapi payload for a sale (Comprobante tipo Ingreso)."""
|
||||
tenant_cp = tenant_config.get("cp", "00000")
|
||||
customer_payload = _build_customer_payload(customer, tenant_cp)
|
||||
|
||||
payload = {
|
||||
"customer": customer_payload,
|
||||
"items": _build_items(sale.get("items", [])),
|
||||
"use": customer.get("uso_cfdi") if customer and customer.get("rfc") else "S01",
|
||||
"payment_form": _resolve_forma_pago(sale),
|
||||
"payment_method": _resolve_metodo_pago(sale),
|
||||
"currency": "MXN",
|
||||
"series": tenant_config.get("serie", "A"),
|
||||
"folio_number": sale["id"],
|
||||
}
|
||||
|
||||
# Optional exchange rate for USD
|
||||
if sale.get("currency") and sale["currency"] != "MXN" and sale.get("exchange_rate"):
|
||||
payload["exchange"] = _fmt6(sale["exchange_rate"])
|
||||
payload["currency"] = sale["currency"]
|
||||
|
||||
return payload
|
||||
|
||||
|
||||
def build_egreso_payload(sale, tenant_config, customer, original_uuid):
|
||||
"""Build Facturapi payload for a credit note (Comprobante tipo Egreso)."""
|
||||
payload = build_ingreso_payload(sale, tenant_config, customer)
|
||||
payload["type"] = "E"
|
||||
payload["related_documents"] = [
|
||||
{"relationship": "01", "documents": [original_uuid]}
|
||||
]
|
||||
payload["payment_method"] = "PUE"
|
||||
return payload
|
||||
|
||||
|
||||
def build_pago_payload(payment, tenant_config, customer, original_uuid):
|
||||
"""Build Facturapi payload for a payment complement (Comprobante tipo Pago)."""
|
||||
tenant_cp = tenant_config.get("cp", "00000")
|
||||
customer_payload = _build_customer_payload(customer, tenant_cp)
|
||||
|
||||
amount = _to_dec(payment.get("amount", 0))
|
||||
base = (amount / Decimal("1.16")).quantize(TWO, ROUND_HALF_UP)
|
||||
iva = (amount - base).quantize(TWO, ROUND_HALF_UP)
|
||||
|
||||
payment_date = payment.get("date") or datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
|
||||
if "T" not in str(payment_date):
|
||||
payment_date = f"{payment_date}T12:00:00"
|
||||
|
||||
forma_pago = FORMA_PAGO_MAP.get(
|
||||
(payment.get("payment_method") or "").lower().strip(), "01"
|
||||
)
|
||||
|
||||
payload = {
|
||||
"type": "P",
|
||||
"customer": customer_payload,
|
||||
"complements": [
|
||||
{
|
||||
"type": "pago",
|
||||
"data": {
|
||||
"payment_form": forma_pago,
|
||||
"payment_date": payment_date,
|
||||
"amount": _fmt2(amount),
|
||||
"related_documents": [
|
||||
{
|
||||
"uuid": original_uuid,
|
||||
"amount": _fmt2(amount),
|
||||
"taxes": [
|
||||
{
|
||||
"type": "IVA",
|
||||
"rate": 0.16,
|
||||
"factor": "Tasa",
|
||||
"base": _fmt2(base),
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
return payload
|
||||
|
||||
|
||||
def build_global_invoice_payload(sales, tenant_config, year, month):
|
||||
"""Build Facturapi payload for a monthly global invoice."""
|
||||
tenant_cp = tenant_config.get("cp", "00000")
|
||||
|
||||
total_subtotal = Decimal("0")
|
||||
total_discount = Decimal("0")
|
||||
total_tax = Decimal("0")
|
||||
total_total = Decimal("0")
|
||||
all_items = []
|
||||
|
||||
for sale in sales:
|
||||
total_subtotal += _to_dec(sale.get("subtotal", 0))
|
||||
total_discount += _to_dec(sale.get("discount_total", 0))
|
||||
total_tax += _to_dec(sale.get("tax_total", 0))
|
||||
total_total += _to_dec(sale.get("total", 0))
|
||||
all_items.extend(_build_items(sale.get("items", [])))
|
||||
|
||||
payload = {
|
||||
"customer": {
|
||||
"tax_id": RFC_PUBLICO_GENERAL,
|
||||
"legal_name": "PUBLICO EN GENERAL",
|
||||
"tax_system": "616",
|
||||
"address": {"zip": tenant_cp},
|
||||
},
|
||||
"items": all_items,
|
||||
"use": "S01",
|
||||
"payment_form": "01",
|
||||
"payment_method": "PUE",
|
||||
"currency": "MXN",
|
||||
"series": tenant_config.get("serie", "FG"),
|
||||
"folio_number": int(f"{year}{month:02d}"),
|
||||
"global": {
|
||||
"periodicity": "04", # Mensual
|
||||
"months": f"{month:02d}",
|
||||
"year": year,
|
||||
},
|
||||
}
|
||||
return payload
|
||||
@@ -1,25 +1,25 @@
|
||||
# /home/Autopartes/pos/services/cfdi_queue.py
|
||||
"""CFDI queue service: manages the timbrado pipeline.
|
||||
"""CFDI queue service: manages the Facturapi timbrado pipeline.
|
||||
|
||||
Flow:
|
||||
1. enqueue_cfdi() — inserts XML into cfdi_queue with status='pending'
|
||||
2. process_queue() — sends pending items to Horux API, updates status
|
||||
1. enqueue_cfdi() — inserts Facturapi JSON payload into cfdi_queue with status='pending'
|
||||
2. process_queue() — sends pending items to Facturapi, updates status
|
||||
3. retry_failed() — retries failed items with exponential backoff
|
||||
4. cancel_cfdi() — sends cancel request to Horux API
|
||||
4. cancel_cfdi() — cancels a stamped CFDI via Facturapi
|
||||
|
||||
Horux API endpoints:
|
||||
POST /api/nexus/cfdi/stamp — send unsigned XML, receive signed+timbrado
|
||||
GET /api/nexus/cfdi/status/:uuid — check timbrado status
|
||||
POST /api/nexus/cfdi/cancel — cancel CFDI with SAT motive code
|
||||
Facturapi endpoints used:
|
||||
POST /v2/invoices — create and stamp an invoice
|
||||
GET /v2/invoices/:id — fetch invoice metadata
|
||||
DELETE /v2/invoices/:id — cancel with SAT motive
|
||||
|
||||
Retry backoff: 5s, 30s, 2m, 10m, 1h (max 5 retries)
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
import requests
|
||||
from services import facturapi_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -29,10 +29,7 @@ MAX_RETRIES = len(BACKOFF_INTERVALS)
|
||||
|
||||
|
||||
def _generate_provisional_folio(conn):
|
||||
"""Generate a provisional folio like PRE-00001.
|
||||
|
||||
Uses the cfdi_queue table's max id to avoid collisions.
|
||||
"""
|
||||
"""Generate a provisional folio like PRE-00001."""
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT COALESCE(MAX(id), 0) + 1 FROM cfdi_queue")
|
||||
seq = cur.fetchone()[0]
|
||||
@@ -40,14 +37,14 @@ def _generate_provisional_folio(conn):
|
||||
return f'PRE-{seq:05d}'
|
||||
|
||||
|
||||
def enqueue_cfdi(conn, sale_id, cfdi_type, xml):
|
||||
def enqueue_cfdi(conn, sale_id, cfdi_type, payload):
|
||||
"""Add a CFDI to the timbrado queue.
|
||||
|
||||
Args:
|
||||
conn: psycopg2 connection
|
||||
sale_id: int (FK to sales)
|
||||
sale_id: int (FK to sales), may be None for global invoices
|
||||
cfdi_type: 'ingreso' | 'egreso' | 'pago'
|
||||
xml: str (unsigned XML from cfdi_builder)
|
||||
payload: dict (Facturapi JSON payload) or str (JSON string)
|
||||
|
||||
Returns:
|
||||
dict: {id, sale_id, type, status, provisional_folio}
|
||||
@@ -55,12 +52,14 @@ def enqueue_cfdi(conn, sale_id, cfdi_type, xml):
|
||||
provisional_folio = _generate_provisional_folio(conn)
|
||||
cur = conn.cursor()
|
||||
|
||||
payload_json = payload if isinstance(payload, str) else json.dumps(payload)
|
||||
|
||||
cur.execute("""
|
||||
INSERT INTO cfdi_queue
|
||||
(sale_id, type, xml_unsigned, status, provisional_folio)
|
||||
(sale_id, type, payload_unsigned, status, provisional_folio)
|
||||
VALUES (%s, %s, %s, 'pending', %s)
|
||||
RETURNING id, created_at
|
||||
""", (sale_id, cfdi_type, xml, provisional_folio))
|
||||
""", (sale_id, cfdi_type, payload_json, provisional_folio))
|
||||
cfdi_id, created_at = cur.fetchone()
|
||||
cur.close()
|
||||
|
||||
@@ -74,17 +73,17 @@ def enqueue_cfdi(conn, sale_id, cfdi_type, xml):
|
||||
}
|
||||
|
||||
|
||||
def process_queue(conn, horux_api_url, api_key):
|
||||
def process_queue(conn, tenant_config, dry_run=False):
|
||||
"""Process all pending CFDI items in the queue.
|
||||
|
||||
Sends each pending XML to Horux for timbrado. On success, updates
|
||||
Sends each pending payload to Facturapi for timbrado. On success, updates
|
||||
the record with the signed XML and UUID fiscal. On failure, increments
|
||||
retry_count and records the error.
|
||||
|
||||
Args:
|
||||
conn: psycopg2 connection
|
||||
horux_api_url: str base URL for Horux API (e.g. 'https://horux.example.com')
|
||||
api_key: str Horux API key
|
||||
tenant_config: dict with facturapi_key (and optional facturapi_org_id)
|
||||
dry_run: if True, validates payload without stamping
|
||||
|
||||
Returns:
|
||||
dict: {processed: int, stamped: int, failed: int, details: [...]}
|
||||
@@ -92,7 +91,7 @@ def process_queue(conn, horux_api_url, api_key):
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute("""
|
||||
SELECT id, sale_id, type, xml_unsigned, retry_count
|
||||
SELECT id, sale_id, type, payload_unsigned, retry_count
|
||||
FROM cfdi_queue
|
||||
WHERE status IN ('pending', 'failed')
|
||||
AND retry_count < %s
|
||||
@@ -103,7 +102,12 @@ def process_queue(conn, horux_api_url, api_key):
|
||||
|
||||
results = {'processed': 0, 'stamped': 0, 'failed': 0, 'details': []}
|
||||
|
||||
for cfdi_id, sale_id, cfdi_type, xml_unsigned, retry_count in items:
|
||||
api_key = tenant_config.get('facturapi_key')
|
||||
if not api_key:
|
||||
cur.close()
|
||||
raise ValueError("Facturapi key not configured for tenant")
|
||||
|
||||
for cfdi_id, sale_id, cfdi_type, payload_unsigned, retry_count in items:
|
||||
results['processed'] += 1
|
||||
|
||||
# Update status to 'sending'
|
||||
@@ -113,54 +117,47 @@ def process_queue(conn, horux_api_url, api_key):
|
||||
conn.commit()
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
f'{horux_api_url}/api/nexus/cfdi/stamp',
|
||||
headers={
|
||||
'Authorization': f'Bearer {api_key}',
|
||||
'Content-Type': 'application/xml',
|
||||
},
|
||||
data=xml_unsigned.encode('utf-8'),
|
||||
timeout=30,
|
||||
)
|
||||
payload = json.loads(payload_unsigned or '{}')
|
||||
if not payload:
|
||||
raise ValueError("Empty payload in queue item")
|
||||
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
uuid_fiscal = data.get('uuid')
|
||||
xml_signed = data.get('xml', '')
|
||||
if dry_run:
|
||||
# TODO: Facturapi dry-run validation (not officially supported)
|
||||
# For now we just skip the API call and mark as stamped with a fake UUID
|
||||
raise ValueError("dry_run is not supported with Facturapi")
|
||||
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue
|
||||
SET status = 'stamped',
|
||||
xml_signed = %s,
|
||||
uuid_fiscal = %s,
|
||||
stamped_at = NOW(),
|
||||
error_message = NULL
|
||||
WHERE id = %s
|
||||
""", (xml_signed, uuid_fiscal, cfdi_id))
|
||||
conn.commit()
|
||||
invoice = facturapi_service.create_invoice(tenant_config, payload)
|
||||
invoice_id = invoice.get('id')
|
||||
uuid_fiscal = invoice.get('uuid')
|
||||
|
||||
results['stamped'] += 1
|
||||
results['details'].append({
|
||||
'id': cfdi_id, 'status': 'stamped', 'uuid': uuid_fiscal
|
||||
})
|
||||
else:
|
||||
error_msg = f'HTTP {response.status_code}: {response.text[:500]}'
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue
|
||||
SET status = 'failed',
|
||||
retry_count = retry_count + 1,
|
||||
error_message = %s
|
||||
WHERE id = %s
|
||||
""", (error_msg, cfdi_id))
|
||||
conn.commit()
|
||||
# Download signed XML for storage
|
||||
try:
|
||||
xml_signed = facturapi_service.download_xml(tenant_config, invoice_id)
|
||||
xml_signed_str = xml_signed.decode('utf-8') if isinstance(xml_signed, bytes) else str(xml_signed)
|
||||
except Exception as xml_err:
|
||||
logger.warning("Could not download signed XML for %s: %s", invoice_id, xml_err)
|
||||
xml_signed_str = ''
|
||||
|
||||
results['failed'] += 1
|
||||
results['details'].append({
|
||||
'id': cfdi_id, 'status': 'failed', 'error': error_msg
|
||||
})
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue
|
||||
SET status = 'stamped',
|
||||
xml_signed = %s,
|
||||
uuid_fiscal = %s,
|
||||
external_id = %s,
|
||||
stamped_at = NOW(),
|
||||
error_message = NULL
|
||||
WHERE id = %s
|
||||
""", (xml_signed_str, uuid_fiscal, invoice_id, cfdi_id))
|
||||
conn.commit()
|
||||
|
||||
except requests.RequestException as e:
|
||||
error_msg = f'Connection error: {str(e)[:500]}'
|
||||
results['stamped'] += 1
|
||||
results['details'].append({
|
||||
'id': cfdi_id, 'status': 'stamped',
|
||||
'uuid': uuid_fiscal, 'external_id': invoice_id,
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f'{type(e).__name__}: {str(e)[:500]}'
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue
|
||||
SET status = 'failed',
|
||||
@@ -180,20 +177,13 @@ def process_queue(conn, horux_api_url, api_key):
|
||||
|
||||
|
||||
def retry_failed(conn):
|
||||
"""Find failed items eligible for retry (based on backoff) and reset to pending.
|
||||
"""Find failed items eligible for retry and reset to pending.
|
||||
|
||||
Uses exponential backoff: item is eligible for retry only if enough
|
||||
time has passed since the last attempt based on retry_count.
|
||||
|
||||
Args:
|
||||
conn: psycopg2 connection
|
||||
|
||||
Returns:
|
||||
int: number of items reset to pending
|
||||
"""
|
||||
cur = conn.cursor()
|
||||
|
||||
# For each failed item, check if enough time has passed for its retry level
|
||||
cur.execute("""
|
||||
SELECT id, retry_count, created_at
|
||||
FROM cfdi_queue
|
||||
@@ -206,15 +196,15 @@ def retry_failed(conn):
|
||||
now = datetime.utcnow()
|
||||
|
||||
for cfdi_id, retry_count, created_at in items:
|
||||
# Calculate required wait time based on retry count
|
||||
if retry_count < len(BACKOFF_INTERVALS):
|
||||
wait_seconds = BACKOFF_INTERVALS[retry_count]
|
||||
else:
|
||||
wait_seconds = BACKOFF_INTERVALS[-1] # max backoff
|
||||
wait_seconds = BACKOFF_INTERVALS[-1]
|
||||
|
||||
# Check if enough time has passed (use created_at as approximation)
|
||||
# In production, you'd track last_attempt_at separately
|
||||
if True: # Always eligible for manual retry trigger
|
||||
# Use created_at as approximation for last attempt.
|
||||
# In production, track last_attempt_at separately.
|
||||
elapsed = (now - created_at).total_seconds()
|
||||
if elapsed >= wait_seconds:
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue SET status = 'pending' WHERE id = %s
|
||||
""", (cfdi_id,))
|
||||
@@ -226,8 +216,8 @@ def retry_failed(conn):
|
||||
|
||||
|
||||
def cancel_cfdi(conn, cfdi_id, motive, replacement_uuid=None,
|
||||
horux_api_url=None, api_key=None):
|
||||
"""Cancel a stamped CFDI via Horux API.
|
||||
tenant_config=None):
|
||||
"""Cancel a stamped CFDI via Facturapi.
|
||||
|
||||
SAT cancellation motives:
|
||||
01: Comprobante emitido con errores con relacion (requires replacement UUID)
|
||||
@@ -240,8 +230,7 @@ def cancel_cfdi(conn, cfdi_id, motive, replacement_uuid=None,
|
||||
cfdi_id: int (cfdi_queue.id)
|
||||
motive: str ('01', '02', '03', '04')
|
||||
replacement_uuid: str (required if motive == '01')
|
||||
horux_api_url: str (optional, skips API call if None — for offline)
|
||||
api_key: str (optional)
|
||||
tenant_config: dict with facturapi_key
|
||||
|
||||
Returns:
|
||||
dict: {id, status, message}
|
||||
@@ -258,13 +247,13 @@ def cancel_cfdi(conn, cfdi_id, motive, replacement_uuid=None,
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute("""
|
||||
SELECT id, uuid_fiscal, status FROM cfdi_queue WHERE id = %s
|
||||
SELECT id, uuid_fiscal, external_id, status FROM cfdi_queue WHERE id = %s
|
||||
""", (cfdi_id,))
|
||||
row = cur.fetchone()
|
||||
if not row:
|
||||
raise ValueError(f"CFDI queue item {cfdi_id} not found")
|
||||
|
||||
_, uuid_fiscal, current_status = row
|
||||
_, uuid_fiscal, external_id, current_status = row
|
||||
|
||||
if current_status == 'cancelled':
|
||||
raise ValueError("CFDI is already cancelled")
|
||||
@@ -280,64 +269,26 @@ def cancel_cfdi(conn, cfdi_id, motive, replacement_uuid=None,
|
||||
cur.close()
|
||||
return {'id': cfdi_id, 'status': 'cancelled', 'message': 'Cancelled locally (was not stamped)'}
|
||||
|
||||
# Send cancel request to Horux
|
||||
if horux_api_url and api_key:
|
||||
try:
|
||||
payload = {
|
||||
'uuid': uuid_fiscal,
|
||||
'motive': motive,
|
||||
}
|
||||
if replacement_uuid:
|
||||
payload['replacement_uuid'] = replacement_uuid
|
||||
if not tenant_config or not tenant_config.get('facturapi_key'):
|
||||
cur.close()
|
||||
raise ValueError("Facturapi key not configured for tenant")
|
||||
|
||||
response = requests.post(
|
||||
f'{horux_api_url}/api/nexus/cfdi/cancel',
|
||||
headers={
|
||||
'Authorization': f'Bearer {api_key}',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
json=payload,
|
||||
timeout=30,
|
||||
)
|
||||
if not external_id:
|
||||
cur.close()
|
||||
raise ValueError("Cannot cancel: no Facturapi invoice id stored")
|
||||
|
||||
if response.status_code == 200:
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue
|
||||
SET status = 'cancelled',
|
||||
cancel_motive = %s,
|
||||
cancel_replacement_uuid = %s,
|
||||
error_message = NULL
|
||||
WHERE id = %s
|
||||
""", (motive, replacement_uuid, cfdi_id))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
return {
|
||||
'id': cfdi_id,
|
||||
'status': 'cancelled',
|
||||
'message': f'Cancelled with SAT (motive {motive})',
|
||||
}
|
||||
else:
|
||||
error_msg = f'Cancel failed: HTTP {response.status_code}: {response.text[:500]}'
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue
|
||||
SET error_message = %s
|
||||
WHERE id = %s
|
||||
""", (error_msg, cfdi_id))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
raise ValueError(error_msg)
|
||||
try:
|
||||
facturapi_service.cancel_invoice(
|
||||
tenant_config, external_id, motive,
|
||||
replacement_uuid=replacement_uuid,
|
||||
)
|
||||
|
||||
except requests.RequestException as e:
|
||||
cur.close()
|
||||
raise ValueError(f'Connection error during cancel: {str(e)}')
|
||||
else:
|
||||
# Offline mode: mark as cancelled locally, will sync later
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue
|
||||
SET status = 'cancelled',
|
||||
cancel_motive = %s,
|
||||
cancel_replacement_uuid = %s,
|
||||
error_message = 'Cancelled offline, pending SAT sync'
|
||||
error_message = NULL
|
||||
WHERE id = %s
|
||||
""", (motive, replacement_uuid, cfdi_id))
|
||||
conn.commit()
|
||||
@@ -345,24 +296,23 @@ def cancel_cfdi(conn, cfdi_id, motive, replacement_uuid=None,
|
||||
return {
|
||||
'id': cfdi_id,
|
||||
'status': 'cancelled',
|
||||
'message': 'Cancelled offline, pending SAT sync',
|
||||
'message': f'Cancelled with SAT (motive {motive})',
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
error_msg = f'Cancel failed: {str(e)[:500]}'
|
||||
cur.execute("""
|
||||
UPDATE cfdi_queue
|
||||
SET error_message = %s
|
||||
WHERE id = %s
|
||||
""", (error_msg, cfdi_id))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
raise ValueError(error_msg)
|
||||
|
||||
|
||||
def get_queue_status(conn, filters=None):
|
||||
"""Get CFDI queue items with optional filters.
|
||||
|
||||
Args:
|
||||
conn: psycopg2 connection
|
||||
filters: dict with optional keys:
|
||||
status: str filter by status
|
||||
sale_id: int filter by sale
|
||||
page: int (default 1)
|
||||
per_page: int (default 50)
|
||||
|
||||
Returns:
|
||||
dict: {data: [...], pagination: {...}}
|
||||
"""
|
||||
"""Get CFDI queue items with optional filters."""
|
||||
filters = filters or {}
|
||||
cur = conn.cursor()
|
||||
|
||||
@@ -392,7 +342,7 @@ def get_queue_status(conn, filters=None):
|
||||
cur.execute(f"""
|
||||
SELECT q.id, q.sale_id, q.type, q.uuid_fiscal, q.status,
|
||||
q.retry_count, q.provisional_folio, q.error_message,
|
||||
q.cancel_motive, q.created_at, q.stamped_at
|
||||
q.cancel_motive, q.created_at, q.stamped_at, q.external_id
|
||||
FROM cfdi_queue q
|
||||
WHERE {where}
|
||||
ORDER BY q.created_at DESC
|
||||
@@ -408,6 +358,7 @@ def get_queue_status(conn, filters=None):
|
||||
'error_message': r[7], 'cancel_motive': r[8],
|
||||
'created_at': str(r[9]) if r[9] else None,
|
||||
'stamped_at': str(r[10]) if r[10] else None,
|
||||
'external_id': r[11],
|
||||
})
|
||||
|
||||
cur.close()
|
||||
|
||||
329
pos/services/facturapi_service.py
Normal file
329
pos/services/facturapi_service.py
Normal file
@@ -0,0 +1,329 @@
|
||||
# /home/Autopartes/pos/services/facturapi_service.py
|
||||
"""Facturapi integration for Nexus POS.
|
||||
|
||||
Uses Facturapi REST API directly (requests + Basic Auth) so it is safe for
|
||||
multi-tenant use. Each call receives the API key explicitly, avoiding the
|
||||
global client used by the official facturapi Python library.
|
||||
|
||||
Authentication modes:
|
||||
1. User key (FACTURAPI_USER_KEY env): creates/verifies organizations per tenant.
|
||||
2. Secret key per tenant (tenant_config.facturapi_secret_key): uses existing org.
|
||||
|
||||
Reference: https://docs.facturapi.io/
|
||||
"""
|
||||
|
||||
import os
|
||||
import base64
|
||||
import logging
|
||||
from decimal import Decimal
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
BASE_URL = "https://www.facturapi.io/v2"
|
||||
USER_KEY = os.environ.get("FACTURAPI_USER_KEY", "")
|
||||
|
||||
|
||||
class FacturapiError(Exception):
|
||||
def __init__(self, message: str, status_code: int = 0, response_body: str = ""):
|
||||
super().__init__(message)
|
||||
self.status_code = status_code
|
||||
self.response_body = response_body
|
||||
|
||||
|
||||
# ─── HTTP helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
def _request(method: str, endpoint: str, api_key: str, json_payload=None, params=None,
|
||||
extra_headers=None, timeout=60):
|
||||
"""Make a request to Facturapi REST API with Basic Auth."""
|
||||
url = f"{BASE_URL}{endpoint}"
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if extra_headers:
|
||||
headers.update(extra_headers)
|
||||
|
||||
try:
|
||||
resp = requests.request(
|
||||
method,
|
||||
url,
|
||||
auth=(api_key, ""),
|
||||
headers=headers,
|
||||
json=json_payload,
|
||||
params=params,
|
||||
timeout=timeout,
|
||||
)
|
||||
except requests.RequestException as e:
|
||||
raise FacturapiError(f"Connection error: {e}", status_code=0)
|
||||
|
||||
if not resp.ok:
|
||||
raise FacturapiError(
|
||||
f"Facturapi {method.upper()} {endpoint} failed: {resp.status_code} {resp.text[:500]}",
|
||||
status_code=resp.status_code,
|
||||
response_body=resp.text,
|
||||
)
|
||||
|
||||
if resp.status_code == 204 or not resp.content:
|
||||
return {}
|
||||
return resp.json()
|
||||
|
||||
|
||||
def _download(method: str, endpoint: str, api_key: str, params=None, timeout=60) -> bytes:
|
||||
"""Download binary content (XML/PDF)."""
|
||||
url = f"{BASE_URL}{endpoint}"
|
||||
resp = requests.request(
|
||||
method,
|
||||
url,
|
||||
auth=(api_key, ""),
|
||||
params=params,
|
||||
timeout=timeout,
|
||||
)
|
||||
if not resp.ok:
|
||||
raise FacturapiError(
|
||||
f"Download failed: {resp.status_code} {resp.text[:500]}",
|
||||
status_code=resp.status_code,
|
||||
)
|
||||
return resp.content
|
||||
|
||||
|
||||
# ─── Tenant config helpers ──────────────────────────────────────────────────
|
||||
|
||||
def _get_secret_key(tenant_config: dict) -> Optional[str]:
|
||||
for key in ("facturapi_key", "facturapi_secret_key"):
|
||||
val = (tenant_config.get(key) or "").strip()
|
||||
if val:
|
||||
return val
|
||||
return None
|
||||
|
||||
|
||||
def _get_user_key() -> Optional[str]:
|
||||
return USER_KEY.strip() or None
|
||||
|
||||
|
||||
def _is_user_key_mode(tenant_config: dict) -> bool:
|
||||
return bool(_get_user_key()) and not _get_secret_key(tenant_config)
|
||||
|
||||
|
||||
def get_api_key(tenant_config: dict) -> str:
|
||||
"""Resolve the API key to use for a tenant.
|
||||
|
||||
Priority:
|
||||
1. tenant_config.facturapi_secret_key (manual override)
|
||||
2. FACTURAPI_USER_KEY env (auto-org mode)
|
||||
"""
|
||||
secret = _get_secret_key(tenant_config)
|
||||
if secret:
|
||||
return secret
|
||||
user = _get_user_key()
|
||||
if user:
|
||||
return user
|
||||
raise FacturapiError(
|
||||
"Facturapi not configured. Set FACTURAPI_USER_KEY env or tenant_config.facturapi_secret_key"
|
||||
)
|
||||
|
||||
|
||||
# ─── Organizations ──────────────────────────────────────────────────────────
|
||||
|
||||
def create_organization(tenant_config: dict) -> dict:
|
||||
"""Create a new Facturapi organization for the tenant.
|
||||
|
||||
Requires FACTURAPI_USER_KEY.
|
||||
Returns dict with id, api_key.
|
||||
"""
|
||||
user_key = _get_user_key()
|
||||
if not user_key:
|
||||
raise FacturapiError("FACTURAPI_USER_KEY is required to create organizations")
|
||||
|
||||
payload = {"name": tenant_config.get("razon_social", tenant_config.get("name", "Nexus"))}
|
||||
legal = tenant_config.get("legal_name") or tenant_config.get("razon_social")
|
||||
if legal:
|
||||
payload["legal"] = {"name": legal}
|
||||
if tenant_config.get("rfc"):
|
||||
payload["legal"] = payload.get("legal", {})
|
||||
payload["legal"]["tax_id"] = tenant_config["rfc"]
|
||||
|
||||
org = _request("POST", "/organizations", user_key, json_payload=payload)
|
||||
org_id = org.get("id")
|
||||
|
||||
# Generate live secret key
|
||||
key_resp = _request("PUT", f"/organizations/{org_id}/apikeys/live", user_key, json_payload={})
|
||||
live_key = key_resp.get("key") if isinstance(key_resp, dict) else str(key_resp)
|
||||
if not live_key:
|
||||
raise FacturapiError(f"Could not generate live key for org {org_id}")
|
||||
|
||||
return {"org_id": org_id, "api_key": live_key}
|
||||
|
||||
|
||||
def get_organization(org_id: str, api_key: str) -> dict:
|
||||
return _request("GET", f"/organizations/{org_id}", api_key)
|
||||
|
||||
|
||||
def upload_csd(tenant_config: dict, cer_b64: str, key_b64: str, password: str) -> dict:
|
||||
"""Upload CSD (Certificado de Sello Digital) to Facturapi.
|
||||
|
||||
cer_b64 and key_b64 are base64-encoded strings.
|
||||
"""
|
||||
api_key = get_api_key(tenant_config)
|
||||
org_id = tenant_config.get("facturapi_org_id")
|
||||
if not org_id:
|
||||
raise FacturapiError("No Facturapi organization configured for tenant")
|
||||
|
||||
cer_bytes = base64.b64decode(cer_b64)
|
||||
key_bytes = base64.b64decode(key_b64)
|
||||
|
||||
url = f"{BASE_URL}/organizations/{org_id}/certificate"
|
||||
files = {
|
||||
"certificate": ("certificate.cer", cer_bytes, "application/octet-stream"),
|
||||
"private_key": ("private_key.key", key_bytes, "application/octet-stream"),
|
||||
"secret": (None, password),
|
||||
}
|
||||
resp = requests.post(url, auth=(api_key, ""), files=files, timeout=60)
|
||||
if not resp.ok:
|
||||
raise FacturapiError(
|
||||
f"CSD upload failed: {resp.status_code} {resp.text[:500]}",
|
||||
status_code=resp.status_code,
|
||||
)
|
||||
return resp.json()
|
||||
|
||||
|
||||
def get_org_status(tenant_config: dict) -> dict:
|
||||
"""Return organization status: configured, has_csd, org_id."""
|
||||
try:
|
||||
api_key = get_api_key(tenant_config)
|
||||
except FacturapiError:
|
||||
return {"configured": False, "has_csd": False, "org_id": None}
|
||||
|
||||
org_id = tenant_config.get("facturapi_org_id")
|
||||
if not org_id:
|
||||
return {"configured": False, "has_csd": False, "org_id": None}
|
||||
|
||||
try:
|
||||
org = get_organization(org_id, api_key)
|
||||
return {
|
||||
"configured": True,
|
||||
"org_id": org_id,
|
||||
"has_csd": bool(org.get("certificate", {}).get("has_certificate")),
|
||||
"legal_name": org.get("legal", {}).get("name"),
|
||||
"tax_id": org.get("legal", {}).get("tax_id"),
|
||||
}
|
||||
except FacturapiError:
|
||||
return {"configured": False, "has_csd": False, "org_id": org_id}
|
||||
|
||||
|
||||
# ─── Customers ──────────────────────────────────────────────────────────────
|
||||
|
||||
def create_or_update_customer(tenant_config: dict, customer_data: dict) -> str:
|
||||
"""Create or update a customer in Facturapi and return its id.
|
||||
|
||||
customer_data: {
|
||||
legal_name: str,
|
||||
tax_id: str,
|
||||
tax_system: str,
|
||||
email: str,
|
||||
zip: str,
|
||||
country: str (optional, ISO 3166 alpha-3),
|
||||
}
|
||||
"""
|
||||
api_key = get_api_key(tenant_config)
|
||||
tax_id = (customer_data.get("tax_id") or "").upper().strip()
|
||||
if not tax_id:
|
||||
raise FacturapiError("Customer tax_id is required")
|
||||
|
||||
# Try to find existing customer
|
||||
existing_id = None
|
||||
try:
|
||||
result = _request("GET", "/customers", api_key, params={"search": tax_id})
|
||||
for c in result.get("data", []):
|
||||
if (c.get("tax_id") or "").upper() == tax_id:
|
||||
existing_id = c.get("id")
|
||||
break
|
||||
except FacturapiError as e:
|
||||
logger.warning("Failed to search Facturapi customer: %s", e)
|
||||
|
||||
is_foreign = bool(customer_data.get("country")) and customer_data["country"] != "MEX"
|
||||
|
||||
payload = {
|
||||
"legal_name": customer_data.get("legal_name", ""),
|
||||
"email": customer_data.get("email"),
|
||||
"address": {
|
||||
"zip": customer_data.get("zip", "00000"),
|
||||
},
|
||||
}
|
||||
if is_foreign:
|
||||
payload["tax_id"] = tax_id
|
||||
payload["address"]["country"] = customer_data["country"]
|
||||
else:
|
||||
payload["tax_id"] = tax_id
|
||||
if customer_data.get("tax_system"):
|
||||
payload["tax_system"] = customer_data["tax_system"]
|
||||
|
||||
if existing_id:
|
||||
_request("PUT", f"/customers/{existing_id}", api_key, json_payload=payload)
|
||||
return existing_id
|
||||
|
||||
new_customer = _request("POST", "/customers", api_key, json_payload=payload)
|
||||
return new_customer.get("id")
|
||||
|
||||
|
||||
# ─── Invoices ───────────────────────────────────────────────────────────────
|
||||
|
||||
def create_invoice(tenant_config: dict, payload: dict) -> dict:
|
||||
"""Create and stamp an invoice in Facturapi.
|
||||
|
||||
Returns the Facturapi invoice object.
|
||||
"""
|
||||
api_key = get_api_key(tenant_config)
|
||||
return _request("POST", "/invoices", api_key, json_payload=payload, timeout=90)
|
||||
|
||||
|
||||
def cancel_invoice(tenant_config: dict, invoice_id: str, motive: str,
|
||||
replacement_uuid: Optional[str] = None) -> dict:
|
||||
"""Cancel an invoice in Facturapi.
|
||||
|
||||
Motive codes:
|
||||
01: errores con relacion (requires replacement_uuid)
|
||||
02: errores sin relacion
|
||||
03: no se llevo a cabo la operacion
|
||||
04: operacion nominativa relacionada en factura global
|
||||
"""
|
||||
api_key = get_api_key(tenant_config)
|
||||
params = {"motive": motive}
|
||||
if replacement_uuid:
|
||||
params["replacement"] = replacement_uuid
|
||||
return _request("DELETE", f"/invoices/{invoice_id}", api_key, params=params, timeout=60)
|
||||
|
||||
|
||||
def download_xml(tenant_config: dict, invoice_id: str) -> bytes:
|
||||
api_key = get_api_key(tenant_config)
|
||||
return _download("GET", f"/invoices/{invoice_id}/xml", api_key)
|
||||
|
||||
|
||||
def download_pdf(tenant_config: dict, invoice_id: str) -> bytes:
|
||||
api_key = get_api_key(tenant_config)
|
||||
return _download("GET", f"/invoices/{invoice_id}/pdf", api_key)
|
||||
|
||||
|
||||
# ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
def is_lco_rejection(message: str) -> bool:
|
||||
"""Detect SAT LCO rejection (CSD not yet propagated)."""
|
||||
if not message:
|
||||
return False
|
||||
msg = message.lower()
|
||||
return any(
|
||||
pattern in msg
|
||||
for pattern in [
|
||||
"lco",
|
||||
"no se encontro el rfc",
|
||||
"rfc no registrado",
|
||||
"lista de contribuyentes obligados",
|
||||
"csd no registrado",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def to_cents(amount) -> int:
|
||||
"""Convert Decimal/float/None to integer cents for Facturapi."""
|
||||
if amount is None:
|
||||
return 0
|
||||
return int(Decimal(str(amount)).quantize(Decimal("0.01")) * 100)
|
||||
@@ -8,7 +8,7 @@ monthly CFDI with InformacionGlobal per SAT requirements.
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
from services.cfdi_builder import build_global_invoice_xml
|
||||
from services.cfdi_facturapi_builder import build_global_invoice_payload
|
||||
from services.cfdi_queue import enqueue_cfdi, _generate_provisional_folio
|
||||
|
||||
|
||||
@@ -137,10 +137,10 @@ def generate_global_invoice(conn, tenant_config, year, month, branch_id=None,
|
||||
return {'error': 'NO_ELIGIBLE_SALES',
|
||||
'message': f'No hay ventas elegibles para factura global de {month:02d}/{year}'}
|
||||
|
||||
xml = build_global_invoice_xml(sales, tenant_config, year, month)
|
||||
payload = build_global_invoice_payload(sales, tenant_config, year, month)
|
||||
|
||||
# Enqueue with sale_id=NULL (global invoice)
|
||||
result = enqueue_cfdi(conn, None, 'ingreso', xml)
|
||||
result = enqueue_cfdi(conn, None, 'ingreso', payload)
|
||||
cfdi_id = result['id']
|
||||
|
||||
cur = conn.cursor()
|
||||
@@ -167,7 +167,7 @@ def generate_global_invoice(conn, tenant_config, year, month, branch_id=None,
|
||||
'sales_count': len(sales),
|
||||
'total': sum(s['total'] for s in sales),
|
||||
'provisional_folio': result['provisional_folio'],
|
||||
'xml': xml,
|
||||
'payload': payload,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user