@vicacha-devs/medusa-plugin-barcode 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.
Files changed (176) hide show
  1. package/.medusa/server/src/admin/index.cjs +3960 -0
  2. package/.medusa/server/src/admin/index.js +3938 -0
  3. package/.medusa/server/src/api/admin/barcodes/[barcode]/route.js +43 -0
  4. package/.medusa/server/src/api/admin/barcodes/batch/route.js +9 -0
  5. package/.medusa/server/src/api/admin/barcodes/bulk-generate/route.js +9 -0
  6. package/.medusa/server/src/api/admin/barcodes/cache/route.js +25 -0
  7. package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/apply/route.js +13 -0
  8. package/.medusa/server/src/api/admin/barcodes/count-sessions/[id]/scan/route.js +13 -0
  9. package/.medusa/server/src/api/admin/barcodes/count-sessions/route.js +27 -0
  10. package/.medusa/server/src/api/admin/barcodes/create-from-barcode/route.js +9 -0
  11. package/.medusa/server/src/api/admin/barcodes/lookup/route.js +12 -0
  12. package/.medusa/server/src/api/admin/barcodes/print/route.js +11 -0
  13. package/.medusa/server/src/api/admin/barcodes/route.js +27 -0
  14. package/.medusa/server/src/api/admin/barcodes/scan/route.js +23 -0
  15. package/.medusa/server/src/api/admin/barcodes/tags/[id]/route.js +28 -0
  16. package/.medusa/server/src/api/admin/barcodes/tags/route.js +25 -0
  17. package/.medusa/server/src/api/admin/barcodes/templates/route.js +40 -0
  18. package/.medusa/server/src/api/admin/plugin/route.js +4 -0
  19. package/.medusa/server/src/api/admin/variants/barcode/[code]/route.js +28 -0
  20. package/.medusa/server/src/api/middlewares.js +73 -0
  21. package/.medusa/server/src/api/store/barcodes/[code]/route.js +28 -0
  22. package/.medusa/server/src/api/store/barcodes/[code]/verify/route.js +20 -0
  23. package/.medusa/server/src/api/store/plugin/route.js +4 -0
  24. package/.medusa/server/src/api/store/variants/barcode/[code]/route.js +28 -0
  25. package/.medusa/server/src/api/validators.js +173 -0
  26. package/.medusa/server/src/hooks/index.js +202 -0
  27. package/.medusa/server/src/index.js +5 -0
  28. package/.medusa/server/src/jobs/purge-expired-barcode-cache.js +10 -0
  29. package/.medusa/server/src/modules/barcode/index.js +16 -0
  30. package/.medusa/server/src/modules/barcode/migrations/Migration20260522000000.js +165 -0
  31. package/.medusa/server/src/modules/barcode/models/index.js +162 -0
  32. package/.medusa/server/src/modules/barcode/operations/context.js +2 -0
  33. package/.medusa/server/src/modules/barcode/operations/data-service.js +10 -0
  34. package/.medusa/server/src/modules/barcode/operations/generation.js +33 -0
  35. package/.medusa/server/src/modules/barcode/operations/index.js +9 -0
  36. package/.medusa/server/src/modules/barcode/operations/labels.js +57 -0
  37. package/.medusa/server/src/modules/barcode/operations/lookup.js +135 -0
  38. package/.medusa/server/src/modules/barcode/operations/maintenance.js +37 -0
  39. package/.medusa/server/src/modules/barcode/operations/stock-count.js +129 -0
  40. package/.medusa/server/src/modules/barcode/operations/tags.js +298 -0
  41. package/.medusa/server/src/modules/barcode/providers/index.js +721 -0
  42. package/.medusa/server/src/modules/barcode/service.js +161 -0
  43. package/.medusa/server/src/modules/barcode/types.js +21 -0
  44. package/.medusa/server/src/modules/barcode/utils/barcode-parser.js +399 -0
  45. package/.medusa/server/src/modules/barcode/utils/label-printer.js +533 -0
  46. package/.medusa/server/src/modules/barcode/utils/normalize-product.js +443 -0
  47. package/.medusa/server/src/modules/barcode/utils/security.js +114 -0
  48. package/.medusa/server/src/modules/barcode/utils/svg-renderer.js +492 -0
  49. package/.medusa/server/src/rbac.js +130 -0
  50. package/.medusa/server/src/sdk/index.js +164 -0
  51. package/.medusa/server/src/subscribers/product-created.js +34 -0
  52. package/.medusa/server/src/types/index.js +2 -0
  53. package/.medusa/server/src/ui/index.js +138 -0
  54. package/.medusa/server/src/workflows/apply-inventory-count.js +68 -0
  55. package/.medusa/server/src/workflows/batch-barcode-import.js +153 -0
  56. package/.medusa/server/src/workflows/bulk-generate-variant-barcodes.js +143 -0
  57. package/.medusa/server/src/workflows/generate-barcode-tag.js +44 -0
  58. package/.medusa/server/src/workflows/helpers.js +15 -0
  59. package/.medusa/server/src/workflows/index.js +7 -0
  60. package/.medusa/server/src/workflows/lookup-and-create-product.js +215 -0
  61. package/.medusa/server/src/workflows/scan-inventory-count.js +15 -0
  62. package/LICENSE +21 -0
  63. package/README.md +844 -2
  64. package/package.json +134 -3
  65. package/src/__tests__/barcode.spec.ts +836 -0
  66. package/src/__tests__/medusa-shim.mjs +331 -0
  67. package/src/__tests__/register-ts-resolver.mjs +135 -0
  68. package/src/admin/README.md +134 -0
  69. package/src/admin/components/barcode-studio-header.tsx +73 -0
  70. package/src/admin/components/batch-import-section.tsx +99 -0
  71. package/src/admin/components/dynamic-qr-tag-drawer-form.tsx +230 -0
  72. package/src/admin/components/dynamic-qr-tag-form.tsx +129 -0
  73. package/src/admin/components/dynamic-qr-tags-table.tsx +136 -0
  74. package/src/admin/components/gtin-lookup-section.tsx +155 -0
  75. package/src/admin/components/index.tsx +14 -0
  76. package/src/admin/components/label-designer-section.tsx +151 -0
  77. package/src/admin/components/label-output-section.tsx +63 -0
  78. package/src/admin/components/overview-cards.tsx +98 -0
  79. package/src/admin/components/product-barcode-card.tsx +127 -0
  80. package/src/admin/components/providers-cache-table.tsx +61 -0
  81. package/src/admin/components/providers-overview-section.tsx +49 -0
  82. package/src/admin/components/rbac-guard.tsx +153 -0
  83. package/src/admin/components/stock-take-scanner-section.tsx +116 -0
  84. package/src/admin/components/stock-take-sessions-section.tsx +109 -0
  85. package/src/admin/hooks/use-barcode-studio.ts +466 -0
  86. package/src/admin/hooks/use-product-barcodes.ts +139 -0
  87. package/src/admin/i18n/README.md +130 -0
  88. package/src/admin/i18n/index.ts +36 -0
  89. package/src/admin/i18n/json/ar.json +157 -0
  90. package/src/admin/i18n/json/de.json +157 -0
  91. package/src/admin/i18n/json/en.json +157 -0
  92. package/src/admin/i18n/json/es.json +157 -0
  93. package/src/admin/i18n/json/fr.json +157 -0
  94. package/src/admin/i18n/json/it.json +157 -0
  95. package/src/admin/i18n/json/nl.json +157 -0
  96. package/src/admin/i18n/json/pl.json +157 -0
  97. package/src/admin/i18n/json/pt.json +157 -0
  98. package/src/admin/i18n/json/tr.json +157 -0
  99. package/src/admin/routes/barcodes/lookup/page.tsx +75 -0
  100. package/src/admin/routes/barcodes/page.tsx +212 -0
  101. package/src/admin/routes/barcodes/print/page.tsx +67 -0
  102. package/src/admin/routes/barcodes/providers/page.tsx +66 -0
  103. package/src/admin/routes/barcodes/stock-take/page.tsx +74 -0
  104. package/src/admin/routes/barcodes/tags/@create/page.tsx +56 -0
  105. package/src/admin/routes/barcodes/tags/page.tsx +72 -0
  106. package/src/admin/tsconfig.json +20 -0
  107. package/src/admin/vite-env.d.ts +1 -0
  108. package/src/admin/widgets/product-barcode-widget.tsx +43 -0
  109. package/src/api/README.md +117 -0
  110. package/src/api/admin/barcodes/[barcode]/route.ts +64 -0
  111. package/src/api/admin/barcodes/batch/route.ts +12 -0
  112. package/src/api/admin/barcodes/bulk-generate/route.ts +12 -0
  113. package/src/api/admin/barcodes/cache/route.ts +35 -0
  114. package/src/api/admin/barcodes/count-sessions/[id]/apply/route.ts +17 -0
  115. package/src/api/admin/barcodes/count-sessions/[id]/scan/route.ts +17 -0
  116. package/src/api/admin/barcodes/count-sessions/route.ts +43 -0
  117. package/src/api/admin/barcodes/create-from-barcode/route.ts +16 -0
  118. package/src/api/admin/barcodes/lookup/route.ts +19 -0
  119. package/src/api/admin/barcodes/print/route.ts +18 -0
  120. package/src/api/admin/barcodes/route.ts +36 -0
  121. package/src/api/admin/barcodes/scan/route.ts +32 -0
  122. package/src/api/admin/barcodes/tags/[id]/route.ts +41 -0
  123. package/src/api/admin/barcodes/tags/route.ts +36 -0
  124. package/src/api/admin/barcodes/templates/route.ts +51 -0
  125. package/src/api/admin/plugin/route.ts +5 -0
  126. package/src/api/admin/variants/barcode/[code]/route.ts +36 -0
  127. package/src/api/middlewares.ts +91 -0
  128. package/src/api/store/barcodes/[code]/route.ts +43 -0
  129. package/src/api/store/barcodes/[code]/verify/route.ts +35 -0
  130. package/src/api/store/plugin/route.ts +5 -0
  131. package/src/api/store/variants/barcode/[code]/route.ts +36 -0
  132. package/src/api/validators.ts +217 -0
  133. package/src/hooks/index.ts +270 -0
  134. package/src/index.ts +4 -0
  135. package/src/jobs/README.md +54 -0
  136. package/src/jobs/purge-expired-barcode-cache.ts +18 -0
  137. package/src/links/README.md +44 -0
  138. package/src/modules/README.md +85 -0
  139. package/src/modules/barcode/index.ts +18 -0
  140. package/src/modules/barcode/migrations/Migration20260522000000.ts +225 -0
  141. package/src/modules/barcode/models/index.ts +168 -0
  142. package/src/modules/barcode/operations/context.ts +14 -0
  143. package/src/modules/barcode/operations/data-service.ts +10 -0
  144. package/src/modules/barcode/operations/generation.ts +57 -0
  145. package/src/modules/barcode/operations/index.ts +8 -0
  146. package/src/modules/barcode/operations/labels.ts +90 -0
  147. package/src/modules/barcode/operations/lookup.ts +202 -0
  148. package/src/modules/barcode/operations/maintenance.ts +53 -0
  149. package/src/modules/barcode/operations/stock-count.ts +217 -0
  150. package/src/modules/barcode/operations/tags.ts +425 -0
  151. package/src/modules/barcode/providers/index.ts +958 -0
  152. package/src/modules/barcode/service.ts +243 -0
  153. package/src/modules/barcode/types.ts +297 -0
  154. package/src/modules/barcode/utils/barcode-parser.ts +453 -0
  155. package/src/modules/barcode/utils/label-printer.ts +641 -0
  156. package/src/modules/barcode/utils/normalize-product.ts +524 -0
  157. package/src/modules/barcode/utils/security.ts +136 -0
  158. package/src/modules/barcode/utils/svg-renderer.ts +574 -0
  159. package/src/providers/README.md +105 -0
  160. package/src/rbac.ts +144 -0
  161. package/src/sdk/index.ts +253 -0
  162. package/src/subscribers/README.md +73 -0
  163. package/src/subscribers/product-created.ts +45 -0
  164. package/src/types/index.ts +1 -0
  165. package/src/types/medusa-ui.d.ts +118 -0
  166. package/src/ui/index.tsx +311 -0
  167. package/src/workflows/README.md +56 -0
  168. package/src/workflows/apply-inventory-count.ts +115 -0
  169. package/src/workflows/batch-barcode-import.ts +218 -0
  170. package/src/workflows/bulk-generate-variant-barcodes.ts +214 -0
  171. package/src/workflows/generate-barcode-tag.ts +85 -0
  172. package/src/workflows/helpers.ts +21 -0
  173. package/src/workflows/index.ts +6 -0
  174. package/src/workflows/lookup-and-create-product.ts +298 -0
  175. package/src/workflows/scan-inventory-count.ts +45 -0
  176. package/tsconfig.json +40 -0
