Skip to content

Commit 821e0a4

Browse files
committed
fix: appointment issues
1 parent 529b256 commit 821e0a4

16 files changed

+314
-199
lines changed

app/Http/Controllers/Backend/DashboardController.php

+53-77
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,30 @@
1414
use App\Models\Withdrawal;
1515
use Carbon\Carbon;
1616

17-
class DashboardController extends Controller
18-
{
19-
public function __construct()
20-
{
17+
class DashboardController extends Controller {
18+
public function __construct() {
2119
$this->middleware(['auth', 'verified']);
2220
}
2321

2422
/*
25-
* create dashboard view page
26-
*/
27-
public function index()
28-
{
23+
* create dashboard view page
24+
*/
25+
public function index() {
2926
$statReports = $this->getStatReports();
3027
$expertStats = [];
3128
$recentConsultations = null;
32-
3329
if (auth()->user()->role_name == 'expert') {
34-
$expertProfile = auth()->user()->expertProfile;
35-
$recentConsultations = UserAppointment::where('expert_profile_id', $expertProfile->id)
36-
->latest()
37-
->limit(10)
38-
->paginate();
39-
$expertStats['Today'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
40-
->whereDate('created_at', now())
41-
->count();
42-
$expertStats['This week'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
43-
->whereBetween('created_at', [
44-
now()->subWeek()->startOf('week'),
45-
now()->startOf('week')
46-
])
47-
->count();
48-
$expertStats['This month'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
49-
->whereBetween('created_at', [
50-
now()->subMonth()->startOf('month'),
51-
now()->startOf('month')
52-
])
53-
->count();
54-
$expertStats['This year'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
55-
->whereBetween('created_at', [
56-
now()->subYear()->startOf('year'),
57-
now()->startOf('year')
58-
])
59-
->count();
60-
$expertStats['All time'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
61-
->count();
30+
$expert = auth()->user()->expertProfile;
31+
$recentConsultations = $expert->appointments()
32+
->unapproved()
33+
->latest()
34+
->limit(10)
35+
->paginate();
36+
} else {
37+
$expert = null;
38+
$recentConsultations = null;
6239
}
40+
6341
$fiscalYear = FiscalYear::where('year', currentFiscalYear())->first();
6442
$chartData = $this->getChartData();
6543
$events = Calendar::with('client')->latest()->latest()->get();
@@ -80,11 +58,46 @@ public function index()
8058
'total' => $totalExpense,
8159
'today' => $todaysExpense?->amount ?? 0
8260
];
83-
return view('backend.dashboard.dashboard', compact('statReports', 'expertStats', 'recentConsultations', 'clients', 'expenses', 'events', 'today', 'services', 'currentEvents', 'fiscalYear', 'chartData', 'projects'));
61+
return view('backend.dashboard.dashboard', compact('statReports', 'expert', 'recentConsultations', 'clients', 'expenses', 'events', 'today', 'services', 'currentEvents', 'fiscalYear', 'chartData', 'projects'));
62+
}
63+
64+
public function getChartData() {
65+
$fiscalYears = FiscalYear::orderBy('year', 'desc')->latest()->latest()->take(3)->get();
66+
67+
$mappedItems = $fiscalYears->map(function ($fy) {
68+
$data = collect([
69+
[
70+
'x' => 'Overdue',
71+
'y' => $fy->invoices()->wherePivot('status', 'overdue')->sum('due')
72+
],
73+
[
74+
'x' => 'Paid',
75+
'y' => $fy->invoices()->wherePivot('status', 'paid')->sum('paid')
76+
],
77+
[
78+
'x' => 'Partial (Paid)',
79+
'y' => $fy->invoices()->wherePivot('status', 'partial')->sum('paid')
80+
],
81+
[
82+
'x' => 'Partial (Due)',
83+
'y' => $fy->invoices()->wherePivot('status', 'partial')->sum('due')
84+
],
85+
[
86+
'x' => 'Due',
87+
'y' => $fy->invoices()->wherePivot('status', 'due')->sum('due')
88+
],
89+
]);
90+
return [
91+
'name' => $fy->year,
92+
'data' => $data
93+
];
94+
});
95+
// dd($mappedItems);
96+
97+
return $mappedItems;
8498
}
8599

86-
protected function getStatReports(): array
87-
{
100+
protected function getStatReports(): array {
88101
$statReports = [];
89102
$filters = [
90103
'daily',
@@ -321,41 +334,4 @@ protected function getStatReports(): array
321334

322335
return $statReports;
323336
}
324-
325-
public function getChartData()
326-
{
327-
$fiscalYears = FiscalYear::orderBy('year', 'desc')->latest()->latest()->take(3)->get();
328-
329-
$mappedItems = $fiscalYears->map(function ($fy) {
330-
$data = collect([
331-
[
332-
'x' => 'Overdue',
333-
'y' => $fy->invoices()->wherePivot('status', 'overdue')->sum('due')
334-
],
335-
[
336-
'x' => 'Paid',
337-
'y' => $fy->invoices()->wherePivot('status', 'paid')->sum('paid')
338-
],
339-
[
340-
'x' => 'Partial (Paid)',
341-
'y' => $fy->invoices()->wherePivot('status', 'partial')->sum('paid')
342-
],
343-
[
344-
'x' => 'Partial (Due)',
345-
'y' => $fy->invoices()->wherePivot('status', 'partial')->sum('due')
346-
],
347-
[
348-
'x' => 'Due',
349-
'y' => $fy->invoices()->wherePivot('status', 'due')->sum('due')
350-
],
351-
]);
352-
return [
353-
'name' => $fy->year,
354-
'data' => $data
355-
];
356-
});
357-
// dd($mappedItems);
358-
359-
return $mappedItems;
360-
}
361337
}

app/Http/Controllers/Backend/UserAppointmentController.php

+66-49
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,49 @@
22

33
namespace App\Http\Controllers\Backend;
44

5-
use App\Http\Controllers\Controller;
6-
use App\Models\AppointmentTime;
7-
use App\Models\Calendar;
8-
use App\Models\UserAppointment;
9-
use App\Notifications\AppointmentApprovedNotification;
105
use Carbon\Carbon;
11-
use Illuminate\Database\Eloquent\Builder;
6+
use App\Models\Calendar;
127
use Illuminate\Http\Request;
8+
use App\Models\ExpertProfile;
9+
use App\Models\AppointmentTime;
10+
use App\Models\UserAppointment;
11+
use App\Http\Controllers\Controller;
1312
use Illuminate\Support\Facades\Notification;
13+
use App\Notifications\AppointmentApprovedNotification;
14+
use Log;
1415

1516
class UserAppointmentController extends Controller
1617
{
17-
public function __construct() {
18+
public function __construct()
19+
{
1820
if (auth()->user() == null) {
1921
return redirect(route('login'));
2022
}
21-
if (request('type') != 'consultation'){
22-
if(!auth()->user()->hasRole('super admin')){
23+
if (request('type') != 'consultation') {
24+
if(!auth()->user()->hasRole('super admin')) {
2325
abort(403, 'You don\'t have access to this page');
2426
}
2527
}
2628
}
2729
public function index()
2830
{
31+
/**
32+
* @var ?ExpertProfile $expert
33+
*/
2934
$expert = auth()->user()->expertProfile;
30-
$appointments = UserAppointment::where('is_approved', false)
31-
->where(function (Builder $q) use ($expert) {
32-
if (request('type') == 'consultation') {
33-
$q->whereNotNull('expert_profile_id');
34-
if ($expert != null) {
35-
$q->where('expert_profile_id', $expert->id);
36-
}
37-
}{
38-
$q->whereNull('expert_profile_id');
39-
}
40-
})
41-
->with('map', 'user')->latest()->paginate(paginateCount());
42-
$apt = $appointments->first();
35+
if ($expert) {
36+
$appointments = $expert->appointments()
37+
->unapproved()
38+
->with('map', 'user')
39+
->latest()
40+
->paginate(paginateCount());
41+
} else {
42+
$appointments = UserAppointment::unapproved()
43+
->whereNull('expert_profile_id')
44+
->with('map', 'user')
45+
->latest()
46+
->paginate(paginateCount());
47+
}
4348

4449
return view('backend.user.appointments', compact('appointments'));
4550
}
@@ -60,7 +65,7 @@ public function timesUpdate(Request $request)
6065
foreach ($request->times as $day => $times) {
6166
AppointmentTime::create([
6267
'user_id' => auth()->id(),
63-
'day'=> $day,
68+
'day' => $day,
6469
'times' => $times
6570
]);
6671
}
@@ -82,39 +87,46 @@ public function timeDelete(AppointmentTime $time)
8287

8388
public function approvedList()
8489
{
90+
/**
91+
* @var ?ExpertProfile $expert
92+
*/
8593
$expert = auth()->user()->expertProfile;
86-
$appointments = UserAppointment::where(['is_approved' => true, 'is_completed' => false])
87-
->where(function (Builder $q) use ($expert) {
88-
if (request('type') == 'consultation') {
89-
$q->whereNotNull('expert_profile_id');
90-
if ($expert != null) {
91-
$q->where('expert_profile_id', $expert->id);
92-
}
93-
}{
94-
$q->whereNull('expert_profile_id');
95-
}
96-
})
97-
->with('map', 'user')->latest('approved_at')->get();
94+
if ($expert) {
95+
$appointments = $expert->appointments()
96+
->approvedOnly()
97+
->with('map', 'user')
98+
->latest()
99+
->paginate(paginateCount());
100+
} else {
101+
$appointments = UserAppointment::approvedOnly()
102+
->whereNull('expert_profile_id')
103+
->with('map', 'user')
104+
->latest()
105+
->paginate(paginateCount());
106+
}
98107

99108
return view('backend.user.appointmentsApproved', compact('appointments'));
100109
}
101110

102111
public function completedList()
103112
{
113+
/**
114+
* @var ?ExpertProfile $expert
115+
*/
104116
$expert = auth()->user()->expertProfile;
105-
$appointments = UserAppointment::where(['is_completed' => true])
106-
->where(function (Builder $q) use ($expert) {
107-
if (request('type') == 'consultation') {
108-
$q->whereNotNull('expert_profile_id');
109-
if ($expert != null) {
110-
$q->where('expert_profile_id', $expert->id);
111-
}
112-
}{
113-
$q->whereNull('expert_profile_id');
114-
}
115-
})
116-
->with('map', 'user')->latest('completed_at')->get();
117-
117+
if ($expert) {
118+
$appointments = $expert->appointments()
119+
->completedOnly()
120+
->with('map', 'user')
121+
->latest()
122+
->paginate(paginateCount());
123+
} else {
124+
$appointments = UserAppointment::completedOnly()
125+
->whereNull('expert_profile_id')
126+
->with('map', 'user')
127+
->latest()
128+
->paginate(paginateCount());
129+
}
118130
return view('backend.user.appointmentsCompleted', compact('appointments'));
119131
}
120132

@@ -141,7 +153,11 @@ public function approve(int $id)
141153
'start' => Carbon::parse($appointment->date.', '.$appointment->time, 'Asia/Dhaka')->format('Y-m-d H:m:s'),
142154
'description' => null,
143155
]);
144-
Notification::route('mail', $appointment->email)->notify(new AppointmentApprovedNotification($appointment));
156+
try {
157+
Notification::route('mail', $appointment->email)->notify(new AppointmentApprovedNotification($appointment));
158+
} catch (\Throwable $th) {
159+
Log::error($th->getMessage());
160+
}
145161
$alert = [
146162
'message' => 'Appointment Approved',
147163
'alert-type' => 'success',
@@ -165,6 +181,7 @@ public function complete(int $id)
165181
$appointment->is_completed = true;
166182
$appointment->completed_at = now();
167183
$appointment->update();
184+
dd($appointment->refresh());
168185
Calendar::create([
169186
'title' => 'Appointment Completed',
170187
'user_appointment_id' => $appointment->id,

app/Http/Controllers/ExpertController.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ public function browse(Request $request)
7272
->latest()->paginate(20);
7373
$posts = ExpertProfile::distinct()->latest()->get('post')->pluck('post');
7474
$districts = ExpertProfile::distinct()->latest()->get('district')->pluck('district');
75-
$thanas = ExpertProfile::distinct()->latest()->get('thana')->pluck('thana');
75+
$district = request()->query('district') ?? ($districts[0] ?? null);
76+
if ($district) {
77+
$thanas = ExpertProfile::where('district', $district)->distinct()->latest()->get('thana')->pluck('thana');
78+
}else{
79+
$thanas = ExpertProfile::distinct()->latest()->get('thana')->pluck('thana');
80+
}
7681
$minExp = ExpertProfile::min('experience');
7782
$maxExp = ExpertProfile::max('experience');
7883
$reviews = Review::with('user')

app/Http/Controllers/UserAppointmentController.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44

55
use Illuminate\Http\Request;
66
use App\Models\UserAppointment;
7-
use App\Http\Requests\StoreUserAppointmentRequest;
8-
use App\Http\Requests\UpdateUserAppointmentRequest;
97

10-
class UserAppointmentController extends Controller
11-
{
8+
class UserAppointmentController extends Controller {
129
/**
1310
* Store a newly created resource in storage.
1411
*/
15-
public function store(Request $request)
16-
{
12+
public function store(Request $request) {
13+
if (!$request->is_physical && $request->location == null) {
14+
$alert = [
15+
'alert-type' => 'error',
16+
'message' => 'No location has been selected'
17+
];
18+
return back()->with($alert);
19+
}
1720
$appointment = UserAppointment::create([
1821
"date" => $request->date,
1922
"expert_profile_id" => $request->expert_id,

app/Models/ExpertProfile.php

+3
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ public function expertCategories()
2929
{
3030
return $this->belongsToMany(ExpertCategory::class);
3131
}
32+
public function appointments(){
33+
return $this->hasMany(UserAppointment::class, 'expert_profile_id');
34+
}
3235
}

0 commit comments

Comments
 (0)