Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit c6b9a2b

Browse files
author
Mario Basic
committed
Added service categories and a yearly report.
1 parent 7ab0db4 commit c6b9a2b

31 files changed

+53238
-38
lines changed

app/Category.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\Service;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Category extends Model
9+
{
10+
protected $fillable = ['name'];
11+
12+
/**
13+
* Get services which belong to this category.
14+
*
15+
* @return [type]
16+
*/
17+
public function services()
18+
{
19+
return $this->hasMany(Service::class);
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use Illuminate\Http\Request;
6+
7+
use App\Http\Requests;
8+
use App\Http\Controllers\Controller;
9+
use App\Category;
10+
11+
class CategoryController extends Controller
12+
{
13+
public function store(Request $request)
14+
{
15+
$this->validate($request, [
16+
'name' => 'required|string|max:255'
17+
]);
18+
19+
$category = Category::create([
20+
'name' => $request->get('name')
21+
]);
22+
23+
return $category->toArray();
24+
}
25+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Category;
7+
use App\Http\Requests;
8+
9+
class CategoryController extends Controller
10+
{
11+
/**
12+
* Display a listing of the resource.
13+
*
14+
* @return \Illuminate\Http\Response
15+
*/
16+
public function index()
17+
{
18+
$categories = Category::orderBy('name')
19+
->with(['services'])
20+
->get();
21+
22+
return view('categories.index')->with(compact('categories'));
23+
}
24+
25+
/**
26+
* Show the form for creating a new resource.
27+
*
28+
* @return \Illuminate\Http\Response
29+
*/
30+
public function create()
31+
{
32+
return view('categories.create');
33+
}
34+
35+
/**
36+
* Store a newly created resource in storage.
37+
*
38+
* @param \Illuminate\Http\Request $request
39+
* @return \Illuminate\Http\Response
40+
*/
41+
public function store(Request $request)
42+
{
43+
$this->validate($request, [
44+
'name' => 'required|string|max:255',
45+
]);
46+
47+
$category = Category::create([
48+
'name' => $request->get('name'),
49+
]);
50+
51+
flash()->success('Category created!');
52+
53+
return redirect()->route('categories.index');
54+
}
55+
56+
/**
57+
* Show the form for editing the specified resource.
58+
*
59+
* @param int $id
60+
* @return \Illuminate\Http\Response
61+
*/
62+
public function edit($id)
63+
{
64+
$category = Category::findOrFail($id);
65+
66+
return view('categories.edit')->with(compact('category'));
67+
}
68+
69+
/**
70+
* Update the specified resource in storage.
71+
*
72+
* @param \Illuminate\Http\Request $request
73+
* @param int $id
74+
* @return \Illuminate\Http\Response
75+
*/
76+
public function update(Request $request, $id)
77+
{
78+
$category = Category::findOrFail($id);
79+
80+
$this->validate($request, [
81+
'name' => 'required|string|max:255',
82+
]);
83+
84+
$category->update([
85+
'name' => $request->get('name'),
86+
]);
87+
88+
flash()->success('Category Updated!');
89+
90+
return redirect()->route('categories.edit', $category->id);
91+
}
92+
93+
/**
94+
* Remove the specified resource from storage.
95+
*
96+
* @param int $id
97+
* @return \Illuminate\Http\Response
98+
*/
99+
public function destroy($id)
100+
{
101+
$category = Category::findOrFail($id);
102+
$category->delete();
103+
104+
flash()->success('Category Deleted!');
105+
106+
return redirect()->route('categories.index');
107+
}
108+
}

app/Http/Controllers/HomeController.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use App\Occurrence;
66
use App\Http\Requests;
77
use Illuminate\Http\Request;
8+
use App\Service;
9+
use App\Client;
10+
use App\Category;
811

912
class HomeController extends Controller
1013
{
@@ -49,4 +52,33 @@ public function index()
4952

5053
return view('home')->with(compact('occurrences', 'upcomingOccurrences'));
5154
}
55+
56+
public function report()
57+
{
58+
$services = Service::orderBy('month')
59+
->orderBy('day')
60+
->where('active', 1)
61+
->with(['client', 'category'])
62+
->get();
63+
64+
$clients = Client::orderBy('name')
65+
->whereHas('services', function($query) {
66+
$query->where('active', 1);
67+
})
68+
->with(['services' => function($query) {
69+
$query->where('active', 1);
70+
}])
71+
->get();
72+
73+
$categories = Category::orderBy('name')
74+
->whereHas('services', function($query) {
75+
$query->where('active', 1);
76+
})
77+
->with(['services' => function($query) {
78+
$query->where('active', 1);
79+
}])
80+
->get();
81+
82+
return view('report')->with(compact('services', 'clients', 'categories'));
83+
}
5284
}

app/Http/Controllers/ServiceController.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Http\Request;
99
use App\Events\ServiceWasCreated;
1010
use App\Events\ServiceWasUpdated;
11+
use App\Category;
1112

1213
class ServiceController extends Controller
1314
{
@@ -18,7 +19,10 @@ class ServiceController extends Controller
1819
*/
1920
public function index()
2021
{
21-
$services = Service::orderBy('month')->orderBy('day')->with(['client'])->get();
22+
$services = Service::orderBy('month')
23+
->orderBy('day')
24+
->with(['client', 'category'])
25+
->get();
2226

2327
return view('services.index')->with(compact('services'));
2428
}
@@ -36,8 +40,9 @@ public function create()
3640
'eur' => 'EUR',
3741
];
3842
$clients = Client::orderBy('name')->pluck('name', 'id')->toArray();
43+
$categories = Category::orderBy('name')->pluck('name', 'id')->toArray();
3944

40-
return view('services.create')->with(compact('currencies', 'clients'));
45+
return view('services.create')->with(compact('currencies', 'clients', 'categories'));
4146
}
4247

4348
/**
@@ -57,10 +62,12 @@ public function store(Request $request)
5762
'currency' => 'required|in:hrk,usd,eur',
5863
'exchange_rate' => 'required|numeric|min:0',
5964
'active' => 'boolean',
60-
'client_id' => 'required|exists:clients,id'
65+
'client_id' => 'required|exists:clients,id',
66+
'category_id' => 'required|exists:categories,id'
6167
]);
6268

6369
$client = Client::find($request->get('client_id'));
70+
$category = Category::find($request->get('category_id'));
6471

6572
$service = new Service;
6673
$service->title = $request->get('title');
@@ -72,6 +79,7 @@ public function store(Request $request)
7279
$service->exchange_rate = $request->get('exchange_rate');
7380
$service->active = $request->get('active', false);
7481
$service->client()->associate($client);
82+
$service->category()->associate($category);
7583
$service->save();
7684

7785
event(new ServiceWasCreated($service));
@@ -96,8 +104,9 @@ public function edit($id)
96104
'eur' => 'EUR',
97105
];
98106
$clients = Client::orderBy('name')->pluck('name', 'id')->toArray();
107+
$categories = Category::orderBy('name')->pluck('name', 'id')->toArray();
99108

100-
return view('services.edit')->with(compact('service', 'currencies', 'clients'));
109+
return view('services.edit')->with(compact('service', 'currencies', 'clients', 'categories'));
101110
}
102111

103112
/**
@@ -120,7 +129,8 @@ public function update(Request $request, $id)
120129
'currency' => 'required|in:hrk,usd,eur',
121130
'exchange_rate' => 'required|numeric|min:0',
122131
'active' => 'boolean',
123-
'client_id' => 'required|exists:clients,id'
132+
'client_id' => 'required|exists:clients,id',
133+
'category_id' => 'required|exists:categories,id'
124134
]);
125135

126136
$service->update([
@@ -136,6 +146,8 @@ public function update(Request $request, $id)
136146

137147
$client = Client::find($request->get('client_id'));
138148
$service->client()->associate($client);
149+
$category = Category::find($request->get('category_id'));
150+
$service->category()->associate($category);
139151
$service->save();
140152

141153
event(new ServiceWasUpdated($service));

app/Http/api_routes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
Route::group(['prefix' => 'api/v1', 'middleware' => 'auth:api'], function () {
44
Route::get('quote', 'QuoteController@getQuote');
5+
6+
Route::post('categories', 'CategoryController@store');
7+
58
Route::get('occurrences/{occurrence}/offer', 'OccurrenceController@toggleOffer');
69
Route::get('occurrences/{occurrence}/payment', 'OccurrenceController@togglePayment');
710
Route::get('occurrences/{occurrence}/receipt', 'OccurrenceController@toggleReceipt');

app/Http/routes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
Route::group(['middleware' => 'auth'], function () {
1515
Route::get('/', 'HomeController@index');
1616

17+
Route::get('report', 'HomeController@report');
18+
1719
Route::resource('clients', 'ClientController');
1820

21+
Route::resource('categories', 'CategoryController');
22+
1923
Route::resource('services', 'ServiceController');
2024
});
2125

app/Service.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App;
44

55
use App\Client;
6+
use App\Category;
67
use App\Occurrence;
78
use Illuminate\Database\Eloquent\Model;
89
use Collective\Html\Eloquent\FormAccessible;
@@ -23,6 +24,16 @@ public function client()
2324
return $this->belongsTo(Client::class);
2425
}
2526

27+
/**
28+
* To which category does this service belong.
29+
*
30+
* @return [type]
31+
*/
32+
public function category()
33+
{
34+
return $this->belongsTo(Category::class);
35+
}
36+
2637
/**
2738
* One per year would be perfect. :)
2839
*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateCategoriesTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('categories', function (Blueprint $table) {
16+
$table->increments('id');
17+
$table->string('name');
18+
$table->timestamps();
19+
});
20+
21+
Schema::table('services', function(Blueprint $table) {
22+
$table->integer('category_id')->unsigned()->nullable();
23+
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::table('services', function(Blueprint $table) {
35+
$table->dropColumn('category_id');
36+
$table->dropForeign(['category_id']);
37+
});
38+
39+
Schema::drop('categories');
40+
}
41+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"bootstrap": "^3.3.6",
1212
"bootstrap-less": "^3.3.8",
1313
"font-awesome": "^4.6.3",
14-
"jquery": "^3.1.0"
14+
"jquery": "^3.1.0",
15+
"selectize": "^0.12.2"
1516
}
1617
}

0 commit comments

Comments
 (0)