WorkersController.php 6.99 KB
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\WorkerRequest;
use App\Models\Company;
use App\Models\Job_title;
use App\Models\Static_worker;
use App\Models\User;
use App\Models\Worker;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class WorkersController extends Controller
{

    public $Status_work = [
                            '0' => 'Не ищу работу',
                            '1' => 'Ищу работу',
                            '2' => 'Не указано'
                          ];

    public function index(Request $request) {
        if ($request->ajax()) {
            $user = User::find($request->id);
            $request->offsetUnset('id');
            $user->update($request->all());
        }

        $status_work = Job_title::query()->active()->orderBy('name')->get();
        $users = User::with('jobtitles')->worker()->realuser();
        $all_worker = $users->count();

        $find_status_work = "";
        if (isset($request->status_work)) {
            $find_status_work = $request->status_work;

            if ($request->status_work > 0) {
                $users = $users->with('workers')->
                whereHas('workers',
                    function (Builder $query) use ($find_status_work) {
                        $query->where('position_work', $find_status_work);
                    }
                );
            } else {
                $users = $users->with('workers');
            }

        } else {
            $users = $users->with('workers');
        }

        $find_key = "";
        if (isset($request->find)) {
            $find_key = $request->find;
            $users = $users->where(function($query) use($find_key) {
                $query->Where('name_man', 'LIKE', "%$find_key%")
                    ->orWhere('email', 'LIKE', "%$find_key%")
                    ->orWhere('telephone', 'LIKE', "%$find_key%")
                    ->orWhere('surname', 'LIKE', "%$find_key%")
                    ->orWhere('surname2', 'LIKE', "%$find_key%");
            });
        }

        $users = $users->orderByDesc('id')->Realuser()->paginate(15);

        $status_wor = $this->Status_work;

        if ($request->ajax()) {
            return view('admin.worker.index_ajax', compact('users', 'status_wor'));
        } else {
            return view('admin.worker.index', compact('users',
                                                        'find_key',
                                                                    'find_status_work',
                                                                    'status_work',
                                                                    'status_wor',
                                                                    'all_worker'));
        }
    }

    public  function form_add_worker(User $user) {
        $job_titles = Job_title::query()->active()->orderBy('name')->get();

        $time_end_anketa = 'Создана только';
        $long_days = Company::find(1)->time_resume;
        $time_end_anketa = date("d.m.Y H:i:s", strtotime(Carbon::now() . "+$long_days days"));

        return view('admin.worker.add', compact('user', 'job_titles', 'time_end_anketa'));
    }

    public function form_store_worker(WorkerRequest $request, User $user) {
        $params = $request->all();
        $worker = Worker::create($params);
        return redirect()->route('admin.basedata');
    }

    public function form_edit_worker(Worker $worker) {
        $job_titles = Job_title::query()->active()->orderBy('name')->get();

        $time_end_anketa = 'Бессрочно';
        if (!empty($worker->updated_at)) {
            $long_days = Company::find(1)->time_resume;
            $time_end_anketa = date("d.m.Y H:i:s", strtotime($worker->updated_at . "+$long_days days"));
        }
        return view('admin.worker.edit', compact('worker', 'job_titles', 'time_end_anketa'));
    }

    public function form_update_worker(Request $request, Worker $worker)
    {
        $params = $request->all();

        $rules = [
            'email' => 'email|string|max:255',
            //'photo' => 'mimes:jpeg,jpg,png|max:15000',
        ];

        $messages = [
            'required' => 'Укажите обязательное поле «:attribute»',
            'confirmed' => 'Пароли не совпадают',
            'email' => 'Введите корректный email',
            'min' => [
                'string' => 'Поле «:attribute» должно быть не меньше :min символов',
                'file' => 'Файл «:attribute» должен быть не меньше :min Кбайт'
            ],
            'max' => [
                'string' => 'Поле «:attribute» должно быть не больше :max символов',
                'file' => 'Файл «:attribute» должен быть не больше :max Кбайт'
            ],
        ];

        $validator = Validator::make($params, $rules, $messages);

        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        } else {
            $user_id = $worker->user_id;
            if ($request->has('photo')) {
                if (!empty($worker->photo)) {
                    Storage::delete($worker->photo);
                }
                if (!empty($request->photo))
                   $params['photo'] = $request->file('photo')->store("workers/$user_id", 'public');
            }
            $worker->update($params);

            return redirect()->route('admin.workers');
        }
    }

    // кабинет - статистика работников
    public function static_workers(Request $request) {
        $stat = Static_worker::with('users');
            //->join('users', 'users.id', '=', 'static_workers.user_id');
        $users = User::query()->active()->OrderBy('id')->get();
        $periods = Static_worker::query()->distinct('year_month')->select('year_month')->get();
        if ($request->ajax()) {
            if (isset($request->user_id))
                if (!$request->user_id == "0")
                    $stat = $stat->Where('user_id', '=', $request->user_id);
            if (isset($request->year_month)) {
                if (!$request->year_month == "0")
                    $stat = $stat->Where('year_month', '=', $request->year_month);
            }
        }

        $stat = $stat->OrderByDesc('year_month');
            //->OrderBy('users.name');
        //OrderBy('users.name')->
        /*$stat->implode() loadMissing(['users' => function (Builder $query) {
            $query->orderBy('name', 'asc');
        }]);*/

        $stat = $stat->paginate(15);

        if ($request->ajax())
            return view('admin.static.index_workers_ajax', compact('stat'));
         else
            return view('admin.static.index_workers', compact('stat', 'users', 'periods'));

    }

}