%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/Traits/
Upload File :
Create Path :
Current File : /var/www/html/camillo/camillo-api-master/app/Traits/CalendarTrait.php

<?php

namespace App\Traits;

use App\Models\Absence;
use App\Models\ArrivalException;
use App\Models\Classroom;
use App\Models\Event;
use App\Models\Festivity;
use App\Models\Minor;
use Carbon\Carbon;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

trait CalendarTrait
{
    /**
     * @param       $start_of_month
     * @param       $end_of_month
     * @param Minor $minor
     * @param null  $classroom
     *
     * @return array
     */
    protected function getFestivities($start_of_month, $end_of_month, Minor $minor, $classroom = null)
    {
        if(!$classroom) {
            $classroom = $minor->getExpectedClassroom();
        }
        $schoolyear = $classroom->schoolyear;


        $festivities = Festivity::where('schoolyear_id', $schoolyear->id)
            ->whereDate('start_date', '>=', $start_of_month->toDateString())
            ->whereDate('end_date', '<=', $end_of_month->toDateString())
            ->get();

        $result = collect();
        /** @var Carbon $day */
        $day = $start_of_month->copy();

        while($day <= $end_of_month) {
            $festivity['isFestivity'] = $day->isSunday();
            $festivity['description'] = $day->isSunday() ? 'Sunday' : '';

            if(!$day->isSunday()) {
                $fest = $festivities->filter(function($item) use($day) {
                    return $item->start_date >= $day->toDateTimeString() && $item->end_date <= $day->toDateTimeString();
                });

                $festivity['isFestivity'] = ($fest->count() > 0);
                $festivity['description'] = ($fest->count() > 0) ? $fest->first()->description :  '';
            }

            $result->put($day->day, $festivity);
            $day = $day->addDay(1);
        }
        return $result;
    }

    /**
     * @param       $start_of_month
     * @param       $end_of_month
     * @param Minor $minor
     * @param null  $classroom
     *
     * @return array
     */
    protected function getExpectedAbsences($start_of_month, $end_of_month, Minor $minor, $classroom = null)
    {
        if(!$classroom) {
            $classroom = $minor->getExpectedClassroom();
        }

        $expected_absences = Absence::where('minor_id', $minor->id)
            ->where('classroom_id', $classroom->id)
            ->whereDate('date', '>=', $start_of_month->toDateString())
            ->whereDate('date', '<=', $end_of_month->toDateString())
            ->get();


        $result = collect();
        /** @var Carbon $day */
        $day = $start_of_month->copy();
        while($day <= $end_of_month) {

            $abs = $expected_absences->filter(function ($item) use ($day){
                return ($item->date == $day);
            });

            $absence['isExpectedAbsence'] = ($abs->count() > 0);
            $absence['reason'] = ($abs->count() > 0) ? $abs->first()->reason : "";
            $absence['user_id'] = ($abs->count() > 0) ? $abs->first()->user->id: "";
            $absence['individual_id'] = ($abs->count() > 0) ? $abs->first()->user->individual->id: "";
            $absence['tutor_name'] = ($abs->count() > 0) ? $abs->first()->user->individual->name . " " . $abs->first()->user->individual->surname : "";

            $result->put($day->day, $absence);
            $day = $day->addDay(1);
        }

        return $result;
    }

    /**
     * @param       $start_of_month
     * @param       $end_of_month
     * @param Minor $minor
     * @param null  $classroom
     *
     * @return array
     */
    protected function getArrivalExceptions($start_of_month, $end_of_month, Minor $minor, $classroom = null)
    {
        if(!$classroom) {
            $classroom = $minor->getExpectedClassroom();
        }

        $arrival_exceptions = ArrivalException::where('minor_id', $minor->id)
            ->where('classroom_id', $classroom->id)
            ->whereDate('date', '>=', $start_of_month->toDateString())
            ->whereDate('date', '<=', $end_of_month->toDateString())
            ->get();

        $result = collect();
        $day = $start_of_month->copy();
        while($day <= $end_of_month){

            $exceptions = $arrival_exceptions->filter(function($item) use ($day) {
                return ($item->date == $day);
            });

            $arrival_exception["hasArrivalException"] = ($exceptions->count() > 0);
            $arrival_exception["user_id"] = ($exceptions->count() > 0) ? $exceptions->first()->user_id : "";
            $arrival_exception["individual_id"] = ($exceptions->count() > 0) ? $exceptions->first()->user->individual->id : "";
            $arrival_exception["tutor_name"] = ($exceptions->count() > 0) ? $exceptions->first()->user->individual->name . " " . $exceptions->first()->user->individual->surname : "";

            $result->put($day->day, $arrival_exception);
            $day = $day->addDay(1);
        }

        return $result;
    }

    /**
     * @param       $start_of_month
     * @param       $end_of_month
     * @param Minor $minor
     * @param null  $classroom
     *
     * @return array
     */
    protected function getEvents($start_of_month, $end_of_month, Minor $minor, $classroom = null)
    {
        if(!$classroom) {
            $classroom = $minor->getExpectedClassroom();
        }
        $month_events = Event::where('minor_id', $minor->id)
                ->where('classroom_id', $classroom->id)
                ->where('date_as_int', '>=', (int)$start_of_month->format('Ymd'))
                ->where('date_as_int', '<=', (int)$end_of_month->format('Ymd'))
                ->get();

        $result = collect();
        $day = $start_of_month->copy();
        while($day <= $end_of_month){

            $events = $month_events->filter(function($item) use ($day){
                return ($item->date->format("Y-m-d") == $day->format("Y-m-d"));
            });

            $day_events = collect();
            foreach($events as $event){
                $evt["id"] = $event->id;
                $evt["timestamp"] = $event->date;
                $evt["description"] = $event->description;
                $evt["event_type_id"] = $event->event_type_id;
                $evt["event_type"] = $event->eventType->event_type;
                $evt["user_id"] = $event->user_id;
                $evt["individual_id"] = $event->user->individual->id;
                $evt["name"] = $event->user->individual->name . " " . $event->user->individual->surname;
                $evt["phone"] = $event->user->individual->mobile;

                $day_events->push($evt);
            }

            $result->put($day->day, $day_events);
            $day = $day->addDay(1);
        }

        return $result;
    }
}


Zerion Mini Shell 1.0