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/dev-hrms/app/Http/Controllers/EmployeeAddressController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\EmployeeAddress;
use App\Models\EmployeeProfile;
use App\Models\Profiles;
use App\Models\Approval;

use App\Library\UserLib;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;

class EmployeeAddressController extends Controller
{
    protected $userLib;
    protected $UserSessionId;
    protected $TeamList = array();
    protected $LevelList = array();
    protected $ViewProfileIds = array();
    protected $ViewApprovalUsers = array();

    public function __construct(UserLib $userLib)
    {
        $this->userLib = $userLib;
    }

    public function getAccessUserProfileList($ProfileId = '')
    {
        $users = Profiles::select('*')
            ->leftJoin('tbl_roles', 'tbl_roles.RoleId', '=', 'tbl_profiles.RoleId')
            ->where('tbl_roles.ReadPermission', 'like', '1')
            ->where('tbl_roles.WritePermission', 'like', '1')
            ->where('tbl_roles.UpdatePermission', 'like', '1')
            ->where('tbl_roles.ApprovalPermission', 'like', '1')
            ->where('tbl_profiles.ServiceBookApprovalPermission', 'like', 'YES')
            ->where('tbl_profiles.ProfileId', 'like', $ProfileId) //check not equal to user profile                   
            ->get();

        if ($users->count() > 0) {   //email is exist                
            foreach ($users as $key => $UValue) {
                $ProfileId = $UValue['ProfileId'];
                $ProfileName = $UValue['ProfileName'];
                $ParentId = $UValue['ParentId'];
                $this->TeamList[] = $UValue;
                $this->ViewProfileIds[] = $ProfileId;
                if ($ParentId) {
                    $this->getAccessUserProfileList($ParentId);
                }
            }
        }

        return $responseData = $this->ViewProfileIds;

        //return response()->json($responseData);
    }



    // Get Approval User 
    public function getApprovalUserLevel($ProfileId = '', $Level = 1)
    {

        $users = Profiles::select('*')
            ->leftJoin('tbl_roles', 'tbl_roles.RoleId', '=', 'tbl_profiles.RoleId')
            ->where('tbl_roles.ReadPermission', 'like', '1')
            ->where('tbl_roles.WritePermission', 'like', '1')
            ->where('tbl_roles.UpdatePermission', 'like', '1')
            ->where('tbl_roles.ApprovalPermission', 'like', '1')
            //->where('tbl_profiles.ServiceBookApprovalPermission','like','YES')
            ->where('tbl_profiles.ProfileId', 'like', $ProfileId) //check not equal to user profile                   
            ->get();

        // echo $users;
        //exit;

        if ($users->count() > 0) {   //email is exist                
            foreach ($users as $key => $UValue) {
                $ProfileId = $UValue['ProfileId'];
                $ProfileName = $UValue['ProfileName'];
                $ParentId = $UValue['ParentId'];
                $this->TeamList[] = $UValue;
                $this->ViewApprovalUsers[] = $ProfileId;
                //$this->LevelList[] = $Level;
                //echo $Level.'-'.$ParentId.'<br/>';

                if ($Level >= 2) {
                    continue;
                }
                if ($ParentId) {
                    $Level++;
                    $this->getApprovalUserLevel($ParentId, $Level);
                }
            }
        }
        return $this->ViewApprovalUsers;
    }

