@12-apps/fiscal 1.5.0 → 1.7.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.
- package/README.md +46 -4
- package/dist/{chunk-HMTPJWXM.js → chunk-2JVTHJAU.js} +2 -2
- package/dist/{chunk-3E3NBXSW.js → chunk-CEK2GDRY.js} +5 -3
- package/dist/chunk-CEK2GDRY.js.map +1 -0
- package/dist/{chunk-MGTROA7K.js → chunk-OAQSGDZ7.js} +11 -7
- package/dist/chunk-OAQSGDZ7.js.map +1 -0
- package/dist/{chunk-ZKWVKCGJ.js → chunk-P5EDTNWE.js} +1 -1
- package/dist/{chunk-ZKWVKCGJ.js.map → chunk-P5EDTNWE.js.map} +1 -1
- package/dist/{chunk-G6QNU4FD.js → chunk-P7CRGZGO.js} +2 -2
- package/dist/{chunk-H33BK623.js → chunk-WRYCYXRA.js} +1 -1
- package/dist/{chunk-H33BK623.js.map → chunk-WRYCYXRA.js.map} +1 -1
- package/dist/{chunk-TJCUO6D6.js → chunk-YHSO3476.js} +6 -5
- package/dist/chunk-YHSO3476.js.map +1 -0
- package/dist/{context-CwZiHLnY.d.ts → context-D71ETH--.d.ts} +155 -19
- package/dist/danfe/index.d.ts +7 -0
- package/dist/danfe/index.js +2 -2
- package/dist/documents/index.d.ts +157 -36
- package/dist/documents/index.js +1104 -305
- package/dist/documents/index.js.map +1 -1
- package/dist/engine/index.d.ts +2 -2
- package/dist/http.d.ts +6 -5
- package/dist/http.js +4 -4
- package/dist/issuer/index.d.ts +10 -3
- package/dist/issuer/index.js +19 -4
- package/dist/issuer/index.js.map +1 -1
- package/dist/jobs.d.ts +5 -5
- package/dist/jobs.js +3 -3
- package/dist/manifest/index.d.ts +6 -4
- package/dist/manifest/index.js.map +1 -1
- package/dist/manifest/server.d.ts +5 -5
- package/dist/manifest/server.js +6 -6
- package/dist/notifications.d.ts +4 -4
- package/dist/notifications.js +2 -2
- package/dist/sale/index.d.ts +17 -4
- package/dist/sale/index.js.map +1 -1
- package/dist/{service-Cz2O84Xr.d.ts → service-DyA64Jc6.d.ts} +64 -5
- package/dist/sidecar/index.d.ts +38 -249
- package/dist/sidecar/index.js +30 -2
- package/dist/sidecar/index.js.map +1 -1
- package/dist/{types-DSYwap42.d.ts → types-6OiBrmZy.d.ts} +2 -2
- package/dist/{types-B1b8LIWX.d.ts → types-BLdkww6G.d.ts} +1 -1
- package/dist/types-BbTxBzsv.d.ts +252 -0
- package/package.json +2 -1
- package/prisma/fiscal.prisma +50 -1
- package/prisma/migrations/20261118180000_fiscal_contingency_blocks/migration.sql +64 -0
- package/sidecar/README.md +70 -10
- package/sidecar/bin/check-platform.php +29 -0
- package/sidecar/extensions.json +12 -0
- package/sidecar/schema/sale-for-document.schema.json +4 -0
- package/sidecar/src/Http/Request.php +6 -1
- package/sidecar/src/Kernel.php +39 -1
- package/sidecar/src/Nfe/DocumentMapper.php +27 -22
- package/sidecar/src/Platform.php +84 -0
- package/src/danfe/render.ts +16 -5
- package/src/documents/contingency-gaps.ts +88 -0
- package/src/documents/contingency-pass.ts +157 -0
- package/src/documents/contingency-xml.ts +136 -0
- package/src/documents/contingency.ts +257 -0
- package/src/documents/errors.ts +8 -1
- package/src/documents/helpers.ts +3 -0
- package/src/documents/index.ts +23 -1
- package/src/documents/lines.ts +6 -2
- package/src/documents/memory-blocks.ts +118 -0
- package/src/documents/memory.ts +6 -2
- package/src/documents/ports.ts +163 -13
- package/src/documents/service.ts +10 -0
- package/src/documents/sql-blocks.ts +202 -0
- package/src/documents/sql-store.ts +4 -3
- package/src/http.ts +1 -0
- package/src/issuer/sidecar.ts +27 -3
- package/src/manifest/index.ts +6 -4
- package/src/sale/types.ts +17 -4
- package/src/sidecar/index.ts +2 -0
- package/src/sidecar/platform.ts +55 -0
- package/src/sidecar/types.ts +1 -3
- package/dist/chunk-3E3NBXSW.js.map +0 -1
- package/dist/chunk-MGTROA7K.js.map +0 -1
- package/dist/chunk-TJCUO6D6.js.map +0 -1
- /package/dist/{chunk-HMTPJWXM.js.map → chunk-2JVTHJAU.js.map} +0 -0
- /package/dist/{chunk-G6QNU4FD.js.map → chunk-P7CRGZGO.js.map} +0 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which numbers of a contingency block are KNOWN unused and which are
|
|
3
|
+
* UNKNOWN — pure, from the block's state (FUT-4717). A holder signs its block
|
|
4
|
+
* in order and hands its notes in, in order, so:
|
|
5
|
+
*
|
|
6
|
+
* - below the highest number it handed in, a number with no document was
|
|
7
|
+
* never signed: KNOWN;
|
|
8
|
+
* - a CLOSED block's numbers above what the holder said it used are KNOWN;
|
|
9
|
+
* between the highest handed in and `usedUpTo` they were signed and are
|
|
10
|
+
* still to come: UNKNOWN until they arrive;
|
|
11
|
+
* - an ABANDONED block (its holder is gone) above the highest handed in may
|
|
12
|
+
* be on paper a buyer holds: UNKNOWN, flagged, never voided;
|
|
13
|
+
* - an OPEN block above the highest handed in is still in use: neither.
|
|
14
|
+
*
|
|
15
|
+
* A range already voided is neither.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ContingencyBlockState, ContingencyGap, NumberRange } from "./ports";
|
|
19
|
+
|
|
20
|
+
/** Consecutive numbers as ranges, ascending. */
|
|
21
|
+
function rangesOf(numbers: readonly number[]): NumberRange[] {
|
|
22
|
+
const sorted = [...new Set(numbers)].sort((a, b) => a - b);
|
|
23
|
+
const ranges: NumberRange[] = [];
|
|
24
|
+
for (const number of sorted) {
|
|
25
|
+
const last = ranges[ranges.length - 1];
|
|
26
|
+
if (last !== undefined && last.to === number - 1) ranges[ranges.length - 1] = { from: last.from, to: number };
|
|
27
|
+
else ranges.push({ from: number, to: number });
|
|
28
|
+
}
|
|
29
|
+
return ranges;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function between(from: number, to: number, keep: (n: number) => boolean): number[] {
|
|
33
|
+
const out: number[] = [];
|
|
34
|
+
for (let n = from; n <= to; n += 1) if (keep(n)) out.push(n);
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The highest number of the block a document was recorded on (`from - 1`: none). */
|
|
39
|
+
function highestRecorded(block: ContingencyBlockState): number {
|
|
40
|
+
const inside = block.documented.filter((n) => n >= block.from && n <= block.to);
|
|
41
|
+
return inside.length === 0 ? block.from - 1 : Math.max(...inside);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface BlockAccount {
|
|
45
|
+
readonly known: NumberRange[];
|
|
46
|
+
readonly unknown: NumberRange[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The block's known and unknown ranges (see the module doc). */
|
|
50
|
+
export function accountFor(block: ContingencyBlockState): BlockAccount {
|
|
51
|
+
const documented = new Set(block.documented);
|
|
52
|
+
const voided = (n: number) => block.voided.some((range) => n >= range.from && n <= range.to);
|
|
53
|
+
const open = (n: number) => !documented.has(n) && !voided(n);
|
|
54
|
+
const highest = highestRecorded(block);
|
|
55
|
+
const known = between(block.from, highest, open);
|
|
56
|
+
let unknown: number[] = [];
|
|
57
|
+
if (block.fate === "CLOSED") {
|
|
58
|
+
const used = Math.max(highest, block.usedUpTo ?? block.from - 1);
|
|
59
|
+
known.push(...between(used + 1, block.to, open));
|
|
60
|
+
unknown = between(highest + 1, used, open);
|
|
61
|
+
} else if (block.fate === "ABANDONED") {
|
|
62
|
+
unknown = between(highest + 1, block.to, open);
|
|
63
|
+
}
|
|
64
|
+
return { known: rangesOf(known), unknown: rangesOf(unknown) };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A range of a block, as the gap the service reports. */
|
|
68
|
+
export function gapOf(block: ContingencyBlockState, range: NumberRange): ContingencyGap {
|
|
69
|
+
return {
|
|
70
|
+
blockId: block.id,
|
|
71
|
+
ownerId: block.ownerId,
|
|
72
|
+
issuingPointId: block.issuingPointId,
|
|
73
|
+
issuerCnpj: block.issuerCnpj,
|
|
74
|
+
environment: block.environment,
|
|
75
|
+
series: block.series,
|
|
76
|
+
holder: block.holder,
|
|
77
|
+
reservedAt: block.reservedAt,
|
|
78
|
+
from: range.from,
|
|
79
|
+
to: range.to,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Whether every number is a document or voided, with nothing left to come: the block needs no more work. */
|
|
84
|
+
export function isSettled(block: ContingencyBlockState): boolean {
|
|
85
|
+
if (block.fate === "OPEN") return false;
|
|
86
|
+
const { known, unknown } = accountFor(block);
|
|
87
|
+
return known.length === 0 && unknown.length === 0;
|
|
88
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gap pass over contingency blocks (FUT-4717): every block read in keyset
|
|
3
|
+
* pages to the end (a block that never settles — an OPEN one, an ABANDONED
|
|
4
|
+
* one with an unknown rest — must not hide the ones reserved after it), its
|
|
5
|
+
* KNOWN unused ranges voided at SEFAZ once, its UNKNOWN ones only reported.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { FiscalDocumentError } from "./errors";
|
|
9
|
+
import { xmlKeys } from "./helpers";
|
|
10
|
+
import { accountFor, gapOf, isSettled } from "./contingency-gaps";
|
|
11
|
+
import type { Deps } from "./service-profile";
|
|
12
|
+
import type { ContingencyBlockState, ContingencyBlockStore, ContingencyGap, FiscalStore } from "./ports";
|
|
13
|
+
|
|
14
|
+
/** How many blocks one read of a pass asks for; the pass reads pages until one comes back empty. */
|
|
15
|
+
const BLOCK_PAGE_SIZE = 1_000;
|
|
16
|
+
|
|
17
|
+
const BLOCK_METHODS = [
|
|
18
|
+
"reserveContingencyBlock",
|
|
19
|
+
"findContingencyBlock",
|
|
20
|
+
"listContingencyBlocks",
|
|
21
|
+
"closeContingencyBlock",
|
|
22
|
+
"settleContingencyBlock",
|
|
23
|
+
"recordContingencyVoid",
|
|
24
|
+
"getDocumentByAccessKey",
|
|
25
|
+
"recordSignedDocument",
|
|
26
|
+
] as const;
|
|
27
|
+
|
|
28
|
+
export function blockStore(store: FiscalStore): FiscalStore & ContingencyBlockStore {
|
|
29
|
+
const candidate = store as FiscalStore & Partial<ContingencyBlockStore>;
|
|
30
|
+
if (BLOCK_METHODS.some((name) => typeof candidate[name] !== "function")) {
|
|
31
|
+
throw new FiscalDocumentError("NOT_SUPPORTED", "this FiscalStore does not keep contingency blocks (ContingencyBlockStore).");
|
|
32
|
+
}
|
|
33
|
+
return candidate as FiscalStore & ContingencyBlockStore;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type BlockFilter = Omit<Parameters<ContingencyBlockStore["listContingencyBlocks"]>[0], "limit" | "after">;
|
|
37
|
+
|
|
38
|
+
/** Every page of the blocks a filter selects, in `(reservedAt, id)` order, to exhaustion. */
|
|
39
|
+
async function* blockPages(store: ContingencyBlockStore, filter: BlockFilter): AsyncGenerator<ContingencyBlockState[]> {
|
|
40
|
+
let after: { reservedAt: string; id: string } | undefined;
|
|
41
|
+
for (;;) {
|
|
42
|
+
const page = await store.listContingencyBlocks({ ...filter, ...(after === undefined ? {} : { after }), limit: BLOCK_PAGE_SIZE });
|
|
43
|
+
const last = page[page.length - 1];
|
|
44
|
+
// An empty page ends the read; a page that does not move the cursor would loop for ever.
|
|
45
|
+
if (last === undefined || (after !== undefined && last.id === after.id)) return;
|
|
46
|
+
yield page;
|
|
47
|
+
after = { reservedAt: last.reservedAt, id: last.id };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The blocks of the pages, one by one. */
|
|
52
|
+
async function* eachBlock(pages: AsyncGenerator<ContingencyBlockState[]>): AsyncGenerator<ContingencyBlockState> {
|
|
53
|
+
for await (const page of pages) yield* page;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function allBlocks(store: ContingencyBlockStore, filter: BlockFilter): Promise<ContingencyBlockState[]> {
|
|
57
|
+
const blocks: ContingencyBlockState[] = [];
|
|
58
|
+
for await (const page of blockPages(store, filter)) blocks.push(...page);
|
|
59
|
+
return blocks;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const ownerFilter = (ownerId: string | undefined) => (ownerId === undefined ? {} : { ownerId });
|
|
63
|
+
|
|
64
|
+
/** Known unused ranges of blocks reserved before `before` (ISO 8601), across owners unless one is named. */
|
|
65
|
+
export async function listKnownGaps(deps: Deps, input: { before: string; ownerId?: string }): Promise<ContingencyGap[]> {
|
|
66
|
+
const blocks = await allBlocks(blockStore(deps.config.store), { ...ownerFilter(input.ownerId), reservedBefore: input.before, unsettledOnly: true });
|
|
67
|
+
return blocks.flatMap((block) => accountFor(block).known.map((range) => gapOf(block, range)));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Ranges whose fate is unknown — flagged for the store, never voided. */
|
|
71
|
+
export async function listUnknownRanges(deps: Deps, input: { ownerId?: string }): Promise<ContingencyGap[]> {
|
|
72
|
+
const blocks = await allBlocks(blockStore(deps.config.store), { ...ownerFilter(input.ownerId), fates: ["CLOSED", "ABANDONED"], unsettledOnly: true });
|
|
73
|
+
return blocks.flatMap((block) => accountFor(block).unknown.map((range) => gapOf(block, range)));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface GapVoidReport {
|
|
77
|
+
readonly voided: ReadonlyArray<ContingencyGap & { readonly protocol: string | null }>;
|
|
78
|
+
readonly failures: ReadonlyArray<ContingencyGap & { readonly class: string; readonly code: string | null; readonly message: string }>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const NOTHING: GapVoidReport = { voided: [], failures: [] };
|
|
82
|
+
|
|
83
|
+
/** Whether the gap is still unused right now: no note recorded and nothing voided inside it since the pass read it. */
|
|
84
|
+
async function stillUnused(store: ContingencyBlockStore, gap: ContingencyGap): Promise<boolean> {
|
|
85
|
+
const fresh = await store.findContingencyBlock({
|
|
86
|
+
ownerId: gap.ownerId, issuerCnpj: gap.issuerCnpj, environment: gap.environment, series: gap.series, number: gap.from,
|
|
87
|
+
});
|
|
88
|
+
if (fresh === null || fresh.id !== gap.blockId) return false;
|
|
89
|
+
const inside = (n: number) => n >= gap.from && n <= gap.to;
|
|
90
|
+
return !fresh.documented.some(inside) && !fresh.voided.some((range) => range.from <= gap.to && range.to >= gap.from);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function voidGap(deps: Deps, store: ContingencyBlockStore, gap: ContingencyGap): Promise<GapVoidReport> {
|
|
94
|
+
// A note handed in late since the page was read takes its number: leave the range for the next pass.
|
|
95
|
+
if (!(await stillUnused(store, gap))) return NOTHING;
|
|
96
|
+
const profile = await deps.ctx.requireProfile(gap.ownerId);
|
|
97
|
+
const outcome = await deps.config.issuer.inutilize({
|
|
98
|
+
model: "NFCE",
|
|
99
|
+
environment: gap.environment,
|
|
100
|
+
signing: await deps.ctx.material(profile),
|
|
101
|
+
uf: profile.address.uf,
|
|
102
|
+
cnpj: gap.issuerCnpj,
|
|
103
|
+
year: Number(gap.reservedAt.slice(0, 4)),
|
|
104
|
+
series: gap.series,
|
|
105
|
+
from: gap.from,
|
|
106
|
+
to: gap.to,
|
|
107
|
+
reason: deps.config.reasons.inutilization,
|
|
108
|
+
});
|
|
109
|
+
if (!outcome.ok) {
|
|
110
|
+
return { voided: [], failures: [{ ...gap, class: outcome.error.class, code: outcome.error.code, message: outcome.error.message }] };
|
|
111
|
+
}
|
|
112
|
+
const xml = outcome.answer.inutilizationXml;
|
|
113
|
+
const eventXmlKey = xml === null ? null : xmlKeys.inutilizedRange(gap);
|
|
114
|
+
if (eventXmlKey !== null) await deps.config.storage.put(eventXmlKey, xml ?? "", "application/xml");
|
|
115
|
+
await store.recordContingencyVoid({
|
|
116
|
+
id: deps.config.newId(),
|
|
117
|
+
blockId: gap.blockId,
|
|
118
|
+
range: { from: gap.from, to: gap.to },
|
|
119
|
+
protocol: outcome.answer.protocol,
|
|
120
|
+
eventXmlKey,
|
|
121
|
+
at: deps.ctx.nowIso(),
|
|
122
|
+
});
|
|
123
|
+
return { voided: [{ ...gap, protocol: outcome.answer.protocol }], failures: [] };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Void one block's known ranges; settle it when nothing is left to account for. */
|
|
127
|
+
async function voidBlock(deps: Deps, store: ContingencyBlockStore, block: ContingencyBlockState): Promise<GapVoidReport> {
|
|
128
|
+
const voided: GapVoidReport["voided"][number][] = [];
|
|
129
|
+
const failures: GapVoidReport["failures"][number][] = [];
|
|
130
|
+
for (const range of accountFor(block).known) {
|
|
131
|
+
const one = await voidGap(deps, store, gapOf(block, range));
|
|
132
|
+
voided.push(...one.voided);
|
|
133
|
+
failures.push(...one.failures);
|
|
134
|
+
}
|
|
135
|
+
const done = voided.map(({ from, to }) => ({ from, to }));
|
|
136
|
+
if (isSettled({ ...block, voided: [...block.voided, ...done] })) await store.settleContingencyBlock({ id: block.id, at: deps.ctx.nowIso() });
|
|
137
|
+
return { voided, failures };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* One pass of the gap job: every KNOWN unused range of a block reserved
|
|
142
|
+
* before `before` is inutilized once (one call per range); a failure is
|
|
143
|
+
* reported and tried again on the next pass. A block left with nothing to
|
|
144
|
+
* account for is settled and read no more. Unknown ranges are never voided.
|
|
145
|
+
* Every unsettled block is read, page after page.
|
|
146
|
+
*/
|
|
147
|
+
export async function voidKnownGaps(deps: Deps, input: { before: string; ownerId?: string }): Promise<GapVoidReport> {
|
|
148
|
+
const store = blockStore(deps.config.store);
|
|
149
|
+
const report = { voided: [] as GapVoidReport["voided"][number][], failures: [] as GapVoidReport["failures"][number][] };
|
|
150
|
+
const pages = blockPages(store, { ...ownerFilter(input.ownerId), reservedBefore: input.before, unsettledOnly: true });
|
|
151
|
+
for await (const block of eachBlock(pages)) {
|
|
152
|
+
const one = await voidBlock(deps, store, block);
|
|
153
|
+
report.voided.push(...one.voided);
|
|
154
|
+
report.failures.push(...one.failures);
|
|
155
|
+
}
|
|
156
|
+
return report;
|
|
157
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading an NFC-e a holder signed offline (`tpEmis = 9`, no `nfeProc`) for
|
|
3
|
+
* `recordSigned`: the fields the document record keeps, each read from the
|
|
4
|
+
* XML, and every reason to refuse it. The XML is the fiscal service's own
|
|
5
|
+
* layout, so the DANFE's reader (`../danfe/xml`) reads it.
|
|
6
|
+
*
|
|
7
|
+
* The access key is checked against the fields it encodes, not only its
|
|
8
|
+
* check digit: a key whose number or series disagrees with `ide` would let a
|
|
9
|
+
* holder record a number outside its block under a key inside it.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { SefazEnvironment } from "../sidecar/types";
|
|
13
|
+
import { accessKeyOf, element, text } from "../danfe/xml";
|
|
14
|
+
import { FiscalDocumentError } from "./errors";
|
|
15
|
+
import { isValidAccessKey } from "./helpers";
|
|
16
|
+
|
|
17
|
+
/** What a station-signed NFC-e says about itself. */
|
|
18
|
+
export interface SignedContingencyNote {
|
|
19
|
+
readonly accessKey: string;
|
|
20
|
+
readonly issuerCnpj: string;
|
|
21
|
+
readonly environment: SefazEnvironment;
|
|
22
|
+
readonly series: number;
|
|
23
|
+
readonly number: number;
|
|
24
|
+
/** `cNF`. */
|
|
25
|
+
readonly keyCode: string;
|
|
26
|
+
/** `dhEmi`. */
|
|
27
|
+
readonly issuedAt: string;
|
|
28
|
+
/** `dhCont` and `xJust`. */
|
|
29
|
+
readonly contingency: { readonly since: string; readonly reason: string };
|
|
30
|
+
readonly totalCents: number;
|
|
31
|
+
readonly discountCents: number;
|
|
32
|
+
/** The consumer's CPF/CNPJ (or foreign id), or `null`. */
|
|
33
|
+
readonly buyerTaxId: string | null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function refuse(why: string): never {
|
|
37
|
+
throw new FiscalDocumentError("CONTINGENCY_XML_REFUSED", why);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function cents(value: string | null): number {
|
|
41
|
+
return Math.round(Number(value ?? "0") * 100);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function required(scope: string | null, tag: string): string {
|
|
45
|
+
const value = text(scope, tag);
|
|
46
|
+
if (value === null || value.trim() === "") refuse(`the XML has no ${tag}.`);
|
|
47
|
+
return value.trim();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The key's own fields: cUF(2) AAMM(4) CNPJ(14) mod(2) serie(3) nNF(9) tpEmis(1) cNF(8) cDV(1). */
|
|
51
|
+
function assertKeyMatches(key: string, ide: string, cnpj: string): void {
|
|
52
|
+
const expected = [
|
|
53
|
+
required(ide, "cUF"),
|
|
54
|
+
required(ide, "dhEmi").slice(2, 4) + required(ide, "dhEmi").slice(5, 7),
|
|
55
|
+
cnpj,
|
|
56
|
+
required(ide, "mod"),
|
|
57
|
+
required(ide, "serie").padStart(3, "0"),
|
|
58
|
+
required(ide, "nNF").padStart(9, "0"),
|
|
59
|
+
required(ide, "tpEmis"),
|
|
60
|
+
required(ide, "cNF"),
|
|
61
|
+
required(ide, "cDV"),
|
|
62
|
+
].join("");
|
|
63
|
+
if (expected !== key) refuse("the access key does not match the fields it encodes.");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Parse and check a station-signed NFC-e. Refuses (`CONTINGENCY_XML_REFUSED`)
|
|
68
|
+
* anything but a model 65 signed in offline contingency (`tpEmis = 9`, with
|
|
69
|
+
* `dhCont` and `xJust`), a key whose check digit or fields are wrong, and an
|
|
70
|
+
* XML with no signature. The issuer's CNPJ and the block are the caller's to
|
|
71
|
+
* check against its profile and its blocks.
|
|
72
|
+
*/
|
|
73
|
+
interface Parts {
|
|
74
|
+
readonly infNFe: string;
|
|
75
|
+
readonly ide: string;
|
|
76
|
+
readonly emit: string;
|
|
77
|
+
readonly accessKey: string;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The blocks every check reads, or a refusal naming what is missing. */
|
|
81
|
+
function assertSignedNote(signedXml: string): void {
|
|
82
|
+
if (typeof signedXml !== "string" || signedXml.trim() === "") refuse("signedXml is empty.");
|
|
83
|
+
if (element(signedXml, "nfeProc") !== null || element(signedXml, "protNFe") !== null) refuse("the XML is authorised already, not a signed note.");
|
|
84
|
+
if (element(signedXml, "Signature") === null) refuse("the XML is not signed.");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function partsOf(signedXml: string): Parts {
|
|
88
|
+
assertSignedNote(signedXml);
|
|
89
|
+
const infNFe = element(signedXml, "infNFe");
|
|
90
|
+
const ide = infNFe === null ? null : element(infNFe, "ide");
|
|
91
|
+
const emit = infNFe === null ? null : element(infNFe, "emit");
|
|
92
|
+
const accessKey = accessKeyOf(signedXml);
|
|
93
|
+
if (infNFe === null || ide === null || emit === null || accessKey === null) refuse("the XML is not an NF-e: no infNFe, ide, emit or access key.");
|
|
94
|
+
return { infNFe, ide, emit, accessKey };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** An NFC-e (65) signed offline (`tpEmis` 9) under a key whose check digit and fields hold. */
|
|
98
|
+
function assertOfflineNfce({ ide, accessKey }: Parts, issuerCnpj: string): void {
|
|
99
|
+
if (required(ide, "mod") !== "65") refuse("the XML is not an NFC-e (model 65).");
|
|
100
|
+
if (required(ide, "tpEmis") !== "9") refuse("the XML is not signed in offline contingency (tpEmis 9).");
|
|
101
|
+
if (!isValidAccessKey(accessKey)) refuse("the access key's check digit is wrong.");
|
|
102
|
+
assertKeyMatches(accessKey, ide, issuerCnpj);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function buyerOf(infNFe: string): string | null {
|
|
106
|
+
const dest = element(infNFe, "dest");
|
|
107
|
+
return text(dest, "CPF") ?? text(dest, "CNPJ") ?? text(dest, "idEstrangeiro");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Parse and check a station-signed NFC-e. Refuses (`CONTINGENCY_XML_REFUSED`)
|
|
112
|
+
* anything but a model 65 signed in offline contingency (`tpEmis = 9`, with
|
|
113
|
+
* `dhCont` and `xJust`), a key whose check digit or fields are wrong, and an
|
|
114
|
+
* XML with no signature. The issuer's CNPJ and the block are the caller's to
|
|
115
|
+
* check against its profile and its blocks.
|
|
116
|
+
*/
|
|
117
|
+
export function parseSignedContingencyNote(signedXml: string): SignedContingencyNote {
|
|
118
|
+
const parts = partsOf(signedXml);
|
|
119
|
+
const { infNFe, ide, emit, accessKey } = parts;
|
|
120
|
+
const issuerCnpj = required(emit, "CNPJ");
|
|
121
|
+
assertOfflineNfce(parts, issuerCnpj);
|
|
122
|
+
const total = element(infNFe, "ICMSTot");
|
|
123
|
+
return {
|
|
124
|
+
accessKey,
|
|
125
|
+
issuerCnpj,
|
|
126
|
+
environment: required(ide, "tpAmb") === "1" ? "producao" : "homologacao",
|
|
127
|
+
series: Number(required(ide, "serie")),
|
|
128
|
+
number: Number(required(ide, "nNF")),
|
|
129
|
+
keyCode: required(ide, "cNF"),
|
|
130
|
+
issuedAt: required(ide, "dhEmi"),
|
|
131
|
+
contingency: { since: required(ide, "dhCont"), reason: required(ide, "xJust") },
|
|
132
|
+
totalCents: cents(text(total, "vNF")),
|
|
133
|
+
discountCents: cents(text(total, "vDesc")),
|
|
134
|
+
buyerTaxId: buyerOf(infNFe),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contingency blocks (FUT-4717): numbers of an issuing point's contingency
|
|
3
|
+
* series handed to a holder in advance, the notes it signed on them recorded
|
|
4
|
+
* as the subjects' `CONTINGENCY` documents (transmitted by the same pass as
|
|
5
|
+
* the server's own), and the known rest of each block voided at SEFAZ.
|
|
6
|
+
*
|
|
7
|
+
* A station-signed note is transmitted from its stored XML exactly as the
|
|
8
|
+
* server's own contingency documents are (`transmit`): nothing rebuilds it
|
|
9
|
+
* from the sale. A rejection keeps its number (the buyer holds its DANFE):
|
|
10
|
+
* `resendContingency` re-signs it under the same number, as for any
|
|
11
|
+
* contingency NFC-e.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { FiscalDocumentError } from "./errors";
|
|
15
|
+
import { assertDraftFits, issuerOf, totalsOf, xmlKeys } from "./helpers";
|
|
16
|
+
import { allBlocks, blockStore } from "./contingency-pass";
|
|
17
|
+
import { type SignedContingencyNote, parseSignedContingencyNote } from "./contingency-xml";
|
|
18
|
+
import type { Deps } from "./service-profile";
|
|
19
|
+
import type {
|
|
20
|
+
ContingencyBlock,
|
|
21
|
+
ContingencyBlockFate,
|
|
22
|
+
ContingencyBlockSource,
|
|
23
|
+
ContingencyBlockState,
|
|
24
|
+
ContingencyBlockStore,
|
|
25
|
+
SaleDraft,
|
|
26
|
+
} from "./ports";
|
|
27
|
+
import type { FiscalDocument, FiscalDocumentEvent } from "./types";
|
|
28
|
+
|
|
29
|
+
/** The block size when the caller names none (decided on FUT-4717: 50, a new block at 10 left). */
|
|
30
|
+
export const DEFAULT_CONTINGENCY_BLOCK_SIZE = 50;
|
|
31
|
+
/** The largest block one call reserves. */
|
|
32
|
+
export const MAX_CONTINGENCY_BLOCK_SIZE = 10_000;
|
|
33
|
+
|
|
34
|
+
function assertHolder(holder: unknown): asserts holder is string {
|
|
35
|
+
if (typeof holder !== "string" || holder.trim() === "" || holder.length > 200) {
|
|
36
|
+
throw new FiscalDocumentError("INVALID_INPUT", "holder must be a non-empty id of at most 200 characters.");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function refuse(why: string): never {
|
|
41
|
+
throw new FiscalDocumentError("CONTINGENCY_XML_REFUSED", why);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function reserveBlock(deps: Deps, input: Parameters<ContingencyBlockSource["reserveBlock"]>[0]): Promise<ContingencyBlock> {
|
|
45
|
+
const store = blockStore(deps.config.store);
|
|
46
|
+
assertHolder(input.holder);
|
|
47
|
+
const size = input.size ?? DEFAULT_CONTINGENCY_BLOCK_SIZE;
|
|
48
|
+
if (!Number.isInteger(size) || size < 1 || size > MAX_CONTINGENCY_BLOCK_SIZE) {
|
|
49
|
+
throw new FiscalDocumentError("INVALID_INPUT", `size must be a whole number from 1 to ${MAX_CONTINGENCY_BLOCK_SIZE}.`);
|
|
50
|
+
}
|
|
51
|
+
const profile = await deps.ctx.requireProfile(input.ownerId);
|
|
52
|
+
if (profile.documentPolicy !== "ISSUED_HERE") {
|
|
53
|
+
throw new FiscalDocumentError("NOT_ISSUABLE", `owner ${input.ownerId}'s documentPolicy is ${profile.documentPolicy}.`);
|
|
54
|
+
}
|
|
55
|
+
const point = await deps.ctx.requirePoint(input.ownerId, input.issuingPointId);
|
|
56
|
+
if (point.model !== "NFCE" || point.contingencySeries === null) {
|
|
57
|
+
throw new FiscalDocumentError("INVALID_INPUT", `issuing point ${point.id} has no NFC-e contingency series.`);
|
|
58
|
+
}
|
|
59
|
+
return store.reserveContingencyBlock({
|
|
60
|
+
request: {
|
|
61
|
+
ownerId: input.ownerId,
|
|
62
|
+
issuerCnpj: profile.cnpj,
|
|
63
|
+
issuingPointId: point.id,
|
|
64
|
+
environment: profile.environment,
|
|
65
|
+
model: "NFCE",
|
|
66
|
+
seriesKind: "CONTINGENCY",
|
|
67
|
+
series: point.contingencySeries,
|
|
68
|
+
},
|
|
69
|
+
size,
|
|
70
|
+
id: deps.config.newId(),
|
|
71
|
+
holder: input.holder,
|
|
72
|
+
at: deps.ctx.nowIso(),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The CONTINGENCY document a holder's signed note becomes, owed a transmission now. */
|
|
77
|
+
function signedDocument(
|
|
78
|
+
deps: Deps,
|
|
79
|
+
input: { ownerId: string; subjectId: string; draft: SaleDraft; note: SignedContingencyNote; block: ContingencyBlock; xmlKey: string },
|
|
80
|
+
profile: Parameters<typeof issuerOf>[0],
|
|
81
|
+
): FiscalDocument {
|
|
82
|
+
const { note, draft } = input;
|
|
83
|
+
const at = deps.ctx.nowIso();
|
|
84
|
+
const sale = { ...draft, series: note.series, number: note.number, keyCode: note.keyCode, issuedAt: note.issuedAt, issuer: issuerOf(profile) };
|
|
85
|
+
return {
|
|
86
|
+
id: deps.config.newId(),
|
|
87
|
+
ownerId: input.ownerId,
|
|
88
|
+
issuerCnpj: note.issuerCnpj,
|
|
89
|
+
subjectId: input.subjectId,
|
|
90
|
+
issuingPointId: input.block.issuingPointId,
|
|
91
|
+
model: "NFCE",
|
|
92
|
+
environment: note.environment,
|
|
93
|
+
seriesKind: "CONTINGENCY",
|
|
94
|
+
series: note.series,
|
|
95
|
+
number: note.number,
|
|
96
|
+
keyCode: note.keyCode,
|
|
97
|
+
status: "CONTINGENCY",
|
|
98
|
+
sale,
|
|
99
|
+
// What the buyer's DANFE prints is the XML's: a resend may never change it.
|
|
100
|
+
totals: { ...totalsOf(draft), totalCents: note.totalCents, discountCents: note.discountCents },
|
|
101
|
+
buyerTaxId: note.buyerTaxId,
|
|
102
|
+
accessKey: note.accessKey,
|
|
103
|
+
protocol: null,
|
|
104
|
+
statusCode: null,
|
|
105
|
+
statusReason: null,
|
|
106
|
+
lastErrorClass: null,
|
|
107
|
+
outcomeUnknown: false,
|
|
108
|
+
issuedAt: note.issuedAt,
|
|
109
|
+
authorizedAt: null,
|
|
110
|
+
contingency: note.contingency,
|
|
111
|
+
cancelledAt: null,
|
|
112
|
+
cancelReason: null,
|
|
113
|
+
cancelProtocol: null,
|
|
114
|
+
replacedById: null,
|
|
115
|
+
numberVoid: null,
|
|
116
|
+
xmlKey: input.xmlKey,
|
|
117
|
+
eventXmlKey: null,
|
|
118
|
+
attempts: 0,
|
|
119
|
+
failures: 0,
|
|
120
|
+
nextAttemptAt: at,
|
|
121
|
+
attention: false,
|
|
122
|
+
deadlineWarnedAt: null,
|
|
123
|
+
createdAt: at,
|
|
124
|
+
updatedAt: at,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function signedEvent(document: FiscalDocument, holder: string): FiscalDocumentEvent {
|
|
129
|
+
return {
|
|
130
|
+
documentId: document.id,
|
|
131
|
+
kind: "SIGN_OFFLINE",
|
|
132
|
+
fromStatus: "PENDING",
|
|
133
|
+
toStatus: "CONTINGENCY",
|
|
134
|
+
at: document.createdAt,
|
|
135
|
+
errorClass: null,
|
|
136
|
+
code: null,
|
|
137
|
+
answer: { signedBy: "HOLDER", holder, accessKey: document.accessKey },
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A replayed upload: the document already recorded under the key, for the same subject. */
|
|
142
|
+
function replayed(existing: FiscalDocument, subjectId: string): FiscalDocument {
|
|
143
|
+
if (existing.subjectId !== subjectId) refuse(`access key ${existing.accessKey ?? ""} is recorded for another subject.`);
|
|
144
|
+
return existing;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function blockOf(store: ContingencyBlockStore, ownerId: string, note: SignedContingencyNote, holder: string): Promise<ContingencyBlock> {
|
|
148
|
+
const block = await store.findContingencyBlock({ ownerId, issuerCnpj: note.issuerCnpj, environment: note.environment, series: note.series, number: note.number });
|
|
149
|
+
if (block === null || block.ownerId !== ownerId || block.holder !== holder) {
|
|
150
|
+
refuse(`number ${note.number} of series ${note.series} is not inside a block held by ${holder}.`);
|
|
151
|
+
}
|
|
152
|
+
// Voided at SEFAZ already (a late hand-in behind a higher number): it can never be authorised, the sale is issued online.
|
|
153
|
+
if (block.voided.some((range) => note.number >= range.from && note.number <= range.to)) {
|
|
154
|
+
refuse(`number ${note.number} of series ${note.series} was voided at SEFAZ.`);
|
|
155
|
+
}
|
|
156
|
+
return block;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The XML must print what the booked sale says: its total and its buyer (a resend could never change them). */
|
|
160
|
+
function assertMatchesSale(note: SignedContingencyNote, draft: SaleDraft): void {
|
|
161
|
+
const totalCents = totalsOf(draft).totalCents;
|
|
162
|
+
if (note.totalCents !== totalCents) refuse(`the XML's total (vNF ${note.totalCents} cents) is not the sale's (${totalCents} cents).`);
|
|
163
|
+
if (note.buyerTaxId !== (draft.buyer?.document.number ?? null)) refuse("the XML's buyer document is not the sale's.");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The note, parsed and checked against the owner's profile. */
|
|
167
|
+
async function checkedNote(deps: Deps, input: Parameters<ContingencyBlockSource["recordSigned"]>[0]) {
|
|
168
|
+
assertHolder(input.holder);
|
|
169
|
+
if (typeof input.subjectId !== "string" || input.subjectId === "") throw new FiscalDocumentError("INVALID_INPUT", "subjectId is required.");
|
|
170
|
+
const note = parseSignedContingencyNote(input.signedXml);
|
|
171
|
+
const profile = await deps.ctx.requireProfile(input.ownerId);
|
|
172
|
+
if (note.issuerCnpj !== profile.cnpj) refuse("the XML's issuer is not the owner's CNPJ.");
|
|
173
|
+
if (note.environment !== profile.environment) refuse(`the XML was signed for ${note.environment}, the owner issues in ${profile.environment}.`);
|
|
174
|
+
return { note, profile };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Insert the note; a concurrent upload of the same note is answered, a number under another key refused. */
|
|
178
|
+
async function insertNote(store: ContingencyBlockStore, document: FiscalDocument, holder: string, subjectId: string): Promise<FiscalDocument> {
|
|
179
|
+
try {
|
|
180
|
+
return await store.recordSignedDocument(document, signedEvent(document, holder));
|
|
181
|
+
} catch (error) {
|
|
182
|
+
if (!(error instanceof FiscalDocumentError) || error.code !== "CONFLICT") throw error;
|
|
183
|
+
const raced = await store.getDocumentByAccessKey(document.ownerId, document.accessKey ?? "");
|
|
184
|
+
if (raced !== null) return replayed(raced, subjectId);
|
|
185
|
+
return refuse(`number ${document.number} of series ${document.series} is recorded under another key.`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export async function recordSigned(deps: Deps, input: Parameters<ContingencyBlockSource["recordSigned"]>[0]): Promise<FiscalDocument> {
|
|
190
|
+
const store = blockStore(deps.config.store);
|
|
191
|
+
const { note, profile } = await checkedNote(deps, input);
|
|
192
|
+
const existing = await store.getDocumentByAccessKey(input.ownerId, note.accessKey);
|
|
193
|
+
if (existing !== null) return replayed(existing, input.subjectId);
|
|
194
|
+
const block = await blockOf(store, input.ownerId, note, input.holder);
|
|
195
|
+
const draft = input.sale ?? (await deps.config.sales.saleFor({ ownerId: input.ownerId, subjectId: input.subjectId, model: "NFCE" }));
|
|
196
|
+
assertDraftFits(draft, "NFCE");
|
|
197
|
+
assertMatchesSale(note, draft);
|
|
198
|
+
const xmlKey = xmlKeys.signed(note.accessKey);
|
|
199
|
+
await deps.config.storage.put(xmlKey, input.signedXml, "application/xml");
|
|
200
|
+
const document = signedDocument(deps, { ownerId: input.ownerId, subjectId: input.subjectId, draft, note, block, xmlKey }, profile);
|
|
201
|
+
return insertNote(store, document, input.holder, input.subjectId);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function heldBlock(
|
|
205
|
+
deps: Deps,
|
|
206
|
+
store: ContingencyBlockStore,
|
|
207
|
+
input: { ownerId: string; holder: string; series: number; from: number },
|
|
208
|
+
): Promise<ContingencyBlockState> {
|
|
209
|
+
const profile = await deps.ctx.requireProfile(input.ownerId);
|
|
210
|
+
const block = await store.findContingencyBlock({
|
|
211
|
+
ownerId: input.ownerId, issuerCnpj: profile.cnpj, environment: profile.environment, series: input.series, number: input.from,
|
|
212
|
+
});
|
|
213
|
+
if (block === null || block.holder !== input.holder || block.from !== input.from) throw new FiscalDocumentError("NOT_FOUND", `${input.holder} holds no block of series ${input.series} from ${input.from}.`);
|
|
214
|
+
return block;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export async function closeBlock(deps: Deps, input: Parameters<ContingencyBlockSource["closeBlock"]>[0]): Promise<ContingencyBlock> {
|
|
218
|
+
const store = blockStore(deps.config.store);
|
|
219
|
+
assertHolder(input.holder);
|
|
220
|
+
const block = await heldBlock(deps, store, input);
|
|
221
|
+
const used = input.sequenceUsed;
|
|
222
|
+
if (!Number.isInteger(used) || used < block.from - 1 || used > block.to) {
|
|
223
|
+
throw new FiscalDocumentError("INVALID_INPUT", `sequenceUsed must be from ${block.from - 1} to ${block.to}.`);
|
|
224
|
+
}
|
|
225
|
+
if (block.fate === "CLOSED" && block.usedUpTo === used) return block;
|
|
226
|
+
if (block.fate !== "OPEN") throw new FiscalDocumentError("TRANSITION_REFUSED", `the block from ${block.from} is ${block.fate}.`);
|
|
227
|
+
const at = deps.ctx.nowIso();
|
|
228
|
+
if (!(await store.closeContingencyBlock({ id: block.id, fate: "CLOSED", usedUpTo: used, at }))) {
|
|
229
|
+
throw new FiscalDocumentError("CONFLICT", `the block from ${block.from} moved while it was closed.`);
|
|
230
|
+
}
|
|
231
|
+
return { ...block, fate: "CLOSED", usedUpTo: used, closedAt: at };
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The holder is gone (a new install took its place): its open blocks are ABANDONED. Answers them. */
|
|
235
|
+
export async function abandonBlocks(deps: Deps, input: { ownerId: string; holder: string }): Promise<ContingencyBlock[]> {
|
|
236
|
+
const store = blockStore(deps.config.store);
|
|
237
|
+
assertHolder(input.holder);
|
|
238
|
+
const open = await allBlocks(store, { ownerId: input.ownerId, holder: input.holder, fates: ["OPEN"] });
|
|
239
|
+
const at = deps.ctx.nowIso();
|
|
240
|
+
const abandoned: ContingencyBlock[] = [];
|
|
241
|
+
for (const block of open) {
|
|
242
|
+
if (await store.closeContingencyBlock({ id: block.id, fate: "ABANDONED", usedUpTo: null, at })) {
|
|
243
|
+
abandoned.push({ ...block, fate: "ABANDONED", closedAt: at });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return abandoned;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
export async function listBlocks(
|
|
250
|
+
deps: Deps,
|
|
251
|
+
input: { ownerId: string; holder?: string; fates?: readonly ContingencyBlockFate[] },
|
|
252
|
+
): Promise<ContingencyBlockState[]> {
|
|
253
|
+
return allBlocks(blockStore(deps.config.store), input);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export { listKnownGaps, listUnknownRanges, voidKnownGaps } from "./contingency-pass";
|
|
257
|
+
export type { GapVoidReport } from "./contingency-pass";
|
package/src/documents/errors.ts
CHANGED
|
@@ -41,7 +41,14 @@ export type FiscalDocumentErrorCode =
|
|
|
41
41
|
* the buyer's return NF-e of entry answers a consumer's NFC-e only; an NF-e
|
|
42
42
|
* past its window is the issuer's own correction, not this path's.
|
|
43
43
|
*/
|
|
44
|
-
| "RETURN_OF_NFCE_ONLY"
|
|
44
|
+
| "RETURN_OF_NFCE_ONLY"
|
|
45
|
+
/**
|
|
46
|
+
* `recordSigned` (FUT-4717): an XML a holder signed offline that the package
|
|
47
|
+
* will not record — another CNPJ or environment, not a model 65, not
|
|
48
|
+
* `tpEmis = 9`, a key whose check digit or fields are wrong, or a number
|
|
49
|
+
* outside every block that holder was given. Nothing was recorded.
|
|
50
|
+
*/
|
|
51
|
+
| "CONTINGENCY_XML_REFUSED";
|
|
45
52
|
|
|
46
53
|
export class FiscalDocumentError extends Error {
|
|
47
54
|
readonly code: FiscalDocumentErrorCode;
|
package/src/documents/helpers.ts
CHANGED
|
@@ -81,6 +81,9 @@ export const xmlKeys = {
|
|
|
81
81
|
linkPayment: (accessKey: string, sequence: number) => `fiscal/${accessKey}/link-payment-${sequence}.xml`,
|
|
82
82
|
inutilization: (document: FiscalDocument) =>
|
|
83
83
|
`fiscal/inutilization/${document.environment}/${document.model}/${document.ownerId}/${document.series}-${document.number}.xml`,
|
|
84
|
+
/** A range of a contingency block voided at once (FUT-4717). */
|
|
85
|
+
inutilizedRange: (gap: { environment: string; ownerId: string; series: number; from: number; to: number }) =>
|
|
86
|
+
`fiscal/inutilization/${gap.environment}/NFCE/${gap.ownerId}/${gap.series}-${gap.from}-${gap.to}.xml`,
|
|
84
87
|
};
|
|
85
88
|
|
|
86
89
|
/** The IBGE code of each federative unit: the access key's first two digits. */
|