# app/models/consolidacao_entrega.rb class ConsolidacaoEntrega < ApplicationRecord # Nome da tabela fixado (mesmo motivo de ConsolidacaoMotorista — inflector pt-BR). self.table_name = 'consolidacao_entregas' 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, bonus: 2, desconto: 3 } TIPO_CORES = { 'entrega_normal' => { bg: 'bg-orange-500', text: 'text-white', label: 'Entrega Normal' }, 'retirada' => { bg: 'bg-orange-800', text: 'text-white', label: 'Retirada' }, 'bonus' => { bg: 'bg-white border border-orange-500', text: 'text-black', label: 'Bônus' }, 'desconto' => { bg: 'bg-gray-900', text: 'text-white', label: 'Desconto' } }.freeze # Uma entrega pode ter vários pilares (Normal + Bônus + Retirada…), # mas não o mesmo pilar repetido. validates :tracking_id, presence: true, uniqueness: { scope: %i[consolidacao_id tipo] } validates :motorista_nome, presence: true validates :tipo, presence: true validates :valor_aplicado, presence: true, numericality: { greater_than_or_equal_to: 0 } # Busca a entrega original (leitura do banco existente) def entrega_original @entrega_original ||= Entrega.find_by(tracking_id: tracking_id) end def tipo_cor TIPO_CORES[tipo] || TIPO_CORES['entrega_normal'] end end