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

Commit 03ad166

Browse files
author
Mario Basic
committed
A lot of good things.
1 parent 89d8e75 commit 03ad166

18 files changed

+271
-35
lines changed

app/Events/ServiceWasCreated.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Events\Event;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
8+
9+
class ServiceWasCreated extends Event
10+
{
11+
use SerializesModels;
12+
13+
public $service;
14+
15+
/**
16+
* Create a new event instance.
17+
*
18+
* @return void
19+
*/
20+
public function __construct($service)
21+
{
22+
$this->service = $service;
23+
}
24+
25+
/**
26+
* Get the channels the event should be broadcast on.
27+
*
28+
* @return array
29+
*/
30+
public function broadcastOn()
31+
{
32+
return [];
33+
}
34+
}

app/Events/ServiceWasUpdated.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Events\Event;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
8+
9+
class ServiceWasUpdated extends Event
10+
{
11+
public $service;
12+
13+
use SerializesModels;
14+
15+
/**
16+
* Create a new event instance.
17+
*
18+
* @return void
19+
*/
20+
public function __construct($service)
21+
{
22+
$this->service = $service;
23+
}
24+
25+
/**
26+
* Get the channels the event should be broadcast on.
27+
*
28+
* @return array
29+
*/
30+
public function broadcastOn()
31+
{
32+
return [];
33+
}
34+
}

app/Http/Controllers/HomeController.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Occurrence;
56
use App\Http\Requests;
67
use Illuminate\Http\Request;
78

89
class HomeController extends Controller
910
{
10-
/**
11-
* Create a new controller instance.
12-
*
13-
* @return void
14-
*/
15-
public function __construct()
16-
{
17-
$this->middleware('auth');
18-
}
19-
2011
/**
2112
* Show the application dashboard.
2213
*
2314
* @return \Illuminate\Http\Response
2415
*/
2516
public function index()
2617
{
27-
return view('home');
18+
$currentMonth = date('m');
19+
$currentYear = date('Y');
20+
$occurrences = Occurrence::orderBy('occurs_at')
21+
// Where occurs_at month and year is the same as current month and year
22+
->where(function($query) use($currentMonth, $currentYear) {
23+
$query->where('occurs_at', 'like', $currentYear . '-' . $currentMonth . '-%');
24+
})
25+
// Return only occurrences that belong to services that are active
26+
->whereHas('service', function($query) {
27+
$query->where('active', 1);
28+
})->with(['service.client'])->get();
29+
30+
return view('home')->with(compact('occurrences'));
2831
}
2932
}

app/Http/Controllers/ServiceController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use App\Service;
77
use App\Http\Requests;
88
use Illuminate\Http\Request;
9+
use App\Events\ServiceWasCreated;
10+
use App\Events\ServiceWasUpdated;
911

1012
class ServiceController extends Controller
1113
{
@@ -70,6 +72,8 @@ public function store(Request $request)
7072
$service->client()->associate($client);
7173
$service->save();
7274

75+
event(new ServiceWasCreated($service));
76+
7377
flash()->success('Service created!');
7478

7579
return redirect()->route('services.index');
@@ -129,6 +133,8 @@ public function update(Request $request, $id)
129133
$client = Client::find($request->get('client_id'));
130134
$service->client()->associate($client);
131135

136+
event(new ServiceWasUpdated($service));
137+
132138
flash()->success('Service Updated!');
133139

134140
return redirect()->route('services.edit', $service->id);

app/Http/routes.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
| It's a breeze. Simply tell Laravel the URIs it should respond to
1010
| and give it the controller to call when that URI is requested.
1111
|
12-
*/
12+
*/
13+
14+
Route::group(['middleware' => 'auth'], function () {
15+
Route::get('/', 'HomeController@index');
1316

14-
Route::group(['middleware' => 'auth'], function() {
1517
Route::resource('clients', 'ClientController');
18+
1619
Route::resource('services', 'ServiceController');
1720
});
1821

1922
Route::auth();
20-
21-
Route::get('/', 'HomeController@index');
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Occurrence;
6+
use App\Events\ServiceWasCreated;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
10+
class CreateServiceOccurrence
11+
{
12+
/**
13+
* Create the event listener.
14+
*
15+
* @return void
16+
*/
17+
public function __construct()
18+
{
19+
//
20+
}
21+
22+
/**
23+
* Handle the event.
24+
*
25+
* @param ServiceWasCreated $event
26+
* @return void
27+
*/
28+
public function handle(ServiceWasCreated $event)
29+
{
30+
$date = \Carbon\Carbon::createFromDate(
31+
date('Y'),
32+
$event->service->month,
33+
$event->service->day
34+
);
35+
36+
$occurence = new Occurrence;
37+
$occurence->occurs_at = $date->timestamp;
38+
$occurence->offer_sent = false;
39+
$occurence->payment_received = false;
40+
$occurence->receipt_sent = false;
41+
$occurence->service()->associate($event->service);
42+
$occurence->save();
43+
44+
/**
45+
* TODO: Maybe do a check here if the occurence for this service
46+
* already exists ???
47+
*/
48+
}
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\ServiceWasUpdated;
6+
use Illuminate\Queue\InteractsWithQueue;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
9+
class UpdateServiceOccurrences
10+
{
11+
/**
12+
* Create the event listener.
13+
*
14+
* @return void
15+
*/
16+
public function __construct()
17+
{
18+
//
19+
}
20+
21+
/**
22+
* Handle the event.
23+
*
24+
* @param ServiceWasUpdated $event
25+
* @return void
26+
*/
27+
public function handle(ServiceWasUpdated $event)
28+
{
29+
foreach($event->service->occurrences()->get() as $occurence)
30+
{
31+
$date = \Carbon\Carbon::createFromDate(
32+
$occurence->occurs_at->year,
33+
$event->service->month,
34+
$event->service->day
35+
);
36+
37+
$occurence->occurs_at = $date->timestamp;
38+
$occurence->save();
39+
}
40+
}
41+
}

app/Occurrence.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\Service;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Occurrence extends Model
9+
{
10+
protected $fillable = ['occurs_at', 'offer_sent', 'payment_received', 'receipt_sent'];
11+
12+
protected $dates = ['occurs_at'];
13+
14+
public function service()
15+
{
16+
return $this->belongsTo(Service::class);
17+
}
18+
}

app/Providers/EventServiceProvider.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ class EventServiceProvider extends ServiceProvider
1313
* @var array
1414
*/
1515
protected $listen = [
16-
'App\Events\SomeEvent' => [
17-
'App\Listeners\EventListener',
16+
'App\Events\ServiceWasCreated' => [
17+
'App\Listeners\CreateServiceOccurrence',
18+
],
19+
'App\Events\ServiceWasUpdated' => [
20+
'App\Listeners\UpdateServiceOccurrences',
1821
],
1922
];
2023

app/Service.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,30 @@
33
namespace App;
44

55
use App\Client;
6+
use App\Occurance;
67
use Illuminate\Database\Eloquent\Model;
78

89
class Service extends Model
910
{
1011
protected $fillable = ['title', 'note', 'month', 'day', 'cost', 'currency', 'active'];
1112

13+
/**
14+
* To which client does this service belong.
15+
*
16+
* @return [type]
17+
*/
1218
public function client()
1319
{
1420
return $this->belongsTo(Client::class);
1521
}
22+
23+
/**
24+
* One per year would be perfect. :)
25+
*
26+
* @return [type]
27+
*/
28+
public function occurances()
29+
{
30+
return $this->hasMany(Occurance::class);
31+
}
1632
}

0 commit comments

Comments
 (0)