<?php

namespace App\Exports;

use App\Models\Refund;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class RefundExport implements FromView
{
    public function view(): View
    {
         
        $from = Carbon::parse(sprintf(
            '%s 00:00',
            request()->query('from', Carbon::now()->toDateString())
        ));
        $to = Carbon::parse(sprintf(
            '%s 23:59:59',
            request()->query('to', Carbon::now()->toDateString())
        ));
            $qb = Refund::whereBetween('created_at', [$from, $to]);
               
        if(request()->query('office_id')){
            $qb->whereHas('ticket.segment.booking',function($query){
               $query->where('office_id',request()->query('office_id'));
            });
        }

        return view('refunds.export', [
            'refunds' => $qb->get()
        ]);
    }
}