Advertisement
marcopolorez

importa-pedido.js

Jun 3rd, 2020
1,457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const path = require('path')
  2. const fs = require('fs')
  3. const axios = require('axios');
  4. const moment = require("moment")
  5. const accents = require('remove-accents');
  6.  
  7. let config;
  8.  
  9. try {
  10.     config = JSON.parse(fs.readFileSync(path.join(path.resolve("../", "config/config.json"))))
  11. } catch(e) {
  12.     console.error("ERRO AO LER O ARQUIVO DE CONFIGURACAO");
  13.     process.exit()
  14. }
  15.  
  16. if (!String.prototype.padEnd) {
  17.     String.prototype.padEnd = function padEnd(targetLength,padString) {
  18.         targetLength = targetLength>>0; //floor if number or convert non-number to 0;
  19.         padString = String((typeof padString !== 'undefined' ? padString : ' '));
  20.         if (this.length > targetLength) {
  21.             return String(this);
  22.         }
  23.         else {
  24.             targetLength = targetLength-this.length;
  25.             if (targetLength > padString.length) {
  26.                 padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
  27.             }
  28.             return String(this) + padString.slice(0,targetLength);
  29.         }
  30.     };
  31. }
  32.  
  33. if (!String.prototype.padStart) {
  34.     String.prototype.padStart = function padStart(targetLength, padString) {
  35.         targetLength = targetLength >> 0; //truncate if number, or convert non-number to 0;
  36.         padString = String(typeof padString !== 'undefined' ? padString : ' ');
  37.         if (this.length >= targetLength) {
  38.             return String(this);
  39.         } else {
  40.             targetLength = targetLength - this.length;
  41.             if (targetLength > padString.length) {
  42.                 padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
  43.             }
  44.             return padString.slice(0, targetLength) + String(this);
  45.         }
  46.     };
  47. }
  48.  
  49. String.prototype.removeAccents = function() {
  50.     return accents.remove(this);
  51. }
  52.  
  53. const linhaTipo0 = function(data) {
  54.     return `000${data.filial.cnpj.toString().padStart(14, '0')}`;
  55. }
  56.  
  57. const linhaTipo1 = function(data) {
  58.  
  59.     let body = ['001']
  60.     body.push(data.id.toString().padStart(8, '0'));
  61.     body.push(moment(data.dataPedido, 'YYYY-MM-DD').format('DDMMYYYY'))
  62.     body.push(moment(data.dataEntrega, 'YYYY-MM-DD').format('DDMMYYYY'))
  63.     body.push(data.total.toFixed(4).replace(".", "").padStart(12, '0'))
  64.     body.push((data.desconto || 0).toFixed(4).replace(".", "").padStart(12, '0'))
  65.     body.push(data.pedidoPagamento.length.toString().padStart(2, '0'))
  66.     body.push(data.pedidoProduto.length.toString().padEnd(3, '0'))
  67.     body.push("CIF");
  68.     body.push(data.frete.toFixed(4).replace(".", "").padStart(12, '0'))
  69.     body.push("REGEX ECOMMERCE".padEnd(60, ' '))
  70.  
  71.     return body.join("");
  72.  
  73. }
  74.  
  75. const linhaTipo2 = function(data) {
  76.  
  77.     let body = ['002']
  78.     body.push(data.nome.removeAccents().toUpperCase().padEnd(60))
  79.     body.push(data.cpf.toString().padStart(14, '0'))
  80.     body.push(data.email.removeAccents().toUpperCase().padEnd(50))
  81.     body.push(data.telefone.replace(/[^0-9]/g, '').substr(0,2).padStart(2, "0"));
  82.     body.push(data.telefone.replace(/[^0-9]/g, '').substr(2).padStart(9, "9").substr(0,9))
  83.     body.push(" ".repeat(2))
  84.     body.push(" ".repeat(60))
  85.     body.push(" ".repeat(20))
  86.     body.push(data.endereco.removeAccents().toUpperCase().padEnd(50, " ").substr(0,50))
  87.     body.push(data.numero.toString().padStart(5, "0").substr(0,5))
  88.     body.push((data.complemento || "").substr(0,10).removeAccents().toUpperCase().padEnd(10, " ").substr(0,10))
  89.     body.push(data.bairro.removeAccents().toUpperCase().padEnd(25, " ").substr(0,25))
  90.     body.push(data.cidade.removeAccents().toUpperCase().padEnd(30, " ").substr(0,30))
  91.     body.push(data.uf.substr(0,2))
  92.     body.push("BR".padEnd(4, " "));
  93.     body.push(data.cep.replace('-', "").padEnd(8, '0').substr(0, 8))
  94.     body.push(data.nome.removeAccents().toUpperCase().padEnd(40, " ").substr(0,40))
  95.     body.push(data.endereco.removeAccents().toUpperCase().padEnd(50, " ").substr(0, 50))
  96.     body.push(data.numero.toString().padStart(5, "0").substr(0,5))
  97.     body.push((data.complemento || "").removeAccents().toUpperCase().padEnd(10, " ").substr(0,10))
  98.     body.push(data.bairro.removeAccents().toUpperCase().padEnd(25, " ").substr(0,25))
  99.     body.push(data.cidade.removeAccents().toUpperCase().padEnd(30, " ").substr(0,30))
  100.     body.push(data.uf.substr(0,2))
  101.     body.push("BR".padEnd(4, " "));
  102.     body.push(data.cep.replace('-', "").substr(0,8))
  103.    
  104.     return body.join("");
  105.  
  106. }
  107.  
  108. const linhaTipo3 = function(data) {
  109.  
  110.     let body = ['003']
  111.     body.push(data.produtoErp.toString().padStart(8, '0'))
  112.     body.push(data.descricao.removeAccents().toUpperCase().padEnd(60).substr(0,60))
  113.     body.push(data.quantidade.toFixed(4).replace(".", "").padStart(12, '0'))
  114.     body.push((data.desconto || 0).toFixed(4).replace(".", "").padStart(12, '0'))
  115.     body.push(data.valor.toFixed(4).replace(".", "").padStart(12, '0'))
  116.     body.push(data.promocao ? "S" : "N")
  117.     body.push((data.observacao || "").removeAccents().toUpperCase().padEnd(60, " ").substr(0,60));
  118.  
  119.     return body.join("");
  120.    
  121. }
  122.  
  123. const linhaTipo3Frete = function(data) {
  124.  
  125.     let body = ['003']
  126.     body.push('0'.padStart(8, '0'))
  127.     body.push('FRETE'.padEnd(60))
  128.     body.push((1).toFixed(4).replace(".", "").padStart(12, '0'))
  129.     body.push((0).toFixed(4).replace(".", "").padStart(12, '0'))
  130.     body.push(data.frete.toFixed(4).replace(".", "").padStart(12, '0'))
  131.     body.push("N")
  132.     body.push("".padEnd(60, " "));
  133.  
  134.     return body.join("");
  135.    
  136. }
  137.  
  138. const linhaTipo4 = function(data) {
  139.  
  140.     let body = ['004']
  141.     body.push(data.parcela.toString().padStart(3, '0'))
  142.     body.push(data.documento.padEnd(25, ' ').substr(0,25))
  143.     body.push(data.dataVencimento ? moment(data.dataVencimento, "YYYY-MM-DD").format("DDMMYYYY") : "00000000")
  144.     body.push(moment(data.dataEmissao, "YYYY-MM-DD").format("DDMMYYYY"))
  145.     body.push(data.valor.toFixed(4).replace(".", "").padStart(12, '0'))
  146.  
  147.     return body.join("");
  148.    
  149. }
  150.  
  151. const linhaTipo5 = function(data) {
  152.  
  153.     let body = ['005']
  154.     body.push(''.padEnd(20, ' '))
  155.     body.push(data.valor.toFixed(2).replace(".", "").padStart(11, '0'))
  156.     body.push(''.padEnd(50, ' '))
  157.     body.push(''.padEnd(6, ' '))
  158.     body.push((data.nsu || '').padEnd(15, ' ').substr(0,15))
  159.     body.push(data.bin.padStart(6, '0').substr(0,6));
  160.     body.push((data.autorizacao || '').padEnd(15, ' ').substr(0,15))
  161.  
  162.     return body.join("");
  163.  
  164. }
  165.  
  166. const linhaTipo6 = function(data) {
  167.  
  168.     let body = ['006']
  169.     body.push(data.retirada ? "R" : "E")
  170.     body.push(moment(data.dataEntrega, 'YYYY-MM-DD').format('DDMMYYYY'))
  171.     body.push(data.horaEntregaInicio.replace(/[^0-9]/g, '').padEnd(6, ' '))
  172.     body.push(data.horaEntregaFim.replace(/[^0-9]/g, '').padEnd(6, ' '))
  173.     body.push(data.statusPagamento ? "S" : "N")
  174.     body.push((data.observacao || "").removeAccents().toUpperCase().padEnd(50, " "));
  175.  
  176.     return body.join("");
  177.  
  178. }
  179.  
  180. const run = function() {
  181.  
  182.     let headers = {
  183.         "Authorization": config.token,
  184.         "content-type": "application/json"
  185.     };
  186.    
  187.     axios.post(`${config.url}/pedido/exportar`, {}, { headers })
  188.     .then(e => {
  189.  
  190.         e.data.forEach(el => {
  191.  
  192.             let file = [];
  193.             console.log(el.id);
  194.  
  195. //            console.log(el);
  196.             file.push(linhaTipo0(el));
  197.             file.push(linhaTipo1(el));
  198.             file.push(linhaTipo2(el));
  199.  
  200.             file.push(linhaTipo3Frete(el));
  201.  
  202.             el.pedidoProduto.forEach(produto => {
  203.                 file.push(linhaTipo3(produto));
  204.             })
  205.  
  206.             el.pedidoPagamento.forEach(pag => {
  207.  
  208.                 if ( el.statusPagamento ) {
  209.                     file.push(linhaTipo4(pag));
  210.                     file.push(linhaTipo5(pag));
  211.                 }
  212.  
  213.             })
  214.  
  215.             file.push(linhaTipo6(el));
  216.  
  217.             fs.writeFileSync(path.join(config.pathGerarPedido, `${el.id.toString().padStart(9, "0")}.TXT`), file.join("\n"));
  218.  
  219.         })
  220.        
  221.     }).catch(err => {
  222.         console.log(err);
  223.     })  
  224.    
  225. }
  226.  
  227. run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement