<?php

use App\User;
use App\Setting;
use App\Language;
use App\BranchDetail;
use App\SystemFoodMeal;
use App\SystemFood;
use App\CategoryCalorie;
use App\SystemPhysicPhysical;
use App\CodePhysic;
use App\MedicalQuestion;
use App\Step;
use App\DoctorExperience;
use App\DoctorLanguage;
use App\About;
use App\Read;
use App\History;
use App\Physical;
use App\FoodCategory;
use App\BranchUser;
use App\Newsletter;
use App\ReservationMeal;
use App\DoctorReservation;
use App\ReservationPhyscal;
use App\Reservation;
use App\CodePhyscal;
use App\RoleModule;
use App\Module;
use App\Article;
use App\Question;
use App\SessionBranch;
use App\Service;
use App\CodeMeal;
use App\Specialty;
use App\SpecialtyPatient;
use App\SpecialtyBranch;
use App\Answer;
use App\Meal;
use App\Workday;
use App\Wbsetting;
use App\Branch;
use App\Anatomy;
use App\VideoCategory;
use Illuminate\Support\Facades\DB;
use App\Providers\AppServiceProvider;
use Illuminate\Support\LazyCollection;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;




if (!function_exists('physicsuser')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function physicsuser($id)
    {
       $data = ReservationPhyscal::where('reservation_id',$id)->get();
       
        return $data;
    }
}

if (!function_exists('physiccodes')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function physiccodes($id)
    {
       $data = CodePhysic::where('system_physcal_id',$id)->get();
       
        return $data;
    }
}

if (!function_exists('codesfood')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function codesfood($id)
    {
       $data = CodeMeal::where('calorie_id',$id)->get();
       
        return $data;
    }
}
if (!function_exists('reservationmeal')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function reservationmeal($id)
    {
       $data = ReservationMeal::where('reservation_id',$id)->first();
       
        return $data['description'];
    }
}
if (!function_exists('caloriesfood')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function caloriesfood($id)
    {
       $data = CategoryCalorie::where('category_id',$id)->get();
       
        return $data;
    }
}
if (!function_exists('categoriesfood')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function categoriesfood($id)
    {
       $data = FoodCategory::where('food_system_id',$id)->get();
       
        return $data;
    }
}

if (!function_exists('historyreads')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function historyreads($id)
    {
        $data = Read::where('history_id',$id)->get();
        return $data;
    }
}


if (!function_exists('doctor')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function doctor($name)
    {
        $data = User::where('name','LIKE', '%'.$name.'%')->pluck('id');
        return $data;
    }
}



if (!function_exists('anatomycats')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function anatomycats($id)
    {
        $data = Anatomy::where('category_id',$id)->get();
        return $data;
    }
}


if (!function_exists('patient')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function patient($name)
    {
        $data = User::where('name','LIKE', '%'.$name.'%')->pluck('id');
        return $data;
    }
}
if (!function_exists('branchids')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function branchids($name)
    {
        $data = Branch::where('name','LIKE', '%'.$name.'%')->pluck('id');
        return $data;
    }
}
if (!function_exists('languages')){

    /**
     * Return list of languages from database
     * @return mixed
     */
    function languages()
    {
        $languages = Language::where('status', 1)->cursor();
        return $languages;
    }
}

if (!function_exists('currentLanguage')){

    /**
     * Return current language from database
     * @return mixed
     */
    function currentLanguage()
    {
        $languages = Language::where('status', 1)->where('local', LaravelLocalization::getCurrentLocale())->first();
        return $languages;
    }
}

if (!function_exists('getCurrentLocale')){

    /**
     * Return current current locale from database
     * @return mixed
     */
    function getCurrentLocale()
    {
        $current = Language::where('status', 1)->where('local', LaravelLocalization::getCurrentLocale())->first()->local;
        return $current;
    }
}

if (!function_exists('descriptions')){

    /**
     * Return description for model
     * @param $model
     * @return mixed
     */
    function descriptions($model)
    {
        $description = $model->descriptions->where('language_id', currentLanguage()->id)->first();
        return $description ?? null;
    }
}

if (!function_exists('parseExternalVideoLink')){

    /**
     * Parse youtube|vimo|etc link to get video ID, video embed link, video thumbnails
     * @param $link
     * @return array
     */
    function parseExternalVideoLink($link) :array
    {

        /**
         * Links to test
         * https://youtu.be/N6wn8zMRlVE
         * https://www.youtube.com/watch?v=zlnau3yk0oU
         * https://www.youtube.com/watch?v=zlnau3yk0oU&features=adsdsd
         * https://www.youtube.com/embed/OC9zn5BKlC0
         */

        $linkPattern = '/^(https:\/\/(www\.)?(youtu|youtube){1}.([a-z]{1,4}))(\/watch\?v=|\/embed\/|\/){0,1}([a-zA-z0-9]{2,15})(&.)*/';

        $m= preg_match($linkPattern, $link, $linkMatches);

        // dd($m, $linkMatches);
        if (count($linkMatches) && $linkMatches[6]){
            $code = $linkMatches[6];
            $video_url = 'https://www.youtube.com/embed/'.$code;
            $image = "https://img.youtube.com/vi/$code/hqdefault.jpg";
        } else {
            $video_url = $link;
        }

        // $public = asset('/uploads/images/'.time().'.jpg');

        // Download images from remote server
        // $in=    fopen($image, "rb");
        // $out=   fopen($public, "wb");
        // while ($chunk = fread($in,8192))
        // {
        //     fwrite($out, $chunk, 8192);
        // }
        // fclose($in);
        // fclose($out);


        return [
            'code' => $code,
            'url' => $video_url,
            'image' => $image
        ];

    }
}


if (!function_exists('videoCategories')){

    /**
     * Return video categories
     * @return LazyCollection
     */
    function videoCategories() :LazyCollection
    {
        $videoCategories = VideoCategory::with('descriptions')->cursor();
        return $videoCategories;
    }
}

if (!function_exists('articleCategories')){

    /**
     * Return article categories
     * @return LazyCollection
     */
    function articleCategories() :LazyCollection
    {
        $articleCategories = Category::with('descriptions')->cursor();
        return $articleCategories;
    }
}


if (!function_exists('specialties')){

    /**
     * Return article categories
     * @return LazyCollection
     */
    function specialties($data)
    {
        $ids = SpecialtyBranch::where('branch_id',$data)->pluck('specialty_id');
        $specialty = Specialty::find($ids);
        return $specialty;
    }
}


if (!function_exists('newsletters')){

    /**
     * Return all newsletters email
     * @return LazyCollection
     */
    function newsletters() :LazyCollection
    {
        $newsletters = Newsletter::cursor();
        return $newsletters;
    }
}

if (!function_exists('getUser')){

    /**
     * Return admin by id
     * @param $id
     * @return mixed
     */
    function getUser($id)
    {
        $user = User::find($id);
        return $user;
    }
}

if (!function_exists('settings')){

    /**
     * Return settings
     * @param null $key
     * @return mixed
     */
    function settings($key = null)
    {
        $settings = [];
        if ($key){
            $settings = Setting::where('key', $key)->first()->value;
        } else {
            $allSettings = Setting::cursor()->toArray();
            foreach ($allSettings as $setting) {
                $settings[$setting['key']] = $setting['value'];
            }
        }
        return $settings;
    }

}

if (!function_exists('meta')){

    /**
     * Return meta fields from module table
     * by field name and local language
     * this functions used mostly in blades views
     * to get meta field to each path individually
     * @param $field
     * @param $local
     * @return mixed
     */
    function meta($field, $local)
    {
        return AppServiceProvider::getMeta()->fields->{$local}->{$field} ?? null;
    }

}

