@vicacha-devs/medusa-plugin-einvoicing 0.0.0-stage → 0.2.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 (70) hide show
  1. package/.medusa/server/src/admin/index.cjs +107 -0
  2. package/.medusa/server/src/admin/index.js +107 -0
  3. package/.medusa/server/src/api/admin/einvoicing/configs/route.d.ts +3 -0
  4. package/.medusa/server/src/api/admin/einvoicing/configs/route.js +23 -0
  5. package/.medusa/server/src/api/admin/einvoicing/configs/route.js.map +1 -0
  6. package/.medusa/server/src/api/admin/einvoicing/drivers/route.d.ts +2 -0
  7. package/.medusa/server/src/api/admin/einvoicing/drivers/route.js +6 -0
  8. package/.medusa/server/src/api/admin/einvoicing/drivers/route.js.map +1 -0
  9. package/.medusa/server/src/api/admin/einvoicing/route.d.ts +3 -0
  10. package/.medusa/server/src/api/admin/einvoicing/route.js +13 -0
  11. package/.medusa/server/src/api/admin/einvoicing/route.js.map +1 -0
  12. package/.medusa/server/src/api/middlewares.d.ts +2 -0
  13. package/.medusa/server/src/api/middlewares.js +21 -0
  14. package/.medusa/server/src/api/middlewares.js.map +1 -0
  15. package/.medusa/server/src/api/validators.d.ts +34 -0
  16. package/.medusa/server/src/api/validators.js +29 -0
  17. package/.medusa/server/src/api/validators.js.map +1 -0
  18. package/.medusa/server/src/index.d.ts +6 -0
  19. package/.medusa/server/src/index.js +7 -0
  20. package/.medusa/server/src/index.js.map +1 -0
  21. package/.medusa/server/src/modules/einvoicing/drivers/country-drivers.d.ts +34 -0
  22. package/.medusa/server/src/modules/einvoicing/drivers/country-drivers.js +221 -0
  23. package/.medusa/server/src/modules/einvoicing/drivers/country-drivers.js.map +1 -0
  24. package/.medusa/server/src/modules/einvoicing/index.d.ts +35 -0
  25. package/.medusa/server/src/modules/einvoicing/index.js +7 -0
  26. package/.medusa/server/src/modules/einvoicing/index.js.map +1 -0
  27. package/.medusa/server/src/modules/einvoicing/integrations/optional-bridges.d.ts +28 -0
  28. package/.medusa/server/src/modules/einvoicing/integrations/optional-bridges.js +67 -0
  29. package/.medusa/server/src/modules/einvoicing/integrations/optional-bridges.js.map +1 -0
  30. package/.medusa/server/src/modules/einvoicing/models/fiscal-config.d.ts +12 -0
  31. package/.medusa/server/src/modules/einvoicing/models/fiscal-config.js +14 -0
  32. package/.medusa/server/src/modules/einvoicing/models/fiscal-config.js.map +1 -0
  33. package/.medusa/server/src/modules/einvoicing/models/fiscal-document.d.ts +18 -0
  34. package/.medusa/server/src/modules/einvoicing/models/fiscal-document.js +24 -0
  35. package/.medusa/server/src/modules/einvoicing/models/fiscal-document.js.map +1 -0
  36. package/.medusa/server/src/modules/einvoicing/service.d.ts +70 -0
  37. package/.medusa/server/src/modules/einvoicing/service.js +112 -0
  38. package/.medusa/server/src/modules/einvoicing/service.js.map +1 -0
  39. package/.medusa/server/src/sdk/index.d.ts +4 -0
  40. package/.medusa/server/src/sdk/index.js +5 -0
  41. package/.medusa/server/src/sdk/index.js.map +1 -0
  42. package/.medusa/server/src/types/index.d.ts +66 -0
  43. package/.medusa/server/src/types/index.js +2 -0
  44. package/.medusa/server/src/types/index.js.map +1 -0
  45. package/.medusa/server/src/workflows/index.d.ts +43 -0
  46. package/.medusa/server/src/workflows/index.js +12 -0
  47. package/.medusa/server/src/workflows/index.js.map +1 -0
  48. package/README.md +58 -2
  49. package/package.json +93 -3
  50. package/src/__tests__/einvoicing.spec.ts +117 -0
  51. package/src/admin/i18n/en.json +4 -0
  52. package/src/admin/i18n/es.json +4 -0
  53. package/src/admin/i18n/index.ts +9 -0
  54. package/src/admin/routes/einvoicing/page.tsx +92 -0
  55. package/src/api/admin/einvoicing/configs/route.ts +39 -0
  56. package/src/api/admin/einvoicing/drivers/route.ts +16 -0
  57. package/src/api/admin/einvoicing/route.ts +31 -0
  58. package/src/api/middlewares.ts +28 -0
  59. package/src/api/validators.ts +30 -0
  60. package/src/index.ts +9 -0
  61. package/src/modules/einvoicing/drivers/country-drivers.ts +266 -0
  62. package/src/modules/einvoicing/index.ts +8 -0
  63. package/src/modules/einvoicing/integrations/optional-bridges.ts +83 -0
  64. package/src/modules/einvoicing/models/fiscal-config.ts +14 -0
  65. package/src/modules/einvoicing/models/fiscal-document.ts +24 -0
  66. package/src/modules/einvoicing/service.ts +156 -0
  67. package/src/sdk/index.ts +4 -0
  68. package/src/types/index.ts +82 -0
  69. package/src/workflows/index.ts +28 -0
  70. package/tsconfig.json +17 -0
