Yohohohohohooho | Sanrei Aya
Sanrei Aya


Server : LiteSpeed
System : Linux barito.iixcp.rumahweb.net 5.14.0-611.49.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Apr 21 16:39:08 EDT 2026 x86_64
User : elvh3918 ( 1528)
PHP Version : 8.2.31
Disable Function : mail
Directory :  /home/elvh3918/public_html/pmm/app/Http/Controllers/Master/Setting/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/elvh3918/public_html/pmm/app/Http/Controllers/Master/Setting/ServiceTypeController.php
<?php

namespace App\Http\Controllers\Master\Setting;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use App\Models\Master\Tenant;
use App\Models\Master\Setting\ServiceType;
use Illuminate\Support\Facades\Auth;

class ServiceTypeController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $breadcrumbs = [
            ['link' => "dashboard", 'name' => "Dashboard"], ['link' => "master.setting.service-type.index", 'name' => "Service Type"]
        ];

        $data_input = [
            'tenant_id' => Request::input('tenant_id'),
        ];

        $query = ServiceType::with('tenant');
        $tenant = Auth::user()->load('model');

        if ($tenant->model_type == 'App\Models\Master\Tenant') {
            $tenant = Tenant::where('id', '=', Auth::user()->model_id)->orderBy('name', 'asc')->get();
            $query = $query->whereRelation('tenant', 'id', '=', Auth::user()->model_id)->orderBy('tenant_id', 'asc')->orderBy('name', 'asc')->get();
        } else if ($tenant->model_type == 'App\Models\Bussiness') {
            $tenant = Tenant::orderBy('name', 'asc')->get();
            if ($data_input['tenant_id'] == 0) {
                $query = $query->orderBy('tenant_id', 'asc')->orderBy('name', 'asc')->get();
            } else {
                $query = $query->whereRelation('tenant', 'id', '=', $data_input['tenant_id'])->orderBy('tenant_id', 'asc')->orderBy('name', 'asc')->get();
            }
        }

        return view('content.master.setting.service_type.index', compact('query', 'tenant', 'data_input'), ['breadcrumbs' => $breadcrumbs]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $breadcrumbs = [
            ['link' => "dashboard", 'name' => "Dashboard"], ['link' => "master.setting.service-type.index", 'name' => "Service Type"], ['link' => "master.setting.service-type.create", 'name' => "Create Service Type"]
        ];

        $tenant = Auth::user()->load('model');
        if ($tenant->model_type == 'App\Models\Master\Tenant') {
            $tenant = Tenant::where('id', '=', Auth::user()->model_id)->orderBy('name', 'asc')->get();
        } else if ($tenant->model_type == 'App\Models\Bussiness') {
            $tenant = Tenant::orderBy('name', 'asc')->get();
        }

        return view('content.master.setting.service_type.create', compact('tenant'), ['breadcrumbs' => $breadcrumbs]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $user = Auth::user()->load('model');
        if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != Request::get('tenant_id')) {
            return redirect()->route('master.setting.service-type.create')->withInput()->with('message', 'system blocked, wrong id');
        }

        $validator = Validator::make(Request::all(), [
            'name' => ['required', 'string', 'max:100'],
        ]);

        if ($validator->fails()) {
            return redirect()->route('master.setting.service-type.create')->withErrors($validator)->withInput()->with('message', 'input failed');
        }

        DB::beginTransaction();
        try {
            ServiceType::create([
                'name' => Request::get('name'),
                'description' => Request::get('description'),
                'tenant_id' => Request::get('tenant_id'),
                'is_active' => Request::get('is_active') != null ? 1: 0,
            ]);
    
            DB::commit();
            return redirect()->route('master.setting.service-type.index')->with('message', 'store success');
        } catch (\Exception $e) {
            DB::rollBack();

            return redirect()->route('master.setting.service-type.create')->withErrors($validator)->withInput()->with('message', 'store error');
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(ServiceType $type)
    {
        $user = Auth::user()->load('model');
        if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != $type->tenant_id) {
            return redirect()->route('master.setting.service-type.index')->with('message', 'system blocked, wrong id');
        }

        $breadcrumbs = [
            ['link' => "dashboard", 'name' => "Dashboard"], ['link' => "master.setting.service-type.index", 'name' => "Service Type"], ['link' => "master/ServiceType/edit/$type->id", 'name' => "Edit Service Type"]
        ];

        $query = [
            'id' => $type->id,
            'name' => $type->name,
            'description' => $type->description,
            'tenant_id' => $type->load('tenant'),
            'is_active' => $type->is_active,
            'updated_at' => $type->updated_at,
        ];

        $tenant = Auth::user()->load('model');
        if ($tenant->model_type == 'App\Models\Master\Tenant') {
            $tenant = Tenant::where('id', '=', Auth::user()->model_id)->orderBy('name', 'asc')->get();
        } else if ($tenant->model_type == 'App\Models\Bussiness') {
            $tenant = Tenant::orderBy('name', 'asc')->get();
        }

        return view('content.master.setting.service_type.edit', compact('query', 'tenant'), ['breadcrumbs' => $breadcrumbs]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, ServiceType $type)
    {
        $user = Auth::user()->load('model');
        if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != $type->tenant_id) {
            return redirect()->route('master.setting.service-type.edit', $type->id)->with('message', 'system blocked, wrong id');
        }

        $validator = Validator::make(Request::all(), [
            'name' => ['required', 'string', 'max:100'],
        ]);

        if ($validator->fails()) {
            return redirect()->route('master.setting.service-type.edit', $type->id)->withErrors($validator)->withInput()->with('message', 'input failed');
        }

        DB::beginTransaction();
        try {
            $type->update([
                'name' => Request::get('name'),
                'description' => Request::get('description'),
                'tenant_id' => Request::get('tenant_id'),
                'is_active' => Request::get('is_active') != null ? 1: 0,
            ]);
    
            DB::commit();
            return redirect()->route('master.setting.service-type.index')->with('message', 'update success');
        } catch (\Exception $e) {
            DB::rollBack();

            return redirect()->route('master.setting.service-type.edit', $type->id)->withErrors($validator)->withInput()->with('message', 'update error');
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(ServiceType $type)
    {
        $user = Auth::user()->load('model');
        if ($user->model_type == 'App\Models\Master\Tenant' && $user->model->id != $type->tenant_id) {
            return redirect()->route('master.setting.service-type.index')->with('message', 'system blocked, wrong id');
        }
        
        $type->delete();
        return redirect()->route('master.setting.service-type.index')->with('message', 'delete success');
    }
}

Yohohohohohooho | Sanrei Aya