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/FeeHeadController.php

<?php

namespace App\Http\Controllers\School;

use Illuminate\Http\Request;
use Auth;
use DB;
use Session;

class FeeHeadController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $search_classes = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();

        $feeheads = \App\FeeHead::leftJoin('school_users', 'feeheads.created_by', '=', 'school_users.idSchoolUser')->leftJoin('school_users as updater', 'feeheads.updated_by', '=', 'updater.idSchoolUser')->where('feeheads.idSchool', '=', Auth::guard('school')->user()->idSchool)
                // ->where('studentCategory','=',$request->studentCategory)
                ->orderBy('idFeehead', 'desc');
        
        if ($request->has('studentCategory')) {
            if($request->studentCategory != "all")
            $feeheads = $feeheads->where('studentCategory', $request->studentCategory);
        } 
        if ($request->has('sections') && count($request->sections) > 0) {
            $feeheads = $feeheads->whereIn('idSection', $request->sections);
        } 
        
        if ($request->has('classes') && count($request->classes) > 0) {
            $feeheads = $feeheads->whereIn('idClass', $request->classes);
        } 
        
        $feeheads = $feeheads->where('idFinancialYear', '=', Session::get('idFinancialYear'))->select('feeheads.*','school_users.name as collector','updater.name as updater_name')->get();
        $categories = \App\StudentCategoryModel::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('categoryName', 'categoryName')->toArray();
        return view('schools.feeheads.index', compact('feeheads', 'classes', 'categories', 'search_classes'));
    }


    public function fetchFeehead(Request $request){
        /*
        
        'processing': true,
        'serverSide': true,
        'serverMethod': 'post',
        'ajax': {
            'url':'feeheader',
            'beforeSend': function (request) {
                request.setRequestHeader("X-CSRF-TOKEN", "{{csrf_token()}}");
            }
        },
        'columns': [
            { data: 'row' },
            { data: 'class' },
            { data: 'ecno' },
            { data: 'section' },
            { data: 'category' },
            { data: 'feeheadName' },
            { data: 'amount' },
            { data: 'duration' },
            { data: 'fine' },
            { data: 'flatFine' },
            { data: 'description' },
            { data: 'collector' },
            { data: 'action' },
        ],
        
        $draw = $_POST['draw'];
        $row = $_POST['start'];
        $rowperpage = $_POST['length']; 
        $columnIndex = $_POST['order'][0]['column']; 
        $columnName = $_POST['columns'][$columnIndex]['data']; 
        $columnSortOrder = $_POST['order'][0]['dir'];
        $searchValue = $request->input('search.value');

        ## Total number of records without filtering
        $totalRecords = \App\FeeHead::select('idFeehead')->where('idFinancialYear', '=', Session::get('idFinancialYear'))
        ->where('feeheads.idSchool', '=', Auth::guard('school')->user()->idSchool)->count();
        
        ## Total number of record with filtering
        $totalRecordwithFilter = \App\FeeHead::select('idFeehead')->where('idFinancialYear', '=', Session::get('idFinancialYear'))->select('feeheads.*')
        ->where('feeheads.idSchool', '=', Auth::guard('school')->user()->idSchool)
        ->orWhere('feeheadName','LIKE','%'.$searchValue.'%')
        ->orWhere('amount','LIKE','%'.$searchValue.'%')->count();

        
        $feeheads = \App\FeeHead::leftJoin('school_users', 'feeheads.created_by', '=', 'school_users.idSchoolUser')
             ->leftJoin('students', 'feeheads.idStudent', '=', 'students.idStudent')
             ->leftJoin('classes', 'feeheads.idClass', '=', 'classes.idClass')
             ->leftJoin('sections', 'feeheads.idSection', '=', 'sections.idSection')
             ->select('feeheads.*','school_users.name as collector',DB::raw('IFNULL(students.ecNo,"") as ecNo'),DB::raw('IFNULL(classes.className,"") as className'),DB::raw('IFNULL(sections.sectionName,"") as sectionName'))
             ->where('feeheads.idFinancialYear', '=', Session::get('idFinancialYear'))
             ->where('feeheads.idSchool', '=', Auth::guard('school')->user()->idSchool);
             
        if($searchValue != ''){
            $feeheads = $feeheads->orWhere('feeheadName','LIKE','%'.$searchValue.'%')
             ->orWhere('students.ecNo','LIKE','%'.$searchValue.'%')
             ->orWhere('className','LIKE','%'.$searchValue.'%')
             ->orWhere('description','LIKE','%'.$searchValue.'%')
             ->orWhere('sectionName','LIKE','%'.$searchValue.'%')
             ->orWhere('amount','LIKE','%'.$searchValue.'%');
        }

        ## Fetch records
        if($columnName == "feeheadName" || $columnName == "fine" || $columnName == "amount" || $columnName == "flatFine" || $columnName == "description")
        $feeheads = $feeheads->orderBy($columnName,$columnSortOrder)->take($rowperpage)->skip($row)->get();
        else if($columnName == "duration")
        $feeheads = $feeheads->orderBy('fromDate',$columnSortOrder)->take($rowperpage)->skip($row)->get();
        else if($columnName == "ecno")
        $feeheads = $feeheads->orderBy('idStudent',$columnSortOrder)->take($rowperpage)->skip($row)->get();
        else if($columnName == "class")
        $feeheads = $feeheads->orderBy('className',$columnSortOrder)->take($rowperpage)->skip($row)->get();
        else if($columnName == "section")
        $feeheads = $feeheads->orderBy('sectionName',$columnSortOrder)->take($rowperpage)->skip($row)->get();
        else $feeheads = $feeheads->take($rowperpage)->skip($row)->get();


        $data = array();
        $k = 0;
        
        foreach($feeheads as $value) {
           $k++; 
           array_push($data, [
              "row"=>$k,
              "class"=>$value->className,
              "ecno"=> $value->ecNo,
              "section"=>$value->sectionName,
              "category"=>$value->studentCategory,
              "feeheadName"=>$value->feeheadName,
              "amount"=>$value->amount,
              "duration"=>$value->fromDate.' to '.$value->toDate,
              "fine"=>$value->fine,
              "flatFine"=>$value->flatFine,
              "description"=>$value->description,
              "collector"=>$value->collector,
              "action"=> '<a href="feeheads/'. $value->idFeehead .'/edit"  class="btn btn-raised btn-info waves-effect btn-round">Edit </a>
              <button class="btn btn-raised btn-danger waves-effect btn-round js-sweetalert" data-id="{{$value->idFeehead}}" data-type="confirm">DELETE</button>',
           ]);
        }
        
        ## Response
        $response = array(
          "draw" => intval($draw),
          "iTotalRecords" => $totalRecords,
          "iTotalDisplayRecords" => $totalRecordwithFilter,
          "aaData" => $data
        );
        
        return json_encode($response);*/
    }

    /**
     * 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) {
        //dd($request->all());
        $rules = [
            'idClass' => 'required',
            'feeheadName' => 'required',
            'fromDate' => 'required|date',
            'toDate' => 'required|date',
            'amount' => 'required',
//            'ecNO' => 'required'
        ];

        if (count($request->sections) == 0) {
            $rules += ['idSection' => 'required'];
        }
        if (count($request->studentCategory) == 0) {
            $rules += ['studentCategory' => 'required'];
        }
        $messages = [
            'idClass.required' => 'Class must be selected.',
            'idSection.required' => 'Section must be selected.',
            'studentCategory.required' => 'Category must be selected.'
        ];
        $this->Validate($request, $rules, $messages);
        if ($request->has('students')) {
            foreach ($request->students as $key => $value) {
                $std = \App\AdmEntry::where('idStudent', '=', $value)->first();
                $feehead = new \App\FeeHead();
                $feehead->fill($request->all());
                $feehead->idStudent = $value;
                $feehead->idFinancialYear = Session::get('idFinancialYear');
                $feehead->idSection = $std->idSection;
                $feehead->studentCategory = $std->studentType;
                if ($request->fines == "per")
                    $feehead->fine = $request->fine;
                else {
                    $feehead->fine = 0.0;
                    $feehead->flatFine = $request->flatFine;
                }
                $feehead->idSchool = Auth::guard('school')->user()->idSchool;
                $feehead->created_by = Auth::guard('school')->user()->idSchoolUser;
                $feehead->save();
            }
        } else {
            foreach ($request->sections as $key1 => $value1) {
                if ($request->has('studentCategory')) {
                    foreach ($request->studentCategory as $key => $value) {
                        $feehead = new \App\FeeHead();
                        $feehead->fill($request->all());
                        $feehead->idSection = $value1;
                        $feehead->idSchool = Auth::guard('school')->user()->idSchool;
                        $feehead->idFinancialYear = Session::get('idFinancialYear');
                        $feehead->studentCategory = $value;
                        if ($request->fines == "per")
                            $feehead->fine = $request->fine;
                        else {
                            $feehead->fine = 0.0;
                            $feehead->flatFine = $request->flatFine;
                        }
                        $feehead->created_by = Auth::guard('school')->user()->idSchoolUser;
                        $feehead->save();
                    }
                } else {
                    $feehead = new \App\FeeHead();
                    $feehead->fill($request->all());
                    $feehead->idSection = $value1;
                    $feehead->idSchool = Auth::guard('school')->user()->idSchool;
                    $feehead->idFinancialYear = Session::get('idFinancialYear');
                    if ($request->fines == "per")
                        $feehead->fine = $request->fine;
                    else {
                        $feehead->fine = 0.0;
                        $feehead->flatFine = $request->flatFine;
                    }
                    $feehead->created_by = Auth::guard('school')->user()->idSchoolUser;
                    $feehead->save();
                }
            }
        }
        flash('Fee-Header has been saved successfully !!');
        if ($request->ajax()) {
            return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
        }
        return redirect('school/feeheads');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        /*$feeheads = \App\FeeHead::where('idFinancialYear','195')->whereNotNull('idStudent')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->get();
        foreach( $feeheads as  $feehead){
            $students = \App\AdmEntry::select('idFinancialYear')->where('idStudent', '=', $feehead->idStudent)->first();
            if($students -> idFinancialYear != $feehead->idFinancialYear){
                $findStudent = \App\AdmEntry::select('idStudent','idClass','ecNo')->where('ecNo','LIKE','%'.$feehead->ecNO)->where('idClass', '=', $feehead->idClass)->where('idFinancialYear', '=', $feehead->idFinancialYear)->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
                if($findStudent != null){
                    $students->feehead = $feehead;
                    $students->correct = $findStudent;

                    $wfeehead = \App\FeeHead::findOrFail($feehead->idFeehead);
                    $wfeehead->idStudent = $findStudent->idStudent;
                    $wfeehead->update();

                    $students->correctFeehead = $wfeehead;
                    //return json_encode($students);
                }
                //return json_encode($students);
            }
        }*/
        $feehead = \App\FeeHead::findOrFail($id);
        return json_encode($feehead);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        $search_classes = \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $feeheads = \App\FeeHead::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                        ->orderBy('idFeehead', 'desc')->get();
        $feehead = \App\FeeHead::findOrFail($id);
        $sections = \App\Section::where('idClass', '=', $feehead->idClass)->get()->pluck('sectionName', 'idSection')->toArray();
        $students = \App\AdmEntry::where('idSection', '=', $feehead->idSection)->where('idFinancialYear', '=', Session::get('idFinancialYear'))->get()->pluck('name', 'idStudent')->toArray();
        $categories = array();
        return view('schools.feeheads.edit', compact('feeheads', 'search_classes', 'feehead', 'sections', 'classes', 'students', 'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        //
        $rules = [
//            'idClass' => 'required',
            'idSection' => 'required',
//            'feeheadName' => 'required',
            'fromDate' => 'required|date',
            'toDate' => 'required|date',
            'amount' => 'required',
//           'fine' => 'required_without:flatFine', 
//            'ecNO' => 'required'
        ];
        $messages = [
            'idClass.required' => 'Class must be selected.',
            'idSection.required' => 'Section must be selected.'
        ];
        $this->validate($request, $rules, $messages);
        if ($request->has('students')) {
            foreach ($request->students as $key => $value) {
                $std = \App\AdmEntry::where('idStudent', '=', $value)->first();
                $feehead = \App\FeeHead::where('idFeehead', '=', $id)->where('idStudent', '=', $value)->first();
                if ($feehead != null) {
                    $feehead->fill($request->all());
                    $feehead->updated_by = Auth::guard('school')->user()->idSchoolUser;
                    $feehead->update();
                } else {
                    $feehead = new \App\FeeHead();
                    $feehead->fill($request->all());
                    $feehead->idStudent = $value;
                    $feehead->idFinancialYear = Session::get('idFinancialYear');
                    $feehead->idClass = $std->idClass;
                    $feehead->idSection = $std->idSection;
                    $feehead->studentCategory = $std->studentType;
                    if ($request->fines == "per")
                        $feehead->fine = $request->fine;
                    else {
                        $feehead->fine = 0.0;
                        $feehead->flatFine = $request->flatFine;
                    }
                    $feehead->idSchool = Auth::guard('school')->user()->idSchool;
                    $feehead->updated_by = Auth::guard('school')->user()->idSchoolUser;
                    $feehead->save();
                }
            }
        } else {
            $feehead = \App\FeeHead::findOrFail($id);
            $feehead->fill($request->all());
            $feehead->updated_by = Auth::guard('school')->user()->idSchoolUser;
            $feehead->update();
        }
        return redirect('school/feeheads');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $feehead = \App\FeeHead::where('idFeehead', '=', $id)->first();
        $transactionIds = DB::table('student_transaction_details')->where('student_transaction_details.idFeehead', $id)->first();
        $lesserIds = DB::table('lesser_transaction_details')->where('lesser_transaction_details.idFeehead', $id)->first();
        if($transactionIds == null && $lesserIds == null){
            $feehead->delete();
            return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
        }else{
            return response()->json(['success' => "FAILED", "message" => "There is a transaction exists for this feehead."], 200, ['app-status' => 'success']);
        }
        
    }

}

Copyright © 2021 - 2025 IMMREX7