if (!function_exists('metaFields')){

    /**
     * Return meta field to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $fieldName
     * @param null $local
     * @return mixed|null
     */
    function metaFields($modulePath, $fieldName = null, $local = null)
    {
        $meta = DB::table('modules')->where('path', $modulePath)->pluck('meta')->toArray();
        $meta = json_decode(array_first($meta));
        $meta = $fieldName && $local ? $meta->fields->{$local}->{$fieldName} ?? null : $meta->fields;
        return $meta ?? null;
    }

}

if (!function_exists('moduleName')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function moduleName($modulePath, $local = null)
    {
        $meta = DB::table('modules')->where('path', $modulePath)->pluck('meta')->toArray();
        $meta = json_decode(array_first($meta));
        $meta = $local ? $meta->module_name->{$local} : $meta->module_name;
        return $meta ?? null;
    }

}

if (!function_exists('counterdetails')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function counterdetails($data)
    {
        $counter = BranchDetail::where('branch_id',$data)->count();
        return $counter;
    }

}
if (!function_exists('detailstrue')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function detailstrue($branch,$specialty)
    {
        $counter = SpecialtyBranch::where('branch_id',$branch)->where('specialty_id',$specialty)->first();
        if($counter){
            return true;
        }
        return false;
    }

}

if (!function_exists('systemfoods')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function systemfoods($systemfood,$meal)
    {
        $counter = SystemFoodMeal::where('system_food_id',$systemfood)->where('meal_id',$meal)->first();
        if($counter){
            return true;
        }
        return false;
    }

}
if (!function_exists('meals')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function meals($systemfood,$meal)
    {
        $counter = SystemFoodMeal::where('system_food_id',$systemfood)->where('meal_id',$meal)->first();
        if($counter){
            return true;
        }
        return false;
    }

}


if (!function_exists('systemphysics')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function systemphysics($systemphysic,$physical)
    {
        $counter = SystemPhysicPhysical::where('system_physic_id',$systemphysic)->where('physical_id',$physical)->first();
        if($counter){
            return true;
        }
        return false;
    }

}
if (!function_exists('physical')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function physical($systemphysic)
    {
        $counter = SystemPhysicPhysical::where('system_physic_id',$systemphysic)->pluck('physical_id');
        $physicals =Physical::find($counter);
        return $physicals;
    }

}



if (!function_exists('specialties')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function specialties($specialty,$patient)
    {
        $counter = SpecialtyPatient::where('specialty_id',$specialty)->where('user_id',$patient)->first();
        if($counter){
            return true;
        }
        return false;
    }

}

if (!function_exists('branchessession')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function branchessession($branch,$session)
    {
        $counter = SessionBranch::where('branch_id',$branch)->where('session_id',$session)->first();
        if($counter){
            return true;
        }
        return false;
    }
}


if (!function_exists('branchestrue')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function branchestrue($branch,$user)
    {
        $counter = BranchUser::where('branch_id',$branch)->where('user_id',$user)->first();
        if($counter){
            return true;
        }
        return false;
    }
}


if (!function_exists('rolemodule')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function rolemodule($role,$module)
    {
        $exist = RoleModule::where('module_id',$module)->where('role_id',$role)->first();
        if($exist){
            return true;
        }
        return false;
    }
}
if (!function_exists('module')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function module($role)
    {
        $exist = RoleModule::where('role_id',$role)->pluck('module_id');
        $modules = Module::find($exist);
        return $modules;
    }
}


if (!function_exists('question')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function question( $data)
    {
        $question = Question::where('question','LIKE', '%'.$data.'%')->pluck('id');
       
        return $question;
    }
}


if (!function_exists('userId')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function userId( $data)
    {
        $ids = User::where('name','LIKE', '%'.$data.'%')->pluck('id');
       
        return $ids;
    }
}


if (!function_exists('patient')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function patient($data)
    {
        $patient = User::where('name','LIKE', '%'.$data.'%')->pluck('id');
       
        return $patient;
    }
}
if (!function_exists('branch')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function branch($data)
    {
        $branch = Branch::where('name','LIKE', '%'.$data.'%')->pluck('id');
       
        return $branch;
    }
}

