IMMREX7

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

<?php

namespace App\Http\Controllers\School\Library;

use Illuminate\Http\Request;
use App\Http\Controllers\School\SchoolController;
use DB;
use Auth;
use Session;

class BookImportController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        return view('schools.library.bookexcelupload');
    }

    /**
     * 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  $value
     * @return \Illuminate\Http\Response
     */
    public function store(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')) {
            $path = $request->file('excelfile')->getRealPath();
            $data = \Excel::load($path)->get();
            //dd($data);
            if ($data->count()) {
                foreach ($data as $key => $value) {
                    if ($value->booktype != null) {
                        $booktype = \App\BookType::where('typeName', '=', $value->booktype)
                                ->where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->first();
                        if (!empty($booktype)) {
                            $idBookType = $booktype->idBookType;
                        } else {
                            $idBookType = null;
                        }
                    } else {
                        $idBookType = null;
                    }
                    if ($value->publisher != null) {
                        $publisher = \App\BookPublisher::where('publisherName', '=', $value->Publisher)
                                ->where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->first();
                        if (!empty($publisher)) {
                            $idPublisher = $publisher->idPublisher;
                        } else {
                            $idPublisher = null;
                        }
                    } else {
                        $idPublisher = null;
                    }
                    // If New Publication Place is Added
                    if ($value->publicationplace != null) {
                        $pubplace = \App\BookPublicationPlace::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('placeName', '=', $value->publicationplace)
                                ->first();
                        if (!empty($pubplace)) {
                            $idPublicationPlace = $pubplace->idPublicationPlace;
                        } else {
                            $idPublicationPlace = null;
                        }
                    } else {
                        $idPublicationPlace = null;
                    }

                    //If New Author  is Added
                    if ($value->author != null) {
                        $author = \App\BookAuthor::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('authorName', '=', $value->author)
                                ->first();
                        if (!empty($author)) {
                            $idAuthor = $author->idAuthor;
                        } else {
                            $idAuthor = null;
                        }
                    } else {
                        $idAuthor = null;
                    }

                    //If New Language  is Added
                    if ($value->language != null) {
                        $language = \App\BookLanguage::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('languageName', '=', $value->language)
                                ->first();
                        if (!empty($language)) {
                            $idLanguage = $language->idLanguage;
                        } else {
                            $idLanguage = null;
                        }
                    } else {
                        $idLanguage = null;
                    }

                    //If New SourceLanguage  is Added
                    if ($value->sourcelanguage != null) {
                        $slanguage = \App\BookSourceLanguage::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('sourcelangName', '=', $value->sourcelanguage)
                                ->first();
                        if (!empty($slanguage)) {
                            $idSourceLanguage = $slanguage->idSourceLanguage;
                        } else {
                            $idSourceLanguage = null;
                        }
                    } else {
                        $idSourceLanguage = null;
                    }

                    //If New Classification  is Added
                    if ($value->classification != null) {
                        $classification = \App\BookClassification::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('classificationName', '=', $value->classification)
                                ->first();
                        if (!empty($classification)) {
                            $idClassification = $classification->idClassification;
                        } else {
                            $idClassification = null;
                        }
                    } else {
                        $idClassification = null;
                    }

                    //If New Book Location  is Added
                    if ($value->booklocation != null) {
                        $location = \App\BookLocation::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('locName', '=', $value->booklocation)
                                ->first();
                        if (!empty($location)) {
                            $idBookLocation = $location->idBookLocation;
                        } else {
                            $idBookLocation = null;
                        }
                    } else {
                        $idBookLocation = null;
                    }

                    //If New SubClass one  is Added
                    if ($value->subclassno != null) {
                        $subclassone = \App\BookSubclass::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('level', '=', '1')
                                ->where('classNo', '=', $value->subclassno)
                                ->first();
                        if (!empty($subclassone)) {
                            $idSubclassone = $subclassone->idSubclass;
                        } else {
                            $idSubclassone = null;
                        }
                    } else {
                        $idSubclassone = null;
                    }

                    //If New SubClass TWO  is Added
                    if ($value->subclasstwo != null) {
                        $subclasstwo = \App\BookSubclass::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('level', '=', '2')
                                ->where('classNo', '=', $value->subclasstwo)
                                ->first();
                        if (!empty($subclasstwo)) {
                            $idSubclasstwo = $subclasstwo->idSubclass;
                        } else {
                            $idSubclasstwo = null;
                        }
                    } else {
                        $idSubclasstwo = null;
                    }

                    //If New Editor  is Added
                    if ($value->editor != null) {
                        $editor = \App\BookEditor::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('editorName', '=', $value->editor)
                                ->first();
                        if (!empty($editor)) {
                            $idEditor = $editor->idEditor;
                        } else {
                            $idEditor = null;
                        }
                    } else {
                        $idEditor = null;
                    }
                    //If New BookSeries  is Added
                    if ($value->bookseries != null) {
                        $series = \App\BookSeries::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('seriesName', '=', $value->bookseries)
                                ->first();
                        if (!empty($series)) {
                            $idBookSeries = $series->idBookSeries;
                        } else {
                            $idBookSeries = null;
                        }
                    } else {
                        $idBookSeries = null;
                    }

                    //If New Vendor  is Added
                    if ($value->vendor != null) {
                        $vendor = \App\BookVendor::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('vendorName', '=', $value->vendor)
                                ->first();
                        if (!empty($vendor)) {
                            $idVendor = $vendor->idVendor;
                        } else {
                            $idVendor = null;
                        }
                    } else {
                        $idVendor = null;
                    }

                    //If New Book Subject  is Added
                    if ($value->booksubject != null) {
                        $subject = \App\BookSubject::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('subjectName', '=', $value->booksubject)
                                ->first();
                        if (!empty($subject)) {
                            $idBookSubject = $subject->idBookSubject;
                        } else {
                            $idBookSubject = null;
                        }
                    } else {
                        $idBookSubject = null;
                    }

                    //If New Faculty  is Added
                    if ($value->faculty != null) {
                        $faculty = \App\BookFaculty::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('facultyName', '=', $value->faculty)
                                ->first();
                        if (!empty($faculty)) {
                            $idFaculty = $faculty->idFaculty;
                        } else {
                            $idFaculty = null;
                        }
                    } else {
                        $idFaculty = null;
                    }

                    //If New BindingType  is Added
                    if ($value->bindingtype != null) {
                        $binding = \App\BindingType::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('typeName', '=', $value->bindingtype)
                                ->first();
                        if (!empty($binding)) {
                            $idBindingType = $binding->idBindingType;
                        } else {
                            $idBindingType = null;
                        }
                    } else {
                        $idBindingType = null;
                    }

                    //If New Book Size  is Added
                    if ($value->booksize != null) {
                        $size = \App\BookSize::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('sizeName', '=', $value->booksize)
                                ->first();
                        if (!empty($size)) {
                            $idBookSize = $size->idBookSize;
                        } else {
                            $idBookSize = null;
                        }
                    } else {
                        $idBookSize = null;
                    }

                    //If New Material  is Added
                    if ($value->material != null) {
                        $material = \App\AccompanyMaterial::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('materialName', '=', $value->material)
                                ->first();
                        if (!empty($material)) {
                            $idMaterial = $material->idMaterial;
                        } else {
                            $idMaterial = null;
                        }
                    } else {
                        $idMaterial = null;
                    }

                    //If New Corporate  is Added
                    if ($value->corporate != null) {
                        $corporate = \App\BookCorporate::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
                                ->where('corporateName', '=', $value->corporate)
                                ->first();
                        if (!empty($corporate)) {
                            $idCorporate = $corporate->idCorporate;
                        } else {
                            $idCorporate = null;
                        }
                    } else {
                        $idCorporate = null;
                    }
                    $arr[] = [
                        'idSchool' => Auth::guard('school')->user()->idSchool,
                        'idFinancialyear' => Session::get('idFinancialYear'),
                        'idBookType' => $idBookType,
                        'accessionNo' => $value->accessionno,
                        'entryDate' => $value->entrydate,
                        'rfid' => $value->rfid,
                        'title' => $value->title,
                        'publicationYear' => $value->publicationyear,
                        'subtitle' => $value->subtitle,
                        'classificationNo' => $value->classificationno,
                        'volumnNo' => $value->volumnno,
                        'isbnNo' => $value->isbnno,
                        'issnno' => $value->issnNo,
                        'edition' => $value->edition,
                        'keywords' => $value->keywords,
                        'notes' => $value->notes,
                        'noOfPages' => $value->noofpages,
                        'noOfCopy' => $value->noofcopy,
                        'billNo' => $value->billno,
                        'billDate' => $value->billdate,
                        'purchaseDate' => $value->purchasedate,
                        'originalCost' => $value->originalcost,
                        'discount' => $value->discount,
                        'costAfterDiscount' => $value->costafterdiscount,
                        'idPublisher' => $idPublisher,
                        'idPlace' => $idPublicationPlace,
                        'idAuthor' => $idAuthor,
                        'idLanguage' => $idLanguage,
                        'idSourceLanguage' => $idSourceLanguage,
                        'idClassification' => $idClassification,
                        'idBookLocation' => $idBookLocation,
                        'idSubClassno' => $idSubclassone,
                        'idSubclasstwo' => $idSubclasstwo,
                        'idEditor' => $idEditor,
                        'idBookSeries' => $idBookSeries,
                        'idEditor' => $idEditor,
                        'idVendor' => $idVendor,
                        'idBookSubject' => $idBookSubject,
                        'idFaculty' => $idFaculty,
                        'idBindingType' => $idBindingType,
                        'idBookSize' => $idBookSize,
                        'idMaterial' => $idMaterial,
                        'idCorporate' => $idCorporate,
                    ];
                }


                if (!empty($arr)) {
                    foreach (array_chunk($arr, 500) as $t) {
                        \DB::table('books')->insert($t);
                    }
                    flash('Record saved successfully.');
                    return redirect('school/bookimport');
                }
            }
        }

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

    /**
     * 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) {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $value
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        //
    }

}

Copyright © 2021 - 2025 IMMREX7