@infuro/cms-core 1.0.66 → 1.0.68
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/dist/admin.cjs +214 -32
- package/dist/admin.js +214 -32
- package/dist/api.cjs +45 -42
- package/dist/api.js +12 -9
- package/dist/auth.cjs +51 -51
- package/dist/auth.js +3 -3
- package/dist/{chunk-QYRXLURF.cjs → chunk-22XKONB5.cjs} +640 -145
- package/dist/{chunk-MVHDC3XF.js → chunk-5NOZSG6J.js} +22 -11
- package/dist/{chunk-TU23GJPJ.js → chunk-7JCCPQEF.js} +1 -1
- package/dist/{chunk-WMMZZRDJ.cjs → chunk-GN4LKG4V.cjs} +1 -1
- package/dist/{chunk-KM2I6AFP.cjs → chunk-HBUV73RB.cjs} +9 -9
- package/dist/chunk-IKTBZJHT.js +16 -0
- package/dist/chunk-M5GPOPGX.cjs +49 -0
- package/dist/{chunk-T33KU5G5.js → chunk-MYUGQHU4.js} +548 -53
- package/dist/{chunk-NV5IRHI3.js → chunk-N4LBZXBG.js} +2 -2
- package/dist/{chunk-J7FMZK66.cjs → chunk-O4AFPDQ4.cjs} +22 -11
- package/dist/{chunk-4U6DATGZ.cjs → chunk-OV5TRZET.cjs} +7 -7
- package/dist/chunk-P6S4XFZO.cjs +18 -0
- package/dist/{chunk-RCE5SJBA.js → chunk-SKJH7PX4.js} +1 -1
- package/dist/{chunk-5MZFQRDW.cjs → chunk-SMHQV3WL.cjs} +4 -4
- package/dist/chunk-SUUD7OUG.cjs +455 -0
- package/dist/{chunk-XFDIIVZB.cjs → chunk-UMC7W5G3.cjs} +3 -3
- package/dist/chunk-WKDBMDUX.js +45 -0
- package/dist/{chunk-6DBDJ4VD.js → chunk-Z5G54EGD.js} +2 -2
- package/dist/chunk-Z5OB5QGN.js +465 -0
- package/dist/chunk-ZIT4IP6N.js +445 -0
- package/dist/chunk-ZX5U3VCB.cjs +471 -0
- package/dist/{chunk-NR44CK5E.js → chunk-ZY64VJMS.js} +1 -1
- package/dist/email-queue-3QBQ63F7.js +8 -0
- package/dist/{email-queue-4OY45JIU.cjs → email-queue-K4E4OFI7.cjs} +9 -6
- package/dist/{emit-order-notification-trigger-ZD5YN3JM.cjs → emit-order-notification-trigger-ROASTYQP.cjs} +3 -3
- package/dist/{emit-order-notification-trigger-EJJZ6XTS.js → emit-order-notification-trigger-YUQTPELC.js} +1 -1
- package/dist/{erp-order-invoice-YUSPZ7CE.cjs → erp-order-invoice-AZ5MX2JB.cjs} +8 -5
- package/dist/{erp-order-invoice-H6HCJ7YS.js → erp-order-invoice-XSYHAMBS.js} +4 -1
- package/dist/generate-local-invoice-pdf-BQTCL5MW.cjs +12 -0
- package/dist/generate-local-invoice-pdf-OCFG47SE.js +3 -0
- package/dist/index.cjs +337 -330
- package/dist/index.d.cts +21 -2
- package/dist/index.d.ts +21 -2
- package/dist/index.js +18 -15
- package/dist/invoice-templates-GZ33OENC.js +2 -0
- package/dist/invoice-templates-RB462AQN.cjs +19 -0
- package/dist/migrations/1783300000000-CreateOrderAssigneesTable.ts +43 -0
- package/dist/{order-assignees-handlers-NPKD64P6.cjs → order-assignees-handlers-KF7RMTV5.cjs} +77 -8
- package/dist/{order-assignees-handlers-LKMAGLG6.js → order-assignees-handlers-L3RVKUBS.js} +77 -8
- package/dist/{order-completion-otp-handlers-NK65LOLV.cjs → order-completion-otp-handlers-DDQREULW.cjs} +75 -18
- package/dist/{order-completion-otp-handlers-LFRHXWK4.js → order-completion-otp-handlers-NROWBGXN.js} +73 -16
- package/dist/{order-notification-dispatcher-LL7ETKLU.cjs → order-notification-dispatcher-6HCJ6Q75.cjs} +12 -9
- package/dist/{order-notification-dispatcher-ECPFI7CD.js → order-notification-dispatcher-KM2SY7TX.js} +7 -4
- package/dist/{rbac-debug-db-5R6XFPSV.js → rbac-debug-db-DFZWZ6GV.js} +1 -1
- package/dist/{rbac-debug-db-JKK2MCWF.cjs → rbac-debug-db-KWV7D5PQ.cjs} +2 -2
- package/dist/resolve-invoice-asset-url-JEZRENJW.cjs +11 -0
- package/dist/resolve-invoice-asset-url-QZ2FRDHI.js +2 -0
- package/package.json +1 -1
- package/dist/chunk-GLD263KW.cjs +0 -862
- package/dist/chunk-OPJEV44S.js +0 -847
- package/dist/email-queue-HGXB2FIS.js +0 -5
package/dist/chunk-OPJEV44S.js
DELETED
|
@@ -1,847 +0,0 @@
|
|
|
1
|
-
import { isErpIntegrationEnabled } from './chunk-JC6DLWTE.js';
|
|
2
|
-
import { mergeEmailLayoutCompanyDetails, getCompanyDetailsFromSettings } from './chunk-YRCKEGL2.js';
|
|
3
|
-
import { __name } from './chunk-SHUYVCID.js';
|
|
4
|
-
import { rgb, PDFDocument, StandardFonts } from 'pdf-lib';
|
|
5
|
-
|
|
6
|
-
// src/plugins/erp/erp-response-map.ts
|
|
7
|
-
function pickString(o, keys) {
|
|
8
|
-
for (const k of keys) {
|
|
9
|
-
const v = o[k];
|
|
10
|
-
if (typeof v === "string" && v.trim()) return v.trim();
|
|
11
|
-
}
|
|
12
|
-
return void 0;
|
|
13
|
-
}
|
|
14
|
-
__name(pickString, "pickString");
|
|
15
|
-
function unwrapErpReadData(json) {
|
|
16
|
-
if (!json || typeof json !== "object") return null;
|
|
17
|
-
const o = json;
|
|
18
|
-
const d = o.data;
|
|
19
|
-
if (d && typeof d === "object" && !Array.isArray(d)) return d;
|
|
20
|
-
return o;
|
|
21
|
-
}
|
|
22
|
-
__name(unwrapErpReadData, "unwrapErpReadData");
|
|
23
|
-
function firstObject(data, keys) {
|
|
24
|
-
for (const k of keys) {
|
|
25
|
-
const v = data[k];
|
|
26
|
-
if (v && typeof v === "object" && !Array.isArray(v)) return v;
|
|
27
|
-
}
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
__name(firstObject, "firstObject");
|
|
31
|
-
function extractEvents(src) {
|
|
32
|
-
const timeline = src.timeline ?? src.events ?? src.history ?? src.trackingEvents;
|
|
33
|
-
if (!Array.isArray(timeline) || !timeline.length) return void 0;
|
|
34
|
-
const events = [];
|
|
35
|
-
for (const row of timeline) {
|
|
36
|
-
if (!row || typeof row !== "object") continue;
|
|
37
|
-
const r = row;
|
|
38
|
-
const at = pickString(r, [
|
|
39
|
-
"at",
|
|
40
|
-
"timestamp",
|
|
41
|
-
"date",
|
|
42
|
-
"occurredAt"
|
|
43
|
-
]) ?? (r.at instanceof Date ? r.at.toISOString() : void 0);
|
|
44
|
-
const label = pickString(r, [
|
|
45
|
-
"label",
|
|
46
|
-
"status",
|
|
47
|
-
"title",
|
|
48
|
-
"message",
|
|
49
|
-
"description"
|
|
50
|
-
]);
|
|
51
|
-
const detail = pickString(r, [
|
|
52
|
-
"detail",
|
|
53
|
-
"notes",
|
|
54
|
-
"description"
|
|
55
|
-
]);
|
|
56
|
-
if (at || label || detail) events.push({
|
|
57
|
-
at,
|
|
58
|
-
label,
|
|
59
|
-
detail
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
return events.length ? events : void 0;
|
|
63
|
-
}
|
|
64
|
-
__name(extractEvents, "extractEvents");
|
|
65
|
-
function mapErpPayloadToFulfillment(data) {
|
|
66
|
-
const nested = firstObject(data, [
|
|
67
|
-
"fulfillment",
|
|
68
|
-
"packaging",
|
|
69
|
-
"shipment",
|
|
70
|
-
"shipping",
|
|
71
|
-
"delivery"
|
|
72
|
-
]);
|
|
73
|
-
const src = nested || data;
|
|
74
|
-
const status = pickString(src, [
|
|
75
|
-
"status",
|
|
76
|
-
"fulfillmentStatus",
|
|
77
|
-
"state",
|
|
78
|
-
"label",
|
|
79
|
-
"packagingStatus"
|
|
80
|
-
]);
|
|
81
|
-
const trackingId = pickString(src, [
|
|
82
|
-
"trackingId",
|
|
83
|
-
"tracking_id",
|
|
84
|
-
"trackingNumber",
|
|
85
|
-
"awb",
|
|
86
|
-
"trackingUrl"
|
|
87
|
-
]);
|
|
88
|
-
const events = extractEvents(src);
|
|
89
|
-
if (!status && !trackingId && !(events && events.length)) return void 0;
|
|
90
|
-
return {
|
|
91
|
-
status,
|
|
92
|
-
trackingId,
|
|
93
|
-
events
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
__name(mapErpPayloadToFulfillment, "mapErpPayloadToFulfillment");
|
|
97
|
-
function mapErpPayloadToInvoiceNumber(data) {
|
|
98
|
-
const nested = firstObject(data, [
|
|
99
|
-
"invoice",
|
|
100
|
-
"latestInvoice",
|
|
101
|
-
"postedInvoice"
|
|
102
|
-
]);
|
|
103
|
-
const src = nested || data;
|
|
104
|
-
return pickString(src, [
|
|
105
|
-
"invoiceNumber",
|
|
106
|
-
"invoice_number",
|
|
107
|
-
"number",
|
|
108
|
-
"name",
|
|
109
|
-
"id"
|
|
110
|
-
]);
|
|
111
|
-
}
|
|
112
|
-
__name(mapErpPayloadToInvoiceNumber, "mapErpPayloadToInvoiceNumber");
|
|
113
|
-
function extractChildOrderRefsFromSalePayload(data) {
|
|
114
|
-
const lists = [
|
|
115
|
-
data.returns,
|
|
116
|
-
data.returnOrders,
|
|
117
|
-
data.relatedReturns,
|
|
118
|
-
data.childOrders,
|
|
119
|
-
data.children
|
|
120
|
-
];
|
|
121
|
-
const seen = /* @__PURE__ */ new Set();
|
|
122
|
-
const out = [];
|
|
123
|
-
for (const list of lists) {
|
|
124
|
-
if (!Array.isArray(list)) continue;
|
|
125
|
-
for (const item of list) {
|
|
126
|
-
if (!item || typeof item !== "object") continue;
|
|
127
|
-
const o = item;
|
|
128
|
-
const ref = pickString(o, [
|
|
129
|
-
"platformReturnId",
|
|
130
|
-
"platform_return_id",
|
|
131
|
-
"refId",
|
|
132
|
-
"ref_id"
|
|
133
|
-
]) ?? (typeof o.id === "string" ? o.id : void 0);
|
|
134
|
-
if (!ref || seen.has(ref)) continue;
|
|
135
|
-
seen.add(ref);
|
|
136
|
-
const t = (pickString(o, [
|
|
137
|
-
"kind",
|
|
138
|
-
"type",
|
|
139
|
-
"orderKind"
|
|
140
|
-
]) || "").toLowerCase();
|
|
141
|
-
const orderKind = /replac/.test(t) ? "replacement" : "return";
|
|
142
|
-
out.push({
|
|
143
|
-
ref,
|
|
144
|
-
orderKind
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return out;
|
|
149
|
-
}
|
|
150
|
-
__name(extractChildOrderRefsFromSalePayload, "extractChildOrderRefsFromSalePayload");
|
|
151
|
-
|
|
152
|
-
// src/lib/invoice/invoice-templates.ts
|
|
153
|
-
var INVOICE_TEMPLATE_IDS = [
|
|
154
|
-
"classic",
|
|
155
|
-
"modern",
|
|
156
|
-
"compact",
|
|
157
|
-
"bold"
|
|
158
|
-
];
|
|
159
|
-
var INVOICE_TEMPLATE_OPTIONS = [
|
|
160
|
-
{
|
|
161
|
-
id: "classic",
|
|
162
|
-
label: "Classic",
|
|
163
|
-
description: "Traditional invoice with header, lined items, and totals footer."
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
id: "modern",
|
|
167
|
-
label: "Modern",
|
|
168
|
-
description: "Clean layout with accent bar and spacious typography."
|
|
169
|
-
},
|
|
170
|
-
{
|
|
171
|
-
id: "compact",
|
|
172
|
-
label: "Compact",
|
|
173
|
-
description: "Dense single-page layout for high line-item counts."
|
|
174
|
-
},
|
|
175
|
-
{
|
|
176
|
-
id: "bold",
|
|
177
|
-
label: "Bold",
|
|
178
|
-
description: "Strong header band and clear payment summary block."
|
|
179
|
-
}
|
|
180
|
-
];
|
|
181
|
-
function normalizeInvoiceTemplateId(raw) {
|
|
182
|
-
const v = String(raw ?? "").trim().toLowerCase();
|
|
183
|
-
return INVOICE_TEMPLATE_IDS.includes(v) ? v : "classic";
|
|
184
|
-
}
|
|
185
|
-
__name(normalizeInvoiceTemplateId, "normalizeInvoiceTemplateId");
|
|
186
|
-
|
|
187
|
-
// src/lib/invoice/generate-local-invoice-pdf.ts
|
|
188
|
-
var THEMES = {
|
|
189
|
-
classic: {
|
|
190
|
-
accent: rgb(0.15, 0.25, 0.45),
|
|
191
|
-
headerBg: rgb(0.96, 0.96, 0.97),
|
|
192
|
-
headerText: rgb(0.12, 0.12, 0.14),
|
|
193
|
-
text: rgb(0.12, 0.12, 0.14),
|
|
194
|
-
muted: rgb(0.4, 0.42, 0.45),
|
|
195
|
-
line: rgb(0.82, 0.84, 0.86),
|
|
196
|
-
bandText: rgb(1, 1, 1)
|
|
197
|
-
},
|
|
198
|
-
modern: {
|
|
199
|
-
accent: rgb(0.1, 0.45, 0.55),
|
|
200
|
-
headerBg: rgb(0.93, 0.97, 0.98),
|
|
201
|
-
headerText: rgb(0.1, 0.12, 0.14),
|
|
202
|
-
text: rgb(0.1, 0.12, 0.14),
|
|
203
|
-
muted: rgb(0.38, 0.42, 0.45),
|
|
204
|
-
line: rgb(0.78, 0.86, 0.88),
|
|
205
|
-
bandText: rgb(1, 1, 1)
|
|
206
|
-
},
|
|
207
|
-
compact: {
|
|
208
|
-
accent: rgb(0.2, 0.2, 0.22),
|
|
209
|
-
headerBg: rgb(0.95, 0.95, 0.95),
|
|
210
|
-
headerText: rgb(0.1, 0.1, 0.1),
|
|
211
|
-
text: rgb(0.1, 0.1, 0.1),
|
|
212
|
-
muted: rgb(0.45, 0.45, 0.45),
|
|
213
|
-
line: rgb(0.85, 0.85, 0.85),
|
|
214
|
-
bandText: rgb(1, 1, 1)
|
|
215
|
-
},
|
|
216
|
-
bold: {
|
|
217
|
-
accent: rgb(0.55, 0.12, 0.12),
|
|
218
|
-
headerBg: rgb(0.55, 0.12, 0.12),
|
|
219
|
-
headerText: rgb(1, 1, 1),
|
|
220
|
-
text: rgb(0.1, 0.1, 0.1),
|
|
221
|
-
muted: rgb(0.4, 0.4, 0.4),
|
|
222
|
-
line: rgb(0.8, 0.75, 0.75),
|
|
223
|
-
bandText: rgb(1, 1, 1)
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
function money(amount, currency) {
|
|
227
|
-
const n = Number.isFinite(amount) ? amount : 0;
|
|
228
|
-
const code = (currency || "INR").trim().toUpperCase() || "INR";
|
|
229
|
-
return `${code} ${n.toFixed(2)}`;
|
|
230
|
-
}
|
|
231
|
-
__name(money, "money");
|
|
232
|
-
function fmtDate(d) {
|
|
233
|
-
const date = d instanceof Date ? d : new Date(d);
|
|
234
|
-
if (Number.isNaN(date.getTime())) return String(d);
|
|
235
|
-
const y = date.getFullYear();
|
|
236
|
-
const m = String(date.getMonth() + 1).padStart(2, "0");
|
|
237
|
-
const day = String(date.getDate()).padStart(2, "0");
|
|
238
|
-
return `${day}-${m}-${y}`;
|
|
239
|
-
}
|
|
240
|
-
__name(fmtDate, "fmtDate");
|
|
241
|
-
function winAnsiSafe(input) {
|
|
242
|
-
return String(input ?? "").replace(/\u20B9/g, "INR ").replace(/\u20AC/g, "EUR ").replace(/\u00A3/g, "GBP ").replace(/[^\x09\x0A\x0D\x20-\x7E]/g, "?");
|
|
243
|
-
}
|
|
244
|
-
__name(winAnsiSafe, "winAnsiSafe");
|
|
245
|
-
function addrLines(a) {
|
|
246
|
-
if (!a) return [];
|
|
247
|
-
const out = [];
|
|
248
|
-
if (a.line1?.trim()) out.push(a.line1.trim());
|
|
249
|
-
if (a.line2?.trim()) out.push(a.line2.trim());
|
|
250
|
-
const cityLine = [
|
|
251
|
-
a.city,
|
|
252
|
-
a.state,
|
|
253
|
-
a.postalCode
|
|
254
|
-
].filter((x) => x && String(x).trim()).join(", ");
|
|
255
|
-
if (cityLine) out.push(cityLine);
|
|
256
|
-
if (a.country?.trim()) out.push(a.country.trim());
|
|
257
|
-
return out.map(winAnsiSafe);
|
|
258
|
-
}
|
|
259
|
-
__name(addrLines, "addrLines");
|
|
260
|
-
function companyHeaderDetailLines(company) {
|
|
261
|
-
const lines = [];
|
|
262
|
-
if (company.supportEmail?.trim()) {
|
|
263
|
-
lines.push(`Email: ${company.supportEmail.trim()}`);
|
|
264
|
-
}
|
|
265
|
-
if (company.supportPhone?.trim()) {
|
|
266
|
-
lines.push(`Phone: ${company.supportPhone.trim()}`);
|
|
267
|
-
}
|
|
268
|
-
if (company.address?.trim()) {
|
|
269
|
-
const parts = company.address.split(/\r?\n/).map((l) => l.trim()).filter(Boolean);
|
|
270
|
-
if (parts.length === 1) {
|
|
271
|
-
lines.push(`Address: ${parts[0]}`);
|
|
272
|
-
} else if (parts.length > 1) {
|
|
273
|
-
lines.push(`Address: ${parts[0]}`);
|
|
274
|
-
lines.push(...parts.slice(1));
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return lines.map(winAnsiSafe);
|
|
278
|
-
}
|
|
279
|
-
__name(companyHeaderDetailLines, "companyHeaderDetailLines");
|
|
280
|
-
function drawText(page, text, x, y, font, size, color) {
|
|
281
|
-
const safe = winAnsiSafe(text).slice(0, 120);
|
|
282
|
-
if (!safe) return;
|
|
283
|
-
page.drawText(safe, {
|
|
284
|
-
x,
|
|
285
|
-
y,
|
|
286
|
-
size,
|
|
287
|
-
font,
|
|
288
|
-
color
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
__name(drawText, "drawText");
|
|
292
|
-
function wrapText(text, font, size, maxWidth) {
|
|
293
|
-
const words = winAnsiSafe(text).split(/\s+/).filter(Boolean);
|
|
294
|
-
if (!words.length) return [
|
|
295
|
-
""
|
|
296
|
-
];
|
|
297
|
-
const lines = [];
|
|
298
|
-
let cur = "";
|
|
299
|
-
for (const w of words) {
|
|
300
|
-
const next = cur ? `${cur} ${w}` : w;
|
|
301
|
-
if (font.widthOfTextAtSize(next, size) <= maxWidth) {
|
|
302
|
-
cur = next;
|
|
303
|
-
} else {
|
|
304
|
-
if (cur) lines.push(cur);
|
|
305
|
-
cur = w;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
if (cur) lines.push(cur);
|
|
309
|
-
return lines.slice(0, 4);
|
|
310
|
-
}
|
|
311
|
-
__name(wrapText, "wrapText");
|
|
312
|
-
async function tryEmbedLogo(doc, logoUrl, maxW, maxH) {
|
|
313
|
-
const url = String(logoUrl ?? "").trim();
|
|
314
|
-
if (!url || !/^https?:\/\//i.test(url)) return null;
|
|
315
|
-
try {
|
|
316
|
-
const res = await fetch(url, {
|
|
317
|
-
signal: AbortSignal.timeout(8e3)
|
|
318
|
-
});
|
|
319
|
-
if (!res.ok) return null;
|
|
320
|
-
const buf = new Uint8Array(await res.arrayBuffer());
|
|
321
|
-
if (buf.byteLength < 24 || buf.byteLength > 25e5) return null;
|
|
322
|
-
const ct = (res.headers.get("content-type") || "").toLowerCase();
|
|
323
|
-
const isPng = ct.includes("png") || buf[0] === 137 && buf[1] === 80 && buf[2] === 78 && buf[3] === 71;
|
|
324
|
-
const isJpg = ct.includes("jpeg") || ct.includes("jpg") || buf[0] === 255 && buf[1] === 216;
|
|
325
|
-
let image;
|
|
326
|
-
if (isPng) image = await doc.embedPng(buf);
|
|
327
|
-
else if (isJpg) image = await doc.embedJpg(buf);
|
|
328
|
-
else {
|
|
329
|
-
try {
|
|
330
|
-
image = await doc.embedPng(buf);
|
|
331
|
-
} catch {
|
|
332
|
-
image = await doc.embedJpg(buf);
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
const scale = Math.min(maxW / image.width, maxH / image.height, 1);
|
|
336
|
-
return {
|
|
337
|
-
image,
|
|
338
|
-
width: image.width * scale,
|
|
339
|
-
height: image.height * scale
|
|
340
|
-
};
|
|
341
|
-
} catch {
|
|
342
|
-
return null;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
__name(tryEmbedLogo, "tryEmbedLogo");
|
|
346
|
-
async function generateLocalInvoicePdf(input) {
|
|
347
|
-
const templateId = normalizeInvoiceTemplateId(input.templateId);
|
|
348
|
-
const theme = THEMES[templateId];
|
|
349
|
-
const doc = await PDFDocument.create();
|
|
350
|
-
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
351
|
-
const fontBold = await doc.embedFont(StandardFonts.HelveticaBold);
|
|
352
|
-
const pageWidth = 595.28;
|
|
353
|
-
const pageHeight = 841.89;
|
|
354
|
-
const margin = templateId === "compact" ? 36 : 48;
|
|
355
|
-
let page = doc.addPage([
|
|
356
|
-
pageWidth,
|
|
357
|
-
pageHeight
|
|
358
|
-
]);
|
|
359
|
-
let y = pageHeight - margin;
|
|
360
|
-
const companyName = input.company.name || "Store";
|
|
361
|
-
const logo = await tryEmbedLogo(doc, input.company.logoUrl, 120, 40);
|
|
362
|
-
const ensureSpace = /* @__PURE__ */ __name((need) => {
|
|
363
|
-
if (y - need < margin + 40) {
|
|
364
|
-
page = doc.addPage([
|
|
365
|
-
pageWidth,
|
|
366
|
-
pageHeight
|
|
367
|
-
]);
|
|
368
|
-
y = pageHeight - margin;
|
|
369
|
-
}
|
|
370
|
-
}, "ensureSpace");
|
|
371
|
-
const companyDetails = companyHeaderDetailLines(input.company);
|
|
372
|
-
const detailLineH = 11;
|
|
373
|
-
const detailsBlockH = companyDetails.length * detailLineH;
|
|
374
|
-
if (templateId === "bold" || templateId === "modern") {
|
|
375
|
-
const contentH = 22 + 16 + detailsBlockH;
|
|
376
|
-
const bandH = Math.max(templateId === "bold" ? 72 : 56, logo ? logo.height + 24 : 56, contentH + 20);
|
|
377
|
-
page.drawRectangle({
|
|
378
|
-
x: 0,
|
|
379
|
-
y: pageHeight - bandH,
|
|
380
|
-
width: pageWidth,
|
|
381
|
-
height: bandH,
|
|
382
|
-
color: theme.accent
|
|
383
|
-
});
|
|
384
|
-
if (logo) {
|
|
385
|
-
page.drawImage(logo.image, {
|
|
386
|
-
x: pageWidth - margin - logo.width,
|
|
387
|
-
y: pageHeight - bandH + (bandH - logo.height) / 2,
|
|
388
|
-
width: logo.width,
|
|
389
|
-
height: logo.height
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
let hy = pageHeight - 26;
|
|
393
|
-
drawText(page, "TAX INVOICE", margin, hy, fontBold, 18, theme.bandText);
|
|
394
|
-
hy -= 18;
|
|
395
|
-
drawText(page, companyName, margin, hy, font, 11, theme.bandText);
|
|
396
|
-
hy -= 14;
|
|
397
|
-
for (const line of companyDetails) {
|
|
398
|
-
drawText(page, line, margin, hy, font, 8, theme.bandText);
|
|
399
|
-
hy -= detailLineH;
|
|
400
|
-
}
|
|
401
|
-
y = pageHeight - bandH - 24;
|
|
402
|
-
} else {
|
|
403
|
-
const contentH = 18 + 14 + detailsBlockH;
|
|
404
|
-
const headerH = Math.max(44, logo ? logo.height + 16 : 44, contentH + 20);
|
|
405
|
-
page.drawRectangle({
|
|
406
|
-
x: margin,
|
|
407
|
-
y: y - headerH + 16,
|
|
408
|
-
width: pageWidth - margin * 2,
|
|
409
|
-
height: headerH,
|
|
410
|
-
color: theme.headerBg
|
|
411
|
-
});
|
|
412
|
-
if (logo) {
|
|
413
|
-
page.drawImage(logo.image, {
|
|
414
|
-
x: pageWidth - margin - 12 - logo.width,
|
|
415
|
-
y: y - headerH + 16 + (headerH - logo.height) / 2,
|
|
416
|
-
width: logo.width,
|
|
417
|
-
height: logo.height
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
let hy = y + 2;
|
|
421
|
-
drawText(page, "TAX INVOICE", margin + 12, hy, fontBold, 16, theme.accent);
|
|
422
|
-
hy -= 16;
|
|
423
|
-
drawText(page, companyName, margin + 12, hy, font, 10, theme.muted);
|
|
424
|
-
hy -= 13;
|
|
425
|
-
for (const line of companyDetails) {
|
|
426
|
-
drawText(page, line, margin + 12, hy, font, 8, theme.muted);
|
|
427
|
-
hy -= detailLineH;
|
|
428
|
-
}
|
|
429
|
-
y -= headerH + 16;
|
|
430
|
-
}
|
|
431
|
-
drawText(page, `Invoice #: ${input.orderNumber}`, margin, y, fontBold, 11, theme.text);
|
|
432
|
-
drawText(page, `Date: ${fmtDate(input.orderDate)}`, pageWidth / 2, y, font, 10, theme.muted);
|
|
433
|
-
y -= 22;
|
|
434
|
-
ensureSpace(100);
|
|
435
|
-
const colW = (pageWidth - margin * 2 - 16) / 2;
|
|
436
|
-
drawText(page, "Bill to", margin, y, fontBold, 10, theme.accent);
|
|
437
|
-
drawText(page, "Ship to", margin + colW + 16, y, fontBold, 10, theme.accent);
|
|
438
|
-
y -= 14;
|
|
439
|
-
const bill = [
|
|
440
|
-
input.customerName,
|
|
441
|
-
input.customerEmail || "",
|
|
442
|
-
input.customerPhone || "",
|
|
443
|
-
...addrLines(input.billingAddress)
|
|
444
|
-
].filter(Boolean);
|
|
445
|
-
const ship = addrLines(input.shippingAddress).length ? [
|
|
446
|
-
input.customerName,
|
|
447
|
-
...addrLines(input.shippingAddress)
|
|
448
|
-
] : bill;
|
|
449
|
-
const rows = Math.max(bill.length, ship.length, 1);
|
|
450
|
-
for (let i = 0; i < rows; i++) {
|
|
451
|
-
ensureSpace(14);
|
|
452
|
-
if (bill[i]) drawText(page, bill[i], margin, y, font, 9, theme.text);
|
|
453
|
-
if (ship[i]) drawText(page, ship[i], margin + colW + 16, y, font, 9, theme.text);
|
|
454
|
-
y -= 12;
|
|
455
|
-
}
|
|
456
|
-
y -= 12;
|
|
457
|
-
const right = pageWidth - margin;
|
|
458
|
-
const contentRight = right - 4;
|
|
459
|
-
const colTotalW = 100;
|
|
460
|
-
const colTaxW = 100;
|
|
461
|
-
const colPriceW = 100;
|
|
462
|
-
const colQtyW = 40;
|
|
463
|
-
const totalRight = contentRight;
|
|
464
|
-
const taxRight = totalRight - colTotalW;
|
|
465
|
-
const priceRight = taxRight - colTaxW;
|
|
466
|
-
const qtyRight = priceRight - colPriceW;
|
|
467
|
-
const itemMaxW = Math.max(80, qtyRight - colQtyW - margin - 8);
|
|
468
|
-
const drawRight = /* @__PURE__ */ __name((text, rightEdge, atY, f, size, color) => {
|
|
469
|
-
const safe = winAnsiSafe(text).slice(0, 40);
|
|
470
|
-
if (!safe) return;
|
|
471
|
-
const w = f.widthOfTextAtSize(safe, size);
|
|
472
|
-
page.drawText(safe, {
|
|
473
|
-
x: rightEdge - w,
|
|
474
|
-
y: atY,
|
|
475
|
-
size,
|
|
476
|
-
font: f,
|
|
477
|
-
color
|
|
478
|
-
});
|
|
479
|
-
}, "drawRight");
|
|
480
|
-
ensureSpace(30);
|
|
481
|
-
const headerBarH = 20;
|
|
482
|
-
const headerBarBottom = y - 5;
|
|
483
|
-
page.drawRectangle({
|
|
484
|
-
x: margin,
|
|
485
|
-
y: headerBarBottom,
|
|
486
|
-
width: pageWidth - margin * 2,
|
|
487
|
-
height: headerBarH,
|
|
488
|
-
color: theme.headerBg
|
|
489
|
-
});
|
|
490
|
-
const headerTextY = headerBarBottom + 6;
|
|
491
|
-
drawText(page, "Item", margin + 4, headerTextY, fontBold, 9, theme.headerText);
|
|
492
|
-
drawRight("Qty", qtyRight, headerTextY, fontBold, 9, theme.headerText);
|
|
493
|
-
drawRight("Price", priceRight, headerTextY, fontBold, 9, theme.headerText);
|
|
494
|
-
drawRight("Tax", taxRight, headerTextY, fontBold, 9, theme.headerText);
|
|
495
|
-
drawRight("Total", totalRight, headerTextY, fontBold, 9, theme.headerText);
|
|
496
|
-
y = headerBarBottom - 10;
|
|
497
|
-
for (const line of input.lineItems) {
|
|
498
|
-
const nameLines = wrapText(line.name || "Item", font, 9, itemMaxW);
|
|
499
|
-
const skuText = line.sku ? winAnsiSafe(`SKU: ${line.sku}`).slice(0, 80) : "";
|
|
500
|
-
const nameH = Math.max(11, nameLines.length * 11);
|
|
501
|
-
const skuH = skuText ? 10 : 0;
|
|
502
|
-
const blockH = nameH + skuH + 6;
|
|
503
|
-
ensureSpace(blockH + 10);
|
|
504
|
-
let ty = y;
|
|
505
|
-
for (const nl of nameLines) {
|
|
506
|
-
drawText(page, nl, margin + 4, ty, font, 9, theme.text);
|
|
507
|
-
ty -= 11;
|
|
508
|
-
}
|
|
509
|
-
if (skuText) {
|
|
510
|
-
drawText(page, skuText, margin + 4, ty, font, 7, theme.muted);
|
|
511
|
-
ty -= 10;
|
|
512
|
-
}
|
|
513
|
-
drawRight(String(line.quantity), qtyRight, y, font, 9, theme.text);
|
|
514
|
-
drawRight(money(line.unitPrice, input.currency), priceRight, y, font, 9, theme.text);
|
|
515
|
-
drawRight(money(line.tax ?? 0, input.currency), taxRight, y, font, 9, theme.text);
|
|
516
|
-
drawRight(money(line.total, input.currency), totalRight, y, font, 9, theme.text);
|
|
517
|
-
const contentBottom = Math.min(ty, y - 12);
|
|
518
|
-
y = contentBottom - 2;
|
|
519
|
-
page.drawLine({
|
|
520
|
-
start: {
|
|
521
|
-
x: margin,
|
|
522
|
-
y
|
|
523
|
-
},
|
|
524
|
-
end: {
|
|
525
|
-
x: pageWidth - margin,
|
|
526
|
-
y
|
|
527
|
-
},
|
|
528
|
-
thickness: 0.5,
|
|
529
|
-
color: theme.line
|
|
530
|
-
});
|
|
531
|
-
y -= 8;
|
|
532
|
-
}
|
|
533
|
-
y -= 16;
|
|
534
|
-
ensureSpace(90);
|
|
535
|
-
const summaryLabelX = totalRight - colTotalW - colTaxW - 8;
|
|
536
|
-
const rowsSummary = [
|
|
537
|
-
[
|
|
538
|
-
"Subtotal",
|
|
539
|
-
money(input.subtotal, input.currency),
|
|
540
|
-
false
|
|
541
|
-
],
|
|
542
|
-
[
|
|
543
|
-
"Discount",
|
|
544
|
-
money(input.discount, input.currency),
|
|
545
|
-
false
|
|
546
|
-
],
|
|
547
|
-
[
|
|
548
|
-
"Tax",
|
|
549
|
-
money(input.tax, input.currency),
|
|
550
|
-
false
|
|
551
|
-
],
|
|
552
|
-
[
|
|
553
|
-
"Total",
|
|
554
|
-
money(input.total, input.currency),
|
|
555
|
-
true
|
|
556
|
-
]
|
|
557
|
-
];
|
|
558
|
-
for (const [label, value, strong] of rowsSummary) {
|
|
559
|
-
const f = strong ? fontBold : font;
|
|
560
|
-
const size = strong ? 11 : 9;
|
|
561
|
-
drawText(page, label, summaryLabelX, y, f, size, strong ? theme.accent : theme.muted);
|
|
562
|
-
drawRight(value, totalRight, y, f, size, theme.text);
|
|
563
|
-
y -= strong ? 18 : 14;
|
|
564
|
-
}
|
|
565
|
-
y -= 20;
|
|
566
|
-
ensureSpace(40);
|
|
567
|
-
drawText(page, "Thank you for your purchase.", margin, y, font, 9, theme.muted);
|
|
568
|
-
y -= 12;
|
|
569
|
-
drawText(page, ``, margin, y, font, 7, theme.muted);
|
|
570
|
-
return doc.save();
|
|
571
|
-
}
|
|
572
|
-
__name(generateLocalInvoicePdf, "generateLocalInvoicePdf");
|
|
573
|
-
|
|
574
|
-
// src/lib/invoice/resolve-invoice-asset-url.ts
|
|
575
|
-
function resolveInvoiceAssetUrl(raw) {
|
|
576
|
-
const t = String(raw ?? "").trim();
|
|
577
|
-
if (!t) return null;
|
|
578
|
-
if (/^https?:\/\//i.test(t)) return t;
|
|
579
|
-
if (t.startsWith("//")) return `https:${t}`;
|
|
580
|
-
const base = (process.env.CMS_PUBLIC_URL || process.env.NEXT_PUBLIC_SITE_URL || process.env.NEXTAUTH_URL || "").trim().replace(/\/+$/, "");
|
|
581
|
-
if (!base) return null;
|
|
582
|
-
if (t.startsWith("/")) return `${base}${t}`;
|
|
583
|
-
return `${base}/${t}`;
|
|
584
|
-
}
|
|
585
|
-
__name(resolveInvoiceAssetUrl, "resolveInvoiceAssetUrl");
|
|
586
|
-
|
|
587
|
-
// src/lib/invoice/build-local-invoice-for-order.ts
|
|
588
|
-
async function loadSettingsGroup(ds, entityMap, group) {
|
|
589
|
-
if (!entityMap.configs) return {};
|
|
590
|
-
const rows = await ds.getRepository(entityMap.configs).find({
|
|
591
|
-
where: {
|
|
592
|
-
settings: group,
|
|
593
|
-
deleted: false
|
|
594
|
-
}
|
|
595
|
-
});
|
|
596
|
-
return Object.fromEntries(rows.map((r) => [
|
|
597
|
-
r.key,
|
|
598
|
-
String(r.value ?? "")
|
|
599
|
-
]));
|
|
600
|
-
}
|
|
601
|
-
__name(loadSettingsGroup, "loadSettingsGroup");
|
|
602
|
-
function addrFromRow(row) {
|
|
603
|
-
if (!row || typeof row !== "object") return null;
|
|
604
|
-
const a = row;
|
|
605
|
-
return {
|
|
606
|
-
line1: String(a.line1 ?? "").trim() || void 0,
|
|
607
|
-
line2: a.line2 != null ? String(a.line2).trim() || null : null,
|
|
608
|
-
city: String(a.city ?? "").trim() || void 0,
|
|
609
|
-
state: String(a.state ?? "").trim() || void 0,
|
|
610
|
-
postalCode: String(a.postalCode ?? "").trim() || void 0,
|
|
611
|
-
country: String(a.country ?? "").trim() || void 0
|
|
612
|
-
};
|
|
613
|
-
}
|
|
614
|
-
__name(addrFromRow, "addrFromRow");
|
|
615
|
-
async function buildLocalInvoicePdfForOrder(ds, entityMap, orderId) {
|
|
616
|
-
if (!entityMap.orders || !entityMap.order_items) {
|
|
617
|
-
return {
|
|
618
|
-
error: "Orders not configured",
|
|
619
|
-
status: 503
|
|
620
|
-
};
|
|
621
|
-
}
|
|
622
|
-
const orderRepo = ds.getRepository(entityMap.orders);
|
|
623
|
-
let order = null;
|
|
624
|
-
try {
|
|
625
|
-
order = await orderRepo.findOne({
|
|
626
|
-
where: {
|
|
627
|
-
id: orderId,
|
|
628
|
-
deleted: false
|
|
629
|
-
},
|
|
630
|
-
relations: [
|
|
631
|
-
"contact",
|
|
632
|
-
"billingAddress",
|
|
633
|
-
"shippingAddress",
|
|
634
|
-
"items",
|
|
635
|
-
"items.product"
|
|
636
|
-
]
|
|
637
|
-
});
|
|
638
|
-
} catch {
|
|
639
|
-
order = await orderRepo.findOne({
|
|
640
|
-
where: {
|
|
641
|
-
id: orderId,
|
|
642
|
-
deleted: false
|
|
643
|
-
},
|
|
644
|
-
relations: [
|
|
645
|
-
"contact",
|
|
646
|
-
"billingAddress",
|
|
647
|
-
"shippingAddress",
|
|
648
|
-
"items"
|
|
649
|
-
]
|
|
650
|
-
});
|
|
651
|
-
}
|
|
652
|
-
if (!order) return {
|
|
653
|
-
error: "Not found",
|
|
654
|
-
status: 404
|
|
655
|
-
};
|
|
656
|
-
const o = order;
|
|
657
|
-
const kind = o.orderKind || "sale";
|
|
658
|
-
if (kind !== "sale") return {
|
|
659
|
-
error: "Invoice only for sale orders",
|
|
660
|
-
status: 400
|
|
661
|
-
};
|
|
662
|
-
const [store, branding, email] = await Promise.all([
|
|
663
|
-
loadSettingsGroup(ds, entityMap, "store"),
|
|
664
|
-
loadSettingsGroup(ds, entityMap, "branding"),
|
|
665
|
-
loadSettingsGroup(ds, entityMap, "email")
|
|
666
|
-
]);
|
|
667
|
-
const companyDetails = mergeEmailLayoutCompanyDetails(branding, email, store);
|
|
668
|
-
const fromBranding = getCompanyDetailsFromSettings(branding);
|
|
669
|
-
const rawLogo = store.invoiceLogoUrl || store.companyLogo || store.logo || companyDetails.logoUrl || fromBranding.logoUrl || null;
|
|
670
|
-
const logoUrl = resolveInvoiceAssetUrl(rawLogo);
|
|
671
|
-
const companyName = store.companyName || store.invoiceCompanyName || store.name || companyDetails.companyName || fromBranding.companyName || "Store";
|
|
672
|
-
const supportEmail = store.companyEmail || store.invoiceEmail || store.email || companyDetails.supportEmail || fromBranding.supportEmail;
|
|
673
|
-
const supportPhone = store.companyPhone || store.invoicePhone || store.phone || companyDetails.supportPhone;
|
|
674
|
-
const address = store.companyAddress || store.invoiceAddress || store.address;
|
|
675
|
-
const lineItems = (o.items ?? []).map((it) => {
|
|
676
|
-
const name = it.product?.name && String(it.product.name).trim() || `Product #${it.productId ?? ""}`;
|
|
677
|
-
return {
|
|
678
|
-
name,
|
|
679
|
-
quantity: Math.max(0, Math.floor(Number(it.quantity) || 0)),
|
|
680
|
-
unitPrice: Number(it.unitPrice) || 0,
|
|
681
|
-
tax: Number(it.tax) || 0,
|
|
682
|
-
total: Number(it.total) || 0,
|
|
683
|
-
sku: it.product?.sku ?? null
|
|
684
|
-
};
|
|
685
|
-
});
|
|
686
|
-
const bytes = await generateLocalInvoicePdf({
|
|
687
|
-
templateId: normalizeInvoiceTemplateId(store.invoiceTemplate),
|
|
688
|
-
orderNumber: String(o.orderNumber || orderId),
|
|
689
|
-
orderDate: o.createdAt || /* @__PURE__ */ new Date(),
|
|
690
|
-
currency: String(o.currency || store.currency || "INR"),
|
|
691
|
-
customerName: String(o.contact?.name ?? "").trim() || "Customer",
|
|
692
|
-
customerEmail: o.contact?.email ?? null,
|
|
693
|
-
customerPhone: o.contact?.phone ?? null,
|
|
694
|
-
billingAddress: addrFromRow(o.billingAddress),
|
|
695
|
-
shippingAddress: addrFromRow(o.shippingAddress),
|
|
696
|
-
lineItems,
|
|
697
|
-
subtotal: Number(o.subtotal) || 0,
|
|
698
|
-
tax: Number(o.tax) || 0,
|
|
699
|
-
discount: Number(o.discount) || 0,
|
|
700
|
-
total: Number(o.total) || 0,
|
|
701
|
-
company: {
|
|
702
|
-
name: companyName,
|
|
703
|
-
logoUrl,
|
|
704
|
-
supportEmail,
|
|
705
|
-
supportPhone,
|
|
706
|
-
address
|
|
707
|
-
}
|
|
708
|
-
});
|
|
709
|
-
return {
|
|
710
|
-
bytes,
|
|
711
|
-
filename: `invoice-${String(o.orderNumber || orderId).replace(/[^\w.-]+/g, "_")}.pdf`
|
|
712
|
-
};
|
|
713
|
-
}
|
|
714
|
-
__name(buildLocalInvoicePdfForOrder, "buildLocalInvoicePdfForOrder");
|
|
715
|
-
|
|
716
|
-
// src/plugins/erp/erp-order-invoice.ts
|
|
717
|
-
function pickInvoiceId(data) {
|
|
718
|
-
const nested = data.invoice && typeof data.invoice === "object" && !Array.isArray(data.invoice) ? data.invoice : null;
|
|
719
|
-
const src = nested || data;
|
|
720
|
-
for (const k of [
|
|
721
|
-
"invoiceId",
|
|
722
|
-
"invoice_id",
|
|
723
|
-
"id"
|
|
724
|
-
]) {
|
|
725
|
-
const v = src[k];
|
|
726
|
-
if (typeof v === "string" && v.trim()) return v.trim();
|
|
727
|
-
}
|
|
728
|
-
return void 0;
|
|
729
|
-
}
|
|
730
|
-
__name(pickInvoiceId, "pickInvoiceId");
|
|
731
|
-
function jsonErr(msg, status) {
|
|
732
|
-
return new Response(JSON.stringify({
|
|
733
|
-
error: msg
|
|
734
|
-
}), {
|
|
735
|
-
status,
|
|
736
|
-
headers: {
|
|
737
|
-
"Content-Type": "application/json"
|
|
738
|
-
}
|
|
739
|
-
});
|
|
740
|
-
}
|
|
741
|
-
__name(jsonErr, "jsonErr");
|
|
742
|
-
var ORDER_INVOICE_EMAIL_ELIGIBLE_STATUSES = /* @__PURE__ */ new Set([
|
|
743
|
-
"pending",
|
|
744
|
-
"placed",
|
|
745
|
-
"confirmed",
|
|
746
|
-
"processing",
|
|
747
|
-
"completed"
|
|
748
|
-
]);
|
|
749
|
-
function isOrderEligibleForInvoiceEmail(status, orderKind) {
|
|
750
|
-
const kind = (orderKind || "sale").trim().toLowerCase();
|
|
751
|
-
if (kind !== "sale") return false;
|
|
752
|
-
return ORDER_INVOICE_EMAIL_ELIGIBLE_STATUSES.has(String(status ?? "").trim().toLowerCase());
|
|
753
|
-
}
|
|
754
|
-
__name(isOrderEligibleForInvoiceEmail, "isOrderEligibleForInvoiceEmail");
|
|
755
|
-
async function resolveOrderInvoicePdfBytes(cms, dataSource, entityMap, orderId, options = {}) {
|
|
756
|
-
try {
|
|
757
|
-
const orderRepo = dataSource.getRepository(entityMap.orders);
|
|
758
|
-
const order = await orderRepo.findOne({
|
|
759
|
-
where: {
|
|
760
|
-
id: orderId,
|
|
761
|
-
deleted: false
|
|
762
|
-
}
|
|
763
|
-
});
|
|
764
|
-
if (!order) return {
|
|
765
|
-
error: "Not found",
|
|
766
|
-
status: 404
|
|
767
|
-
};
|
|
768
|
-
const kind = order.orderKind || "sale";
|
|
769
|
-
if (kind !== "sale") return {
|
|
770
|
-
error: "Invoice only for sale orders",
|
|
771
|
-
status: 400
|
|
772
|
-
};
|
|
773
|
-
if (options.ownerContactId != null && order.contactId !== options.ownerContactId) {
|
|
774
|
-
return {
|
|
775
|
-
error: "Not found",
|
|
776
|
-
status: 404
|
|
777
|
-
};
|
|
778
|
-
}
|
|
779
|
-
const on = await isErpIntegrationEnabled(cms, dataSource, entityMap);
|
|
780
|
-
const erp = cms.getPlugin("erp");
|
|
781
|
-
if (on && erp?.submission) {
|
|
782
|
-
const meta = order.metadata && typeof order.metadata === "object" && !Array.isArray(order.metadata) ? order.metadata : {};
|
|
783
|
-
const inv = meta.invoice && typeof meta.invoice === "object" && !Array.isArray(meta.invoice) ? meta.invoice : {};
|
|
784
|
-
let invoiceId = typeof inv.invoiceId === "string" ? inv.invoiceId.trim() : "";
|
|
785
|
-
if (!invoiceId) {
|
|
786
|
-
const refId = String(order.orderNumber || "");
|
|
787
|
-
try {
|
|
788
|
-
const r = await erp.submission.postErpReadAction("get-invoice", {
|
|
789
|
-
platformOrderId: refId
|
|
790
|
-
});
|
|
791
|
-
const d = r.ok ? unwrapErpReadData(r.json) : null;
|
|
792
|
-
invoiceId = d ? pickInvoiceId(d) || "" : "";
|
|
793
|
-
} catch {
|
|
794
|
-
invoiceId = "";
|
|
795
|
-
}
|
|
796
|
-
}
|
|
797
|
-
if (invoiceId) {
|
|
798
|
-
const pdf = await erp.submission.fetchInvoicePdf(invoiceId);
|
|
799
|
-
if (pdf.ok && pdf.buffer) {
|
|
800
|
-
return {
|
|
801
|
-
bytes: new Uint8Array(pdf.buffer),
|
|
802
|
-
filename: `order-invoice-${orderId}.pdf`,
|
|
803
|
-
contentType: pdf.contentType || "application/pdf",
|
|
804
|
-
source: "erp"
|
|
805
|
-
};
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
const local = await buildLocalInvoicePdfForOrder(dataSource, entityMap, orderId);
|
|
810
|
-
if ("error" in local) return {
|
|
811
|
-
error: local.error,
|
|
812
|
-
status: local.status
|
|
813
|
-
};
|
|
814
|
-
return {
|
|
815
|
-
bytes: local.bytes,
|
|
816
|
-
filename: local.filename.startsWith("invoice-") ? local.filename.replace(/^invoice-/, "order-invoice-") : local.filename,
|
|
817
|
-
contentType: "application/pdf",
|
|
818
|
-
source: "local"
|
|
819
|
-
};
|
|
820
|
-
} catch (err) {
|
|
821
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
822
|
-
console.error("[invoice] resolveOrderInvoicePdfBytes failed", {
|
|
823
|
-
orderId,
|
|
824
|
-
error: msg
|
|
825
|
-
});
|
|
826
|
-
return {
|
|
827
|
-
error: `Invoice failed: ${msg}`,
|
|
828
|
-
status: 500
|
|
829
|
-
};
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
__name(resolveOrderInvoicePdfBytes, "resolveOrderInvoicePdfBytes");
|
|
833
|
-
async function streamOrderInvoicePdf(cms, dataSource, entityMap, orderId, options) {
|
|
834
|
-
const resolved = await resolveOrderInvoicePdfBytes(cms, dataSource, entityMap, orderId, options);
|
|
835
|
-
if ("error" in resolved) return jsonErr(resolved.error, resolved.status);
|
|
836
|
-
return new Response(Buffer.from(resolved.bytes), {
|
|
837
|
-
status: 200,
|
|
838
|
-
headers: {
|
|
839
|
-
"Content-Type": resolved.contentType,
|
|
840
|
-
"Content-Disposition": `attachment; filename="${resolved.filename}"`,
|
|
841
|
-
"X-Invoice-Source": resolved.source
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
}
|
|
845
|
-
__name(streamOrderInvoicePdf, "streamOrderInvoicePdf");
|
|
846
|
-
|
|
847
|
-
export { INVOICE_TEMPLATE_IDS, INVOICE_TEMPLATE_OPTIONS, ORDER_INVOICE_EMAIL_ELIGIBLE_STATUSES, buildLocalInvoicePdfForOrder, extractChildOrderRefsFromSalePayload, generateLocalInvoicePdf, isOrderEligibleForInvoiceEmail, mapErpPayloadToFulfillment, mapErpPayloadToInvoiceNumber, normalizeInvoiceTemplateId, resolveInvoiceAssetUrl, resolveOrderInvoicePdfBytes, streamOrderInvoicePdf, unwrapErpReadData };
|