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/QoutaController.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 Illuminate\Http\Request;

class QoutaController extends Controller
{
    public function qouta_index()
    {
        $qoutas = Qouta::with('user')->latest()->get();
        $seasons = Season::latest();

        return view('role_wise.super_admin.pages.qouta.index', compact('qoutas', 'seasons'));
    }


    public function qouta_create()
    {
        $today = now()->toDateString();
        return view('role_wise.super_admin.pages.qouta.create', [
            'agences' => CompanyInfo::all(),
            'seasons' => Season::where('end_date', '>=', $today)->get(),
        ]);
    }

    public function qoute_store(Request $request)
    {
        // Validate all incoming data
        $validated = $request->validate([
            'user_id' => 'required|integer|exists:users,id',
            'agency_id' => 'required|integer|exists:company_infos,id',
            'season_id' => 'required|integer|exists:seasons,id',
            'makka_qouta' => 'required|integer|min:1',
            'modina_qouta' => 'required|integer|min:1',
        ]);
        $duplicate = Qouta::where('agency_id', $validated['agency_id'])
            ->where('season_id', $validated['season_id'])
            ->first();
        if ($duplicate) {
            return redirect()->back()
                ->withInput()
                ->with(['error' => 'A quota for this agency and season already exists.']);
        }

        Qouta::create([
            'user_id' => $validated['user_id'],
            'agency_id' => $validated['agency_id'],
            'season_id' => $validated['season_id'],
            'makka_qouta' => $validated['makka_qouta'],
            'modina_qouta' => $validated['modina_qouta'],
        ]);

        return redirect()
            ->back()
            ->with('success', 'Quota created successfully.');
    }

    public function qouta_edit($id)
    {
        $today = now()->toDateString();
        $qouta = Qouta::findOrFail($id);
        $agences = CompanyInfo::all();
        $seasons = Season::where('end_date', '>=', $today)->get();

        return view('role_wise.super_admin.pages.qouta.update', compact('qouta', 'agences', 'seasons'));
    }

    public function qouta_update(Request $request, $id)
    {
        $request->validate([
            'user_id' => 'required|integer|exists:users,id',
            'agency_id' => 'required|integer|exists:company_infos,id',
            'season_id' => 'required|integer|exists:seasons,id',
            'makka_qouta' => 'required|integer|min:1',
            'modina_qouta' => 'required|integer|min:1',
        ]);

        $qouta = Qouta::findOrFail($id);
        $newMakkaQouta = $request->makka_qouta;
        $newModinaQouta = $request->modina_qouta;

        // ---------- Makka Quota Logic ----------
        // $oldMakkaQouta = $qouta->makka_qouta;
        // $oldMakkaExtraQouta = $qouta->makka_extra_qouta;

        // if ($newMakkaQouta > $oldMakkaQouta) {
        //     if (! ($qouta->makka_used_qouta === 0 && $oldMakkaExtraQouta === 0)) {
        //         $diff = $newMakkaQouta - $oldMakkaQouta;
        //         $newExtraQouta = max(0, $oldMakkaExtraQouta - $diff);
        //         if ($qouta->makka_used_qouta >= $oldMakkaQouta) {
        //             $qouta->makka_used_qouta += ($oldMakkaExtraQouta - $newExtraQouta);
        //         }
        //         $qouta->makka_extra_qouta = $newExtraQouta;
        //     }
        // }

        // ---------- Modina Quota Logic ----------
        // $oldModinaQouta = $qouta->modina_qouta;
        // $oldModinaExtraQouta = $qouta->modina_extra_qouta;

        // if ($newModinaQouta > $oldModinaQouta) {
        //     if (! ($qouta->modina_used_qouta === 0 && $oldModinaExtraQouta === 0)) {
        //         $diff = $newModinaQouta - $oldModinaQouta;
        //         $newExtraQouta = max(0, $oldModinaExtraQouta - $diff);
        //         if ($qouta->modina_used_qouta >= $oldModinaQouta) {
        //             $qouta->modina_used_qouta += ($oldModinaExtraQouta - $newExtraQouta);
        //         }
        //         $qouta->modina_extra_qouta = $newExtraQouta;
        //     }
        // }

        // Update other fields
        $qouta->user_id = $request->user_id;
        $qouta->agency_id = $request->agency_id;
        $qouta->season_id = $request->season_id;
        $qouta->makka_qouta = $newMakkaQouta;
        $qouta->modina_qouta = $newModinaQouta;
        $qouta->save();

        return redirect()->back()->with('success', 'Quota updated successfully.');
    }

    public function qouta_destroy($id)
    {
        $qouta = Qouta::findOrFail($id);
        $qouta->delete();
        return redirect()->back()->with('success', 'Quota and related files deleted successfully.');
    }
}