@codemind.ec/medusa-plugin-invoice 1.0.0 → 1.0.2

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 (31) hide show
  1. package/.medusa/server/src/admin/index.js +512 -7
  2. package/.medusa/server/src/admin/index.mjs +513 -10
  3. package/.medusa/server/src/api/admin/invoice-config/route.js +26 -0
  4. package/.medusa/server/src/api/admin/invoice-templates/[id]/preview/route.js +119 -0
  5. package/.medusa/server/src/api/admin/invoice-templates/[id]/restore/route.js +21 -0
  6. package/.medusa/server/src/api/admin/invoice-templates/[id]/route.js +31 -0
  7. package/.medusa/server/src/api/admin/invoice-templates/route.js +20 -0
  8. package/.medusa/server/src/api/admin/invoice-templates/validators.js +18 -0
  9. package/.medusa/server/src/api/middlewares.js +36 -0
  10. package/.medusa/server/src/index.js +16 -1
  11. package/.medusa/server/src/modules/invoice-generator/errors.js +30 -0
  12. package/.medusa/server/src/modules/invoice-generator/index.js +15 -0
  13. package/.medusa/server/src/modules/invoice-generator/loaders/create-default-config.js +34 -0
  14. package/.medusa/server/src/modules/invoice-generator/migrations/Migration20250728094738.js +18 -0
  15. package/.medusa/server/src/modules/invoice-generator/migrations/Migration20250728124134.js +14 -0
  16. package/.medusa/server/src/modules/invoice-generator/migrations/Migration20250728151454.js +14 -0
  17. package/.medusa/server/src/modules/invoice-generator/migrations/Migration20260120005559.js +14 -0
  18. package/.medusa/server/src/modules/invoice-generator/migrations/Migration20260216191700.js +14 -0
  19. package/.medusa/server/src/modules/invoice-generator/migrations/Migration20260601120000.js +17 -0
  20. package/.medusa/server/src/modules/invoice-generator/models/invoice-config.js +16 -0
  21. package/.medusa/server/src/modules/invoice-generator/models/invoice-template.js +19 -0
  22. package/.medusa/server/src/modules/invoice-generator/models/invoice.js +17 -0
  23. package/.medusa/server/src/modules/invoice-generator/service.js +84 -0
  24. package/.medusa/server/src/modules/invoice-generator/templates/defaults/index.js +380 -0
  25. package/.medusa/server/src/modules/invoice-generator/templates/order-invoice.js +348 -0
  26. package/.medusa/server/src/modules/invoice-generator/templates/quote-proforma.js +468 -0
  27. package/.medusa/server/src/modules/invoice-generator/templates/strategy.js +110 -0
  28. package/LICENSE +21 -0
  29. package/README.md +204 -0
  30. package/index.d.ts +138 -0
  31. package/package.json +26 -2
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CodeMindEC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,204 @@
1
+ # @codemind.ec/medusa-plugin-invoice
2
+
3
+ Plugin de **facturación y cotización PDF** para Medusa v2 — genera facturas de pedido y proformas de cotización con plantillas HTML personalizables desde el admin.
4
+
5
+ [![Medusa v2](https://img.shields.io/badge/medusa-v2-blueviolet)](https://docs.medusajs.com)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ---
9
+
10
+ ## Features
11
+
12
+ - **Generación de PDFs** — facturas de pedido y proformas de cotización vía pdfmake + Handlebars.
13
+ - **Plantillas HTML editables** — CRUD completo de plantillas con preview en tiempo real desde el admin.
14
+ - **Configuración de empresa** — logo, RUC, dirección, teléfono, notas de pie de factura.
15
+ - **Strategy Pattern** — cada tipo de documento (factura, proforma) tiene su propia estrategia extensible.
16
+ - **Templates embebidos** — plantillas por defecto incluidas en el código; restaurables con un clic.
17
+ - **Admin UI** — widget de descarga en pedidos, editor de plantillas con CodeMirror, página de configuración.
18
+
19
+ ---
20
+
21
+ ## Requisitos
22
+
23
+ | Requisito | Versión |
24
+ |-----------|----------|
25
+ | Node.js | >= 20 |
26
+ | Medusa | >= 2.13 |
27
+
28
+ ---
29
+
30
+ ## Instalación
31
+
32
+ ```bash
33
+ pnpm add @codemind.ec/medusa-plugin-invoice
34
+ ```
35
+
36
+ ---
37
+
38
+ ## Configuración
39
+
40
+ ### 1. Registrar el plugin y módulo en `medusa-config.ts`
41
+
42
+ ```typescript
43
+ import { defineConfig } from "@medusajs/framework/utils"
44
+
45
+ export default defineConfig({
46
+ // ...
47
+ plugins: [
48
+ {
49
+ resolve: "@codemind.ec/medusa-plugin-invoice",
50
+ options: {},
51
+ },
52
+ ],
53
+ modules: [
54
+ {
55
+ resolve: "@codemind.ec/medusa-plugin-invoice/modules/invoice-generator",
56
+ options: {},
57
+ },
58
+ ],
59
+ })
60
+ ```
61
+
62
+ ### 2. Variables de entorno (opcionales)
63
+
64
+ El loader del módulo puede leer estas variables para configurar los valores iniciales de la empresa al crear la base de datos por primera vez:
65
+
66
+ | Variable | Descripción | Default |
67
+ |-----------------------|------------------------------------|----------------|
68
+ | `STORE_NAME` | Nombre de la empresa | — |
69
+ | `STORE_ADDRESS` | Dirección de la empresa | — |
70
+ | `STORE_PHONE` | Teléfono de la empresa | — |
71
+ | `ADMIN_EMAIL` | Email de notificaciones admin | — |
72
+ | `MEDUSA_BACKEND_URL` | URL base para embeber imágenes | `http://localhost:9000` |
73
+
74
+ Todos los valores se pueden editar después desde el admin.
75
+
76
+ ---
77
+
78
+ ## Arquitectura
79
+
80
+ ```
81
+ medusa-plugin-invoice/
82
+ ├── src/
83
+ │ ├── admin/ # UI del admin (widgets + rutas)
84
+ │ │ ├── widgets/order-invoice.tsx # Botón "Descargar comprobante" en pedidos
85
+ │ │ └── routes/ # Páginas de config y plantillas
86
+ │ ├── api/ # Rutas REST del plugin
87
+ │ │ └── admin/
88
+ │ │ ├── invoice-config/ # GET/POST configuración de empresa
89
+ │ │ └── invoice-templates/ # CRUD de plantillas + preview + restore
90
+ │ └── modules/
91
+ │ └── invoice-generator/
92
+ │ ├── models/ # Invoice, InvoiceConfig, InvoiceTemplate
93
+ │ ├── templates/
94
+ │ │ ├── strategy.ts # BaseDocumentStrategy + TemplateFactory
95
+ │ │ ├── order-invoice.ts # Estrategia de factura de pedido
96
+ │ │ ├── quote-proforma.ts # Estrategia de proforma de cotización
97
+ │ │ └── defaults/index.ts # HTML embebido de plantillas por defecto
98
+ │ ├── loaders/ # Seed de config y plantillas iniciales
99
+ │ ├── migrations/ # Migraciones de base de datos
100
+ │ ├── service.ts # InvoiceGeneratorService
101
+ │ └── index.ts # INVOICE_MODULE export
102
+ └── index.d.ts # Declaraciones de tipos
103
+ ```
104
+
105
+ ### Separación de responsabilidades
106
+
107
+ | Capa | Ubicación | Responsabilidad |
108
+ |------|-----------|-----------------|
109
+ | **Módulo** (modelos, servicio, migraciones, loader) | Plugin | Datos y lógica de dominio aislada |
110
+ | **API routes** | Plugin | Endpoints CRUD del módulo (config, plantillas) |
111
+ | **Admin UI** | Plugin | Widgets y páginas de administración |
112
+ | **Workflows / Steps** | App (`apps/medusa/`) | Orquestación cross-module |
113
+ | **Subscribers** | App (`apps/medusa/`) | Reacción a eventos del sistema |
114
+
115
+ > **¿Por qué los workflows NO están en el plugin?**
116
+ >
117
+ > Los workflows son **orquestación cross-module** por naturaleza. Por ejemplo, `generate-invoice-pdf` consulta el módulo `order` (via query graph), resuelve países del módulo `country`, verifica metadata de cotizaciones desde tipos de la app, y *luego* llama al módulo invoice. Moverlos al plugin crearía dependencias circulares (plugin → app) y eliminaría la capacidad de la app de personalizar la orquestación. Esta es la convención recomendada por Medusa v2: los plugins exponen módulos + API, las apps controlan la orquestación.
118
+
119
+ ---
120
+
121
+ ## API Routes
122
+
123
+ ### Invoice Config
124
+
125
+ | Método | Ruta | Descripción |
126
+ |--------|------|-------------|
127
+ | `GET` | `/admin/invoice-config` | Obtener configuración actual |
128
+ | `POST` | `/admin/invoice-config` | Actualizar configuración |
129
+
130
+ ### Invoice Templates
131
+
132
+ | Método | Ruta | Descripción |
133
+ |--------|------|-------------|
134
+ | `GET` | `/admin/invoice-templates` | Listar plantillas |
135
+ | `POST` | `/admin/invoice-templates` | Crear plantilla |
136
+ | `GET` | `/admin/invoice-templates/:id` | Obtener plantilla |
137
+ | `POST` | `/admin/invoice-templates/:id` | Actualizar plantilla |
138
+ | `DELETE` | `/admin/invoice-templates/:id` | Eliminar plantilla (no defaults) |
139
+ | `POST` | `/admin/invoice-templates/:id/preview` | Generar PDF de preview |
140
+ | `POST` | `/admin/invoice-templates/:id/restore` | Restaurar HTML por defecto |
141
+
142
+ ---
143
+
144
+ ## Uso desde la app
145
+
146
+ ### Imports disponibles
147
+
148
+ ```typescript
149
+ // Módulo y servicio
150
+ import { INVOICE_MODULE, InvoiceGeneratorService } from "@codemind.ec/medusa-plugin-invoice"
151
+
152
+ // Tipos de factura de pedido
153
+ import type { InvoiceOrder, InvoiceLineItem, InvoiceOrderAddress, OrderInvoiceInput } from "@codemind.ec/medusa-plugin-invoice"
154
+
155
+ // Tipos de proforma de cotización
156
+ import type { QuoteProformaInput, ProformaConfigField, QuoteCustomerInfo, QuoteScheduleRules } from "@codemind.ec/medusa-plugin-invoice"
157
+
158
+ // Enums
159
+ import { InvoiceStatus, TemplateType } from "@codemind.ec/medusa-plugin-invoice"
160
+ ```
161
+
162
+ ### Generar un PDF desde un workflow/step
163
+
164
+ ```typescript
165
+ const invoiceService = container.resolve<InvoiceGeneratorService>(INVOICE_MODULE)
166
+
167
+ // Factura de pedido
168
+ const pdfBuffer = await invoiceService.generatePdf({
169
+ template: "order_invoice",
170
+ data: { order, items, invoiceDisplayId: 1, created_at: "2025-01-01" },
171
+ invoice_id: "inv_01ABC...",
172
+ })
173
+
174
+ // Proforma de cotización
175
+ const pdfBuffer = await invoiceService.generatePdf({
176
+ template: "quote_proforma",
177
+ data: { serviceType: "Catering", /* ... */ },
178
+ })
179
+ ```
180
+
181
+ ---
182
+
183
+ ## Extensibilidad
184
+
185
+ ### Registrar una nueva estrategia de documento
186
+
187
+ ```typescript
188
+ import { TemplateFactory, BaseDocumentStrategy } from "@codemind.ec/medusa-plugin-invoice"
189
+
190
+ class MyCustomStrategy extends BaseDocumentStrategy<MyInput> {
191
+ async buildDocumentDefinition(input, config, htmlTemplate?) {
192
+ if (htmlTemplate) return this.renderHtmlTemplate(htmlTemplate, input)
193
+ // ... construir doc pdfmake programáticamente
194
+ }
195
+ }
196
+
197
+ TemplateFactory.register("my_custom_doc", MyCustomStrategy)
198
+ ```
199
+
200
+ ---
201
+
202
+ ## License
203
+
204
+ MIT — [CodeMind](https://codemind.ec)
package/index.d.ts ADDED
@@ -0,0 +1,138 @@
1
+ declare module "@codemind.ec/medusa-plugin-invoice" {
2
+ export const INVOICE_MODULE: "invoiceGenerator"
3
+
4
+ export enum InvoiceStatus {
5
+ LATEST = "latest",
6
+ STALE = "stale",
7
+ }
8
+
9
+ export enum TemplateType {
10
+ ORDER_INVOICE = "order_invoice",
11
+ QUOTE_PROFORMA = "quote_proforma",
12
+ }
13
+
14
+ export interface InvoiceOrderAddress {
15
+ first_name?: string | null
16
+ last_name?: string | null
17
+ address_1?: string | null
18
+ address_2?: string | null
19
+ city?: string | null
20
+ province?: string | null
21
+ postal_code?: string | null
22
+ country_code?: string | null
23
+ phone?: string | null
24
+ metadata?: Record<string, unknown> | null
25
+ }
26
+
27
+ export interface InvoiceLineItem {
28
+ id: string
29
+ title: string
30
+ quantity: number
31
+ unit_price: number
32
+ subtotal?: number | null
33
+ variant_title?: string | null
34
+ variant?: { title?: string | null; product?: { title?: string | null } | null } | null
35
+ metadata?: Record<string, unknown> | null
36
+ }
37
+
38
+ export interface InvoiceOrder {
39
+ id: string
40
+ display_id?: number | null
41
+ email?: string | null
42
+ created_at: string | Date
43
+ currency_code?: string | null
44
+ total?: number | null
45
+ subtotal?: number | null
46
+ tax_total?: number | null
47
+ discount_total?: number | null
48
+ shipping_address?: InvoiceOrderAddress | null
49
+ billing_address?: InvoiceOrderAddress | null
50
+ shipping_methods?: Array<{ name?: string | null; amount?: number | null; shipping_option_id?: string | null }> | null
51
+ items?: InvoiceLineItem[] | null
52
+ metadata?: Record<string, unknown> | null
53
+ }
54
+
55
+ export interface OrderInvoiceInput {
56
+ order: InvoiceOrder
57
+ items: InvoiceLineItem[]
58
+ invoiceDisplayId: number
59
+ created_at: string
60
+ }
61
+
62
+ export interface ProformaConfigField {
63
+ label: string
64
+ value: string
65
+ }
66
+
67
+ export interface ProformaLineItem {
68
+ label: string
69
+ total: number
70
+ }
71
+
72
+ export interface QuoteCustomerInfo {
73
+ name: string
74
+ email: string
75
+ phone: string
76
+ cedula?: string
77
+ eventDate: string
78
+ eventTime: string
79
+ province?: string
80
+ city?: string
81
+ address?: string
82
+ addressDetails?: string
83
+ }
84
+
85
+ export interface QuoteScheduleRules {
86
+ minAdvanceDays: number
87
+ maxAdvanceDays: number
88
+ minEventTime: string
89
+ maxEventTime: string
90
+ }
91
+
92
+ export interface QuoteProformaInput {
93
+ serviceType: string
94
+ scheduleRules: QuoteScheduleRules
95
+ serviceName: string
96
+ configFields: ProformaConfigField[]
97
+ breakdown: ProformaLineItem[]
98
+ includes: string[]
99
+ customerInfo: QuoteCustomerInfo | null
100
+ totals: {
101
+ subtotal: number
102
+ extras: number
103
+ shipping?: number
104
+ discount?: number
105
+ taxes?: number
106
+ total: number
107
+ }
108
+ dateStr: string
109
+ quoteNumber: string
110
+ isHomeDelivery?: boolean
111
+ }
112
+
113
+ export type GeneratePdfParams =
114
+ | { template: "order_invoice"; data: OrderInvoiceInput }
115
+ | { template: "quote_proforma"; data: QuoteProformaInput }
116
+
117
+ export class InvoiceGeneratorService {
118
+ generatePdf(params: GeneratePdfParams & { invoice_id?: string }): Promise<Buffer>
119
+ listInvoiceConfigs(filters?: Record<string, unknown>): Promise<any[]>
120
+ updateInvoiceConfigs(data: Record<string, unknown>): Promise<any>
121
+ retrieveInvoiceConfig(id: string): Promise<any>
122
+ listInvoices(filters?: Record<string, unknown>): Promise<any[]>
123
+ updateInvoices(data: any): Promise<any>
124
+ createInvoices(data: Record<string, unknown>): Promise<any>
125
+ deleteInvoices(id: string): Promise<void>
126
+ retrieveInvoice(id: string): Promise<any>
127
+ listInvoiceTemplates(filters?: Record<string, unknown>): Promise<any[]>
128
+ createInvoiceTemplates(data: Record<string, unknown>): Promise<any>
129
+ updateInvoiceTemplates(data: Record<string, unknown>): Promise<any>
130
+ deleteInvoiceTemplates(id: string): Promise<void>
131
+ retrieveInvoiceTemplate(id: string): Promise<any>
132
+ }
133
+ }
134
+
135
+ declare module "@codemind.ec/medusa-plugin-invoice/modules/invoice-generator" {
136
+ export const INVOICE_MODULE: "invoiceGenerator"
137
+ export default object
138
+ }
package/package.json CHANGED
@@ -1,14 +1,26 @@
1
1
  {
2
2
  "name": "@codemind.ec/medusa-plugin-invoice",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Invoice & quotation PDF widget for Medusa v2 admin — download order receipts and manage company billing config.",
5
5
  "author": "Kollor <dev@codemind.ec> (https://codemind.ec)",
6
6
  "license": "MIT",
7
7
  "files": [
8
- ".medusa/server"
8
+ ".medusa/server",
9
+ "index.d.ts"
9
10
  ],
11
+ "types": "./index.d.ts",
10
12
  "exports": {
13
+ ".": {
14
+ "types": "./index.d.ts",
15
+ "default": "./.medusa/server/src/index.js"
16
+ },
11
17
  "./package.json": "./package.json",
18
+ "./modules/invoice-generator": {
19
+ "types": "./index.d.ts",
20
+ "default": "./.medusa/server/src/modules/invoice-generator/index.js"
21
+ },
22
+ "./modules/*": "./.medusa/server/src/modules/*/index.js",
23
+ "./.medusa/server/src/modules/*": "./.medusa/server/src/modules/*/index.js",
12
24
  "./admin": {
13
25
  "import": "./.medusa/server/src/admin/index.mjs",
14
26
  "require": "./.medusa/server/src/admin/index.js",
@@ -32,6 +44,8 @@
32
44
  "prepublishOnly": "medusa plugin:build"
33
45
  },
34
46
  "devDependencies": {
47
+ "@codemirror/lang-html": "^6.4.11",
48
+ "@codemirror/theme-one-dark": "^6.1.3",
35
49
  "@medusajs/admin-sdk": "2.13.3",
36
50
  "@medusajs/admin-shared": "2.13.3",
37
51
  "@medusajs/cli": "2.13.3",
@@ -43,9 +57,13 @@
43
57
  "@medusajs/ui": "^4.1.3",
44
58
  "@swc/core": "^1.7.28",
45
59
  "@tanstack/react-query": "^5.64.2",
60
+ "@types/jsdom": "^28.0.1",
46
61
  "@types/node": "^20",
62
+ "@types/pdfmake": "^0.2.11",
47
63
  "@types/react": "^18.3.3",
48
64
  "@types/react-dom": "^18.3.0",
65
+ "@uiw/react-codemirror": "^4.25.8",
66
+ "handlebars": "^4.7.8",
49
67
  "prop-types": "^15.8.1",
50
68
  "react": "^18.3.1",
51
69
  "react-dom": "^18.3.1",
@@ -54,6 +72,12 @@
54
72
  "typescript": "^5",
55
73
  "vite": "^5.4.14"
56
74
  },
75
+ "dependencies": {
76
+ "axios": "^1.7.7",
77
+ "html-to-pdfmake": "^2.5.33",
78
+ "jsdom": "^29.0.1",
79
+ "pdfmake": "^0.2.20"
80
+ },
57
81
  "peerDependencies": {
58
82
  "@medusajs/admin-sdk": "2.13.3",
59
83
  "@medusajs/cli": "2.13.3",