-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathProjectController.php
156 lines (138 loc) · 4.59 KB
/
ProjectController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProjectRequest;
use App\Models\Client;
use App\Models\Project;
use Illuminate\Http\Request;
use Modules\HR\Entities\Employee;
class ProjectController extends Controller
{
public function __construct()
{
$this->authorizeResource(Project::class);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\View\View
*/
public function index()
{
$this->authorize('list', Project::class);
if (request()->has('client_id')) {
$client = Client::find(request()->input('client_id'));
$projects = $client->projects()->paginate();
} else {
$projects = Project::getList();
}
return view('project.index')->with([
'projects' => $projects,
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\View\View
*/
public function create()
{
return view('project.create')->with([
'clients' => Client::active()->get(),
]);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\ProjectRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(ProjectRequest $request)
{
$validated = $request->validated();
$project = Project::create([
'name' => $validated['name'],
'client_id' => $validated['client_id'],
'client_project_id' => $validated['client_project_id'],
'status' => $validated['status'],
'invoice_email' => $validated['invoice_email'],
]);
return redirect(route('projects.edit', $project->id))->with('status', 'Project created successfully!');
}
/**
* Display the specified resource.
*
* @param \App\Models\Project $project
*/
public function show(Project $project)
{
$project->load('stages', 'stages.billings', 'stages.billings.invoice', 'employees');
return view('project.show')->with([
'project' => $project,
'clients' => Client::select('id', 'name')->get(),
'employees' => Employee::select('id', 'name')->get(),
]);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Project $project
*
* @return \Illuminate\View\View
*/
public function edit(Project $project)
{
$project->load('stages', 'stages.billings', 'stages.billings.invoice');
return view('project.edit')->with([
'project' => $project,
'clients' => Client::select('id', 'name')->get(),
]);
}
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\ProjectRequest $request
* @param \App\Models\Project $project
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(ProjectRequest $request, Project $project)
{
$validated = $request->validated();
$project->update([
'name' => $validated['name'],
'client_id' => $validated['client_id'],
'client_project_id' => $validated['client_project_id'],
'status' => $validated['status'],
'invoice_email' => $validated['invoice_email'],
'gst_applicable' => isset($validated['gst_applicable']) ? true : false,
]);
return redirect(route('projects.edit', $project->id))->with('status', 'Project updated successfully!');
}
/**
* Add Employees to this Project.
*
* @param \App\Models\Project $project
* @param \App\Http\Requests\ProjectRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function addEmployee(Project $project, Request $request)
{
$project->employees()->attach($request->get('employeeId'), ['contribution_type' => $request->get('contribution')]);
return redirect(route('projects.show', $project))->with('status', 'Employee added to the project successfully!');
}
/**
* Remove Employees from this Project.
*
* @param \App\Models\Project $project
* @param \App\Http\Requests\ProjectRequest $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function removeEmployee(Project $project, Request $request)
{
$project->employees()->detach($request->get('employeeId'));
return redirect(route('projects.show', $project))->with('status', 'Employee removed from the project successfully!');
}
}