Implantação da função d adicionar entregas por fora da operação
This commit is contained in:
@@ -164,6 +164,91 @@ class ConsolidacaoEntregasController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
# GET /consolidacoes/:consolidacao_id/consolidacao_entregas/buscar_nf?nf=123
|
||||
# APONTAMENTO MANUAL — busca a NF na SimpleRoute (sem filtro de período).
|
||||
def buscar_nf
|
||||
authorize @consolidacao, :update?
|
||||
|
||||
nf = params[:nf].to_s.strip
|
||||
return render(json: { ok: false, erro: 'Informe o número da nota.' }, status: :unprocessable_entity) if nf.blank?
|
||||
|
||||
entregas = Entrega.da_conta_gade.por_nf(nf).order(:checkout)
|
||||
|
||||
if entregas.empty?
|
||||
render json: { ok: false, erro: "Nota #{nf} não encontrada na base de rastreio." }
|
||||
else
|
||||
render json: { ok: true, nf: nf, entregas: entregas.map { |e| entrega_json(e) } }
|
||||
end
|
||||
end
|
||||
|
||||
# POST /consolidacoes/:consolidacao_id/consolidacao_entregas/apontar
|
||||
# Cria um apontamento manual a partir de um tracking_id encontrado pela NF.
|
||||
def apontar
|
||||
authorize @consolidacao, :update?
|
||||
|
||||
tipo = params[:tipo].to_s
|
||||
return render_apontamento_erro('Tipo inválido.') unless ConsolidacaoEntrega.tipos.key?(tipo)
|
||||
|
||||
entrega = Entrega.da_conta_gade.find_by(tracking_id: params[:tracking_id])
|
||||
return render_apontamento_erro('Entrega não encontrada na base de rastreio.') unless entrega
|
||||
|
||||
motorista = entrega.driver.to_s.strip
|
||||
return render_apontamento_erro('Entrega sem motorista — não é possível apontar.') if motorista.blank?
|
||||
|
||||
# Garante o motorista na consolidação (pode não estar, já que a NF veio por fora).
|
||||
@consolidacao.consolidacao_motoristas.find_or_create_by!(motorista_nome: motorista)
|
||||
|
||||
ce = @consolidacao.consolidacao_entregas
|
||||
.find_or_initialize_by(tracking_id: entrega.tracking_id, tipo: tipo)
|
||||
if ce.persisted?
|
||||
return render_apontamento_erro("Essa nota já está classificada como #{ConsolidacaoEntrega::TIPO_CORES[tipo][:label]} para #{motorista}.")
|
||||
end
|
||||
|
||||
ce.assign_attributes(
|
||||
motorista_nome: motorista,
|
||||
valor_aplicado: valor_para_tipo(tipo),
|
||||
manual: true,
|
||||
created_by: current_user.id
|
||||
)
|
||||
ce.save!
|
||||
|
||||
recalcular_motorista(motorista)
|
||||
auditar!(:editar, @consolidacao,
|
||||
dados_novos: { apontamento_manual: entrega.numero_nf, tipo: tipo, motorista: motorista })
|
||||
|
||||
respond_to do |format|
|
||||
format.json { render json: resumo_json(motorista).merge(motorista: motorista) }
|
||||
format.html do
|
||||
redirect_to validar_consolidacao_consolidacao_entregas_path(@consolidacao, motorista: motorista),
|
||||
notice: "Apontamento da nota #{entrega.numero_nf} adicionado para #{motorista}."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /consolidacoes/:consolidacao_id/consolidacao_entregas/remover_apontamento?id=1
|
||||
# Remove um apontamento manual. (Não passa pelo guard de elegibilidade, pois o
|
||||
# tracking está fora do período — por isso não reusa o toggle classificar.)
|
||||
def remover_apontamento
|
||||
authorize @consolidacao, :update?
|
||||
|
||||
ce = @consolidacao.consolidacao_entregas.apontamentos.find_by(id: params[:id])
|
||||
return render_apontamento_erro('Apontamento não encontrado.') unless ce
|
||||
|
||||
motorista = ce.motorista_nome
|
||||
ce.destroy
|
||||
recalcular_motorista(motorista)
|
||||
auditar!(:editar, @consolidacao,
|
||||
dados_novos: { remover_apontamento_manual: ce.tracking_id, motorista: motorista })
|
||||
|
||||
respond_to do |format|
|
||||
format.json { render json: resumo_json(motorista).merge(motorista: motorista) }
|
||||
format.html do
|
||||
redirect_to revisar_consolidacao_consolidacao_entregas_path(@consolidacao, motorista: motorista),
|
||||
notice: 'Apontamento removido.'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_consolidacao
|
||||
@@ -200,6 +285,27 @@ class ConsolidacaoEntregasController < ApplicationController
|
||||
tracking_ids_elegiveis(motorista).include?(tracking_id)
|
||||
end
|
||||
|
||||
# Dados da entrega para o modal de apontamento (conferência antes de salvar).
|
||||
def entrega_json(e)
|
||||
{
|
||||
tracking_id: e.tracking_id,
|
||||
nf: e.numero_nf,
|
||||
motorista: e.driver,
|
||||
veiculo: e.vehicle,
|
||||
local: e.local,
|
||||
status: e.status,
|
||||
pago: e.elegivel_pagamento?,
|
||||
data: (e.checkout || e.planned_date)&.to_s
|
||||
}
|
||||
end
|
||||
|
||||
def render_apontamento_erro(msg)
|
||||
respond_to do |format|
|
||||
format.json { render json: { ok: false, erro: msg }, status: :unprocessable_entity }
|
||||
format.html { redirect_to wizard_consolidacao_path(@consolidacao), alert: msg }
|
||||
end
|
||||
end
|
||||
|
||||
def render_nao_elegivel
|
||||
respond_to do |format|
|
||||
format.json { render json: { ok: false, erro: 'Entrega não elegível para este motorista.' }, status: :unprocessable_entity }
|
||||
|
||||
@@ -114,11 +114,18 @@ class ConsolidacoesController < ApplicationController
|
||||
@motoristas = @consolidacao.consolidacao_motoristas.order(:motorista_nome).map do |cm|
|
||||
total = @consolidacao.entregas_elegiveis_count(cm.motorista_nome)
|
||||
classificadas = @consolidacao.classificadas_count(cm.motorista_nome)
|
||||
apontamentos = @consolidacao.consolidacao_entregas
|
||||
.apontamentos
|
||||
.where(motorista_nome: cm.motorista_nome)
|
||||
.distinct.count(:tracking_id)
|
||||
{
|
||||
registro: cm,
|
||||
total: total,
|
||||
classificadas: classificadas,
|
||||
completo: total.positive? && classificadas >= total
|
||||
apontamentos: apontamentos,
|
||||
# Espelha o gate do servidor (todas_entregas_classificadas?): um motorista
|
||||
# só com apontamento manual tem 0 entregas elegíveis e já está "completo".
|
||||
completo: total.zero? || classificadas >= total
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
118
app/javascript/controllers/apontamento_controller.js
Normal file
118
app/javascript/controllers/apontamento_controller.js
Normal file
@@ -0,0 +1,118 @@
|
||||
// app/javascript/controllers/apontamento_controller.js
|
||||
// Stimulus controller — Apontamento manual de entrega na consolidação.
|
||||
// Busca a NF na base de rastreio (SimpleRoute), exibe os dados para conferência
|
||||
// e cria o apontamento com o tipo (pilar) escolhido.
|
||||
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ["modal", "nf", "erro", "resultados", "confirmar", "tipo"]
|
||||
static values = { buscarUrl: String, apontarUrl: String }
|
||||
|
||||
// ── Modal ──────────────────────────────────────────────────
|
||||
abrir() {
|
||||
this.reset()
|
||||
this.modalTarget.classList.remove("hidden")
|
||||
this.nfTarget.focus()
|
||||
}
|
||||
|
||||
fechar() {
|
||||
this.modalTarget.classList.add("hidden")
|
||||
}
|
||||
|
||||
// Fecha ao clicar no fundo (fora do card)
|
||||
fundo(event) {
|
||||
if (event.target === this.modalTarget) this.fechar()
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.nfTarget.value = ""
|
||||
this.erroTarget.classList.add("hidden")
|
||||
this.resultadosTarget.innerHTML = ""
|
||||
this.confirmarTarget.classList.add("hidden")
|
||||
}
|
||||
|
||||
// ── Busca da NF ────────────────────────────────────────────
|
||||
async buscar(event) {
|
||||
event?.preventDefault()
|
||||
const nf = this.nfTarget.value.trim()
|
||||
if (!nf) { this.mostrarErro("Informe o número da nota."); return }
|
||||
|
||||
this.erroTarget.classList.add("hidden")
|
||||
this.resultadosTarget.innerHTML = `<p class="text-gray-400 text-sm">Buscando…</p>`
|
||||
this.confirmarTarget.classList.add("hidden")
|
||||
|
||||
try {
|
||||
const url = `${this.buscarUrlValue}?nf=${encodeURIComponent(nf)}`
|
||||
const resp = await fetch(url, { headers: { "Accept": "application/json" } })
|
||||
const data = await resp.json()
|
||||
|
||||
if (!data.ok) {
|
||||
this.resultadosTarget.innerHTML = ""
|
||||
this.mostrarErro(data.erro || "Nota não encontrada.")
|
||||
return
|
||||
}
|
||||
this.renderResultados(data.entregas)
|
||||
} catch (e) {
|
||||
this.resultadosTarget.innerHTML = ""
|
||||
this.mostrarErro("Erro ao buscar a nota. Tente novamente.")
|
||||
}
|
||||
}
|
||||
|
||||
renderResultados(entregas) {
|
||||
const cards = entregas.map((e, i) => {
|
||||
const pago = e.pago
|
||||
? `<span class="text-green-400">paga</span>`
|
||||
: `<span class="text-yellow-400">${e.status || "pendente"}</span>`
|
||||
return `
|
||||
<label class="flex gap-3 items-start bg-[#0a0a0a] border border-[#2a2a2a] rounded-lg p-3 cursor-pointer hover:border-orange-500">
|
||||
<input type="radio" name="apontamento_entrega" value="${e.tracking_id}" ${i === 0 ? "checked" : ""} class="mt-1 accent-orange-500">
|
||||
<div class="text-sm">
|
||||
<p class="text-white font-bold">NF ${e.nf || "—"} · ${e.motorista || "sem motorista"}</p>
|
||||
<p class="text-gray-400">${e.veiculo || "—"} · ${e.local || "—"}</p>
|
||||
<p class="text-gray-500 text-xs">${e.data || "—"} · ${pago}</p>
|
||||
</div>
|
||||
</label>`
|
||||
}).join("")
|
||||
|
||||
this.resultadosTarget.innerHTML = cards
|
||||
this.confirmarTarget.classList.remove("hidden")
|
||||
}
|
||||
|
||||
// ── Criar apontamento ──────────────────────────────────────
|
||||
async apontar(event) {
|
||||
event?.preventDefault()
|
||||
const selecionado = this.resultadosTarget.querySelector('input[name="apontamento_entrega"]:checked')
|
||||
if (!selecionado) { this.mostrarErro("Selecione uma entrega."); return }
|
||||
|
||||
const btn = event.currentTarget
|
||||
btn.disabled = true
|
||||
try {
|
||||
const resp = await fetch(this.apontarUrlValue, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').content
|
||||
},
|
||||
body: JSON.stringify({ tracking_id: selecionado.value, tipo: this.tipoTarget.value })
|
||||
})
|
||||
const data = await resp.json()
|
||||
if (!resp.ok || data.ok === false) {
|
||||
this.mostrarErro(data.erro || "Não foi possível adicionar o apontamento.")
|
||||
return
|
||||
}
|
||||
// Recarrega para refletir motorista/valores atualizados.
|
||||
window.location.reload()
|
||||
} catch (e) {
|
||||
this.mostrarErro("Erro ao adicionar o apontamento. Tente novamente.")
|
||||
} finally {
|
||||
btn.disabled = false
|
||||
}
|
||||
}
|
||||
|
||||
mostrarErro(msg) {
|
||||
this.erroTarget.textContent = msg
|
||||
this.erroTarget.classList.remove("hidden")
|
||||
}
|
||||
}
|
||||
@@ -152,8 +152,12 @@ class Consolidacao < ApplicationRecord
|
||||
|
||||
# Entrega conta como classificada se tiver ≥1 pilar (distinct por tracking_id,
|
||||
# já que agora pode haver vários pilares por entrega).
|
||||
# Apontamentos manuais (manual: true) NÃO entram aqui: eles vivem fora do
|
||||
# conjunto elegível do período, então contá-los inflaria o progresso para
|
||||
# além de 100% (e quebraria o gate classificadas >= elegíveis).
|
||||
def classificadas_count(motorista)
|
||||
consolidacao_entregas.where(motorista_nome: motorista).distinct.count(:tracking_id)
|
||||
consolidacao_entregas.where(motorista_nome: motorista, manual: false)
|
||||
.distinct.count(:tracking_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
@@ -5,6 +5,10 @@ class ConsolidacaoEntrega < ApplicationRecord
|
||||
|
||||
belongs_to :consolidacao
|
||||
|
||||
# Apontamentos manuais — entregas incluídas por fora do período/operação
|
||||
# (ex.: nota que chegou depois). Buscadas pela NF direto na SimpleRoute.
|
||||
scope :apontamentos, -> { where(manual: true) }
|
||||
|
||||
enum tipo: {
|
||||
entrega_normal: 0,
|
||||
retirada: 1,
|
||||
|
||||
@@ -39,6 +39,13 @@ class Entrega < ApplicationRecord
|
||||
where(driver: nome)
|
||||
}
|
||||
|
||||
# Busca pela NF (reference_id). A coluna é numérica, então comparamos como
|
||||
# texto para aceitar a entrada do usuário de forma robusta. Usado no
|
||||
# apontamento manual de entregas (NF que chegou fora do período).
|
||||
scope :por_nf, ->(nf) {
|
||||
where('reference_id::text = ?', nf.to_s.strip)
|
||||
}
|
||||
|
||||
scope :da_veiculo, ->(vehicle) {
|
||||
where(vehicle: vehicle)
|
||||
}
|
||||
|
||||
@@ -66,9 +66,22 @@
|
||||
<span class="<%= c.tipo_cor[:bg] %> <%= c.tipo_cor[:text] %> px-2 py-1 rounded text-xs font-bold">
|
||||
<%= c.tipo_cor[:label] %>
|
||||
</span>
|
||||
<% if c.manual? %>
|
||||
<span class="ml-1 bg-orange-500/15 text-orange-400 border border-orange-500/40 px-2 py-1 rounded text-xs font-bold">➕ Apontamento</span>
|
||||
<% end %>
|
||||
</td>
|
||||
<td class="px-4 py-3 text-right font-bold <%= c.desconto? ? 'text-red-400' : 'text-white' %>">
|
||||
<%= c.desconto? ? '-' : '' %><%= moeda(c.valor_aplicado) %>
|
||||
<div class="flex items-center justify-end gap-2">
|
||||
<span><%= c.desconto? ? '-' : '' %><%= moeda(c.valor_aplicado) %></span>
|
||||
<% if c.manual? && !(@consolidacao.finalizada? && current_user.operador?) %>
|
||||
<%= button_to '✕',
|
||||
remover_apontamento_consolidacao_consolidacao_entregas_path(@consolidacao, id: c.id, motorista: @motorista),
|
||||
method: :delete,
|
||||
form: { data: { turbo_confirm: 'Remover este apontamento manual?' } },
|
||||
class: 'text-gray-500 hover:text-red-400 text-sm leading-none px-1',
|
||||
title: 'Remover apontamento' %>
|
||||
<% end %>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
@@ -31,6 +31,63 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%# Apontamento manual — adiciona entrega por fora (busca pela NF na rastreio) %>
|
||||
<% unless @consolidacao.finalizada? && current_user.operador? %>
|
||||
<div data-controller="apontamento"
|
||||
data-apontamento-buscar-url-value="<%= buscar_nf_consolidacao_consolidacao_entregas_path(@consolidacao) %>"
|
||||
data-apontamento-apontar-url-value="<%= apontar_consolidacao_consolidacao_entregas_path(@consolidacao) %>"
|
||||
class="mb-4">
|
||||
<button type="button" data-action="apontamento#abrir"
|
||||
class="w-full md:w-auto inline-flex items-center justify-center gap-2 bg-[#1a1a1a] hover:bg-[#2a2a2a] text-white border border-dashed border-orange-500/60 hover:border-orange-500 font-semibold px-4 py-3 rounded-lg min-h-[48px]">
|
||||
➕ Apontamento manual
|
||||
<span class="text-gray-500 text-xs font-normal hidden md:inline">(nota que chegou por fora)</span>
|
||||
</button>
|
||||
|
||||
<%# Modal %>
|
||||
<div data-apontamento-target="modal" data-action="click->apontamento#fundo"
|
||||
class="hidden fixed inset-0 z-50 bg-black/70 flex items-center justify-center p-4">
|
||||
<div class="bg-[#1a1a1a] border border-[#2a2a2a] rounded-2xl w-full max-w-lg p-6 max-h-[90vh] overflow-y-auto">
|
||||
<div class="flex items-center justify-between mb-1">
|
||||
<h2 class="text-xl font-bold text-white">Apontamento manual</h2>
|
||||
<button type="button" data-action="apontamento#fechar"
|
||||
class="text-gray-500 hover:text-white text-2xl leading-none">✕</button>
|
||||
</div>
|
||||
<p class="text-gray-400 text-sm mb-4">Busque a nota pela base de rastreio e adicione ao fechamento do motorista correspondente.</p>
|
||||
|
||||
<p data-apontamento-target="erro" class="hidden bg-red-500/10 text-red-400 border border-red-500/30 rounded-lg px-3 py-2 text-sm mb-3"></p>
|
||||
|
||||
<%# Busca da NF %>
|
||||
<form data-action="submit->apontamento#buscar" class="flex gap-2 mb-4">
|
||||
<input data-apontamento-target="nf" type="text" inputmode="numeric"
|
||||
placeholder="Número da nota (NF)"
|
||||
class="flex-1 bg-[#0a0a0a] border border-[#2a2a2a] text-white rounded-lg px-3 py-3 text-base focus:border-orange-500 focus:outline-none">
|
||||
<button type="submit"
|
||||
class="bg-orange-500 hover:bg-orange-600 text-black font-bold px-5 rounded-lg min-h-[48px]">Buscar</button>
|
||||
</form>
|
||||
|
||||
<%# Resultados (preenchidos via JS) %>
|
||||
<div data-apontamento-target="resultados" class="space-y-2 mb-4"></div>
|
||||
|
||||
<%# Confirmação — tipo + adicionar (oculto até a busca retornar) %>
|
||||
<div data-apontamento-target="confirmar" class="hidden border-t border-[#2a2a2a] pt-4">
|
||||
<label class="block text-gray-400 text-sm mb-2">Classificar como</label>
|
||||
<select data-apontamento-target="tipo"
|
||||
class="w-full bg-[#0a0a0a] border border-[#2a2a2a] text-white rounded-lg px-3 py-3 text-base focus:border-orange-500 focus:outline-none mb-4 cursor-pointer">
|
||||
<option value="entrega_normal">🚚 Entrega Normal — <%= moeda(Configuracao.preco_entrega) %></option>
|
||||
<option value="retirada">📦 Retirada — <%= moeda(Configuracao.preco_retirada) %></option>
|
||||
<option value="bonus">⭐ Bônus — <%= moeda(Configuracao.preco_bonus) %></option>
|
||||
<option value="desconto">⚠️ Desconto — <%= moeda(Configuracao.preco_desconto) %></option>
|
||||
</select>
|
||||
<button type="button" data-action="apontamento#apontar"
|
||||
class="w-full bg-green-600 hover:bg-green-500 text-white font-bold py-3.5 rounded-lg min-h-[48px]">
|
||||
Adicionar apontamento ✓
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%# Lista de motoristas %>
|
||||
<div class="space-y-3">
|
||||
<% @motoristas.each do |m| %>
|
||||
@@ -50,6 +107,9 @@
|
||||
· <span class="<%= m[:completo] ? 'text-green-400' : 'text-orange-400' %>">
|
||||
<%= m[:classificadas] %>/<%= m[:total] %> classificadas
|
||||
</span>
|
||||
<% if m[:apontamentos].positive? %>
|
||||
· <span class="text-orange-400">➕ <%= m[:apontamentos] %> apontamento<%= 's' if m[:apontamentos] > 1 %></span>
|
||||
<% end %>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user