@cfdi/xml
Version:
Generacion, sellado y timbrado de XML CFDI 4.0 para Node.js - facturacion electronica Mexico
3 lines (2 loc) • 14.5 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("fs"),f=require("@cfdi/csd"),r=require("@cfdi/xsd"),R=require("os"),g=require("path"),N=require("@cfdi/transform"),D=require("@saxon-he/cli"),p=require("xml-js"),x=o=>o.join(" "),c=(o,t)=>{const e={};return t.forEach(i=>{i in o&&(e[i]=o[i])}),Object.keys(o).forEach(i=>{i in e||(e[i]=o[i])}),e};class P{xml={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}}};tc="cfdi:Comprobante";version="4.0";XMLSchema="http://www.w3.org/2001/XMLSchema-instance";cfd="http://www.sat.gob.mx/cfd/4";locations=["http://www.sat.gob.mx/cfd/4","http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd"];schema=r.Schema.of();constructor(t){const{debug:e,schema:i}=t||{debug:!1};this.schema.setConfig({debug:e,path:i?.path}),this.restartCfdi()}xmlns(t){t.xsi||this.addXmlns("xsi",this.XMLSchema),t.cfdi||this.addXmlns("cfdi",this.cfd);for(const e in t)this.addXmlns(e,t[e])}addXmlns(t,e){this.xml["cfdi:Comprobante"]._attributes[`xmlns:${t}`]=e}addSchemaLocation(t){const e="xsi:schemaLocation";this.xml["cfdi:Comprobante"]._attributes[e]||(this.xml["cfdi:Comprobante"]._attributes[e]="");const s=(this.xml["cfdi:Comprobante"]._attributes[e]||"").split(" "),a=Array.from(new Set([...s,...t].filter(Boolean))),l=x(a);this.xml["cfdi:Comprobante"]._attributes[e]=l}setAttributesXml(t={}){const{version:e="1.0",encoding:i="utf-8"}=t;this.xml._declaration._attributes={version:e,encoding:i}}setAttributes(t={}){const{xmlns:e,schemaLocation:i}=t||{};this.xmlns(e||{}),this.addSchemaLocation(i||this.locations)}comprobante(t){const e=["xsi:schemaLocation","Version","Serie","Folio","Fecha","Sello","FormaPago","NoCertificado","Certificado","CondicionesDePago","SubTotal","Descuento","Moneda","TipoCambio","Total","TipoDeComprobante","Exportacion","MetodoPago","LugarExpedicion","Confirmacion","xmlns:cfdi","xmlns:xsi"],i=c({...this.xml["cfdi:Comprobante"]._attributes,Version:this.version,...t,Sello:"",NoCertificado:"",Certificado:"",SubTotal:t.SubTotal,Descuento:t.Descuento,Total:t.Total},e);this.xml["cfdi:Comprobante"]._attributes=i,this.schema.cfdi.comprobante.validateInit(this.xml["cfdi:Comprobante"]._attributes)}informacionGlobal(t){this.schema.cfdi.informacionGlobal.validate(t),this.xml["cfdi:Comprobante"]={"cfdi:InformacionGlobal":{_attributes:t},...this.xml["cfdi:Comprobante"]}}relacionados(t){this.xml["cfdi:Comprobante"]={"cfdi:CfdiRelacionados":t.getRelation(),...this.xml["cfdi:Comprobante"]}}emisor(t){this.xml["cfdi:Comprobante"]["cfdi:Emisor"]=t.emisor}receptor(t){this.xml["cfdi:Comprobante"]["cfdi:Receptor"]=t.receptor}concepto(t){if(t.isComplement()){const e=t.getComplementProperties();this.addXmlns(e.xmlnskey,e.xmlns),this.addSchemaLocation(e.schemaLocation)}this.xml["cfdi:Comprobante"]["cfdi:Conceptos"]||(this.xml["cfdi:Comprobante"]["cfdi:Conceptos"]={"cfdi:Concepto":[]}),this.xml["cfdi:Comprobante"]["cfdi:Conceptos"]["cfdi:Concepto"].push(t.getConcept())}impuesto(t){this.xml["cfdi:Comprobante"]["cfdi:Impuestos"]=t.impuesto}complemento(t){this.xml["cfdi:Comprobante"]["cfdi:Complemento"]||(this.xml["cfdi:Comprobante"]["cfdi:Complemento"]={});const e=t.getComplement();this.addXmlns(e.xmlnskey,e.xmlns),this.addSchemaLocation(e.schemaLocation),this.xml["cfdi:Comprobante"]["cfdi:Complemento"][e.key]=e.complement}setCertificado(t){t&&(this.xml["cfdi:Comprobante"]._attributes.Certificado=t)}setNoCertificado(t){t&&(this.xml["cfdi:Comprobante"]._attributes.NoCertificado=t)}setSello(t){t&&(this.xml["cfdi:Comprobante"]._attributes.Sello=t)}restartCfdi(){this.xml={_declaration:{_attributes:{version:"1.0",encoding:"utf-8"}}},this.xml["cfdi:Comprobante"]={_attributes:{},"cfdi:Emisor":{},"cfdi:Receptor":{}},this.setAttributes()}get xmlObject(){return this.xml}}class C{static generateNameTemp(){return Date.now().toString()}static readFileSync(t){return n.readFileSync(t,"utf8")}static getTmpFullPath(t){return g.join(R.tmpdir(),`${t}.xml`)}}class d{static info(t,...e){console.log(`INFO: ${t}`,...e)}static error(t,...e){console.error(`ERROR: ${t}`,...e)}static warn(t,...e){console.warn(`WARN: ${t}`,...e)}}class h{code;details;name;message;method="";constructor({message:t,code:e,details:i,name:s,method:a}){this.message=t,this.code=e,this.details=i,this.name=s||"XmlError",this.method=a||""}setName(t){t&&(this.name=t)}setMethod(t){t&&(this.method=t)}toString(){return`${this.name}: ${this.message} ${this.method}`}}function u({e:o,name:t,method:e,debug:i=!1}){if(o instanceof h)return o.setName(t),o.setMethod(e),i&&d.error(o.toString()),o;if(o instanceof Error){const a=new h({message:o.message,code:"error",method:e,name:t});return i&&d.error(a.toString()),a}const s=new h({message:String(o),code:"error",name:t,method:e});return i&&d.error(s.toString()),s}class T extends P{_cadenaOriginal="";saxon=void 0;xslt=null;debug=!1;constructor(t){super(t),this.xslt=t?.xslt,this.saxon=t?.saxon,this._cadenaOriginal="",this.setDebug(!!t?.debug)}certificar(t){try{const e=f.Certificate.fromFileSync(t);return this.setNoCertificado(e.noCertificado()),this.setCertificado(e.toBase64()),this}catch(e){throw u({e,method:"certificar",debug:this.debug,name:"@cfdi/csd"})}}async sellar(t,e){const i=await this.generarCadenaOriginal(),s=await this.generarSello(i,t,e);this._cadenaOriginal=i,this.setSello(s)}getJsonCdfi(){return this.xml}getXmlCdfi(){const t={compact:!0,ignoreComment:!0,spaces:4},e=p.js2xml({...this.xml},t);return this.restartCfdi(),e}saveFile(t,e,i){try{const s=`${e}${i}.xml`;return n.writeFileSync(s,Buffer.from(t,"base64"),"utf8"),!0}catch{return!1}}generarCadenaOriginal(){if(!this.xslt)throw new Error("¡Ups! Direcction Not Found Extensible Stylesheet Language Transformation");try{const t=C.getTmpFullPath(C.generateNameTemp()),e={compact:!0,ignoreComment:!0,spaces:4},i=p.js2xml(this.xml,e);n.writeFileSync(t,i,"utf8");let s;return this.saxon?s=new D.Transform(this.saxon).s(t).xsl(String(this.xslt.path)).warnings("silent").run():s=new N.Transform().s(t).xsl(String(this.xslt.path)).warnings("silent").run(),this.debug&&(console.log("xslt =>",this.xslt),console.log("cadena original =>",s)),n.unlinkSync(t),s}catch(t){throw u({e:t,method:"getCadenaOriginal",debug:this.debug,name:"@cfdi/xml"})}}generarSello(t,e,i){try{return f.PrivateKey.fromFileSync(e,i).sign(t)}catch(s){throw u({e:s,method:"getSello",debug:this.debug,name:"@cfdi/xml => @cfdi/csd"})}}get sello(){return this.xml["cfdi:Comprobante"]?._attributes?.Sello||""}get cadenaOriginal(){return this._cadenaOriginal}get isBebug(){return this.debug}setDebug(t){this.debug=t}}class L{relacionada={};constructor(t){r.Schema.of().cfdi.relacionados.validate(t),this.relacionada._attributes=t}addRelation(t){this.relacionada["cfdi:CfdiRelacionado"]||(this.relacionada["cfdi:CfdiRelacionado"]=[]);const e={UUID:t};r.Schema.of().cfdi.relacionado.validate(e),this.relacionada["cfdi:CfdiRelacionado"].push({_attributes:e})}getRelation(){return this.relacionada}toJson(){return this.relacionada}}class F{emisor={_attributes:{Rfc:"",Nombre:"",RegimenFiscal:""}};constructor(t){r.Schema.of().cfdi.emisor.validate(t),this.emisor._attributes=t}setRfc(t){this.emisor._attributes.Rfc=t}setNombre(t){this.emisor._attributes.Nombre=t}setRegimenFiscal(t){this.emisor._attributes.RegimenFiscal=t}setFacAtrAdquirente(t){this.emisor._attributes.FacAtrAdquirente=t}toJson(){return r.Schema.of().cfdi.emisor.validate(this.emisor._attributes),this.emisor}}class w{receptor={_attributes:{Rfc:"",Nombre:"",UsoCFDI:"",DomicilioFiscalReceptor:"",RegimenFiscalReceptor:""}};constructor(t){r.Schema.of().cfdi.receptor.validate(t),this.receptor._attributes=t}setRFC(t){this.receptor._attributes.Rfc=t}setNombre(t){this.receptor._attributes.Nombre=t}setUsoCFDI(t){this.receptor._attributes.UsoCFDI=t}setDomicilioFiscalReceptor(t){this.receptor._attributes.DomicilioFiscalReceptor=t}setResidenciaFiscal(t){this.receptor._attributes.ResidenciaFiscal=t}setNumRegIdTrib(t){this.receptor._attributes.NumRegIdTrib=t}setRegimenFiscalReceptor(t){this.receptor._attributes.RegimenFiscalReceptor=t}toJson(){return r.Schema.of().cfdi.receptor.validate(this.receptor._attributes),this.receptor}}class _{impuesto={};constructor(t={}){if(Object.keys(t).length!==0){const e=t;r.Schema.of().cfdi.impuestos.validate(e);const i=c(e,["TotalImpuestosRetenidos","TotalImpuestosTrasladados"]);this.impuesto._attributes=i}}setTraslado(t){this.impuesto["cfdi:Traslados"]||(this.impuesto["cfdi:Traslados"]={"cfdi:Traslado":[]});const i={_attributes:c(t,["Base","Impuesto","TipoFactor","TasaOCuota","Importe"])};return this.impuesto["cfdi:Traslados"]["cfdi:Traslado"].push(i),this}setRetencion(t){this.impuesto["cfdi:Retenciones"]||(this.impuesto["cfdi:Retenciones"]={"cfdi:Retencion":[]});const i={_attributes:c(t,["Base","Impuesto","TipoFactor","TasaOCuota","Importe"])};return this.impuesto["cfdi:Retenciones"]["cfdi:Retencion"].push(i),this}getTotalImpuestos(){return this.impuesto._attributes}getRetenciones(){return this.impuesto["cfdi:Retenciones"]["cfdi:Retencion"]}getTraslados(){return this.impuesto["cfdi:Traslados"]["cfdi:Traslado"]}}class b extends _{existComplemnt=!1;complementProperties={};concepto={};constructor(t){super();const e={...t};this.existComplemnt=!1,r.Schema.of().concepto.concepto.validate(e),this.concepto._attributes=e}complemento(t){this.concepto["cfdi:ComplementoConcepto"]||(this.concepto["cfdi:ComplementoConcepto"]={}),this.existComplemnt=!0;const{complement:e,key:i,schemaLocation:s,xmlns:a,xmlnskey:l}=t.getComplement();this.complementProperties.key=i,this.complementProperties.xmlns=a,this.complementProperties.xmlnskey=l,this.complementProperties.schemaLocation=s,this.concepto["cfdi:ComplementoConcepto"][i]=e}terceros(t){return r.Schema.of().concepto.terceros.validate(t),this.concepto["cfdi:ACuentaTerceros"]={_attributes:t},this}predial(t){const e={Numero:t};return r.Schema.of().concepto.predial.validate(e),this.concepto["cfdi:CuentaPredial"]={_attributes:e},this}parte(t){const e={...t,Cantidad:Number(t.Cantidad),ValorUnitario:Number(t.ValorUnitario),Importe:Number(t.ValorUnitario)};return r.Schema.of().concepto.parte.validate(e),this.concepto["cfdi:Parte"]={_attributes:e},this}aduana(t){const e={NumeroPedimento:t};return r.Schema.of().concepto.informacionAduanera.validate(e),{_attributes:e}}setParteInformacionAduanera(t){return this.concepto["cfdi:Parte"]?(this.concepto["cfdi:Parte"]["cfdi:InformacionAduanera"]||(this.concepto["cfdi:Parte"]["cfdi:InformacionAduanera"]=[]),this.concepto["cfdi:Parte"]["cfdi:InformacionAduanera"].push(this.aduana(t)),this):(console.log("utilize primero parte"),this)}InformacionAduanera(t){return this.concepto["cfdi:InformacionAduanera"]||(this.concepto["cfdi:InformacionAduanera"]=[]),this.concepto["cfdi:InformacionAduanera"].push(this.aduana(t)),this}traslado(t){const e={...t};return r.Schema.of().concepto.traslado.validate(e),this.setTraslado(e),this.concepto["cfdi:Impuestos"]=this.impuesto,this}retencion(t){const e={...t};return this.setRetencion(e),this.concepto["cfdi:Impuestos"]=this.impuesto,this}getConcept(){const t={...this.concepto};return this.concepto={},t}isComplement(){return this.existComplemnt}getComplementProperties(){return this.complementProperties}}class M extends _{constructor(t={}){super(t)}traslados(t){const e={...t};return r.Schema.of().cfdi.traslado.validate(e),this.setTraslado(e),this}retenciones(t){const e={...t};return r.Schema.of().cfdi.retencion.validate(e),this.setRetencion(e),this}}var S=(o=>(o.NoobjetoDeimpuesto="01",o.SíObjetoDeImpuesto="02",o.SíObjetoDeImpuestoYNobligadoAlDesglose="03",o))(S||{}),E=(o=>(o.INGRESO="I",o.EGRESO="E",o.TRASLADO="T",o.NOMINA="N",o.PAGO="P",o))(E||{}),O=(o=>(o.NOTA_DE_CREDITO="01",o.NOTA_DE_DEBITO="02",o.DELOVUCION_DE_MERCANCIA="03",o.SUSTITUCION_DE_CFDI_PREVIOS="04",o.TRASLADOS_DE_MERCANCIA_FACTURADOS_PREVIAMENTE="05",o.FACTURA_POR_TRASLADOS_PREVIOS="06",o.APLICACION_DE_ANTICIPO="07",o.PAGOS_EN_PARCIALIDADES="08",o.PAGOS_DIFERIDOS="09",o))(O||{}),I=(o=>(o.GENERAL_LEY_DE_PERSONAS_MORALES="601",o.PERSONAS_MORALES_CON_FINES_NO_LUCRATIVOS="603",o.SUELDOS_Y_SALARIOS="605",o.ARRENDAMIENTO="606",o.DEMAS_INGRESOS="608",o.CONSOLIDACION="609",o.RESIDENTES_EN_EL_EXTRANJERO="610",o.INGRESOS_POR_DIVIDENDOS_SOCIOS_Y_ACCIONISTAS="611",o.PERSONAS_FISICAS_CON_ACTIVIDADES_EMPRESARIALES_Y_PROFESIONALES="612",o.INGRESOS_POR_INTERESES="614",o.SIN_OBLIGACIONES_FISCALES="616",o.SOCIEDADES_COOPERATIVAS_DE_PRODUCCION="620",o.REGIMEN_DE_INCORPORACION_FISCAL="621",o.ACTIVIDADES_AGRICOLAS_GANADERAS_SILVICOLAS_Y_PESQUERAS="622",o.OPCIONAL_PARA_GRUPOS_DE_SOCIEDADES="623",o.COORDINADOS="624",o.HIDROCARBUROS="628",o.REGIMEN_DE_ENAJENACION_O_ADQUISICION_DE_BIENES="607",o.PREFERENTES_Y_EMPRESAS_MULTINACIONALES="629",o.ENAJENACION_DE_ACCIONES_EN_BOLSA_DE_VALORES="630",o.REGIMEN_DE_LOS_INGRESOS_POR_OBTENCION_DE_PREMIOS="615",o))(I||{});const A=Symbol.for("@cfdi/xml.donation-shown"),m=typeof process<"u"&&process.env||{},v=m.NODE_ENV==="production",U=m.NODE_ENV==="test"||!!m.VITEST||!!m.JEST_WORKER_ID,G=globalThis[A]===!0;!v&&!U&&!G&&(globalThis[A]=!0,console.log(["╔══════════════════════════════════════════════════════════╗","║ 💛 ¿Te gusta @cfdi/xml? ¡Apóyame con una donación! 💛 ║","╠══════════════════════════════════════════════════════════╣","║ ☕ Buy Me a Coffee: ║","║ https://buymeacoffee.com/recreandodev ║","║ ║","║ ✉️ Correo: ║","║ amisael.amir.misael@gmail.com ║","║ (escríbeme y te paso mi número de cuenta) ║","║ ║","║ 🛠️ Este mensaje solo aparece en modo desarrollo ║","╚══════════════════════════════════════════════════════════╝"].join(`
`)));exports.CFDI=T;exports.Concepto=b;exports.Concepts=b;exports.Emisor=F;exports.Impuestos=M;exports.InvoiceRelation=O;exports.InvoiceType=E;exports.ObjetoImpEnum=S;exports.Receptor=w;exports.Relacionado=L;exports.TaxSystem=I;