    public function pendingEmployeeAddressList()
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }

        $GetEmployeeAddress = EmployeeAddress::select('tbl_employees_address.EmployeeId as EmployeeId')
            ->where('Status', '=', '1')
            ->where('IsDeleted', '=', '0')
            ->get();
        $GetPendingEmpAddress = EmployeeProfile::select('tbl_employees.*', 'tbl_designation.DesignationName as DesignationName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_employees.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_designation', 'tbl_employees.DesignationId', '=', 'tbl_designation.DesignationId')
            ->where('tbl_employees.Status', '=', '1')
            ->where('tbl_employees.IsDeleted', '=', '0')
            ->whereNotIn('tbl_employees.EmployeeId', $GetEmployeeAddress)
            ->get();

        // print_r(json_encode($GetPendingHindiEmp));
        // exit;

        return view('admin.EmployeeProfile.employee-address', ['GetPendingEmpAddress' => $GetPendingEmpAddress]);
    }

    #### Store Employee Address
    public function storeEmployeeAddress(Request $request)
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }

        $SameAsPermanent = '';
        if ($request->input('SameAsPermanent')) {
            $SameAsPermanent = '1';
        } else {
            $SameAsPermanent = '0';
        }

        // echo $request->input('HomeTownDeclaration');
        // exit;

        $address['EmployeesAddressId'] = $this->userLib->useridGenrator();
        $address['EmployeeId'] = $request->input('EmployeeId');
        $address['EmployeeCode'] = $request->input('EmployeeCode');
        $address['HomeTownDeclaration'] = $request->input('HomeTownDeclaration');
        //$address['HomeTownBlockYear'] = $request->input('HomeTownBlockYear');
        $address['PermanentStreetAddress'] = $request->input('PermanentStreetAddress');
        $address['PermanentCountry'] = $request->input('PermanentCountry');
        $address['PermanentState'] = $request->input('PermanentState');
        $address['PermanentCity'] = $request->input('PermanentCity');
        $address['PermanentDistrict'] = $request->input('PermanentDistrict');
        $address['PermanentPinCode'] = $request->input('PermanentPinCode');
        $address['SameAsPermanent'] = $SameAsPermanent;
        $address['CurrentStreetAddress'] = $request->input('CurrentStreetAddress');
        $address['CurrentCountry'] = $request->input('CurrentCountry');
        $address['CurrentState'] = $request->input('CurrentState');
        $address['CurrentDistrict'] = $request->input('CurrentDistrict');
        $address['CurrentCity'] = $request->input('CurrentCity');
        $address['CurrentPinCode'] = $request->input('CurrentPinCode');
        $address['Created_by'] = session('UserId');
        $address['Created_at'] = date('Y-m-d H:i:s', time());
        $address['Updated_by'] = session('UserId');
        $address['Updated_at'] = date('Y-m-d H:i:s', time());
        $address['Status'] = '1';
        $address['IsDeleted'] = '0';
        $address['OrgId'] = '';
        $address['OwnerId'] = '';
        $employee_address = EmployeeAddress::insert($address);

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

    ### Edit Employee Address
    public function editEmployeeAddress(Request $request)
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }

        $EmployeesAddressId = $request->input('EmployeesAddressId');
        $EmployeeAddress = EmployeeAddress::select('tbl_employees_address.*', 'tbl_employees.EmployeeName as EmployeeName')
            ->leftJoin('tbl_employees', 'tbl_employees_address.EmployeeId', '=', 'tbl_employees.EmployeeId')
            ->where('tbl_employees_address.EmployeesAddressId', $EmployeesAddressId)
            //->where('IsDeleted', '=', '0')
            ->get()->first();

        return response()->json([
            'status' => 200,
            'EmployeeAddress' => $EmployeeAddress,
        ]);
    }

    #### Update Employee Address
    public function updateEmployeeAddress(Request $request)
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }

        $EmployeesAddressId = $request->input('EmployeesAddressId');
        $SameAsPermanent = '';
        if ($request->input('SameAsPermanent')) {
            $SameAsPermanent = '1';
        } else {
            $SameAsPermanent = '0';
        }

        // echo $request->input('HomeTownDeclaration');
        // exit;

        $address['HomeTownDeclaration'] = $request->input('HomeTownDeclaration');
        //$address['HomeTownBlockYear'] = $request->input('HomeTownBlockYear');
        $address['PermanentStreetAddress'] = $request->input('PermanentStreetAddress');
        $address['PermanentCountry'] = $request->input('PermanentCountry');
        $address['PermanentState'] = $request->input('PermanentState');
        $address['PermanentCity'] = $request->input('PermanentCity');
        $address['PermanentDistrict'] = $request->input('PermanentDistrict');
        $address['PermanentPinCode'] = $request->input('PermanentPinCode');
        $address['SameAsPermanent'] = $SameAsPermanent;
        $address['CurrentStreetAddress'] = $request->input('CurrentStreetAddress');
        $address['CurrentCountry'] = $request->input('CurrentCountry');
        $address['CurrentState'] = $request->input('CurrentState');
        $address['CurrentDistrict'] = $request->input('CurrentDistrict');
        $address['CurrentCity'] = $request->input('CurrentCity');
        $address['CurrentPinCode'] = $request->input('CurrentPinCode');
        $address['Updated_by'] = session('UserId');
        $address['Updated_at'] = date('Y-m-d H:i:s', time());

        $EmployeeAddress = EmployeeAddress::where('EmployeesAddressId', $EmployeesAddressId)->update($address);
        if ($EmployeeAddress) {
            return response()->json('status_success');
        } else {
            return response()->json('status_failed');
        }
    }

    ### Change Status
    public function changeStatusEmpAddress(Request $request)
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }

        $EmployeesAddressId = $request->input('EmployeesAddressId');
        $data = EmployeeAddress::where('EmployeesAddressId', $EmployeesAddressId)->get()->first();

        if ($data->Status == '1') {
            $Status = '0';
        } else {
            $Status = '1';
        }

        $values = array();
        $values['Status'] = $Status;
        $values['Updated_by'] = session('UserId');
        $values['Updated_at'] = date('Y-m-d H:i:s', time());
        $ChangeStatus = EmployeeAddress::select('tbl_employees_address.*')->where('EmployeesAddressId', $EmployeesAddressId)->update($values);
        if ($ChangeStatus) {
            return response()->json('status_success');
        } else {
            return response()->json('status_failed');
        }
    }

    #### Delete Employee Address
    public function deleteEmployeeAddress(Request $request)
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }

        $data = array();
        $EmployeesAddressId = $request->input('EmployeesAddressId');
        $data['IsDeleted'] = 1;
        $data['Status'] = 0;
        $data['Updated_by'] = session('UserId');
        $data['Updated_at'] = date('Y-m-d H:i:s', time());

        $deleteData = EmployeeAddress::where('EmployeesAddressId', $EmployeesAddressId)->update($data);
        if ($deleteData) {
            return response()->json('status_success');
        } else {
            return response()->json('status_failed');
        }
    }

    #### Delete Permanent Employee Address
    public function destroyEmployeeAddress(Request $request)
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }
        $EmployeeAddressId = $request->input('EmployeeAddressId');
        $destroyData = EmployeeAddress::where('EmployeeAddressId', $EmployeeAddressId);
        $destroyData->delete();
        if ($destroyData) {
            return response()->json('status_success');
        } else {
            return response()->json('status_failed');
        }
    }

    #### Submit For Approval
    public function SubmitEmployeeAddressApproval(Request $request, $ProfileId = '')
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }
        $data = array();
        $EmployeeId = $request->input('EmployeeId');
        $ProfileId = $request->input('ProfileId');
        $UserId = $request->input('UserId');
        $ApprovalRemark = $request->input('ApprovalRemark');
        // In case of Farward for review
        $ModuleId = $request->input('ModuleId');

        $data['ApprovalStatus'] = 0;
        $data['Updated_by'] = session('UserId');
        $data['Updated_at'] = date('Y-m-d H:i:s', time());

        if ($ModuleId) {
            $GetNoOfApproval = EmployeeAddress::where('EmployeeId', $EmployeeId)
                ->where('ApprovalLock', '1')
                ->where('ApprovalStatus', '=', '0')
                ->where('IsDeleted', '0')
                ->get();

            foreach ($GetNoOfApproval as $key => $ApprovalCount) {
                $NoOfApproval = $ApprovalCount->NoOfApproval + 1;

                // Update Employee PayMatrix Table in case of Farward for review
                $data['NoOfApproval'] = $NoOfApproval;

                $ApprovalRequest = EmployeeAddress::where('EmployeeId', $EmployeeId)
                    ->where('ApprovalLock', '1')
                    ->where('ApprovalStatus', '0')
                    ->where('IsDeleted', '0')
                    ->update($data);
            }

            // Update Approval Table in case of Farward for review
            $update_approval['ApprovalStatus'] =  '3'; // Approve = 1, Reject = 2, Forward = 3.
            $update_approval['ApprovalLevel'] = $NoOfApproval;
            $update_approval['Updated_by'] = session('UserId');
            $update_approval['Updated_at'] = date('Y-m-d H:i:s', time());

            $Update_Approval_Module = Approval::where('ModuleId', $ModuleId)
                ->where('ProfileId', session('ProfileId'))
                ->where('UserId', session('UserId'))
                ->where('ApprovalStatus', '=', '0')
                ->update($update_approval);

            if ($Update_Approval_Module) {

                $approval =  array();
                $approval['ModuleId'] =  $ModuleId;
                $approval['EmployeeId'] =  $EmployeeId;
                $approval['ModuleTitle'] =  'Address';
                $approval['ApprovalStatus'] =  '0';
                $approval['ApprovalComments'] =  $ApprovalRemark;
                $approval['ApprovalFrom'] = session('UserId');
                $approval['ProfileId'] = $ProfileId;
                $approval['UserId'] = $UserId;

                //$approval['ApprovalLevel'] = '';
                $approval['Created_at'] = date('Y-m-d H:i:s', time());
                $approval['Created_by'] = session('UserId');
                $approval['Updated_by'] = session('UserId');
                $approval['Updated_at'] = date('Y-m-d H:i:s', time());
                $Approval_Module_New = Approval::insert($approval);

                if ($Approval_Module_New) {
                    return response()->json('status_success');
                } else {
                    return response()->json('status_fail');
                }
            }
        } else {
            $data['ApprovalLock'] = 1;
            $data['NoOfApproval'] = 0;
            $data['ApprovalComment'] = '';
            $ApprovalRequest = EmployeeAddress::where('EmployeeId', $EmployeeId)
                ->where('ApprovalStatus', '=', '0')
                ->where('ApprovalLock', '0')
                ->where('IsDeleted', '0')
                ->update($data);
            if ($ApprovalRequest) {

                $approval =  array();
                $approval['ModuleId'] =  $this->userLib->useridGenrator();
                $approval['EmployeeId'] =  $EmployeeId;
                $approval['ModuleTitle'] =  'Address';
                $approval['ApprovalStatus'] =  '0';
                $approval['ApprovalComments'] =  $ApprovalRemark;
                $approval['ApprovalFrom'] = session('UserId');
                $approval['ProfileId'] = $ProfileId;
                $approval['UserId'] = $UserId;

                //$approval['ApprovalLevel'] = '';
                $approval['Created_at'] = date('Y-m-d H:i:s', time());
                $approval['Created_by'] = session('UserId');
                $approval['Updated_at'] = date('Y-m-d H:i:s', time());
                $approval['Updated_by'] = session('UserId');

                $Approval_Module = Approval::insert($approval);

                if ($Approval_Module) {
                    return response()->json('status_success');
                } else {
                    return response()->json('status_fail');
                }
            }
        }
    }

    #### Approve Employee Address
    public function ApproveEmployeeAddress(Request $request, $EmployeeId = '', $ModuleId = '')
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }

        $LoginUserProfileId = session('ProfileId');
        $UserId = session('UserId');
        $EmployeeId = $request->input('EmployeeId');
        $ModuleId = $request->input('ModuleId');
        $ApprovalStatusTbl = $request->input('ApprovalStatus');

        $GetNoOfApproval = EmployeeAddress::select('NoOfApproval')
            ->where('EmployeeId', $EmployeeId)
            ->where('ApprovalStatus', '0')
            ->where('ApprovalLock', '1')
            //->where('NoOfApproval', '!=', '0')
            ->where('IsDeleted', '0')
            ->orderBy('id', 'DESC')
            ->get()
            ->first();

        $NoOfApproval = $GetNoOfApproval->NoOfApproval + 1;

        $approval =  array();
        $approval['ApprovalStatus'] = $ApprovalStatusTbl;
        $approval['ApprovalLevel'] = $NoOfApproval;
        $approval['Updated_by'] = session('UserId');
        $approval['Updated_at'] = date('Y-m-d H:i:s', time());
        $ApprovalUpdate = Approval::where('ModuleId', $ModuleId)->where('EmployeeId', $EmployeeId)->where('ProfileId', $LoginUserProfileId)->where('UserId', $UserId)->update($approval);

        if ($ApprovalUpdate) {
            // Update EmployeeAddress Table 
            $data = array();
            $data['ApprovalStatus'] = 1;
            $data['NoOfApproval'] = $NoOfApproval;
            $data['Updated_by'] = session('UserId');
            $data['Updated_at'] = date('Y-m-d H:i:s', time());

            $approveData = EmployeeAddress::where('EmployeeId', $EmployeeId)
                ->where('ApprovalLock', '1')
                ->where('ApprovalStatus', '0')
                ->where('IsDeleted', '0')
                ->update($data);

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

    #### Reject Employee Address
    public function RejectEmployeeAddress(Request $request)
    {
        $this->UserSessionId = session('UserId');
        if (empty($this->UserSessionId)) {
            return redirect('/');
        }

        $data = array();
        $EmployeeId = $request->input('EmployeeId');
        $Comment = $request->input('RejectComment');
        $LoginUserProfileId = session('ProfileId');
        $UserId = session('UserId');
        $ModuleId = $request->input('ModuleId');
        $ApprovalStatusTbl = $request->input('ApprovalStatus');

        $GetNoOfApproval = EmployeeAddress::select('NoOfApproval')
            ->where('EmployeeId', $EmployeeId)
            ->where('ApprovalStatus', '0')
            ->where('ApprovalLock', '1')
            //->where('NoOfApproval', '!=', '0')
            ->where('IsDeleted', '0')
            ->orderBy('id', 'DESC')
            ->get()
            ->first();

        $NoOfApproval = $GetNoOfApproval->NoOfApproval + 1;

        // Rejected = 2,
        $data['ApprovalStatus'] = $ApprovalStatusTbl;
        $data['ApprovalLock'] = 0;
        $data['NoOfApproval'] = $NoOfApproval;
        $data['ApprovalComment'] = $Comment;
        $data['Updated_by'] = session('UserId');
        $data['Updated_at'] = date('Y-m-d H:i:s', time());

        $approval =  array();
        $approval['ApprovalStatus'] = $ApprovalStatusTbl;
        $approval['ApprovalLevel'] = $NoOfApproval;
        $approval['ApprovalComments'] = $Comment;
        $approval['Updated_by'] = session('UserId');
        $approval['Updated_at'] = date('Y-m-d H:i:s', time());

        $ApprovalUpdate = Approval::where('ModuleId', $ModuleId)->where('EmployeeId', $EmployeeId)->where('ProfileId', $LoginUserProfileId)->where('UserId', $UserId)->update($approval);

        if ($ApprovalUpdate) {

            $rejectData = EmployeeAddress::where('EmployeeId', $EmployeeId)
                ->where('ApprovalLock', '1')
                ->where('ApprovalStatus', '0')
                ->where('IsDeleted', '0')->update($data);
            if ($rejectData) {

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