novo pilar tipo: termo (valor 5 no enum), lançado como uma linha por lote com manual: true e uma coluna nova quantidade. O valor_aplicado guarda o total do lote (qtd × preço), então recalcular_motorista! e todas as somas existentes seguem corretas sem mudança — linhas antigas ficam com quantidade = 1.
This commit is contained in:
78
app/javascript/controllers/apontamento_termo_controller.js
Normal file
78
app/javascript/controllers/apontamento_termo_controller.js
Normal file
@@ -0,0 +1,78 @@
|
||||
// app/javascript/controllers/apontamento_termo_controller.js
|
||||
// Stimulus — Entrega de termo. Lança um lote de N termos para o motorista da tela.
|
||||
// Entrega sem NF nem código de rastreio: só a quantidade (stepper − N +), a um
|
||||
// preço fixo configurável.
|
||||
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ["modal", "quantidade", "erro"]
|
||||
static values = { apontarTermoUrl: String, motorista: String }
|
||||
|
||||
abrir() {
|
||||
this.erroTarget.classList.add("hidden")
|
||||
this.quantidadeTarget.value = "1"
|
||||
this.modalTarget.classList.remove("hidden")
|
||||
}
|
||||
|
||||
fechar() {
|
||||
this.modalTarget.classList.add("hidden")
|
||||
}
|
||||
|
||||
// Fecha ao clicar no fundo (fora do card)
|
||||
fundo(event) {
|
||||
if (event.target === this.modalTarget) this.fechar()
|
||||
}
|
||||
|
||||
// Stepper
|
||||
mais() {
|
||||
this.quantidadeTarget.value = String(this.qtdAtual() + 1)
|
||||
}
|
||||
|
||||
menos() {
|
||||
this.quantidadeTarget.value = String(Math.max(1, this.qtdAtual() - 1))
|
||||
}
|
||||
|
||||
qtdAtual() {
|
||||
const n = parseInt(this.quantidadeTarget.value, 10)
|
||||
return Number.isFinite(n) && n > 0 ? n : 1
|
||||
}
|
||||
|
||||
async adicionar(event) {
|
||||
event?.preventDefault()
|
||||
|
||||
const quantidade = this.qtdAtual()
|
||||
const motorista = this.motoristaValue.trim()
|
||||
if (!motorista) { this.mostrarErro("Motorista não identificado."); return }
|
||||
|
||||
const btn = event.currentTarget
|
||||
btn.disabled = true
|
||||
try {
|
||||
const resp = await fetch(this.apontarTermoUrlValue, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').content
|
||||
},
|
||||
body: JSON.stringify({ motorista: motorista, quantidade: quantidade })
|
||||
})
|
||||
const data = await resp.json()
|
||||
if (!resp.ok || data.ok === false) {
|
||||
this.mostrarErro(data.erro || "Não foi possível adicionar as entregas de termo.")
|
||||
return
|
||||
}
|
||||
// Recarrega para refletir o valor atualizado do motorista.
|
||||
window.location.reload()
|
||||
} catch (e) {
|
||||
this.mostrarErro("Erro ao adicionar as entregas de termo. Tente novamente.")
|
||||
} finally {
|
||||
btn.disabled = false
|
||||
}
|
||||
}
|
||||
|
||||
mostrarErro(msg) {
|
||||
this.erroTarget.textContent = msg
|
||||
this.erroTarget.classList.remove("hidden")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user