if (!function_exists('counterwork')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function counterwork($data)
    {
        $counter = Workday::where('branch_id',$data)->count();
        return $counter;
    }

}

if (!function_exists('empolyeebranch')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function empolyeebranch($data)
    {
        $users = User::where('role_id','3')->pluck('id');
        $counter = BranchUser::where('branch_id',$data)->whereIn('user_id',$users)->count();
        return $counter;
    }

}
if (!function_exists('doctorbranch')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function doctorbranch($data)
    {
        $users = User::where('role_id','4')->pluck('id');
        $counter = BranchUser::where('branch_id',$data)->whereIn('user_id',$users)->count();
        return $counter;
    }

}

if (!function_exists('trainnerbranch')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function trainnerbranch($data)
    {
        $users = User::where('role_id','5')->pluck('id');
        $counter = BranchUser::where('branch_id',$data)->whereIn('user_id',$users)->count();
        return $counter;
    }

}
if (!function_exists('patientbranch')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function patientbranch($data)
    {
        
        $data = BranchUser::where('user_id',$data)->first();
        $branch = Branch::find($data->branch_id);
        return $branch->name;
    }

}

if (!function_exists('employeebranches')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function employeebranches($data)
    {
        $branches = Branch::pluck('id');
        $datas = BranchUser::where('user_id',$data)->whereIn('branch_id',$branches)->get();
        $print ='';
        if(count($datas)>0){
        foreach($datas as $key=>$row){
            $branch = Branch::where('id',$row->branch_id)->whereIn('id',$branches)->first();
            $print .=$branch['name'];
            if($key+1!== count($datas)){
                $print .= ' - ';
            }
        }
        }
        
        
        return $print;
    }

}

if (!function_exists('clinicsbranch')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function clinicsbranch($data)
    {
        $counter = SpecialtyBranch::where('branch_id',$data)->count();
        return $counter;
    }

}
if (!function_exists('reservationbranch')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function reservationbranch($data)
    {
        $counter = Reservation::where('branch_id',$data)->count();
        return $counter;
    }

}
if (!function_exists('sessionbranch')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function sessionbranch($data)
    {
        $counter = SessionBranch::where('branch_id',$data)->count();
        return $counter;
    }

}

if (!function_exists('answers')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function answers($data)
    {
        $answer = Answer::where('question_id',$data)->get();
        return $answer;
    }

}


if (!function_exists('questions')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function questions($data)
    {
        $answer = Question::where('answer_id',$data)->get();
        return $answer;
    }

}


if (!function_exists('steps')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function steps($data)
    {
        $service = Step::where('service_id',$data)->get();
        return $service;
    }

}

if (!function_exists('remain')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function remain($data)
    {
        $count = DoctorReservation::where('doctor_reservation_id',$data)->count();
        return $count;
    }

}

if (!function_exists('services')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function services($data)
    {
        $services = Service::where('type_id',$data)->get();
        return $services;
    }

}
if (!function_exists('steps')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function steps($data)
    {
        $steps = Step::where('service_id',$data)->get();
        return $steps;
    }

}
if (!function_exists('diff')){

    /**
     * Return product categories
     * @return LazyCollection
     */
    function diff($data)
    {
        $time = (strtotime($data) - time()) / 60;
        $time = abs($time);
        $time_hour = $time/60;
        if($time >= 60 && $time <= 1440){
             
            return __('web.since').' '.number_format($time_hour,0) . __('web.hour');
        }
        elseif($time > 1440){
            $time_day = $time_hour / 24;
            return __('web.since').' '.number_format($time_day,0) . __('web.day');
        }
return __('web.since').' '.number_format($time,0) . __('web.minute');
    } 
}



if (!function_exists('experiences')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function experiences($data)
    {
        $rows = DoctorExperience::where('user_id',$data)->get();
        return $rows;
    }

}
if (!function_exists('langs')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function langs($data)
    {
        $rows = DoctorLanguage::where('user_id',$data)->get();
        return $rows;
    }

}

