@12-apps/fiscal 0.0.0-stage → 1.0.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 +124 -2
- package/dist/chunk-7QVYU63E.js +7 -0
- package/dist/chunk-7QVYU63E.js.map +1 -0
- package/dist/engine/index.d.ts +361 -0
- package/dist/engine/index.js +591 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/manifest/index.d.ts +83 -0
- package/dist/manifest/index.js +33 -0
- package/dist/manifest/index.js.map +1 -0
- package/package.json +68 -4
- package/src/engine/compare.ts +68 -0
- package/src/engine/compute.ts +40 -0
- package/src/engine/errors.ts +28 -0
- package/src/engine/index.ts +20 -0
- package/src/engine/oracle.ts +81 -0
- package/src/engine/plan.ts +268 -0
- package/src/engine/rounding.ts +81 -0
- package/src/engine/types.ts +270 -0
- package/src/engine/validate.ts +222 -0
- package/src/manifest/index.ts +37 -0
- package/src/manifest/permissions.ts +28 -0
- package/tables/ibs-cbs-2026-11-13.json +156 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__name
|
|
3
|
+
} from "../chunk-7QVYU63E.js";
|
|
4
|
+
|
|
5
|
+
// src/engine/errors.ts
|
|
6
|
+
var FiscalEngineError = class extends Error {
|
|
7
|
+
static {
|
|
8
|
+
__name(this, "FiscalEngineError");
|
|
9
|
+
}
|
|
10
|
+
code;
|
|
11
|
+
constructor(code, message) {
|
|
12
|
+
super(`[fiscal/engine] ${code}: ${message}`);
|
|
13
|
+
this.name = "FiscalEngineError";
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/engine/rounding.ts
|
|
19
|
+
var BP = 10000n;
|
|
20
|
+
var RATE_SCALE = BP * BP;
|
|
21
|
+
function roundHalfEven(numerator, denominator) {
|
|
22
|
+
const quotient = numerator / denominator;
|
|
23
|
+
const twice = (numerator - quotient * denominator) * 2n;
|
|
24
|
+
if (twice > denominator) return quotient + 1n;
|
|
25
|
+
if (twice < denominator) return quotient;
|
|
26
|
+
return quotient % 2n === 0n ? quotient : quotient + 1n;
|
|
27
|
+
}
|
|
28
|
+
__name(roundHalfEven, "roundHalfEven");
|
|
29
|
+
function effectiveRate(rateBp, reductionBp) {
|
|
30
|
+
return BigInt(rateBp) * (BP - BigInt(reductionBp));
|
|
31
|
+
}
|
|
32
|
+
__name(effectiveRate, "effectiveRate");
|
|
33
|
+
function roundTaxCents(baseCents, rateBp, reductionBp) {
|
|
34
|
+
return Number(roundHalfEven(BigInt(baseCents) * effectiveRate(rateBp, reductionBp), RATE_SCALE));
|
|
35
|
+
}
|
|
36
|
+
__name(roundTaxCents, "roundTaxCents");
|
|
37
|
+
function taxesOn(base, rates) {
|
|
38
|
+
return rates.map((rate) => roundHalfEven(base * rate, RATE_SCALE));
|
|
39
|
+
}
|
|
40
|
+
__name(taxesOn, "taxesOn");
|
|
41
|
+
function splitTaxInclusivePrice(priceCents, rates) {
|
|
42
|
+
const price = BigInt(priceCents);
|
|
43
|
+
const total = rates.reduce((sum, rate) => sum + rate, 0n);
|
|
44
|
+
let base = price * RATE_SCALE / (RATE_SCALE + total) + 3n;
|
|
45
|
+
let taxes = taxesOn(base, rates);
|
|
46
|
+
while (base > 0n && base + taxes.reduce((sum, tax) => sum + tax, 0n) > price) {
|
|
47
|
+
base -= 1n;
|
|
48
|
+
taxes = taxesOn(base, rates);
|
|
49
|
+
}
|
|
50
|
+
const taxCents = taxes.map(Number);
|
|
51
|
+
return { baseCents: Number(base), taxCents, netCents: priceCents - taxCents.reduce((sum, tax) => sum + tax, 0) };
|
|
52
|
+
}
|
|
53
|
+
__name(splitTaxInclusivePrice, "splitTaxInclusivePrice");
|
|
54
|
+
function quantityInTenThousandths(quantity) {
|
|
55
|
+
return BigInt(Math.round(quantity * 1e4));
|
|
56
|
+
}
|
|
57
|
+
__name(quantityInTenThousandths, "quantityInTenThousandths");
|
|
58
|
+
function netUnitValue(netCents, quantity) {
|
|
59
|
+
const scaled = roundHalfEven(BigInt(netCents) * 10n ** 12n, quantityInTenThousandths(quantity));
|
|
60
|
+
const digits = scaled.toString().padStart(11, "0");
|
|
61
|
+
return `${digits.slice(0, -10)}.${digits.slice(-10)}`;
|
|
62
|
+
}
|
|
63
|
+
__name(netUnitValue, "netUnitValue");
|
|
64
|
+
|
|
65
|
+
// src/engine/validate.ts
|
|
66
|
+
var ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
67
|
+
var IBGE_MUNICIPALITY = /^\d{7}$/;
|
|
68
|
+
var IBGE_UF = /^\d{2}$/;
|
|
69
|
+
var REGIMES = ["REGIME_REGULAR", "SIMPLES_IBS_CBS_REGULAR", "SIMPLES", "MEI", "NAO_CONTRIBUINTE"];
|
|
70
|
+
function refuse(code, message) {
|
|
71
|
+
throw new FiscalEngineError(code, message);
|
|
72
|
+
}
|
|
73
|
+
__name(refuse, "refuse");
|
|
74
|
+
function isRecord(value) {
|
|
75
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
76
|
+
}
|
|
77
|
+
__name(isRecord, "isRecord");
|
|
78
|
+
function isIsoDate(value) {
|
|
79
|
+
if (typeof value !== "string") return false;
|
|
80
|
+
const match = ISO_DATE.exec(value);
|
|
81
|
+
if (!match) return false;
|
|
82
|
+
const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])];
|
|
83
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
84
|
+
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
|
|
85
|
+
}
|
|
86
|
+
__name(isIsoDate, "isIsoDate");
|
|
87
|
+
function isCents(value) {
|
|
88
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
89
|
+
}
|
|
90
|
+
__name(isCents, "isCents");
|
|
91
|
+
function isBp(value) {
|
|
92
|
+
return isCents(value) && value <= 1e4;
|
|
93
|
+
}
|
|
94
|
+
__name(isBp, "isBp");
|
|
95
|
+
function isCode(value) {
|
|
96
|
+
return typeof value === "string" && value !== "";
|
|
97
|
+
}
|
|
98
|
+
__name(isCode, "isCode");
|
|
99
|
+
function isQuantity(value) {
|
|
100
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) return false;
|
|
101
|
+
const tenThousandths = Math.round(value * 1e4);
|
|
102
|
+
return tenThousandths > 0 && Number.isSafeInteger(tenThousandths) && Math.abs(tenThousandths / 1e4 - value) < 1e-9;
|
|
103
|
+
}
|
|
104
|
+
__name(isQuantity, "isQuantity");
|
|
105
|
+
function assertInput(ok, what) {
|
|
106
|
+
if (!ok) refuse("INVALID_INPUT", what);
|
|
107
|
+
}
|
|
108
|
+
__name(assertInput, "assertInput");
|
|
109
|
+
function assertTable(ok, what) {
|
|
110
|
+
if (!ok) refuse("INVALID_TABLES", what);
|
|
111
|
+
}
|
|
112
|
+
__name(assertTable, "assertTable");
|
|
113
|
+
function assertTaxClass(taxClass, where) {
|
|
114
|
+
assertInput(isRecord(taxClass), `${where} must be an object`);
|
|
115
|
+
const value = taxClass;
|
|
116
|
+
assertInput(isCode(value.cClassTrib), `${where}.cClassTrib must be a code`);
|
|
117
|
+
assertInput(isCode(value.cstIbsCbs), `${where}.cstIbsCbs must be a code`);
|
|
118
|
+
assertInput(
|
|
119
|
+
value.regimeEspecifico === null || value.regimeEspecifico === "BARES_RESTAURANTES",
|
|
120
|
+
`${where}.regimeEspecifico must be "BARES_RESTAURANTES" or null`
|
|
121
|
+
);
|
|
122
|
+
assertInput(isBp(value.reductionBp), `${where}.reductionBp must be integer basis points in 0..10000`);
|
|
123
|
+
}
|
|
124
|
+
__name(assertTaxClass, "assertTaxClass");
|
|
125
|
+
function assertLine(line, index) {
|
|
126
|
+
const where = `lines[${index}]`;
|
|
127
|
+
assertInput(isRecord(line), `${where} must be an object`);
|
|
128
|
+
const value = line;
|
|
129
|
+
assertInput(isCode(value.id), `${where}.id must be a non-empty string`);
|
|
130
|
+
assertInput(isCents(value.valueCents), `${where}.valueCents must be non-negative integer cents`);
|
|
131
|
+
assertInput(isQuantity(value.quantity), `${where}.quantity must be positive, with at most four decimals`);
|
|
132
|
+
assertInput(typeof value.foodAndDrink === "boolean", `${where}.foodAndDrink must be a boolean`);
|
|
133
|
+
assertTaxClass(value.taxClass, `${where}.taxClass`);
|
|
134
|
+
}
|
|
135
|
+
__name(assertLine, "assertLine");
|
|
136
|
+
function assertServiceCharge(charge) {
|
|
137
|
+
if (charge === null) return;
|
|
138
|
+
assertInput(isRecord(charge), "exclusions.serviceCharge must be an object or null");
|
|
139
|
+
const value = charge;
|
|
140
|
+
assertInput(isCode(value.id), "exclusions.serviceCharge.id must be a non-empty string");
|
|
141
|
+
assertInput(isCents(value.amountCents), "exclusions.serviceCharge.amountCents must be non-negative integer cents");
|
|
142
|
+
assertInput(typeof value.passedToStaff === "boolean", "exclusions.serviceCharge.passedToStaff must be a boolean");
|
|
143
|
+
assertTaxClass(value.taxClass, "exclusions.serviceCharge.taxClass");
|
|
144
|
+
}
|
|
145
|
+
__name(assertServiceCharge, "assertServiceCharge");
|
|
146
|
+
function assertIntermediation(retained) {
|
|
147
|
+
if (retained === null) return;
|
|
148
|
+
assertInput(isRecord(retained), "exclusions.retainedIntermediation must be an object or null");
|
|
149
|
+
const value = retained;
|
|
150
|
+
assertInput(isCode(value.id), "exclusions.retainedIntermediation.id must be a non-empty string");
|
|
151
|
+
assertInput(isCents(value.amountCents), "exclusions.retainedIntermediation.amountCents must be non-negative integer cents");
|
|
152
|
+
}
|
|
153
|
+
__name(assertIntermediation, "assertIntermediation");
|
|
154
|
+
function assertExclusions(exclusions) {
|
|
155
|
+
assertInput(isRecord(exclusions), "exclusions must be an object");
|
|
156
|
+
const value = exclusions;
|
|
157
|
+
assertInput("serviceCharge" in value, "exclusions.serviceCharge must be given (null when there is none)");
|
|
158
|
+
assertInput("retainedIntermediation" in value, "exclusions.retainedIntermediation must be given (null when there is none)");
|
|
159
|
+
assertServiceCharge(value.serviceCharge);
|
|
160
|
+
assertIntermediation(value.retainedIntermediation);
|
|
161
|
+
}
|
|
162
|
+
__name(assertExclusions, "assertExclusions");
|
|
163
|
+
function assertUniqueIds(input) {
|
|
164
|
+
const ids = input.lines.map((line) => line.id);
|
|
165
|
+
if (input.exclusions.serviceCharge) ids.push(input.exclusions.serviceCharge.id);
|
|
166
|
+
if (input.exclusions.retainedIntermediation) ids.push(input.exclusions.retainedIntermediation.id);
|
|
167
|
+
const seen = /* @__PURE__ */ new Set();
|
|
168
|
+
for (const id of ids) {
|
|
169
|
+
assertInput(!seen.has(id), `line id "${id}" is used twice`);
|
|
170
|
+
seen.add(id);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
__name(assertUniqueIds, "assertUniqueIds");
|
|
174
|
+
function assertSeller(seller) {
|
|
175
|
+
assertInput(isRecord(seller), "seller must be an object");
|
|
176
|
+
const value = seller;
|
|
177
|
+
assertInput(REGIMES.includes(value.regime), `seller.regime must be one of ${REGIMES.join(", ")}`);
|
|
178
|
+
assertInput(isIsoDate(value.regimeEffectiveFrom), "seller.regimeEffectiveFrom must be a YYYY-MM-DD day");
|
|
179
|
+
assertInput(typeof value.restaurantRegime === "boolean", "seller.restaurantRegime must be a boolean");
|
|
180
|
+
}
|
|
181
|
+
__name(assertSeller, "assertSeller");
|
|
182
|
+
function assertTaxInput(input) {
|
|
183
|
+
assertInput(isRecord(input), "the input must be an object");
|
|
184
|
+
const value = input;
|
|
185
|
+
assertInput(Array.isArray(value.lines) && value.lines.length > 0, "lines must hold at least one line");
|
|
186
|
+
value.lines.forEach(assertLine);
|
|
187
|
+
assertSeller(value.seller);
|
|
188
|
+
assertInput(
|
|
189
|
+
typeof value.destinationIbge === "string" && IBGE_MUNICIPALITY.test(value.destinationIbge),
|
|
190
|
+
"destinationIbge must be a 7-digit IBGE municipality code"
|
|
191
|
+
);
|
|
192
|
+
assertExclusions(value.exclusions);
|
|
193
|
+
assertUniqueIds(input);
|
|
194
|
+
assertInput(isIsoDate(value.supplyDate), "supplyDate must be a YYYY-MM-DD day");
|
|
195
|
+
assertRateTables(value.tables);
|
|
196
|
+
}
|
|
197
|
+
__name(assertTaxInput, "assertTaxInput");
|
|
198
|
+
function assertRateMap(map, keyShape, where) {
|
|
199
|
+
assertTable(isRecord(map), `${where} must be an object`);
|
|
200
|
+
for (const [key, bp] of Object.entries(map)) {
|
|
201
|
+
assertTable(keyShape.test(key), `${where} key "${key}" is not an IBGE code of the right length`);
|
|
202
|
+
assertTable(isBp(bp), `${where}["${key}"] must be integer basis points in 0..10000`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
__name(assertRateMap, "assertRateMap");
|
|
206
|
+
function assertSphere(sphere, mapKey, keyShape, where) {
|
|
207
|
+
assertTable(isRecord(sphere), `${where} must be an object`);
|
|
208
|
+
const value = sphere;
|
|
209
|
+
assertTable(isBp(value.referenceBp), `${where}.referenceBp must be integer basis points in 0..10000`);
|
|
210
|
+
assertRateMap(value[mapKey], keyShape, `${where}.${mapKey}`);
|
|
211
|
+
}
|
|
212
|
+
__name(assertSphere, "assertSphere");
|
|
213
|
+
function assertPeriodFlags(value, where) {
|
|
214
|
+
assertTable(value.status === "LAW" || value.status === "ESTIMATE", `${where}.status must be LAW or ESTIMATE`);
|
|
215
|
+
for (const flag of ["informative", "appliesToSimplesNacional", "taxInsidePrice"]) {
|
|
216
|
+
assertTable(typeof value[flag] === "boolean", `${where}.${flag} must be a boolean`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
__name(assertPeriodFlags, "assertPeriodFlags");
|
|
220
|
+
function assertPeriod(period, index) {
|
|
221
|
+
const where = `periods[${index}]`;
|
|
222
|
+
assertTable(isRecord(period), `${where} must be an object`);
|
|
223
|
+
const value = period;
|
|
224
|
+
assertTable(isIsoDate(value.from), `${where}.from must be a YYYY-MM-DD day`);
|
|
225
|
+
assertTable(
|
|
226
|
+
value.to === null || isIsoDate(value.to) && value.to >= value.from,
|
|
227
|
+
`${where}.to must be null or a day on or after from`
|
|
228
|
+
);
|
|
229
|
+
assertPeriodFlags(value, where);
|
|
230
|
+
assertTable(isBp(value.cbsBp), `${where}.cbsBp must be integer basis points in 0..10000`);
|
|
231
|
+
assertSphere(value.ibsUf, "byUf", IBGE_UF, `${where}.ibsUf`);
|
|
232
|
+
assertSphere(value.ibsMun, "byMunicipality", IBGE_MUNICIPALITY, `${where}.ibsMun`);
|
|
233
|
+
}
|
|
234
|
+
__name(assertPeriod, "assertPeriod");
|
|
235
|
+
function assertClassCode(code, where) {
|
|
236
|
+
assertTable(isRecord(code), `${where} must be an object`);
|
|
237
|
+
const value = code;
|
|
238
|
+
assertTable(isCode(value.cClassTrib) && isCode(value.cstIbsCbs), `${where} must carry cClassTrib and cstIbsCbs`);
|
|
239
|
+
}
|
|
240
|
+
__name(assertClassCode, "assertClassCode");
|
|
241
|
+
function assertTableHeader(value) {
|
|
242
|
+
assertTable(isCode(value.version), "version must be a non-empty string");
|
|
243
|
+
assertTable(isBp(value.gorjetaCapBp), "gorjetaCapBp must be integer basis points in 0..10000");
|
|
244
|
+
const special = value.specialRegimes;
|
|
245
|
+
assertTable(isRecord(special) && isRecord(special.BARES_RESTAURANTES), "specialRegimes.BARES_RESTAURANTES must be an object");
|
|
246
|
+
assertTable(isBp(special.BARES_RESTAURANTES.reductionBp), "specialRegimes.BARES_RESTAURANTES.reductionBp must be basis points");
|
|
247
|
+
assertTable(isRecord(value.exclusionClasses), "exclusionClasses must be an object");
|
|
248
|
+
const classes = value.exclusionClasses;
|
|
249
|
+
assertClassCode(classes.GORJETA, "exclusionClasses.GORJETA");
|
|
250
|
+
assertClassCode(classes.INTERMEDIATION, "exclusionClasses.INTERMEDIATION");
|
|
251
|
+
}
|
|
252
|
+
__name(assertTableHeader, "assertTableHeader");
|
|
253
|
+
function assertRateTables(tables) {
|
|
254
|
+
assertTable(isRecord(tables), "the rate table must be an object");
|
|
255
|
+
const value = tables;
|
|
256
|
+
assertTableHeader(value);
|
|
257
|
+
assertTable(Array.isArray(value.periods) && value.periods.length > 0, "periods must hold at least one period");
|
|
258
|
+
const periods = value.periods;
|
|
259
|
+
periods.forEach(assertPeriod);
|
|
260
|
+
const sorted = [...periods].sort((a, b) => a.from.localeCompare(b.from));
|
|
261
|
+
for (let i = 1; i < sorted.length; i += 1) {
|
|
262
|
+
const previous = sorted[i - 1];
|
|
263
|
+
assertTable(previous.to !== null && previous.to < sorted[i].from, `periods starting ${previous.from} and ${sorted[i].from} overlap`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
__name(assertRateTables, "assertRateTables");
|
|
267
|
+
function periodAt(tables, date) {
|
|
268
|
+
const period = tables.periods.find((p) => p.from <= date && (p.to === null || date <= p.to));
|
|
269
|
+
return period ?? refuse("NO_RATE_PERIOD", `table ${tables.version} has no period covering ${date}`);
|
|
270
|
+
}
|
|
271
|
+
__name(periodAt, "periodAt");
|
|
272
|
+
|
|
273
|
+
// src/engine/plan.ts
|
|
274
|
+
var TREATMENT = {
|
|
275
|
+
REGIME_REGULAR: "FULL",
|
|
276
|
+
SIMPLES_IBS_CBS_REGULAR: "FULL",
|
|
277
|
+
SIMPLES: "INFORMATIVE",
|
|
278
|
+
MEI: "NONE",
|
|
279
|
+
NAO_CONTRIBUINTE: "NONE"
|
|
280
|
+
};
|
|
281
|
+
function isSimplesOptant(regime) {
|
|
282
|
+
return regime === "SIMPLES" || regime === "SIMPLES_IBS_CBS_REGULAR";
|
|
283
|
+
}
|
|
284
|
+
__name(isSimplesOptant, "isSimplesOptant");
|
|
285
|
+
function ownOrReference(rates, key, referenceBp) {
|
|
286
|
+
return Object.hasOwn(rates, key) ? rates[key] : referenceBp;
|
|
287
|
+
}
|
|
288
|
+
__name(ownOrReference, "ownOrReference");
|
|
289
|
+
function ratesAt(period, destinationIbge) {
|
|
290
|
+
return {
|
|
291
|
+
cbsBp: period.cbsBp,
|
|
292
|
+
ibsUfBp: ownOrReference(period.ibsUf.byUf, destinationIbge.slice(0, 2), period.ibsUf.referenceBp),
|
|
293
|
+
ibsMunBp: ownOrReference(period.ibsMun.byMunicipality, destinationIbge, period.ibsMun.referenceBp)
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
__name(ratesAt, "ratesAt");
|
|
297
|
+
function effectiveRates(rates, reductionBp) {
|
|
298
|
+
return [rates.cbsBp, rates.ibsUfBp, rates.ibsMunBp].map((rate) => effectiveRate(rate, reductionBp));
|
|
299
|
+
}
|
|
300
|
+
__name(effectiveRates, "effectiveRates");
|
|
301
|
+
function gorjetaIsExcluded(input) {
|
|
302
|
+
const charge = input.exclusions.serviceCharge;
|
|
303
|
+
if (!charge || !charge.passedToStaff || !input.seller.restaurantRegime) return false;
|
|
304
|
+
const foodAndDrink = input.lines.filter((line) => line.foodAndDrink).reduce((sum, line) => sum + line.valueCents, 0);
|
|
305
|
+
return BigInt(charge.amountCents) * 10000n <= BigInt(foodAndDrink) * BigInt(input.tables.gorjetaCapBp);
|
|
306
|
+
}
|
|
307
|
+
__name(gorjetaIsExcluded, "gorjetaIsExcluded");
|
|
308
|
+
function reductionFor(input, taxClass) {
|
|
309
|
+
if (input.seller.restaurantRegime && taxClass.regimeEspecifico === "BARES_RESTAURANTES") {
|
|
310
|
+
return input.tables.specialRegimes.BARES_RESTAURANTES.reductionBp;
|
|
311
|
+
}
|
|
312
|
+
return taxClass.reductionBp;
|
|
313
|
+
}
|
|
314
|
+
__name(reductionFor, "reductionFor");
|
|
315
|
+
function exclusionClass(code) {
|
|
316
|
+
return { cClassTrib: code.cClassTrib, cstIbsCbs: code.cstIbsCbs, regimeEspecifico: null, reductionBp: 0 };
|
|
317
|
+
}
|
|
318
|
+
__name(exclusionClass, "exclusionClass");
|
|
319
|
+
function exclusionDrafts(input, gorjetaExcluded) {
|
|
320
|
+
const drafts = [];
|
|
321
|
+
const charge = input.exclusions.serviceCharge;
|
|
322
|
+
if (charge) {
|
|
323
|
+
drafts.push({
|
|
324
|
+
lineId: charge.id,
|
|
325
|
+
source: gorjetaExcluded ? "GORJETA_EXCLUDED" : "SERVICE_CHARGE",
|
|
326
|
+
quantity: 1,
|
|
327
|
+
valueCents: charge.amountCents,
|
|
328
|
+
taxClass: gorjetaExcluded ? exclusionClass(input.tables.exclusionClasses.GORJETA) : charge.taxClass,
|
|
329
|
+
excluded: gorjetaExcluded
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
const retained = input.exclusions.retainedIntermediation;
|
|
333
|
+
if (retained) {
|
|
334
|
+
if (!input.seller.restaurantRegime) {
|
|
335
|
+
throw new FiscalEngineError(
|
|
336
|
+
"INVALID_INPUT",
|
|
337
|
+
"a retained intermediation is excluded only when seller.restaurantRegime is set (art. 274 sole paragraph, II); otherwise the amount belongs in the lines"
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
drafts.push({
|
|
341
|
+
lineId: retained.id,
|
|
342
|
+
source: "INTERMEDIATION_EXCLUDED",
|
|
343
|
+
quantity: 1,
|
|
344
|
+
valueCents: retained.amountCents,
|
|
345
|
+
taxClass: exclusionClass(input.tables.exclusionClasses.INTERMEDIATION),
|
|
346
|
+
excluded: true
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
return drafts;
|
|
350
|
+
}
|
|
351
|
+
__name(exclusionDrafts, "exclusionDrafts");
|
|
352
|
+
function planLine(draft, input, context) {
|
|
353
|
+
const reductionBp = draft.excluded ? 0 : reductionFor(input, draft.taxClass);
|
|
354
|
+
const taxed = !draft.excluded && context.ratesApply;
|
|
355
|
+
let taxBaseCents = draft.excluded ? 0 : draft.valueCents;
|
|
356
|
+
if (taxed && context.period.taxInsidePrice) {
|
|
357
|
+
taxBaseCents = splitTaxInclusivePrice(draft.valueCents, effectiveRates(context.rates, reductionBp)).baseCents;
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
lineId: draft.lineId,
|
|
361
|
+
source: draft.source,
|
|
362
|
+
quantity: draft.quantity,
|
|
363
|
+
valueCents: draft.valueCents,
|
|
364
|
+
taxClass: draft.taxClass,
|
|
365
|
+
reductionBp,
|
|
366
|
+
taxed,
|
|
367
|
+
taxBaseCents
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
__name(planLine, "planLine");
|
|
371
|
+
function planTax(input) {
|
|
372
|
+
assertTaxInput(input);
|
|
373
|
+
const { regime, regimeEffectiveFrom } = input.seller;
|
|
374
|
+
if (input.supplyDate < regimeEffectiveFrom) {
|
|
375
|
+
throw new FiscalEngineError(
|
|
376
|
+
"REGIME_NOT_IN_EFFECT",
|
|
377
|
+
`the supply on ${input.supplyDate} precedes the seller's ${regime} regime, effective ${regimeEffectiveFrom}`
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
const period = periodAt(input.tables, input.supplyDate);
|
|
381
|
+
const rates = ratesAt(period, input.destinationIbge);
|
|
382
|
+
const treatment = TREATMENT[regime];
|
|
383
|
+
const ratesApply = treatment !== "NONE" && (!isSimplesOptant(regime) || period.appliesToSimplesNacional);
|
|
384
|
+
const gorjetaExcluded = gorjetaIsExcluded(input);
|
|
385
|
+
const drafts = [
|
|
386
|
+
...input.lines.map((line) => ({
|
|
387
|
+
lineId: line.id,
|
|
388
|
+
source: "LINE",
|
|
389
|
+
quantity: line.quantity,
|
|
390
|
+
valueCents: line.valueCents,
|
|
391
|
+
taxClass: line.taxClass,
|
|
392
|
+
excluded: false
|
|
393
|
+
})),
|
|
394
|
+
...exclusionDrafts(input, gorjetaExcluded)
|
|
395
|
+
];
|
|
396
|
+
return {
|
|
397
|
+
input,
|
|
398
|
+
period,
|
|
399
|
+
rates,
|
|
400
|
+
treatment,
|
|
401
|
+
informative: treatment === "INFORMATIVE" || treatment === "FULL" && period.informative,
|
|
402
|
+
lines: drafts.map((draft) => planLine(draft, input, { ratesApply, period, rates })),
|
|
403
|
+
gorjetaExcludedCents: gorjetaExcluded ? input.exclusions.serviceCharge.amountCents : 0,
|
|
404
|
+
intermediationExcludedCents: input.exclusions.retainedIntermediation?.amountCents ?? 0
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
__name(planTax, "planTax");
|
|
408
|
+
function zeroFigures(line) {
|
|
409
|
+
return {
|
|
410
|
+
reductionBp: line.reductionBp,
|
|
411
|
+
cbsRateBp: 0,
|
|
412
|
+
cbsCents: 0,
|
|
413
|
+
ibsUfRateBp: 0,
|
|
414
|
+
ibsUfCents: 0,
|
|
415
|
+
ibsMunRateBp: 0,
|
|
416
|
+
ibsMunCents: 0
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
__name(zeroFigures, "zeroFigures");
|
|
420
|
+
function lineTax(plan, line, figures) {
|
|
421
|
+
const taxCents = figures.cbsCents + figures.ibsUfCents + figures.ibsMunCents;
|
|
422
|
+
const netValueCents = plan.period.taxInsidePrice ? line.valueCents - taxCents : null;
|
|
423
|
+
return {
|
|
424
|
+
lineId: line.lineId,
|
|
425
|
+
source: line.source,
|
|
426
|
+
quantity: line.quantity,
|
|
427
|
+
valueCents: line.valueCents,
|
|
428
|
+
taxBaseCents: line.taxBaseCents,
|
|
429
|
+
taxClass: line.taxClass,
|
|
430
|
+
...figures,
|
|
431
|
+
taxCents,
|
|
432
|
+
netValueCents,
|
|
433
|
+
netUnitValue: netValueCents === null ? null : netUnitValue(netValueCents, line.quantity)
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
__name(lineTax, "lineTax");
|
|
437
|
+
function assembleBreakdown(plan, figures, tablesVersion) {
|
|
438
|
+
const lines = plan.lines.map((line, index) => lineTax(plan, line, figures[index]));
|
|
439
|
+
const sum = /* @__PURE__ */ __name((pick) => lines.reduce((total, line) => total + pick(line), 0), "sum");
|
|
440
|
+
const cbsCents = sum((line) => line.cbsCents);
|
|
441
|
+
const ibsUfCents = sum((line) => line.ibsUfCents);
|
|
442
|
+
const ibsMunCents = sum((line) => line.ibsMunCents);
|
|
443
|
+
return {
|
|
444
|
+
lines,
|
|
445
|
+
valueCents: sum((line) => line.valueCents),
|
|
446
|
+
taxBaseCents: sum((line) => line.taxBaseCents),
|
|
447
|
+
cbsCents,
|
|
448
|
+
ibsUfCents,
|
|
449
|
+
ibsMunCents,
|
|
450
|
+
ibsCents: ibsUfCents + ibsMunCents,
|
|
451
|
+
totalTaxCents: cbsCents + ibsUfCents + ibsMunCents,
|
|
452
|
+
netValueCents: plan.period.taxInsidePrice ? sum((line) => line.netValueCents ?? 0) : null,
|
|
453
|
+
gorjetaExcludedCents: plan.gorjetaExcludedCents,
|
|
454
|
+
intermediationExcludedCents: plan.intermediationExcludedCents,
|
|
455
|
+
regime: plan.input.seller.regime,
|
|
456
|
+
treatment: plan.treatment,
|
|
457
|
+
informative: plan.informative,
|
|
458
|
+
provisional: plan.period.status === "ESTIMATE",
|
|
459
|
+
taxInsidePrice: plan.period.taxInsidePrice,
|
|
460
|
+
supplyDate: plan.input.supplyDate,
|
|
461
|
+
destinationIbge: plan.input.destinationIbge,
|
|
462
|
+
tablesVersion
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
__name(assembleBreakdown, "assembleBreakdown");
|
|
466
|
+
|
|
467
|
+
// src/engine/compute.ts
|
|
468
|
+
function tableFigures(plan, line) {
|
|
469
|
+
if (!line.taxed) return zeroFigures(line);
|
|
470
|
+
const { cbsBp, ibsUfBp, ibsMunBp } = plan.rates;
|
|
471
|
+
return {
|
|
472
|
+
reductionBp: line.reductionBp,
|
|
473
|
+
cbsRateBp: cbsBp,
|
|
474
|
+
cbsCents: roundTaxCents(line.taxBaseCents, cbsBp, line.reductionBp),
|
|
475
|
+
ibsUfRateBp: ibsUfBp,
|
|
476
|
+
ibsUfCents: roundTaxCents(line.taxBaseCents, ibsUfBp, line.reductionBp),
|
|
477
|
+
ibsMunRateBp: ibsMunBp,
|
|
478
|
+
ibsMunCents: roundTaxCents(line.taxBaseCents, ibsMunBp, line.reductionBp)
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
__name(tableFigures, "tableFigures");
|
|
482
|
+
function computeTax(input) {
|
|
483
|
+
const plan = planTax(input);
|
|
484
|
+
return assembleBreakdown(
|
|
485
|
+
plan,
|
|
486
|
+
plan.lines.map((line) => tableFigures(plan, line)),
|
|
487
|
+
input.tables.version
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
__name(computeTax, "computeTax");
|
|
491
|
+
|
|
492
|
+
// src/engine/oracle.ts
|
|
493
|
+
var ANSWER_FIELDS = ["reductionBp", "cbsRateBp", "cbsCents", "ibsUfRateBp", "ibsUfCents", "ibsMunRateBp", "ibsMunCents"];
|
|
494
|
+
function assertAnswer(answer, lineId) {
|
|
495
|
+
for (const field of ANSWER_FIELDS) {
|
|
496
|
+
const value = answer?.[field];
|
|
497
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) {
|
|
498
|
+
throw new FiscalEngineError(
|
|
499
|
+
"INVALID_ORACLE_ANSWER",
|
|
500
|
+
`the calculator answered ${String(value)} for ${field} of line "${lineId}" \u2014 expected integer cents or basis points`
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return answer;
|
|
505
|
+
}
|
|
506
|
+
__name(assertAnswer, "assertAnswer");
|
|
507
|
+
function requestFor(input, taxClass, taxBaseCents, quantity) {
|
|
508
|
+
return {
|
|
509
|
+
supplyDate: input.supplyDate,
|
|
510
|
+
destinationIbge: input.destinationIbge,
|
|
511
|
+
cClassTrib: taxClass.cClassTrib,
|
|
512
|
+
cstIbsCbs: taxClass.cstIbsCbs,
|
|
513
|
+
taxBaseCents,
|
|
514
|
+
quantity
|
|
515
|
+
};
|
|
516
|
+
}
|
|
517
|
+
__name(requestFor, "requestFor");
|
|
518
|
+
async function computeTaxWithOracle(input, calculator) {
|
|
519
|
+
const plan = planTax(input);
|
|
520
|
+
const figures = [];
|
|
521
|
+
for (const line of plan.lines) {
|
|
522
|
+
if (!line.taxed) {
|
|
523
|
+
figures.push(zeroFigures(line));
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
const answer = await calculator.calculateItem(requestFor(input, line.taxClass, line.taxBaseCents, line.quantity));
|
|
527
|
+
figures.push(assertAnswer(answer, line.lineId));
|
|
528
|
+
}
|
|
529
|
+
return assembleBreakdown(plan, figures, `${input.tables.version}+oracle:${calculator.version}`);
|
|
530
|
+
}
|
|
531
|
+
__name(computeTaxWithOracle, "computeTaxWithOracle");
|
|
532
|
+
|
|
533
|
+
// src/engine/compare.ts
|
|
534
|
+
var LINE_FIELDS = [
|
|
535
|
+
"valueCents",
|
|
536
|
+
"taxBaseCents",
|
|
537
|
+
"reductionBp",
|
|
538
|
+
"cbsRateBp",
|
|
539
|
+
"cbsCents",
|
|
540
|
+
"ibsUfRateBp",
|
|
541
|
+
"ibsUfCents",
|
|
542
|
+
"ibsMunRateBp",
|
|
543
|
+
"ibsMunCents",
|
|
544
|
+
"netValueCents"
|
|
545
|
+
];
|
|
546
|
+
var ORDER_FIELDS = [
|
|
547
|
+
"taxBaseCents",
|
|
548
|
+
"cbsCents",
|
|
549
|
+
"ibsUfCents",
|
|
550
|
+
"ibsMunCents",
|
|
551
|
+
"totalTaxCents",
|
|
552
|
+
"netValueCents",
|
|
553
|
+
"gorjetaExcludedCents",
|
|
554
|
+
"intermediationExcludedCents",
|
|
555
|
+
"treatment",
|
|
556
|
+
"informative"
|
|
557
|
+
];
|
|
558
|
+
function lineDisagreements(left, right) {
|
|
559
|
+
if (!right) return [{ lineId: left.lineId, field: "line", left: left.lineId, right: void 0 }];
|
|
560
|
+
return LINE_FIELDS.filter((field) => left[field] !== right[field]).map((field) => ({
|
|
561
|
+
lineId: left.lineId,
|
|
562
|
+
field,
|
|
563
|
+
left: left[field],
|
|
564
|
+
right: right[field]
|
|
565
|
+
}));
|
|
566
|
+
}
|
|
567
|
+
__name(lineDisagreements, "lineDisagreements");
|
|
568
|
+
function compareBreakdowns(left, right) {
|
|
569
|
+
const rightById = new Map(right.lines.map((line) => [line.lineId, line]));
|
|
570
|
+
const lines = left.lines.flatMap((line) => lineDisagreements(line, rightById.get(line.lineId)));
|
|
571
|
+
const leftIds = new Set(left.lines.map((line) => line.lineId));
|
|
572
|
+
const extra = right.lines.filter((line) => !leftIds.has(line.lineId)).map((line) => ({ lineId: line.lineId, field: "line", left: void 0, right: line.lineId }));
|
|
573
|
+
const order = ORDER_FIELDS.filter((field) => left[field] !== right[field]).map((field) => ({
|
|
574
|
+
lineId: null,
|
|
575
|
+
field,
|
|
576
|
+
left: left[field],
|
|
577
|
+
right: right[field]
|
|
578
|
+
}));
|
|
579
|
+
return [...lines, ...extra, ...order];
|
|
580
|
+
}
|
|
581
|
+
__name(compareBreakdowns, "compareBreakdowns");
|
|
582
|
+
export {
|
|
583
|
+
FiscalEngineError,
|
|
584
|
+
assertRateTables,
|
|
585
|
+
compareBreakdowns,
|
|
586
|
+
computeTax,
|
|
587
|
+
computeTaxWithOracle,
|
|
588
|
+
ratesAt,
|
|
589
|
+
roundTaxCents
|
|
590
|
+
};
|
|
591
|
+
//# sourceMappingURL=index.js.map
|