HEX
Server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.40
System: Linux ip-172-31-40-18 4.14.146-93.123.amzn1.x86_64 #1 SMP Tue Sep 24 00:45:23 UTC 2019 x86_64
User: apache (48)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: //var/www/html/punjabcabs/app/Http/Controllers/ProviderResources/DocumentController.php
<?php

namespace App\Http\Controllers\ProviderResources;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Auth;
use Setting;
use Storage;
use Exception;
use \Carbon\Carbon;

use App\Document;
use App\ProviderDocument;
use App\ProviderService;

class DocumentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $VehicleDocuments = Document::vehicle()->get();
        $DriverDocuments = Document::driver()->get();

        $Provider = \Auth::guard('provider')->user();

        return view('provider.document.index', compact('DriverDocuments', 'VehicleDocuments', 'Provider'));
    }

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

    /**
     * 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  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'document' => 'mimes:jpg,jpeg,png,pdf',
        ]);

        try {

            $Document = ProviderDocument::where('provider_id', \Auth::guard('provider')->user()->id)
                ->where('document_id', $id)
                ->firstOrFail();

            $Document->update([
                'url' => $request->document->store('provider/documents'),
                'status' => 'ASSESSING',
            ]);

            return back();
        } catch (ModelNotFoundException $e) {

            ProviderDocument::create([
                'url' => $request->document->store('provider/documents'),
                'provider_id' => \Auth::guard('provider')->user()->id,
                'document_id' => $id,
                'status' => 'ASSESSING',
            ]);
        }

        return back();
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function upload_document(Request $request)
    {
        //Config::set('auth.providers.users.model', 'App\Provider');
        // $this->validate($request, [
        //     'document' => 'required',
        //     'image' => 'required|mimes:jpg,jpeg,png,pdf',
        // ]);
        try {
            $document_name = $request->document;
            if ($document_name == 'license') {
                $id = 1;
            } elseif ($document_name == 'reg_certificate') {
                $id = 2;
            } elseif ($document_name == 'insurance') {
                $id = 3;
            } elseif ($document_name == 'driver_certificate') {
                $id = 4;
            } elseif ($document_name == 'vehicle_certificate') {
                $id = 5;
            } elseif ($document_name == 'vehicle_photo') {
                $id = 6;
            } elseif ($document_name == 'rwc_certificate') {
                $id = 7;
            } else {
                $id = 0;
            }
            if ($request->hasFile('image') && $id != 0) {

                $Image = ProviderDocument::where('provider_id', Auth::user()->id)
                    ->where('document_id', $id)
                    ->first();
                $file = $request->file('image');
                $filename = time() . '.' . $file->getClientOriginalExtension();
                if ($Image != null) {
                    $Image->update([
                        //'url' => $request->image->store('provider/documents'),
                        'url' => $file->storeAs('provider/documents', $filename),
                        'status' => 'ASSESSING',
                    ]);
                } else {
                    //$path = $file->storeAs('provider/documents', $filename);
                    ProviderDocument::create([
                        //'url' => $request->image->store('provider/documents'),
                        'url' => $file->storeAs('provider/documents', $filename),
                        'provider_id' => Auth::user()->id,
                        'document_id' => $id,
                        'status' => 'ASSESSING',
                    ]);
                }
            }
            return response()->json(['message' => 'Documents Updated successfully!']);
        } catch (ModelNotFoundException $e) {
            return $e;
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy_documents(Request $request)
    {
        $this->validate($request, [
            'document' => 'required',
        ]);

        try {
            $document_name = $request->document;
            if ($document_name == 'license') {
                $id = 1;
            } elseif ($document_name == 'reg_certificate') {
                $id = 2;
            } elseif ($document_name == 'insurance') {
                $id = 3;
            } elseif ($document_name == 'driver_certificate') {
                $id = 4;
            } elseif ($document_name == 'vehicle_certificate') {
                $id = 5;
            } elseif ($document_name == 'vehicle_photo') {
                $id = 6;
            } elseif ($document_name == 'rwc_certificate') {
                $id = 7;
            } else {
                $id = 0;
            }
            if ($id != 0) {
                $Document = ProviderDocument::where('provider_id', Auth::user()->id)
                    ->where('document_id', $id)
                    ->firstOrFail();
                Storage::delete($Document->url);
                $Document->delete();
            }
            return response()->json(['message' => 'Documents Deleted successfully!']);
        } catch (ModelNotFoundException $e) {
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    public function get_documents(Request $request)
    {

        try {

            $license = ProviderDocument::where('provider_id', Auth::user()->id)->where('document_id', 1)->first();
            if ($license != null) {
                //$license = asset('storage/' . $license->url);
                $license = assetApi($license->url);
            } else {
                $license = '';
            }

            $reg_certificate = ProviderDocument::where('provider_id', Auth::user()->id)->where('document_id', 2)->first();
            if ($reg_certificate != null) {
                $reg_certificate = assetApi($reg_certificate->url);
            } else {
                $reg_certificate = '';
            }

            $insurance = ProviderDocument::where('provider_id', Auth::user()->id)->where('document_id', 3)->first();
            if ($insurance != null) {
                $insurance = assetApi($insurance->url);
            } else {
                $insurance = '';
            }

            $driver_certificate = ProviderDocument::where('provider_id', Auth::user()->id)->where('document_id', 4)->first();
            if ($driver_certificate != null) {
                $driver_certificate = assetApi($driver_certificate->url);
            } else {
                $driver_certificate = '';
            }

            $vehicle_certificate = ProviderDocument::where('provider_id', Auth::user()->id)->where('document_id', 5)->first();
            if ($vehicle_certificate != null) {
                $vehicle_certificate = assetApi($vehicle_certificate->url);
            } else {
                $vehicle_certificate = '';
            }

            $vehicle_photo = ProviderDocument::where('provider_id', Auth::user()->id)->where('document_id', 6)->first();
            if ($vehicle_photo != null) {
                $vehicle_photo = assetApi($vehicle_photo->url);
            } else {
                $vehicle_photo = '';
            }

            $rwc_certificate = ProviderDocument::where('provider_id', Auth::user()->id)->where('document_id', 7)->first();
            if ($rwc_certificate != null) {
                $rwc_certificate = assetApi($rwc_certificate->url);
            } else {
                $rwc_certificate = '';
            }

            return response()->json(['license' => $license, 'reg_certificate' => $reg_certificate, 'insurance' => $insurance, 'driver_certificate' => $driver_certificate, 'vehicle_certificate' => $vehicle_certificate, 'vehicle_photo' => $vehicle_photo, 'rwc_certificate' => $rwc_certificate]);
        } catch (ModelNotFoundException $e) {
            return response()->json(['error' => 'Please Upload Document!'], 500);
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    public function expiry_document(Request $request)
    {

        try {

            $provider_service = ProviderService::where('provider_id', Auth::user()->id)
                ->first();
            $data = [];
            $now = Carbon::now();
            if ($provider_service->license_status != 0) {
                $expiry_date = Carbon::parse($provider_service->license_expire);
                if ($now >= $expiry_date) {
                    $message = 'Update the document before expiry otherwise you will not be able to take trip';
                } else {
                    $message = 'Your document expired. You are not allowed  to take ride until you submit the updaded document.';
                }
                $license = array(
                    'document_name' => 'License Certificate',
                    'expire_date' => $provider_service->license_expire,
                    'message' => $message
                );
                $data[] = $license;
            }

            if ($provider_service->insurance_status != 0) {
                $expiry_date = Carbon::parse($provider_service->insurance_expire);
                if ($now >= $expiry_date) {
                    $message = 'Update the document before expiry otherwise you will not be able to take trip';
                } else {
                    $message = 'Your document expired. You are not allowed  to take ride until you submit the updaded document.';
                }

                $insurance = array(
                    'document_name' => 'Insurance Certificate',
                    'expire_date' => $provider_service->insurance_expire,
                    'message' => $message
                );
                $data[] = $insurance;
            }

            if ($provider_service->certificate_status != 0) {
                $expiry_date = Carbon::parse($provider_service->certificate_expire);
                if ($now >= $expiry_date) {
                    $message = 'Update the document before expiry otherwise you will not be able to take trip';
                } else {
                    $message = 'Your document expired. You are not allowed  to take ride until you submit the updaded document.';
                }

                $certificate = array(
                    'document_name' => 'Driver Certificate',
                    'expire_date' => $provider_service->certificate_expire,
                    'message' => $message
                );
                $data[] = $certificate;
            }

            return response()->json($data);
        } catch (ModelNotFoundException $e) {
            return response()->json(['error' => 'Please Upload Document!'], 500);
        }
    }
}