@@ -0,0 +1,641 @@
1
+ import type {
2
+ LabelPresetId,
3
+ LabelPrintItem,
4
+ LabelPrintOutput,
5
+ LabelTemplateSpec,
6
+ } from "../types"
7
+ import { renderBarcodeSvg } from "./svg-renderer"
8
+ import { escapeHtml } from "./security"
9
+
10
+ /**
11
+ * Built-in thermal roll & A4 sheet label presets.
12
+ */
13
+ export const BUILTIN_LABEL_TEMPLATES: Record<
14
+ Exclude<LabelPresetId, "custom">,
15
+ LabelTemplateSpec
16
+ > = {
17
+ thermal_50x25: {
18
+ id: "thermal_50x25",
19
+ name: "Thermal Roll 50×25 mm (Retail Price Tag)",
20
+ preset_id: "thermal_50x25",
21
+ layout_type: "thermal_roll",
22
+ width_mm: 50,
23
+ height_mm: 25,
24
+ columns: 1,
25
+ rows: 1,
26
+ margin_top_mm: 1.5,
27
+ margin_left_mm: 2,
28
+ gap_x_mm: 0,
29
+ gap_y_mm: 2,
30
+ symbology: "ean13",
31
+ show_title: true,
32
+ show_variant_title: true,
33
+ show_sku: true,
34
+ show_price: true,
35
+ show_compare_at_price: true,
36
+ show_brand: false,
37
+ show_qr: false,
38
+ custom_header: null,
39
+ custom_footer: null,
40
+ dpi: 203,
41
+ is_default: true,
42
+ },
43
+ thermal_57x32: {
44
+ id: "thermal_57x32",
45
+ name: "Thermal Roll 57×32 mm (Shelf & Apparel Tag)",
46
+ preset_id: "thermal_57x32",
47
+ layout_type: "thermal_roll",
48
+ width_mm: 57,
49
+ height_mm: 32,
50
+ columns: 1,
51
+ rows: 1,
52
+ margin_top_mm: 2,
53
+ margin_left_mm: 2,
54
+ gap_x_mm: 0,
55
+ gap_y_mm: 2,
56
+ symbology: "ean13",
57
+ show_title: true,
58
+ show_variant_title: true,
59
+ show_sku: true,
60
+ show_price: true,
61
+ show_compare_at_price: true,
62
+ show_brand: true,
63
+ show_qr: true,
64
+ custom_header: null,
65
+ custom_footer: null,
66
+ dpi: 203,
67
+ is_default: false,
68
+ },
69
+ thermal_100x50: {
70
+ id: "thermal_100x50",
71
+ name: "Thermal Roll 100×50 mm (Warehouse Carton / GS1-128)",
72
+ preset_id: "thermal_100x50",
73
+ layout_type: "thermal_roll",
74
+ width_mm: 100,
75
+ height_mm: 50,
76
+ columns: 1,
77
+ rows: 1,
78
+ margin_top_mm: 3,
79
+ margin_left_mm: 3,
80
+ gap_x_mm: 0,
81
+ gap_y_mm: 3,
82
+ symbology: "code128",
83
+ show_title: true,
84
+ show_variant_title: true,
85
+ show_sku: true,
86
+ show_price: true,
87
+ show_compare_at_price: false,
88
+ show_brand: true,
89
+ show_qr: true,
90
+ custom_header: "WAREHOUSE INVENTORY",
91
+ custom_footer: null,
92
+ dpi: 203,
93
+ is_default: false,
94
+ },
95
+ jewelry_30x15: {
96
+ id: "jewelry_30x15",
97
+ name: "Jewelry / Eyewear Dumbbell Tag 30×15 mm",
98
+ preset_id: "jewelry_30x15",
99
+ layout_type: "thermal_roll",
100
+ width_mm: 30,
101
+ height_mm: 15,
102
+ columns: 1,
103
+ rows: 1,
104
+ margin_top_mm: 1,
105
+ margin_left_mm: 1,
106
+ gap_x_mm: 0,
107
+ gap_y_mm: 1.5,
108
+ symbology: "code128",
109
+ show_title: true,
110
+ show_variant_title: false,
111
+ show_sku: true,
112
+ show_price: true,
113
+ show_compare_at_price: false,
114
+ show_brand: false,
115
+ show_qr: false,
116
+ custom_header: null,
117
+ custom_footer: null,
118
+ dpi: 300,
119
+ is_default: false,
120
+ },
121
+ a4_24up_70x37: {
122
+ id: "a4_24up_70x37",
123
+ name: "A4 Sheet 24-Up (3×8 — 70×37 mm)",
124
+ preset_id: "a4_24up_70x37",
125
+ layout_type: "a4_sheet",
126
+ width_mm: 70,
127
+ height_mm: 37,
128
+ columns: 3,
129
+ rows: 8,
130
+ margin_top_mm: 1,
131
+ margin_left_mm: 0,
132
+ gap_x_mm: 0,
133
+ gap_y_mm: 0,
134
+ symbology: "ean13",
135
+ show_title: true,
136
+ show_variant_title: true,
137
+ show_sku: true,
138
+ show_price: true,
139
+ show_compare_at_price: true,
140
+ show_brand: true,
141
+ show_qr: false,
142
+ custom_header: null,
143
+ custom_footer: null,
144
+ dpi: 300,
145
+ is_default: false,
146
+ },
147
+ a4_40up_52x30: {
148
+ id: "a4_40up_52x30",
149
+ name: "A4 Sheet 40-Up (4×10 — 52.5×29.7 mm)",
150
+ preset_id: "a4_40up_52x30",
151
+ layout_type: "a4_sheet",
152
+ width_mm: 52.5,
153
+ height_mm: 29.7,
154
+ columns: 4,
155
+ rows: 10,
156
+ margin_top_mm: 0,
157
+ margin_left_mm: 0,
158
+ gap_x_mm: 0,
159
+ gap_y_mm: 0,
160
+ symbology: "ean13",
161
+ show_title: true,
162
+ show_variant_title: true,
163
+ show_sku: true,
164
+ show_price: true,
165
+ show_compare_at_price: true,
166
+ show_brand: false,
167
+ show_qr: false,
168
+ custom_header: null,
169
+ custom_footer: null,
170
+ dpi: 300,
171
+ is_default: false,
172
+ },
173
+ a4_65up_38x21: {
174
+ id: "a4_65up_38x21",
175
+ name: "A4 Sheet 65-Up Mini (5×13 — 38.1×21.2 mm)",
176
+ preset_id: "a4_65up_38x21",
177
+ layout_type: "a4_sheet",
178
+ width_mm: 38.1,
179
+ height_mm: 21.2,
180
+ columns: 5,
181
+ rows: 13,
182
+ margin_top_mm: 10.7,
183
+ margin_left_mm: 4.7,
184
+ gap_x_mm: 2.5,
185
+ gap_y_mm: 0,
186
+ symbology: "ean13",
187
+ show_title: true,
188
+ show_variant_title: false,
189
+ show_sku: false,
190
+ show_price: true,
191
+ show_compare_at_price: false,
192
+ show_brand: false,
193
+ show_qr: false,
194
+ custom_header: null,
195
+ custom_footer: null,
196
+ dpi: 300,
197
+ is_default: false,
198
+ },
199
+ }
200
+
201
+ /**
202
+ * Formats a numeric price in a currency using Intl.NumberFormat.
203
+ */
204
+ export function formatLabelCurrency(
205
+ amount: number | null | undefined,
206
+ currencyCode = "USD"
207
+ ): string | null {
208
+ if (typeof amount !== "number" || !Number.isFinite(amount)) {
209
+ return null
210
+ }
211
+ const code = (currencyCode || "USD").toUpperCase()
212
+ try {
213
+ return new Intl.NumberFormat("en-US", {
214
+ style: "currency",
215
+ currency: code,
216
+ minimumFractionDigits: 2,
217
+ maximumFractionDigits: 2,
218
+ }).format(amount)
219
+ } catch {
220
+ return `${code} ${amount.toFixed(2)}`
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Expands print items according to each item's `quantity` (capped to avoid DoS).
226
+ */
227
+ export function expandLabelPrintItems(
228
+ items: LabelPrintItem[],
229
+ maxTotalLabels = 500
230
+ ): LabelPrintItem[] {
231
+ const expanded: LabelPrintItem[] = []
232
+ for (const item of items) {
233
+ const qty = Math.max(1, Math.min(200, Math.floor(item.quantity ?? 1)))
234
+ for (let i = 0; i < qty; i++) {
235
+ if (expanded.length >= maxTotalLabels) {
236
+ return expanded
237
+ }
238
+ expanded.push({ ...item, quantity: 1 })
239
+ }
240
+ }
241
+ return expanded
242
+ }
243
+
244
+ /**
245
+ * Sanitizes strings for Zebra ZPL `^FD...^FS` field data commands.
246
+ */
247
+ function sanitizeZplField(input: string | null | undefined): string {
248
+ if (!input) return ""
249
+ return String(input)
250
+ .replace(/[\^~\\]/g, " ")
251
+ .replace(/[\r\n\t]+/g, " ")
252
+ .trim()
253
+ }
254
+
255
+ /**
256
+ * Generates raw Zebra ZPL II (`^XA ... ^XZ`) thermal label commands.
257
+ */
258
+ export function generateZplLabels(
259
+ items: LabelPrintItem[],
260
+ template: LabelTemplateSpec
261
+ ): string {
262
+ const expanded = expandLabelPrintItems(items)
263
+ const dotsPerMm = template.dpi === 300 ? 12 : 8
264
+ const widthDots = Math.round(template.width_mm * dotsPerMm)
265
+ const heightDots = Math.round(template.height_mm * dotsPerMm)
266
+ const padX = Math.max(12, Math.round(template.margin_left_mm * dotsPerMm))
267
+ const padY = Math.max(12, Math.round(template.margin_top_mm * dotsPerMm))
268
+
269
+ const blocks: string[] = []
270
+
271
+ for (const item of expanded) {
272
+ const symbology = item.symbology || template.symbology || "ean13"
273
+ const priceText = template.show_price
274
+ ? formatLabelCurrency(item.price, item.currency_code)
275
+ : null
276
+ const compareText =
277
+ template.show_compare_at_price &&
278
+ typeof item.compare_at_price === "number" &&
279
+ typeof item.price === "number" &&
280
+ item.compare_at_price > item.price
281
+ ? formatLabelCurrency(item.compare_at_price, item.currency_code)
282
+ : null
283
+
284
+ const lines: string[] = [
285
+ "^XA",
286
+ "^CI28",
287
+ `^PW${widthDots}`,
288
+ `^LL${heightDots}`,
289
+ "^LH0,0",
290
+ ]
291
+
292
+ let cursorY = padY
293
+
294
+ if (template.custom_header) {
295
+ lines.push(
296
+ `^FO${padX},${cursorY}^A0N,16,16^FD${sanitizeZplField(
297
+ template.custom_header
298
+ )}^FS`
299
+ )
300
+ cursorY += 20
301
+ }
302
+
303
+ if (template.show_brand && item.brand) {
304
+ lines.push(
305
+ `^FO${padX},${cursorY}^A0N,16,16^FD${sanitizeZplField(
306
+ item.brand.toUpperCase()
307
+ )}^FS`
308
+ )
309
+ cursorY += 18
310
+ }
311
+
312
+ if (template.show_title) {
313
+ const titleLine = item.title.slice(0, 34)
314
+ lines.push(
315
+ `^FO${padX},${cursorY}^A0N,22,22^FD${sanitizeZplField(titleLine)}^FS`
316
+ )
317
+ cursorY += 24
318
+ }
319
+
320
+ if (template.show_variant_title && item.variant_title) {
321
+ lines.push(
322
+ `^FO${padX},${cursorY}^A0N,18,18^FD${sanitizeZplField(
323
+ item.variant_title.slice(0, 34)
324
+ )}^FS`
325
+ )
326
+ cursorY += 20
327
+ }
328
+
329
+ if (template.show_sku && item.sku) {
330
+ lines.push(
331
+ `^FO${padX},${cursorY}^A0N,16,16^FDSKU: ${sanitizeZplField(item.sku)}^FS`
332
+ )
333
+ cursorY += 18
334
+ }
335
+
336
+ if (priceText) {
337
+ const priceLine = compareText
338
+ ? `${priceText} (MRP ${compareText})`
339
+ : priceText
340
+ lines.push(
341
+ `^FO${padX},${cursorY}^A0N,26,26^FD${sanitizeZplField(priceLine)}^FS`
342
+ )
343
+ cursorY += 30
344
+ }
345
+
346
+ const barHeight = Math.max(36, Math.min(90, heightDots - cursorY - 28))
347
+ if (symbology === "qr") {
348
+ lines.push(
349
+ `^FO${padX},${cursorY}^BQN,2,4^FDQA,${sanitizeZplField(
350
+ item.qr_url || item.barcode
351
+ )}^FS`
352
+ )
353
+ } else if (symbology === "ean13" || symbology === "isbn13") {
354
+ const digits12 = item.barcode
355
+ .replace(/\D/g, "")
356
+ .slice(0, 12)
357
+ .padStart(12, "0")
358
+ lines.push(
359
+ `^FO${padX},${cursorY}^BY2^BEN,${barHeight},Y,N^FD${digits12}^FS`
360
+ )
361
+ } else if (symbology === "ean8") {
362
+ const digits7 = item.barcode
363
+ .replace(/\D/g, "")
364
+ .slice(0, 7)
365
+ .padStart(7, "0")
366
+ lines.push(
367
+ `^FO${padX},${cursorY}^BY2^B8N,${barHeight},Y,N^FD${digits7}^FS`
368
+ )
369
+ } else {
370
+ lines.push(
371
+ `^FO${padX},${cursorY}^BY2^BCN,${barHeight},Y,N,N^FD${sanitizeZplField(
372
+ item.barcode
373
+ )}^FS`
374
+ )
375
+ }
376
+
377
+ if (
378
+ template.show_qr &&
379
+ symbology !== "qr" &&
380
+ (item.qr_url || item.barcode)
381
+ ) {
382
+ const qrX = Math.max(padX + 180, widthDots - 95)
383
+ lines.push(
384
+ `^FO${qrX},${padY}^BQN,2,3^FDQA,${sanitizeZplField(
385
+ item.qr_url || item.barcode
386
+ )}^FS`
387
+ )
388
+ }
389
+
390
+ if (template.custom_footer) {
391
+ lines.push(
392
+ `^FO${padX},${heightDots - 20}^A0N,15,15^FD${sanitizeZplField(
393
+ template.custom_footer
394
+ )}^FS`
395
+ )
396
+ }
397
+
398
+ lines.push("^PQ1,0,1,Y", "^XZ")
399
+ blocks.push(lines.join("\n"))
400
+ }
401
+
402
+ return blocks.join("\n\n")
403
+ }
404
+
405
+ /**
406
+ * Renders print-ready HTML + SVG and ZPL II output for a list of barcode/price label items.
407
+ */
408
+ export function renderPrintableLabels(
409
+ items: LabelPrintItem[],
410
+ templateOrPreset: LabelTemplateSpec | LabelPresetId = "thermal_50x25"
411
+ ): LabelPrintOutput {
412
+ const template: LabelTemplateSpec =
413
+ typeof templateOrPreset === "string"
414
+ ? (BUILTIN_LABEL_TEMPLATES as Record<string, LabelTemplateSpec>)[
415
+ templateOrPreset
416
+ ] || BUILTIN_LABEL_TEMPLATES.thermal_50x25
417
+ : templateOrPreset
418
+
419
+ const expanded = expandLabelPrintItems(items)
420
+ const isSheet = template.layout_type === "a4_sheet"
421
+
422
+ const labelCardsHtml = expanded.map((item) => {
423
+ const symbology = item.symbology || template.symbology || "ean13"
424
+ const barcodeSvg = renderBarcodeSvg({
425
+ code: item.barcode,
426
+ symbology,
427
+ width: template.show_qr && symbology !== "qr" ? 150 : 210,
428
+ height: template.height_mm <= 22 ? 42 : 58,
429
+ includeText: true,
430
+ })
431
+
432
+ const qrSvg =
433
+ template.show_qr && symbology !== "qr"
434
+ ? renderBarcodeSvg({
435
+ code: item.qr_url || item.barcode,
436
+ symbology: "qr",
437
+ width: 54,
438
+ height: 54,
439
+ includeText: false,
440
+ quietZone: 2,
441
+ })
442
+ : ""
443
+
444
+ const formattedPrice = template.show_price
445
+ ? formatLabelCurrency(item.price, item.currency_code)
446
+ : null
447
+ const formattedCompare =
448
+ template.show_compare_at_price &&
449
+ typeof item.compare_at_price === "number" &&
450
+ typeof item.price === "number" &&
451
+ item.compare_at_price > item.price
452
+ ? formatLabelCurrency(item.compare_at_price, item.currency_code)
453
+ : null
454
+
455
+ return `<div class="mpb-label-card">
456
+ ${
457
+ template.custom_header
458
+ ? `<div class="mpb-label-header">${escapeHtml(template.custom_header)}</div>`
459
+ : ""
460
+ }
461
+ <div class="mpb-label-top">
462
+ <div class="mpb-label-meta">
463
+ ${
464
+ template.show_brand && item.brand
465
+ ? `<div class="mpb-label-brand">${escapeHtml(item.brand)}</div>`
466
+ : ""
467
+ }
468
+ ${
469
+ template.show_title
470
+ ? `<div class="mpb-label-title">${escapeHtml(item.title)}</div>`
471
+ : ""
472
+ }
473
+ ${
474
+ template.show_variant_title && item.variant_title
475
+ ? `<div class="mpb-label-variant">${escapeHtml(item.variant_title)}</div>`
476
+ : ""
477
+ }
478
+ ${
479
+ template.show_sku && item.sku
480
+ ? `<div class="mpb-label-sku">SKU: ${escapeHtml(item.sku)}</div>`
481
+ : ""
482
+ }
483
+ </div>
484
+ ${
485
+ formattedPrice
486
+ ? `<div class="mpb-label-price-box">
487
+ ${
488
+ formattedCompare
489
+ ? `<span class="mpb-label-compare">${escapeHtml(formattedCompare)}</span>`
490
+ : ""
491
+ }
492
+ <span class="mpb-label-price">${escapeHtml(formattedPrice)}</span>
493
+ </div>`
494
+ : ""
495
+ }
496
+ </div>
497
+ <div class="mpb-label-barcode-row">
498
+ <div class="mpb-label-barcode">${barcodeSvg}</div>
499
+ ${qrSvg ? `<div class="mpb-label-qr">${qrSvg}</div>` : ""}
500
+ </div>
501
+ ${
502
+ template.custom_footer
503
+ ? `<div class="mpb-label-footer">${escapeHtml(template.custom_footer)}</div>`
504
+ : ""
505
+ }
506
+ </div>`
507
+ })
508
+
509
+ const pageCss = isSheet
510
+ ? `@page { size: A4 portrait; margin: ${template.margin_top_mm}mm ${template.margin_left_mm}mm; }`
511
+ : `@page { size: ${template.width_mm}mm ${template.height_mm}mm; margin: 0; }`
512
+
513
+ const gridCss = isSheet
514
+ ? `display: grid; grid-template-columns: repeat(${template.columns}, ${template.width_mm}mm); column-gap: ${template.gap_x_mm}mm; row-gap: ${template.gap_y_mm}mm;`
515
+ : `display: flex; flex-direction: column; gap: ${template.gap_y_mm}mm;`
516
+
517
+ const html = `<!DOCTYPE html>
518
+ <html lang="en">
519
+ <head>
520
+ <meta charset="utf-8" />
521
+ <title>${escapeHtml(template.name)}</title>
522
+ <style>
523
+ ${pageCss}
524
+ * { box-sizing: border-box; margin: 0; padding: 0; }
525
+ body {
526
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
527
+ background: #ffffff;
528
+ color: #111827;
529
+ -webkit-print-color-adjust: exact;
530
+ print-color-adjust: exact;
531
+ }
532
+ .mpb-label-sheet {
533
+ ${gridCss}
534
+ }
535
+ .mpb-label-card {
536
+ width: ${template.width_mm}mm;
537
+ height: ${template.height_mm}mm;
538
+ padding: 1.4mm 1.8mm;
539
+ border: 0.25mm dashed #d1d5db;
540
+ display: flex;
541
+ flex-direction: column;
542
+ justify-content: space-between;
543
+ overflow: hidden;
544
+ page-break-inside: avoid;
545
+ break-inside: avoid;
546
+ }
547
+ @media print {
548
+ .mpb-label-card { border-color: transparent; }
549
+ }
550
+ .mpb-label-header, .mpb-label-footer {
551
+ font-size: 6pt;
552
+ font-weight: 700;
553
+ letter-spacing: 0.06em;
554
+ text-transform: uppercase;
555
+ text-align: center;
556
+ color: #374151;
557
+ }
558
+ .mpb-label-top {
559
+ display: flex;
560
+ justify-content: space-between;
561
+ align-items: flex-start;
562
+ gap: 1.5mm;
563
+ }
564
+ .mpb-label-meta {
565
+ min-width: 0;
566
+ flex: 1;
567
+ }
568
+ .mpb-label-brand {
569
+ font-size: 6pt;
570
+ font-weight: 700;
571
+ text-transform: uppercase;
572
+ color: #4b5563;
573
+ }
574
+ .mpb-label-title {
575
+ font-size: ${template.height_mm <= 20 ? "6.5pt" : "8pt"};
576
+ font-weight: 700;
577
+ line-height: 1.15;
578
+ white-space: nowrap;
579
+ overflow: hidden;
580
+ text-overflow: ellipsis;
581
+ }
582
+ .mpb-label-variant, .mpb-label-sku {
583
+ font-size: 6pt;
584
+ color: #4b5563;
585
+ white-space: nowrap;
586
+ overflow: hidden;
587
+ text-overflow: ellipsis;
588
+ }
589
+ .mpb-label-price-box {
590
+ display: flex;
591
+ flex-direction: column;
592
+ align-items: flex-end;
593
+ flex-shrink: 0;
594
+ }
595
+ .mpb-label-compare {
596
+ font-size: 6pt;
597
+ color: #6b7280;
598
+ text-decoration: line-through;
599
+ }
600
+ .mpb-label-price {
601
+ font-size: ${template.height_mm <= 20 ? "7.5pt" : "9.5pt"};
602
+ font-weight: 800;
603
+ color: #111827;
604
+ }
605
+ .mpb-label-barcode-row {
606
+ display: flex;
607
+ align-items: center;
608
+ justify-content: center;
609
+ gap: 1.5mm;
610
+ flex: 1;
611
+ min-height: 0;
612
+ }
613
+ .mpb-label-barcode svg {
614
+ width: 100%;
615
+ max-height: ${Math.max(9, Math.round(template.height_mm * 0.48))}mm;
616
+ display: block;
617
+ }
618
+ .mpb-label-qr svg {
619
+ width: ${Math.max(10, Math.round(template.height_mm * 0.42))}mm;
620
+ height: ${Math.max(10, Math.round(template.height_mm * 0.42))}mm;
621
+ display: block;
622
+ }
623
+ </style>
624
+ </head>
625
+ <body>
626
+ <div class="mpb-label-sheet">
627
+ ${labelCardsHtml.join("\n")}
628
+ </div>
629
+ </body>
630
+ </html>`
631
+
632
+ const zpl = generateZplLabels(items, template)
633
+
634
+ return {
635
+ template,
636
+ total_labels: expanded.length,
637
+ html,
638
+ zpl,
639
+ items: expanded,
640
+ }
641
+ }