@12-apps/fiscal 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +26 -0
  2. package/dist/{chunk-RRFKYVLZ.js → chunk-GGYJPFBJ.js} +2 -2
  3. package/dist/{chunk-27B4HNO6.js → chunk-GKT3X5CF.js} +38 -2
  4. package/dist/chunk-GKT3X5CF.js.map +1 -0
  5. package/dist/{chunk-X6MRDUYH.js → chunk-H33BK623.js} +1 -1
  6. package/dist/{chunk-X6MRDUYH.js.map → chunk-H33BK623.js.map} +1 -1
  7. package/dist/{chunk-FOFIN5M7.js → chunk-JVQKJNOC.js} +1 -1
  8. package/dist/{chunk-FOFIN5M7.js.map → chunk-JVQKJNOC.js.map} +1 -1
  9. package/dist/{chunk-HQ2NXSTX.js → chunk-MEBG7FGG.js} +2 -2
  10. package/dist/{chunk-ALABGVPO.js → chunk-MGTROA7K.js} +2 -2
  11. package/dist/{chunk-V5ZQAAJT.js → chunk-V6G2OFLA.js} +8 -5
  12. package/dist/chunk-V6G2OFLA.js.map +1 -0
  13. package/dist/{context-ChnWR4Kb.d.ts → context-yc1C2__K.d.ts} +1 -1
  14. package/dist/danfe/index.js +2 -2
  15. package/dist/documents/index.d.ts +25 -7
  16. package/dist/documents/index.js +60 -3
  17. package/dist/documents/index.js.map +1 -1
  18. package/dist/http.d.ts +6 -3
  19. package/dist/http.js +4 -4
  20. package/dist/issuer/index.d.ts +2 -2
  21. package/dist/issuer/index.js +1 -1
  22. package/dist/jobs.d.ts +3 -3
  23. package/dist/jobs.js +3 -3
  24. package/dist/manifest/server.d.ts +3 -3
  25. package/dist/manifest/server.js +6 -6
  26. package/dist/notifications.d.ts +2 -2
  27. package/dist/notifications.js +2 -2
  28. package/dist/{port-DpRFK56U.d.ts → port-B1-ipIL4.d.ts} +1 -1
  29. package/dist/sale/index.d.ts +28 -2
  30. package/dist/{service-KTjH7AxW.d.ts → service-BB9lWdyg.d.ts} +30 -3
  31. package/dist/sidecar/index.d.ts +3 -1
  32. package/dist/sidecar/index.js +1 -1
  33. package/package.json +1 -1
  34. package/sidecar/README.md +13 -0
  35. package/sidecar/schema/sale-for-document.schema.json +21 -0
  36. package/sidecar/src/Nfe/DocumentMapper.php +46 -1
  37. package/src/documents/context.ts +1 -1
  38. package/src/documents/errors.ts +14 -1
  39. package/src/documents/helpers.ts +49 -0
  40. package/src/documents/index.ts +3 -2
  41. package/src/documents/service.ts +98 -1
  42. package/src/http.ts +3 -0
  43. package/src/sale/types.ts +27 -1
  44. package/src/sidecar/types.ts +3 -1
  45. package/dist/chunk-27B4HNO6.js.map +0 -1
  46. package/dist/chunk-V5ZQAAJT.js.map +0 -1
  47. /package/dist/{chunk-RRFKYVLZ.js.map → chunk-GGYJPFBJ.js.map} +0 -0
  48. /package/dist/{chunk-HQ2NXSTX.js.map → chunk-MEBG7FGG.js.map} +0 -0
  49. /package/dist/{chunk-ALABGVPO.js.map → chunk-MGTROA7K.js.map} +0 -0
@@ -5,7 +5,7 @@ import {
5
5
  parseIssuingPointInput,
6
6
  parseProfileInput,
7
7
  viewOf
8
- } from "../chunk-RRFKYVLZ.js";
8
+ } from "../chunk-GGYJPFBJ.js";
9
9
  import {
10
10
  element
11
11
  } from "../chunk-PNTKG3DZ.js";
