|
| 1 | +/** |
| 2 | + * Ejemplos de facturas con impuestos locales (CFDI complemento Impuestos Locales) usando el SDK de FiscalAPI |
| 3 | + * Todos los métodos usan el modo "ByReferences" - se envían IDs de entidades pre-configuradas en FiscalAPI |
| 4 | + */ |
| 5 | + |
| 6 | +import { FiscalapiClient, FiscalapiSettings, Invoice, InvoiceItem, ItemTax } from '../src/index'; |
| 7 | +import { inspect } from 'util'; |
| 8 | + |
| 9 | +// Configuración de la consola para mostrar objetos anidados |
| 10 | +inspect.defaultOptions.depth = null; |
| 11 | +inspect.defaultOptions.colors = true; |
| 12 | + |
| 13 | +// Configuración de FiscalAPI |
| 14 | +const settings: FiscalapiSettings = { |
| 15 | + // apiUrl: 'https://test.fiscalapi.com', |
| 16 | + // apiKey: '<API_KEY>', |
| 17 | + // tenant: '<TENANT_ID>', |
| 18 | + apiUrl: "http://localhost:5001", |
| 19 | + apiKey: "sk_development_b470ea83_3c0f_4209_b933_85223b960d91", |
| 20 | + tenant: "102e5f13-e114-41dd-bea7-507fce177281", |
| 21 | + debug: true |
| 22 | +}; |
| 23 | + |
| 24 | +// IDs de personas pre-configuradas en FiscalAPI (modo ByReferences) |
| 25 | +const currentDate = '2026-01-27T10:04:06'; |
| 26 | +const escuelaKemperUrgateId = "2e7b988f-3a2a-4f67-86e9-3f931dd48581"; |
| 27 | +const karlaFuenteNolascoId = "109f4d94-63ea-4a21-ab15-20c8b87d8ee9"; |
| 28 | + |
| 29 | +// Items comunes para las facturas de ingreso |
| 30 | +const invoiceItems: InvoiceItem[] = [ |
| 31 | + { |
| 32 | + itemCode: '01010101', |
| 33 | + quantity: '9.5', |
| 34 | + unitOfMeasurementCode: 'E48', |
| 35 | + description: 'Invoicing software as a service', |
| 36 | + unitPrice: '3587.75', |
| 37 | + taxObjectCode: '02', |
| 38 | + itemSku: '7506022301697', |
| 39 | + discount: '255.85', |
| 40 | + itemTaxes: [ |
| 41 | + { taxCode: '002', taxTypeCode: 'Tasa', taxRate: '0.160000', taxFlagCode: 'T' } as ItemTax |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + itemCode: '01010101', |
| 46 | + quantity: '8', |
| 47 | + unitOfMeasurementCode: 'E48', |
| 48 | + description: 'Software Consultant', |
| 49 | + unitPrice: '250.85', |
| 50 | + taxObjectCode: '02', |
| 51 | + itemSku: '7506022301698', |
| 52 | + discount: '255.85', |
| 53 | + itemTaxes: [ |
| 54 | + { taxCode: '002', taxTypeCode: 'Tasa', taxRate: '0.160000', taxFlagCode: 'T' } as ItemTax |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + itemCode: '01010101', |
| 59 | + quantity: '6', |
| 60 | + unitOfMeasurementCode: 'E48', |
| 61 | + description: 'Computer software', |
| 62 | + unitPrice: '1250.75', |
| 63 | + taxObjectCode: '02', |
| 64 | + itemSku: '7506022301699', |
| 65 | + itemTaxes: [ |
| 66 | + { taxCode: '002', taxTypeCode: 'Tasa', taxRate: '0.160000', taxFlagCode: 'T' } as ItemTax, |
| 67 | + { taxCode: '002', taxTypeCode: 'Tasa', taxRate: '0.106666', taxFlagCode: 'R' } as ItemTax |
| 68 | + ] |
| 69 | + } |
| 70 | +]; |
| 71 | + |
| 72 | +// ============================================================================ |
| 73 | +// 1. FACTURA INGRESO - IMPUESTOS LOCALES CEDULAR + ISH (ByReferences) |
| 74 | +// ============================================================================ |
| 75 | +async function facturaImpuestosLocalesCedularIshByReferences(client: FiscalapiClient): Promise<void> { |
| 76 | + console.log('\n=== Factura Ingreso con Impuestos Locales CEDULAR + ISH (ByReferences) ===\n'); |
| 77 | + |
| 78 | + const invoice: Invoice = { |
| 79 | + versionCode: '4.0', |
| 80 | + series: 'F', |
| 81 | + date: currentDate, |
| 82 | + paymentFormCode: '01', |
| 83 | + currencyCode: 'MXN', |
| 84 | + typeCode: 'I', |
| 85 | + expeditionZipCode: '42501', |
| 86 | + paymentMethodCode: 'PUE', |
| 87 | + exchangeRate: 1, |
| 88 | + exportCode: '01', |
| 89 | + issuer: { |
| 90 | + id: escuelaKemperUrgateId |
| 91 | + }, |
| 92 | + recipient: { |
| 93 | + id: karlaFuenteNolascoId |
| 94 | + }, |
| 95 | + items: invoiceItems, |
| 96 | + complement: { |
| 97 | + localTaxes: { |
| 98 | + taxes: [ |
| 99 | + { taxName: 'CEDULAR', taxRate: '3.00', taxAmount: '6.00', taxFlagCode: 'R' }, |
| 100 | + { taxName: 'ISH', taxRate: '8.00', taxAmount: '16.00', taxFlagCode: 'R' } |
| 101 | + ] |
| 102 | + } |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + const response = await client.invoices.create(invoice); |
| 107 | + console.log('Response:', response); |
| 108 | +} |
| 109 | + |
| 110 | +// ============================================================================ |
| 111 | +// 2. FACTURA INGRESO - IMPUESTOS LOCALES CEDULAR (ByReferences) |
| 112 | +// ============================================================================ |
| 113 | +async function facturaImpuestosLocalesCedularByReferences(client: FiscalapiClient): Promise<void> { |
| 114 | + console.log('\n=== Factura Ingreso con Impuestos Locales CEDULAR (ByReferences) ===\n'); |
| 115 | + |
| 116 | + const invoice: Invoice = { |
| 117 | + versionCode: '4.0', |
| 118 | + series: 'F', |
| 119 | + date: currentDate, |
| 120 | + paymentFormCode: '01', |
| 121 | + currencyCode: 'MXN', |
| 122 | + typeCode: 'I', |
| 123 | + expeditionZipCode: '42501', |
| 124 | + paymentMethodCode: 'PUE', |
| 125 | + exchangeRate: 1, |
| 126 | + exportCode: '01', |
| 127 | + issuer: { |
| 128 | + id: escuelaKemperUrgateId |
| 129 | + }, |
| 130 | + recipient: { |
| 131 | + id: karlaFuenteNolascoId |
| 132 | + }, |
| 133 | + items: invoiceItems, |
| 134 | + complement: { |
| 135 | + localTaxes: { |
| 136 | + taxes: [ |
| 137 | + { taxName: 'CEDULAR', taxRate: '3.00', taxAmount: '6.00', taxFlagCode: 'R' } |
| 138 | + ] |
| 139 | + } |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + const response = await client.invoices.create(invoice); |
| 144 | + console.log('Response:', response); |
| 145 | +} |
| 146 | + |
| 147 | +// ============================================================================ |
| 148 | +// 3. FACTURA INGRESO - IMPUESTOS LOCALES ISH (ByReferences) |
| 149 | +// ============================================================================ |
| 150 | +async function facturaImpuestosLocalesIshByReferences(client: FiscalapiClient): Promise<void> { |
| 151 | + console.log('\n=== Factura Ingreso con Impuestos Locales ISH (ByReferences) ===\n'); |
| 152 | + |
| 153 | + const invoice: Invoice = { |
| 154 | + versionCode: '4.0', |
| 155 | + series: 'F', |
| 156 | + date: currentDate, |
| 157 | + paymentFormCode: '01', |
| 158 | + currencyCode: 'MXN', |
| 159 | + typeCode: 'I', |
| 160 | + expeditionZipCode: '42501', |
| 161 | + paymentMethodCode: 'PUE', |
| 162 | + exchangeRate: 1, |
| 163 | + exportCode: '01', |
| 164 | + issuer: { |
| 165 | + id: escuelaKemperUrgateId |
| 166 | + }, |
| 167 | + recipient: { |
| 168 | + id: karlaFuenteNolascoId |
| 169 | + }, |
| 170 | + items: invoiceItems, |
| 171 | + complement: { |
| 172 | + localTaxes: { |
| 173 | + taxes: [ |
| 174 | + { taxName: 'ISH', taxRate: '8.00', taxAmount: '16.00', taxFlagCode: 'R' } |
| 175 | + ] |
| 176 | + } |
| 177 | + } |
| 178 | + }; |
| 179 | + |
| 180 | + const response = await client.invoices.create(invoice); |
| 181 | + console.log('Response:', response); |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +// ============================================================================ |
| 186 | +// FUNCION PRINCIPAL |
| 187 | +// ============================================================================ |
| 188 | +async function main(): Promise<void> { |
| 189 | + console.log('=== Ejemplos de Factura con Impuestos Locales FiscalAPI (ByReferences) ===\n'); |
| 190 | + |
| 191 | + const client = FiscalapiClient.create(settings); |
| 192 | + |
| 193 | + try { |
| 194 | + // Descomentar el caso de uso que se desea ejecutar |
| 195 | + |
| 196 | + //await facturaImpuestosLocalesCedularIshByReferences(client); |
| 197 | + // await facturaImpuestosLocalesCedularByReferences(client); |
| 198 | + await facturaImpuestosLocalesIshByReferences(client); |
| 199 | + |
| 200 | + |
| 201 | + console.log('\nEjecución completada.'); |
| 202 | + } catch (error) { |
| 203 | + console.error('Error:', error); |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +// Ejecutar función principal |
| 208 | +main(); |
0 commit comments