if (!function_exists('aboutdescription')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function aboutdescription($data)
    {
        $rows = About::where('id',$data)->first();
        return $rows['description'];
    }

}
if (!function_exists('wbsetting')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function wbsetting($data)
    {
        $rows = Wbsetting::where('key',$data)->first();
        return $rows['value'];
    }

}

if (!function_exists('reads')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function reads($data)
    {
        $ids = History::where('user_id',$data)->pluck('id');
        $rows = Read::whereIn('history_id',$ids)->get();
        
        return $rows;
    }

}

if (!function_exists('read')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function read($device_id,$patient_id)
    {
        $rows = Read::where('device_id',$device_id)->where('user_id',$patient_id)->get();
        return $rows;
    }

}
if (!function_exists('branchname')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function branchname($id)
    {
        $rows = Branch::where('id',$id)->first();
        return $rows['name'];
    }

}
if (!function_exists('servicename')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function servicename($id)
    {
        $rows = Service::where('id',$id)->first();
        return $rows['name'];
    }

}
if (!function_exists('patientname')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function patientname($id)
    {
        $rows = User::where('id',$id)->first();
        return $rows['name'];
    }

}
if (!function_exists('doctorname')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function doctorname($id)
    {
        $rows = User::where('id',$id)->first();
        return $rows['name'];
    }

}


if (!function_exists('modulerole')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function modulerole($id)
    {
        $user_id= \Auth::user()->role_id;
        $rows = RoleModule::where('role_id',$user_id)->where('module_id',$id)->first();
        if($rows){
            return true;
        }
        return false;
    }

}


if (!function_exists('notin')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function notin($id)
    {
        
        $rows = User::where('id',\Auth::user()->id)->whereNotIn('role_id',[4,5,6])->first();
        if($rows){
            return true;
        }
        return false;
    }

}


if (!function_exists('checkquestion')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function checkquestion($id)
    {
        
        $rows = MedicalQuestion::where('top_id',$id)->first();
        if($rows){
            return true;
        }
        return false;
    }

}

if (!function_exists('questionsanswer')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function questionsanswer($id)
    {
        
        $rows = Question::where('answer_id',$id)->get();
        
        return $rows;
    }

}
if (!function_exists('questionanswer')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function questionanswer($id)
    {
        
        $rows = MedicalQuestion::where('top_id',$id)->first();
        
        return $rows['question_id'];
    }

}


if (!function_exists('answer')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function answer($data)
    {
        $answer = MedicalQuestion::where('top_id',$data)->first();
        return $answer['answer_id'];
    }

}


if (!function_exists('mealscode')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function mealscode($data,$type)
    {
        $all = ReservationMeal::where('reservation_id',$data)->where('type',$type)->get();
        return $all;
    }

}

if (!function_exists('codephysics')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function codephysics($code,$physical)
    {
        $data = CodePhyscal::where('code_id',$code)->where('physcal_id',$physical)->first();
        if($data){
           return true; 
        }
        return false;
    }

}

if (!function_exists('calorie')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function calorie($data)
    {
        $data = CategoryCalorie::where('id',$data)->first();
        
        return $data->name;
    }

}

if (!function_exists('categoryfood')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function categoryfood($data)
    {
        
        $data = FoodCategory::where('id',$data)->first();
        
        return $data['name'];
    }

}

if (!function_exists('systemcategory')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function systemcategory($data)
    {
        $cat = FoodCategory::where('id',$data)->first();
        $data = SystemFood::where('id',$cat['food_system_id'])->first();
        
        return $data['name'];
    }

}

if (!function_exists('idssystem')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function idssystem($data)
    {
        
        
        $cat = FoodCategory::where('food_system_id',$data)->pluck('id');
        
        return $cat;
    }

}
if (!function_exists('idscode')){

    /**
     * Return module name to module by module path from modules table
     * most used in blades views
     * @param $modulePath
     * @param null $local
     * @return mixed
     */
    function idscode($data)
    {
        
        
        $cat = FoodCategory::where('name', 'LIKE', '%'.$data.'%')->pluck('id');
        
        return $cat;
    }

}



