<?php

namespace App\Modules\Bookings;

use Illuminate\Support\Facades\Auth;

class Order extends Task
{
    /**
     * Array of validation rules.
     *
     * @var  array
     */
    protected static $rules = [
        'client_id'    => 'required',
        'client_type'  => 'required',
        'site_id'      => 'required_if:client_type,client',
        'site_user_id' => 'required_if:client_type,client',
    ];

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    public static function boot()
    {
        parent::boot();

        // When a transaction is created we want to
        // update the created_by field
        static::creating(function ($model) {
            $model->type = 'order';
            $model->status_id = 2;
            $model->created_by = Auth::user()->id;
            $model->updated_by = Auth::user()->id;
        });

        // When a transaction gets updated we want to know who
        // updated it
        static::updating(function ($model) {
            $model->updated_by = Auth::user()->id;
        });
    }

    /**
     * Restrict query to type orders.
     */
    public function scopeOrders($query)
    {
        return $query->where('type', 'order');
    }
}
