File: /mnt/data/ghayatcom/ghayatcom-api/app/Http/Controllers/Api/DashboardController.php
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Resources\AppointmentDetailResource;
use App\Http\Resources\PrescriptionCollection;
use Illuminate\Database\QueryException;
use App\{Blog,User,Appointment,DependantAccountHolder,Payment,HealthRecords,Prescription,PrescriptionRequest,UserPharmacy};
use App\Http\Resources\UserSettingsResource;
use Exception;
use Throwable;
use DB;
use Auth;
use Storage;
use Illuminate\Support\Carbon;
use App\Enums\AppoitmentStatusEnum;
class DashboardController extends Controller
{
/**
* @param Request $request
* @return string|null
**/
public function index(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$result = Blog::orderBy('id','desc')->limit(4)->get();
return self::sentResponse(200, $result, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
/**
* @param Request $request
* @return object
**/
public function getUser(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$userData = User::where('id',$auth_id)->with(['doctorDocument','userDetail','securitySettings','notificationSettings'])->withCount(['insuranceDetail','myDependants','healthProfile'])->first();
$userData->unread_notify = auth()->user()->unreadNotifications()->count();
return new UserSettingsResource($userData, 200, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
/**
* @param Request $request
* @return string|null
**/
public function patientDashboardCount(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$appointmentArr = [];
$dependantArr = [$auth_id];
$dependantQ = DependantAccountHolder::where('user_id', $auth_id)->where('status', 1)->get();
for ($i=0; $i < count($dependantQ); $i++) {
array_push($dependantArr, $dependantQ[$i]->dependant_id);
}
$appointmentCount = Appointment::whereIn('patient_id',$dependantArr)->count();
$healthRecordCount = HealthRecords::whereIn('user_id',$dependantArr)->count();
$appointmentQ = Appointment::whereIn('patient_id',$dependantArr)->where('appointment_status',AppoitmentStatusEnum::COMPLETED())->get();
for ($i=0; $i < count($appointmentQ); $i++) {
array_push($appointmentArr, $appointmentQ[$i]->id);
}
$feesPaid = 0;
if(count($appointmentArr) != false) {
$feesPaid = Payment::whereIn('appointment_id',$appointmentArr)->sum('consultation_fee');
}
$userCompletion = User::find($auth_id)->user_completion;
$upcomingAppointment = Appointment::whereIn('patient_id',$dependantArr)->where(function($q){
$q->where('appointment_status',AppoitmentStatusEnum::ACCEPTED());
$q->where('consultation_end_time',NULL);
$q->where('appointment_start_dt','>=',Carbon::now());
})->orderBy('appointment_end_dt','asc')->first();
if (! empty($upcomingAppointment))
{
$upcomingAppointmentDetail = new AppointmentDetailResource($upcomingAppointment);
}
else
{
$upcomingAppointmentDetail = (object) [];
}
$result = [
'upcoming_appointment' => $upcomingAppointmentDetail,
'total_appointments' => $appointmentCount,
'fees_paid' => $feesPaid,
'total_health_records' => $healthRecordCount,
'user_completion' => $userCompletion,
];
return self::sentResponse(200, $result, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
/**
* @param Request $request
* @return object
**/
public function patientDashboardPrescriptions(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$dependantArr = [$auth_id];
$dependantQ = DependantAccountHolder::where('user_id', $auth_id)->where('status', 1)->get();
for ($i=0; $i < count($dependantQ); $i++) {
array_push($dependantArr, $dependantQ[$i]->dependant_id);
}
$list = Prescription::with(['appointment','prescriptionDetails'])->whereHas('appointment', function($q){
$q->where('appointment_status',AppoitmentStatusEnum::COMPLETED());
$q->where('consultation_end_time','!=',NULL);
})->where(function($q) use($dependantArr){
$q->where('status', '1');
$q->whereIn('patient_id', $dependantArr);
});
if(!empty($request->doctor_id)) {
/**
* @var string|null $doctor_id
*/
$doctor_id = $request->doctor_id;
$list = $list->whereHas('appointment', function($q) use($doctor_id){
$q->where('doctor_id',$doctor_id);
});
} else {
$list = $list->limit(3);
}
$list = $list->orderBy('id','desc')->get();
return new PrescriptionCollection($list, 200);
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
/**
* @param Request $request
* @return string|null
**/
public function patientPrescribedDoctors(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$dependantArr = [$auth_id];
$dependantQ = DependantAccountHolder::where('user_id', $auth_id)->where('status', 1)->get();
for ($i=0; $i < count($dependantQ); $i++) {
array_push($dependantArr, $dependantQ[$i]->dependant_id);
}
$result = User::with(['userDetail','consultationPrice'])->whereHas('myDoctorAppointmentList', function($q) use($dependantArr){
$q->whereIn('patient_id', $dependantArr);
})->get();
return self::sentResponse(200, $result, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
/**
* @param Request $request
* @return string|null
**/
public function patientDashboardDoctors(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$result = User::select('users.*','cp.price','cp.currency_code','cp.type AS consultation_id')->role('doctor')
->leftJoin('consultation_prices AS cp', 'users.id', '=', 'cp.user_id')
->with(['userDetail.UserAreaSpecialization','sumSub'])->where('status','2')->orderBy('id','desc')->limit(5)->groupBy('users.id')->get();
return self::sentResponse(200, $result, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
/**
* @param Request $request
* @return string|null
**/
public function doctorDashboardPatients(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$result = User::role('patient')->where('status','2')->whereHas('myAppointmentPatient',function($q) use($auth_id){
$q->where('doctor_id', $auth_id);
})->orderBy('id','desc')->limit(3)->groupBy('users.id')->get();
return self::sentResponse(200, $result, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
/**
* @param Request $request
* @return string|null
**/
public function doctorIncome(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$appointmentArr = [];
$totalAppointmentArr = [];
$appointmentQ = Appointment::where(function($q) use($auth_id){
$q->where('doctor_id',$auth_id);
$q->where('appointment_status',AppoitmentStatusEnum::COMPLETED());
});
$totalAppointmentQ = $appointmentQ->get();
for ($i=0; $i < count($totalAppointmentQ); $i++) {
array_push($totalAppointmentArr, $totalAppointmentQ[$i]->id);
}
if(!empty($request->type))
{
$checkDate = '';
if($request->type == 'week') {
$checkDate = Carbon::now()->subDays(7)->format('Y-m-d');
}
if($request->type == 'month') {
$checkDate = Carbon::now()->subMonth()->format('Y-m-d');
}
$appointmentQ = $appointmentQ->whereDate('appointment_end_dt','>=',$checkDate);;
}
$appointmentQ = $appointmentQ->get();
for ($i=0; $i < count($appointmentQ); $i++) {
array_push($appointmentArr, $appointmentQ[$i]->id);
}
$income = 0;
$total = 0;
if(count($appointmentArr) != false) {
$income = Payment::whereIn('appointment_id',$appointmentArr)->sum('consultation_fee');
}
if(count($totalAppointmentArr) != false) {
$total = Payment::whereIn('appointment_id',$totalAppointmentArr)->sum('consultation_fee');
}
$result = [
'income' => $income,
'total' => $total
];
return self::sentResponse(200, $result, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
public function pharmacyDashboard(Request $request)
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$pharmacy_id = $request->pharmacy_id ? [$request->pharmacy_id] : [];
if(empty($pharmacy_id)) {
$pharmacies = $authRole->pharmacy();
foreach($pharmacies->get() as $row){
/**
* @var object $row
*/
array_push($pharmacy_id,$row->id);
}
}
// pending request count
$requestList = PrescriptionRequest::where(function($q) use($pharmacy_id) {
$q->whereIn('pharmacy_id',$pharmacy_id);
$q->where('status','1');
})->count();
// dispensed count
$dispensedList = PrescriptionRequest::where(function($q) use($pharmacy_id) {
$q->whereIn('pharmacy_id',$pharmacy_id);
$q->where('status','3');
})->count();
//awaiting collection count
$awaitingList = PrescriptionRequest::where(function($q) use($pharmacy_id) {
$q->whereIn('pharmacy_id',$pharmacy_id);
$q->where('status','2');
})->count();
//total count
$totalList = PrescriptionRequest::where(function($q) use($pharmacy_id) {
$q->whereIn('pharmacy_id',$pharmacy_id);
})->count();
$result = [
'pending_request' => $requestList,
'dispensed' => $dispensedList,
'awaiting_collections' => $awaitingList,
'total_list' => $totalList
];
return self::sentResponse(200, $result, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
public function userPharmacyList()
{
try {
$authRole = Auth::user();
$auth_id = auth()->user()->id;
$list = UserPharmacy::with(['pharmacyDetail'])->where('user_id',$auth_id)->get();
return self::sentResponse(200, $list, __('digimed_validation.success_response.data_fetch_success'));
} catch(Exception | Throwable | QueryException $e) {
return self::sentResponse(500, [], $e->getMessage());
}
}
}