@@ -19,18 +19,20 @@ import {
19
19
  FiscalContext,
20
20
  MAX_FAILURES,
21
21
  assertConfig,
22
+ assertDraftFits,
22
23
  assertReason,
23
24
  canTransition,
24
25
  computeAccessKey,
25
26
  draftOf,
26
27
  drawKeyCode,
28
+ isValidAccessKey,
27
29
  issuerOf,
28
30
  localIso,
29
31
  scrubSecrets,
30
32
  totalsOf,
31
33
  transition,
32
34
  xmlKeys
33
- } from "../chunk-27B4HNO6.js";
35
+ } from "../chunk-GKT3X5CF.js";
34
36
  import {
35
37
  FISCAL_DOCUMENT_MODELS,
36
38
  FISCAL_DOCUMENT_STATUSES,
@@ -42,7 +44,7 @@ import {
42
44
  } from "../chunk-3SDXLSG4.js";
43
45
  import {
44
46
  FiscalDocumentError
45
- } from "../chunk-X6MRDUYH.js";
47
+ } from "../chunk-H33BK623.js";
46
48
  import {
47
49
  __name
48
50
  } from "../chunk-7QVYU63E.js";
@@ -400,6 +402,7 @@ async function issue(deps, input) {
400
402
  if (existing !== null) return existing;
401
403
  const signing = await deps.ctx.material(profile);
402
404
  const draft = await deps.config.sales.saleFor({ ownerId: input.ownerId, subjectId: input.subjectId, model: input.model });
405
+ assertDraftFits(draft, input.model);
403
406
  const numbered = await numberOrExisting(
404
407
  deps,
405
408
  input,
@@ -415,6 +418,7 @@ async function issueOffline(deps, input) {
415
418
  if (existing !== null) return existing;
416
419
  const signing = await deps.ctx.material(profile);
417
420
  const draft = await deps.config.sales.saleFor({ ownerId: input.ownerId, subjectId: input.subjectId, model: "NFCE" });
421
+ assertDraftFits(draft, "NFCE");
418
422
  const numbered = await numberOrExisting(
419
423
  deps,
420
424
  target,
@@ -434,6 +438,7 @@ async function resendContingency(deps, input) {
434
438
  }
435
439
  const profile = await deps.ctx.requireProfile(input.ownerId);
436
440
  const draft = await deps.config.sales.saleFor({ ownerId: input.ownerId, subjectId: document.subjectId, model: document.model });
441
+ assertDraftFits(draft, document.model);
437
442
  const totals = totalsOf(draft);
438
443
  const buyerTaxId = draft.buyer?.document.number ?? null;
439
444
  if (printedFieldsChange(document, totals, buyerTaxId)) {
@@ -477,6 +482,51 @@ async function cancel(deps, input) {
477
482
  return { document: cancelled, failure: null };
478
483
  }
479
484
  __name(cancel, "cancel");
485
+ var LATE_CANCEL_CODES = ["501"];
486
+ var isLateCancel = /* @__PURE__ */ __name((code) => code !== null && LATE_CANCEL_CODES.includes(code), "isLateCancel");
487
+ async function liveReturnOf(deps, document) {
488
+ const returns = (await deps.config.store.listBySubject(document.ownerId, document.subjectId, "NFE")).filter(isLive);
489
+ const quoting = returns.filter((candidate) => returnsDocument(candidate, document));
490
+ return quoting[quoting.length - 1] ?? null;
491
+ }
492
+ __name(liveReturnOf, "liveReturnOf");
493
+ async function cancelOrReturn(deps, input) {
494
+ assertReason(input.reason, "reason");
495
+ const document = await requireDocument(deps, input.ownerId, input.documentId);
496
+ if (document.status === "CANCELLED") return { outcome: "CANCELLED", document, returnDocument: null, failure: null };
497
+ const existing = document.accessKey === null ? null : await liveReturnOf(deps, document);
498
+ if (existing !== null) return { outcome: "RETURNED", document, returnDocument: existing, failure: null };
499
+ const cancelled = await cancel(deps, input);
500
+ if (cancelled.failure === null) return { outcome: "CANCELLED", document: cancelled.document, returnDocument: null, failure: null };
501
+ const late = cancelled.failure.class === "REJECTED" && isLateCancel(cancelled.failure.code);
502
+ if (!late) return { outcome: "REFUSED", document: cancelled.document, returnDocument: null, failure: cancelled.failure };
503
+ return { outcome: "RETURNED", document: cancelled.document, returnDocument: await issueReturn(deps, document, input.returnPointId), failure: cancelled.failure };
504
+ }
505
+ __name(cancelOrReturn, "cancelOrReturn");
506
+ function returnsDocument(candidate, document) {
507
+ const quotes = (candidate.sale.referencedDocuments ?? []).some((reference) => reference.accessKey === document.accessKey);
508
+ return candidate.model === "NFE" && candidate.sale.purpose === "RETURN" && quotes;
509
+ }
510
+ __name(returnsDocument, "returnsDocument");
511
+ async function issueReturn(deps, document, returnPointId) {
512
+ if (document.model !== "NFCE") {
513
+ throw new FiscalDocumentError("RETURN_OF_NFCE_ONLY", `document ${document.id} is an ${document.model}: only an NFC-e is answered with a buyer's return.`);
514
+ }
515
+ const live = (await deps.config.store.listBySubject(document.ownerId, document.subjectId, "NFE")).filter(isLive);
516
+ if (live.length > 0) {
517
+ throw new FiscalDocumentError("SUBJECT_HAS_DOCUMENT", `subject ${document.subjectId} already holds a live NF-e that is not document ${document.id}'s return.`);
518
+ }
519
+ const draft = await deps.config.sales.saleFor({ ownerId: document.ownerId, subjectId: document.subjectId, model: "NFE" });
520
+ if (!returnsDocument({ model: "NFE", sale: draft }, document)) {
521
+ throw new FiscalDocumentError("INVALID_INPUT", `the return the sale source answered for subject ${document.subjectId} does not quote document ${document.id}.`);
522
+ }
523
+ const returned = await issue(deps, { ownerId: document.ownerId, subjectId: document.subjectId, issuingPointId: returnPointId, model: "NFE" });
524
+ if (returned.id === document.id || !returnsDocument(returned, document)) {
525
+ throw new FiscalDocumentError("CONFLICT", `subject ${document.subjectId}'s live NF-e is not document ${document.id}'s return.`);
526
+ }
527
+ return returned;
528
+ }
529
+ __name(issueReturn, "issueReturn");
480
530
  function lookupMoves(document) {
481
531
  if (!canTransition(document.status, "LOOKUP")) return false;
482
532
  if (document.status === "REPLACED" || document.status === "REJECTED") return document.numberVoid === "LOOKUP_FIRST";
@@ -565,6 +615,11 @@ function createFiscalDocuments(config) {
565
615
  resendContingency: resendContingency.bind(null, deps),
566
616
  /** Cancel an authorised document. A refusal (late, say) answers the issuer's failure and changes nothing. */
567
617
  cancel: cancel.bind(null, deps),
618
+ /**
619
+ * Cancel an authorised document, or — when SEFAZ says the cancel is too late
620
+ * (501) — issue the buyer's return NF-e of entry quoting it. Idempotent.
621
+ */
622
+ cancelOrReturn: cancelOrReturn.bind(null, deps),
568
623
  /** Ask SEFAZ what it knows of the key; a state the answer moves is recorded. */
569
624
  lookup: lookup.bind(null, deps),
570
625
  /** A person takes a document back from `attention`; the sweep tries it again. */
@@ -1201,6 +1256,7 @@ export {
1201
1256
  FISCAL_DOCUMENT_STATUSES,
1202
1257
  FISCAL_NEXT_NUMBER_SQL,
1203
1258
  FiscalDocumentError,
1259
+ LATE_CANCEL_CODES,
1204
1260
  MAX_FAILURES,
1205
1261
  OWED_VOIDS,
1206
1262
  SIGNING_IDENTITY_KINDS,
@@ -1216,6 +1272,7 @@ export {
1216
1272
  draftOf,
1217
1273
  drawKeyCode,
1218
1274
  isLive,
1275
+ isValidAccessKey,
1219
1276
  localIso,
1220
1277
  parseCertificateUpload,
1221
1278
  parseIssuingPointInput,