HEX
Server: Apache/2.4.46 (Ubuntu)
System: Linux localhost 5.11.0-49-generic #55-Ubuntu SMP Wed Jan 12 17:36:34 UTC 2022 x86_64
User: root (0)
PHP: 7.4.16
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/fieldsblaze-heroku/app/Http/Controllers/ProfileController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Profiles;
use App\Models\UserProfile;
use App\Models\ModuleList;
use App\Models\CompanyProfile;
use App\Models\User;
use App\Library\UserLib;
use DB;

class ProfileController extends Controller
{
    protected $userLib;
    protected $TeamList;
    protected $TeamProfileList;
    public function __construct(UserLib $userLib)
    {
        $this->userLib = $userLib;
    }
    #### All UserProfile
    public function allProfile()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $UserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
            ->where('tbl_profiles.IsDeleted', '=', '0')
            ->orderBy('tbl_profiles.ProfileName', 'desc')
            ->get();
        $ActiveUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '0')
        ->where('tbl_profiles.Status', '=', '1')
        ->orderBy('tbl_profiles.ProfileName', 'desc')
        ->get();
        $InActiveUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '0')
        ->where('tbl_profiles.Status', '=', '0')
        ->orderBy('tbl_profiles.ProfileName', 'desc')
        ->get();
        $TrashUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '1')
        ->orderBy('tbl_profiles.ProfileName', 'desc')
        ->get();

        return view('admin.all-profile', ['UserProfile' => $UserProfile,'ActiveUserProfile' => $ActiveUserProfile, 'InActiveUserProfile' => $InActiveUserProfile, 'TrashUserProfile' => $TrashUserProfile]);
    }

    #### Active UserProfile
    public function activeUserProfile()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $UserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
            ->where('tbl_profiles.IsDeleted', '=', '0')
            ->orderBy('tbl_profiles.ProfileName', 'desc')
            ->get();
        $ActiveUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '0')
        ->where('tbl_profiles.Status', '=', '1')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();
        $InActiveUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '0')
        ->where('tbl_profiles.Status', '=', '0')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();
        $TrashUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '1')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();

        return view('admin.active-profile', ['UserProfile' => $UserProfile,'ActiveUserProfile' => $ActiveUserProfile, 'InActiveUserProfile' => $InActiveUserProfile, 'TrashUserProfile' => $TrashUserProfile]);
    }

    #### In-active UserProfile
    public function inActiveUserProfile()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $UserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
            ->where('IsDeleted', '=', '0')
            ->orderBy('tbl_profiles.ProfileName','desc')
            ->get();
        $ActiveUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '0')
        ->where('tbl_profiles.Status', '=', '1')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();
        $InActiveUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '0')
        ->where('tbl_profiles.Status', '=', '0')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();
        $TrashUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '1')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();

        return view('admin.inactive-profile', ['UserProfile' => $UserProfile,'ActiveUserProfile' => $ActiveUserProfile, 'InActiveUserProfile' => $InActiveUserProfile, 'TrashUserProfile' => $TrashUserProfile]);
    }

    #### Trash UserProfile
    public function trashUserProfile()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $UserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
            ->where('tbl_profiles.IsDeleted', '=', '0')
            ->orderBy('tbl_profiles.ProfileName','desc')
            ->get();
        $ActiveUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '0')
        ->where('tbl_profiles.Status', '=', '1')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();
        $InActiveUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '0')
        ->where('tbl_profiles.Status', '=', '0')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();
        $TrashUserProfile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
        ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
        ->where('tbl_profiles.IsDeleted', '=', '1')
        ->orderBy('tbl_profiles.ProfileName','desc')
        ->get();

        return view('admin.trash-profile', ['UserProfile' => $UserProfile,'ActiveUserProfile' => $ActiveUserProfile, 'InActiveUserProfile' => $InActiveUserProfile, 'TrashUserProfile' => $TrashUserProfile]);
    }


    ### List of Profile
    public function userProfiles()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $Profile = Profiles::select('tbl_profiles.*')
            ->where('Status', '=', '1')
            ->where('IsDeleted', '=', '0')
            ->orderBy('ProfileName')
            ->get();
        return response()->json([
            'Status' => 200,
            'Profile' => $Profile
        ]);
    }

    ### View Profile
    public function viewProfile($ProfileId){
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $Profile = Profiles::select('tbl_profiles.*', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_profiles.Updated_by', '=', 'tbl_admin.UserId')
            ->where('tbl_profiles.ProfileId', $ProfileId)
            ->get()->first();

        $UserProfile = UserProfile::select('*')
            ->where('ProfileId', $ProfileId)
            ->get();

        // $Profile->products = $UserProfile;

        $Profile->UserProfiles = array();
        $FinalHtml = '';

        // $ModuleList = UserProfile::select('*')
        //     ->where('Status', '=', '1')
        //     ->where('ProfileId', $id)
        //     ->OrderBy('ModuleName', 'Asc')
        //     ->get();

        $ModuleList = ModuleList::select('*')
            ->where('Status', '=', '1')
            //->where('ProfileId', $id)
            ->OrderBy('Name', 'Asc')
            ->get();
        $s = 1;
        $index = 0;
        foreach ($ModuleList as $key => $ModuleValue) {
            $ModuleId = $ModuleValue['ModuleId'];
            $Name = $ModuleValue['Name'];
            $R = $ModuleValue['R'];
            $W = $ModuleValue['W'];
            $U = $ModuleValue['U'];
            $D = $ModuleValue['D'];
            $Status = $ModuleValue['Status'];


            //Get data from user profile
            $UserProfile = UserProfile::select('*')
            ->where('Status', '=', '1')
            ->where('ProfileId', $ProfileId)
            ->where('ModuleId', $ModuleId)
            ->OrderBy('ModuleName', 'Asc')
            ->get()->first();

            $UserProfileData = json_decode(json_encode($UserProfile),true);

            $upR = isset($UserProfileData['R']) ? $UserProfileData['R'] : '';
            $upW = isset($UserProfileData['W']) ? $UserProfileData['W'] : '';
            $upU = isset($UserProfileData['U']) ? $UserProfileData['U'] : '';
            $upD = isset($UserProfileData['D']) ? $UserProfileData['D'] : '';




            $RHtmlChecked = '';
            if ($upR) {
                $RHtmlChecked = 'Checked';
            }

            $WHtmlChecked = '';
            if ($upW) {
                $WHtmlChecked = 'Checked';
            }

            $UHtmlChecked = '';
            if ($upU) {
                $UHtmlChecked = 'Checked';
            }

            $DHtmlChecked = '';
            if ($upD) {
                $DHtmlChecked = 'Checked';
            }

            $html = '<tr>
            <td>' . $s . '</td>
            <td>' . $Name . '</td>
            <td>
                <div class="d-inline">
                    <input type="checkbox" name="W"
                        id="W_' . $ModuleId . '" value="' . 'W_' . $index . '_' . $ModuleId . '" class="Create cmcheckbox access-checkbox" ' . $WHtmlChecked . ' style="cursor: not-allowed;" onclick="return false;" />
                    <label for="CustomerCreate"></label>
                </div>
            </td>
            <td>
                <div class="d-inline">
                <input type="checkbox" name="R"
                id="R_' . $ModuleId . '" value="' . 'R_' . $index . '_' . $ModuleId . '" class="Read cmcheckbox access-checkbox" ' . $RHtmlChecked . '  style="cursor: not-allowed;" onclick="return false;" />
            <label for="CustomerCreate"></label>
                </div>
            </td>
            <td>
                <div class="d-inline">
                    <input type="checkbox" name="U"
                    id="U_' . $ModuleId . '" value="' . 'U_' . $index . '_' . $ModuleId . '" class="Update cmcheckbox access-checkbox" ' . $UHtmlChecked . ' style="cursor: not-allowed;" onclick="return false;" />
                    <label for="CustomerCreate"></label>
                </div>
            </td>
            <td>
                <div class="d-inline">
                <input type="checkbox" name="D"
                id="D_' . $ModuleId . '" value="' . 'D_' . $index . '_' . $ModuleId . '" class="Delete cmcheckbox access-checkbox" ' . $DHtmlChecked . ' style="cursor: not-allowed;" onclick="return false;" />
                <label for="CustomerCreate"></label>
                </div>
            </td>
            </tr>';

            $FinalHtml .= $html;

            $s++;
            $index++;
        }
        $Profile->UserProfiles = $FinalHtml;
        return response()->json([
            'Status' => 200,
            'Profile' => $Profile
        ]);
    }


    ### List of Module
    public function ModuleList()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $ModuleList = ModuleList::select('*')
            ->where('Status', '=', '1')
            ->get();
        // return response()->json([
        //     'Status' => 200,
        //     'ModuleList' => $ModuleList
        // ]);
        $s = 1;
        $index = 0;
        foreach ($ModuleList as $key => $ModuleValue) {
            $ModuleId = $ModuleValue['ModuleId'];
            $Name = $ModuleValue['Name'];
            $R = $ModuleValue['R'];
            $W = $ModuleValue['W'];
            $U = $ModuleValue['U'];
            $D = $ModuleValue['D'];
            $Status = $ModuleValue['Status'];

            // <input type="checkbox" name="W"
            //         id="W_'.$ModuleId.'" value="'.'W_'.$ModuleId.'" class="Create cmcheckbox access-checkbox" onclick=\'validateModules("'.$ModuleId.'");\' />

            $html = '<tr>
        <td>' . $s . '</td>
        <td>' . $Name . '</td>
        <td>
            <div class="d-inline">
                <input type="checkbox" name="W"
                    id="W_' . $ModuleId . '" value="' . 'W_' . $index . '_' . $ModuleId . '" class="Create cmcheckbox access-checkbox"  />
                <label for="CustomerCreate"></label>
            </div>
        </td>
        <td>
            <div class="d-inline">
            <input type="checkbox" name="R"
            id="R_' . $ModuleId . '" value="' . 'R_' . $index . '_' . $ModuleId . '" class="Read cmcheckbox access-checkbox" />
        <label for="CustomerCreate"></label>
            </div>
        </td>
        <td>
            <div class="d-inline">
                <input type="checkbox" name="U"
                id="U_' . $ModuleId . '" value="' . 'U_' . $index . '_' . $ModuleId . '" class="Update cmcheckbox access-checkbox" />
                <label for="CustomerCreate"></label>
            </div>
        </td>
        <td>
            <div class="d-inline">
            <input type="checkbox" name="D"
            id="D_' . $ModuleId . '" value="' . 'D_' . $index . '_' . $ModuleId . '" class="Delete cmcheckbox access-checkbox" />
            <label for="CustomerCreate"></label>
            </div>
        </td>
        </tr>';


            echo $html;
            $s++;
            $index++;
        }
    }


    ### Store New Profile
    public function addProfile(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $getCRMId = CompanyProfile::select('tbl_company_profile.*')
            // ->where('IsDeleted', '=', '0')
            // ->where('Status', '=', '1')
            ->get()
            ->first();

        $CRMId = $getCRMId['CrmId'];

        $CreatedDate = date('Y-m-d H:i:s', time());
        $ProfileId = $this->userLib->useridGenrator();

        $ProfileName = $request->input('ProfileTitle');
        $access_role = $request->input('access_role');

        $access_role = json_decode($access_role, true);


        $data = array();

        $data['ProfileId'] = $ProfileId;
        $data['ProfileName'] = $ProfileName;
        $data['ExpenseAmount'] = $request->input('UserExpenseAmount');



        if ($request->input('addStatus')) {
            $data['Status'] = 1;
        } else {
            $data['Status'] = 0;
        }
        $data['Created_at'] = $CreatedDate;
        $data['Created_by'] = session('UserId');
        $data['Updated_at'] = $CreatedDate;
        $data['Updated_by'] = session('UserId');
        $data['CrmId'] =  $CRMId;

        $Profile_insert = Profiles::insert($data);
        if ($Profile_insert) {




            $latestProfileId = Profiles::orderBy('Created_at', 'DESC')->first();
            $CreatedDate = date('Y-m-d H:i:s', time());

            $UserProfileData = UserProfile::select('*')
                ->where('ProfileId', $ProfileId)
                ->get();
            $UserProfileDataCount = $UserProfileData->count();



            if ($UserProfileDataCount > 0) {
            } else {
                $ModuleList = ModuleList::select('*')
                    ->where('Status', '=', '1')
                    ->get();
                $module_count = $ModuleList->count();
                if ($module_count > 0) {
                    foreach ($ModuleList as $key => $ModuleListValue) {
                        $ModuleId = $ModuleListValue->ModuleId;
                        $ModuleName = $ModuleListValue->Name;
                        $pdata = array();
                        $pdata['Status'] = '1';
                        $pdata['IsDeleted'] = '0';
                        $pdata['Updated_at'] = $CreatedDate;
                        $pdata['Created_at'] = $CreatedDate;
                        $pdata['ProfileId'] = $ProfileId;
                        $pdata['ModuleName'] = $ModuleName;
                        $pdata['ModuleId'] = $ModuleId;
                        $pdata['ProfileName'] = $ProfileName;
                        $User_profile_insert = UserProfile::insert($pdata);
                    }
                }
            }

            //update access role
            if (count($access_role) > 0) {
                for ($k = 0; $k < count($access_role); $k++) {
                    $AccessRoleData = $access_role[$k];
                    $item = isset($AccessRoleData['item']) ? $AccessRoleData['item'] : '';
                    $selected = isset($AccessRoleData['selected']) ? $AccessRoleData['selected'] : '';
                    $selectedValue = '0';
                    if ($selected) {
                        $selectedValue = '1';
                    }

                    $itemData = explode("_", $item);
                    $itemKey = $itemData[0];
                    $index = $itemData[1];
                    $ModuleId = $itemData[2];

                    $AccessPermission = array();
                    $AccessPermission[$itemKey] = $selectedValue;

                    $User_profile_update = UserProfile::where('ProfileId', $ProfileId)->where('ModuleId', $ModuleId)->update($AccessPermission);
                }
            }


            return response()->json('status_success');
        } else {
            return response()->json('status_failed');
        }
    }

    ### Edit Profile
    public function editProfile($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $Profile = Profiles::select('*')
            ->where('ProfileId', $id)
            ->get()->first();

        $UserProfile = UserProfile::select('*')
            ->where('ProfileId', $id)
            ->get();

        // $Profile->products = $UserProfile;

        $Profile->UserProfiles = array();
        $FinalHtml = '';

        // $ModuleList = UserProfile::select('*')
        //     ->where('Status', '=', '1')
        //     ->where('ProfileId', $id)
        //     ->OrderBy('ModuleName', 'Asc')
        //     ->get();

        $ModuleList = ModuleList::select('*')
            ->where('Status', '=', '1')
            //->where('ProfileId', $id)
            ->OrderBy('Name', 'Asc')
            ->get();
        $s = 1;
        $index = 0;
        foreach ($ModuleList as $key => $ModuleValue) {
            $ModuleId = $ModuleValue['ModuleId'];
            $Name = $ModuleValue['Name'];
            $R = $ModuleValue['R'];
            $W = $ModuleValue['W'];
            $U = $ModuleValue['U'];
            $D = $ModuleValue['D'];
            $Status = $ModuleValue['Status'];


            //Get data from user profile
            $UserProfile = UserProfile::select('*')
            ->where('Status', '=', '1')
            ->where('ProfileId', $id)
            ->where('ModuleId', $ModuleId)
            ->OrderBy('ModuleName', 'Asc')
            ->get()->first();

            $UserProfileData = json_decode(json_encode($UserProfile),true);

            $upR = isset($UserProfileData['R']) ? $UserProfileData['R'] : '';
            $upW = isset($UserProfileData['W']) ? $UserProfileData['W'] : '';
            $upU = isset($UserProfileData['U']) ? $UserProfileData['U'] : '';
            $upD = isset($UserProfileData['D']) ? $UserProfileData['D'] : '';




            $RHtmlChecked = '';
            if ($upR) {
                $RHtmlChecked = 'Checked';
            }

            $WHtmlChecked = '';
            if ($upW) {
                $WHtmlChecked = 'Checked';
            }

            $UHtmlChecked = '';
            if ($upU) {
                $UHtmlChecked = 'Checked';
            }

            $DHtmlChecked = '';
            if ($upD) {
                $DHtmlChecked = 'Checked';
            }

            $html = '<tr>
            <td>' . $s . '</td>
            <td>' . $Name . '</td>
            <td>
                <div class="d-inline">
                    <input type="checkbox" name="W"
                        id="W_' . $ModuleId . '" value="' . 'W_' . $index . '_' . $ModuleId . '" class="Create cmcheckbox access-checkbox" ' . $WHtmlChecked . '  />
                    <label for="CustomerCreate"></label>
                </div>
            </td>
            <td>
                <div class="d-inline">
                <input type="checkbox" name="R"
                id="R_' . $ModuleId . '" value="' . 'R_' . $index . '_' . $ModuleId . '" class="Read cmcheckbox access-checkbox" ' . $RHtmlChecked . '  />
            <label for="CustomerCreate"></label>
                </div>
            </td>
            <td>
                <div class="d-inline">
                    <input type="checkbox" name="U"
                    id="U_' . $ModuleId . '" value="' . 'U_' . $index . '_' . $ModuleId . '" class="Update cmcheckbox access-checkbox" ' . $UHtmlChecked . ' />
                    <label for="CustomerCreate"></label>
                </div>
            </td>
            <td>
                <div class="d-inline">
                <input type="checkbox" name="D"
                id="D_' . $ModuleId . '" value="' . 'D_' . $index . '_' . $ModuleId . '" class="Delete cmcheckbox access-checkbox" ' . $DHtmlChecked . ' />
                <label for="CustomerCreate"></label>
                </div>
            </td>
            </tr>';

            $FinalHtml .= $html;

            $s++;
            $index++;
        }
        $Profile->UserProfiles = $FinalHtml;
        return response()->json([
            'Status' => 200,
            'Profile' => $Profile
        ]);
    }

    public function updateProfile(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $getCRMId = CompanyProfile::select('tbl_company_profile.*')
            // ->where('IsDeleted', '=', '0')
            // ->where('Status', '=', '1')
            ->get()
            ->first();

        $CRMId = $getCRMId['CrmId'];

        $UpdatedDate = date('Y-m-d H:i:s', time());

        $ProfileId = $request->input('ProfileId');
        $ProfileName = $request->input('ProfileTitle');
        $access_role = $request->input('access_role');

        $access_role = json_decode($access_role, true);

        $data = array();

        $data['ProfileName'] = $ProfileName;
        $data['ExpenseAmount'] = $request->input('UserExpenseAmount');
        $data['Status'] = $request->input('Status');

        $data['Updated_at'] = $UpdatedDate;
        $data['Updated_by'] = session('UserId');
        $data['CrmId'] =  $CRMId;

        $Profile_update = Profiles::where('ProfileId', $ProfileId)->update($data);
        if ($Profile_update) {

            $UserProfileData = UserProfile::select('*')
                ->where('ProfileId', $ProfileId)
                ->get();
            $UserProfileDataCount = $UserProfileData->count();

            // echo $UserProfileDataCount;
            // exit;

            // if ($UserProfileDataCount > 0) {
            // } else {
            //     $ModuleList = ModuleList::select('*')
            //         ->where('Status', '=', '1')
            //         ->get();
            //     $module_count = $ModuleList->count();
            //     if ($module_count > 0) {
            //         foreach ($ModuleList as $key => $ModuleListValue) {
            //             $ModuleId = $ModuleListValue->ModuleId;
            //             $ModuleName = $ModuleListValue->Name;
            //             $pdata = array();
            //             $pdata['Status'] = '1';
            //             $pdata['IsDeleted'] = '0';
            //             $pdata['Updated_at'] = $UpdatedDate;
            //             $pdata['ProfileId'] = $ProfileId;
            //             $pdata['ModuleName'] = $ModuleName;
            //             $pdata['ModuleId'] = $ModuleId;
            //             $pdata['ProfileName'] = $ProfileName;
            //             $User_profile_insert = UserProfile::insert($pdata);
            //         }
            //     }
            // }

            //update access role
            if (count($access_role) > 0) {
                for ($k = 0; $k < count($access_role); $k++) {
                    $AccessRoleData = $access_role[$k];
                    $item = isset($AccessRoleData['item']) ? $AccessRoleData['item'] : '';
                    $selected = isset($AccessRoleData['selected']) ? $AccessRoleData['selected'] : '';
                    $selectedValue = '0';
                    if ($selected) {
                        $selectedValue = '1';
                    }

                    $itemData = explode("_", $item);
                    $itemKey = $itemData[0];
                    $index = $itemData[1];
                    $ModuleId = $itemData[2];

                    $ModuleName ='';
                    if($ModuleId)
                    {
                        $ModuleList = ModuleList::select('*')
                        ->where('Status', '=', '1')
                        ->where('ModuleId', $ModuleId)
                        ->OrderBy('Name', 'Asc')
                        ->get()->first();
                        $ModuleName = isset($ModuleList->Name) ? $ModuleList->Name : '';
                    }

                    $AccessPermission = array();
                    $AccessPermission[$itemKey] = $selectedValue;

                    //echo json_encode($AccessPermission);

                    $User_profile_validate = UserProfile::where('ProfileId', $ProfileId)->where('ModuleId', $ModuleId)->get();
                    if($User_profile_validate->count() > 0)
                    {
                        $User_profile_update = UserProfile::where('ProfileId', $ProfileId)->where('ModuleId', $ModuleId)->update($AccessPermission);
                        //echo "updated";
                    }
                    else
                    {

                        $pdata = array();
                        $pdata['Status'] = '1';
                        $pdata['IsDeleted'] = '0';
                        $pdata['Created_at'] = $UpdatedDate;
                        $pdata['Created_by'] = session('UserId');
                        $pdata['Updated_by'] = session('UserId');
                        $pdata['Updated_at'] = $UpdatedDate;
                        $pdata['ProfileId'] = $ProfileId;
                        $pdata['ModuleName'] = $ModuleName;
                        $pdata['ModuleId'] = $ModuleId;
                        $pdata['ProfileName'] = $ProfileName;
                        $User_profile_insert = UserProfile::insert($pdata);
                        //echo "insert";
                    }

                }
            }
            return response()->json('status_success');
        } else {
            return response()->json('status_failed');
        }
    }

    ### Check if Profile Associated with User
    public function checkUserProfile($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $GetUserProfile = User::select('tbl_user.*')
            ->where('ProfileId', $id)
            ->get()->first();

        if ($GetUserProfile) {
            return response()->json(array("exists" => true));
        } else {
            return response()->json(array("exists" => false));
        }
    }


    #### Activate Profile
    public function activateProfile($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $CreatedDate = date('Y-m-d H:i:s', time());

        $value = array();
        $value['Updated_at'] = $CreatedDate;
        $value['Updated_by'] = session('UserId');
        $value['Status'] = '1';
        Profiles::where('ProfileId', $id)->update($value);
    }

    #### De-Activate Profile
    public function deActivateProfile($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $CreatedDate = date('Y-m-d H:i:s', time());

        $value = array();
        $value['Updated_at'] = $CreatedDate;
        $value['Updated_by'] = session('UserId');
        $value['Status'] = '0';
        $deactivate_profile = Profiles::where('ProfileId', $id)->update($value);

        if ($deactivate_profile) {
            return response()->json('status_success');
        }
    }

    #### Temp Delete Profile
    public function deleteProfile($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $CreatedDate = date('Y-m-d H:i:s', time());

        $value = array();
        $value['Updated_at'] = $CreatedDate;
        $value['Updated_by'] = session('UserId');
        $value['Status'] = '0';
        $value['IsDeleted'] = '1';
        $delete_profile = Profiles::where('ProfileId', $id)->update($value);

        if ($delete_profile) {
            $delete_user_profile = UserProfile::where('ProfileId', $id)->update($value);
            return response()->json('status_success');
        }
    }
    #### Restore Profile
    public function restoreProfile($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $CreatedDate = date('Y-m-d H:i:s', time());
        $value = array();
        $value['Updated_at'] = $CreatedDate;
        $value['Updated_by'] = session('UserId');
        $value['Status'] = '1';
        $value['IsDeleted'] = '0';
        $restore_profile = Profiles::where('ProfileId', $id)->update($value);
    }

    #### Delete Permanent Profile
    public function destroyProfile($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $destroyData = Profiles::where('ProfileId', $id);
        $destroyData->delete();
    }

    ### Check Profile Title if Exist
    public function checkProfileName(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $ProfileName = $request->input('ProfileTitle');
        $id = $request->input('id');
        $GetProfileTitle = Profiles::where('ProfileName', $ProfileName)
            ->orWhere('ProfileName', $id)->first();
        if ($GetProfileTitle) {
            return response()->json(array("exists" => true));
        } else {
            return response()->json(array("exists" => false));
        }
    }

    ### Check Product if Exist in Edit form
    public function checkProfileNameEdit(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $ProfileName = $request->input('ProfileName');
        $id = $request->input('id');
        $GetProfileTitle = Profiles::where('ProfileName', $ProfileName)
            ->where('ProfileName', '!=', $id)
            ->get()->first();

        if ($GetProfileTitle) {
            return response()->json(array("exists" => true));
        } else {
            return response()->json(array("exists" => false));
        }
    }

    ### Get Profile Expense Rate
    public function profileExpenseRate($ProfileId)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $ProfileExpenseRate = Profiles::select('*')
            ->where('ProfileId', $ProfileId)
            ->get()->first();
        return response()->json([
            'Status' => 200,
            'ProfileExpenseRate' => $ProfileExpenseRate
        ]);
    }

    //ReportingManageList


    //Team List
    public function getManagerTeamList($ManagerId='', $ProfileId='') {
        //Check user is exist or not

        if($ProfileId)
        {
            $users = User::select('UserId', 'FirstName', 'LastName', 'Email','Mobile', 'ManagerId')
            ->leftJoin('tbl_profiles', 'tbl_user.ProfileId', '=', 'tbl_profiles.ProfileId')
            ->where('tbl_user.ProfileId',$ProfileId)
            ->orderBy('tbl_profiles.InOrder', 'asc')
            ->get();
        }
        else
        {
            $users = User::select('UserId', 'FirstName', 'LastName', 'Email','Mobile', 'ManagerId')
            ->leftJoin('tbl_profiles', 'tbl_user.ProfileId', '=', 'tbl_profiles.ProfileId')
            ->where('tbl_user.ManagerId',$ManagerId)
            ->orderBy('tbl_profiles.InOrder', 'asc')
            ->get();
        }


        if ($users->count() > 0)
        {
            foreach ($users as $key => $UValue)
            {
                $UserId = $UValue['UserId'];
                $FirstName = $UValue['FirstName'];
                $LastName = $UValue['LastName'];
                $ManagerId = $UValue['ManagerId'];

                //echo "Userid = ".$UserId.", Name = ".$FirstName."<br/>";

                if($UserId)  {
                    $this->TeamList[] = $UserId;
                    $this->getManagerTeamList($UserId);
                }
            }
        }
       return $responseData = $this->TeamList;
    }



    //Team List
    public function getProfileTeamList22($ProfileId='') {
        //Check user is exist or not


      //  $ProfileId='o6kuSRjPCXsXf9J3R00TMYqmUbbvI9uTCk1XH';
            //DB::enableQueryLog();
          $ProfileList = DB::table('tbl_profiles')->select("ProfileId", "ProfileName", "ReportingManagerId")
          ->where('ProfileId',$ProfileId)
          ->get();
          //dd(DB::getQueryLog());

         // echo " -- ProfileId = ".$ProfileId ." === ".$ProfileList->count()."<br/>";

          $ProfileList2 = json_decode(json_encode($ProfileList), true);

          print_r($ProfileList2);

            if ($ProfileList->count() > 0)
            {
                $ProfileList = json_decode(json_encode($ProfileList), true);
                foreach ($ProfileList as $key => $UValue)
                {
                    $ProfileId = $UValue['ProfileId'];
                    $ProfileName = $UValue['ProfileName'];
                    $ReportingManagerId = $UValue['ReportingManagerId'];

                  //  echo "ProfileId = ".$ProfileId.", ProfileName = ".$ProfileName.", ReportingManagerId = ".$ReportingManagerId."<br/>";

                    if($ReportingManagerId)
                    {
                        $this->TeamProfileList[] = $ProfileId;
                        $this->getProfileTeamList($ReportingManagerId);
                    }
                }
            }
            return $responseData = $this->TeamProfileList;

    }

    // public function ReportingManageList222222($ProfileId)
    // {
    //     if (empty(session('UserId'))) {
    //         return redirect('/admin/logout');
    //     }

    //     $TeamUserIds = "";
    //     $TeamUserIds = $this->getProfileTeamList($ProfileId);



    //     $TeamUser = array();
    //    if($TeamUserIds){
    //     $TeamUser = User::select('tbl_user.*', 'tbl_profiles.ProfileName as ProfileName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
    //         ->leftJoin('tbl_profiles', 'tbl_user.ProfileId', '=', 'tbl_profiles.ProfileId')
    //         ->leftJoin('tbl_admin', 'tbl_user.Updated_by', '=', 'tbl_admin.UserId')
    //         ->whereIn('tbl_user.ProfileId', $TeamUserIds)
    //         ->where('tbl_user.IsDeleted', '=', '0')
    //         ->orderBy('tbl_profiles.InOrder', 'asc')
    //         ->get();
    //    }

    //     // $ProfileExpenseRate = Profiles::select('*')
    //     //     ->where('ProfileId', $ProfileId)
    //     //     ->get()->first();
    //     return response()->json([
    //         'Status' => 200,
    //         'TeamUserIds' => $TeamUserIds,
    //         'TeamUser' => $TeamUser
    //     ]);
    // }


    public function getProfileTeamListLIO($ProfileId='') {
        //Check user is exist or not


      //  $ProfileId='o6kuSRjPCXsXf9J3R00TMYqmUbbvI9uTCk1XH';
            //DB::enableQueryLog();
          $ProfileList = Profiles::select("ProfileId", "ProfileName", "ReportingManagerId")
          ->where('ReportingManagerId',$ProfileId)
          ->get();


        //   $ProfileList2 = json_decode(json_encode($ProfileList), true);

        //   print_r($ProfileList2);

            if ($ProfileList->count() > 0)
            {
                //$ProfileList = json_decode(json_encode($ProfileList), true);
                foreach ($ProfileList as $key => $UValue)
                {
                    $ProfileId = $UValue['ProfileId'];
                    $ProfileName = $UValue['ProfileName'];
                    $ReportingManagerId = $UValue['ReportingManagerId'];

                    echo "ProfileId = ".$ProfileId.", ProfileName = ".$ProfileName.", ReportingManagerId = ".$ReportingManagerId."<br/>";

                    if($ReportingManagerId)
                    {
                        $this->TeamProfileList[] = $ProfileId;
                        $this->getProfileTeamList($ProfileId);
                    }
                }
            }
            return $responseData = $this->TeamProfileList;

    }


    public function getProfileTeamList($ProfileId='') {
        //Check user is exist or not


      //  $ProfileId='o6kuSRjPCXsXf9J3R00TMYqmUbbvI9uTCk1XH';
          //DB::enableQueryLog();
          $ProfileList = Profiles::select("ProfileId", "ProfileName", "ReportingManagerId")
          ->where('ProfileId',trim($ProfileId))
          ->get();
         // dd(DB::getQueryLog());


       // echo "ProfileId = ".$ProfileId." ======= ".$ProfileList->count()."<br/>";

            if ($ProfileList->count() > 0)
            {
                //$ProfileList = json_decode(json_encode($ProfileList), true);
                foreach ($ProfileList as $key => $UValue)
                {
                    $ProfileId = $UValue['ProfileId'];
                    $ProfileName = $UValue['ProfileName'];
                    $ReportingManagerId = $UValue['ReportingManagerId'];

                    $ReportingManagerId = trim($ReportingManagerId);

                    if($ReportingManagerId)
                    {
                        $this->TeamProfileList[] = $ProfileId;
                        $this->getProfileTeamList($ReportingManagerId);
                    }
                }
            }
            return $responseData = $this->TeamProfileList;

    }

    public function ReportingManageOptionList($ProfileId='', $SelectedUserId='')
    {
        if (empty(session('UserId'))) {
          //  return redirect('/admin/logout');

        }

        $ProfileList = Profiles::select("ProfileId", "ProfileName", "ReportingManagerId")
        ->where('ProfileId',$ProfileId)
        ->get();

        $ReportingManagerId = '';
        if ($ProfileList->count() > 0)
        {
            $ProfileListData = $ProfileList->first();
            $ReportingManagerId = $ProfileListData->ReportingManagerId;
        }

        $TeamUserIds = "";
        $TeamUserIds = $this->getProfileTeamList($ReportingManagerId);


      //  echo json_encode($TeamUserIds);

        $TeamUser = array();
       if($TeamUserIds){
        $TeamUser = User::select('tbl_user.*', 'tbl_profiles.ProfileName as ProfileName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_profiles', 'tbl_user.ProfileId', '=', 'tbl_profiles.ProfileId')
            ->leftJoin('tbl_admin', 'tbl_user.Updated_by', '=', 'tbl_admin.UserId')
            ->whereIn('tbl_user.ProfileId', $TeamUserIds)
            ->where('tbl_user.IsDeleted', '=', '0')
            ->orderBy('tbl_profiles.InOrder', 'asc')
            ->get();
       }

        // $ProfileExpenseRate = Profiles::select('*')
        //     ->where('ProfileId', $ProfileId)
        //     ->get()->first();
        // return response()->json([
        //     'Status' => 200,
        //     'TeamUserIds' => $TeamUserIds,
        //     'TeamUser' => $TeamUser
        // ]);

        echo "<option value=''>Please select reporting manager</option>";
        echo "<option value='None'>None</option>";
        foreach ($TeamUser as $User) {
            $UserId = $User->UserId;
            $FirstName = $User->FirstName;
            $LastName = $User->LastName;
            $ProfileName = $User->ProfileName;

            $FullName = $FirstName.' '.$LastName;

            if($SelectedUserId == $UserId)
            {
                echo "<option selected value='" . $UserId . "'>" . $FullName ." (".$ProfileName.") "."</option>";
            }
            else
            {
                echo "<option value='" . $UserId . "'>" . $FullName ." (".$ProfileName.") ". "</option>";
            }
        }
    }

}