%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/Configuration.php

<?php

namespace App\Models;

use App\Exceptions\AppException;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Throwable;

/**
 * @property integer id
 * @property boolean should_get_holidays_from_working_day_overtime
 * @property integer holidays_percentage_from_working_day_overtime
 * @property boolean should_get_holidays_from_non_working_day_overtime
 * @property integer holidays_percentage_from_non_working_day_overtime
 * @property boolean should_postpone_holidays
 * @property integer holidays_validity_in_months
 * @property integer expected_daily_working_hours
 * @property integer overtime_percentage_on_working_day
 * @property integer overtime_percentage_on_non_working_day
 * @property integer out_sick_percentage
 * @property mixed valid_from
 * @property mixed valid_to
 * @property integer user_id
 * @property integer annual_leave_quantity
 * @property User user
 */
class Configuration extends BaseModel
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'should_get_holidays_from_working_day_overtime',
        'holidays_percentage_from_working_day_overtime',
        'should_get_holidays_from_non_working_day_overtime',
        'holidays_percentage_from_non_working_day_overtime',
        'should_postpone_holidays',
        'holidays_validity_in_months',
        'expected_daily_working_hours',
        'overtime_percentage_on_working_day',
        'overtime_percentage_on_non_working_day',
        'out_sick_percentage',
        'valid_from',
        'valid_to',
        'annual_leave_quantity',
    ];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id'                                                => 'integer',
        'should_get_holidays_from_working_day_overtime'     => 'boolean',
        'holidays_percentage_from_working_day_overtime'     => 'integer',
        'should_get_holidays_from_non_working_day_overtime' => 'boolean',
        'holidays_percentage_from_non_working_day_overtime' => 'integer',
        'should_postpone_holidays'                          => 'boolean',
        'holidays_validity_in_months'                       => 'integer',
        'expected_daily_working_hours'                      => 'integer',
        'overtime_percentage_on_working_day'                => 'integer',
        'overtime_percentage_on_non_working_day'            => 'integer',
        'out_sick_percentage'                               => 'integer',
        'valid_from'                                        => 'date',
        'valid_to'                                          => 'date',
        'annual_leave_quantity'                             => 'integer',
    ];

    /**
     * @param $data
     * @return Configuration
     * @throws Throwable
     */
    public static function createItem($data)
    {
        try {
            DB::beginTransaction();
            $current = self::query()
                           ->orderByDesc('created_at');
            if (array_key_exists('user_id', $data)) {
                $current = $current->where('user_id', $data['user_id']);
            }
            /** @var Configuration $current */
            $current = $current->first();

            if ($current) {
                if (!$current->valid_to) {
                    throw new AppException(trans('configurations.active_config_exists'));
                }
                $currentStart = $current->valid_from;
                $currentEnd = $current->valid_to;
                $overlaps = Carbon::parse($data['valid_from'])
                                  ->between($currentStart, $currentEnd);
                if ($overlaps) {
                    throw new AppException(trans('configurations.config_overlapping'));
                }
            }

            /** @var Configuration $configuration */
            $configuration = self::query()
                                 ->make([
                                     'should_get_holidays_from_working_day_overtime'     => $data['should_get_holidays_from_working_day_overtime'],
                                     'holidays_percentage_from_working_day_overtime'     => array_key_exists('holidays_percentage_from_working_day_overtime', $data)
                                         ? $data['holidays_percentage_from_working_day_overtime']
                                         : null,
                                     'should_get_holidays_from_non_working_day_overtime' => $data['should_get_holidays_from_non_working_day_overtime'],
                                     'holidays_percentage_from_non_working_day_overtime' => array_key_exists('holidays_percentage_from_non_working_day_overtime', $data)
                                         ? $data['holidays_percentage_from_non_working_day_overtime']
                                         : null,
                                     'should_postpone_holidays'                          => $data['should_postpone_holidays'],
                                     'expected_daily_working_hours'                      => $data['expected_daily_working_hours'],
                                     'holidays_validity_in_months'                       => $data['holidays_validity_in_months'],
                                     'overtime_percentage_on_working_day'                => $data['overtime_percentage_on_working_day'],
                                     'overtime_percentage_on_non_working_day'            => $data['overtime_percentage_on_non_working_day'],
                                     'out_sick_percentage'                               => $data['out_sick_percentage'],
                                     'valid_from'                                        => $data['valid_from'],
                                     'annual_leave_quantity'                             => $data['annual_leave_quantity'],
                                     'valid_to'                                          => array_key_exists('valid_to', $data)
                                         ? $data['valid_to']
                                         : null,
                                 ]);
            if (array_key_exists('user_id', $data)) {
                $configuration->user()
                              ->associate($data['user_id']);
            }
            $configuration->save();
            DB::commit();
            return $configuration;
        } catch (Throwable $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * @return Builder|Model|object
     */
    public static function getCurrent()
    {
        $now = Carbon::now();
        return self::query()
                   ->where('user_id', null)
                   ->where('valid_from', '<', $now)
                   ->where(function ($q) use ($now) {
                       $q->whereNull('valid_to');
                       $q->orWhere('valid_to', '>', $now);
                   })
                   ->first();
    }

    /**
     * @param $data
     * @return Configuration
     * @throws Throwable
     */
    public function updateItem($data)
    {
        try {
            DB::beginTransaction();
            $this->should_get_holidays_from_working_day_overtime = $data['should_get_holidays_from_working_day_overtime'];
            $this->holidays_percentage_from_working_day_overtime = array_key_exists('holidays_percentage_from_working_day_overtime', $data)
                ? $data['holidays_percentage_from_working_day_overtime']
                : null;
            $this->should_get_holidays_from_non_working_day_overtime = $data['should_get_holidays_from_non_working_day_overtime'];
            $this->holidays_percentage_from_non_working_day_overtime = array_key_exists('holidays_percentage_from_non_working_day_overtime', $data)
                ? $data['holidays_percentage_from_non_working_day_overtime']
                : null;
            $this->should_postpone_holidays = $data['should_postpone_holidays'];
            $this->expected_daily_working_hours = $data['expected_daily_working_hours'];
            $this->holidays_validity_in_months = $data['holidays_validity_in_months'];
            $this->overtime_percentage_on_working_day = $data['overtime_percentage_on_working_day'];
            $this->overtime_percentage_on_non_working_day = $data['overtime_percentage_on_non_working_day'];
            $this->out_sick_percentage = $data['out_sick_percentage'];
            $this->valid_from = $data['valid_from'];
            $this->annual_leave_quantity = $data['annual_leave_quantity'];
            $this->valid_to = array_key_exists('valid_to', $data)
                ? $data['valid_to']
                : null;
            $this->save();
            DB::commit();
            return $this;
        } catch (Throwable $e) {
            DB::rollBack();
            throw $e;
        }
    }

}

Zerion Mini Shell 1.0