Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const path = require('path')
- const fs = require('fs')
- const axios = require('axios');
- const moment = require("moment")
- const accents = require('remove-accents');
- let config;
- try {
- config = JSON.parse(fs.readFileSync(path.join(path.resolve("../", "config/config.json"))))
- } catch(e) {
- console.error("ERRO AO LER O ARQUIVO DE CONFIGURACAO");
- process.exit()
- }
- if (!String.prototype.padEnd) {
- String.prototype.padEnd = function padEnd(targetLength,padString) {
- targetLength = targetLength>>0; //floor if number or convert non-number to 0;
- padString = String((typeof padString !== 'undefined' ? padString : ' '));
- if (this.length > targetLength) {
- return String(this);
- }
- else {
- targetLength = targetLength-this.length;
- if (targetLength > padString.length) {
- padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
- }
- return String(this) + padString.slice(0,targetLength);
- }
- };
- }
- if (!String.prototype.padStart) {
- String.prototype.padStart = function padStart(targetLength, padString) {
- targetLength = targetLength >> 0; //truncate if number, or convert non-number to 0;
- padString = String(typeof padString !== 'undefined' ? padString : ' ');
- if (this.length >= targetLength) {
- return String(this);
- } else {
- targetLength = targetLength - this.length;
- if (targetLength > padString.length) {
- padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
- }
- return padString.slice(0, targetLength) + String(this);
- }
- };
- }
- String.prototype.removeAccents = function() {
- return accents.remove(this);
- }
- const linhaTipo0 = function(data) {
- return `000${data.filial.cnpj.toString().padStart(14, '0')}`;
- }
- const linhaTipo1 = function(data) {
- let body = ['001']
- body.push(data.id.toString().padStart(8, '0'));
- body.push(moment(data.dataPedido, 'YYYY-MM-DD').format('DDMMYYYY'))
- body.push(moment(data.dataEntrega, 'YYYY-MM-DD').format('DDMMYYYY'))
- body.push(data.total.toFixed(4).replace(".", "").padStart(12, '0'))
- body.push((data.desconto || 0).toFixed(4).replace(".", "").padStart(12, '0'))
- body.push(data.pedidoPagamento.length.toString().padStart(2, '0'))
- body.push(data.pedidoProduto.length.toString().padEnd(3, '0'))
- body.push("CIF");
- body.push(data.frete.toFixed(4).replace(".", "").padStart(12, '0'))
- body.push("REGEX ECOMMERCE".padEnd(60, ' '))
- return body.join("");
- }
- const linhaTipo2 = function(data) {
- let body = ['002']
- body.push(data.nome.removeAccents().toUpperCase().padEnd(60))
- body.push(data.cpf.toString().padStart(14, '0'))
- body.push(data.email.removeAccents().toUpperCase().padEnd(50))
- body.push(data.telefone.replace(/[^0-9]/g, '').substr(0,2).padStart(2, "0"));
- body.push(data.telefone.replace(/[^0-9]/g, '').substr(2).padStart(9, "9").substr(0,9))
- body.push(" ".repeat(2))
- body.push(" ".repeat(60))
- body.push(" ".repeat(20))
- body.push(data.endereco.removeAccents().toUpperCase().padEnd(50, " ").substr(0,50))
- body.push(data.numero.toString().padStart(5, "0").substr(0,5))
- body.push((data.complemento || "").substr(0,10).removeAccents().toUpperCase().padEnd(10, " ").substr(0,10))
- body.push(data.bairro.removeAccents().toUpperCase().padEnd(25, " ").substr(0,25))
- body.push(data.cidade.removeAccents().toUpperCase().padEnd(30, " ").substr(0,30))
- body.push(data.uf.substr(0,2))
- body.push("BR".padEnd(4, " "));
- body.push(data.cep.replace('-', "").padEnd(8, '0').substr(0, 8))
- body.push(data.nome.removeAccents().toUpperCase().padEnd(40, " ").substr(0,40))
- body.push(data.endereco.removeAccents().toUpperCase().padEnd(50, " ").substr(0, 50))
- body.push(data.numero.toString().padStart(5, "0").substr(0,5))
- body.push((data.complemento || "").removeAccents().toUpperCase().padEnd(10, " ").substr(0,10))
- body.push(data.bairro.removeAccents().toUpperCase().padEnd(25, " ").substr(0,25))
- body.push(data.cidade.removeAccents().toUpperCase().padEnd(30, " ").substr(0,30))
- body.push(data.uf.substr(0,2))
- body.push("BR".padEnd(4, " "));
- body.push(data.cep.replace('-', "").substr(0,8))
- return body.join("");
- }
- const linhaTipo3 = function(data) {
- let body = ['003']
- body.push(data.produtoErp.toString().padStart(8, '0'))
- body.push(data.descricao.removeAccents().toUpperCase().padEnd(60).substr(0,60))
- body.push(data.quantidade.toFixed(4).replace(".", "").padStart(12, '0'))
- body.push((data.desconto || 0).toFixed(4).replace(".", "").padStart(12, '0'))
- body.push(data.valor.toFixed(4).replace(".", "").padStart(12, '0'))
- body.push(data.promocao ? "S" : "N")
- body.push((data.observacao || "").removeAccents().toUpperCase().padEnd(60, " ").substr(0,60));
- return body.join("");
- }
- const linhaTipo3Frete = function(data) {
- let body = ['003']
- body.push('0'.padStart(8, '0'))
- body.push('FRETE'.padEnd(60))
- body.push((1).toFixed(4).replace(".", "").padStart(12, '0'))
- body.push((0).toFixed(4).replace(".", "").padStart(12, '0'))
- body.push(data.frete.toFixed(4).replace(".", "").padStart(12, '0'))
- body.push("N")
- body.push("".padEnd(60, " "));
- return body.join("");
- }
- const linhaTipo4 = function(data) {
- let body = ['004']
- body.push(data.parcela.toString().padStart(3, '0'))
- body.push(data.documento.padEnd(25, ' ').substr(0,25))
- body.push(data.dataVencimento ? moment(data.dataVencimento, "YYYY-MM-DD").format("DDMMYYYY") : "00000000")
- body.push(moment(data.dataEmissao, "YYYY-MM-DD").format("DDMMYYYY"))
- body.push(data.valor.toFixed(4).replace(".", "").padStart(12, '0'))
- return body.join("");
- }
- const linhaTipo5 = function(data) {
- let body = ['005']
- body.push(''.padEnd(20, ' '))
- body.push(data.valor.toFixed(2).replace(".", "").padStart(11, '0'))
- body.push(''.padEnd(50, ' '))
- body.push(''.padEnd(6, ' '))
- body.push((data.nsu || '').padEnd(15, ' ').substr(0,15))
- body.push(data.bin.padStart(6, '0').substr(0,6));
- body.push((data.autorizacao || '').padEnd(15, ' ').substr(0,15))
- return body.join("");
- }
- const linhaTipo6 = function(data) {
- let body = ['006']
- body.push(data.retirada ? "R" : "E")
- body.push(moment(data.dataEntrega, 'YYYY-MM-DD').format('DDMMYYYY'))
- body.push(data.horaEntregaInicio.replace(/[^0-9]/g, '').padEnd(6, ' '))
- body.push(data.horaEntregaFim.replace(/[^0-9]/g, '').padEnd(6, ' '))
- body.push(data.statusPagamento ? "S" : "N")
- body.push((data.observacao || "").removeAccents().toUpperCase().padEnd(50, " "));
- return body.join("");
- }
- const run = function() {
- let headers = {
- "Authorization": config.token,
- "content-type": "application/json"
- };
- axios.post(`${config.url}/pedido/exportar`, {}, { headers })
- .then(e => {
- e.data.forEach(el => {
- let file = [];
- console.log(el.id);
- // console.log(el);
- file.push(linhaTipo0(el));
- file.push(linhaTipo1(el));
- file.push(linhaTipo2(el));
- file.push(linhaTipo3Frete(el));
- el.pedidoProduto.forEach(produto => {
- file.push(linhaTipo3(produto));
- })
- el.pedidoPagamento.forEach(pag => {
- if ( el.statusPagamento ) {
- file.push(linhaTipo4(pag));
- file.push(linhaTipo5(pag));
- }
- })
- file.push(linhaTipo6(el));
- fs.writeFileSync(path.join(config.pathGerarPedido, `${el.id.toString().padStart(9, "0")}.TXT`), file.join("\n"));
- })
- }).catch(err => {
- console.log(err);
- })
- }
- run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement