Skip to content

Instantly share code, notes, and snippets.

@fabiofl
Last active June 25, 2018 06:42
Show Gist options
  • Save fabiofl/5837707a076aed2f99cbc4dbf9ac570a to your computer and use it in GitHub Desktop.
Save fabiofl/5837707a076aed2f99cbc4dbf9ac570a to your computer and use it in GitHub Desktop.
// Clean page and setup
const cupom = $('.CupomFiscal')
$('body').html(cupom)
$('[style]').removeAttr('style')
cupom.css({'margin':0,'width':'auto'})
const tableTemplate = `
<table id="result">
<thead>
<tr>
<td>Item</td>
<td>Código</td>
<td>Descrição</td>
<td>Qtd.</td>
<td>$ / Unidade</td>
<td>$ / Item</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
`;
const result = $(tableTemplate).appendTo('body')
// Clean Header and Footer
cupom.find('#tabelaItens, div.CFTamanhoFixo:first-child, #dadosCorpoCF > table:nth-child(-n+4), #dadosRodapeCF').remove()
// Get footer content and remove it from DOM
const footer = $('#painelItens table.CFTamanhoFixo:last-child')
const footerContent = $(footer.find('tbody').html().replace(/\,+/g,'.').replace(/ *R\$ */,''))
footer.remove();
// Format items and add to Result table
function formatItem(item) {
let number = item.find("tr:first-child td:nth-child(1)").text()
let code = item.find("tr:first-child td:nth-child(2)").text()
let description = item.find("tr:first-child td:nth-child(3)").text()
let quantity = item.find("tr:last-child td:nth-child(2)").text().replace(' x','').replace(',','.')
let unitPrice = item.find("tr:last-child td:nth-child(3)").text().replace(',','.')
let itemPrice = item.find("tr:last-child td:nth-child(5)").text().replace(',','.')
const result = `
<tr>
<td>${number}</td>
<td>${code}</td>
<td>${description}</td>
<td>${quantity}</td>
<td>${unitPrice}</td>
<td>${itemPrice}</td>
</tr>
`;
return result;
}
$('.CFTamanhoFixo').each((index,value)=>{
let item = $(value);
result.find('tbody').append(formatItem(item))
item.remove()
})
// footerContent.appendTo(result)
cupom.remove()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment