Skip to content

Commit

Permalink
fix: appointment issues
Browse files Browse the repository at this point in the history
  • Loading branch information
itsemon245 committed Jul 3, 2024
1 parent 529b256 commit 821e0a4
Show file tree
Hide file tree
Showing 16 changed files with 314 additions and 199 deletions.
130 changes: 53 additions & 77 deletions app/Http/Controllers/Backend/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,30 @@
use App\Models\Withdrawal;
use Carbon\Carbon;

class DashboardController extends Controller
{
public function __construct()
{
class DashboardController extends Controller {
public function __construct() {
$this->middleware(['auth', 'verified']);
}

/*
* create dashboard view page
*/
public function index()
{
* create dashboard view page
*/
public function index() {
$statReports = $this->getStatReports();
$expertStats = [];
$recentConsultations = null;

if (auth()->user()->role_name == 'expert') {
$expertProfile = auth()->user()->expertProfile;
$recentConsultations = UserAppointment::where('expert_profile_id', $expertProfile->id)
->latest()
->limit(10)
->paginate();
$expertStats['Today'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
->whereDate('created_at', now())
->count();
$expertStats['This week'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
->whereBetween('created_at', [
now()->subWeek()->startOf('week'),
now()->startOf('week')
])
->count();
$expertStats['This month'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
->whereBetween('created_at', [
now()->subMonth()->startOf('month'),
now()->startOf('month')
])
->count();
$expertStats['This year'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
->whereBetween('created_at', [
now()->subYear()->startOf('year'),
now()->startOf('year')
])
->count();
$expertStats['All time'] = UserAppointment::where('expert_profile_id', $expertProfile->id)
->count();
$expert = auth()->user()->expertProfile;
$recentConsultations = $expert->appointments()
->unapproved()
->latest()
->limit(10)
->paginate();
} else {
$expert = null;
$recentConsultations = null;
}

$fiscalYear = FiscalYear::where('year', currentFiscalYear())->first();
$chartData = $this->getChartData();
$events = Calendar::with('client')->latest()->latest()->get();
Expand All @@ -80,11 +58,46 @@ public function index()
'total' => $totalExpense,
'today' => $todaysExpense?->amount ?? 0
];
return view('backend.dashboard.dashboard', compact('statReports', 'expertStats', 'recentConsultations', 'clients', 'expenses', 'events', 'today', 'services', 'currentEvents', 'fiscalYear', 'chartData', 'projects'));
return view('backend.dashboard.dashboard', compact('statReports', 'expert', 'recentConsultations', 'clients', 'expenses', 'events', 'today', 'services', 'currentEvents', 'fiscalYear', 'chartData', 'projects'));
}

public function getChartData() {
$fiscalYears = FiscalYear::orderBy('year', 'desc')->latest()->latest()->take(3)->get();

$mappedItems = $fiscalYears->map(function ($fy) {
$data = collect([
[
'x' => 'Overdue',
'y' => $fy->invoices()->wherePivot('status', 'overdue')->sum('due')
],
[
'x' => 'Paid',
'y' => $fy->invoices()->wherePivot('status', 'paid')->sum('paid')
],
[
'x' => 'Partial (Paid)',
'y' => $fy->invoices()->wherePivot('status', 'partial')->sum('paid')
],
[
'x' => 'Partial (Due)',
'y' => $fy->invoices()->wherePivot('status', 'partial')->sum('due')
],
[
'x' => 'Due',
'y' => $fy->invoices()->wherePivot('status', 'due')->sum('due')
],
]);
return [
'name' => $fy->year,
'data' => $data
];
});
// dd($mappedItems);

return $mappedItems;
}

protected function getStatReports(): array
{
protected function getStatReports(): array {
$statReports = [];
$filters = [
'daily',
Expand Down Expand Up @@ -321,41 +334,4 @@ protected function getStatReports(): array

return $statReports;
}

public function getChartData()
{
$fiscalYears = FiscalYear::orderBy('year', 'desc')->latest()->latest()->take(3)->get();

$mappedItems = $fiscalYears->map(function ($fy) {
$data = collect([
[
'x' => 'Overdue',
'y' => $fy->invoices()->wherePivot('status', 'overdue')->sum('due')
],
[
'x' => 'Paid',
'y' => $fy->invoices()->wherePivot('status', 'paid')->sum('paid')
],
[
'x' => 'Partial (Paid)',
'y' => $fy->invoices()->wherePivot('status', 'partial')->sum('paid')
],
[
'x' => 'Partial (Due)',
'y' => $fy->invoices()->wherePivot('status', 'partial')->sum('due')
],
[
'x' => 'Due',
'y' => $fy->invoices()->wherePivot('status', 'due')->sum('due')
],
]);
return [
'name' => $fy->year,
'data' => $data
];
});
// dd($mappedItems);

return $mappedItems;
}
}
115 changes: 66 additions & 49 deletions app/Http/Controllers/Backend/UserAppointmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,49 @@

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use App\Models\AppointmentTime;
use App\Models\Calendar;
use App\Models\UserAppointment;
use App\Notifications\AppointmentApprovedNotification;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use App\Models\Calendar;
use Illuminate\Http\Request;
use App\Models\ExpertProfile;
use App\Models\AppointmentTime;
use App\Models\UserAppointment;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Notification;
use App\Notifications\AppointmentApprovedNotification;
use Log;

class UserAppointmentController extends Controller
{
public function __construct() {
public function __construct()
{
if (auth()->user() == null) {
return redirect(route('login'));
}
if (request('type') != 'consultation'){
if(!auth()->user()->hasRole('super admin')){
if (request('type') != 'consultation') {
if(!auth()->user()->hasRole('super admin')) {
abort(403, 'You don\'t have access to this page');
}
}
}
public function index()
{
/**
* @var ?ExpertProfile $expert
*/
$expert = auth()->user()->expertProfile;
$appointments = UserAppointment::where('is_approved', false)
->where(function (Builder $q) use ($expert) {
if (request('type') == 'consultation') {
$q->whereNotNull('expert_profile_id');
if ($expert != null) {
$q->where('expert_profile_id', $expert->id);
}
}{
$q->whereNull('expert_profile_id');
}
})
->with('map', 'user')->latest()->paginate(paginateCount());
$apt = $appointments->first();
if ($expert) {
$appointments = $expert->appointments()
->unapproved()
->with('map', 'user')
->latest()
->paginate(paginateCount());
} else {
$appointments = UserAppointment::unapproved()
->whereNull('expert_profile_id')
->with('map', 'user')
->latest()
->paginate(paginateCount());
}

return view('backend.user.appointments', compact('appointments'));
}
Expand All @@ -60,7 +65,7 @@ public function timesUpdate(Request $request)
foreach ($request->times as $day => $times) {
AppointmentTime::create([
'user_id' => auth()->id(),
'day'=> $day,
'day' => $day,
'times' => $times
]);
}
Expand All @@ -82,39 +87,46 @@ public function timeDelete(AppointmentTime $time)

public function approvedList()
{
/**
* @var ?ExpertProfile $expert
*/
$expert = auth()->user()->expertProfile;
$appointments = UserAppointment::where(['is_approved' => true, 'is_completed' => false])
->where(function (Builder $q) use ($expert) {
if (request('type') == 'consultation') {
$q->whereNotNull('expert_profile_id');
if ($expert != null) {
$q->where('expert_profile_id', $expert->id);
}
}{
$q->whereNull('expert_profile_id');
}
})
->with('map', 'user')->latest('approved_at')->get();
if ($expert) {
$appointments = $expert->appointments()
->approvedOnly()
->with('map', 'user')
->latest()
->paginate(paginateCount());
} else {
$appointments = UserAppointment::approvedOnly()
->whereNull('expert_profile_id')
->with('map', 'user')
->latest()
->paginate(paginateCount());
}

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

public function completedList()
{
/**
* @var ?ExpertProfile $expert
*/
$expert = auth()->user()->expertProfile;
$appointments = UserAppointment::where(['is_completed' => true])
->where(function (Builder $q) use ($expert) {
if (request('type') == 'consultation') {
$q->whereNotNull('expert_profile_id');
if ($expert != null) {
$q->where('expert_profile_id', $expert->id);
}
}{
$q->whereNull('expert_profile_id');
}
})
->with('map', 'user')->latest('completed_at')->get();

if ($expert) {
$appointments = $expert->appointments()
->completedOnly()
->with('map', 'user')
->latest()
->paginate(paginateCount());
} else {
$appointments = UserAppointment::completedOnly()
->whereNull('expert_profile_id')
->with('map', 'user')
->latest()
->paginate(paginateCount());
}
return view('backend.user.appointmentsCompleted', compact('appointments'));
}

Expand All @@ -141,7 +153,11 @@ public function approve(int $id)
'start' => Carbon::parse($appointment->date.', '.$appointment->time, 'Asia/Dhaka')->format('Y-m-d H:m:s'),
'description' => null,
]);
Notification::route('mail', $appointment->email)->notify(new AppointmentApprovedNotification($appointment));
try {
Notification::route('mail', $appointment->email)->notify(new AppointmentApprovedNotification($appointment));
} catch (\Throwable $th) {
Log::error($th->getMessage());
}
$alert = [
'message' => 'Appointment Approved',
'alert-type' => 'success',
Expand All @@ -165,6 +181,7 @@ public function complete(int $id)
$appointment->is_completed = true;
$appointment->completed_at = now();
$appointment->update();
dd($appointment->refresh());
Calendar::create([
'title' => 'Appointment Completed',
'user_appointment_id' => $appointment->id,
Expand Down
7 changes: 6 additions & 1 deletion app/Http/Controllers/ExpertController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ public function browse(Request $request)
->latest()->paginate(20);
$posts = ExpertProfile::distinct()->latest()->get('post')->pluck('post');
$districts = ExpertProfile::distinct()->latest()->get('district')->pluck('district');
$thanas = ExpertProfile::distinct()->latest()->get('thana')->pluck('thana');
$district = request()->query('district') ?? ($districts[0] ?? null);
if ($district) {
$thanas = ExpertProfile::where('district', $district)->distinct()->latest()->get('thana')->pluck('thana');
}else{
$thanas = ExpertProfile::distinct()->latest()->get('thana')->pluck('thana');
}
$minExp = ExpertProfile::min('experience');
$maxExp = ExpertProfile::max('experience');
$reviews = Review::with('user')
Expand Down
15 changes: 9 additions & 6 deletions app/Http/Controllers/UserAppointmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

use Illuminate\Http\Request;
use App\Models\UserAppointment;
use App\Http\Requests\StoreUserAppointmentRequest;
use App\Http\Requests\UpdateUserAppointmentRequest;

class UserAppointmentController extends Controller
{
class UserAppointmentController extends Controller {
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
public function store(Request $request) {
if (!$request->is_physical && $request->location == null) {
$alert = [
'alert-type' => 'error',
'message' => 'No location has been selected'
];
return back()->with($alert);
}
$appointment = UserAppointment::create([
"date" => $request->date,
"expert_profile_id" => $request->expert_id,
Expand Down
3 changes: 3 additions & 0 deletions app/Models/ExpertProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ public function expertCategories()
{
return $this->belongsToMany(ExpertCategory::class);
}
public function appointments(){
return $this->hasMany(UserAppointment::class, 'expert_profile_id');
}
}
Loading

0 comments on commit 821e0a4

Please sign in to comment.