HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/MdnDirecteur/hours.komma.cloud/app/Komma/Companies/CompanyController.php
<?php

namespace App\Komma\Companies;


use App\Http\Controllers\Controller;
use App\Komma\Users\User;
use Carbon\Carbon;
use App\Komma\Cache\CacheService;
use App\Komma\Contacts\Contact;
use App\Komma\Messages\MessageController;
use App\Komma\Notifications\NotificationService;
use App\Komma\Projects\ProjectService;
use App\Komma\Settings\Acquisitions\Acquisition;
use App\Komma\Settings\Countries\Country;
use App\Komma\Settings\Kinds\Kind;
use App\Komma\Subprojects\SubprojectService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class CompanyController extends Controller
{
    private $companyService;
    private $messageController;
    private $companyRepository;
    private $cacheService;
    private $projectService;
    private $notificationService;

    public function __construct(CompanyService $companyService, MessageController $messageController, CompanyRepository $companyRepository, CacheService $cacheService, ProjectService $projectService, NotificationService $notificationService)
    {
        $this->middleware('auth');
        $this->companyService = $companyService;
        $this->messageController = $messageController;
        $this->companyRepository = $companyRepository;
        $this->cacheService = $cacheService;
        $this->projectService = $projectService;
        $this->notificationService = $notificationService;
    }


    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        //create variables
        $action = !empty($request->action) ? $request->action : "active";
        $search = !empty($request->search) ? $request->search : "";

        //get companies by action and search
        if (!$companies = $this->companyService->action($action, $search));

        //get count values
        $badgeCounter = (object)$this->companyService->getCountCompanies();

        //return
        return view('companies.index', compact('companies', 'action', 'badgeCounter'));
    }


    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id, Request $request, SubprojectService $subprojectService)
    {
        //get search value
        $search = !empty($request->search) ? $request->search : "";

        // Load companies with all relations
        $company = $this->companyRepository->fullCompany($id, $search);

        //get budgets of projects
        $budgets = collect($this->projectService->getBudgetsProjects($company->Projects));

        //get company information
        $companyBudgets = (object)$this->companyService->getCompanyInformation($company, $budgets);

        //create default begin date
        $firstDate = [$company->created_at->format('d-m-Y')];

        //loop though projects of company
        foreach ($company->Projects as $key => $project)
        {

            // If user user is not admin
            if(!Auth::user()->isAdmin()) {

                // Check if user is allowed for project
                if($project->hasAllowedUsersRestriction() && !in_array(Auth::user()->id, $project->allowedUsers())) {
                    $company->Projects->forget($key);
                    continue;
                }
            }

            //get first date
            $getDate = $this->projectService->getFirstDateProject($project);
            $firstDate[] = !empty($getDate[0]) ? $getDate[0] : $company->created_at->format('d-m-Y');
        }
        usort($firstDate, function($a, $b){
            return strtotime($a) - strtotime($b);
        });

        //get filter params
        $begin = !empty($request->begin) ? $request->begin : $firstDate[0];
        $end = !empty($request->end) ? $request->end : Carbon::today()->format('d-m-Y');
        $user = !empty($request->user) ? $request->user : "";

        //filter projects
        foreach ($company->Projects as $index => $project) {
            //filter projects
            $company->Projects[$index] = $this->projectService->filterProjectHours($project, $begin, $end, $user, false)['project'];
        }


        //get users for select box
        $users = User::select('name')->get();

        //paginate projects
        $projects = $subprojectService->paginate($company->Projects, 8, \Request::get('page'));

        //return
        return view('companies.show', compact('company', 'projects', 'budgets', 'begin', 'end', 'user', 'users', 'companyBudgets'));
    }


    /**
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        //get company
        $company = Company::find($id);
        //get all countries
        $countries = Country::orderBy('name', 'asc')->get();
        //get all Acquisitions
        $acquisitions = Acquisition::orderBy('name', 'asc')->get();
        //get all kinds
        $kinds = Kind::orderBy('name', 'asc')->get();

        //return
        return view('companies.edit', compact('countries', 'company', 'acquisitions', 'kinds'));
    }


    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        //get cached values
        $cache = Cache::has('createCompany') ? (object)Cache::pull('createCompany') : "";
        $country = $this->cacheService->fillSelectBox("country", $cache);
        $acquisition = $this->cacheService->fillSelectBox("acquisition", $cache);
        $kind = $this->cacheService->fillSelectBox("kind", $cache);

        //get all countries
        $countries = Country::orderBy('name', 'asc')->get();
        //get all Acquisitions
        $acquisitions = Acquisition::orderBy('name', 'asc')->get();
        //get all kinds
        $kinds = Kind::orderBy('name', 'asc')->get();
        //get ref
        $ref = \Request::filled('ref') ? \Request::get('ref') : '';

        //return
        return view('companies.create', compact('ref', 'countries', 'country', 'cache', 'acquisitions', 'acquisition', 'kinds', 'kind'));
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:companies',
            'country' => 'required',
            'hourly_rate' => 'required',
        ]);

        $company = \DB::transaction(function () use ($request) {
            //store new company
            $company = new Company();
            $company->name = $request->name;
            $company->country_id = $request->country;
            $company->hourly_rate = $request->hourly_rate;
            $company->acquisition_id = empty($request->acquisition) ? 0 : $request->acquisition;
            $company->save();


            //message + activity
            $this->messageController->create("Nieuwe klant", Company::find($company->id));

            //return created company
            return $company;
        });

        // Find redirect route
        $redirectRoute = 'klanten';
        if ($request->filled('ref')) {
            $ref = $request->get('ref');
            $redirectRoute = str_replace('-', '/', $ref) . '?company=' . $company->id;
        }

        if($request->ajax()) {
            return $company->id;
        } else {
            //redirect
            return redirect('/' . $redirectRoute);
        }
    }


    /**
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required',
            'country' => 'required',
            'hourly_rate' => 'required',
        ]);

        \DB::transaction(function () use ($request, $id) {
            //update company
            $company = Company::find($id);
            $company->name = $request->name;
            $company->country_id = $request->country;
            $company->acquisition_id = $request->acquisition;
            $company->hourly_rate = $request->hourly_rate;
            $company->save();


            //message + activity
            $value = "Klant";
            $this->messageController->changed($value, $company);
        });

        // Find redirect route
        $redirectRoute = 'klanten/' . $id;

        //redirect
        return redirect('/' . $redirectRoute);
    }


    /**
     * @param $company
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy($company)
    {
        //find company
        $getCompany = Company::find($company);
        //if company has projects
        if (count($getCompany->Projects)) {
            //message
            $value = "Klant";
            $underlying = "projecten";
            $this->messageController->failed($value, $underlying);
        } else {
            \DB::transaction(function () use ($getCompany, $company) {
                //delete
                $getCompany->delete();

                $deleted = (object)["deleted" => true];

                $this->notificationService->companyNotification($deleted, $getCompany);

                //message + activity
                $value = "Klant";
                $this->messageController->destroyed($value, $getCompany);
            });
        }

        //redirect
        return redirect('/klanten');
    }


    /**
     * @param $company
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function restore($company)
    {
        //find company
        Company::withTrashed()->find($company)->restore();

        //message
        $subject = Company::find($company);
        $value = "Klant";
        $this->messageController->recovered($value, $subject);


        //redirect
        return redirect('/klanten');
    }

}