@adatechnology/purchase-contracts 0.0.0-stage → 0.1.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 +16 -2
- package/dist/index.cjs +1548 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2005 -0
- package/dist/index.d.ts +2005 -0
- package/dist/index.js +1420 -0
- package/dist/index.js.map +1 -0
- package/package.json +34 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1548 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
DEFAULT_PURCHASE_SCOPES: () => DEFAULT_PURCHASE_SCOPES,
|
|
25
|
+
DISPATCH_CHANNEL: () => DISPATCH_CHANNEL,
|
|
26
|
+
DISPATCH_CHANNELS: () => DISPATCH_CHANNELS,
|
|
27
|
+
DIVERGENCE: () => DIVERGENCE,
|
|
28
|
+
DispatchUnavailableError: () => DispatchUnavailableError,
|
|
29
|
+
INTEGRATION_STATUS: () => INTEGRATION_STATUS,
|
|
30
|
+
InstallmentsMismatchError: () => InstallmentsMismatchError,
|
|
31
|
+
InvalidOrderTransitionError: () => InvalidOrderTransitionError,
|
|
32
|
+
InvoiceAlreadyReceivedError: () => InvoiceAlreadyReceivedError,
|
|
33
|
+
MONEY_SCALE: () => MONEY_SCALE,
|
|
34
|
+
NfeInvalidError: () => NfeInvalidError,
|
|
35
|
+
NfeParseError: () => NfeParseError,
|
|
36
|
+
OPEN_ORDER_STATUSES: () => OPEN_ORDER_STATUSES,
|
|
37
|
+
ORDER_ACTION: () => ORDER_ACTION,
|
|
38
|
+
ORDER_SORTS: () => ORDER_SORTS,
|
|
39
|
+
ORDER_STATUS: () => ORDER_STATUS,
|
|
40
|
+
ORDER_STATUSES: () => ORDER_STATUSES,
|
|
41
|
+
OrderLineNotFoundError: () => OrderLineNotFoundError,
|
|
42
|
+
OrderNotFoundError: () => OrderNotFoundError,
|
|
43
|
+
PAYMENT_STATUS: () => PAYMENT_STATUS,
|
|
44
|
+
PURCHASE_ERROR_CODES: () => PURCHASE_ERROR_CODES,
|
|
45
|
+
PURCHASE_LIMITS: () => PURCHASE_LIMITS,
|
|
46
|
+
PurchaseConfigMissingError: () => PurchaseConfigMissingError,
|
|
47
|
+
PurchaseDisabledError: () => PurchaseDisabledError,
|
|
48
|
+
PurchaseError: () => PurchaseError,
|
|
49
|
+
PurchaseItemNotFoundError: () => PurchaseItemNotFoundError,
|
|
50
|
+
PurchaseUnauthenticatedError: () => PurchaseUnauthenticatedError,
|
|
51
|
+
REPORT_GROUP: () => REPORT_GROUP,
|
|
52
|
+
REPORT_GROUPS: () => REPORT_GROUPS,
|
|
53
|
+
ReceiptNotFoundError: () => ReceiptNotFoundError,
|
|
54
|
+
ReportRangeTooLongError: () => ReportRangeTooLongError,
|
|
55
|
+
SORT_ORDER: () => SORT_ORDER,
|
|
56
|
+
SUPPLIER_SORTS: () => SUPPLIER_SORTS,
|
|
57
|
+
SelfApprovalError: () => SelfApprovalError,
|
|
58
|
+
SupplierDocumentTakenError: () => SupplierDocumentTakenError,
|
|
59
|
+
SupplierInUseError: () => SupplierInUseError,
|
|
60
|
+
SupplierInactiveError: () => SupplierInactiveError,
|
|
61
|
+
SupplierItemNotFoundError: () => SupplierItemNotFoundError,
|
|
62
|
+
SupplierItemTakenError: () => SupplierItemTakenError,
|
|
63
|
+
SupplierNoContactError: () => SupplierNoContactError,
|
|
64
|
+
SupplierNotFoundError: () => SupplierNotFoundError,
|
|
65
|
+
UNIT_FACTOR_ONE: () => UNIT_FACTOR_ONE,
|
|
66
|
+
ZERO: () => import_recipe_contracts.ZERO,
|
|
67
|
+
add: () => import_recipe_contracts.add,
|
|
68
|
+
addDays: () => addDays,
|
|
69
|
+
allocateExtras: () => allocateExtras,
|
|
70
|
+
allowedActions: () => allowedActions,
|
|
71
|
+
buildInstallments: () => buildInstallments,
|
|
72
|
+
buildOrderMessage: () => buildOrderMessage,
|
|
73
|
+
buildSuggestion: () => buildSuggestion,
|
|
74
|
+
canPerform: () => canPerform,
|
|
75
|
+
centsOf: () => centsOf,
|
|
76
|
+
civilDateIn: () => civilDateIn,
|
|
77
|
+
compare: () => import_recipe_contracts.compare,
|
|
78
|
+
computeTotals: () => computeTotals,
|
|
79
|
+
createOrderSchema: () => createOrderSchema,
|
|
80
|
+
createSupplierItemSchema: () => createSupplierItemSchema,
|
|
81
|
+
createSupplierSchema: () => createSupplierSchema,
|
|
82
|
+
divergenceOf: () => divergenceOf,
|
|
83
|
+
exportFormatSchema: () => exportFormatSchema,
|
|
84
|
+
formatMoneyPt: () => formatMoneyPt,
|
|
85
|
+
formatPaymentTerms: () => formatPaymentTerms,
|
|
86
|
+
formatQuantityPt: () => formatQuantityPt,
|
|
87
|
+
isCivilDate: () => isCivilDate,
|
|
88
|
+
isOrderLate: () => isOrderLate,
|
|
89
|
+
itemRefsLookupSchema: () => itemRefsLookupSchema,
|
|
90
|
+
landedUnitCost: () => landedUnitCost,
|
|
91
|
+
lineTotal: () => lineTotal,
|
|
92
|
+
listOrdersQuerySchema: () => listOrdersQuerySchema,
|
|
93
|
+
listReceiptsQuerySchema: () => listReceiptsQuerySchema,
|
|
94
|
+
listSupplierItemsQuerySchema: () => listSupplierItemsQuerySchema,
|
|
95
|
+
listSuppliersQuerySchema: () => listSuppliersQuerySchema,
|
|
96
|
+
money: () => money,
|
|
97
|
+
moneySchema: () => moneySchema,
|
|
98
|
+
multiply: () => import_recipe_contracts.multiply,
|
|
99
|
+
nfePreviewSchema: () => nfePreviewSchema,
|
|
100
|
+
optionalReasonSchema: () => optionalReasonSchema,
|
|
101
|
+
orderLineInputSchema: () => orderLineInputSchema,
|
|
102
|
+
ordersFromSuggestionSchema: () => ordersFromSuggestionSchema,
|
|
103
|
+
packsFor: () => packsFor,
|
|
104
|
+
parseNfeXml: () => parseNfeXml,
|
|
105
|
+
parsePaymentTerms: () => parsePaymentTerms,
|
|
106
|
+
pendingQuantity: () => pendingQuantity,
|
|
107
|
+
priceHistoryQuerySchema: () => priceHistoryQuerySchema,
|
|
108
|
+
quantityText: () => quantityText,
|
|
109
|
+
reasonSchema: () => reasonSchema,
|
|
110
|
+
receiptLineInputSchema: () => receiptLineInputSchema,
|
|
111
|
+
receiveSchema: () => receiveSchema,
|
|
112
|
+
requiresApproval: () => requiresApproval,
|
|
113
|
+
sendOrderSchema: () => sendOrderSchema,
|
|
114
|
+
shortageOf: () => shortageOf,
|
|
115
|
+
spendReportQuerySchema: () => spendReportQuerySchema,
|
|
116
|
+
statusAfterReceipt: () => statusAfterReceipt,
|
|
117
|
+
statusAfterSend: () => statusAfterSend,
|
|
118
|
+
stockQuantityOf: () => stockQuantityOf,
|
|
119
|
+
subtract: () => import_recipe_contracts.subtract,
|
|
120
|
+
suggestionQuerySchema: () => suggestionQuerySchema,
|
|
121
|
+
supplierLookupSchema: () => supplierLookupSchema,
|
|
122
|
+
supplierPerformanceQuerySchema: () => supplierPerformanceQuerySchema,
|
|
123
|
+
toStockQuantity: () => toStockQuantity,
|
|
124
|
+
updateOrderSchema: () => updateOrderSchema,
|
|
125
|
+
updateSupplierItemSchema: () => updateSupplierItemSchema,
|
|
126
|
+
updateSupplierSchema: () => updateSupplierSchema,
|
|
127
|
+
weightedAverageCost: () => weightedAverageCost
|
|
128
|
+
});
|
|
129
|
+
module.exports = __toCommonJS(index_exports);
|
|
130
|
+
|
|
131
|
+
// src/purchase.types.ts
|
|
132
|
+
var ORDER_STATUS = {
|
|
133
|
+
DRAFT: "draft",
|
|
134
|
+
PENDING_APPROVAL: "pending_approval",
|
|
135
|
+
APPROVED: "approved",
|
|
136
|
+
SENT: "sent",
|
|
137
|
+
PARTIALLY_RECEIVED: "partially_received",
|
|
138
|
+
RECEIVED: "received",
|
|
139
|
+
CLOSED: "closed",
|
|
140
|
+
CANCELED: "canceled"
|
|
141
|
+
};
|
|
142
|
+
var ORDER_STATUSES = Object.values(ORDER_STATUS);
|
|
143
|
+
var OPEN_ORDER_STATUSES = [
|
|
144
|
+
ORDER_STATUS.APPROVED,
|
|
145
|
+
ORDER_STATUS.SENT,
|
|
146
|
+
ORDER_STATUS.PARTIALLY_RECEIVED
|
|
147
|
+
];
|
|
148
|
+
var DISPATCH_CHANNEL = {
|
|
149
|
+
WHATSAPP: "whatsapp",
|
|
150
|
+
EMAIL: "email",
|
|
151
|
+
MANUAL: "manual"
|
|
152
|
+
};
|
|
153
|
+
var DISPATCH_CHANNELS = Object.values(DISPATCH_CHANNEL);
|
|
154
|
+
var DIVERGENCE = {
|
|
155
|
+
NONE: "none",
|
|
156
|
+
QUANTITY: "quantity",
|
|
157
|
+
PRICE: "price",
|
|
158
|
+
BOTH: "both"
|
|
159
|
+
};
|
|
160
|
+
var INTEGRATION_STATUS = {
|
|
161
|
+
SKIPPED: "skipped",
|
|
162
|
+
DONE: "done",
|
|
163
|
+
FAILED: "failed"
|
|
164
|
+
};
|
|
165
|
+
var PAYMENT_STATUS = {
|
|
166
|
+
NONE: "none",
|
|
167
|
+
OPEN: "open",
|
|
168
|
+
PARTIAL: "partial",
|
|
169
|
+
PAID: "paid"
|
|
170
|
+
};
|
|
171
|
+
var SORT_ORDER = {
|
|
172
|
+
ASC: "asc",
|
|
173
|
+
DESC: "desc"
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// src/schemas.ts
|
|
177
|
+
var import_recipe_contracts3 = require("@adatechnology/recipe-contracts");
|
|
178
|
+
var import_zod = require("zod");
|
|
179
|
+
|
|
180
|
+
// src/rules/schedule.ts
|
|
181
|
+
var import_recipe_contracts2 = require("@adatechnology/recipe-contracts");
|
|
182
|
+
|
|
183
|
+
// src/rules/money.ts
|
|
184
|
+
var import_recipe_contracts = require("@adatechnology/recipe-contracts");
|
|
185
|
+
var MONEY_SCALE = 2;
|
|
186
|
+
function money(value) {
|
|
187
|
+
return (0, import_recipe_contracts.formatDecimal)(value, MONEY_SCALE);
|
|
188
|
+
}
|
|
189
|
+
__name(money, "money");
|
|
190
|
+
function quantityText(value) {
|
|
191
|
+
return (0, import_recipe_contracts.formatDecimal)(value);
|
|
192
|
+
}
|
|
193
|
+
__name(quantityText, "quantityText");
|
|
194
|
+
function sum(values) {
|
|
195
|
+
return values.reduce((total, value) => (0, import_recipe_contracts.add)(total, value), import_recipe_contracts.ZERO);
|
|
196
|
+
}
|
|
197
|
+
__name(sum, "sum");
|
|
198
|
+
function lineTotal(quantity, unitPrice) {
|
|
199
|
+
return (0, import_recipe_contracts.parseDecimal)(money((0, import_recipe_contracts.multiply)((0, import_recipe_contracts.parseDecimal)(quantity), (0, import_recipe_contracts.parseDecimal)(unitPrice))));
|
|
200
|
+
}
|
|
201
|
+
__name(lineTotal, "lineTotal");
|
|
202
|
+
function centsOf(value) {
|
|
203
|
+
const rounded = (0, import_recipe_contracts.roundToScale)(value, MONEY_SCALE);
|
|
204
|
+
return rounded.numerator * 100n / rounded.denominator;
|
|
205
|
+
}
|
|
206
|
+
__name(centsOf, "centsOf");
|
|
207
|
+
function fromCentsBig(cents) {
|
|
208
|
+
return (0, import_recipe_contracts.rational)(cents, 100n);
|
|
209
|
+
}
|
|
210
|
+
__name(fromCentsBig, "fromCentsBig");
|
|
211
|
+
function isNegative(value) {
|
|
212
|
+
return (0, import_recipe_contracts.compare)(value, import_recipe_contracts.ZERO) < 0;
|
|
213
|
+
}
|
|
214
|
+
__name(isNegative, "isNegative");
|
|
215
|
+
function clampToZero(value) {
|
|
216
|
+
return isNegative(value) ? import_recipe_contracts.ZERO : value;
|
|
217
|
+
}
|
|
218
|
+
__name(clampToZero, "clampToZero");
|
|
219
|
+
|
|
220
|
+
// src/rules/schedule.ts
|
|
221
|
+
var DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
|
|
222
|
+
function isCivilDate(value) {
|
|
223
|
+
if (!DATE_PATTERN.test(value)) return false;
|
|
224
|
+
const date = /* @__PURE__ */ new Date(`${value}T00:00:00Z`);
|
|
225
|
+
return !Number.isNaN(date.getTime()) && date.toISOString().slice(0, 10) === value;
|
|
226
|
+
}
|
|
227
|
+
__name(isCivilDate, "isCivilDate");
|
|
228
|
+
function addDays(date, days) {
|
|
229
|
+
const base = /* @__PURE__ */ new Date(`${date}T00:00:00Z`);
|
|
230
|
+
base.setUTCDate(base.getUTCDate() + days);
|
|
231
|
+
return base.toISOString().slice(0, 10);
|
|
232
|
+
}
|
|
233
|
+
__name(addDays, "addDays");
|
|
234
|
+
function civilDateIn(instant, timeZone) {
|
|
235
|
+
const parts = new Intl.DateTimeFormat("en-CA", {
|
|
236
|
+
timeZone,
|
|
237
|
+
year: "numeric",
|
|
238
|
+
month: "2-digit",
|
|
239
|
+
day: "2-digit"
|
|
240
|
+
}).formatToParts(instant);
|
|
241
|
+
const get = /* @__PURE__ */ __name((type) => parts.find((part) => part.type === type)?.value ?? "", "get");
|
|
242
|
+
return `${get("year")}-${get("month")}-${get("day")}`;
|
|
243
|
+
}
|
|
244
|
+
__name(civilDateIn, "civilDateIn");
|
|
245
|
+
function buildInstallments(params) {
|
|
246
|
+
const terms = params.termDays.length > 0 ? [
|
|
247
|
+
...params.termDays
|
|
248
|
+
].sort((a, b) => a - b) : [
|
|
249
|
+
0
|
|
250
|
+
];
|
|
251
|
+
const totalCents = centsOf((0, import_recipe_contracts2.parseDecimal)(params.total));
|
|
252
|
+
const count = BigInt(terms.length);
|
|
253
|
+
const base = totalCents / count;
|
|
254
|
+
const first = totalCents - base * (count - 1n);
|
|
255
|
+
return terms.map((days, index) => ({
|
|
256
|
+
number: index + 1,
|
|
257
|
+
dueDate: addDays(params.baseDate, days),
|
|
258
|
+
amount: money(fromCentsBig(index === 0 ? first : base))
|
|
259
|
+
}));
|
|
260
|
+
}
|
|
261
|
+
__name(buildInstallments, "buildInstallments");
|
|
262
|
+
function parsePaymentTerms(text) {
|
|
263
|
+
const clean = text.trim().toLowerCase();
|
|
264
|
+
if (clean === "" || clean === "a vista" || clean === "\xE0 vista" || clean === "0") return [
|
|
265
|
+
0
|
|
266
|
+
];
|
|
267
|
+
const parts = clean.split(/[\s/,;+-]+/).filter(Boolean);
|
|
268
|
+
const days = parts.map((part) => Number(part));
|
|
269
|
+
if (days.some((day) => !Number.isInteger(day) || day < 0 || day > 365)) return null;
|
|
270
|
+
return [
|
|
271
|
+
...new Set(days)
|
|
272
|
+
].sort((a, b) => a - b);
|
|
273
|
+
}
|
|
274
|
+
__name(parsePaymentTerms, "parsePaymentTerms");
|
|
275
|
+
function formatPaymentTerms(days) {
|
|
276
|
+
if (days.length === 0 || days.length === 1 && days[0] === 0) return "\xE0 vista";
|
|
277
|
+
return days.join("/");
|
|
278
|
+
}
|
|
279
|
+
__name(formatPaymentTerms, "formatPaymentTerms");
|
|
280
|
+
|
|
281
|
+
// src/schemas.ts
|
|
282
|
+
var PURCHASE_LIMITS = {
|
|
283
|
+
NAME: 160,
|
|
284
|
+
DOCUMENT: 20,
|
|
285
|
+
CONTACT: 120,
|
|
286
|
+
EMAIL: 254,
|
|
287
|
+
PHONE: 30,
|
|
288
|
+
SKU: 100,
|
|
289
|
+
UNIT: 12,
|
|
290
|
+
ITEM_REF: 128,
|
|
291
|
+
NOTE: 2e3,
|
|
292
|
+
REASON: 500,
|
|
293
|
+
BATCH_CODE: 100,
|
|
294
|
+
INVOICE_NUMBER: 30,
|
|
295
|
+
SEARCH: 100,
|
|
296
|
+
PAGE_SIZE: 100,
|
|
297
|
+
ORDER_LINES: 300,
|
|
298
|
+
TERMS: 24,
|
|
299
|
+
TERM_DAYS: 365,
|
|
300
|
+
LEAD_DAYS: 365,
|
|
301
|
+
PRIORITY: 1e3,
|
|
302
|
+
LOOKUP: 500,
|
|
303
|
+
IDEMPOTENCY_KEY: 128,
|
|
304
|
+
XML_BYTES: 5e6,
|
|
305
|
+
REPORT_DAYS: 400
|
|
306
|
+
};
|
|
307
|
+
var unitSchema = import_zod.z.enum(import_recipe_contracts3.RECIPE_UNITS);
|
|
308
|
+
var moneySchema = import_zod.z.string().trim().regex(/^\d{1,12}(\.\d{1,2})?$/, "Valor com at\xE9 duas casas.");
|
|
309
|
+
var civilDate = import_zod.z.string().refine(isCivilDate, "Data no formato aaaa-mm-dd.");
|
|
310
|
+
var pageShape = {
|
|
311
|
+
page: import_zod.z.coerce.number().int().min(1).default(1),
|
|
312
|
+
pageSize: import_zod.z.coerce.number().int().min(1).max(PURCHASE_LIMITS.PAGE_SIZE).default(20)
|
|
313
|
+
};
|
|
314
|
+
var boolText = import_zod.z.enum([
|
|
315
|
+
"true",
|
|
316
|
+
"false"
|
|
317
|
+
]).transform((value) => value === "true").optional();
|
|
318
|
+
function csvOf(allowed) {
|
|
319
|
+
return import_zod.z.string().trim().transform((value) => value.split(",").map((item) => item.trim()).filter(Boolean)).refine((values) => values.every((value) => allowed.includes(value)), "Valor inv\xE1lido.").transform((values) => values).optional();
|
|
320
|
+
}
|
|
321
|
+
__name(csvOf, "csvOf");
|
|
322
|
+
function emptyToNull(schema) {
|
|
323
|
+
return {
|
|
324
|
+
create: schema.nullable().optional().transform((value) => value ? value : null),
|
|
325
|
+
update: schema.nullable().optional().transform((value) => value === void 0 ? void 0 : value ? value : null)
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
__name(emptyToNull, "emptyToNull");
|
|
329
|
+
var nullableText = /* @__PURE__ */ __name((max) => emptyToNull(import_zod.z.string().trim().max(max)), "nullableText");
|
|
330
|
+
var documentField = emptyToNull(import_zod.z.string().trim().max(PURCHASE_LIMITS.DOCUMENT + 10).transform((value) => value.replace(/[^0-9A-Za-z]/g, "").toUpperCase()).refine((value) => value === "" || value.length >= 11 && value.length <= PURCHASE_LIMITS.DOCUMENT, {
|
|
331
|
+
message: "Documento com 11 a 20 caracteres (CPF, CNPJ ou equivalente)."
|
|
332
|
+
}));
|
|
333
|
+
var phoneField = emptyToNull(import_zod.z.string().trim().max(PURCHASE_LIMITS.PHONE).refine((value) => value === "" || /^\+?[0-9 ()-]{8,}$/.test(value), {
|
|
334
|
+
message: "Telefone inv\xE1lido."
|
|
335
|
+
}));
|
|
336
|
+
var emailField = emptyToNull(import_zod.z.union([
|
|
337
|
+
import_zod.z.literal(""),
|
|
338
|
+
import_zod.z.string().trim().toLowerCase().email().max(PURCHASE_LIMITS.EMAIL)
|
|
339
|
+
]));
|
|
340
|
+
var termDaysSchema = import_zod.z.array(import_zod.z.number().int().min(0).max(PURCHASE_LIMITS.TERM_DAYS)).max(PURCHASE_LIMITS.TERMS).transform((days) => [
|
|
341
|
+
...new Set(days)
|
|
342
|
+
].sort((a, b) => a - b));
|
|
343
|
+
var weekdaysSchema = import_zod.z.array(import_zod.z.number().int().min(0).max(6)).max(7).transform((days) => [
|
|
344
|
+
...new Set(days)
|
|
345
|
+
].sort((a, b) => a - b));
|
|
346
|
+
var supplierShape = {
|
|
347
|
+
name: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.NAME),
|
|
348
|
+
document: documentField.create,
|
|
349
|
+
contactName: nullableText(PURCHASE_LIMITS.CONTACT).create,
|
|
350
|
+
email: emailField.create,
|
|
351
|
+
phone: phoneField.create,
|
|
352
|
+
whatsapp: phoneField.create,
|
|
353
|
+
paymentTermDays: termDaysSchema.default([
|
|
354
|
+
0
|
|
355
|
+
]),
|
|
356
|
+
leadTimeDays: import_zod.z.number().int().min(0).max(PURCHASE_LIMITS.LEAD_DAYS).nullable().default(null),
|
|
357
|
+
minimumOrderAmount: moneySchema.nullable().default(null),
|
|
358
|
+
deliveryWeekdays: weekdaysSchema.default([]),
|
|
359
|
+
notes: import_zod.z.string().trim().max(PURCHASE_LIMITS.NOTE).default(""),
|
|
360
|
+
isActive: import_zod.z.boolean().default(true)
|
|
361
|
+
};
|
|
362
|
+
var createSupplierSchema = import_zod.z.object({
|
|
363
|
+
id: import_zod.z.string().uuid().optional(),
|
|
364
|
+
...supplierShape
|
|
365
|
+
});
|
|
366
|
+
var updateSupplierSchema = import_zod.z.object({
|
|
367
|
+
name: supplierShape.name.optional(),
|
|
368
|
+
document: documentField.update,
|
|
369
|
+
contactName: nullableText(PURCHASE_LIMITS.CONTACT).update,
|
|
370
|
+
email: emailField.update,
|
|
371
|
+
phone: phoneField.update,
|
|
372
|
+
whatsapp: phoneField.update,
|
|
373
|
+
paymentTermDays: termDaysSchema.optional(),
|
|
374
|
+
leadTimeDays: import_zod.z.number().int().min(0).max(PURCHASE_LIMITS.LEAD_DAYS).nullable().optional(),
|
|
375
|
+
minimumOrderAmount: moneySchema.nullable().optional(),
|
|
376
|
+
deliveryWeekdays: weekdaysSchema.optional(),
|
|
377
|
+
notes: import_zod.z.string().trim().max(PURCHASE_LIMITS.NOTE).optional(),
|
|
378
|
+
isActive: import_zod.z.boolean().optional()
|
|
379
|
+
}).refine((value) => Object.values(value).some((field) => field !== void 0), {
|
|
380
|
+
message: "Nada para alterar."
|
|
381
|
+
});
|
|
382
|
+
var SUPPLIER_SORTS = [
|
|
383
|
+
"name",
|
|
384
|
+
"createdAt",
|
|
385
|
+
"updatedAt"
|
|
386
|
+
];
|
|
387
|
+
var listSuppliersQuerySchema = import_zod.z.object({
|
|
388
|
+
...pageShape,
|
|
389
|
+
search: import_zod.z.string().trim().max(PURCHASE_LIMITS.SEARCH).optional(),
|
|
390
|
+
isActive: boolText,
|
|
391
|
+
sort: import_zod.z.enum(SUPPLIER_SORTS).default("name"),
|
|
392
|
+
order: import_zod.z.enum([
|
|
393
|
+
SORT_ORDER.ASC,
|
|
394
|
+
SORT_ORDER.DESC
|
|
395
|
+
]).default(SORT_ORDER.ASC)
|
|
396
|
+
});
|
|
397
|
+
var supplierLookupSchema = import_zod.z.object({
|
|
398
|
+
ids: import_zod.z.array(import_zod.z.string().uuid()).min(1).max(PURCHASE_LIMITS.LOOKUP)
|
|
399
|
+
});
|
|
400
|
+
var supplierItemShape = {
|
|
401
|
+
itemRef: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.ITEM_REF).nullable().default(null),
|
|
402
|
+
itemName: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.NAME),
|
|
403
|
+
supplierSku: nullableText(PURCHASE_LIMITS.SKU).create,
|
|
404
|
+
purchaseUnit: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.UNIT),
|
|
405
|
+
unitsPerPurchase: import_recipe_contracts3.positiveDecimalSchema.default("1"),
|
|
406
|
+
stockUnit: unitSchema.nullable().default(null),
|
|
407
|
+
lastPrice: import_recipe_contracts3.decimalSchema.nullable().default(null),
|
|
408
|
+
priority: import_zod.z.number().int().min(0).max(PURCHASE_LIMITS.PRIORITY).default(0),
|
|
409
|
+
isActive: import_zod.z.boolean().default(true)
|
|
410
|
+
};
|
|
411
|
+
var createSupplierItemSchema = import_zod.z.object(supplierItemShape);
|
|
412
|
+
var updateSupplierItemSchema = import_zod.z.object({
|
|
413
|
+
itemRef: supplierItemShape.itemRef.optional(),
|
|
414
|
+
itemName: supplierItemShape.itemName.optional(),
|
|
415
|
+
supplierSku: nullableText(PURCHASE_LIMITS.SKU).update,
|
|
416
|
+
purchaseUnit: supplierItemShape.purchaseUnit.optional(),
|
|
417
|
+
unitsPerPurchase: import_recipe_contracts3.positiveDecimalSchema.optional(),
|
|
418
|
+
stockUnit: unitSchema.nullable().optional(),
|
|
419
|
+
lastPrice: import_recipe_contracts3.decimalSchema.nullable().optional(),
|
|
420
|
+
priority: import_zod.z.number().int().min(0).max(PURCHASE_LIMITS.PRIORITY).optional(),
|
|
421
|
+
isActive: import_zod.z.boolean().optional()
|
|
422
|
+
}).refine((value) => Object.values(value).some((field) => field !== void 0), {
|
|
423
|
+
message: "Nada para alterar."
|
|
424
|
+
});
|
|
425
|
+
var listSupplierItemsQuerySchema = import_zod.z.object({
|
|
426
|
+
...pageShape,
|
|
427
|
+
supplierId: import_zod.z.string().uuid().optional(),
|
|
428
|
+
itemRef: import_zod.z.string().trim().max(PURCHASE_LIMITS.ITEM_REF).optional(),
|
|
429
|
+
search: import_zod.z.string().trim().max(PURCHASE_LIMITS.SEARCH).optional(),
|
|
430
|
+
isActive: boolText
|
|
431
|
+
});
|
|
432
|
+
var orderLineInputSchema = import_zod.z.object({
|
|
433
|
+
supplierItemId: import_zod.z.string().uuid().optional(),
|
|
434
|
+
itemRef: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.ITEM_REF).optional(),
|
|
435
|
+
itemName: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.NAME).optional(),
|
|
436
|
+
supplierSku: import_zod.z.string().trim().max(PURCHASE_LIMITS.SKU).optional(),
|
|
437
|
+
purchaseUnit: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.UNIT).optional(),
|
|
438
|
+
unitsPerPurchase: import_recipe_contracts3.positiveDecimalSchema.optional(),
|
|
439
|
+
stockUnit: unitSchema.optional(),
|
|
440
|
+
quantity: import_recipe_contracts3.positiveDecimalSchema,
|
|
441
|
+
unitPrice: import_recipe_contracts3.decimalSchema.optional(),
|
|
442
|
+
notes: import_zod.z.string().trim().max(PURCHASE_LIMITS.REASON).optional()
|
|
443
|
+
}).refine((line) => line.supplierItemId !== void 0 || line.itemName !== void 0 && line.purchaseUnit !== void 0, {
|
|
444
|
+
message: "Linha sem item do fornecedor precisa de nome e unidade de compra."
|
|
445
|
+
});
|
|
446
|
+
var createOrderSchema = import_zod.z.object({
|
|
447
|
+
supplierId: import_zod.z.string().uuid(),
|
|
448
|
+
expectedAt: civilDate.nullable().optional(),
|
|
449
|
+
notes: import_zod.z.string().trim().max(PURCHASE_LIMITS.NOTE).default(""),
|
|
450
|
+
freight: moneySchema.default("0"),
|
|
451
|
+
discount: moneySchema.default("0"),
|
|
452
|
+
/** Ausente: a condição do fornecedor. */
|
|
453
|
+
paymentTermDays: termDaysSchema.optional(),
|
|
454
|
+
lines: import_zod.z.array(orderLineInputSchema).min(1).max(PURCHASE_LIMITS.ORDER_LINES)
|
|
455
|
+
});
|
|
456
|
+
var updateOrderSchema = import_zod.z.object({
|
|
457
|
+
expectedAt: civilDate.nullable().optional(),
|
|
458
|
+
notes: import_zod.z.string().trim().max(PURCHASE_LIMITS.NOTE).optional(),
|
|
459
|
+
freight: moneySchema.optional(),
|
|
460
|
+
discount: moneySchema.optional(),
|
|
461
|
+
paymentTermDays: termDaysSchema.optional(),
|
|
462
|
+
lines: import_zod.z.array(orderLineInputSchema).min(1).max(PURCHASE_LIMITS.ORDER_LINES).optional()
|
|
463
|
+
}).refine((value) => Object.values(value).some((field) => field !== void 0), {
|
|
464
|
+
message: "Nada para alterar."
|
|
465
|
+
});
|
|
466
|
+
var reasonSchema = import_zod.z.object({
|
|
467
|
+
reason: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.REASON)
|
|
468
|
+
});
|
|
469
|
+
var optionalReasonSchema = import_zod.z.object({
|
|
470
|
+
reason: import_zod.z.string().trim().max(PURCHASE_LIMITS.REASON).default("")
|
|
471
|
+
});
|
|
472
|
+
var sendOrderSchema = import_zod.z.object({
|
|
473
|
+
channel: import_zod.z.enum(DISPATCH_CHANNELS).default(DISPATCH_CHANNEL.WHATSAPP),
|
|
474
|
+
/** Preço no texto do pedido (padrão: sem). */
|
|
475
|
+
withPrices: import_zod.z.boolean().default(false)
|
|
476
|
+
});
|
|
477
|
+
var ORDER_SORTS = [
|
|
478
|
+
"number",
|
|
479
|
+
"createdAt",
|
|
480
|
+
"expectedAt",
|
|
481
|
+
"total"
|
|
482
|
+
];
|
|
483
|
+
var listOrdersQuerySchema = import_zod.z.object({
|
|
484
|
+
...pageShape,
|
|
485
|
+
status: csvOf(ORDER_STATUSES),
|
|
486
|
+
supplierId: import_zod.z.string().uuid().optional(),
|
|
487
|
+
/** Número do pedido ou nome do fornecedor. */
|
|
488
|
+
search: import_zod.z.string().trim().max(PURCHASE_LIMITS.SEARCH).optional(),
|
|
489
|
+
late: boolText,
|
|
490
|
+
from: civilDate.optional(),
|
|
491
|
+
to: civilDate.optional(),
|
|
492
|
+
sort: import_zod.z.enum(ORDER_SORTS).default("number"),
|
|
493
|
+
order: import_zod.z.enum([
|
|
494
|
+
SORT_ORDER.ASC,
|
|
495
|
+
SORT_ORDER.DESC
|
|
496
|
+
]).default(SORT_ORDER.DESC)
|
|
497
|
+
});
|
|
498
|
+
var receiptLineInputSchema = import_zod.z.object({
|
|
499
|
+
/** Linha do pedido que está chegando (recebimento com pedido). */
|
|
500
|
+
orderLineId: import_zod.z.string().uuid().optional(),
|
|
501
|
+
supplierItemId: import_zod.z.string().uuid().optional(),
|
|
502
|
+
itemRef: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.ITEM_REF).optional(),
|
|
503
|
+
itemName: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.NAME).optional(),
|
|
504
|
+
supplierSku: import_zod.z.string().trim().max(PURCHASE_LIMITS.SKU).optional(),
|
|
505
|
+
purchaseUnit: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.UNIT).optional(),
|
|
506
|
+
unitsPerPurchase: import_recipe_contracts3.positiveDecimalSchema.optional(),
|
|
507
|
+
stockUnit: unitSchema.optional(),
|
|
508
|
+
/** Na unidade de compra. */
|
|
509
|
+
quantity: import_recipe_contracts3.positiveDecimalSchema,
|
|
510
|
+
/** Ausente: o preço do pedido (ou o último do fornecedor). */
|
|
511
|
+
unitPrice: import_recipe_contracts3.decimalSchema.optional(),
|
|
512
|
+
batchCode: import_zod.z.string().trim().max(PURCHASE_LIMITS.BATCH_CODE).optional(),
|
|
513
|
+
expiresAt: civilDate.nullable().optional(),
|
|
514
|
+
divergenceReason: import_zod.z.string().trim().max(PURCHASE_LIMITS.REASON).optional()
|
|
515
|
+
}).refine((line) => line.orderLineId !== void 0 || line.supplierItemId !== void 0 || line.itemName !== void 0 && line.purchaseUnit !== void 0, {
|
|
516
|
+
message: "Linha precisa da linha do pedido, do item do fornecedor ou de nome e unidade."
|
|
517
|
+
});
|
|
518
|
+
var installmentInput = import_zod.z.object({
|
|
519
|
+
dueDate: civilDate,
|
|
520
|
+
amount: moneySchema
|
|
521
|
+
});
|
|
522
|
+
var receiveSchema = import_zod.z.object({
|
|
523
|
+
/** Obrigatório no recebimento sem pedido; com pedido, vem dele. */
|
|
524
|
+
supplierId: import_zod.z.string().uuid().optional(),
|
|
525
|
+
/** ISO com fuso. Ausente: agora. */
|
|
526
|
+
receivedAt: import_zod.z.string().datetime({
|
|
527
|
+
offset: true
|
|
528
|
+
}).optional(),
|
|
529
|
+
invoiceNumber: import_zod.z.string().trim().max(PURCHASE_LIMITS.INVOICE_NUMBER).optional(),
|
|
530
|
+
invoiceKey: import_zod.z.string().trim().transform((value) => value.replace(/\D/g, "")).refine((value) => value === "" || value.length === 44, "Chave de acesso com 44 d\xEDgitos.").optional(),
|
|
531
|
+
invoiceIssuedAt: import_zod.z.string().datetime({
|
|
532
|
+
offset: true
|
|
533
|
+
}).optional(),
|
|
534
|
+
freight: moneySchema.default("0"),
|
|
535
|
+
discount: moneySchema.default("0"),
|
|
536
|
+
notes: import_zod.z.string().trim().max(PURCHASE_LIMITS.NOTE).default(""),
|
|
537
|
+
/** Mesmo valor reenviado devolve o recebimento já gravado (clique duplo, rede caindo). */
|
|
538
|
+
idempotencyKey: import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.IDEMPOTENCY_KEY).optional(),
|
|
539
|
+
/** Parcelas da conta a pagar; ausente: pela condição do pedido/fornecedor, ou pelas duplicatas da nota. */
|
|
540
|
+
installments: import_zod.z.array(installmentInput).min(1).max(PURCHASE_LIMITS.TERMS).optional(),
|
|
541
|
+
lines: import_zod.z.array(receiptLineInputSchema).min(1).max(PURCHASE_LIMITS.ORDER_LINES)
|
|
542
|
+
});
|
|
543
|
+
var listReceiptsQuerySchema = import_zod.z.object({
|
|
544
|
+
...pageShape,
|
|
545
|
+
supplierId: import_zod.z.string().uuid().optional(),
|
|
546
|
+
orderId: import_zod.z.string().uuid().optional(),
|
|
547
|
+
from: civilDate.optional(),
|
|
548
|
+
to: civilDate.optional(),
|
|
549
|
+
/** Só os que tiveram falha numa integração (estoque ou contas), para reprocessar. */
|
|
550
|
+
failed: boolText
|
|
551
|
+
});
|
|
552
|
+
var priceHistoryQuerySchema = import_zod.z.object({
|
|
553
|
+
...pageShape,
|
|
554
|
+
itemRef: import_zod.z.string().trim().max(PURCHASE_LIMITS.ITEM_REF).optional(),
|
|
555
|
+
supplierItemId: import_zod.z.string().uuid().optional(),
|
|
556
|
+
supplierId: import_zod.z.string().uuid().optional(),
|
|
557
|
+
from: civilDate.optional(),
|
|
558
|
+
to: civilDate.optional()
|
|
559
|
+
}).refine((query) => query.itemRef !== void 0 || query.supplierItemId !== void 0, {
|
|
560
|
+
message: "Informe itemRef ou supplierItemId."
|
|
561
|
+
});
|
|
562
|
+
var itemRefsLookupSchema = import_zod.z.object({
|
|
563
|
+
itemRefs: import_zod.z.array(import_zod.z.string().trim().min(1).max(PURCHASE_LIMITS.ITEM_REF)).min(1).max(PURCHASE_LIMITS.LOOKUP)
|
|
564
|
+
});
|
|
565
|
+
var suggestionQuerySchema = import_zod.z.object({
|
|
566
|
+
supplierId: import_zod.z.string().uuid().optional()
|
|
567
|
+
});
|
|
568
|
+
var ordersFromSuggestionSchema = import_zod.z.object({
|
|
569
|
+
/** Ausente: um rascunho por fornecedor sugerido. */
|
|
570
|
+
supplierIds: import_zod.z.array(import_zod.z.string().uuid()).max(100).optional(),
|
|
571
|
+
expectedAt: civilDate.nullable().optional()
|
|
572
|
+
});
|
|
573
|
+
var nfePreviewSchema = import_zod.z.object({
|
|
574
|
+
xml: import_zod.z.string().min(1).max(PURCHASE_LIMITS.XML_BYTES),
|
|
575
|
+
/** Casa os itens da nota com as linhas pendentes deste pedido. */
|
|
576
|
+
orderId: import_zod.z.string().uuid().optional()
|
|
577
|
+
});
|
|
578
|
+
var REPORT_GROUP = {
|
|
579
|
+
SUPPLIER: "supplier",
|
|
580
|
+
ITEM: "item",
|
|
581
|
+
MONTH: "month"
|
|
582
|
+
};
|
|
583
|
+
var REPORT_GROUPS = Object.values(REPORT_GROUP);
|
|
584
|
+
var spendReportQuerySchema = import_zod.z.object({
|
|
585
|
+
from: civilDate,
|
|
586
|
+
to: civilDate,
|
|
587
|
+
groupBy: import_zod.z.enum(REPORT_GROUPS).default(REPORT_GROUP.SUPPLIER),
|
|
588
|
+
supplierId: import_zod.z.string().uuid().optional(),
|
|
589
|
+
itemRef: import_zod.z.string().trim().max(PURCHASE_LIMITS.ITEM_REF).optional()
|
|
590
|
+
}).refine((query) => query.from <= query.to, {
|
|
591
|
+
message: "In\xEDcio depois do fim."
|
|
592
|
+
});
|
|
593
|
+
var supplierPerformanceQuerySchema = import_zod.z.object({
|
|
594
|
+
from: civilDate,
|
|
595
|
+
to: civilDate
|
|
596
|
+
}).refine((query) => query.from <= query.to, {
|
|
597
|
+
message: "In\xEDcio depois do fim."
|
|
598
|
+
});
|
|
599
|
+
var exportFormatSchema = import_zod.z.object({
|
|
600
|
+
format: import_zod.z.enum([
|
|
601
|
+
"csv",
|
|
602
|
+
"xlsx"
|
|
603
|
+
]).default("csv")
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
// src/errors.ts
|
|
607
|
+
var PurchaseError = class extends Error {
|
|
608
|
+
static {
|
|
609
|
+
__name(this, "PurchaseError");
|
|
610
|
+
}
|
|
611
|
+
statusCode;
|
|
612
|
+
code;
|
|
613
|
+
details;
|
|
614
|
+
publicDetails;
|
|
615
|
+
constructor(message, statusCode, code, details, publicDetails) {
|
|
616
|
+
super(message), this.statusCode = statusCode, this.code = code, this.details = details, this.publicDetails = publicDetails;
|
|
617
|
+
this.name = "PurchaseError";
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
var PURCHASE_ERROR_CODES = {
|
|
621
|
+
DISABLED: "PURCHASE_DISABLED",
|
|
622
|
+
UNAUTHENTICATED: "PURCHASE_UNAUTHENTICATED",
|
|
623
|
+
CONFIG_MISSING: "PURCHASE_CONFIG_MISSING",
|
|
624
|
+
SUPPLIER_NOT_FOUND: "PURCHASE_SUPPLIER_NOT_FOUND",
|
|
625
|
+
SUPPLIER_INACTIVE: "PURCHASE_SUPPLIER_INACTIVE",
|
|
626
|
+
SUPPLIER_DOCUMENT_TAKEN: "PURCHASE_SUPPLIER_DOCUMENT_TAKEN",
|
|
627
|
+
SUPPLIER_IN_USE: "PURCHASE_SUPPLIER_IN_USE",
|
|
628
|
+
SUPPLIER_ITEM_NOT_FOUND: "PURCHASE_SUPPLIER_ITEM_NOT_FOUND",
|
|
629
|
+
SUPPLIER_ITEM_TAKEN: "PURCHASE_SUPPLIER_ITEM_TAKEN",
|
|
630
|
+
ITEM_NOT_FOUND: "PURCHASE_ITEM_NOT_FOUND",
|
|
631
|
+
ORDER_NOT_FOUND: "PURCHASE_ORDER_NOT_FOUND",
|
|
632
|
+
ORDER_LINE_NOT_FOUND: "PURCHASE_ORDER_LINE_NOT_FOUND",
|
|
633
|
+
INVALID_TRANSITION: "PURCHASE_INVALID_TRANSITION",
|
|
634
|
+
SELF_APPROVAL: "PURCHASE_SELF_APPROVAL",
|
|
635
|
+
RECEIPT_NOT_FOUND: "PURCHASE_RECEIPT_NOT_FOUND",
|
|
636
|
+
INVOICE_ALREADY_RECEIVED: "PURCHASE_INVOICE_ALREADY_RECEIVED",
|
|
637
|
+
INSTALLMENTS_MISMATCH: "PURCHASE_INSTALLMENTS_MISMATCH",
|
|
638
|
+
DISPATCH_UNAVAILABLE: "PURCHASE_DISPATCH_UNAVAILABLE",
|
|
639
|
+
NO_CONTACT: "PURCHASE_SUPPLIER_NO_CONTACT",
|
|
640
|
+
NFE_INVALID: "PURCHASE_NFE_INVALID",
|
|
641
|
+
REPORT_RANGE_TOO_LONG: "PURCHASE_REPORT_RANGE_TOO_LONG"
|
|
642
|
+
};
|
|
643
|
+
var PurchaseDisabledError = class extends PurchaseError {
|
|
644
|
+
static {
|
|
645
|
+
__name(this, "PurchaseDisabledError");
|
|
646
|
+
}
|
|
647
|
+
constructor() {
|
|
648
|
+
super("Compras n\xE3o habilitado para a empresa.", 404, PURCHASE_ERROR_CODES.DISABLED);
|
|
649
|
+
this.name = "PurchaseDisabledError";
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
var PurchaseUnauthenticatedError = class extends PurchaseError {
|
|
653
|
+
static {
|
|
654
|
+
__name(this, "PurchaseUnauthenticatedError");
|
|
655
|
+
}
|
|
656
|
+
constructor() {
|
|
657
|
+
super("Sem empresa autenticada.", 401, PURCHASE_ERROR_CODES.UNAUTHENTICATED);
|
|
658
|
+
this.name = "PurchaseUnauthenticatedError";
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
var PurchaseConfigMissingError = class extends PurchaseError {
|
|
662
|
+
static {
|
|
663
|
+
__name(this, "PurchaseConfigMissingError");
|
|
664
|
+
}
|
|
665
|
+
constructor(field) {
|
|
666
|
+
super(`Configura\xE7\xE3o ausente ou inv\xE1lida: ${field}.`, 500, PURCHASE_ERROR_CODES.CONFIG_MISSING, {
|
|
667
|
+
field
|
|
668
|
+
});
|
|
669
|
+
this.name = "PurchaseConfigMissingError";
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
var SupplierNotFoundError = class extends PurchaseError {
|
|
673
|
+
static {
|
|
674
|
+
__name(this, "SupplierNotFoundError");
|
|
675
|
+
}
|
|
676
|
+
constructor(supplierId) {
|
|
677
|
+
super("Fornecedor n\xE3o encontrado.", 404, PURCHASE_ERROR_CODES.SUPPLIER_NOT_FOUND, {
|
|
678
|
+
supplierId
|
|
679
|
+
});
|
|
680
|
+
this.name = "SupplierNotFoundError";
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
var SupplierInactiveError = class extends PurchaseError {
|
|
684
|
+
static {
|
|
685
|
+
__name(this, "SupplierInactiveError");
|
|
686
|
+
}
|
|
687
|
+
constructor(supplierId) {
|
|
688
|
+
super("Fornecedor desativado.", 422, PURCHASE_ERROR_CODES.SUPPLIER_INACTIVE, {
|
|
689
|
+
supplierId
|
|
690
|
+
});
|
|
691
|
+
this.name = "SupplierInactiveError";
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
var SupplierDocumentTakenError = class extends PurchaseError {
|
|
695
|
+
static {
|
|
696
|
+
__name(this, "SupplierDocumentTakenError");
|
|
697
|
+
}
|
|
698
|
+
constructor() {
|
|
699
|
+
super("J\xE1 existe fornecedor com este documento.", 409, PURCHASE_ERROR_CODES.SUPPLIER_DOCUMENT_TAKEN);
|
|
700
|
+
this.name = "SupplierDocumentTakenError";
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
var SupplierInUseError = class extends PurchaseError {
|
|
704
|
+
static {
|
|
705
|
+
__name(this, "SupplierInUseError");
|
|
706
|
+
}
|
|
707
|
+
constructor(supplierId) {
|
|
708
|
+
super("Fornecedor com pedidos ou recebimentos: desative em vez de excluir.", 409, PURCHASE_ERROR_CODES.SUPPLIER_IN_USE, {
|
|
709
|
+
supplierId
|
|
710
|
+
});
|
|
711
|
+
this.name = "SupplierInUseError";
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
var SupplierItemNotFoundError = class extends PurchaseError {
|
|
715
|
+
static {
|
|
716
|
+
__name(this, "SupplierItemNotFoundError");
|
|
717
|
+
}
|
|
718
|
+
constructor(supplierItemId) {
|
|
719
|
+
super("Item do fornecedor n\xE3o encontrado.", 404, PURCHASE_ERROR_CODES.SUPPLIER_ITEM_NOT_FOUND, {
|
|
720
|
+
supplierItemId
|
|
721
|
+
});
|
|
722
|
+
this.name = "SupplierItemNotFoundError";
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
var SupplierItemTakenError = class extends PurchaseError {
|
|
726
|
+
static {
|
|
727
|
+
__name(this, "SupplierItemTakenError");
|
|
728
|
+
}
|
|
729
|
+
constructor() {
|
|
730
|
+
super("Este fornecedor j\xE1 tem um item com este c\xF3digo ou ligado a este item do cat\xE1logo.", 409, PURCHASE_ERROR_CODES.SUPPLIER_ITEM_TAKEN);
|
|
731
|
+
this.name = "SupplierItemTakenError";
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
var PurchaseItemNotFoundError = class extends PurchaseError {
|
|
735
|
+
static {
|
|
736
|
+
__name(this, "PurchaseItemNotFoundError");
|
|
737
|
+
}
|
|
738
|
+
constructor(itemRef) {
|
|
739
|
+
super("Item n\xE3o encontrado no cat\xE1logo.", 404, PURCHASE_ERROR_CODES.ITEM_NOT_FOUND, {
|
|
740
|
+
itemRef
|
|
741
|
+
}, {
|
|
742
|
+
itemRef
|
|
743
|
+
});
|
|
744
|
+
this.name = "PurchaseItemNotFoundError";
|
|
745
|
+
}
|
|
746
|
+
};
|
|
747
|
+
var OrderNotFoundError = class extends PurchaseError {
|
|
748
|
+
static {
|
|
749
|
+
__name(this, "OrderNotFoundError");
|
|
750
|
+
}
|
|
751
|
+
constructor(orderId) {
|
|
752
|
+
super("Pedido de compra n\xE3o encontrado.", 404, PURCHASE_ERROR_CODES.ORDER_NOT_FOUND, {
|
|
753
|
+
orderId
|
|
754
|
+
});
|
|
755
|
+
this.name = "OrderNotFoundError";
|
|
756
|
+
}
|
|
757
|
+
};
|
|
758
|
+
var OrderLineNotFoundError = class extends PurchaseError {
|
|
759
|
+
static {
|
|
760
|
+
__name(this, "OrderLineNotFoundError");
|
|
761
|
+
}
|
|
762
|
+
constructor(orderLineId) {
|
|
763
|
+
super("Linha do pedido n\xE3o encontrada.", 422, PURCHASE_ERROR_CODES.ORDER_LINE_NOT_FOUND, {
|
|
764
|
+
orderLineId
|
|
765
|
+
}, {
|
|
766
|
+
orderLineId
|
|
767
|
+
});
|
|
768
|
+
this.name = "OrderLineNotFoundError";
|
|
769
|
+
}
|
|
770
|
+
};
|
|
771
|
+
var InvalidOrderTransitionError = class extends PurchaseError {
|
|
772
|
+
static {
|
|
773
|
+
__name(this, "InvalidOrderTransitionError");
|
|
774
|
+
}
|
|
775
|
+
constructor(params) {
|
|
776
|
+
super("A\xE7\xE3o n\xE3o permitida no status atual do pedido.", 409, PURCHASE_ERROR_CODES.INVALID_TRANSITION, params, params);
|
|
777
|
+
this.name = "InvalidOrderTransitionError";
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
var SelfApprovalError = class extends PurchaseError {
|
|
781
|
+
static {
|
|
782
|
+
__name(this, "SelfApprovalError");
|
|
783
|
+
}
|
|
784
|
+
constructor() {
|
|
785
|
+
super("Quem enviou o pedido para aprova\xE7\xE3o n\xE3o pode aprov\xE1-lo.", 403, PURCHASE_ERROR_CODES.SELF_APPROVAL);
|
|
786
|
+
this.name = "SelfApprovalError";
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
var ReceiptNotFoundError = class extends PurchaseError {
|
|
790
|
+
static {
|
|
791
|
+
__name(this, "ReceiptNotFoundError");
|
|
792
|
+
}
|
|
793
|
+
constructor(receiptId) {
|
|
794
|
+
super("Recebimento n\xE3o encontrado.", 404, PURCHASE_ERROR_CODES.RECEIPT_NOT_FOUND, {
|
|
795
|
+
receiptId
|
|
796
|
+
});
|
|
797
|
+
this.name = "ReceiptNotFoundError";
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
var InvoiceAlreadyReceivedError = class extends PurchaseError {
|
|
801
|
+
static {
|
|
802
|
+
__name(this, "InvoiceAlreadyReceivedError");
|
|
803
|
+
}
|
|
804
|
+
constructor(receiptId) {
|
|
805
|
+
super("Esta nota j\xE1 foi recebida.", 409, PURCHASE_ERROR_CODES.INVOICE_ALREADY_RECEIVED, {
|
|
806
|
+
receiptId
|
|
807
|
+
}, {
|
|
808
|
+
receiptId
|
|
809
|
+
});
|
|
810
|
+
this.name = "InvoiceAlreadyReceivedError";
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
var InstallmentsMismatchError = class extends PurchaseError {
|
|
814
|
+
static {
|
|
815
|
+
__name(this, "InstallmentsMismatchError");
|
|
816
|
+
}
|
|
817
|
+
constructor(params) {
|
|
818
|
+
super("A soma das parcelas n\xE3o fecha com o total do recebimento.", 422, PURCHASE_ERROR_CODES.INSTALLMENTS_MISMATCH, params, params);
|
|
819
|
+
this.name = "InstallmentsMismatchError";
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
var DispatchUnavailableError = class extends PurchaseError {
|
|
823
|
+
static {
|
|
824
|
+
__name(this, "DispatchUnavailableError");
|
|
825
|
+
}
|
|
826
|
+
constructor(channel) {
|
|
827
|
+
super("Envio por este canal n\xE3o est\xE1 configurado.", 422, PURCHASE_ERROR_CODES.DISPATCH_UNAVAILABLE, {
|
|
828
|
+
channel
|
|
829
|
+
}, {
|
|
830
|
+
channel
|
|
831
|
+
});
|
|
832
|
+
this.name = "DispatchUnavailableError";
|
|
833
|
+
}
|
|
834
|
+
};
|
|
835
|
+
var SupplierNoContactError = class extends PurchaseError {
|
|
836
|
+
static {
|
|
837
|
+
__name(this, "SupplierNoContactError");
|
|
838
|
+
}
|
|
839
|
+
constructor(channel) {
|
|
840
|
+
super("Fornecedor sem contato para este canal.", 422, PURCHASE_ERROR_CODES.NO_CONTACT, {
|
|
841
|
+
channel
|
|
842
|
+
}, {
|
|
843
|
+
channel
|
|
844
|
+
});
|
|
845
|
+
this.name = "SupplierNoContactError";
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
var NfeInvalidError = class extends PurchaseError {
|
|
849
|
+
static {
|
|
850
|
+
__name(this, "NfeInvalidError");
|
|
851
|
+
}
|
|
852
|
+
constructor(reason) {
|
|
853
|
+
super("XML de NF-e inv\xE1lido.", 422, PURCHASE_ERROR_CODES.NFE_INVALID, {
|
|
854
|
+
reason
|
|
855
|
+
}, {
|
|
856
|
+
reason
|
|
857
|
+
});
|
|
858
|
+
this.name = "NfeInvalidError";
|
|
859
|
+
}
|
|
860
|
+
};
|
|
861
|
+
var ReportRangeTooLongError = class extends PurchaseError {
|
|
862
|
+
static {
|
|
863
|
+
__name(this, "ReportRangeTooLongError");
|
|
864
|
+
}
|
|
865
|
+
constructor(maxDays) {
|
|
866
|
+
super("Per\xEDodo longo demais para o relat\xF3rio.", 422, PURCHASE_ERROR_CODES.REPORT_RANGE_TOO_LONG, {
|
|
867
|
+
maxDays
|
|
868
|
+
}, {
|
|
869
|
+
maxDays
|
|
870
|
+
});
|
|
871
|
+
this.name = "ReportRangeTooLongError";
|
|
872
|
+
}
|
|
873
|
+
};
|
|
874
|
+
|
|
875
|
+
// src/providers.ts
|
|
876
|
+
var DEFAULT_PURCHASE_SCOPES = {
|
|
877
|
+
read: [
|
|
878
|
+
"purchase:read"
|
|
879
|
+
],
|
|
880
|
+
write: [
|
|
881
|
+
"purchase:write"
|
|
882
|
+
],
|
|
883
|
+
approve: [
|
|
884
|
+
"purchase:approve"
|
|
885
|
+
],
|
|
886
|
+
service: [
|
|
887
|
+
"purchase:service"
|
|
888
|
+
]
|
|
889
|
+
};
|
|
890
|
+
|
|
891
|
+
// src/rules/orderStatus.ts
|
|
892
|
+
var ORDER_ACTION = {
|
|
893
|
+
EDIT: "edit",
|
|
894
|
+
SUBMIT: "submit",
|
|
895
|
+
APPROVE: "approve",
|
|
896
|
+
REJECT: "reject",
|
|
897
|
+
SEND: "send",
|
|
898
|
+
RECEIVE: "receive",
|
|
899
|
+
CLOSE: "close",
|
|
900
|
+
CANCEL: "cancel"
|
|
901
|
+
};
|
|
902
|
+
var ALLOWED = {
|
|
903
|
+
edit: [
|
|
904
|
+
ORDER_STATUS.DRAFT
|
|
905
|
+
],
|
|
906
|
+
submit: [
|
|
907
|
+
ORDER_STATUS.DRAFT
|
|
908
|
+
],
|
|
909
|
+
approve: [
|
|
910
|
+
ORDER_STATUS.PENDING_APPROVAL
|
|
911
|
+
],
|
|
912
|
+
reject: [
|
|
913
|
+
ORDER_STATUS.PENDING_APPROVAL
|
|
914
|
+
],
|
|
915
|
+
// Reenviar um pedido já enviado é permitido (fornecedor não recebeu, mudou de canal).
|
|
916
|
+
send: [
|
|
917
|
+
ORDER_STATUS.APPROVED,
|
|
918
|
+
ORDER_STATUS.SENT
|
|
919
|
+
],
|
|
920
|
+
receive: [
|
|
921
|
+
ORDER_STATUS.APPROVED,
|
|
922
|
+
ORDER_STATUS.SENT,
|
|
923
|
+
ORDER_STATUS.PARTIALLY_RECEIVED
|
|
924
|
+
],
|
|
925
|
+
close: [
|
|
926
|
+
ORDER_STATUS.PARTIALLY_RECEIVED
|
|
927
|
+
],
|
|
928
|
+
cancel: [
|
|
929
|
+
ORDER_STATUS.DRAFT,
|
|
930
|
+
ORDER_STATUS.PENDING_APPROVAL,
|
|
931
|
+
ORDER_STATUS.APPROVED,
|
|
932
|
+
ORDER_STATUS.SENT
|
|
933
|
+
]
|
|
934
|
+
};
|
|
935
|
+
function canPerform(status, action) {
|
|
936
|
+
return ALLOWED[action].includes(status);
|
|
937
|
+
}
|
|
938
|
+
__name(canPerform, "canPerform");
|
|
939
|
+
function allowedActions(status) {
|
|
940
|
+
return Object.keys(ALLOWED).filter((action) => canPerform(status, action));
|
|
941
|
+
}
|
|
942
|
+
__name(allowedActions, "allowedActions");
|
|
943
|
+
function statusAfterSend(status) {
|
|
944
|
+
return status === ORDER_STATUS.PARTIALLY_RECEIVED ? status : ORDER_STATUS.SENT;
|
|
945
|
+
}
|
|
946
|
+
__name(statusAfterSend, "statusAfterSend");
|
|
947
|
+
function requiresApproval(params) {
|
|
948
|
+
return params.thresholdCents !== null && params.totalCents > params.thresholdCents;
|
|
949
|
+
}
|
|
950
|
+
__name(requiresApproval, "requiresApproval");
|
|
951
|
+
function isOrderLate(params) {
|
|
952
|
+
if (!params.expectedAt) return false;
|
|
953
|
+
if (params.status !== ORDER_STATUS.APPROVED && params.status !== ORDER_STATUS.SENT && params.status !== ORDER_STATUS.PARTIALLY_RECEIVED) return false;
|
|
954
|
+
return params.expectedAt < params.today;
|
|
955
|
+
}
|
|
956
|
+
__name(isOrderLate, "isOrderLate");
|
|
957
|
+
|
|
958
|
+
// src/rules/totals.ts
|
|
959
|
+
var import_recipe_contracts4 = require("@adatechnology/recipe-contracts");
|
|
960
|
+
function computeTotals(params) {
|
|
961
|
+
const subtotal = sum(params.lines.map((line) => lineTotal(line.quantity, line.unitPrice)));
|
|
962
|
+
const freight = (0, import_recipe_contracts4.parseDecimal)(params.freight ?? "0");
|
|
963
|
+
const discount = (0, import_recipe_contracts4.parseDecimal)(params.discount ?? "0");
|
|
964
|
+
const total = clampToZero((0, import_recipe_contracts4.subtract)(add2(subtotal, freight), discount));
|
|
965
|
+
return {
|
|
966
|
+
subtotal: money(subtotal),
|
|
967
|
+
freight: money(freight),
|
|
968
|
+
discount: money(discount),
|
|
969
|
+
total: money(total)
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
__name(computeTotals, "computeTotals");
|
|
973
|
+
function add2(left, right) {
|
|
974
|
+
return sum([
|
|
975
|
+
left,
|
|
976
|
+
right
|
|
977
|
+
]);
|
|
978
|
+
}
|
|
979
|
+
__name(add2, "add2");
|
|
980
|
+
function allocateExtras(params) {
|
|
981
|
+
const totals = params.lineTotals.map((value) => centsOf((0, import_recipe_contracts4.parseDecimal)(value)));
|
|
982
|
+
const base = totals.reduce((acc, value) => acc + value, 0n);
|
|
983
|
+
const net = centsOf((0, import_recipe_contracts4.parseDecimal)(params.freight)) - centsOf((0, import_recipe_contracts4.parseDecimal)(params.discount));
|
|
984
|
+
if (totals.length === 0) return [];
|
|
985
|
+
if (base === 0n || net === 0n) {
|
|
986
|
+
if (net === 0n) return totals.map(() => "0.00");
|
|
987
|
+
return splitEvenly(net, totals.length).map((cents) => money(fromCentsBig(cents)));
|
|
988
|
+
}
|
|
989
|
+
const sign = net < 0n ? -1n : 1n;
|
|
990
|
+
const absolute = net * sign;
|
|
991
|
+
const shares = totals.map((value, index) => {
|
|
992
|
+
const exact = absolute * value;
|
|
993
|
+
return {
|
|
994
|
+
index,
|
|
995
|
+
floor: exact / base,
|
|
996
|
+
remainder: exact % base
|
|
997
|
+
};
|
|
998
|
+
});
|
|
999
|
+
let left = absolute - shares.reduce((acc, share) => acc + share.floor, 0n);
|
|
1000
|
+
const byRemainder = [
|
|
1001
|
+
...shares
|
|
1002
|
+
].sort((a, b) => b.remainder > a.remainder ? 1 : b.remainder < a.remainder ? -1 : a.index - b.index);
|
|
1003
|
+
const extra = /* @__PURE__ */ new Map();
|
|
1004
|
+
for (const share of byRemainder) {
|
|
1005
|
+
if (left <= 0n) break;
|
|
1006
|
+
extra.set(share.index, 1n);
|
|
1007
|
+
left -= 1n;
|
|
1008
|
+
}
|
|
1009
|
+
return shares.map((share) => money(fromCentsBig((share.floor + (extra.get(share.index) ?? 0n)) * sign)));
|
|
1010
|
+
}
|
|
1011
|
+
__name(allocateExtras, "allocateExtras");
|
|
1012
|
+
function splitEvenly(cents, parts) {
|
|
1013
|
+
const count = BigInt(parts);
|
|
1014
|
+
const sign = cents < 0n ? -1n : 1n;
|
|
1015
|
+
const absolute = cents * sign;
|
|
1016
|
+
const base = absolute / count;
|
|
1017
|
+
const rest = absolute % count;
|
|
1018
|
+
return Array.from({
|
|
1019
|
+
length: parts
|
|
1020
|
+
}, (_, index) => (base + (BigInt(index) < rest ? 1n : 0n)) * sign);
|
|
1021
|
+
}
|
|
1022
|
+
__name(splitEvenly, "splitEvenly");
|
|
1023
|
+
function landedUnitCost(params) {
|
|
1024
|
+
const stockQuantity = (0, import_recipe_contracts4.multiply)((0, import_recipe_contracts4.parseDecimal)(params.quantity), (0, import_recipe_contracts4.parseDecimal)(params.unitsPerPurchase));
|
|
1025
|
+
if ((0, import_recipe_contracts.compare)(stockQuantity, import_recipe_contracts4.ZERO) <= 0) return null;
|
|
1026
|
+
const value = clampToZero(sum([
|
|
1027
|
+
(0, import_recipe_contracts4.parseDecimal)(params.lineTotal),
|
|
1028
|
+
(0, import_recipe_contracts4.parseDecimal)(params.allocated)
|
|
1029
|
+
]));
|
|
1030
|
+
return (0, import_recipe_contracts4.formatDecimal)((0, import_recipe_contracts4.divide)(value, stockQuantity));
|
|
1031
|
+
}
|
|
1032
|
+
__name(landedUnitCost, "landedUnitCost");
|
|
1033
|
+
function toStockQuantity(quantity, unitsPerPurchase) {
|
|
1034
|
+
return quantityText((0, import_recipe_contracts4.multiply)((0, import_recipe_contracts4.parseDecimal)(quantity), (0, import_recipe_contracts4.parseDecimal)(unitsPerPurchase)));
|
|
1035
|
+
}
|
|
1036
|
+
__name(toStockQuantity, "toStockQuantity");
|
|
1037
|
+
function pendingQuantity(params) {
|
|
1038
|
+
const pending = (0, import_recipe_contracts4.subtract)((0, import_recipe_contracts4.subtract)((0, import_recipe_contracts4.parseDecimal)(params.quantity), (0, import_recipe_contracts4.parseDecimal)(params.receivedQuantity)), (0, import_recipe_contracts4.parseDecimal)(params.canceledQuantity));
|
|
1039
|
+
return quantityText(clampToZero(pending));
|
|
1040
|
+
}
|
|
1041
|
+
__name(pendingQuantity, "pendingQuantity");
|
|
1042
|
+
function statusAfterReceipt(lines) {
|
|
1043
|
+
const anyPending = lines.some((line) => (0, import_recipe_contracts.compare)((0, import_recipe_contracts4.parseDecimal)(pendingQuantity(line)), import_recipe_contracts4.ZERO) > 0);
|
|
1044
|
+
return anyPending ? ORDER_STATUS.PARTIALLY_RECEIVED : ORDER_STATUS.RECEIVED;
|
|
1045
|
+
}
|
|
1046
|
+
__name(statusAfterReceipt, "statusAfterReceipt");
|
|
1047
|
+
function divergenceOf(params) {
|
|
1048
|
+
const quantity = params.pendingQuantity !== null && (0, import_recipe_contracts.compare)((0, import_recipe_contracts4.parseDecimal)(params.receivedQuantity), (0, import_recipe_contracts4.parseDecimal)(params.pendingQuantity)) > 0;
|
|
1049
|
+
const tolerance = params.priceToleranceCents ?? 0n;
|
|
1050
|
+
let price = false;
|
|
1051
|
+
if (params.orderedPrice !== null) {
|
|
1052
|
+
const ordered = (0, import_recipe_contracts4.parseDecimal)(params.orderedPrice);
|
|
1053
|
+
const received = (0, import_recipe_contracts4.parseDecimal)(params.receivedPrice);
|
|
1054
|
+
const delta = centsOf((0, import_recipe_contracts4.subtract)(received, ordered));
|
|
1055
|
+
price = (delta < 0n ? -delta : delta) > tolerance;
|
|
1056
|
+
}
|
|
1057
|
+
if (quantity && price) return DIVERGENCE.BOTH;
|
|
1058
|
+
if (quantity) return DIVERGENCE.QUANTITY;
|
|
1059
|
+
if (price) return DIVERGENCE.PRICE;
|
|
1060
|
+
return DIVERGENCE.NONE;
|
|
1061
|
+
}
|
|
1062
|
+
__name(divergenceOf, "divergenceOf");
|
|
1063
|
+
function weightedAverageCost(points) {
|
|
1064
|
+
const weight = sum(points.map((point) => (0, import_recipe_contracts4.parseDecimal)(point.quantity)));
|
|
1065
|
+
if ((0, import_recipe_contracts.compare)(weight, import_recipe_contracts4.ZERO) <= 0) return null;
|
|
1066
|
+
const value = sum(points.map((point) => (0, import_recipe_contracts4.multiply)((0, import_recipe_contracts4.parseDecimal)(point.cost), (0, import_recipe_contracts4.parseDecimal)(point.quantity))));
|
|
1067
|
+
return (0, import_recipe_contracts4.formatDecimal)((0, import_recipe_contracts4.divide)(value, weight));
|
|
1068
|
+
}
|
|
1069
|
+
__name(weightedAverageCost, "weightedAverageCost");
|
|
1070
|
+
var UNIT_FACTOR_ONE = (0, import_recipe_contracts4.formatDecimal)((0, import_recipe_contracts4.rational)(1n));
|
|
1071
|
+
|
|
1072
|
+
// src/rules/suggestion.ts
|
|
1073
|
+
var import_recipe_contracts5 = require("@adatechnology/recipe-contracts");
|
|
1074
|
+
function shortageOf(row) {
|
|
1075
|
+
const onHand = clampToZero((0, import_recipe_contracts5.parseDecimal)(row.onHandQuantity ?? "0"));
|
|
1076
|
+
const byNeed = (0, import_recipe_contracts5.subtract)((0, import_recipe_contracts5.parseDecimal)(row.neededQuantity), onHand);
|
|
1077
|
+
const byMinimum = row.minimumQuantity === null ? import_recipe_contracts5.ZERO : (0, import_recipe_contracts5.subtract)((0, import_recipe_contracts5.parseDecimal)(row.minimumQuantity), onHand);
|
|
1078
|
+
const raw = (0, import_recipe_contracts5.subtract)((0, import_recipe_contracts5.maximum)(byNeed, byMinimum), (0, import_recipe_contracts5.parseDecimal)(row.incomingQuantity));
|
|
1079
|
+
return quantityText(clampToZero(raw));
|
|
1080
|
+
}
|
|
1081
|
+
__name(shortageOf, "shortageOf");
|
|
1082
|
+
function packsFor(shortage, unitsPerPurchase) {
|
|
1083
|
+
const packs = (0, import_recipe_contracts5.divide)((0, import_recipe_contracts5.parseDecimal)(shortage), (0, import_recipe_contracts5.parseDecimal)(unitsPerPurchase));
|
|
1084
|
+
return quantityText((0, import_recipe_contracts5.roundToScale)(packs, 0, import_recipe_contracts5.ROUNDING.UP));
|
|
1085
|
+
}
|
|
1086
|
+
__name(packsFor, "packsFor");
|
|
1087
|
+
function comparePerStockUnit(left, right) {
|
|
1088
|
+
if (left.lastPrice === null) return right.lastPrice === null ? 0 : 1;
|
|
1089
|
+
if (right.lastPrice === null) return -1;
|
|
1090
|
+
const leftCost = (0, import_recipe_contracts5.divide)((0, import_recipe_contracts5.parseDecimal)(left.lastPrice), (0, import_recipe_contracts5.parseDecimal)(left.unitsPerPurchase));
|
|
1091
|
+
const rightCost = (0, import_recipe_contracts5.divide)((0, import_recipe_contracts5.parseDecimal)(right.lastPrice), (0, import_recipe_contracts5.parseDecimal)(right.unitsPerPurchase));
|
|
1092
|
+
return (0, import_recipe_contracts.compare)(leftCost, rightCost);
|
|
1093
|
+
}
|
|
1094
|
+
__name(comparePerStockUnit, "comparePerStockUnit");
|
|
1095
|
+
function buildSuggestion(params) {
|
|
1096
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1097
|
+
for (const row of params.demand) {
|
|
1098
|
+
const incomingQuantity = params.incoming.get(row.itemRef) ?? "0";
|
|
1099
|
+
const shortage = shortageOf({
|
|
1100
|
+
...row,
|
|
1101
|
+
incomingQuantity
|
|
1102
|
+
});
|
|
1103
|
+
if ((0, import_recipe_contracts.compare)((0, import_recipe_contracts5.parseDecimal)(shortage), import_recipe_contracts5.ZERO) <= 0) continue;
|
|
1104
|
+
const candidates = params.supplierItems.filter((item) => item.itemRef === row.itemRef && item.isActive && params.suppliers.get(item.supplierId)?.isActive === true && (params.supplierId === void 0 || item.supplierId === params.supplierId)).sort((left, right) => left.priority - right.priority || comparePerStockUnit(left, right));
|
|
1105
|
+
const chosen = candidates[0];
|
|
1106
|
+
if (!chosen && params.supplierId !== void 0) continue;
|
|
1107
|
+
const suggested = chosen ? packsFor(shortage, chosen.unitsPerPurchase) : null;
|
|
1108
|
+
const line = {
|
|
1109
|
+
itemRef: row.itemRef,
|
|
1110
|
+
itemName: row.itemName,
|
|
1111
|
+
stockUnit: row.stockUnit,
|
|
1112
|
+
neededQuantity: quantityText((0, import_recipe_contracts5.parseDecimal)(row.neededQuantity)),
|
|
1113
|
+
onHandQuantity: row.onHandQuantity === null ? null : quantityText((0, import_recipe_contracts5.parseDecimal)(row.onHandQuantity)),
|
|
1114
|
+
incomingQuantity: quantityText((0, import_recipe_contracts5.parseDecimal)(incomingQuantity)),
|
|
1115
|
+
minimumQuantity: row.minimumQuantity === null ? null : quantityText((0, import_recipe_contracts5.parseDecimal)(row.minimumQuantity)),
|
|
1116
|
+
shortageQuantity: shortage,
|
|
1117
|
+
supplierItemId: chosen?.id ?? null,
|
|
1118
|
+
purchaseUnit: chosen?.purchaseUnit ?? null,
|
|
1119
|
+
unitsPerPurchase: chosen ? quantityText((0, import_recipe_contracts5.parseDecimal)(chosen.unitsPerPurchase)) : null,
|
|
1120
|
+
suggestedQuantity: suggested,
|
|
1121
|
+
unitPrice: chosen?.lastPrice ? quantityText((0, import_recipe_contracts5.parseDecimal)(chosen.lastPrice)) : null,
|
|
1122
|
+
estimatedTotal: chosen?.lastPrice && suggested ? money(lineTotal(suggested, chosen.lastPrice)) : null
|
|
1123
|
+
};
|
|
1124
|
+
const key = chosen?.supplierId ?? null;
|
|
1125
|
+
groups.set(key, [
|
|
1126
|
+
...groups.get(key) ?? [],
|
|
1127
|
+
line
|
|
1128
|
+
]);
|
|
1129
|
+
}
|
|
1130
|
+
return [
|
|
1131
|
+
...groups.entries()
|
|
1132
|
+
].map(([supplierId, lines]) => {
|
|
1133
|
+
const supplier = supplierId ? params.suppliers.get(supplierId) : void 0;
|
|
1134
|
+
const estimated = sum(lines.map((line) => (0, import_recipe_contracts5.parseDecimal)(line.estimatedTotal ?? "0")));
|
|
1135
|
+
const minimum = supplier?.minimumOrderAmount ? (0, import_recipe_contracts5.parseDecimal)(supplier.minimumOrderAmount) : null;
|
|
1136
|
+
return {
|
|
1137
|
+
supplierId,
|
|
1138
|
+
supplierName: supplier?.name ?? null,
|
|
1139
|
+
lines: lines.sort((a, b) => a.itemName.localeCompare(b.itemName, "pt-BR")),
|
|
1140
|
+
estimatedTotal: money(estimated),
|
|
1141
|
+
belowMinimumOrder: minimum !== null && centsOf(estimated) < centsOf(minimum)
|
|
1142
|
+
};
|
|
1143
|
+
}).sort((left, right) => {
|
|
1144
|
+
if (left.supplierId === null) return 1;
|
|
1145
|
+
if (right.supplierId === null) return -1;
|
|
1146
|
+
return (left.supplierName ?? "").localeCompare(right.supplierName ?? "", "pt-BR");
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
__name(buildSuggestion, "buildSuggestion");
|
|
1150
|
+
function stockQuantityOf(quantity, unitsPerPurchase) {
|
|
1151
|
+
return quantityText((0, import_recipe_contracts5.multiply)((0, import_recipe_contracts5.parseDecimal)(quantity), (0, import_recipe_contracts5.parseDecimal)(unitsPerPurchase)));
|
|
1152
|
+
}
|
|
1153
|
+
__name(stockQuantityOf, "stockQuantityOf");
|
|
1154
|
+
|
|
1155
|
+
// src/rules/orderMessage.ts
|
|
1156
|
+
var import_recipe_contracts6 = require("@adatechnology/recipe-contracts");
|
|
1157
|
+
function formatQuantityPt(value) {
|
|
1158
|
+
const [whole, fraction = ""] = value.split(".");
|
|
1159
|
+
const trimmed = fraction.replace(/0+$/, "");
|
|
1160
|
+
return trimmed ? `${whole},${trimmed}` : whole ?? value;
|
|
1161
|
+
}
|
|
1162
|
+
__name(formatQuantityPt, "formatQuantityPt");
|
|
1163
|
+
function formatMoneyPt(value) {
|
|
1164
|
+
const fixed = (0, import_recipe_contracts6.formatDecimal)((0, import_recipe_contracts6.parseDecimal)(value), 2);
|
|
1165
|
+
const [whole = "0", fraction = "00"] = fixed.split(".");
|
|
1166
|
+
return `R$ ${whole.replace(/\B(?=(\d{3})+(?!\d))/g, ".")},${fraction}`;
|
|
1167
|
+
}
|
|
1168
|
+
__name(formatMoneyPt, "formatMoneyPt");
|
|
1169
|
+
function formatDatePt(date) {
|
|
1170
|
+
const [year, month, day] = date.split("-");
|
|
1171
|
+
return `${day}/${month}/${year}`;
|
|
1172
|
+
}
|
|
1173
|
+
__name(formatDatePt, "formatDatePt");
|
|
1174
|
+
function buildOrderMessage(input, options = {}) {
|
|
1175
|
+
const header = [
|
|
1176
|
+
`*Pedido de compra n\xBA ${input.number}* \u2014 ${input.companyName}`,
|
|
1177
|
+
`Fornecedor: ${input.supplierName}`
|
|
1178
|
+
];
|
|
1179
|
+
const lines = input.lines.map((line) => {
|
|
1180
|
+
const sku = line.supplierSku ? ` (c\xF3d. ${line.supplierSku})` : "";
|
|
1181
|
+
const price = options.withPrices ? ` \u2014 ${formatMoneyPt(line.unitPrice)}/${line.purchaseUnit}` : "";
|
|
1182
|
+
return `\u2022 ${formatQuantityPt(line.quantity)} ${line.purchaseUnit} \u2014 ${line.itemName}${sku}${price}`;
|
|
1183
|
+
});
|
|
1184
|
+
const footer = [];
|
|
1185
|
+
if (input.expectedAt) footer.push(`Entrega: ${formatDatePt(input.expectedAt)}`);
|
|
1186
|
+
footer.push(`Pagamento: ${formatPaymentTerms(input.paymentTermDays)}`);
|
|
1187
|
+
if (options.withPrices) footer.push(`Total: ${formatMoneyPt(input.total)}`);
|
|
1188
|
+
if (input.notes.trim()) footer.push(`Obs.: ${input.notes.trim()}`);
|
|
1189
|
+
footer.push("Por favor, confirme o recebimento deste pedido.");
|
|
1190
|
+
return [
|
|
1191
|
+
...header,
|
|
1192
|
+
"",
|
|
1193
|
+
...lines,
|
|
1194
|
+
"",
|
|
1195
|
+
...footer
|
|
1196
|
+
].join("\n");
|
|
1197
|
+
}
|
|
1198
|
+
__name(buildOrderMessage, "buildOrderMessage");
|
|
1199
|
+
|
|
1200
|
+
// src/nfe/nfe.ts
|
|
1201
|
+
var import_recipe_contracts7 = require("@adatechnology/recipe-contracts");
|
|
1202
|
+
|
|
1203
|
+
// src/nfe/xml.ts
|
|
1204
|
+
var ENTITIES = {
|
|
1205
|
+
amp: "&",
|
|
1206
|
+
lt: "<",
|
|
1207
|
+
gt: ">",
|
|
1208
|
+
quot: '"',
|
|
1209
|
+
apos: "'"
|
|
1210
|
+
};
|
|
1211
|
+
function decode(value) {
|
|
1212
|
+
return value.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (match, entity) => {
|
|
1213
|
+
if (entity[0] === "#") {
|
|
1214
|
+
const code = entity[1]?.toLowerCase() === "x" ? Number.parseInt(entity.slice(2), 16) : Number(entity.slice(1));
|
|
1215
|
+
return Number.isFinite(code) && code > 0 && code <= 1114111 ? String.fromCodePoint(code) : match;
|
|
1216
|
+
}
|
|
1217
|
+
return ENTITIES[entity.toLowerCase()] ?? match;
|
|
1218
|
+
});
|
|
1219
|
+
}
|
|
1220
|
+
__name(decode, "decode");
|
|
1221
|
+
function localName(name) {
|
|
1222
|
+
const index = name.indexOf(":");
|
|
1223
|
+
return index === -1 ? name : name.slice(index + 1);
|
|
1224
|
+
}
|
|
1225
|
+
__name(localName, "localName");
|
|
1226
|
+
var XmlParseError = class extends Error {
|
|
1227
|
+
static {
|
|
1228
|
+
__name(this, "XmlParseError");
|
|
1229
|
+
}
|
|
1230
|
+
constructor(message) {
|
|
1231
|
+
super(message);
|
|
1232
|
+
this.name = "XmlParseError";
|
|
1233
|
+
}
|
|
1234
|
+
};
|
|
1235
|
+
function parseXml(source) {
|
|
1236
|
+
const root = {
|
|
1237
|
+
name: "#root",
|
|
1238
|
+
attributes: {},
|
|
1239
|
+
children: [],
|
|
1240
|
+
text: ""
|
|
1241
|
+
};
|
|
1242
|
+
const stack = [
|
|
1243
|
+
root
|
|
1244
|
+
];
|
|
1245
|
+
let index = 0;
|
|
1246
|
+
while (index < source.length) {
|
|
1247
|
+
const open = source.indexOf("<", index);
|
|
1248
|
+
if (open === -1) break;
|
|
1249
|
+
const parent = stack[stack.length - 1];
|
|
1250
|
+
if (open > index) parent.text += decode(source.slice(index, open));
|
|
1251
|
+
if (source.startsWith("<!--", open)) {
|
|
1252
|
+
const end = source.indexOf("-->", open + 4);
|
|
1253
|
+
if (end === -1) throw new XmlParseError("coment\xE1rio sem fim");
|
|
1254
|
+
index = end + 3;
|
|
1255
|
+
continue;
|
|
1256
|
+
}
|
|
1257
|
+
if (source.startsWith("<![CDATA[", open)) {
|
|
1258
|
+
const end = source.indexOf("]]>", open + 9);
|
|
1259
|
+
if (end === -1) throw new XmlParseError("CDATA sem fim");
|
|
1260
|
+
parent.text += source.slice(open + 9, end);
|
|
1261
|
+
index = end + 3;
|
|
1262
|
+
continue;
|
|
1263
|
+
}
|
|
1264
|
+
if (source.startsWith("<!", open)) throw new XmlParseError("DOCTYPE n\xE3o \xE9 aceito");
|
|
1265
|
+
if (source.startsWith("<?", open)) {
|
|
1266
|
+
const end = source.indexOf("?>", open + 2);
|
|
1267
|
+
if (end === -1) throw new XmlParseError("instru\xE7\xE3o sem fim");
|
|
1268
|
+
index = end + 2;
|
|
1269
|
+
continue;
|
|
1270
|
+
}
|
|
1271
|
+
const close = source.indexOf(">", open + 1);
|
|
1272
|
+
if (close === -1) throw new XmlParseError("tag sem fim");
|
|
1273
|
+
const raw = source.slice(open + 1, close);
|
|
1274
|
+
index = close + 1;
|
|
1275
|
+
if (raw.startsWith("/")) {
|
|
1276
|
+
const name = localName(raw.slice(1).trim());
|
|
1277
|
+
const current = stack.pop();
|
|
1278
|
+
if (!current || current.name !== name) throw new XmlParseError(`fechamento inesperado de ${name}`);
|
|
1279
|
+
continue;
|
|
1280
|
+
}
|
|
1281
|
+
const selfClosing = raw.endsWith("/");
|
|
1282
|
+
const body = selfClosing ? raw.slice(0, -1) : raw;
|
|
1283
|
+
const nameMatch = /^\s*([^\s/>]+)/.exec(body);
|
|
1284
|
+
if (!nameMatch?.[1]) throw new XmlParseError("tag sem nome");
|
|
1285
|
+
const attributes = {};
|
|
1286
|
+
const attributePattern = /([^\s=]+)\s*=\s*("([^"]*)"|'([^']*)')/g;
|
|
1287
|
+
let match;
|
|
1288
|
+
const rest = body.slice(nameMatch[0].length);
|
|
1289
|
+
while ((match = attributePattern.exec(rest)) !== null) {
|
|
1290
|
+
attributes[localName(match[1])] = decode(match[3] ?? match[4] ?? "");
|
|
1291
|
+
}
|
|
1292
|
+
const element = {
|
|
1293
|
+
name: localName(nameMatch[1]),
|
|
1294
|
+
attributes,
|
|
1295
|
+
children: [],
|
|
1296
|
+
text: ""
|
|
1297
|
+
};
|
|
1298
|
+
parent.children.push(element);
|
|
1299
|
+
if (!selfClosing) stack.push(element);
|
|
1300
|
+
}
|
|
1301
|
+
if (stack.length !== 1) throw new XmlParseError("documento incompleto");
|
|
1302
|
+
return root;
|
|
1303
|
+
}
|
|
1304
|
+
__name(parseXml, "parseXml");
|
|
1305
|
+
function child(element, name) {
|
|
1306
|
+
return element?.children.find((item) => item.name === name);
|
|
1307
|
+
}
|
|
1308
|
+
__name(child, "child");
|
|
1309
|
+
function childrenOf(element, name) {
|
|
1310
|
+
return element?.children.filter((item) => item.name === name) ?? [];
|
|
1311
|
+
}
|
|
1312
|
+
__name(childrenOf, "childrenOf");
|
|
1313
|
+
function textOf(element, name) {
|
|
1314
|
+
const found = child(element, name);
|
|
1315
|
+
const value = found?.text.trim();
|
|
1316
|
+
return value ? value : null;
|
|
1317
|
+
}
|
|
1318
|
+
__name(textOf, "textOf");
|
|
1319
|
+
function find(element, name) {
|
|
1320
|
+
for (const item of element.children) {
|
|
1321
|
+
if (item.name === name) return item;
|
|
1322
|
+
const nested = find(item, name);
|
|
1323
|
+
if (nested) return nested;
|
|
1324
|
+
}
|
|
1325
|
+
return void 0;
|
|
1326
|
+
}
|
|
1327
|
+
__name(find, "find");
|
|
1328
|
+
|
|
1329
|
+
// src/nfe/nfe.ts
|
|
1330
|
+
var NfeParseError = class extends Error {
|
|
1331
|
+
static {
|
|
1332
|
+
__name(this, "NfeParseError");
|
|
1333
|
+
}
|
|
1334
|
+
constructor(message) {
|
|
1335
|
+
super(message);
|
|
1336
|
+
this.name = "NfeParseError";
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
1339
|
+
function decimalOf(text, fallback = "0") {
|
|
1340
|
+
const value = (text ?? fallback).trim();
|
|
1341
|
+
if (!/^\d+(\.\d+)?$/.test(value)) throw new NfeParseError(`valor inv\xE1lido: ${value}`);
|
|
1342
|
+
const [whole = "0", fraction = ""] = value.split(".");
|
|
1343
|
+
const rounded = fraction.length > 4 ? roundFraction(whole, fraction) : `${whole}.${fraction || "0"}`;
|
|
1344
|
+
return quantityText((0, import_recipe_contracts7.parseDecimal)(rounded));
|
|
1345
|
+
}
|
|
1346
|
+
__name(decimalOf, "decimalOf");
|
|
1347
|
+
function roundFraction(whole, fraction) {
|
|
1348
|
+
const scaled = BigInt(whole + fraction.slice(0, 4));
|
|
1349
|
+
const next = Number(fraction[4] ?? "0");
|
|
1350
|
+
const value = next >= 5 ? scaled + 1n : scaled;
|
|
1351
|
+
const text = value.toString().padStart(5, "0");
|
|
1352
|
+
return `${text.slice(0, -4)}.${text.slice(-4)}`;
|
|
1353
|
+
}
|
|
1354
|
+
__name(roundFraction, "roundFraction");
|
|
1355
|
+
function moneyOf(text) {
|
|
1356
|
+
return money((0, import_recipe_contracts7.parseDecimal)(decimalOf(text)));
|
|
1357
|
+
}
|
|
1358
|
+
__name(moneyOf, "moneyOf");
|
|
1359
|
+
function dateOnly(text) {
|
|
1360
|
+
if (!text) return null;
|
|
1361
|
+
const match = /^(\d{4}-\d{2}-\d{2})/.exec(text.trim());
|
|
1362
|
+
return match?.[1] ?? null;
|
|
1363
|
+
}
|
|
1364
|
+
__name(dateOnly, "dateOnly");
|
|
1365
|
+
function parseItem(det) {
|
|
1366
|
+
const prod = child(det, "prod");
|
|
1367
|
+
if (!prod) throw new NfeParseError("item sem prod");
|
|
1368
|
+
const itemNumber = Number(det.attributes.nItem ?? "0");
|
|
1369
|
+
const ean = textOf(prod, "cEAN");
|
|
1370
|
+
const batches = childrenOf(prod, "rastro").map((rastro) => ({
|
|
1371
|
+
batchCode: textOf(rastro, "nLote") ?? "",
|
|
1372
|
+
quantity: decimalOf(textOf(rastro, "qLote")),
|
|
1373
|
+
expiresAt: dateOnly(textOf(rastro, "dVal"))
|
|
1374
|
+
}));
|
|
1375
|
+
return {
|
|
1376
|
+
itemNumber: Number.isInteger(itemNumber) ? itemNumber : 0,
|
|
1377
|
+
supplierSku: textOf(prod, "cProd") ?? "",
|
|
1378
|
+
ean: ean && ean.toUpperCase() !== "SEM GTIN" ? ean : null,
|
|
1379
|
+
description: textOf(prod, "xProd") ?? "",
|
|
1380
|
+
ncm: textOf(prod, "NCM"),
|
|
1381
|
+
cfop: textOf(prod, "CFOP"),
|
|
1382
|
+
unit: (textOf(prod, "uCom") ?? "UN").toUpperCase(),
|
|
1383
|
+
quantity: decimalOf(textOf(prod, "qCom")),
|
|
1384
|
+
unitPrice: decimalOf(textOf(prod, "vUnCom")),
|
|
1385
|
+
total: moneyOf(textOf(prod, "vProd")),
|
|
1386
|
+
discount: moneyOf(textOf(prod, "vDesc")),
|
|
1387
|
+
freight: moneyOf(textOf(prod, "vFrete")),
|
|
1388
|
+
batches: batches.filter((batch) => batch.batchCode !== "")
|
|
1389
|
+
};
|
|
1390
|
+
}
|
|
1391
|
+
__name(parseItem, "parseItem");
|
|
1392
|
+
function parseNfeXml(xml) {
|
|
1393
|
+
let root;
|
|
1394
|
+
try {
|
|
1395
|
+
root = parseXml(xml);
|
|
1396
|
+
} catch (error) {
|
|
1397
|
+
throw new NfeParseError(error instanceof XmlParseError ? error.message : "XML ileg\xEDvel");
|
|
1398
|
+
}
|
|
1399
|
+
const infNFe = find(root, "infNFe");
|
|
1400
|
+
if (!infNFe) throw new NfeParseError("n\xE3o \xE9 uma NF-e (sem infNFe)");
|
|
1401
|
+
const ide = child(infNFe, "ide");
|
|
1402
|
+
const model = textOf(ide, "mod");
|
|
1403
|
+
if (model && model !== "55" && model !== "65") throw new NfeParseError(`modelo ${model} n\xE3o \xE9 NF-e`);
|
|
1404
|
+
const idAttribute = infNFe.attributes.Id ?? "";
|
|
1405
|
+
const fromProtocol = find(root, "chNFe")?.text.trim() ?? "";
|
|
1406
|
+
const accessKey = (/(\d{44})/.exec(idAttribute)?.[1] ?? fromProtocol).trim();
|
|
1407
|
+
if (!/^\d{44}$/.test(accessKey)) throw new NfeParseError("chave de acesso ausente ou inv\xE1lida");
|
|
1408
|
+
const emit = child(infNFe, "emit");
|
|
1409
|
+
const dest = child(infNFe, "dest");
|
|
1410
|
+
const icmsTot = child(child(infNFe, "total"), "ICMSTot");
|
|
1411
|
+
const items = childrenOf(infNFe, "det").map(parseItem);
|
|
1412
|
+
if (items.length === 0) throw new NfeParseError("nota sem itens");
|
|
1413
|
+
const installments = childrenOf(child(infNFe, "cobr"), "dup").map((dup) => ({
|
|
1414
|
+
number: textOf(dup, "nDup") ?? "",
|
|
1415
|
+
dueDate: dateOnly(textOf(dup, "dVenc")) ?? "",
|
|
1416
|
+
amount: moneyOf(textOf(dup, "vDup"))
|
|
1417
|
+
}));
|
|
1418
|
+
return {
|
|
1419
|
+
accessKey,
|
|
1420
|
+
number: textOf(ide, "nNF") ?? "",
|
|
1421
|
+
series: textOf(ide, "serie"),
|
|
1422
|
+
issuedAt: textOf(ide, "dhEmi") ?? textOf(ide, "dEmi"),
|
|
1423
|
+
issuer: {
|
|
1424
|
+
document: textOf(emit, "CNPJ") ?? textOf(emit, "CPF"),
|
|
1425
|
+
name: textOf(emit, "xNome") ?? "",
|
|
1426
|
+
tradeName: textOf(emit, "xFant")
|
|
1427
|
+
},
|
|
1428
|
+
recipientDocument: textOf(dest, "CNPJ") ?? textOf(dest, "CPF"),
|
|
1429
|
+
items,
|
|
1430
|
+
totals: {
|
|
1431
|
+
products: moneyOf(textOf(icmsTot, "vProd")),
|
|
1432
|
+
freight: moneyOf(textOf(icmsTot, "vFrete")),
|
|
1433
|
+
discount: moneyOf(textOf(icmsTot, "vDesc")),
|
|
1434
|
+
other: moneyOf(textOf(icmsTot, "vOutro")),
|
|
1435
|
+
total: moneyOf(textOf(icmsTot, "vNF"))
|
|
1436
|
+
},
|
|
1437
|
+
installments: installments.filter((item) => item.dueDate !== "")
|
|
1438
|
+
};
|
|
1439
|
+
}
|
|
1440
|
+
__name(parseNfeXml, "parseNfeXml");
|
|
1441
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1442
|
+
0 && (module.exports = {
|
|
1443
|
+
DEFAULT_PURCHASE_SCOPES,
|
|
1444
|
+
DISPATCH_CHANNEL,
|
|
1445
|
+
DISPATCH_CHANNELS,
|
|
1446
|
+
DIVERGENCE,
|
|
1447
|
+
DispatchUnavailableError,
|
|
1448
|
+
INTEGRATION_STATUS,
|
|
1449
|
+
InstallmentsMismatchError,
|
|
1450
|
+
InvalidOrderTransitionError,
|
|
1451
|
+
InvoiceAlreadyReceivedError,
|
|
1452
|
+
MONEY_SCALE,
|
|
1453
|
+
NfeInvalidError,
|
|
1454
|
+
NfeParseError,
|
|
1455
|
+
OPEN_ORDER_STATUSES,
|
|
1456
|
+
ORDER_ACTION,
|
|
1457
|
+
ORDER_SORTS,
|
|
1458
|
+
ORDER_STATUS,
|
|
1459
|
+
ORDER_STATUSES,
|
|
1460
|
+
OrderLineNotFoundError,
|
|
1461
|
+
OrderNotFoundError,
|
|
1462
|
+
PAYMENT_STATUS,
|
|
1463
|
+
PURCHASE_ERROR_CODES,
|
|
1464
|
+
PURCHASE_LIMITS,
|
|
1465
|
+
PurchaseConfigMissingError,
|
|
1466
|
+
PurchaseDisabledError,
|
|
1467
|
+
PurchaseError,
|
|
1468
|
+
PurchaseItemNotFoundError,
|
|
1469
|
+
PurchaseUnauthenticatedError,
|
|
1470
|
+
REPORT_GROUP,
|
|
1471
|
+
REPORT_GROUPS,
|
|
1472
|
+
ReceiptNotFoundError,
|
|
1473
|
+
ReportRangeTooLongError,
|
|
1474
|
+
SORT_ORDER,
|
|
1475
|
+
SUPPLIER_SORTS,
|
|
1476
|
+
SelfApprovalError,
|
|
1477
|
+
SupplierDocumentTakenError,
|
|
1478
|
+
SupplierInUseError,
|
|
1479
|
+
SupplierInactiveError,
|
|
1480
|
+
SupplierItemNotFoundError,
|
|
1481
|
+
SupplierItemTakenError,
|
|
1482
|
+
SupplierNoContactError,
|
|
1483
|
+
SupplierNotFoundError,
|
|
1484
|
+
UNIT_FACTOR_ONE,
|
|
1485
|
+
ZERO,
|
|
1486
|
+
add,
|
|
1487
|
+
addDays,
|
|
1488
|
+
allocateExtras,
|
|
1489
|
+
allowedActions,
|
|
1490
|
+
buildInstallments,
|
|
1491
|
+
buildOrderMessage,
|
|
1492
|
+
buildSuggestion,
|
|
1493
|
+
canPerform,
|
|
1494
|
+
centsOf,
|
|
1495
|
+
civilDateIn,
|
|
1496
|
+
compare,
|
|
1497
|
+
computeTotals,
|
|
1498
|
+
createOrderSchema,
|
|
1499
|
+
createSupplierItemSchema,
|
|
1500
|
+
createSupplierSchema,
|
|
1501
|
+
divergenceOf,
|
|
1502
|
+
exportFormatSchema,
|
|
1503
|
+
formatMoneyPt,
|
|
1504
|
+
formatPaymentTerms,
|
|
1505
|
+
formatQuantityPt,
|
|
1506
|
+
isCivilDate,
|
|
1507
|
+
isOrderLate,
|
|
1508
|
+
itemRefsLookupSchema,
|
|
1509
|
+
landedUnitCost,
|
|
1510
|
+
lineTotal,
|
|
1511
|
+
listOrdersQuerySchema,
|
|
1512
|
+
listReceiptsQuerySchema,
|
|
1513
|
+
listSupplierItemsQuerySchema,
|
|
1514
|
+
listSuppliersQuerySchema,
|
|
1515
|
+
money,
|
|
1516
|
+
moneySchema,
|
|
1517
|
+
multiply,
|
|
1518
|
+
nfePreviewSchema,
|
|
1519
|
+
optionalReasonSchema,
|
|
1520
|
+
orderLineInputSchema,
|
|
1521
|
+
ordersFromSuggestionSchema,
|
|
1522
|
+
packsFor,
|
|
1523
|
+
parseNfeXml,
|
|
1524
|
+
parsePaymentTerms,
|
|
1525
|
+
pendingQuantity,
|
|
1526
|
+
priceHistoryQuerySchema,
|
|
1527
|
+
quantityText,
|
|
1528
|
+
reasonSchema,
|
|
1529
|
+
receiptLineInputSchema,
|
|
1530
|
+
receiveSchema,
|
|
1531
|
+
requiresApproval,
|
|
1532
|
+
sendOrderSchema,
|
|
1533
|
+
shortageOf,
|
|
1534
|
+
spendReportQuerySchema,
|
|
1535
|
+
statusAfterReceipt,
|
|
1536
|
+
statusAfterSend,
|
|
1537
|
+
stockQuantityOf,
|
|
1538
|
+
subtract,
|
|
1539
|
+
suggestionQuerySchema,
|
|
1540
|
+
supplierLookupSchema,
|
|
1541
|
+
supplierPerformanceQuerySchema,
|
|
1542
|
+
toStockQuantity,
|
|
1543
|
+
updateOrderSchema,
|
|
1544
|
+
updateSupplierItemSchema,
|
|
1545
|
+
updateSupplierSchema,
|
|
1546
|
+
weightedAverageCost
|
|
1547
|
+
});
|
|
1548
|
+
//# sourceMappingURL=index.cjs.map
|