Skip to content

fix(fiscal): plazo de baja, motivos de nota de crédito y una sola nota total (H1/H2/H4) - #502

Merged
PapiCuche merged 4 commits into
masterfrom
fix/fiscal-annul-guards
Dec 6, 2026
Merged

PapiCuche merged 4 commits into
masterfrom
fix/fiscal-annul-guards

Conversation

@PapiCuche

Copy link
Copy Markdown
Owner

Qué corrige (PR-1 de anulaciones, ADR #500)

Tres fallos del código que ya está en producción. Ninguno causó daño: todavía no se ha emitido ninguna nota ni ninguna baja real. Todo falla cerrado mientras las preguntas al contador sigan abiertas.

  • H1, plazo de la baja. Antes los 7 días se contaban desde que el sistema registró la aceptación de SUNAT. Ahora valen los dos plazos y manda el más estricto:

    • emisión + 7 días, la validación 2957 de SUNAT;
    • aceptación de la CDR + 7 días, el artículo 14.1.b.

    Las fechas se toman en hora de Lima y el mensaje de rechazo dice qué plazo aplicó. Lo mismo vale para las notas que se dan de baja.

  • H2, motivos de la nota de crédito. La nota siempre revierte el total, así que sólo se aceptan los motivos 01, 02 y 06. Los demás se rechazan antes de reservar el número. El 13 trae una explicación adicional: sólo aplica a ventas al crédito. Una nota antigua con otro motivo no se firma.

  • H4, tope. Sólo puede existir una nota de crédito viva por comprobante. El control corre dentro de la transacción, con bloqueo de la fila del original y antes de reservar el número. La misma clave sigue devolviendo la misma nota. Una nota rechazada, o dada de baja con una baja aceptada, libera el original.

  • H3, entrega al cliente. Queda documentado en el código. La corrección es el PR-4.

Pruebas

  • store.test_fiscal_annul_guards: 21 pruebas nuevas, que fallaron antes del cambio. Incluyen límites de 7 y 8 días en hora de Lima y concurrencia en PostgreSQL: dos claves distintas producen una sola nota y un solo número.
  • 42 módulos fiscales, contables, de devoluciones, cancelaciones y correo: 1227 pruebas en verde.
  • Clases fiscales de tests.py: 465 pruebas en verde.
  • Cambié una prueba existente, que afirmaba que dos notas de crédito totales sobre el mismo original eran válidas.
  • Sin migraciones.

Cambio de comportamiento

order_document_copy usa la misma lista de motivos. Una factura con una nota de crédito viva de motivo 02 ahora se trata como anulada y no se envía como comprobante válido.

El despliegue necesita un GO propio.

🤖 Generated with Claude Code

PapiCuche and others added 4 commits December 5, 2026 18:44
H1: a Comunicacion de Baja is allowed only while both seven-day windows
hold - issue date in Lima (SUNAT validation 2957) and acceptance date
(art. 14.1.b) - the stricter applies and the refusal names it; also at
send time and for notes voided by RA.
H2: the full-reversal credit note accepts only Catalogo 09 reasons 01,
02 and 06, refused before a correlative is reserved and again at signing.
H4: at most one live full credit note per original, checked under the
original's row lock before reserving a number; same key stays idempotent.
H3: document that the void gate relies on the not-granted attestation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Credit notes require a request_key (view 400 and service), so a note
  left GENERATED stays resumable by replaying its key.
- A GENERATED credit note that can never be signed no longer blocks its
  original: a legacy reason outside 01/02/06, or a keyless one older than
  30 minutes. Its number stays spent and unsent; the accountant decides.
  Signing such a note is refused once another full credit note lives.
- Idempotency checks the request: the same key with another note type,
  reason or debit amount raises FiscalNoteConflict (HTTP 409).
- Note creation re-checks the original's eligibility under its row lock;
  create_void_communication takes the same row locks and re-checks, and
  a void is refused while a live full credit note exists, so a note and
  a void on one invoice serialise and only one is born.
- Comment why the CDR clock is defensive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ard weights

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A keyed credit note left GENERATED after a failed signing, whose key the
client lost, blocked further credit notes and voids on the invoice.
H4 now raises FullCreditNoteExists naming the live note. A pending,
unsigned note answers 409 credit_note_pending_signature with its id,
number, request_key and reason, so the client resumes it explicitly
with that key; it is never signed inside a request that brought another
key. A signed or later note keeps the 400 refusal, now with its id and
number. The void refusal names the pending note the same way, and note
payloads expose request_key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@PapiCuche
PapiCuche merged commit 6558fb5 into master Dec 6, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant