Last active
November 11, 2021 15:21
-
-
Save mauroquinteros/938947d2bea06ec43aa55e3d0def7265 to your computer and use it in GitHub Desktop.
Generar Comprobantes
This file contains 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
const axios = require("axios"); | |
const URL = "http://flaskapi-env.eba-wnb42kap.us-east-1.elasticbeanstalk.com"; | |
const HEADERS = { | |
Authorization: "Bearer c3f40934-a078-44a8-b6c7-f9a37fd721a7", | |
"Content-Type": "application/json", | |
}; | |
function sliceIntoChunks(arr, chunkSize) { | |
const chunkList = []; | |
for (let i = 0; i < arr.length; i += chunkSize) { | |
const chunk = arr.slice(i, i + chunkSize); | |
chunkList.push(chunk); | |
} | |
return chunkList; | |
} | |
async function createBill(serieId, charge) { | |
const body = { | |
serie_id: serieId, | |
contractor_document: "20546880169", | |
charge: { | |
amount: charge.amount, | |
plan_id: charge.plan_id, | |
charge_date: "2021-11-10", | |
}, | |
}; | |
console.log(`\nPlan ID: ${charge.plan_id} con el monto ${charge.amount}`); | |
const response = await axios.post( | |
`${URL}/api/accounting/bills/generate`, | |
body, | |
{ | |
headers: HEADERS, | |
} | |
); | |
const { data } = await response.data; | |
return data; | |
} | |
async function sendCharges(billId, chunkList) { | |
for (let i = 0; i < chunkList.length; i++) { | |
console.log( | |
`N° subcobros para el billId ${billId}: ${chunkList[i].length}` | |
); | |
const body = { | |
bill_id: billId, | |
charges: chunkList[i], | |
}; | |
const response = await axios.put( | |
`${URL}/api/accounting/charges/status`, | |
body, | |
{ | |
headers: HEADERS, | |
} | |
); | |
let { data } = await response.data; | |
console.log(`N° Subcobros actualizados ${data.updated_charges}`); | |
} | |
} | |
async function generateBills() { | |
const serieId = 46; | |
const body = { | |
serie_id: serieId, | |
fecha_inicial: "2021-10-01", | |
fecha_final: "2021-10-31", | |
}; | |
const response = await axios.post(`${URL}/api/accounting/charges`, body, { | |
headers: HEADERS, | |
}); | |
const { data } = await response.data; | |
console.log("N° facturas por generar: ", data.charges.length); | |
data.charges.forEach((charge, index) => | |
console.log( | |
`N° de subcobros por actualizar de la factura ${index + 1}: ${ | |
charge.sub_charges.length | |
}` | |
) | |
); | |
for (let i = 0; i < data.charges.length; i++) { | |
let { bill_id } = await createBill(serieId, data.charges[i]); | |
console.log("New Bill: ", bill_id); | |
const chunkList = sliceIntoChunks(data.charges[i].sub_charges, 100); | |
let responseCharges = await sendCharges(bill_id, chunkList); | |
} | |
} | |
generateBills(); |
This file contains 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
const axios = require("axios"); | |
URL = "DEV_URL"; | |
function sliceIntoChunks(arr, chunkSize) { | |
const chunkList = []; | |
for (let i = 0; i < arr.length; i += chunkSize) { | |
const chunk = arr.slice(i, i + chunkSize); | |
chunkList.push(chunk); | |
} | |
return chunkList; | |
} | |
function serializeVoucherList(chunkList) { | |
const globalVoucherList = []; | |
for (let i = 0; i < chunkList.length; i++) { | |
let voucherList = []; | |
voucherList = chunkList[i].map((chunk) => { | |
return { | |
id_cobro: chunk.id, | |
date: chunk.date.split("/").reverse().join("-"), | |
amount: chunk.amount, | |
plan_id: chunk.plan_id, | |
contractor_document: chunk.contractor_document, | |
}; | |
}); | |
globalVoucherList.push(voucherList); | |
} | |
return globalVoucherList; | |
} | |
async function sendCharges(serieId, chunkVoucherList) { | |
for (let i = 0; i < chunkVoucherList.length; i++) { | |
console.log(`N° cobros del chunk ${i + 1}: ${chunkVoucherList[i].length}`); | |
const body = { | |
serie_id: serieId, | |
charges: chunkVoucherList[i], | |
}; | |
const headers = { | |
Authorization: "Bearer TOKEN", | |
"Content-Type": "application/json", | |
}; | |
const response = await axios.post( | |
`${URL}/api/accounting/vouchers/generate`, | |
body, | |
{ | |
headers: headers, | |
} | |
); | |
const { data } = await response.data; | |
console.log(data); | |
} | |
} | |
async function generateBills() { | |
const body = { | |
serie_id: 45, | |
fecha_inicial: "2021-10-01", | |
fecha_final: "2021-10-30", | |
}; | |
const headers = { | |
Authorization: "Bearer TOKEN", | |
"Content-Type": "application/json", | |
}; | |
const response = await axios.post(`${URL}/api/accounting/charges`, body, { | |
headers: headers, | |
}); | |
const { data } = await response.data; | |
console.log("N° cobros originales: ", data.charges.length); | |
const chunkList = sliceIntoChunks(data.charges, 100); | |
const chunkVoucherList = serializeVoucherList(chunkList); | |
sendCharges(45, chunkVoucherList); | |
} | |
generateBills(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment