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/VisitController.php
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Visit;
use App\Models\VisitNotes;
use App\Models\Customer;
use App\Models\VisitCustomer;
use App\Models\BeatPlanCustomer;
use App\Library\UserLib;
use DB;

class VisitController extends Controller
{
    protected $userLib;
    public function __construct(UserLib $userLib)
    {
        $this->userLib = $userLib;
    }

    public function allVisits()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $AllVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $ApprovedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->where('tbl_visits.VisitStatus', '=', '1')
            ->where('tbl_visits.CheckedIn', '=', null)
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $RejectedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->where('tbl_visits.VisitStatus', '=', '2')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $TrashedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '1')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();


        return view('admin.visits', ['AllVisit' => $AllVisit, 'ApprovedVisit' => $ApprovedVisit, 'RejectedVisit' => $RejectedVisit, 'TrashedVisit' => $TrashedVisit]);
    }

    #### Approved Visit
    public function approvedVisit()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $AllVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $ApprovedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->where('tbl_visits.VisitStatus', '=', '1')
            ->where('tbl_visits.CheckedIn', '=', null)
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $RejectedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->where('tbl_visits.VisitStatus', '=', '2')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $TrashedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '1')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();


        return view('admin.approved-visit', ['AllVisit' => $AllVisit, 'ApprovedVisit' => $ApprovedVisit, 'RejectedVisit' => $RejectedVisit, 'TrashedVisit' => $TrashedVisit]);
    }

    #### Rejected Visit
    public function rejectedVisit()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $AllVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $ApprovedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->where('tbl_visits.VisitStatus', '=', '1')
            ->where('tbl_visits.CheckedIn', '=', null)
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $RejectedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->where('tbl_visits.VisitStatus', '=', '2')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $TrashedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '1')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();


        return view('admin.rejected-visit', ['AllVisit' => $AllVisit, 'ApprovedVisit' => $ApprovedVisit, 'RejectedVisit' => $RejectedVisit, 'TrashedVisit' => $TrashedVisit]);
    }

    #### Trashed Visit
    public function trashVisit()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $AllVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $ApprovedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->where('tbl_visits.VisitStatus', '=', '1')
            ->where('tbl_visits.CheckedIn', '=', null)
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $RejectedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '0')
            ->where('tbl_visits.VisitStatus', '=', '2')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();
        $TrashedVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visits.IsDeleted', '=', '1')
            ->orderBy('tbl_visits.id', 'desc')
            ->get();

        return view('admin.trash-visit', ['AllVisit' => $AllVisit, 'ApprovedVisit' => $ApprovedVisit, 'RejectedVisit' => $RejectedVisit, 'TrashedVisit' => $TrashedVisit]);
    }

    public function viewVisit($VisitId)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $ViewVisit = Visit::select('tbl_visits.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName', 'tbl_beat_plan.BeatPlanName as BeatPlanName', 'tbl_sales_area.Name as SalesAreaName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by','tbl_visit_notes.VisitNoteTitle as VisitNote')
            ->leftJoin('tbl_admin', 'tbl_visits.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_customer', 'tbl_visits.CustomerId', '=', 'tbl_customer.CustomerId')
            ->leftJoin('tbl_beat_plan', 'tbl_visits.BeatPlanId', '=', 'tbl_beat_plan.BeatPlanId')
            ->leftJoin('tbl_sales_area', 'tbl_visits.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_visit_notes', 'tbl_visits.VisitNoteId', '=', 'tbl_visit_notes.VisitNoteId')
            ->where('VisitId', $VisitId)
            ->get()->first();

            if ($ViewVisit->CheckedIn && $ViewVisit->CheckedOut) {
                $InTime = $ViewVisit->CheckedIn;
                $OutTime = $ViewVisit->CheckedOut;

                $t1 = strtotime($InTime);
                $t2 = strtotime($OutTime);
                $TimeSpent = gmdate('H:i', $t2 - $t1) . ':00';
            } else {
                $TimeSpent = 'Awaiting updates';
            }



            $VisitCustomerList = VisitCustomer::select('tbl_visit_customer.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->where('tbl_visit_customer.VisitId', $VisitId)
            ->leftJoin('tbl_admin', 'tbl_visit_customer.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_customer', 'tbl_visit_customer.CustomerId', '=', 'tbl_customer.CustomerId')
            ->where('tbl_visit_customer.Status', '=', '1')
            ->where('tbl_visit_customer.IsDeleted', '=', '0')
            ->get();

        return view('admin.view-visit', ['ViewVisit' => $ViewVisit, 'VisitCustomerList' => $VisitCustomerList, 'TimeSpent' => $TimeSpent]);
    }

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

        $CustomerIds = '';
        if ($request->has('Customer')) {
            $getCustomerIds = $request->input('Customer');
            $CustomerIds = implode(',', $getCustomerIds);
        }

        if ($request->has('VisitStatus')) {
            $VisitStatus = '1';
        } else {
            $VisitStatus = '0';
        }

        $data['VisitDate'] = '';
        if ($request->input('Date')) {
            $Date = str_replace('/', '-', $request->input('Date'));
        }
        $data['VisitDate'] = date('d-m-Y', strtotime($Date));


        $data['VisitType'] =  $request->input('VisitType');
        $data['UserId'] = $request->input('UserName');
        $data['AssignTo'] = $request->input('UserName');
        $data['BeatPlanId'] = $request->input('BeatPlan');
        $data['SalesAreaId'] = $request->input('SalesArea');
        $data['CustomerId'] = $CustomerIds;
        $data['VisitStatus'] = $VisitStatus;
        $data['StatusComment'] = '';
        $data['IsDeleted'] = 0;
        $data['Status'] = 1;
        $data['Created_by'] = session('UserId');
        $data['Created_at'] = date('Y-m-d H:i:s', time());
        $data['Updated_by'] = session('UserId');
        $data['Updated_at'] = date('Y-m-d H:i:s', time());
        $data['DeviceName'] = 'WEB';

        // $insert_visit = Visit::insert($data);

        if ($data) {
            $latestVisit = Visit::orderBy('Created_at', 'DESC')->first();
            $CreatedDate = date('Y-m-d H:i:s', time());

            if ($request->input('BeatPlan')) {
                $BeatPlanId = $request->input('BeatPlan');
                $BeatPlanCustomers = BeatPlanCustomer::select('tbl_beat_plan_customers.*')
                    ->where('BeatPlanId', $BeatPlanId)
                    ->get();

                if ($BeatPlanCustomers->count() > 0) {
                    $VCinsert = 0;
                    $Vinsert = 0;
                    foreach ($BeatPlanCustomers as $key => $BPValue) {

                        $CustomerId = $BPValue->CustomerId;

                        $g_VisitId = $this->userLib->useridGenrator();
                        $data['VisitId'] = $g_VisitId;
                        $g_VisitNo = "VISIT-" . time() . '-' . rand();
                        $data['VisitNo'] = $g_VisitNo;
                        $data['CustomerId'] = $CustomerId;

                        $insert_visit = Visit::insert($data);
                        if ($insert_visit) {
                            $VCinsert = $VCinsert + 1;
                        }

                        $VisitId = $g_VisitId;
                        $VisitNo = $g_VisitNo;
                        $VisitType =  $request->input('VisitType');
                        $UserId = $request->input('UserName');
                        $VisitDate = 0;
                        if ($request->input('Date')) {
                            $VDate = str_replace('/', '-', $request->input('Date'));
                        }
                        $VisitDate = date('d-m-Y', strtotime($VDate));
                        $BeatPlanId = $request->input('BeatPlan');
                        $BeatCustomerId = $CustomerId;
                        $DeviceName = 'WEB';
                        $IsDeleted = '0';
                        $Status = '1';

                        $InsertVisit = DB::connection('pgsql')->insert("insert into salesforce.Visit__c (Visit_Note_Id__c,Visit_Type__c,User_Id__c,Visit_Date__c,BeatPlan_Id__c,Customer_Id__c,Device_Name__c,IsDeleted,Status__c,Name) values ('$VisitId','$VisitType','$UserId','$VisitDate','$BeatPlanId','$BeatCustomerId','$DeviceName','$IsDeleted','$Status','$VisitNo')");
                        if ($InsertVisit) {
                            $Vinsert = $Vinsert + 1;
                        }
                    }
                }
            }
            else {
                // Insert Customer to tbl_visit_customer
                $CustomerIdsArray = $request->input('Customer');
                if ($CustomerIdsArray) {
                    if (count($CustomerIdsArray) > 0) {
                        $VCinsert = 0;
                        $Vinsert = 0;
                        for ($k = 0; $k < count($CustomerIdsArray); $k++) {
                            $CustomerId = $CustomerIdsArray[$k];
                            if ($CustomerId) {
                                $g_VisitId = $this->userLib->useridGenrator();
                                $data['VisitId'] = $g_VisitId;
                                $g_VisitNo = "VISIT-" . time() . '-' . rand();
                                $data['VisitNo'] = $g_VisitNo;
                                $data['CustomerId'] = $CustomerId;
                                $insert_visit = Visit::insert($data);
                                if ($insert_visit) {
                                    $VCinsert = $VCinsert + 1;
                                }

                                $VisitId = $g_VisitId;
                                $VisitNo = $g_VisitNo;
                                $VisitType =  $request->input('VisitType');
                                $UserId = $request->input('UserName');
                                $VisitDate = '';
                                if ($request->input('Date')) {
                                    $VDate = str_replace('/', '-', $request->input('Date'));
                                }
                                $VisitDate = date('d-m-Y', strtotime($VDate));
                                $SalesAreaId = $request->input('SalesArea');
                                $BeatCustomerId = $CustomerId;
                                $DeviceName = 'WEB';
                                $IsDeleted = '0';
                                $Status = '1';

                                $InsertVisit = DB::connection('pgsql')->insert("insert into salesforce.Visit__c (Visit_Note_Id__c,Visit_Type__c,User_Id__c,Visit_Date__c,Customer_Id__c,Device_Name__c,IsDeleted,Status__c,Name) values ('$VisitId','$VisitType','$UserId','$VisitDate','$BeatCustomerId','$DeviceName','$IsDeleted','$Status','$VisitNo')");

                                if ($InsertVisit) {
                                    $Vinsert = $Vinsert + 1;
                                }
                            }
                        }
                        if (count($CustomerIdsArray) == $VCinsert) {
                        }
                        if (count($CustomerIdsArray) == $Vinsert) {
                        }
                    }
                }
            }
        }
        return response()->json('status_success');

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

        $visit_id = $request->input('VisitId');

        if ($request->has('VisitStatus')) {
            $VisitStatus = '1';
        } else {
            $VisitStatus = '0';
        }

        $data['VisitDate'] = '';
        if ($request->input('Date')) {
            $Date = str_replace('/', '-', $request->input('Date'));
        }
        $data['VisitDate'] = date('d-m-Y', strtotime($Date));
        $data['UserId'] = $request->input('UserName');

        if ($request->input('CheckIn')) {
            $GetCheckIn = $request->input('CheckIn');
            $CheckInTime = date('h:i A', strtotime($GetCheckIn));
            $data['CheckedIn'] = $CheckInTime;
        }

        if ($request->input('CheckOut')) {
            $GetCheckOut = $request->input('CheckOut');
            $CheckOutTime = date('h:i A', strtotime($GetCheckOut));
            $data['CheckedOut'] = $CheckOutTime;
        }

        $data['AssignTo'] = $request->input('UserName');
        $data['VisitStatus'] = $VisitStatus;
        $data['StatusComment'] = '';
        $data['Updated_by'] = session('UserId');
        $data['Updated_at'] = date('Y-m-d H:i:s', time());
        $data['DeviceName'] = 'WEB';

        $update_visit = Visit::where('VisitId', $visit_id)->update($data);

        $UserId = $request->input('UserName');
        $VisitDate = '';
        if ($request->input('Date')) {
            $Date = str_replace('/', '-', $request->input('Date'));
        }
        $VisitDate = date('d-m-Y', strtotime($Date));

        if ($request->input('CheckIn')) {
            $GetCheckIn = $request->input('CheckIn');
            $CheckInTime = date('H:i:s', strtotime($GetCheckIn));
            $CheckedIn = $CheckInTime;
        }
        if ($request->input('CheckOut')) {
            $CheckedOut = $request->input('CheckOut');
            $CheckOutTime = date('H:i:s', strtotime($GetCheckOut));
            $CheckedOut = $CheckOutTime;
        }
        $DeviceName = 'WEB';
        $Updated_by = session('UserId');

        DB::connection('pgsql')->update("update salesforce.Visit__c set User_Id__c='$UserId',Visit_Date__c='$VisitDate',Checked_In__c='$CheckedIn',Checked_Out__c='$CheckedOut',Device_Name__c='$DeviceName',Updated_by__c='$Updated_by' where Visit_Note_Id__c = '$visit_id'");

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

    }

    #### Delete Visit
    public function deleteVisit($VisitId)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $UpdatedDate = date('Y-m-d H:i:s', time());

        $data = array();
        $data['Updated_at'] = $UpdatedDate;
        $data['Updated_by'] = session('UserId');
        $data['IsDeleted'] = '1';

        $visit_delete = Visit::where('VisitId', $VisitId)->update($data);
   // Delete Visit On Heroku Database
        DB::connection('pgsql')->update("update salesforce.Visit__c set IsDeleted='1',Status__c='0' where Visit_Note_Id__c = '$VisitId'");

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

    #### Delete Permanent
    public function destroyVisit($VisitId)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $destroyData = Visit::where('VisitId', $VisitId);
        $destroyData->delete();
    }
    #### Restore Visit
    public function restoreVisit($VisitId)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $UpdatedDate = date('Y-m-d H:i:s', time());

        $data = array();
        $data['Updated_at'] = $UpdatedDate;
        $data['Updated_by'] = session('UserId');
        $data['IsDeleted'] = '0';

        $leave_restore = Visit::where('VisitId', $VisitId)->update($data);
        // Restore Visit On Heroku Database
        DB::connection('pgsql')->update("update salesforce.Visit__c set IsDeleted='0',Status__c='1' where Visit_Note_Id__c = '$VisitId'");

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

    #### Delete Selected Record
    public function deleteSelectedVisit(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $id = $request->input('SelectedVisit');
        if ($id) {
            foreach ($id as $VisitId) {
                $checkData = Visit::select('tbl_visits.*')
                    ->where('VisitId', '=', $VisitId)
                    ->get()->first();
                if ($checkData->IsDeleted == '0') {
                    $UpdatedDate = date(
                        'Y-m-d H:i:s',
                        time()
                    );
                    $data = array();
                    $data['Updated_at'] = $UpdatedDate;
                    $data['IsDeleted'] = '1';

                    $selected_entry = Visit::where('VisitId', $VisitId)->update($data);
                    if ($selected_entry) {
                        echo '1';
                    } else {
                        echo '0';
                    }
                } else {
                    $selected_entry = Visit::select('tbl_visits.*')
                        ->where('VisitId', '=', $VisitId)
                        ->get()->first();
                    $selected_entry->delete();
                }
            }
        }

        return redirect()->back()->with('status_success');
    }

    #### Approve Visit
    public function approveVisit(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $UpdatedDate = date('Y-m-d H:i:s', time());
        $VisitId = $request->input('VisitId');
        $Comment = $request->input('ApproveComment');
        $data = array();
        $data['Updated_at'] = $UpdatedDate;
        $data['Updated_by'] = session('UserId');
        $data['VisitStatus'] = '1';
        $data['StatusComment'] = $Comment;
        //unset($data['_token']);

        $leave_approve = Visit::where('VisitId', $VisitId)->update($data);
        if ($leave_approve) {
            return response()->json('status_success');
        } else {
            return response()->json('status_failed');
        }
    }

    #### Reject Visit
    public function rejectVisit(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $UpdatedDate = date('Y-m-d H:i:s', time());
        $VisitId = $request->input('VisitId');
        $Comment = $request->input('RejectComment');
        $data = array();
        $data['Updated_at'] = $UpdatedDate;
        $data['Updated_by'] = session('UserId');
        $data['VisitStatus'] = '2';
        $data['StatusComment'] = $Comment;
        //unset($data['_token']);

        $leave_reject = Visit::where('VisitId', $VisitId)->update($data);
        if ($leave_reject) {
            return response()->json('status_success');
        } else {
            return response()->json('status_failed');
        }
    }

    ### Edit Records
    public function editVisit($VisitId)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $Visit = Visit::select('tbl_visits.*', 'tbl_user.FirstName as FirstName', 'tbl_user.LastName as LastName')
            ->leftJoin('tbl_user', 'tbl_visits.UserId', '=', 'tbl_user.UserId')
            ->where('VisitId', '=', $VisitId)
            ->get()->first();
        return response()->json([
            'status' => 200,
            'Visit' => $Visit,
        ]);
    }
}