HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/ipsremont-demo/app/Repository/Branch/BranchRepository.php
<?php


namespace App\Repository\Branch;


use App\Http\Requests\Branch\IndexRequest;
use App\Models\Branch;
use App\Repository\BaseRepository;
use App\Traits\GridTrait;

class BranchRepository extends BaseRepository
{

    use GridTrait;

    /**
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function getQuery()
    {
        return Branch::query();
    }

    /**
     * @param IndexRequest $request
     *
     * @return mixed
     */
    public function search(IndexRequest $request)
    {
        $query = self::getAvailable();

        $this->makeQueryOrdered(Branch::class, $query, $request);

        if (isset($request->name) && !empty($request->name)) {
            $query->where('name', 'like', '%' . $request->name . '%');
        }

        $perPage = $request->per_page ?? config('crud.defaultPerPage');

        return $query->paginate((int) $perPage)->appends($request->all());
    }

    /**
     * @param $id
     */
    public static function delete($id)
    {
        self::getById($id)->delete();
    }

    public function getCentral()
    {
        $query = Branch::query()->where('central', 1);

        return $query->first();
    }

    public static function getByCode($code)
    {
        return Branch::query()->where('external_id', $code)->first();
    }

    public function updateCentral($model)
    {
        $model->refresh();
        if ($model->central) {
            Branch::query()->where('id', '!=', $model->id)
                ->where('central', 1)
                ->update(['central' => 0]);
        }
    }

    public static function fillStorage($external_id, $name)
    {
        $model = Branch::query()->where('external_id', $external_id)->withTrashed()->first();
        if (!$model) {
            $model = new Branch();
            $model->external_id = $external_id;
            $model->name = $name;
            $model->display = 1;
            $model->save();
        } else {
            $model->name = $name;
            $model->deleted_at = null;
            $model->update();
        }
    }

    public static function getBranchIdsByRegion(int $regionId): array
    {
        return Branch::query()->where('region_id', $regionId)->pluck('id')->toArray();
    }

}