@php use Illuminate\Support\Facades\DB; $idIgreja = request('idIgreja'); $talao = request('talao'); // ── Nome da igreja ───────────────────────────────────────────────────────── $igreja = $idIgreja ? DB::table('igrejas')->where('idIgreja', $idIgreja)->first() : null; $nomeIgreja = $igreja ? $igreja->Nome : 'Todas as igrejas'; // ── Query de recibos ─────────────────────────────────────────────────────── $query = DB::table('financeiro_recibos as r') ->leftJoin('financeiro_recibos_categorias as rc', 'rc.id', '=', 'r.idReciboCategoria') ->leftJoin('financeiro_lancamentos as l', 'l.idLancamento', '=', 'r.idLancamento'); if ($idIgreja) { $query->where('r.idIgreja', $idIgreja); } if ($talao) { $query->where('r.Talao', $talao); } $recibos = $query ->select( 'r.idRecibo', 'r.Sequencial', 'r.Talao', 'r.Descricao', 'r.Justificativa', 'rc.Legenda as CategoriaLegenda', 'rc.Nome as CategoriaNome', 'l.idLancamento', 'l.Descricao as LancDescricao', 'l.dtPagamento', 'l.Valor as LancValor' ) ->orderBy('r.Talao') ->orderBy('r.Sequencial') ->get(); // ── Totais ──────────────────────────────────────────────────────────────── $totalValor = $recibos->sum('LancValor'); $totalCount = $recibos->count(); $porTalao = $recibos->groupBy('Talao'); $vinculados = $recibos->whereNotNull('idLancamento')->count(); // ── Helpers ─────────────────────────────────────────────────────────────── $fmtMoeda = fn($v) => 'R$ ' . number_format(abs((float)$v), 2, ',', '.'); $fmtData = fn($d) => $d ? \Carbon\Carbon::parse($d)->format('d/m/Y') : '—'; $fmtSit = function($s) { return match($s) { 'A' => ['texto' => 'Ativo', 'classe' => 'badge-ativo'], 'C' => ['texto' => 'Cancelado', 'classe' => 'badge-cancelado'], 'P' => ['texto' => 'Pago', 'classe' => 'badge-pago'], default => ['texto' => ($s ?: '—'), 'classe' => 'badge-outro'], }; }; @endphp Recibos{{ $talao ? ' — Talão '.$talao : '' }} — {{ $nomeIgreja }}
{{-- ── Cabeçalho ── --}}
@include('relatorios.partials.logo-header', ['logoStyle' => 'height:40px;width:auto;object-fit:contain;'])
RELATÓRIO DE RECIBOS
{{ $nomeIgreja }}
@if($talao)
Talão: {{ $talao }}
@else
Todos os talões
@endif
Total de recibos: {{ $totalCount }}
@if($totalValor > 0)
Valor total: {{ $fmtMoeda($totalValor) }}
@endif
Emissão: {{ now()->format('d/m/Y H:i') }}
{{-- ── Resumo ── --}}
Total de Recibos
{{ $totalCount }}
Valor Total
{{ $fmtMoeda($totalValor) }}
Com Lançamento Vinculado
{{ $vinculados }}
{{-- ── Recibos ── --}} @if($recibos->isEmpty())
Nenhum recibo encontrado para os filtros selecionados.
@else @if(!$talao && $porTalao->count() > 1) {{-- Múltiplos talões: agrupar --}} @foreach($porTalao as $numTalao => $recsDeTalao)
Talão {{ $numTalao ?: 'Sem talão' }} {{ $recsDeTalao->count() }} recibo(s) · {{ $fmtMoeda($recsDeTalao->sum('Valor')) }}
@foreach($recsDeTalao as $r) @endforeach
Nº Seq Categoria Descrição / Pagador Justificativa Valor Sit. Emissão
{{ $r->Sequencial }} @if($r->CategoriaLegenda) {{ $r->CategoriaLegenda }} @endif @if($r->CategoriaNome)
{{ $r->CategoriaNome }}
@endif @if(!$r->CategoriaLegenda && !$r->CategoriaNome) @endif
{{ $r->Descricao ?: '—' }}
@if($r->idLancamento)
Lanç. #{{ $r->idLancamento }} @if($r->LancDescricao) — {{ $r->LancDescricao }} @endif @if($r->dtPagamento) · {{ $fmtData($r->dtPagamento) }} @endif @if($r->LancValor) · {{ $fmtMoeda($r->LancValor) }} @endif
@endif
{{ $r->Justificativa ?: '—' }} {{ $r->LancValor ? $fmtMoeda($r->LancValor) : '—' }}
Subtotal Talão {{ $numTalao }}: {{ $fmtMoeda($recsDeTalao->sum('LancValor')) }}
@endforeach {{-- Total geral --}}
Total Geral: {{ $fmtMoeda($totalValor) }}  |  {{ $totalCount }} recibo(s)
@else {{-- Talão único ou todos sem agrupamento visual --}}
{{ $talao ? 'Talão ' . $talao : 'Todos os Recibos' }} — {{ $totalCount }} recibo(s)
@if(!$talao)@endif @foreach($recibos as $r) @if(!$talao)@endif @endforeach
Nº SeqTalãoCategoria Descrição / Pagador Justificativa Valor Sit. Emissão
{{ $r->Sequencial }}{{ $r->Talao ?: '—' }} @if($r->CategoriaLegenda) {{ $r->CategoriaLegenda }} @endif @if($r->CategoriaNome)
{{ $r->CategoriaNome }}
@endif @if(!$r->CategoriaLegenda && !$r->CategoriaNome) @endif
{{ $r->Descricao ?: '—' }}
@if($r->idLancamento)
Lanç. #{{ $r->idLancamento }} @if($r->LancDescricao) — {{ $r->LancDescricao }} @endif @if($r->dtPagamento) · {{ $fmtData($r->dtPagamento) }} @endif @if($r->LancValor) · {{ $fmtMoeda($r->LancValor) }} @endif
@endif
{{ $r->Justificativa ?: '—' }} {{ $r->LancValor ? $fmtMoeda($r->LancValor) : '—' }}
Total: {{ $fmtMoeda($totalValor) }} {{ $totalCount }} recibo(s)
@endif @endif {{-- ── Rodapé ── --}}