Files
controles/templates/nova_venda.html

121 lines
4.8 KiB
HTML

{% extends 'base.html' %}
{% block title %}Nova Venda{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="mb-4">Nova Venda</h1>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}">{{ message }}</div>
{% endfor %}
{% endif %}
{% endwith %}
<form method="POST" class="needs-validation" novalidate>
<div class="mb-3">
<label for="militante_id" class="form-label">Militante</label>
<select class="form-select" id="militante_id" name="militante_id" required>
<option value="">Selecione um militante</option>
{% for militante in militantes %}
<option value="{{ militante.id }}">{{ militante.nome }}</option>
{% endfor %}
</select>
<div class="invalid-feedback">
Por favor, selecione o militante.
</div>
</div>
<div class="mb-3">
<label for="material_id" class="form-label">Material</label>
<select class="form-select" id="material_id" name="material_id" required>
<option value="">Selecione um material</option>
{% for material in materiais %}
<option value="{{ material.id }}" data-preco="{{ material.preco }}">{{ material.nome }} - R$ {{ "%.2f"|format(material.preco) }}</option>
{% endfor %}
</select>
<div class="invalid-feedback">
Por favor, selecione o material.
</div>
</div>
<div class="mb-3">
<label for="quantidade" class="form-label">Quantidade</label>
<input type="number" class="form-control" id="quantidade" name="quantidade" min="1" required>
<div class="invalid-feedback">
Por favor, insira a quantidade.
</div>
</div>
<div class="mb-3">
<label for="valor_total" class="form-label">Valor Total</label>
<input type="number" class="form-control" id="valor_total" name="valor_total" step="0.01" readonly required>
</div>
<div class="mb-3">
<label for="data_venda" class="form-label">Data da Venda</label>
<input type="date" class="form-control" id="data_venda" name="data_venda" required>
<div class="invalid-feedback">
Por favor, insira a data da venda.
</div>
</div>
<div class="d-flex justify-content-between">
<button type="submit" class="btn btn-success">Registrar</button>
<a href="{{ url_for('listar_vendas') }}" class="btn btn-outline-secondary">Voltar</a>
</div>
</form>
</div>
</div>
</div>
<script>
// Validação do formulário
(function () {
'use strict'
var forms = document.querySelectorAll('.needs-validation')
Array.prototype.slice.call(forms)
.forEach(function (form) {
form.addEventListener('submit', function (event) {
if (!form.checkValidity()) {
event.preventDefault()
event.stopPropagation()
}
form.classList.add('was-validated')
}, false)
})
})()
// Cálculo do valor total
document.getElementById('material_id').addEventListener('change', function() {
calcularValorTotal();
});
document.getElementById('quantidade').addEventListener('input', function() {
calcularValorTotal();
});
function calcularValorTotal() {
const materialSelect = document.getElementById('material_id');
const quantidadeInput = document.getElementById('quantidade');
const valorTotalInput = document.getElementById('valor_total');
if (materialSelect.value && quantidadeInput.value) {
const preco = parseFloat(materialSelect.options[materialSelect.selectedIndex].dataset.preco);
const quantidade = parseFloat(quantidadeInput.value);
const valorTotal = preco * quantidade;
valorTotalInput.value = valorTotal.toFixed(2);
} else {
valorTotalInput.value = '';
}
}
</script>
{% endblock %}