119 lines
4.4 KiB
JavaScript
119 lines
4.4 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function() {
|
|
console.log('Carregando script vendas.js...');
|
|
|
|
// Funções de validação e formatação de datas
|
|
function validarData(data) {
|
|
if (!data) return false;
|
|
|
|
const dataObj = new Date(data);
|
|
if (isNaN(dataObj.getTime())) return false;
|
|
|
|
const hoje = new Date();
|
|
hoje.setHours(0, 0, 0, 0);
|
|
|
|
return dataObj <= hoje;
|
|
}
|
|
|
|
function formatarData(data) {
|
|
if (!data) return '';
|
|
|
|
const dataObj = new Date(data);
|
|
if (isNaN(dataObj.getTime())) return '';
|
|
|
|
return dataObj.toLocaleDateString('pt-BR');
|
|
}
|
|
|
|
// Configurar campos de data
|
|
const camposData = document.querySelectorAll('input[type="date"]');
|
|
camposData.forEach(campo => {
|
|
// Definir data máxima como hoje
|
|
const hoje = new Date().toISOString().split('T')[0];
|
|
campo.setAttribute('max', hoje);
|
|
|
|
campo.addEventListener('change', function() {
|
|
if (!validarData(this.value)) {
|
|
this.setCustomValidity('Data inválida ou futura');
|
|
this.classList.add('is-invalid');
|
|
} else {
|
|
this.setCustomValidity('');
|
|
this.classList.remove('is-invalid');
|
|
}
|
|
});
|
|
});
|
|
|
|
// Configurar tabela de vendas
|
|
const tabelaVendas = $('#vendasTable').DataTable({
|
|
language: {
|
|
url: '//cdn.datatables.net/plug-ins/1.13.7/i18n/pt-BR.json'
|
|
},
|
|
columnDefs: [
|
|
{
|
|
targets: 3, // Coluna de data
|
|
type: 'date-br',
|
|
render: function(data, type, row) {
|
|
if (type === 'sort') {
|
|
return data.split('/').reverse().join('');
|
|
}
|
|
return data;
|
|
}
|
|
},
|
|
{
|
|
targets: 2, // Coluna de valor
|
|
type: 'numeric',
|
|
render: function(data, type, row) {
|
|
if (type === 'sort') {
|
|
return parseFloat(data.replace('R$ ', '').replace(',', '.'));
|
|
}
|
|
return data;
|
|
}
|
|
},
|
|
{ targets: -1, orderable: false } // Coluna de ações
|
|
],
|
|
order: [[3, 'desc']] // Ordenar por data decrescente por padrão
|
|
});
|
|
|
|
// Atualizar valor total ao mudar quantidade ou material
|
|
const campoQuantidade = document.getElementById('quantidade');
|
|
const campoMaterial = document.getElementById('material_id');
|
|
const campoValorTotal = document.getElementById('valor_total');
|
|
|
|
function atualizarValorTotal() {
|
|
if (!campoQuantidade || !campoMaterial || !campoValorTotal) return;
|
|
|
|
const quantidade = parseInt(campoQuantidade.value) || 0;
|
|
const materialSelecionado = campoMaterial.options[campoMaterial.selectedIndex];
|
|
const preco = materialSelecionado ? parseFloat(materialSelecionado.dataset.preco) || 0 : 0;
|
|
|
|
campoValorTotal.value = (quantidade * preco).toFixed(2);
|
|
}
|
|
|
|
if (campoQuantidade) {
|
|
campoQuantidade.addEventListener('change', atualizarValorTotal);
|
|
}
|
|
if (campoMaterial) {
|
|
campoMaterial.addEventListener('change', atualizarValorTotal);
|
|
}
|
|
|
|
// Configurar modal de edição
|
|
const modalEditarVenda = document.getElementById('modalEditarVenda');
|
|
if (modalEditarVenda) {
|
|
modalEditarVenda.addEventListener('show.bs.modal', function(event) {
|
|
const button = event.relatedTarget;
|
|
if (!button) return;
|
|
|
|
const vendaId = button.getAttribute('data-venda-id');
|
|
const militanteId = button.getAttribute('data-militante-id');
|
|
const materialId = button.getAttribute('data-material-id');
|
|
const quantidade = button.getAttribute('data-quantidade');
|
|
const valorTotal = button.getAttribute('data-valor-total');
|
|
const dataVenda = button.getAttribute('data-data-venda');
|
|
|
|
document.getElementById('editVendaId').value = vendaId;
|
|
document.getElementById('editMilitanteId').value = militanteId;
|
|
document.getElementById('editMaterialId').value = materialId;
|
|
document.getElementById('editQuantidade').value = quantidade;
|
|
document.getElementById('editValorTotal').value = valorTotal;
|
|
document.getElementById('editDataVenda').value = dataVenda;
|
|
});
|
|
}
|
|
});
|