@12-apps/fiscal 1.0.0 → 1.2.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 (140) hide show
  1. package/README.md +164 -7
  2. package/dist/chunk-2LBQRYPV.js +21 -0
  3. package/dist/chunk-2LBQRYPV.js.map +1 -0
  4. package/dist/chunk-3SDXLSG4.js +24 -0
  5. package/dist/chunk-3SDXLSG4.js.map +1 -0
  6. package/dist/chunk-5S2PJHAE.js +85 -0
  7. package/dist/chunk-5S2PJHAE.js.map +1 -0
  8. package/dist/chunk-G3V7IYNF.js +192 -0
  9. package/dist/chunk-G3V7IYNF.js.map +1 -0
  10. package/dist/chunk-JVQKJNOC.js +14 -0
  11. package/dist/chunk-JVQKJNOC.js.map +1 -0
  12. package/dist/chunk-N5DCKYFR.js +610 -0
  13. package/dist/chunk-N5DCKYFR.js.map +1 -0
  14. package/dist/chunk-NVRYAAQ4.js +212 -0
  15. package/dist/chunk-NVRYAAQ4.js.map +1 -0
  16. package/dist/chunk-PNTKG3DZ.js +47 -0
  17. package/dist/chunk-PNTKG3DZ.js.map +1 -0
  18. package/dist/chunk-WTGASIZE.js +247 -0
  19. package/dist/chunk-WTGASIZE.js.map +1 -0
  20. package/dist/chunk-XQGW5HUG.js +30 -0
  21. package/dist/chunk-XQGW5HUG.js.map +1 -0
  22. package/dist/context-ChnWR4Kb.d.ts +455 -0
  23. package/dist/danfe/en-US.d.ts +12 -0
  24. package/dist/danfe/en-US.js +51 -0
  25. package/dist/danfe/en-US.js.map +1 -0
  26. package/dist/danfe/index.d.ts +50 -0
  27. package/dist/danfe/index.js +18 -0
  28. package/dist/danfe/index.js.map +1 -0
  29. package/dist/danfe/pt-BR.d.ts +11 -0
  30. package/dist/danfe/pt-BR.js +51 -0
  31. package/dist/danfe/pt-BR.js.map +1 -0
  32. package/dist/documents/index.d.ts +181 -0
  33. package/dist/documents/index.js +1234 -0
  34. package/dist/documents/index.js.map +1 -0
  35. package/dist/engine/index.d.ts +3 -246
  36. package/dist/http.d.ts +75 -0
  37. package/dist/http.js +16 -0
  38. package/dist/http.js.map +1 -0
  39. package/dist/issuer/index.d.ts +43 -0
  40. package/dist/issuer/index.js +131 -0
  41. package/dist/issuer/index.js.map +1 -0
  42. package/dist/jobs.d.ts +74 -0
  43. package/dist/jobs.js +22 -0
  44. package/dist/jobs.js.map +1 -0
  45. package/dist/labels-BNvflT0h.d.ts +44 -0
  46. package/dist/manifest/index.d.ts +30 -16
  47. package/dist/manifest/index.js +11 -17
  48. package/dist/manifest/index.js.map +1 -1
  49. package/dist/manifest/server.d.ts +57 -0
  50. package/dist/manifest/server.js +25 -0
  51. package/dist/manifest/server.js.map +1 -0
  52. package/dist/notifications.d.ts +93 -0
  53. package/dist/notifications.js +41 -0
  54. package/dist/notifications.js.map +1 -0
  55. package/dist/port-DpRFK56U.d.ts +238 -0
  56. package/dist/sale/index.d.ts +269 -0
  57. package/dist/sale/index.js +1 -0
  58. package/dist/sale/index.js.map +1 -0
  59. package/dist/service-KTjH7AxW.d.ts +139 -0
  60. package/dist/sidecar/index.d.ts +218 -0
  61. package/dist/sidecar/index.js +8 -0
  62. package/dist/sidecar/index.js.map +1 -0
  63. package/dist/types-B1b8LIWX.d.ts +247 -0
  64. package/package.json +63 -5
  65. package/prisma/fiscal.prisma +158 -0
  66. package/prisma/migrations/20261113220000_fiscal_documents/migration.sql +201 -0
  67. package/sidecar/.dockerignore +5 -0
  68. package/sidecar/Dockerfile +68 -0
  69. package/sidecar/README.md +162 -0
  70. package/sidecar/composer.json +30 -0
  71. package/sidecar/composer.lock +2224 -0
  72. package/sidecar/docker/apache.conf +14 -0
  73. package/sidecar/docker/icp-brasil.pem +8031 -0
  74. package/sidecar/docker/mpm.conf +17 -0
  75. package/sidecar/docker/openssl.cnf +16 -0
  76. package/sidecar/docker/php.ini +19 -0
  77. package/sidecar/public/index.php +15 -0
  78. package/sidecar/schema/sale-for-document.schema.json +422 -0
  79. package/sidecar/src/ErrorClass.php +31 -0
  80. package/sidecar/src/Http/Request.php +48 -0
  81. package/sidecar/src/Http/Response.php +32 -0
  82. package/sidecar/src/Http/Router.php +59 -0
  83. package/sidecar/src/Kernel.php +99 -0
  84. package/sidecar/src/Module.php +31 -0
  85. package/sidecar/src/Nfe/Classifier.php +89 -0
  86. package/sidecar/src/Nfe/Credentials.php +52 -0
  87. package/sidecar/src/Nfe/DocumentMapper.php +428 -0
  88. package/sidecar/src/Nfe/DocumentSchema.php +39 -0
  89. package/sidecar/src/Nfe/Input.php +125 -0
  90. package/sidecar/src/Nfe/Model.php +17 -0
  91. package/sidecar/src/Nfe/NfeModule.php +62 -0
  92. package/sidecar/src/Nfe/NfeService.php +504 -0
  93. package/sidecar/src/Nfe/Sefaz.php +49 -0
  94. package/sidecar/src/Nfe/SefazReply.php +81 -0
  95. package/sidecar/src/Nfe/Transport/CurlTransport.php +198 -0
  96. package/sidecar/src/Nfe/Transport/SoapAdapter.php +57 -0
  97. package/sidecar/src/Nfe/Transport/Transport.php +20 -0
  98. package/sidecar/src/Nfe/Transport/TransportFailure.php +42 -0
  99. package/sidecar/src/RequestLog.php +54 -0
  100. package/sidecar/src/ServiceError.php +69 -0
  101. package/src/danfe/en-US.ts +53 -0
  102. package/src/danfe/index.ts +11 -0
  103. package/src/danfe/labels.ts +49 -0
  104. package/src/danfe/pt-BR.ts +52 -0
  105. package/src/danfe/render.ts +265 -0
  106. package/src/danfe/xml.ts +45 -0
  107. package/src/documents/context.ts +260 -0
  108. package/src/documents/errors.ts +48 -0
  109. package/src/documents/helpers.ts +177 -0
  110. package/src/documents/index.ts +40 -0
  111. package/src/documents/lines.ts +63 -0
  112. package/src/documents/machine-steps.ts +260 -0
  113. package/src/documents/machine.ts +128 -0
  114. package/src/documents/memory.ts +138 -0
  115. package/src/documents/nfe-proc.ts +51 -0
  116. package/src/documents/ports.ts +130 -0
  117. package/src/documents/profile-input.ts +168 -0
  118. package/src/documents/profile.ts +98 -0
  119. package/src/documents/service-profile.ts +104 -0
  120. package/src/documents/service.ts +299 -0
  121. package/src/documents/settle.ts +121 -0
  122. package/src/documents/signing.ts +61 -0
  123. package/src/documents/sql-rows.ts +180 -0
  124. package/src/documents/sql-store.ts +242 -0
  125. package/src/documents/sql.ts +18 -0
  126. package/src/documents/steps.ts +173 -0
  127. package/src/documents/tests/integration/pglite.ts +73 -0
  128. package/src/documents/types.ts +193 -0
  129. package/src/http.ts +266 -0
  130. package/src/issuer/index.ts +9 -0
  131. package/src/issuer/port.ts +109 -0
  132. package/src/issuer/sidecar.ts +171 -0
  133. package/src/jobs.ts +115 -0
  134. package/src/manifest/index.ts +25 -16
  135. package/src/manifest/server.ts +20 -0
  136. package/src/notifications.ts +120 -0
  137. package/src/sale/index.ts +8 -0
  138. package/src/sale/types.ts +318 -0
  139. package/src/sidecar/index.ts +9 -0
  140. package/src/sidecar/types.ts +245 -0
@@ -0,0 +1,181 @@
1
+ import { F as FiscalDocumentModel, j as SeriesKind, h as FiscalDocument, i as FiscalDocumentEvent } from '../port-DpRFK56U.js';
2
+ export { D as DocumentEventKind, k as DocumentTotals, l as FISCAL_DOCUMENT_MODELS, n as FISCAL_DOCUMENT_STATUSES, g as FiscalDocumentStatus, N as NumberVoid, O as OWED_VOIDS, o as isLive } from '../port-DpRFK56U.js';
3
+ import { h as SaleDraft, F as FiscalStore } from '../context-ChnWR4Kb.js';
4
+ export { C as Cipher, i as ContingencyBlockSource, j as DOCUMENT_POLICIES, k as DOCUMENT_TRANSITIONS, l as DocumentInput, f as DocumentPolicy, D as DocumentStorage, E as EmitterProfile, m as EmitterProfileView, n as FINAL_STATUSES, b as FISCAL_CERTIFICATE_EXPIRING, c as FISCAL_CONTINGENCY_DEADLINE, d as FISCAL_DOCUMENT_NEEDS_ATTENTION, e as FISCAL_DOCUMENT_REJECTED, g as FiscalDocumentsConfig, a as FiscalNotifyPort, o as FiscalReasons, I as IssuingPoint, M as MAX_FAILURES, N as NumberRequest, p as SIGNING_IDENTITY_KINDS, q as SaleSource, r as SigningIdentities, s as SigningIdentity, S as SigningIdentityKind, T as Transition, t as canTransition, u as createPlatformIdentity, v as createSigningIdentities, w as createStoreCertificateIdentity, x as scrubSecrets, y as transition, z as viewOf } from '../context-ChnWR4Kb.js';
5
+ export { C as CONTINGENCY_WARNING_AFTER_MS, a as CertificateFacts, b as CertificateUpload, E as EmitterProfileInput, F as FiscalDocuments, c as FiscalDocumentsServiceConfig, I as IssueInput, d as IssuingPointInput, S as SweepReport, e as createFiscalDocuments, p as parseCertificateUpload, f as parseIssuingPointInput, g as parseProfileInput } from '../service-KTjH7AxW.js';
6
+ import { a as TaxBreakdown } from '../types-B1b8LIWX.js';
7
+ import { SaleForDocument, SaleLine } from '../sale/index.js';
8
+ import '../sidecar/index.js';
9
+
10
+ /**
11
+ * Every refusal the document module makes, as a typed error with a `code`.
12
+ * A message never carries a certificate, a password or a CSC: the module
13
+ * never holds one long enough to quote it.
14
+ */
15
+ type FiscalDocumentErrorCode =
16
+ /** The state machine has no such transition from the document's state. */
17
+ "TRANSITION_REFUSED"
18
+ /** An input is missing, malformed or out of range. */
19
+ | "INVALID_INPUT"
20
+ /** No emitter profile, issuing point or document under that id. */
21
+ | "NOT_FOUND"
22
+ /** The profile does not let the package issue (`documentPolicy`), or lacks what issuing needs. */
23
+ | "NOT_ISSUABLE"
24
+ /** The signing identity is declared but not built yet (`PLATFORM`). */
25
+ | "NOT_SUPPORTED"
26
+ /** A series' counter passed 999 999 999. */
27
+ | "SERIES_EXHAUSTED"
28
+ /** Another worker advanced the document first; re-read it. */
29
+ | "CONFLICT"
30
+ /** The subject already has a live document of that model (the per-subject guard). */
31
+ | "SUBJECT_HAS_DOCUMENT"
32
+ /** The document's state has no paper to print (only AUTHORIZED, or CONTINGENCY awaiting transmission). */
33
+ | "NOT_PRINTABLE"
34
+ /**
35
+ * A resend of a rejected contingency NFC-e would change what the buyer's
36
+ * DANFE prints and its QR encodes (the total, the buyer's document): such a
37
+ * sale needs a new note, issued by a person.
38
+ */
39
+ | "RESEND_CHANGES_PRINTED_FIELDS"
40
+ /** A `RETURN` quotes no document (`referencedDocuments` is absent or empty). */
41
+ | "RETURN_NEEDS_REFERENCE"
42
+ /**
43
+ * A `RETURN` asked of an NFC-e. An NFC-e has no return purpose: the return is
44
+ * an NF-e (model 55) of entry that references the NFC-e's key.
45
+ */
46
+ | "RETURN_ON_NFCE";
47
+ declare class FiscalDocumentError extends Error {
48
+ readonly code: FiscalDocumentErrorCode;
49
+ constructor(code: FiscalDocumentErrorCode, message: string);
50
+ }
51
+
52
+ /**
53
+ * The pure helpers the document service builds a document with: the local
54
+ * clock, the key code, the access key, the issuer block, the totals, the
55
+ * storage keys. No port, no state.
56
+ */
57
+
58
+ /** `2026-11-20T12:30:00-03:00`: the instant as the owner's wall clock. */
59
+ declare function localIso(at: Date, offset: string): string;
60
+ /** `cNF`: eight random digits, never equal to the number's own eight (the layout's rule). */
61
+ declare function drawKeyCode(number: number): string;
62
+ /** A stored sale back to the draft it was built from (for a second document of the same sale). */
63
+ declare function draftOf(sale: SaleForDocument): SaleDraft;
64
+ /** Storage keys, by access key: one folder per document, whatever its owner. */
65
+ declare const xmlKeys: {
66
+ signed: (accessKey: string) => string;
67
+ /** One signed XML per digest: every XML a failed call returned under a key is kept, none overwritten. */
68
+ signedByDigest: (accessKey: string, digest: string) => string;
69
+ authorized: (accessKey: string) => string;
70
+ cancel: (accessKey: string) => string;
71
+ inutilization: (document: FiscalDocument) => string;
72
+ };
73
+ /**
74
+ * Whether `key` is an access key as the layout writes one: 44 characters, the
75
+ * CNPJ inside it alphanumeric or not, and its modulo-11 check digit right.
76
+ */
77
+ declare function isValidAccessKey(key: string): boolean;
78
+ /**
79
+ * The 44-character access key a document will carry, computed as the layout
80
+ * does: cUF, AAMM of `dhEmi`, CNPJ, model, series, number, `tpEmis` (1 online,
81
+ * 9 offline NFC-e), `cNF` and the check digit. The package knows a document's
82
+ * key before any call, so an unanswered send can always be looked up.
83
+ */
84
+ declare function computeAccessKey(input: {
85
+ readonly sale: Pick<SaleForDocument, "issuer" | "issuedAt" | "series" | "number" | "keyCode">;
86
+ readonly model: FiscalDocumentModel;
87
+ readonly seriesKind: SeriesKind;
88
+ }): string;
89
+
90
+ /**
91
+ * The reference {@link FiscalStore}: in memory, for tests and harnesses —
92
+ * the role `createMemory*Store` plays in the packages that own storage seams.
93
+ * It keeps the same promises as the SQL store (`./sql-store`): a draw and the
94
+ * insert built from it commit together (a `build` that throws leaves the
95
+ * counter untouched), a replaced document is advanced before its replacement
96
+ * is inserted, and a subject has at most one live document per model.
97
+ */
98
+
99
+ interface MemoryFiscalStore extends FiscalStore {
100
+ /** Every event, in order, for assertions. */
101
+ readonly events: FiscalDocumentEvent[];
102
+ /** The next number each counter would hand out, keyed `cnpj|environment|model|series`. */
103
+ readonly counters: Map<string, number>;
104
+ }
105
+ declare function createMemoryFiscalStore(): MemoryFiscalStore;
106
+
107
+ /**
108
+ * The number draw, as one SQL statement over the package's migration
109
+ * (`fiscal_number_counters`). A host's `FiscalStore.createDocument` runs it
110
+ * and the document INSERT in ONE transaction: the row lock this UPDATE takes
111
+ * serialises concurrent draws on a series, and a failed insert rolls the
112
+ * counter back with it, so a series has no gap a failed write made. The only
113
+ * gaps left are numbers SEFAZ never used (a rejection, a replaced document),
114
+ * which the sweep inutilizes.
115
+ *
116
+ * Parameters: `$1` the issuer's CNPJ, `$2` environment, `$3` model, `$4` series,
117
+ * `$5` issuing point id. No row comes back when the series is not that
118
+ * point's; an exhausted series fails the counter's CHECK (at most
119
+ * 1 000 000 000, the next number after 999 999 999), and the transaction with it.
120
+ */
121
+ declare const FISCAL_NEXT_NUMBER_SQL = "UPDATE \"fiscal_number_counters\"\nSET \"next_number\" = \"next_number\" + 1, \"updated_at\" = CURRENT_TIMESTAMP\nWHERE \"issuer_cnpj\" = $1 AND \"environment\" = $2 AND \"model\" = $3 AND \"series\" = $4 AND \"issuing_point_id\" = $5\nRETURNING \"next_number\" - 1 AS \"number\"";
122
+
123
+ /**
124
+ * `createPrismaFiscalStore` — the package's own {@link FiscalStore}, over the
125
+ * host's Prisma client and the package's migration. A host passes its client
126
+ * and nothing else: the numbering invariants live here, not in each host.
127
+ *
128
+ * - The draw and the insert are ONE transaction (`FISCAL_NEXT_NUMBER_SQL`):
129
+ * the counter's row lock serialises concurrent draws, and a refused insert
130
+ * gives the number back.
131
+ * - Going offline replaces the online document in that same transaction,
132
+ * before the insert, so a crash leaves exactly one live document.
133
+ * - The per-subject guard is the partial unique index
134
+ * `fiscal_documents_live_subject_key`: a second live document is refused as
135
+ * `SUBJECT_HAS_DOCUMENT`, and nothing is drawn.
136
+ * - A transition is saved only over the row it started from (`updated_at` and
137
+ * `status`), with its event, in one transaction.
138
+ *
139
+ * It speaks raw SQL through the client's `$queryRawUnsafe` /
140
+ * `$executeRawUnsafe` / `$transaction`, so it needs no generated model types
141
+ * and works on the host's client whatever else its schema holds.
142
+ */
143
+
144
+ /** The two raw-SQL methods of a Prisma client (or of its transaction client). */
145
+ interface FiscalSqlClient {
146
+ $queryRawUnsafe<T = unknown>(query: string, ...values: unknown[]): Promise<T>;
147
+ $executeRawUnsafe(query: string, ...values: unknown[]): Promise<number>;
148
+ }
149
+ /** A Prisma client: the raw-SQL methods and an interactive transaction. */
150
+ interface FiscalPrismaClient extends FiscalSqlClient {
151
+ $transaction<T>(run: (tx: FiscalSqlClient) => Promise<T>): Promise<T>;
152
+ }
153
+ declare function createPrismaFiscalStore(config: {
154
+ readonly prisma: FiscalPrismaClient;
155
+ }): FiscalStore;
156
+
157
+ /**
158
+ * The document mapping's tax half: the engine's breakdown (`computeTax`)
159
+ * onto the `SaleForDocument` lines a host's `SaleSource` answers. The engine
160
+ * decides; this writes what it decided:
161
+ *
162
+ * - one document line per breakdown line, in the breakdown's order — so an
163
+ * exclusion the engine segregated (the service charge passed to the staff
164
+ * at most 15% of the food and drink, class 410019; a platform's retained
165
+ * intermediation, 410020) IS its own item, with its class and no tax
166
+ * (RCBS art. 399 §2), and a service charge the engine did not exclude is a
167
+ * taxed item under the class the host passed;
168
+ * - the IBS/CBS group from the line's figures (`vBC`, the nominal rates, the
169
+ * reduction, the amounts), or no group at all where the engine answered
170
+ * `NONE` (MEI, não contribuinte).
171
+ *
172
+ * What the engine does not know — the product code, NCM, CFOP, unit, the
173
+ * ICMS, PIS and COFINS — arrives per line id from the host. A breakdown line
174
+ * with no details is refused, never invented.
175
+ */
176
+
177
+ /** Everything a document line needs that the breakdown does not carry. */
178
+ type LineDetails = Omit<SaleLine, "id" | "quantity" | "valueCents" | "ibsCbs">;
179
+ declare function documentLines(breakdown: TaxBreakdown, details: Readonly<Record<string, LineDetails>>): SaleLine[];
180
+
181
+ export { FISCAL_NEXT_NUMBER_SQL, FiscalDocument, FiscalDocumentError, type FiscalDocumentErrorCode, FiscalDocumentEvent, FiscalDocumentModel, type FiscalPrismaClient, type FiscalSqlClient, FiscalStore, type LineDetails, type MemoryFiscalStore, SaleDraft, SeriesKind, computeAccessKey, createMemoryFiscalStore, createPrismaFiscalStore, documentLines, draftOf, drawKeyCode, isValidAccessKey, localIso, xmlKeys };