<commit_before><?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use App\Widgets\AppointmentWidget;

# use App\User;

class Appointment extends Model
{
    protected $fillable = ['issuer_id', 'contact_id', 'business_id', 'service_id', 'start_at', 'duration', 'comments'];

    protected $guarded = ['id', 'hash', 'status', 'finish_at'];

    protected $dates = ['start_at', 'finish_at'];

    protected $widget = null;

    const STATUS_RESERVED  = 'R';
    const STATUS_CONFIRMED = 'C';
    const STATUS_ANNULATED = 'A';
    const STATUS_SERVED    = 'S';

    const PROFILE_USER    = 'user';
    const PROFILE_MANAGER = 'manager';

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->attributes['hash'] = md5($this->start_at.'/'.$this->contact_id.'/'.$this->business_id.'/'.$this->service_id);
    }

    public function save(array $options = array())
    {
        parent::save();
    }

    public function issuer()
    {
        return $this->belongsTo('App\User');
    }

    public function contact()
    {
        return $this->belongsTo('App\Contact');
    }

    public function user()
    {
        return $this->contact->first()->user->first();
    }

    public function business()
    {
        return $this->belongsTo('App\Business');
    }

    public function service()
    {
        return $this->belongsTo('App\Service');
    }

    public function duplicates()
    {
        return !self::where('hash', $this->hash)->get()->isEmpty();
    }

    public function getFinishAtAttribute()
    {
        if (is_numeric($this->duration)) {
            return $this->start_at->addMinutes($this->duration);
        }
        return $this->start_at;
    }

    public function getCodeAttribute()
    {
        return substr($this->hash, 0, $this->business->pref('appointment_code_length'));
    }

    public function getTZAttribute()
    {
        return $this->business->timezone;
    }

    public function getStatusLabelAttribute()
    {
        $labels = [ Self::STATUS_RESERVED  => 'reserved',
                    Self::STATUS_CONFIRMED => 'confirmed',
                    Self::STATUS_ANNULATED => 'annulated',
                    Self::STATUS_SERVED    => 'served',
                ];

        return array_key_exists($this->status, $labels) ? $labels[$this->status] : '';
    }

    public function getDateAttribute()
    {
        return $this->start_at->timezone('UTC')->toDateString();
    }

    public function setStartAtAttribute(Carbon $datetime)
    {
        $this->attributes['start_at'] = $datetime;
    }

    public function isActive()
    {
        return $this->status == Self::STATUS_CONFIRMED || $this->status == Self::STATUS_RESERVED;
    }

    public function isPending()
    {
        return $this->isActive() && $this->isFuture();
    }

    public function isFuture()
    {
        return !$this->isDue();
    }<commit_msg>Save null comments when empty<commit_after><?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use App\Widgets\AppointmentWidget;

# use App\User;

class Appointment extends Model
{
    protected $fillable = ['issuer_id', 'contact_id', 'business_id', 'service_id', 'start_at', 'duration', 'comments'];

    protected $guarded = ['id', 'hash', 'status', 'finish_at'];

    protected $dates = ['start_at', 'finish_at'];

    protected $widget = null;

    const STATUS_RESERVED  = 'R';
    const STATUS_CONFIRMED = 'C';
    const STATUS_ANNULATED = 'A';
    const STATUS_SERVED    = 'S';

    const PROFILE_USER    = 'user';
    const PROFILE_MANAGER = 'manager';

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->attributes['hash'] = md5($this->start_at.'/'.$this->contact_id.'/'.$this->business_id.'/'.$this->service_id);
    }

    public function save(array $options = array())
    {
        parent::save();
    }

    public function issuer()
    {
        return $this->belongsTo('App\User');
    }

    public function contact()
    {
        return $this->belongsTo('App\Contact');
    }

    public function user()
    {
        return $this->contact->first()->user->first();
    }

    public function business()
    {
        return $this->belongsTo('App\Business');
    }

    public function service()
    {
        return $this->belongsTo('App\Service');
    }

    public function duplicates()
    {
        return !self::where('hash', $this->hash)->get()->isEmpty();
    }

    public function getFinishAtAttribute()
    {
        if (is_numeric($this->duration)) {
            return $this->start_at->addMinutes($this->duration);
        }
        return $this->start_at;
    }

    public function getCodeAttribute()
    {
        return substr($this->hash, 0, $this->business->pref('appointment_code_length'));
    }

    public function getTZAttribute()
    {
        return $this->business->timezone;
    }

    public function getStatusLabelAttribute()
    {
        $labels = [ Self::STATUS_RESERVED  => 'reserved',
                    Self::STATUS_CONFIRMED => 'confirmed',
                    Self::STATUS_ANNULATED => 'annulated',
                    Self::STATUS_SERVED    => 'served',
                ];

        return array_key_exists($this->status, $labels) ? $labels[$this->status] : '';
    }

    public function getDateAttribute()
    {
        return $this->start_at->timezone('UTC')->toDateString();
    }

    public function setStartAtAttribute(Carbon $datetime)
    {
        $this->attributes['start_at'] = $datetime;
    }

    public function setCommentsAttribute($comments)
    {
        $this->attributes['comments'] = $comments ?: null;
    }

    public function isActive()
    {
        return $this->status == Self::STATUS_CONFIRMED || $this->status == Self::STATUS_RESERVED;
    }

    public function isPending()
    {
        return $this->isActive() && $this->isFuture();
    }

    public function isFuture()
    {
        return !$this->isDue();
    }
{'fraction_non_alphanumeric': 0.1517798286090969, 'fraction_numerical': 0.0006591957811470006, 'mean_word_length': 2.5890005913660556, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3262447', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1793, 'n_words': 342}