IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/FinancialYearController.php

<?php

namespace App\Http\Controllers\School;

use Illuminate\Validation\Rule;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Auth;
use DB;

class FinancialYearController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        $fys = \App\FinancialYear::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idFinancialYear', 'desc')->get();
        $sessions = [ "" => "-- Select --" ]+\App\FinancialYear::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->orderBy('startDate','DESC')->get()->pluck('financialYearName', 'idFinancialYear')->toArray();
        return view('schools.financialyear.index', compact('fys','sessions'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $rules = [
            'financialYearName' => 'required|unique:financial_years,financialYearName,NULL,idFinancialYear,idSchool,' . Auth::guard('school')->user()->idSchool,
            'startDate' => 'required',
            'endDate' => 'required'
        ];
        $message = [
            'financialYearName.required' => 'Session Name  Must be Filled.',
            'financialYearName.unique' => 'Session Name Is Already Exist.',
        ];
        $this->Validate($request, $rules, $message);

        $fy = new \App\FinancialYear();
        $fy->fill($request->all());
        $fy->idSchool = Auth::guard('school')->user()->idSchool;
        $fy->save();

        //import data of feeheader,subjects,Periods
        if(isset($request->idFinancialYear)){
            $feeheads = \App\FeeHead::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $request->idFinancialYear)->get();
            foreach($feeheads as $feehead){
                $fromDate = Carbon::parse($feehead->fromDate)->addYear()->format('d-m-Y');
                $toDate = Carbon::parse($feehead->toDate)->addYear()->format('d-m-Y');
                $feehead->fromDate = $fromDate;
                $feehead->toDate = $toDate;
                $feehead->idFinancialYear =  $fy->idFinancialYear;
                $clone = $feehead->toArray();
                unset($clone['idFeehead']);
                if (DB::table('feeheads')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $clone['idFinancialYear'])
                ->where('feeheadName', '=', $clone['feeheadName'])->where('amount', '=', $clone['amount'])->whereDate('fromDate', '=', $clone['fromDate']->toDateString())->where('idClass', '=', $clone['idClass'])->where('idSection', '=', $clone['idSection'])->doesntExist())
                \App\FeeHead::insert($clone);
            }
            $subjects = \App\Subject::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=',$request->idFinancialYear)->get();
            foreach($subjects as $subject){
                $subject->idFinancialYear =  $fy->idFinancialYear;
                $clone = $subject->toArray();
                unset($clone['idSubject']);
                if (DB::table('subjects')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $clone['idFinancialYear'])
                ->where('subjectName', '=', $clone['subjectName'])->where('idClass', '=', $clone['idClass'])->doesntExist())
                \App\Subject::insert($clone);
            }
            $periods = \App\Period::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $request->idFinancialYear)->get();
            foreach($periods as $period){
                $period->idFinancialYear =  $fy->idFinancialYear;
                $clone = $period->toArray();
                unset($clone['idPeriod']);
                if (DB::table('periods')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $clone['idFinancialYear'])
                ->where('periodName', '=', $clone['periodName'])->where('idClass', '=', $clone['idClass'])->where('idSection', '=', $clone['idSection'])->doesntExist())
                \App\Period::insert($clone);
            }
        }
        
        return redirect('school/fys');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        $fys = \App\FinancialYear::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idFinancialYear', 'desc')->get();
        $fy = \App\FinancialYear::where('idFinancialYear', '=', $id)->first();
        $sessions = [ "" => "-- Select --" ]+\App\FinancialYear::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->orderBy('startDate','DESC')->get()->pluck('financialYearName', 'idFinancialYear')->toArray();
        return view('schools.financialyear.index', compact('fy', 'fys','sessions'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        $fy = \App\FinancialYear::where('idFinancialYear', '=', $id)->first();
        $rules = [
            'financialYearName' => 'required|unique:financial_years,financialYearName,' . $id . ',idFinancialYear,idSchool,' . Auth::guard('school')->user()->idSchool,
            'startDate' => 'required|date',
            'endDate' => 'required|date'
        ];
        $message = [
            'financialYearName.required' => 'Session Name Must be Filled.',
        ];
        $this->Validate($request, $rules, $message);
        $fy->fill($request->all());
        $fy->update();

        if(isset($request->idFinancialYear)){
            $feeheads = \App\FeeHead::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $request->idFinancialYear)->get();
            $data = array();
            $records = 0;
            foreach($feeheads as $feehead){
                $fromDate = Carbon::parse($feehead->fromDate)->addYear()->format('d-m-Y');
                $toDate = Carbon::parse($feehead->toDate)->addYear()->format('d-m-Y');
                $feehead->fromDate = $fromDate;
                $feehead->toDate = $toDate;
                $feehead->idFinancialYear =  $fy->idFinancialYear;
                $clone = $feehead->toArray();
                unset($clone['idFeehead']);
                if (DB::table('feeheads')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $clone['idFinancialYear'])
                ->where('feeheadName', '=', $clone['feeheadName'])->where('amount', '=', $clone['amount'])->whereDate('fromDate', '=', $clone['fromDate']->toDateString())->where('idClass', '=', $clone['idClass'])->where('idSection', '=', $clone['idSection'])->doesntExist())
                {
                    \App\FeeHead::insert($clone);
                }else{
                    if($feehead->studentCategory != null) {
                        if (DB::table('feeheads')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $clone['idFinancialYear'])
                        ->where('feeheadName', '=', $clone['feeheadName'])->where('studentCategory', '=', $clone['studentCategory'])->where('amount', '=', $clone['amount'])->whereDate('fromDate', '=', $clone['fromDate']->toDateString())->where('idClass', '=', $clone['idClass'])->where('idSection', '=', $clone['idSection'])->doesntExist())
                        {
                            \App\FeeHead::insert($clone);
                        }
                    }
                }
            }
            $subjects = \App\Subject::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=',$request->idFinancialYear)->get();
            foreach($subjects as $subject){
                $subject->idFinancialYear =  $fy->idFinancialYear;
                $clone = $subject->toArray();
                unset($clone['idSubject']);
                if (DB::table('subjects')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $clone['idFinancialYear'])
                ->where('subjectName', '=', $clone['subjectName'])->where('idClass', '=', $clone['idClass'])->doesntExist())
                \App\Subject::insert($clone);
            }
            $periods = \App\Period::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $request->idFinancialYear)->get();
            foreach($periods as $period){
                $period->idFinancialYear =  $fy->idFinancialYear;
                $clone = $period->toArray();
                unset($clone['idPeriod']);
                if (DB::table('periods')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->where('idFinancialYear', '=', $clone['idFinancialYear'])
                ->where('periodName', '=', $clone['periodName'])->where('idClass', '=', $clone['idClass'])->where('idSection', '=', $clone['idSection'])->doesntExist())
                \App\Period::insert($clone);
            }
        }

        return redirect('school/fys');
    }

    /**
     * Check There is Any dependency Exist
     *

     */
    public function deletefys($id) {
        //
        $fys = \App\FinancialYear::where('idFinancialYear', '=', $id)->first();
        $schact = \App\SchemeActivation::where('idUnit', '=', $id)->get();
        if ($schact->count() > 0) {
            return redirect()->back()->with('message', 'You Can not Delete this Financial Year Because it is Already in Use!');
        } else {
            return view('financialyear.delete', compact('fys'));
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $fy = \App\FinancialYear::where('idFinancialYear', '=', $id)->first();
        $fy->delete();
        return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
    }

    public function stateList() {
        $states = \App\State::orderBy('idState')->get();
        return view('schools.state_list', compact('states'));
    }

    public function bankList() {
        $banks = \App\Bank::orderBy('idBank')->get();
        return view('schools.bank_list', compact('banks'));
    }

}

Copyright © 2021 - 2025 IMMREX7