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

<?php

namespace App\Http\Controllers\School;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use App\Imports\StudentImport;
use Maatwebsite\Excel\Facades\Excel;
use Session;

class EmployeeController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {

        $employees = \App\Employee::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idEmployee', 'desc')->get();
        return view('schools.employees.index', compact('employees'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        //  $types = ['' => '--Select--'] + \App\EmployeeType::get()->pluck('typeName', 'idType')->toArray();
        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        $departments = ['' => '--Select--'] + \App\Department::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('departmentName', 'idDepartment')->toArray();
        return view('schools.employees.form', compact( 'departments','school'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(\App\Http\Requests\EmployeeRequest $request) {
        $employee = new \App\Employee();
        $employee->fill($request->all());
        $password = $request->password;
        $employee->pwd = $password;
        $employee->password = bcrypt($password);
        $employee->idSchool = Auth::guard('school')->user()->idSchool;
        DB::beginTransaction();
        $employee->save();

        if ($request->hasFile('photo')) {
            $photo = 'photo_' . $employee->idEmployee . '.' . $request->file('photo')->getClientOriginalExtension();
            $request->file('photo')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/employees/', $photo);
            $employee->photo = $photo;
        }
        $employee->update();

        if (count($request->otheraccounts) > 0) {
            foreach ($request->otheraccounts as $key => $value) {
                $othacc = new \App\EmployeeOtherAccount();
                $othacc->fill($request->all());
                $othacc->idEmployee = $employee->idEmployee;
                $othacc->accName = $value['accName'];
                $othacc->accNumber = $value['accNumber'];
                $othacc->otherDetails = $value['otherDetails'];
                $othacc->save();
            }
        }

        DB::commit();
        flash('Employee has been saved successfully !!');
        if ($request->ajax()) {
            return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
        }
        return redirect('school/employees');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        $teacher = \App\Employee::where('idEmployee', '=', $id)->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first(['firstName', 'lastName']);
        return json_encode($teacher);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        $departments = ['' => '--Select--'] + \App\Department::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('departmentName', 'idDepartment')->toArray();
        $employee = \App\Employee::where('idEmployee', '=', $id)->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        $designations = \App\Designation::where('idDepartment', '=', $employee->idDepartment)->get()->pluck('designationName', 'idDesignation')->toArray();
        return view('schools.employees.form', compact('employee', 'departments', 'designations','school'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        $employee = \App\Employee::where('idEmployee', '=', $id)->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        $employee->fill($request->all());

        if ($request->hasFile('photo')) {
            $photo = 'photo_' . $employee->idEmployee . '.' . $request->file('photo')->getClientOriginalExtension();
            $request->file('photo')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/employees/', $photo);
            $employee->photo = $photo;
        }
        DB::beginTransaction();
        $employee->update();
        foreach ($request->otheraccounts as $key => $value) {
            if (isset($value['idOtherAccount'])) {
                $othacc = \App\EmployeeOtherAccount::where('idEmployee', '=', $id)->where('idOtherAccount', '=', $value['idOtherAccount'])->first();
            } else {
                $othacc = new \App\EmployeeOtherAccount();
            }
            $othacc->fill($request->all());
            $othacc->idEmployee = $employee->idEmployee;
            $othacc->accName = $value['accName'];
            $othacc->accNumber = $value['accNumber'];
            $othacc->otherDetails = $value['otherDetails'];
            $othacc->save();
        }
        DB::commit();
        return redirect('school/employees');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $employee = \App\Employee::where('idEmployee', '=', $id)->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        DB::beginTransaction();
        $employee->other_accounts()->delete();
        $employee->delete();
        DB::commit();
        return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
    }

    public function status(Request $request){
        $employee = \App\Employee::where('idEmployee', '=', $request->id)->where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        if($request->status == 'active'){
            $employee->isActive = 'Y';
        }else $employee->isActive = 'N';
        $employee->update();
        return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
    }

    public function excelUploadForm() {
        return view('schools.employees.excelupload');
    }

    public function excelUpload(Request $request) {
        ini_set('memory_limit', -1);
        ini_set('max_execution_time', '300');
        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        if ($request->hasFile('excelfile')) {
            $import = new StudentImport();
            Excel::import($import, $request->file('excelfile'));
            $row = 0;
	        if (count($import->getData()) > 0) {
                    foreach ($import->getData() as $key => $dr) {
                        $row++;
                        $value = (object) $dr;
                        $department = \App\Department::where('departmentName', '=', $value->department)->where('idSchool', '=', $school->idSchool)->first();
                        if($department == null){
                            flash('Department is invalid or not correct in row no.'.$row);
                            return redirect('school/empexcelupload');
                        }
                        if (!empty($department)) {
                            $designation = \App\Designation::where('designationName', '=', $value->designation)
                                    ->where('idDepartment', '=', $department->idDepartment)
                                    ->where('idSchool', '=', $school->idSchool)
                                    ->first();
                                    if($designation == null){
                                        flash('Designation is invalid or not correct in row no.'.$row);
                                        return redirect('school/empexcelupload');
                                    }
                        }
                        $bank = \App\Bank::where('bankName', '=', $value->bank)->first();
                        if(!empty($bank)){
                            $bankid = $bank->idBank;
                        }else{
                            $bankid = null;
                        }
                        if($bank == null){
                            flash('Bank information is invalid or not correct in row no.'.$row);
                            return redirect('school/empexcelupload');
                        }
                        $state = \App\State::where('stateName', '=', $value->state)->first();
                        if($state == null){
                            flash('State is invalid or not correct in row no.'.$row);
                            return redirect('school/empexcelupload');
                        }
                        try{
                            $arr[] = [
                                'idSchool' => $school->idSchool,
                                'firstName' => $value->firstName,
                                'middleName' => $value->middleName,
                                'lastName' => $value->lastName,
                                'idDepartment' => $department->idDepartment,
                                'idDesignation' => $designation->idDesignation,
                                'enrollmentNo' => $value->enrollmentNo,
                                'dob' => $value->dob,
                                'mobile' => $value->mobile,
                                'email' => $value->email,
                                'doj' => $value->doj,
                                'gender' => $value->gender,
                                'aadhaarNo' => $value->aadhaarNo,
                                'panNo' => $value->panNo,
                                'bloodGroup' => $value->bloodGroup,
                                'address' => $value->address,
                                'city' => $value->city,
                                'pincode' => $value->pincode,
                                'landmark' => $value->landmark,
                                'idState' => $state->idState,
                                'telephone' => $value->telephone,
                                'contactPerson' => $value->contactPerson,
                                'contactPersonAddress' => $value->contactPersonAddress,
                                'contactPersonRelation' => $value->contactPersonRelation,
                                'contactPersonTelephone' => $value->contactPersonTelephone,
                                'contactPersonMobile' => $value->contactPersonMobile,
                                'idBank' => $bankid,
                                'accountNo' => $value->accountno,
                                'ifscCode' => $value->ifsccode,
                                'pwd' => '123456',
                                'password' => bcrypt('123456'),
                            ];
                        }catch (\Exception $e) {
                            flash('Error in row .'.$row. ' '.$e->getMessage());
                            return redirect('school/empexcelupload');
                        }
                    }


                    if (!empty($arr)) {
                        try {
                            DB::beginTransaction();
                            foreach (array_chunk($arr, 500) as $t) {
                                \DB::table('employees')->insert($t);
                            }
                            DB::commit();
                            flash('Record saved successfully.');
                            return redirect('school/empexcelupload');
                        }catch (\Exception $e) {
                            // An error occured; cancel the transaction...
                            DB::rollback();
                            // and throw the error again.
                            flash('Error in '.$e->getMessage());
                            return redirect('school/empexcelupload');
                        }
                    }
                }
        }

        flash('Request data does not have any files to import.');
    }

    public function imageUploadform() {
        $employees = \App\Employee::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        //    ->where('isActive', '=', 'Y')
                        ->whereNull('photo')
                        ->orderBy('idEmployee')->get();
        return view('schools.employees.imageupload', compact('employees'));
    }

    public function imageUpload(Request $request) {
        //  dd($request->idStudent);
        $rules = ['photo' => 'required|max:400|mimes:jpg,jpeg,png'];
        $this->validate($request, $rules);
        $employee = \App\Employee::where('idEmployee', '=', $request->idEmployee)->first();
        if ($request->hasFile('photo')) {
            $photo = 'photo_' . $employee->idEmployee . '.' . $request->file('photo')->getClientOriginalExtension();
            $request->file('photo')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/employees/', $photo);
            $employee->photo = $photo;
        }
        $employee->update();
        if ($request->ajax()) {
            return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
        }
    }

    public function viewEmployee($id) {
        $employee = \App\Employee::where('idEmployee', '=', $id)->first();
        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        return view('schools.employees.viewemployee', compact('employee','school'));
    }

    public function getStudentDocForm(Request $request) {
        $stddocs = DB::table('student_documents')
                ->join('document_types', 'student_documents.idDocType', '=', 'document_types.idDocType')
                ->join('employees', 'student_documents.idEmployee', '=', 'employees.idEmployee')
                ->where('employees.idSchool', '=', Auth::guard('school')->user()->idSchool)
                ->select('student_documents.idFinancialYear', 'enrollmentNo as ecNo', 'firstName', 'middleName', 'lastName', 'documentType', 'idStdDocument', 'student_documents.updated_at');

        if ($request->has('idFinancialYear')) {
            $stddocs = $stddocs->where('student_documents.idFinancialYear', '=', $request->idFinancialYear)
                    ->get();
        } else {
            $stddocs = $stddocs->where('student_documents.idFinancialYear', '=', Session::get('idFinancialYear'))
                    ->get();
        }


        $departments = ['' => '--Select--'] + \App\Department::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->get()->pluck('departmentName', 'idDepartment')->toArray();

        $doctypes = ['' => '--Select Document Type--'] + \App\DocumentType::where('type', '=', 'Employee')->where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                        ->orderBy('idDocType', 'desc')->get()->pluck('documentType', 'idDocType')->toArray();

        return view('schools.hrms.employeedocs', compact('departments', 'doctypes', 'stddocs'));
    }

    public function saveEmployeeDocs(Request $request) {
//        dd($request->all());
        $dtype = \App\DocumentType::where('idDocType', '=', $request->idDocType)->first();
        $employee = \App\Employee::where('idEmployee', '=', $request->idEmployee)->first();
        $isExist = \App\StudentDocument::where('idDocType', '=', $request->idDocType)->where('idEmployee', '=', $request->idEmployee)->first();
        if($isExist != null){
            flash('Same document cannot be uploaded twice.');
            return redirect('school/employeedocs');
        }
        $stddoc = new \App\StudentDocument();
        $stddoc->fill($request->all());
        $stddoc->idFinancialYear = Session::get('idFinancialYear');
        $stddoc->idEmployee = $employee->idEmployee;
        if ($request->hasFile('document')) {
            $doc = $dtype->documentType . '_' . $employee->idEmployee . '.' . $request->file('document')->getClientOriginalExtension();
            $request->file('document')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/employees/', $doc);
            $stddoc->document = $doc;
        }
        $stddoc->save();
        return redirect('school/employeedocs');
    }

    public function viewStdDoc($id) {
        $stddoc = \App\StudentDocument::where('idStdDocument', '=', $id)->first();
        $employee = \App\Employee::where('idEmployee', '=', $stddoc->idEmployee)->first();
        $path = storage_path('app/public/schools/' . $employee->idSchool . '/employees/' . $stddoc->document);
        return response()->file($path);
    }

    public function deleteStdDoc($id) {
        $stddoc = \App\StudentDocument::where('idStdDocument', '=', $id)->first();
        $stddoc->delete();
        return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
    }

}

Copyright © 2021 - 2025 IMMREX7