<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Auth;
use App\CompanyFilter;
use App\Http\Models\CompanyFilterModel;
use App\Http\Utils\Utils;

class CompanyScope implements Scope {

    public function apply(Builder $builder, Model $model) {
        $user = Auth::user();
        if ($user) {
            $company_id = $user->company_id;
        } else {
            $company = Utils::getCompanyDomain();
            $company_id = $company ? $company->company_id : 1;
        }
        if ($company_id) {
            $builder->where('company_id', $company_id);
        }
        // add default order
        if ($model->getDefaultOrder()) {
            $builder->orderBy($model->getDefaultOrder());
        }
    }

}
