Advertisement
EulisAires

viaseg js 2

Mar 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function generate() {
  2.  
  3.     const httpGet = (url, callback) => {
  4.         const http = new XMLHttpRequest();
  5.         http.open('GET', url, true);
  6.  
  7.         http.onload = () => {
  8.             if (http.status >= 200 && http.status < 400) {
  9.                 // Success!
  10.                 callback(http.responseText);
  11.             }
  12.         };
  13.  
  14.         http.send();
  15.     };
  16.  
  17.  
  18.     httpGet('/get_vistorias', (res) => {
  19.  
  20.         let vistorias = JSON.parse(res);
  21.         console.log(vistorias)
  22.         document.querySelector('body').innerHTML += `<table id="vist-table"><tr>
  23.         <th>Número</th>
  24.         <th>Data de Vistoria</th>
  25.         <th>Grupo</th>
  26.         <th>Concessionária</th>
  27.         <th>Placa</th>
  28.         <th>Setor</th>
  29.         <th>Vendedor</th>
  30.         <th>Modo de Pagamento</th>
  31.         <th>Valor</th>
  32.       </tr></table>`;
  33.         vistorias.forEach((vist) => {
  34.  
  35.             document.querySelector('#vist-table').innerHTML += `<tr>
  36.             <td>${vist.pk}</td>
  37.             <td>${vist.fields.Data_de_Registro.split('-').reverse().join('/')}
  38.             </td>
  39.             <td>${vist.fields.Grupo_da_concessionaria[0].toUpperCase() + vist.fields.Grupo_da_concessionaria.slice(1)}</td>
  40.             <td>${vist.fields.Concessionaria[0].toUpperCase() + vist.fields.Concessionaria.slice(1)}</td>
  41.             <td>${vist.fields.Placa.toUpperCase()}</td>
  42.             <td>${vist.fields.Setor[0].toUpperCase() + vist.fields.Setor.slice(1)}</td>
  43.             <td>${vist.fields.Vendedor[0].toUpperCase() + vist.fields.Vendedor.slice(1)}</td>
  44.             <td>${vist.fields.Modos_de_pagamento[0].toUpperCase() + vist.fields.Modos_de_pagamento.slice(1)}</td>
  45.             <td>${vist.fields.Valor_pago}</td>
  46.         </tr>`;
  47.            
  48.         });
  49.  
  50.         var page = new URL(window.location.href).searchParams.get("page");
  51.  
  52.         var doc = new jsPDF('landscape', 'pt');
  53.  
  54.         var res = doc.autoTableHtmlToJson(document.getElementById("vist-table"));
  55.         doc.autoTable(res.columns, res.data, { margin: { top: 80 } });
  56.  
  57.         var header = function (data) {
  58.             doc.setFontSize(18);
  59.             doc.setTextColor(40);
  60.             doc.setFontStyle('normal');
  61.             //doc.addImage(headerImgData, 'JPEG', data.settings.margin.left, 20, 50, 50);
  62.             //   doc.text("Relatorio das Vistorias", data.settings.margin.left, 50);
  63.         };
  64.  
  65.         var options = {
  66.             beforePageContent: header,
  67.             margin: {
  68.                 top: 80
  69.             },
  70.             startY: doc.autoTableEndPosY() + 20
  71.         };
  72.  
  73.         const putZero = (num) => {
  74.             return num >= 10 ? num : `0${num}`
  75.         }
  76.  
  77.         // doc.autoTable(res.columns, res.data, options);
  78.         // console.log(date)
  79.         doc.text(`Relatório de Vistorias - ${putZero(new Date().getDate())}/${putZero(new Date().getMonth()+1)}/${putZero(new Date().getFullYear())}`, 50, 49)
  80.         doc.save(`Relatório-${putZero(new Date().getDate())}-${putZero(new Date().getMonth()+1)}-${putZero(new Date().getFullYear())}.pdf`);
  81.     });
  82.  
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement