HEX
Server: LiteSpeed
System: Linux s3.sitechai.com 4.18.0-553.51.1.lve.1.el8.x86_64 #1 SMP Wed May 14 14:34:57 UTC 2025 x86_64
User: workzeni (2217)
PHP: 8.1.32
Disabled: mail, show_source, system, shell_exec, passthru, exec, eval, shell
Upload Files
File: /home/workzeni/agency-erp-05.workzenix.com/app/Http/Controllers/Module/AgencyController.php
<?php

namespace App\Http\Controllers\Module;

use App\Http\Controllers\Controller;
use App\Models\CompanyInfo;
use App\Models\Qouta;
use App\Models\Season;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class AgencyController extends Controller
{
    public function agency_index()
    {
        $agencies = CompanyInfo::all();
        $userCount = User::whereIn('company_id', $agencies->pluck('id'))->count();

        return view('role_wise.super_admin.pages.agency.index', [
            'agencies' => $agencies,
            'users' => $userCount,
        ]);
    }

    public function agency_active(Request $request, $id)
    {
        $agency = CompanyInfo::findOrFail($id);

        // Block status change for 'Taiba Alliance'
        if (trim($agency->name) === 'Taiba Alliance') {
            return response()->json([
                'success' => false,
                'message' => 'Status change is not allowed for Taiba Alliance.',
            ], 403);
        }

        $agency->status = $request->status ?? 0;
        $agency->save();

        return response()->json([
            'success' => true,
            'message' => 'Agency status updated',
            'new_status' => $agency->status,
        ]);
    }

    // public function agency_show($id)
    // {
    //     $agency = CompanyInfo::findOrFail($id);

    //     return view('role_wise.super_admin.pages.agency.show', compact('agency'));
    // }

    public function agency_create()
    {
        $today = now()->toDateString();
        $seasons = Season::where('end_date', '>=', $today)
            // ->where('start_date', '<=', $today)
            ->latest()
            ->get();

        return view('role_wise.super_admin.pages.agency.create', compact('seasons'));
    }

    public function agency_store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'logo' => 'required|image|mimes:jpeg,png,jpg|max:2048',
            'season_id' => 'required|numeric',
            'makka_qouta' => 'required|numeric',
            'modina_qouta' => 'required|numeric',
        ]);

        $companyName = Str::slug($request->name);
        $uploadPath = public_path("/uploads_file/{$companyName}/logo");
        if (! file_exists($uploadPath)) {
            mkdir($uploadPath, 0755, true);
        }

        $timestamp = now()->format('ymdHis');
        $extension = $request->file('logo')->getClientOriginalExtension();
        $logoName = "logo_{$timestamp}.".$extension;
        $request->file('logo')->move($uploadPath, $logoName);
        $relativePath = "/uploads_file/{$companyName}/logo/{$logoName}";

        $company = new CompanyInfo;
        $company->name = $request->name;
        $company->logo = $relativePath;
        $company->status = true;
        $company->save();

        // Qouta
        $qouta = new Qouta;
        $qouta->user_id = auth()->user()->id;
        $qouta->agency_id = $company->id;
        $qouta->season_id = $request->season_id;
        $qouta->makka_qouta = $request->makka_qouta;
        $qouta->modina_qouta = $request->modina_qouta;
        $qouta->save();

        return redirect()->back()->with('success', 'Agency created successfully!');
    }

    public function agency_edit($id)
    {
        $agency = CompanyInfo::findOrFail($id);

        return view('role_wise.super_admin.pages.agency.update', compact('agency'));
    }

    public function agency_update(Request $request, $id)
    {
        $agency = CompanyInfo::findOrFail($id);
        $request->validate([
            'name' => 'required|string|max:255',
            'logo' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
        ]);

        $oldCompanyName = Str::slug($agency->name);
        $newCompanyName = Str::slug($request->name); // New slug from input

        // Paths for old and new folders
        $oldPath = public_path("/uploads_file/{$oldCompanyName}");
        $newPath = public_path("/uploads_file/{$newCompanyName}");

        // Check if name has changed and old folder exists
        if ($oldCompanyName !== $newCompanyName && file_exists($oldPath)) {
            // Rename the folder
            rename($oldPath, $newPath);
        }

        $agency->name = $request->name;

        $destinationPath = $newPath.'/logo';

        if ($request->hasFile('logo')) {
            // Delete old logo if exists
            if ($agency->logo && file_exists($destinationPath.'/'.$agency->logo)) {
                unlink($destinationPath.'/'.$agency->logo);
            }

            $file = $request->file('logo');
            $filename = time().'.'.$file->getClientOriginalExtension();

            if (! file_exists($destinationPath)) {
                mkdir($destinationPath, 0755, true);
            }

            $file->move($destinationPath, $filename);
            $agency->logo = "/uploads_file/{$newCompanyName}/logo/$filename";
        }

        $agency->save();

        return redirect()->back()->with('success', 'Agency updated successfully!');
    }

    public function agency_destroy($id)
    {
        // Find the
        $agency = CompanyInfo::findOrFail($id);
        $companyName = $agency->name;
        $logoPath = public_path("/uploads_file/{$companyName}/logo/{$agency->logo}");
        if (File::exists($logoPath)) {
            File::delete($logoPath);
        }
        $imagePath = public_path("/uploads_file/{$companyName}/image/{$agency->image}");
        if (File::exists($imagePath)) {
            File::delete($imagePath);
        }
        $agency->delete();

        return redirect()->back()->with('success', 'Agency and related files deleted successfully.');
    }

    // Manage Agency Profile
    public function manage_profile($id)
    {
        $agency = CompanyInfo::find($id);

        if (! $agency) {
            abort(404, 'Agency not found.');
        }

        return view('admin.agency.profile', compact('agency'));
    }

    public function update_profile(Request $request, $id)
    {
        $agency = CompanyInfo::findOrFail($id);

        // Validate inputs matching your blade form
        $request->validate([
            'name' => 'required|string|max:255',
            'owner_name' => 'required|string|max:255',
            'address' => 'required|string',
            'hajj_license' => 'required|string|max:255',
            'phone' => 'required|string|max:20',
            'email' => 'required|email|max:255',
            'website' => 'nullable|url|max:255',
            'logo' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
            'hajj_license_file' => 'nullable|file|mimes:pdf,jpeg,jpg,png|max:5120',
        ]);

        $oldCompanyName = Str::slug($agency->name);
        $newCompanyName = Str::slug($request->name);

        $oldPath = public_path("/uploads_file/{$oldCompanyName}");
        $newPath = public_path("/uploads_file/{$newCompanyName}");

        // Rename folder if company name changed and old folder exists
        if ($oldCompanyName !== $newCompanyName && file_exists($oldPath)) {
            rename($oldPath, $newPath);
        }

        // Update basic fields
        $agency->name = $request->name;
        $agency->owner_name = $request->owner_name;
        $agency->address = $request->address;
        $agency->hajj_license = $request->hajj_license;
        $agency->phone = $request->phone;
        $agency->email = $request->email;
        $agency->website = $request->website;

        // Handle logo upload
        if ($request->hasFile('logo')) {
            $logoPath = $newPath.'/logo';

            // Delete old logo if exists
            if ($agency->logo && file_exists(public_path($agency->logo))) {
                unlink(public_path($agency->logo));
            }

            if (! file_exists($logoPath)) {
                mkdir($logoPath, 0755, true);
            }

            $file = $request->file('logo');
            $filename = time().'.'.$file->getClientOriginalExtension();
            $file->move($logoPath, $filename);

            $agency->logo = "/uploads_file/{$newCompanyName}/logo/{$filename}";
        }

        // Handle hajj_license_file upload
        if ($request->hasFile('hajj_license_file')) {
            $licensePath = $newPath.'/hajj_license';

            // Delete old license file if exists
            if ($agency->hajj_license_file && file_exists(public_path($agency->hajj_license_file))) {
                unlink(public_path($agency->hajj_license_file));
            }

            if (! file_exists($licensePath)) {
                mkdir($licensePath, 0755, true);
            }

            $file = $request->file('hajj_license_file');
            $filename = time().'.'.$file->getClientOriginalExtension();
            $file->move($licensePath, $filename);

            $agency->hajj_license_file = "/uploads_file/{$newCompanyName}/hajj_license/{$filename}";
        }

        $agency->save();

        return redirect()->back()->with('success', 'Agency profile updated successfully!');
    }

    public function agency_loginAs($agency_id)
    {
        if (! in_array(Auth::user()->role, [1, 2, 3])) {
            abort(403, 'Unauthorized action.');
        }
        $agency = CompanyInfo::where('id', $agency_id)->where('status', 1)->first();
        if (! $agency) {
            return redirect()->back()->with('error', 'This agency is inactive or does not exist.');
        }
        $user = User::where('company_id', $agency_id)->where('status', 1)->first();
        if (! $user) {
            return redirect()->back()->with('error', 'No active user found for this agency.');
        }
        Auth::logout();
        Auth::login($user);

        // Redirect based on user role
        switch ($user->role) {
            case 1:
                return redirect()->route('admin.dashboard');
            case 2:
                return redirect()->route('manager.dashboard');
            case 3:
                return redirect()->route('account.dashboard');
            case 4:
                return redirect()->route('user.dashboard');
            default:
                return redirect()->route('login');
        }
    }
}