implantação da fase 2 e 3
This commit is contained in:
@@ -1,88 +1,39 @@
|
||||
# app/models/entrega.rb
|
||||
#
|
||||
# Model de LEITURA para a tabela existente da Gade Hospitalar.
|
||||
# NUNCA criar migration para esta tabela.
|
||||
# NUNCA executar INSERT, UPDATE, DELETE ou DROP nesta tabela.
|
||||
#
|
||||
# ⚠️ READ-ONLY — tabela existente: public.db_reem_simplerout_2026
|
||||
# NUNCA fazer DROP, TRUNCATE, DELETE ou migration nessa tabela.
|
||||
class Entrega < ApplicationRecord
|
||||
self.table_name = 'db_reem_simplerout_2026'
|
||||
self.table_name = 'db_reem_simplerout_2026'
|
||||
self.primary_key = 'tracking_id'
|
||||
|
||||
# Apenas leitura — segurança contra mutações acidentais
|
||||
# Apenas leitura
|
||||
def readonly?
|
||||
true
|
||||
end
|
||||
|
||||
# ── Scopes ──────────────────────────────────────────────────
|
||||
scope :concluidas, -> { where(status: 'completed') }
|
||||
scope :com_checkin, -> { where.not(checkin: nil) }
|
||||
scope :pagas, -> { concluidas.com_checkin }
|
||||
scope :pendentes, -> { where.not(status: 'completed') }
|
||||
# Scopes
|
||||
scope :do_account, -> { where(account_id: 95907) }
|
||||
scope :pagas, -> { where(status: 'completed').where.not(checkin: nil) }
|
||||
scope :pendentes, -> { where.not(status: 'completed').or(where(checkin: nil)) }
|
||||
|
||||
scope :no_periodo, ->(inicio, fim) {
|
||||
where(planned_date: inicio.to_date..fim.to_date)
|
||||
scope :do_mes, ->(data = Date.today) {
|
||||
do_account
|
||||
.where(planned_date: data.beginning_of_month..data.end_of_month)
|
||||
}
|
||||
|
||||
scope :do_dia, ->(data = Date.today) {
|
||||
do_account.where(planned_date: data)
|
||||
}
|
||||
|
||||
scope :do_motorista, ->(nome) {
|
||||
where(driver: nome)
|
||||
where('LOWER(driver) = ?', nome.to_s.downcase)
|
||||
}
|
||||
|
||||
scope :da_rota, ->(route_id) {
|
||||
where(route_id: route_id)
|
||||
}
|
||||
|
||||
scope :da_conta_gade, -> {
|
||||
where(account_id: ENV.fetch('DB_EXISTING_ACCOUNT_ID', 95907).to_i)
|
||||
}
|
||||
|
||||
# ── Métodos de classe ────────────────────────────────────────
|
||||
|
||||
# Lista motoristas únicos (para selects, consolidações)
|
||||
def self.motoristas_ativos(inicio: nil, fim: nil)
|
||||
base = da_conta_gade
|
||||
base = base.no_periodo(inicio, fim) if inicio && fim
|
||||
base.distinct.order(:driver).pluck(:driver).compact.reject(&:empty?)
|
||||
# Helpers de instância
|
||||
def paga?
|
||||
status == 'completed' && checkin.present?
|
||||
end
|
||||
|
||||
# Lista rotas únicas (route_id → descrição)
|
||||
def self.rotas_unicas(inicio: nil, fim: nil)
|
||||
base = da_conta_gade
|
||||
base = base.no_periodo(inicio, fim) if inicio && fim
|
||||
base.distinct.pluck(:route_id).compact
|
||||
end
|
||||
|
||||
# Contagem de entregas pagas para cálculo estimado
|
||||
def self.contar_pagas(inicio:, fim:, motorista: nil, route_id: nil)
|
||||
base = pagas.da_conta_gade.no_periodo(inicio, fim)
|
||||
base = base.do_motorista(motorista) if motorista.present?
|
||||
base = base.da_rota(route_id) if route_id.present?
|
||||
base.count
|
||||
end
|
||||
|
||||
# ── Helpers de instância ─────────────────────────────────────
|
||||
|
||||
def numero_nf
|
||||
reference_id
|
||||
end
|
||||
|
||||
def local
|
||||
contact_name.presence || address
|
||||
end
|
||||
|
||||
def concluida?
|
||||
status == 'completed'
|
||||
end
|
||||
|
||||
def checkin_registrado?
|
||||
checkin.present?
|
||||
end
|
||||
|
||||
def elegivel_pagamento?
|
||||
concluida? && checkin_registrado?
|
||||
end
|
||||
|
||||
def atraso_minutos
|
||||
return 0 unless delay.present?
|
||||
delay.to_s.split(':').then { |h, m, _s| h.to_i * 60 + m.to_i }
|
||||
def data_formatada
|
||||
planned_date&.strftime('%d/%m/%Y')
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user