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

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\BeatPlan;
use App\Models\Customer;
use App\Models\BeatPlanCustomer;
use App\Models\Visits;
use Illuminate\Http\Request;
use App\Library\UserLib;
use DB;

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


    // All Beat Plans
    public function allBeatPlan()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $BeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as AdminUpdated_by', 'tbl_admin.FirstName as AdminCreated_by', 'tbl_user.FirstName as Updated_by', 'tbl_user.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_beat_plan.Updated_by', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $ActiveBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->where('tbl_beat_plan.Status', '=', '1')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $InactiveBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->where('tbl_beat_plan.Status', '=', '0')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $TrashBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '1')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();
        return view('admin.beat-plans', ['BeatPlan' => $BeatPlan, 'ActiveBeatPlan' => $ActiveBeatPlan, 'InactiveBeatPlan' => $InactiveBeatPlan, 'TrashBeatPlan' => $TrashBeatPlan]);
    }

    // Active Plans
    public function activeBeatPlan()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $BeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $ActiveBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as AdminUpdated_by', 'tbl_admin.FirstName as AdminCreated_by', 'tbl_user.FirstName as Updated_by', 'tbl_user.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_beat_plan.Updated_by', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->where('tbl_beat_plan.Status', '=', '1')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $InactiveBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->where('tbl_beat_plan.Status', '=', '0')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $TrashBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '1')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();
        return view('admin.active-beat-plans', ['BeatPlan' => $BeatPlan, 'ActiveBeatPlan' => $ActiveBeatPlan, 'InactiveBeatPlan' => $InactiveBeatPlan, 'TrashBeatPlan' => $TrashBeatPlan]);
    }

    // Inactive Plans
    public function inactiveBeatPlan()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $BeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $ActiveBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->where('tbl_beat_plan.Status', '=', '1')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $InactiveBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as AdminUpdated_by', 'tbl_admin.FirstName as AdminCreated_by', 'tbl_user.FirstName as Updated_by', 'tbl_user.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_beat_plan.Updated_by', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->where('tbl_beat_plan.Status', '=', '0')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $TrashBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '1')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();
        return view('admin.inactive-beat-plans', ['BeatPlan' => $BeatPlan, 'ActiveBeatPlan' => $ActiveBeatPlan, 'InactiveBeatPlan' => $InactiveBeatPlan, 'TrashBeatPlan' => $TrashBeatPlan]);
    }

    ### Trash Beat Plan
    public function trashBeatPlan()
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $BeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

            $ActiveBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->where('tbl_beat_plan.Status', '=', '1')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $InactiveBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '0')
            ->where('tbl_beat_plan.Status', '=', '0')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();

        $TrashBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_admin.FirstName as AdminUpdated_by', 'tbl_admin.FirstName as AdminCreated_by', 'tbl_user.FirstName as Updated_by', 'tbl_user.FirstName as Created_by')
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_user', 'tbl_beat_plan.Updated_by', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->where('tbl_beat_plan.IsDeleted', '=', '1')
            ->orderBy('tbl_beat_plan.id', 'desc')
            ->get();
        return view('admin.trash-beat-plans', ['BeatPlan' => $BeatPlan, 'TrashBeatPlan' => $TrashBeatPlan, 'ActiveBeatPlan' => $ActiveBeatPlan, 'InactiveBeatPlan' => $InactiveBeatPlan]);
    }

    ### View Plan
    public function viewBeatPlan($BeatPlanId)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }

        $ViewBeatPlan = BeatPlan::select('tbl_beat_plan.*', 'tbl_sales_territory.Name as SalesTerritory', 'tbl_sales_area.Name as SalesAreaName', 'tbl_customer.CustomerName as DistributorName', 'tbl_user.FirstName as UserFirstName', 'tbl_user.FirstName as Updated_by', 'tbl_user.FirstName as Created_by')
            ->where('tbl_beat_plan.BeatPlanId', '=', $BeatPlanId)
            ->leftJoin('tbl_admin', 'tbl_beat_plan.Updated_by', '=', 'tbl_admin.UserId')
            //->leftJoin('tbl_user', 'tbl_beat_plan.Updated_by', '=', 'tbl_user.UserId')
            ->leftJoin('tbl_sales_territory', 'tbl_beat_plan.SalesTerritoryId', '=', 'tbl_sales_territory.TerritoryId')
            ->leftJoin('tbl_sales_area', 'tbl_beat_plan.SalesAreaId', '=', 'tbl_sales_area.SalesAreaId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan.DistributorNameId', '=', 'tbl_customer.CustomerId')
            ->leftJoin('tbl_user', 'tbl_beat_plan.AssignTo', '=', 'tbl_user.UserId')
            // ->leftJoin('tbl_customer', 'tbl_beat_plan.RetailerName', '=', 'tbl_customer.CustomerId')
            ->get()->first();

        $VisitCustomerList = BeatPlanCustomer::select('tbl_beat_plan_customers.*', 'tbl_customer.CustomerName as CustomerName', 'tbl_customer.CustomerCode as CustomerCode', 'tbl_sales_area.Name as SalesArea', 'tbl_admin.FirstName as Updated_by', 'tbl_admin.FirstName as Created_by')
            ->where('tbl_beat_plan_customers.BeatPlanId', $BeatPlanId)
            ->leftJoin('tbl_admin', 'tbl_beat_plan_customers.Updated_by', '=', 'tbl_admin.UserId')
            ->leftJoin('tbl_customer', 'tbl_beat_plan_customers.CustomerId', '=', 'tbl_customer.CustomerId')

            //->leftJoin('tbl_visits', 'tbl_beat_plan_customers.CustomerId', '=', 'tbl_visits.CustomerId')
            //->leftJoin('tbl_visits', 'tbl_beat_plan_customers.BeatPlanId', '=', 'tbl_visits.BeatPlanId')

            ->leftJoin('tbl_sales_area', 'tbl_customer.SalesArea', '=', 'tbl_sales_area.SalesAreaId')
            //->where('tbl_visits.BeatPlanId', $BeatPlanId)
            ->where('tbl_beat_plan_customers.Status', '=', '1')
            ->where('tbl_beat_plan_customers.IsDeleted', '=', '0')
            ->get();

            // if ($VisitCustomerList->count() > 0) {
            //     foreach ($VisitCustomerList as $key => $CustomerList) {

            //         $CustomerId = $CustomerList->CustomerId;

            //         $VisitList = Visits::select('*')
            //             ->where('BeatPlanId', $BeatPlanId)
            //             ->where('CustomerId', $CustomerId)
            //             ->where('Status', '=', '1')
            //             ->where('IsDeleted', '=', '0')
            //             ->get();

            //             //print_r(json_encode($VisitList));
            //     }
            // }
            // exit;

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

        // Retailer Name
        $ViewBeatPlan->RetailerName = '';
        if ($ViewBeatPlan->RetailerNameId) {
            $CustomerIdArray = explode(",", $ViewBeatPlan->RetailerNameId);

            if (count($CustomerIdArray) > 0) {
                $customerNames = '';
                for ($k = 0; $k < count($CustomerIdArray); $k++) {
                    $CustoomerId = $CustomerIdArray[$k];
                    if ($CustoomerId) {
                        $Retailer = Customer::select('CustomerName')
                            ->where('CustomerId', $CustoomerId)
                            ->orderBy('tbl_customer.id', 'desc')
                            ->get()->first();
                        $customerNames .= ucfirst($Retailer->CustomerName) . ", ";
                    }
                }

                if ($customerNames) {
                    $customerNames = rtrim($customerNames, ', ');
                }


                $ViewBeatPlan->RetailerName = $customerNames;
            }
        }

        // Customer Name
        $ViewBeatPlan->Customer = '';
        if ($ViewBeatPlan->CustomerId) {
            $CustomerIdArray = explode(",", $ViewBeatPlan->CustomerId);


            if (count($CustomerIdArray) > 0) {
                $customerName = '';
                for ($k = 0; $k < count($CustomerIdArray); $k++) {
                    $CustomerId = $CustomerIdArray[$k];
                    if ($CustomerId) {
                        $Customer = Customer::select('CustomerName')
                            ->where('CustomerId', $CustomerId)
                            ->orderBy('tbl_customer.id', 'desc')
                            ->get()->first();
                        $customerName .= ucfirst($Customer->CustomerName) . ", ";
                    }
                }

                if ($customerName) {
                    $customerName = rtrim($customerName, ', ');
                }


                $ViewBeatPlan->Customer = $customerName;
            }
        }


        return view('admin.view-beat-plan', ['BeatPlan' => $ViewBeatPlan,  'VisitCustomerList' => $VisitCustomerList]);
    }

    ### Store New Records
    public function storeBeatPlan(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $BeatPlan = new BeatPlan;
        $g_BeatPlanId = $this->userLib->useridGenrator();
        $BeatPlan->BeatPlanId = $g_BeatPlanId;
        $BeatPlanType = $request->input('BeatPlanType');
        $BeatPlan->BeatPlanType = $BeatPlanType;
        $BeatName = $request->input('BeatPlanName');
        $BeatPlan->BeatPlanName = $BeatName;

        $BeatPlanDate = '';
        if ($request->input('BeatPlanDate')) {
            $BeatPlanDate = str_replace('/', '-', $request->input('BeatPlanDate'));
        }
        $BeatDate = date('d-m-Y', strtotime($BeatPlanDate));
        $BeatPlan->BeatPlanDate = $BeatDate;

        $BeatPlan->BeatPlanAreaType = $request->input('BeatPlanAreaType');
        $TerritoryId = $request->input('SalesTerritoryName');
        $BeatPlan->SalesTerritoryId = $TerritoryId;
        $AreaId = $request->input('SalesAreaName');
        $BeatPlan->SalesAreaId = $AreaId;
        $DistributorId = $request->input('DistributorName');
        $BeatPlan->DistributorNameId = $DistributorId;
        //$BeatPlan->CustomerId = $request->input('CustomerId');

        if ($request->has('CustomerId')) {
            $getCustomerId = $request->input('CustomerId');
            $CustomerId = implode(',', $getCustomerId);
            $BeatPlan->RetailerNameId = $CustomerId;
        }
        //$BeatPlan->RetailerName = $request->input('RetailerName');

        if ($request->has('RetailerName')) {
            $getRetailerName = $request->input('RetailerName');
            $RetailerName = implode(',', $getRetailerName);
            $BeatPlan->RetailerNameId = $RetailerName;
        }


        $BeatPlan->Created_by = session('UserId');
        $BeatPlan->Created_at = date('Y-m-d H:i:s', time());
        $Assign = $request->input('AssignTo');
        $BeatPlan->AssignTo = $Assign;
        $BeatPlan->Updated_by = session('UserId');
        $BeatPlan->Updated_at = date('Y-m-d H:i:s', time());
        $BeatPlan->IsDeleted = '0';
        $BeatPlan->Status = '1';

        $saveBeatPlan = $BeatPlan->save();

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

        // Save customer names ID to Beat Plan Customers Table

        if ($saveBeatPlan) {

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

            $BeatPlanType = $request->input('BeatPlanType');
            if ($BeatPlanType == 'DistributorWise') {
                $CustomerIdsArray = $request->input('RetailerName');
                $CustomerType = 'Retailer';
            } else {
                $CustomerType = 'Customer';
                $CustomerIdsArray = $request->input('CustomerId');
            }

            if ($CustomerIdsArray) {

                if (count($CustomerIdsArray) > 0) {
                    $bpcinsert = 0;
                    for ($k = 0; $k < count($CustomerIdsArray); $k++) {
                        $CustomerId = $CustomerIdsArray[$k];
                        if ($CustomerId) {
                            $bpc = array();
                            $bpc['BeatPlanCustomerId'] = $this->userLib->useridGenrator();
                            $bpc['BeatPlanId'] = $latestBeatPlan->BeatPlanId;
                            $BeatPlanDate = '';
                            if ($request->input('BeatPlanDate')) {
                                $BeatPlanDate = str_replace('/', '-', $request->input('BeatPlanDate'));
                            }
                            $bpc['CustomerScheduleTime'] = date('h:i A'); // date('Y-m-d', strtotime($BeatPlanDate));
                            $bpc['CustomerId'] = $CustomerId;
                            $bpc['CustomerType'] = $CustomerType;
                            $bpc['AssignTo'] = $request->input('AssignTo');
                            $bpc['Created_by'] = session('UserId');
                            $bpc['Created_at'] = $CreatedDate;
                            $bpc['Updated_by'] = session('UserId');
                            $bpc['Updated_at'] = $CreatedDate;
                            $bpc['IsDeleted'] = 0;
                            $bpc['Status'] = 1;
                            $BeatPlanCustomer = BeatPlanCustomer::insert($bpc);
                            if ($bpc) {
                                $bpcinsert = $bpcinsert + 1;
                            }
                        }
                    }
                    if (count($CustomerIdsArray) == $bpcinsert) {
                        //echo $BeatPlanCustomer;
                    }
                }
            }
        }

        $BeatPlanId = $g_BeatPlanId;
        $BeatPlanName = $BeatName;
        $BeatPlanDate = $BeatDate;
        if($BeatPlanType == 'DistributorWise'){
            $DistributorNameId = $DistributorId;
            $RetailerNameId = $RetailerName;
        }
        else{
            $DistributorNameId = '';
            $RetailerNameId = '';
        }
        if($BeatPlanType == 'SalesAreaWise'){
            $SalesTerritoryId = $TerritoryId;
            $SalesAreaId = $AreaId;
            $CustomerNameId = $CustomerId;
        }
        else{
            $SalesTerritoryId = '';
            $SalesAreaId = '';
            $CustomerNameId = '';
        }
        $AssignTo = $Assign;
        $IsDeleted = '0';
        $Status = '1';
        $InsertBeatPlan = DB::connection('pgsql')->insert("insert into salesforce.Beat_Plan__c (Beat_Plan_Id__c,Name,BeatPlan_Date__c,BeatPlan_Type__c,Distributor_Name_Id__c,Retailer_Name_Id__c,Sales_Territory_Id__c,Sales_Area_Id__c,Customer_Id__c,Assign_To__c,Is_Deleted__c,Status__c) values ('$BeatPlanId','$BeatPlanName','$BeatPlanDate','$BeatPlanType','$DistributorNameId','$RetailerNameId','$SalesTerritoryId','$SalesAreaId','$CustomerNameId','$AssignTo','$IsDeleted','$Status')");

        if ($InsertBeatPlan) {

            $latestBeatPlanDetail = DB::connection('pgsql')->select("select * from salesforce.Beat_Plan__c where Beat_Plan_Id__c = '$BeatPlanId'");

                    $BeatPlanId_sf = $BeatPlanId;
                    // if($latestBeatPlanDetail > 0)
                    // {
                    //     foreach ($latestBeatPlanDetail as $BValue) {
                    //         $BeatPlanId_sf = $BValue->Beat_Plan_Id__c;
                    //     }
                    // }

            $BeatPlanType_sf = $request->input('BeatPlanType');
            if ($BeatPlanType_sf == 'DistributorWise') {
                $CustomerIdsArray_sf = $request->input('RetailerName');
                $CustomerType_sf = 'Retailer';
            } else {
                $CustomerType_sf = 'Customer';
                $CustomerIdsArray_sf = $request->input('CustomerId');
            }

            if ($CustomerIdsArray_sf) {

                if (count($CustomerIdsArray_sf) > 0) {
                    $bpcinsert_sf = 0;
                    for ($k = 0; $k < count($CustomerIdsArray_sf); $k++) {
                        $CustomerId_sf = $CustomerIdsArray_sf[$k];
                        if ($CustomerId_sf) {
                            $BeatPlanCustomerId = $this->userLib->useridGenrator();
                            $BeatPlanId = $BeatPlanId_sf;
                            // $BeatPlanDate = '';
                            // if ($request->input('BeatPlanDate')) {
                            //     $BeatPlanDate = str_replace('/', '-', $request->input('BeatPlanDate'));
                            // }
                            $CustomerScheduleTime = date('h:i A');
                            $CustomerId = $CustomerId_sf;
                            $CustomerType = $CustomerType_sf;
                            $AssignTo = $request->input('AssignTo');
                            $IsDeleted = 0;
                            $Status = 1;
                            $InsertBeatPlanCustomer = DB::connection('pgsql')->insert("insert into salesforce.Beat_Plan_Customers__c (BeatPlan_Customer_Id__c,External_Beat_Plan_Id__c,Customer_Schedule_Time__c,Customer_Id__c,Customer_Type__c,Assign_To__c,IsDeleted__c,Status__c) values ('$BeatPlanCustomerId','$BeatPlanId','$CustomerScheduleTime','$CustomerId','$CustomerType','$AssignTo','$IsDeleted','$Status')");
                            if ($InsertBeatPlanCustomer) {
                                $bpcinsert_sf = $bpcinsert_sf + 1;
                            }
                        }
                    }
                    if (count($CustomerIdsArray_sf) == $bpcinsert_sf) {
                        //echo $BeatPlanCustomer;
                    }
                }
            }
        }
        return response()->json('status_success');
    }

    #### Change Status
    public function statusUpdateBeatPlan($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $data = BeatPlan::select('tbl_beat_plan.*')
            ->where('id', '=', $id)
            ->get()->first();
        if ($data->Status == '1') {
            $Status = '0';
        } else {
            $Status = '1';
        }

        $values = array('Status' => $Status);
        BeatPlan::where('id', $id)->update($values);
        return redirect()->back();
    }


    #### Delete Temporary
    public function deleteBeatPlan($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['IsDeleted'] = '1';
        $value['Status'] = '0';
        BeatPlan::where('BeatPlanId', $id)->update($value);

        DB::connection('pgsql')->update("update salesforce.Beat_Plan__c set IsDeleted='1',Status__c='0' where Beat_Plan_Id__c = '$id'");
    }

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

        $id = $request->input('SelectedBeatPlan');
        if ($id) {
            foreach ($id as $beatPlan) {
                $checkData = BeatPlan::where('BeatPlanId', $beatPlan);
                if ($checkData->IsDeleted == '0') {
                    $CreatedDate = date(
                        'Y-m-d H:i:s',
                        time()
                    );
                    $data = array();
                    $data['Updated_at'] = $CreatedDate;
                    $data['IsDeleted'] = '1';
                    $data['Status'] = '0';

                    $selected_beatPlan = BeatPlan::where('BeatPlanId', $beatPlan)->update($data);
                    if ($selected_beatPlan) {
                        echo '1';
                    } else {
                        echo '0';
                    }
                } else {
                    $selected_beatPlan = BeatPlan::where('BeatPlanId', $beatPlan);
                    $selected_beatPlan->delete();
                }
            }
        }

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

    #### Restore
    public function restoreBeatPlan($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $value = array('IsDeleted' => '0');
        BeatPlan::where('BeatPlanId', $id)->update($value);

        DB::connection('pgsql')->update("update salesforce.Beat_Plan__c set IsDeleted='0',Status__c='1' where Beat_Plan_Id__c = '$id'");
    }

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

    #### Delete Beat Plan Customer
    public function deletePlanCustomer($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['IsDeleted'] = '1';
        $delete_plan_customer = BeatPlanCustomer::where('BeatPlanCustomerId', $id)->update($value);
        if ($delete_plan_customer) {
            $GetCustomerId = BeatPlanCustomer::where('BeatPlanCustomerId', $id)
            ->get()->first();

            $CustomerId = $GetCustomerId->CustomerId;
            $BeatPlanId = $GetCustomerId->BeatPlanId;

            $customerdata = array();
            $customerdata['BeatPlan'] = '';
            $customerdata['Updated_at'] = $CreatedDate;
            Customer::where('CustomerId', $CustomerId)->update($customerdata);
        }
    }

    ### Edit Records
    public function editBeatPlan($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $BeatPlan = BeatPlan::select('tbl_beat_plan.*')
            ->where('BeatPlanId', '=', $id)
            ->get()->first();
        return response()->json([
            'status' => 200,
            'BeatPlan' => $BeatPlan,
        ]);
    }

    ### Update Records
    public function updateBeatPlan(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $plan_id =  $request->input('id');
        $UpdatedDate = date('Y-m-d H:i:s', time());
        $data = array();
        $data['Updated_at'] = $UpdatedDate;
        if ($request->input('BeatPlanType')) {
            $data['BeatPlanType'] = $request->input('BeatPlanType');
        }
        $data['BeatPlanName'] = $request->input('BeatPlanName');
        $data['BeatPlanDate'] = '';
        if ($request->input('BeatPlanDate')) {
            $BeatPlanDate = str_replace('/', '-', $request->input('BeatPlanDate'));
        }
        $data['BeatPlanDate'] = date('d-m-Y', strtotime($BeatPlanDate));
        $data['BeatPlanAreaType'] = $request->input('BeatPlanAreaType');
        $data['SalesTerritoryId'] = $request->input('SalesTerritoryName');
        $data['SalesAreaId'] = $request->input('SalesAreaName');
        $data['DistributorNameId'] = $request->input('DistributorName');
        if ($request->has('CustomerId')) {
            $getCustomerId = $request->input('CustomerId');
            $data['CustomerId'] = implode(',', $getCustomerId);
        }
        if ($request->has('RetailerName')) {
            $getRetailerName = $request->input('RetailerName');
            $data['RetailerNameId'] = implode(',', $getRetailerName);
        }
        $data['Updated_by'] = session('UserId');
        $data['AssignTo'] = $request->input('AssignTo');

        $beatPlan_update = BeatPlan::where('BeatPlanId', $plan_id)->update($data);

        $beat_plan_id =  $request->input('id');

        if ($request->input('BeatPlanType')) {
            $BeatPlanType = $request->input('BeatPlanType');
        }

        $BeatPlanName = $request->input('BeatPlanName');
        $BeatPlanDate = '';
        if ($request->input('BeatPlanDate')) {
            $BeatPlanDate = str_replace('/', '-', $request->input('BeatPlanDate'));
        }
        $BeatPlanDate = date('d-m-Y', strtotime($BeatPlanDate));
        // $BeatPlanAreaType = $request->input('BeatPlanAreaType');
        // $SalesTerritoryId = $request->input('SalesTerritoryName');
        // $SalesAreaId = $request->input('SalesAreaName');
        // $DistributorNameId = $request->input('DistributorName');
        // if ($request->has('CustomerId')) {
        //     $getCustomerId = $request->input('CustomerId');
        //     $CustomerNameId = implode(',', $getCustomerId);
        // }
        // if ($request->has('RetailerName')) {
        //     $getRetailerName = $request->input('RetailerName');
        //     $RetailerNameId = implode(',', $getRetailerName);
        // }
        $AssignTo = $request->input('AssignTo');

        DB::connection('pgsql')->update("update salesforce.Beat_Plan__c set Name='$BeatPlanName',BeatPlan_Date__c='$BeatPlanDate',BeatPlan_Type__c='$BeatPlanType',Assign_To__c='$AssignTo' where Beat_Plan_Id__c='$beat_plan_id'");

        if ($beatPlan_update) {
            echo '1';
        } else {
            echo '0';
        }
        return redirect()->back()->with('status_success');
    }

    ### Beat Plan Area Customers
    public function planAreaCustomer($id)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $BeatPlans = BeatPlanCustomer::select('tbl_beat_plan_customers.*')
            ->where('BeatPlanId', '=', $id)
            ->get();

        $str = "<table>";
        $str .= "<thead>";
        $str .= "<tr>";
        $str .= "<th>SN</th>";
        $str .= "<th>Customer Name</th>";
        $str .= "<th>Set Time</th>";
        $str .= "</tr>";
        $str .= "</thead>";
        $str .= "<tbody>";
        if ($BeatPlans->count() > 0) {
            $cnt = 1;
            foreach ($BeatPlans as $key => $BeatPlan) {
                $CustomerId = $BeatPlan['CustomerId'];
                $BeatPlanId = $BeatPlan['BeatPlanId'];
                $CustomerName = '';
                $customer = Customer::where('CustomerId', $CustomerId)->get();
                if ($customer->count() > 0) {
                    $customer = $customer->first();
                    $CustomerName = $customer->CustomerName;
                }
                $BeatPlanName = $BeatPlan['BeatPlanName'];
                $BeatPlanDate = $BeatPlan['CustomerScheduleTime'];
                $CheckIn = date('H:i', strtotime($BeatPlanDate));
                $BeatPlanCustomerId = $BeatPlan['BeatPlanCustomerId'];

                $str .= "<tr>";
                $str .= "<td>" . $cnt .  "</td>";
                $str .= "<td>" . $CustomerName . "</td>";
                $str .= "<td><input type='time' class='form-control form-control-sm beatplancustmerdatetime' name='beatplancustmerdatetime' id='beatplancustmerdatetime_'" . $CustomerId . "  data-beatplancustomerid='" . $BeatPlanCustomerId . "' data-customerid='" . $CustomerId . "' data-beatplanid='" . $BeatPlanId . "' value='" . $CheckIn . "' ></td>";
                $str .= "</tr>";
                $cnt = $cnt + 1;
            }
        }
        $str .= "</tbody>";
        $str .= "</table>";
        echo $str;
    }

    public function planCustomerTime(Request $request)
    {
        if (empty(session('UserId'))) {
            return redirect('/admin/logout');
        }
        $input = $request->all();
        $data = $input;
        $CheckIn = $request->input('CustomerTime');
        $CheckInTime = date('h:i A', strtotime($CheckIn));

        $data['CustomerScheduleTime'] = $CheckInTime;
        $BeatPlanId = $data['BeatPlanId'];
        $CustomerId = $data['CustomerId'];
        $BeatPlanCustomerId = $data['BeatPlanCustomerId'];

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

        unset($data['_token']);
        unset($data['CustomerTime']);
        $update_time = BeatPlanCustomer::where('BeatPlanId', $BeatPlanId)
            ->where('CustomerId', $CustomerId)
            ->where('BeatPlanCustomerId', $BeatPlanCustomerId)
            ->update($data);
        return response()->json('status_success');
    }
}