@@ -0,0 +1,156 @@
1
+ import { MedusaError, MedusaService } from "@medusajs/framework/utils"
2
+ import type {
3
+ FiscalCountryDriver,
4
+ FiscalDriverContext,
5
+ IssueFiscalDocumentInput,
6
+ } from "../../types"
7
+ import { createBuiltInFiscalDrivers } from "./drivers/country-drivers"
8
+ import {
9
+ resolveOptionalB2BCompany,
10
+ resolveOptionalDianService,
11
+ resolveOptionalDocumentsPdfId,
12
+ } from "./integrations/optional-bridges"
13
+ import { FiscalConfigModel } from "./models/fiscal-config"
14
+ import { FiscalDocumentModel } from "./models/fiscal-document"
15
+
16
+ export class EinvoicingModuleService extends MedusaService({
17
+ FiscalDocument: FiscalDocumentModel,
18
+ FiscalConfig: FiscalConfigModel,
19
+ }) {
20
+ private readonly drivers_ = new Map<string, FiscalCountryDriver>()
21
+
22
+ constructor(
23
+ container: Record<string, unknown>,
24
+ options?: { drivers?: FiscalCountryDriver[] }
25
+ ) {
26
+ super(container, options)
27
+ for (const d of createBuiltInFiscalDrivers()) {
28
+ this.drivers_.set(d.id, d)
29
+ }
30
+ for (const custom of options?.drivers ?? []) {
31
+ this.drivers_.set(custom.id, custom)
32
+ }
33
+ }
34
+
35
+ async registerDriver(driver: FiscalCountryDriver): Promise<void> {
36
+ this.drivers_.set(driver.id, driver)
37
+ }
38
+
39
+ async listDrivers(): Promise<
40
+ Array<{ id: string; label: string; countryCodes: string[] }>
41
+ > {
42
+ return [...this.drivers_.values()].map((d) => ({
43
+ id: d.id,
44
+ label: d.label,
45
+ countryCodes: d.countryCodes,
46
+ }))
47
+ }
48
+
49
+ async resolveDriverForCountry(
50
+ countryCode: string,
51
+ preferredDriverId?: string
52
+ ): Promise<FiscalCountryDriver> {
53
+ if (preferredDriverId && this.drivers_.has(preferredDriverId)) {
54
+ return this.drivers_.get(preferredDriverId)!
55
+ }
56
+ const upper = countryCode.toUpperCase()
57
+ for (const driver of this.drivers_.values()) {
58
+ if (driver.countryCodes.includes(upper)) {
59
+ return driver
60
+ }
61
+ }
62
+ return this.drivers_.get("generic-webhook")!
63
+ }
64
+
65
+ async issueDocument(
66
+ input: IssueFiscalDocumentInput,
67
+ container?: { resolve?: (key: string) => any }
68
+ ) {
69
+ const countryCode = input.country_code.toUpperCase()
70
+ const configs = await this.listFiscalConfigs({
71
+ country_code: countryCode,
72
+ is_active: true,
73
+ })
74
+ const cfg = configs?.[0]
75
+ const driver = await this.resolveDriverForCountry(
76
+ countryCode,
77
+ input.driver_id || cfg?.driver_id
78
+ )
79
+
80
+ const b2bInfo = await resolveOptionalB2BCompany(container, input.company_id)
81
+ const buyerTaxId = b2bInfo?.tax_id || input.buyer_tax_id || null
82
+ if (buyerTaxId) {
83
+ const check = driver.validateTaxId(buyerTaxId)
84
+ if (!check.valid) {
85
+ throw new MedusaError(
86
+ MedusaError.Types.INVALID_DATA,
87
+ `Invalid buyer tax ID for ${countryCode} (${driver.id}): ${check.reason}`
88
+ )
89
+ }
90
+ }
91
+
92
+ const prefix = cfg?.prefix || "INV"
93
+ const seq = Number(cfg?.next_number || 1)
94
+ const document_number = `${prefix}-${countryCode}-${String(seq).padStart(6, "0")}`
95
+
96
+ const ctx: FiscalDriverContext = {
97
+ order_id: input.order_id,
98
+ country_code: countryCode,
99
+ document_type: input.document_type || "invoice",
100
+ document_number,
101
+ currency_code: (input.currency_code || "USD").toUpperCase(),
102
+ subtotal: Number(input.subtotal ?? 0),
103
+ tax_total: Number(input.tax_total ?? 0),
104
+ total: Number(input.total ?? 0),
105
+ issuer_tax_id: cfg?.issuer_tax_id || "ISSUER_TAX_ID",
106
+ issuer_name: cfg?.issuer_name || "Medusa Store",
107
+ buyer_tax_id: buyerTaxId,
108
+ buyer_name: b2bInfo?.legal_name || input.buyer_name || null,
109
+ buyer_email: b2bInfo?.email || input.buyer_email || null,
110
+ is_b2b: Boolean(b2bInfo?.is_b2b || input.company_id),
111
+ company_id: b2bInfo?.company_id || input.company_id || null,
112
+ items: input.items,
113
+ settings: (cfg?.settings as Record<string, unknown> | null) ?? null,
114
+ }
115
+
116
+ const dianService =
117
+ driver.id === "co-dian" ? resolveOptionalDianService(container) : null
118
+ const result =
119
+ dianService && typeof dianService.submitFromEinvoicing === "function"
120
+ ? await dianService.submitFromEinvoicing(ctx)
121
+ : await driver.submitDocument(ctx)
122
+ const pdfDocumentId = await resolveOptionalDocumentsPdfId(
123
+ container,
124
+ input.order_id
125
+ )
126
+
127
+ const created = await this.createFiscalDocuments({
128
+ order_id: input.order_id,
129
+ country_code: countryCode,
130
+ driver_id: driver.id,
131
+ document_type: ctx.document_type,
132
+ document_number,
133
+ external_id: result.external_id ?? null,
134
+ status: result.status,
135
+ buyer_tax_id: buyerTaxId,
136
+ is_b2b: ctx.is_b2b,
137
+ company_id: ctx.company_id,
138
+ pdf_document_id: pdfDocumentId,
139
+ payload_xml: result.payload_xml ?? null,
140
+ response_payload: result.response_payload ?? null,
141
+ error_message: result.error_message ?? null,
142
+ issued_at: result.success ? new Date() : null,
143
+ })
144
+
145
+ if (cfg?.id) {
146
+ await this.updateFiscalConfigs({
147
+ id: cfg.id,
148
+ next_number: seq + 1,
149
+ })
150
+ }
151
+
152
+ return created
153
+ }
154
+ }
155
+
156
+ export default EinvoicingModuleService
@@ -0,0 +1,4 @@
1
+ export const einvoicingEndpoints = {
2
+ adminBase: "/admin/einvoicing",
3
+ storeBase: "/store/einvoicing",
4
+ } as const
@@ -0,0 +1,82 @@
1
+ export type FiscalDocumentType =
2
+ | "invoice"
3
+ | "credit_note"
4
+ | "debit_note"
5
+ | "pos_receipt"
6
+
7
+ export type FiscalDocumentStatus =
8
+ | "draft"
9
+ | "submitted"
10
+ | "accepted"
11
+ | "rejected"
12
+ | "cancelled"
13
+
14
+ export interface FiscalTaxIdValidation {
15
+ valid: boolean
16
+ normalized?: string
17
+ reason?: string
18
+ }
19
+
20
+ export interface FiscalDriverContext {
21
+ order_id: string
22
+ country_code: string
23
+ document_type: FiscalDocumentType
24
+ document_number: string
25
+ currency_code: string
26
+ subtotal: number
27
+ tax_total: number
28
+ total: number
29
+ issuer_tax_id: string
30
+ issuer_name: string
31
+ buyer_tax_id?: string | null
32
+ buyer_name?: string | null
33
+ buyer_email?: string | null
34
+ is_b2b?: boolean
35
+ company_id?: string | null
36
+ items?: Array<{
37
+ title: string
38
+ quantity: number
39
+ unit_price: number
40
+ tax_rate?: number
41
+ }>
42
+ settings?: Record<string, unknown> | null
43
+ }
44
+
45
+ export interface FiscalSubmissionResult {
46
+ success: boolean
47
+ status: FiscalDocumentStatus
48
+ external_id?: string
49
+ payload_xml?: string
50
+ response_payload?: Record<string, unknown>
51
+ error_message?: string
52
+ }
53
+
54
+ export interface FiscalCountryDriver {
55
+ id: string
56
+ label: string
57
+ countryCodes: string[]
58
+ validateTaxId: (taxId: string) => FiscalTaxIdValidation
59
+ buildPayload: (ctx: FiscalDriverContext) => string
60
+ submitDocument: (ctx: FiscalDriverContext) => Promise<FiscalSubmissionResult>
61
+ }
62
+
63
+ export interface IssueFiscalDocumentInput {
64
+ order_id: string
65
+ country_code: string
66
+ driver_id?: string
67
+ document_type?: FiscalDocumentType
68
+ currency_code?: string
69
+ subtotal?: number
70
+ tax_total?: number
71
+ total?: number
72
+ buyer_tax_id?: string | null
73
+ buyer_name?: string | null
74
+ buyer_email?: string | null
75
+ company_id?: string | null
76
+ items?: FiscalDriverContext["items"]
77
+ }
78
+
79
+ export interface CreateEinvoicingInput {
80
+ label: string
81
+ active?: boolean
82
+ }
@@ -0,0 +1,28 @@
1
+ import {
2
+ createStep,
3
+ createWorkflow,
4
+ StepResponse,
5
+ WorkflowResponse,
6
+ } from "@medusajs/framework/workflows-sdk"
7
+ import { EINVOICING_MODULE } from "../modules/einvoicing"
8
+ import type EinvoicingModuleService from "../modules/einvoicing/service"
9
+ import type { IssueFiscalDocumentInput } from "../types"
10
+
11
+ export const issueFiscalDocumentStep = createStep(
12
+ "issue-fiscal-document",
13
+ async (input: IssueFiscalDocumentInput, { container }) => {
14
+ const service = container.resolve(
15
+ EINVOICING_MODULE
16
+ ) as EinvoicingModuleService
17
+ const doc = await service.issueDocument(input, container)
18
+ return new StepResponse(doc)
19
+ }
20
+ )
21
+
22
+ export const issueFiscalDocumentWorkflow = createWorkflow(
23
+ "issue-fiscal-document",
24
+ (input: IssueFiscalDocumentInput) => {
25
+ const document = issueFiscalDocumentStep(input)
26
+ return new WorkflowResponse(document)
27
+ }
28
+ )
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "extends": "../../packages/typescript-config/plugin.json",
3
+ "compilerOptions": {
4
+ "rootDir": "./",
5
+ "outDir": "./.medusa/server",
6
+ "noEmit": false
7
+ },
8
+ "include": ["**/*", ".medusa/types/*"],
9
+ "exclude": [
10
+ "node_modules",
11
+ ".medusa/server",
12
+ ".medusa/admin",
13
+ "src/admin",
14
+ "src/__tests__",
15
+ "eslint.config.*"
16
+ ]
17
+ }