herramientaenvioestupendo 1.1.13 → 1.1.15
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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/index.js +1 -1
- package/src/services.js +37 -20
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Para instalar el aplicativo CLI, deben realizar el siguiente procedimiento :
|
|
|
14
14
|
```npm i -g herramientaenvioestupendo```
|
|
15
15
|
|
|
16
16
|
- Luego de esto deben validar la version de la herramienta :
|
|
17
|
-
```estupendo --version```, si esta indica que es la 1.1.
|
|
17
|
+
```estupendo --version```, si esta indica que es la 1.1.15 es la correcta.
|
|
18
18
|
|
|
19
19
|
- Para conocer que opciones tienen de comandos solo deben ingresar en su CMD :
|
|
20
20
|
```estupendo --help```
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { program } from "commander"
|
|
|
3
3
|
import { sendTxt, sendXml, sendAttached, sendJson, requestDocumentRace, sendTxtCYP, sendTxtFlexo } from "./services.js"
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
program.name("estupendo").version("1.1.
|
|
6
|
+
program.name("estupendo").version("1.1.15")
|
|
7
7
|
|
|
8
8
|
program.command("send_xml")
|
|
9
9
|
.requiredOption("-d, --directory <char>", "Directorio donde se encuentran los documentos XML")
|
package/src/services.js
CHANGED
|
@@ -467,7 +467,7 @@ const requestDocumentRace = async (options) => {
|
|
|
467
467
|
return
|
|
468
468
|
}
|
|
469
469
|
|
|
470
|
-
let urlBase = "https://
|
|
470
|
+
let urlBase = "https://dev.estupendo.com.co/api"
|
|
471
471
|
let identificacion = "";
|
|
472
472
|
let token = "";
|
|
473
473
|
let fechaIni = "";
|
|
@@ -529,6 +529,7 @@ const requestDocumentRace = async (options) => {
|
|
|
529
529
|
// Descargar y guardar XML
|
|
530
530
|
if (xml) {
|
|
531
531
|
const xmlPath = path.join(xmlDir, `${baseName}.xml`);
|
|
532
|
+
|
|
532
533
|
try {
|
|
533
534
|
const response = await axios.get(xml, { responseType: 'arraybuffer' });
|
|
534
535
|
fs.writeFileSync(xmlPath, response.data);
|
|
@@ -538,37 +539,48 @@ const requestDocumentRace = async (options) => {
|
|
|
538
539
|
const parser = new xml2js.Parser({ explicitArray: false });
|
|
539
540
|
const parsedXml = await parser.parseStringPromise(xmlContent);
|
|
540
541
|
|
|
541
|
-
|
|
542
|
-
let extractedValue = "";
|
|
543
|
-
if (
|
|
544
|
-
parsedXml &&
|
|
545
|
-
parsedXml["AttachedDocument"] &&
|
|
546
|
-
parsedXml["AttachedDocument"]["cac:Attachment"]["cac:ExternalReference"]["cbc:Description"]
|
|
547
|
-
) {
|
|
548
|
-
extractedValue = parsedXml["AttachedDocument"]["cac:Attachment"]["cac:ExternalReference"]["cbc:Description"];
|
|
549
|
-
}
|
|
542
|
+
let invoiceXML = "";
|
|
550
543
|
|
|
551
|
-
|
|
544
|
+
// ✅ Caso 1: Viene como AttachedDocument
|
|
545
|
+
if (parsedXml?.AttachedDocument) {
|
|
546
|
+
const description =
|
|
547
|
+
parsedXml?.AttachedDocument?.["cac:Attachment"]?.["cac:ExternalReference"]?.["cbc:Description"];
|
|
552
548
|
|
|
553
|
-
|
|
549
|
+
if (description) {
|
|
550
|
+
invoiceXML = description
|
|
551
|
+
.replace("<![CDATA[", "")
|
|
552
|
+
.replace("]]>", "")
|
|
553
|
+
.trim();
|
|
554
|
+
}
|
|
555
|
+
}
|
|
554
556
|
|
|
557
|
+
// ✅ Caso 2: Viene directamente como Invoice
|
|
558
|
+
else if (parsedXml?.Invoice) {
|
|
559
|
+
invoiceXML = xmlContent; // ya es el invoice completo
|
|
560
|
+
}
|
|
555
561
|
|
|
556
|
-
|
|
562
|
+
// ✅ Si tenemos XML del invoice lo formateamos
|
|
563
|
+
if (invoiceXML) {
|
|
564
|
+
const formattedXML = xmlFormatter(invoiceXML, {
|
|
557
565
|
indentation: " ",
|
|
558
566
|
collapseContent: true,
|
|
559
567
|
lineSeparator: "\n",
|
|
560
568
|
});
|
|
561
569
|
|
|
562
|
-
|
|
563
570
|
const extraFilePath = path.join(documentXMLDir, `${baseName}.xml`);
|
|
564
571
|
fs.writeFileSync(extraFilePath, formattedXML, "utf-8");
|
|
565
|
-
|
|
572
|
+
|
|
573
|
+
console.log(`XML guardado: ${extraFilePath}`);
|
|
574
|
+
} else {
|
|
575
|
+
console.warn(`No se encontró Invoice ni AttachedDocument en: ${baseName}`);
|
|
566
576
|
}
|
|
577
|
+
|
|
567
578
|
} catch (error) {
|
|
568
|
-
console.error(`Error
|
|
579
|
+
console.error(`Error procesando XML: ${baseName}`, error.message);
|
|
569
580
|
}
|
|
570
581
|
}
|
|
571
582
|
|
|
583
|
+
|
|
572
584
|
// Descargar y guardar PDF
|
|
573
585
|
if (pdf) {
|
|
574
586
|
const pdfPath = path.join(pdfDir, `${baseName}.pdf`);
|
|
@@ -644,10 +656,15 @@ const sendTxtFlexo = async (options) => {
|
|
|
644
656
|
try {
|
|
645
657
|
const data =await request(urlApi, {txtEncode: encoded})
|
|
646
658
|
console.log(data)
|
|
647
|
-
//
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
659
|
+
// Validar que el documento fue autorizado (estado === 2)
|
|
660
|
+
if (data.estado === 2 ) {
|
|
661
|
+
// Mover archivo .txt a carpeta "procesados"
|
|
662
|
+
const destino = path.join(procesadosDir, archivo);
|
|
663
|
+
fs.renameSync(name, destino);
|
|
664
|
+
console.log(`Archivo movido a: ${destino}`);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
|
|
651
668
|
output.push(data)
|
|
652
669
|
} catch (error) {
|
|
653
670
|
console.error(`Error al procesar el archivo ${archivo}:`, error);
|