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

<?php

namespace App\Http\Controllers\School;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use Session;

class AssignDiscountController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        //
        $stddiscounts = \App\StudentDiscount::leftJoin('school_users', 'student_discounts.created_by', '=', 'school_users.idSchoolUser')->select('student_discounts.*','school_users.name as collector')->where('student_discounts.idSchool', '=', Auth::guard('school')->user()->idSchool)
                ->orderBy('idStdDiscount', 'desc');
        if ($request->has('idFinancialYear')) {
            $stddiscounts = $stddiscounts->where('idFinancialYear', '=', $request->idFinancialYear)->get();
        } else {
            $stddiscounts = $stddiscounts->where('idFinancialYear', '=', Session::get('idFinancialYear'))->get();
        }
        $classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        return view('schools.students.assign_discount', compact('classes', 'stddiscounts'));
    }

    /**
     * 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 = [
            'idClass' => 'required',
            'idSection' => 'required',
            'idStudent' => 'required',
            'idFeehead' => 'required',
            'amount' => 'required_without:percentage'
        ];
        if ($request->has('idFeehead')) {
            $feehead = \App\FeeHead::where('idFeehead', '=', $request->idFeehead)->first();
            if ($request->amount > $feehead->amount) {
                $rules += ['discamt' => 'required'];
            }
        }
        $messages = [
            'idClass.required' => 'Class must be selected',
            'idSection.required' => 'Section must be selected',
            'idFeehead.required' => 'Select Fee Header in which you want to assign discount',
            'idStudent.required' => 'Select Students Enrollment no.',
            'discamt.required' => 'Discounted Amount should not be greater than Feehead Amount.'
        ];
        $this->validate($request, $rules, $messages);
        $discount = new \App\StudentDiscount();
        $discount->fill($request->all());
        $discount->idSchool = Auth::guard('school')->user()->idSchool;
        $discount->idFinancialYear = Session::get('idFinancialYear');
        $discount->isActive = 'Y';
        $discount->created_by = Auth::guard('school')->user()->idSchoolUser;
        $discount->save();
        return redirect('school/assigndiscount');
    }

    /**
     * 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) {
        $stddiscounts = \App\StudentDiscount::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->where('idFinancialYear', '=', Session::get('idFinancialYear'))
                        ->orderBy('idStdDiscount', 'desc')->get();
        $classes = ['' => '--Select--'] + \App\ClassM::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idClass')->get()->pluck('className', 'idClass')->toArray();
        $discount = \App\StudentDiscount::where('idStdDiscount', '=', $id)->first();
        $studentDetails =  \App\AdmEntry::where('idStudent', '=', $discount->idStudent)->first();
        $feehead = \App\FeeHead::where('idFeehead', '=', $discount->idFeehead)->first();
            
        return view('schools.students.assign_discount', compact('classes', 'stddiscounts', 'discount','studentDetails','feehead'));
    }

    /**
     * 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',
            'idStudent' => 'required',
            'idFeehead' => 'required',
            'amount' => 'required_without:percentage'
        ];
        if ($request->has('idFeehead')) {
            $feehead = \App\FeeHead::where('idFeehead', '=', $request->idFeehead)->first();
            if ($request->amount > $feehead->amount) {
                $rules += ['discamt' => 'required'];
            }
        }
        $messages = [
            'idClass.required' => 'Class must be selected',
            'idSection.required' => 'Section must be selected',
            'idFeehead.required' => 'Select Fee Header in which you want to assign discount',
            'idStudent.required' => 'Select Students Enrollment no.',
            'discamt.required' => 'Discounted Amount should not be greater than Feehead Amount.'
        ];
        $this->validate($request, $rules, $messages);
        $discount = \App\StudentDiscount::where('idStdDiscount', '=', $id)->first();
        $discount->fill($request->all());
        $discount->created_by = Auth::guard('school')->user()->idSchoolUser;
        $discount->update();
        return redirect('school/assigndiscount');
    }

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

    public function activateDiscount($id) {
        $discount = \App\StudentDiscount::where('idStdDiscount', '=', $id)->first();
        $discount->isActive = 'Y';
        $discount->created_by = Auth::guard('school')->user()->idSchoolUser;
        $discount->update();
        return redirect('school/assigndiscount');
    }

    public function deactivateDiscount($id) {
        $discount = \App\StudentDiscount::where('idStdDiscount', '=', $id)->first();
        $discount->isActive = 'N';
        $discount->created_by = Auth::guard('school')->user()->idSchoolUser;
        $discount->update();
        return redirect('school/assigndiscount');
    }

}

Copyright © 2021 - 2025 IMMREX7