Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/xhandler/xsender/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class _Model(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True)


class _Answer(BaseModel):
"""An answer of the core: ignores the fields a newer core adds."""

model_config = ConfigDict(extra="ignore", frozen=True)


class URLs(_Model):
"""The SUNAT services where a company sends its documents."""

Expand Down Expand Up @@ -128,19 +134,22 @@ def __str__(self) -> str:
return self.value


class CDR(_Model):
class CDR(_Answer):
"""The receipt SUNAT gives back (Constancia de Recepcion)."""

zip: Bytes
response_code: str
description: str
notes: list[str]
document_description: str | None = None
"""For an accepted guía de remisión, the URL its printed copy's QR code must
encode; None for other documents (and with a core older than 6.0.0-alpha.2)."""
status: Status
status_code: str | None = None
"""The ticket status, only when verifying a ticket."""


class Fault(_Model):
class Fault(_Answer):
"""An error answered by SUNAT."""

code: str
Expand All @@ -154,14 +163,14 @@ class Fault(_Model):
status: Status


class NoCDR(_Model):
class NoCDR(_Answer):
"""A ticket status without CDR, e.g. "98" (in progress)."""

status_code: str
status: Status


class SendResult(_Model):
class SendResult(_Answer):
"""The answer to a sent file: a CDR, a ticket to verify later (summaries,
voided documents, packs, guias) or a fault."""

Expand All @@ -173,15 +182,15 @@ class SendResult(_Model):
"""Where the ticket is verified."""


class VerifyResult(_Model):
class VerifyResult(_Answer):
"""The answer to a ticket: a CDR, no CDR yet (e.g. in progress) or a fault."""

cdr: CDR | None = None
no_cdr: NoCDR | None = None
fault: Fault | None = None


class ConsultStatus(_Model):
class ConsultStatus(_Answer):
"""e.g. "0001" (accepted) for get_status, or "0" for validate_cdp."""

status_code: str
Expand All @@ -190,7 +199,7 @@ class ConsultStatus(_Model):
"""The CDR of get_status_cdr."""


class ConsultResult(_Model):
class ConsultResult(_Answer):
"""The answer of the consult and validation services."""

status: ConsultStatus | None = None
Expand Down
20 changes: 20 additions & 0 deletions tests/test_xsender.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from xhandler import ErrorKind, XhandlerError
from xhandler.xsender import (
BILL_CONSULT_SERVICE_URL,
CDR,
ConsultDocument,
Credentials,
FileSender,
Expand Down Expand Up @@ -65,13 +66,32 @@ def test_send_file_reads_the_cdr() -> None:
assert result.cdr is not None
assert result.cdr.status is Status.ACCEPTED
assert result.cdr.zip
assert result.cdr.document_description is None
assert result.send_target.url == URLs.beta().invoice
assert result.verify_ticket_target is None
request = sunat.requests[0]
assert request.headers["SOAPAction"] == "urn:sendBill"
assert str(request.url) == URLs.beta().invoice


def test_cdrs_carry_the_document_description() -> None:
"""A newer core may add fields: the answers keep reading"""
cdr = CDR.model_validate(
{
"zip": "",
"response_code": "0",
"description": "La Guia numero T001-1, ha sido aceptada",
"notes": [],
"document_description": "https://e-factura.sunat.gob.pe/v1/contribuyente/gre/comprobantes/descargaqr?hashqr=abc",
"status": "ACEPTADO",
"added_by_a_newer_core": True,
}
)
assert cdr.document_description == (
"https://e-factura.sunat.gob.pe/v1/contribuyente/gre/comprobantes/descargaqr?hashqr=abc"
)


def test_send_file_reads_faults() -> None:
"""SUNAT answers SOAP faults with HTTP 500"""
sunat = Sunat(lambda _: answer("send_file_rejected"))
Expand Down
Loading