%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/hr/api/app/Models/
Upload File :
Create Path :
Current File : /var/www/html/hr/api/app/Models/WorkingDay.php

<?php

namespace App\Models;

use App\Exceptions\AppException;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Facades\DB;
use Throwable;

/**
 * @property integer id
 * @property string starts_at
 * @property string ends_at
 * @property boolean has_shifts
 * @property integer day_id
 * @property mixed valid_from
 * @property mixed valid_to
 * @property Day day
 */
class WorkingDay extends BaseModel
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'starts_at',
        'ends_at',
        'has_shifts',
        'valid_from',
        'valid_to',
    ];

    protected $guarded = [
        'day_id',
    ];

    protected $dates = [
        'valid_from',
        'valid_to',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id'         => 'integer',
        'starts_at'  => 'time',
        'ends_at'    => 'time',
        'has_shifts' => 'boolean',
        'valid_from' => 'date',
        'valid_to'   => 'date',
    ];

    /**
     * @param $data
     * @return WorkingDay
     * @throws Throwable
     */
    public static function createItem($data)
    {
        try {
            DB::beginTransaction();
            /** @var WorkingDay $sameDayWorkingDay */
            $sameDayWorkingDay = self::query()
                                     ->where('day_id', $data['day_id'])
                                     ->orderByDesc('created_at')
                                     ->first();

            if ($sameDayWorkingDay) {
                if (!$sameDayWorkingDay->valid_to) {
                    throw new AppException('Can not create a new working day unless you set the valid to of the current working day');
                }
                $currentStart = $sameDayWorkingDay->valid_from;
                $currentEnd = $sameDayWorkingDay->valid_to;
                $overlaps = Carbon::parse($data['valid_from'])
                                  ->between($currentStart, $currentEnd);
                if ($overlaps) {
                    throw new AppException('Working day you are trying to create is overlapping with the current dates.');
                }
            }

            /** @var WorkingDay $day */
            $day = self::query()
                       ->make([
                           'starts_at'  => $data['starts_at'],
                           'ends_at'    => $data['ends_at'],
                           'has_shifts' => $data['has_shifts'],
                           'valid_from' => $data['valid_from'],
                           'valid_to'   => array_key_exists('valid_to', $data)
                               ? $data['valid_to']
                               : null,
                       ]);
            $day->day()
                ->associate($data['day_id']);
            $day->save();
            DB::commit();
            return $day;
        } catch (Throwable $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function day()
    {
        return $this->belongsTo(Day::class);
    }

    public static function getDayNames()
    {
        $now = Carbon::now();
        $workingDays = collect(self::query()
                                   ->where('valid_from', '<=', $now)
                                   ->where(function ($q) use ($now) {
                                       $q->whereNull('valid_to');
                                       $q->orWhere('valid_to', '>=', $now);
                                   })
                                   ->with('day')
                                   ->get());
        $data = collect();

        foreach ($workingDays as $day) {
            $data->push($day->day->name);
        }

        return $data->toArray();
    }

    public static function getWorkingDaysCount($start, $end, $user = null, $includePto = true)
    {
        if (!$user) {
            $user = self::getLoggedInUser();
        }
        $daysOff = $user->getDaysOffIn($start, $end, $includePto);
        $range = CarbonPeriod::create($start, $end);
        $dates = collect();

        foreach ($range as $date) {
            $isDayOff = collect($daysOff)
                    ->pluck('date')
                    ->filter(function (Carbon $d) use ($date) {
                        return $d->isSameDay($date);
                    })
                    ->count() > 0;
            if (!$isDayOff && Day::isWorkingDay($date)) {
                $dates->push($date);
            }
        }
        return $dates->count();
    }

    /**
     * @param $data
     * @return WorkingDay
     * @throws Throwable
     */
    public function updateItem($data)
    {
        try {
            $this->starts_at = $data['starts_at'];
            $this->ends_at = $data['ends_at'];
            $this->has_shifts = $data['has_shifts'];
            $this->valid_from = $data['valid_from'];
            $this->valid_to = array_key_exists('valid_to', $data)
                ? $data['valid_to']
                : null;
            $this->day()
                 ->associate($data['day_id']);
            $this->save();
            return $this;
        } catch (Throwable $e) {
            throw $e;
        }
    }

}

Zerion Mini Shell 1.0