Last active
July 21, 2022 00:00
-
-
Save alejoasotelo/99e0bdf16db64b783fc42d66321c2946 to your computer and use it in GitHub Desktop.
Script para obtener las facturas de AFIP por fechas dentro de Mis Comprobantes AFIP (https://serviciosjava2.afip.gob.ar/mcmp/jsp/comprobantesEmitidos.do)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@author Alejo Sotelo <alejosotelo.com.ar> | |
@date 2022-07-20 | |
*/ | |
async function getFacturas(from, to, tipoComprobante) { | |
var query = encodeURIComponent(from + '+-+' + to + '&tiposComprobantes[]=' + tipoComprobante) | |
var r = await fetch("https://serviciosjava2.afip.gob.ar/mcmp/jsp/ajax.do?f=generarConsulta&t=E&fechaEmision=" + query, { | |
"credentials": "include", | |
"headers": { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0", | |
"Accept": "application/json, text/javascript, */*; q=0.01", | |
"Accept-Language": "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3", | |
"X-Requested-With": "XMLHttpRequest", | |
"Sec-Fetch-Dest": "empty", | |
"Sec-Fetch-Mode": "cors", | |
"Sec-Fetch-Site": "same-origin", | |
"Pragma": "no-cache", | |
"Cache-Control": "no-cache" | |
}, | |
"referrer": "https://serviciosjava2.afip.gob.ar/mcmp/jsp/comprobantesEmitidos.do", | |
"method": "GET", | |
"mode": "cors" | |
}); | |
var response = await r.json(); | |
console.log(response); | |
if (response.estado != 'ok') { | |
console.log(response.mensajeError); | |
return false; | |
} | |
r = await fetch("https://serviciosjava2.afip.gob.ar/mcmp/jsp/ajax.do?f=listaResultados&id=" + response.datos.idConsulta + "&_=1658359327266", { | |
"credentials": "include", | |
"headers": { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0", | |
"Accept": "application/json, text/javascript, */*; q=0.01", | |
"Accept-Language": "es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3", | |
"X-Requested-With": "XMLHttpRequest", | |
"Sec-Fetch-Dest": "empty", | |
"Sec-Fetch-Mode": "cors", | |
"Sec-Fetch-Site": "same-origin", | |
"Pragma": "no-cache", | |
"Cache-Control": "no-cache" | |
}, | |
"referrer": "https://serviciosjava2.afip.gob.ar/mcmp/jsp/comprobantesEmitidos.do", | |
"method": "GET", | |
"mode": "cors" | |
}); | |
response = await r.json(); | |
console.log(response); | |
if (response.estado != 'ok') { | |
console.log(response.mensajeError); | |
return false; | |
} | |
return response.datos.data.map(factura => { | |
return { | |
fecha: factura[0], | |
tipoComprobante: factura[1], | |
puntoDeVenta: factura[3], | |
facturaNumero: factura[4], | |
receptor: factura[11], | |
receptorNombre: factura[12], | |
importeTotal: factura[23] | |
} | |
}); | |
} | |
function getFirstDayOfMonth(year, month) { | |
return new Date(year, month, 1); | |
} | |
function getLastDayOfMonth(year, month) { | |
return new Date(year, month + 1, 0) | |
} | |
function getFirstAndLastDayOfMonth(year, month) { | |
return { | |
first: getFirstDayOfMonth(year, month), | |
last: getLastDayOfMonth(year, month) | |
} | |
} | |
function padTo2Digits(num) { | |
return num.toString().padStart(2, '0'); | |
} | |
function formatDate(date) { | |
return [ | |
padTo2Digits(date.getDate()), | |
padTo2Digits(date.getMonth() + 1), | |
date.getFullYear(), | |
].join('/'); | |
} | |
async function getFacturasByYear(year, tipoComprobante) { | |
var facturas = []; | |
for (var i = 0; i <= 11; i++) { | |
var month = getFirstAndLastDayOfMonth(year, i); | |
var firstDay = formatDate(month.first); | |
var lastDay = formatDate(month.last); | |
var facturasByMonth = await getFacturas(firstDay, lastDay, tipoComprobante); | |
facturas = [...facturas, ...facturasByMonth]; | |
} | |
return facturas; | |
} | |
var pesoARS = Intl.NumberFormat("es-AR", { | |
style: "currency", | |
currency: "ARS", | |
}); | |
var facturas = await getFacturasByYear(2022, 11); | |
console.log('Mis facturas', facturas); | |
var totalFacturado = facturas.reduce((total, item) => total + parseFloat(item.importeTotal), 0).toFixed(2); | |
console.log('El total facturado del año es de: %c' + pesoARS.format(totalFacturado), 'font-weight: bold;') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment