teste #3

Merged
victor merged 20 commits from teste into main 2026-07-02 15:31:51 -03:00
Showing only changes of commit 7f21a16327 - Show all commits

View File

@@ -193,12 +193,14 @@ module Analytics
SQL
end
# Sem filtro de conta: o INNER JOIN com a tabela da operação (gade_entregas_*)
# já restringe aos dados do cliente. Mantém a query idêntica à de gestão.
sql = <<~SQL
WITH ultimo AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY reference_id ORDER BY checkout DESC NULLS LAST) AS rn
FROM #{conn.quote_table_name(Entrega.table_name)}
WHERE reference_id IS NOT NULL #{conta_sql(conn)}
WHERE reference_id IS NOT NULL
)
#{unions.join("\nUNION ALL\n")}
SQL
@@ -225,23 +227,5 @@ module Analytics
" AND ((r.checkout >= #{ini} AND r.checkout < #{fim_excl})" \
" OR (r.checkout IS NULL AND r.planned_date >= #{ini} AND r.planned_date <= #{fim_dia}))"
end
# Replica Entrega.da_conta_gade como fragmento "AND (account_id ...)".
# Lista separada por vírgula em DB_EXISTING_ACCOUNT_ID; "all"/vazio = sem filtro.
def conta_sql(conn)
contas = ENV.fetch('DB_EXISTING_ACCOUNT_ID', '95907').to_s.strip
return '' if contas.empty? || contas.casecmp?('all')
valores = contas.split(',', -1).map(&:strip)
inclui_null = valores.any?(&:empty?)
lista = valores.reject(&:empty?).map { |v| conn.quote(v) }
condicoes = []
condicoes << "account_id IN (#{lista.join(', ')})" if lista.any?
condicoes << 'account_id IS NULL' if inclui_null
return '' if condicoes.empty?
"AND (#{condicoes.join(' OR ')})"
end
end
end