File: D:/HostingSpaces/SBogers10/hours.komma.pro/app/Komma/Subprojects/SubprojectController.php
<?php
namespace App\Komma\Subprojects;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use App\Komma\Messages\MessageController;
use App\Komma\Projects\Project;
use App\Komma\Projects\ProjectService;
use App\Komma\Hours\HourService;
use App\Komma\Settings\Subprojecttemplates\SubprojectTemplate;
use App\Komma\Settings\TaskTemplates\TaskTemplate;
use App\Komma\Tasks\Task;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class SubprojectController extends Controller
{
protected $subprojectRepository;
protected $subprojectService;
protected $messageController;
protected $projectService;
protected $hourService;
public function __construct(MessageController $messageController, SubprojectRepository $subprojectRepository, SubprojectService $subprojectService, ProjectService $projectService, HourService $hourService)
{
$this->middleware('auth');
$this->messageController = $messageController;
$this->subprojectRepository = $subprojectRepository;
$this->subprojectService = $subprojectService;
$this->projectService = $projectService;
$this->hourService = $hourService;
}
/**
* @param Project $project
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function show(Project $project, $id)
{
session()->forget(['date', 'view', 'user']);
// Load project with all relations
$subproject = $this->subprojectRepository->fullSubproject($id);
//get all subprojects of project for move
$projectSubprojects = $this->subprojectRepository->getProjectSubprojects($subproject->project_id);
//get first date
$firstDate = $this->projectService->getFirstDateSubproject($subproject);
$firstDate = !empty($firstDate[0]) ? $firstDate[0] : $subproject->created_at->format('d-m-Y');
//get begin and end date
$begin = \Request::filled('begin') ? \Request::get('begin') : $firstDate;
$end = \Request::filled('end') ? \Request::get('end') : Carbon::today()->endOfDay()->format('d-m-Y');
$user = \Request::filled('user') ? \Request::get('user') : "";
//filter hours by begin and end date
$data = $this->projectService->filterSubprojectHours($subproject, $begin, $end, $user);
$subproject = $data['subproject'];
$workers = $data['workers'];
$chosenSubproject = \Request::filled('subproject') ? Subproject::with(['Tasks', 'Tasks.TaskTemplate'])->find(\Request::get('subproject')) : $subproject;
// get the hours divided by their status
$allSubprojectHours = (object)$this->subprojectService->getSumSubprojectHours($subproject);
// paginate by a different amount than the default 10
if(\Request::has('pa') && \Request::get('pa') == 'all') {
$hours = $this->hourService->getHours($subproject);
$hoursCount = count($hours);
$hours = $this->subprojectService->paginate($hours, $hoursCount);
} else {
$hours = $this->subprojectService->paginate($this->hourService->getHours($subproject), \Request::has('pa') ? \Request::get('pa') : 10, \Request::get('page'));
}
return view('subprojects.show', compact('project', 'subproject', 'allSubprojectHours', 'workers', 'begin', 'end', 'user', 'hours', 'chosenSubproject', 'projectSubprojects'));
}
/**
* @param $project
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function create($project)
{
$project = Project::find($project);
$projects = Project::orderBy('name', 'asc')->get();
$subprojectTemplates = SubprojectTemplate::orderBy('name', 'asc')->where('name','!=', 'Import')->get();
$taskTemplates = TaskTemplate::orderBy('name', 'asc')->get();
$ref = \Request::filled('ref') ? \Request::get('ref') : 'projects';
return view('subprojects.create', compact('subprojectTemplates', 'taskTemplates', 'project', 'projects', 'ref'));
}
/**
* @param Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function store(Request $request)
{
$this->validate($request, [
'subproject' => 'required',
'name' => 'required',
'budget' => 'Numeric',
'billable' => 'required',
'hourly_rate' => [
'required',
function ($attribute, $value, $fail) {
if ($value === '0' && request()->billable === "1") {
$fail('Bij een facaturabel deelproject moet de uurprijs hoger dan 0 zijn!');
}
if ($value !== '0' && request()->billable === "0") {
$fail('Bij een niet-facaturabel deelproject moet de uurprijs 0 zijn!');
}
},
],
]);
//check if exist
if (!empty($this->subprojectService->checkIfExist($request))) {
//return redirect
if($request->ajax()) {
return $request->subproject;
} else {
return redirect('/' . $this->subprojectService->checkIfExist($request));
}
}
//store subproject and get stored subproject
$subproject = $this->subprojectService->storeSubproject($request);
// Find redirect route
$redirectRoute = 'projecten';
//if ref exist
if ($request->filled('ref')) {
//get ref
$ref = $request->get('ref');
//replace - with / if - exist
$redirectRoute = str_replace('-', '/', $ref . '?project=' . $subproject->project_id . '&subproject=' . $subproject->id);
}
//return redirect
if($request->ajax()) {
return $subproject->id;
} else {
return redirect('/' . $redirectRoute);
}
}
/**
* @param $subproject
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit($subproject)
{
//find subproject
$subproject = Subproject::find($subproject);
//return
return view('subprojects.edit', compact('subproject'));
}
/**
* @param Request $request
* @param $subproject
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function update(Request $request, $subproject)
{
$subproject = Subproject::find($subproject);
$allSubprojectHours = (object)$this->subprojectService->getSumSubprojectHours($subproject);
$writtenHours = (array_sum($allSubprojectHours->totalBillWithBudget)+array_sum($allSubprojectHours->totalNotBillWithBudget)+array_sum($allSubprojectHours->totalExceedsSubProject));
//validate
$this->validate($request, [
'name' => 'required',
'budget' => 'Numeric|required',
'hourly_rate' => [
'required',
function ($attribute, $value, $fail) {
if ($value === '0' && request()->billable === "1") {
$fail('Bij een facaturabel deelproject moet de uurprijs hoger dan 0 zijn!');
}
if ($value !== '0' && request()->billable === "0") {
$fail('Bij een niet-facaturabel deelproject moet de uurprijs 0 zijn!');
}
},
],
]);
//transaction
\DB::transaction(function () use ($request, $subproject) {
//find and save subproject
$subproject->name = $request->name;
$subproject->budget = $request->budget;
$subproject->hourly_rate = $request->hourly_rate;
$subproject->billable = $request->billable;
$subproject->locked = $request->locked;
$subproject->save();
// check/correct the billable status for all the hours in the subproject
$this->hourService->updateBillable($this->hourService->getHours($subproject), $subproject->billable);
//update out of budget hours
$this->hourService->updateOutBudget($this->hourService->getHours($subproject)->sortBy("date"), $subproject);
// show a message to the user
$subject = Subproject::find($subproject->id);
$value = "Deelproject";
$this->messageController->changed($value, $subject);
});
//return redirect
return redirect('/subprojecten/' . $subproject->id);
}
/**
* @param Subproject $subproject
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
public function destroy(Subproject $subproject)
{
$subproject->load('Tasks', 'Expenses');
// Check that there aren't any task with hours bind to the subproject
foreach ($subproject->Tasks as $projectTask) {
if(!isset($projectTask->Hours) || $projectTask->Hours->isEmpty()) continue;
$this->messageController->failed("Deelproject", "uren");
return back();
}
// Check that there aren't any task bind to the subproject
if(count($subproject->Expenses) > 0) {
$this->messageController->failed("Deelproject", "onkosten");
return back();
}
// All safety check are valid, delete the project
$subproject->delete();
$this->messageController->destroyed("Deelproject", $subproject);
return back();
}
/**
* @param $subproject
* @return \Illuminate\Http\RedirectResponse
*/
public function restore($subproject)
{
\DB::transaction(function () use ($subproject) {
Subproject::withTrashed()->find($subproject)->restore();
//message + activity
$subject = Subproject::find($subproject);
$value = "Deelproject";
$this->messageController->recovered($value, $subject);
});
//redirect back
return back();
}
/**
* API call for the content for the select box in the hour write form
*
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
*/
public function getTasksForSubProject(Request $request){
$foundSubproject = $this->subprojectService->getSubProjectWithTasksByID($request->id);
if(!empty($foundSubproject)) {
return response()->json(['subproject' => $foundSubproject]);
} else {
return response('No Tasks found.', 404);
}
}
/**
* API call to fill the subproject progressbar in the hour write form
*
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
* @throws \Throwable
*/
public function getProgressBarForSubProject(Request $request){
return response($this->subprojectService->getProjectSubProgressBarByID($request->id)->render());
}
}