HomeController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Http\Controllers;
  3. use DateTime;
  4. use App\Models\Client;
  5. use App\Models\Bill;
  6. use App\Models\Note;
  7. use App\Models\ProTransaction;
  8. use Illuminate\Http\Request;
  9. class HomeController extends Controller
  10. {
  11. public function dashboard(Request $request)
  12. {
  13. //patients where performer is the mcp
  14. $performer = $this->performer();
  15. $performerProID = $performer->pro->id;
  16. $keyNumbers = [];
  17. $totalPatients = Client::where('mcp_pro_id', $performer->pro->id)->count();
  18. $keyNumbers['totalPatients'] = $totalPatients;
  19. $patientNotSeenYet = Client::where('mcp_pro_id', $performer->pro->id)
  20. ->where(function ($query) {
  21. $query->where('has_mcp_done_onboarding_visit', 'UNKNOWN')
  22. ->orWhere('has_mcp_done_onboarding_visit', 'NO');
  23. })->count();
  24. $keyNumbers['patientsNotSeenYet'] = $patientNotSeenYet;
  25. $pendingBillsToSign = Bill::where(function ($query) use ($performerProID) {
  26. $query->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false)->where('is_cancelled', false);
  27. })
  28. ->orWhere(function ($query) use ($performerProID) {
  29. $query->where('cm_pro_id', $performerProID)->where('is_signed_by_cm', false)->where('is_cancelled', false);;
  30. })->orWhere(function ($query) use ($performerProID) {
  31. $query->where('rme_pro_id', $performerProID)->where('is_signed_by_rme', false)->where('is_cancelled', false);;
  32. })->orWhere(function ($query) use ($performerProID) {
  33. $query->where('rmm_pro_id', $performerProID)->where('is_signed_by_rmm', false)->where('is_cancelled', false);;
  34. })->count();
  35. $keyNumbers['pendingBillsToSign'] = $pendingBillsToSign;
  36. $pendingNotesToSign = Note::where(function ($query) use ($performerProID) {
  37. $query->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false)->where('is_cancelled', false);;
  38. })
  39. ->orWhere(function ($query) use ($performerProID) {
  40. $query->where('ally_pro_id', $performerProID)->where('is_signed_by_ally', false)->where('is_cancelled', false);;
  41. })->count();
  42. $keyNumbers['pendingNotesToSign'] = $pendingNotesToSign;
  43. $reimbursement = [];
  44. $reimbursement["currentBalance"] = $performer->pro->balance;
  45. $reimbursement["nextPaymentDate"] = '--';
  46. $lastPayment = ProTransaction::where('pro_id', $performerProID)->where('plus_or_minus', 'PLUS')->orderBy('created_at', 'DESC')->first();
  47. if ($lastPayment) {
  48. $reimbursement["lastPayment"] = $lastPayment->amount;
  49. $reimbursement["lastPaymentDate"] = $lastPayment->created_at;
  50. } else {
  51. $reimbursement["lastPayment"] = '--';
  52. $reimbursement["lastPaymentDate"] = '--';
  53. }
  54. //if today is < 15th, next payment is 15th, else nextPayment is
  55. $today = strtotime(date('Y-m-d'));
  56. $todayDate = date('j', $today);
  57. $todayMonth = date('m', $today);
  58. $todayYear = date('Y', $today);
  59. if($todayDate < 15){
  60. $nextPaymentDate = new DateTime();
  61. $nextPaymentDate->setDate($todayYear, $todayMonth, 15);
  62. $reimbursement['nextPaymentDate'] = $nextPaymentDate->format('m/d/Y');
  63. }else{
  64. $nextPaymentDate = new \DateTime();
  65. $lastDayOfMonth = date('t', $today);
  66. $nextPaymentDate->setDate($todayYear, $todayMonth, $lastDayOfMonth);
  67. $reimbursement['nextPaymentDate'] = $nextPaymentDate->format('m/d/Y');;
  68. }
  69. $clientsWithAppointments = Client::where("mcp_pro_id", $performerProID)
  70. ->whereNotNull('next_mcp_appointment')->get();
  71. $appointments = [];
  72. foreach ($clientsWithAppointments as $client) {
  73. $appointment = [
  74. 'client_uid' => $client->uid,
  75. 'title' => $client->name_first . ' ' . $client->name_last,
  76. 'start' => $client->next_mcp_appointment
  77. ];
  78. $appointments[] = $appointment;
  79. }
  80. return view('app/dashboard', compact('keyNumbers', 'reimbursement', 'appointments'));
  81. }
  82. public function patients(Request $request)
  83. {
  84. $proID = $this->performer()->pro->id;
  85. $patients = Client::where(function ($q) use($proID) {
  86. $q->where('mcp_pro_id', $proID)
  87. ->orWhere('cm_pro_id', $proID)
  88. ->orWhere('rmm_pro_id', $proID)
  89. ->orWhere('rme_pro_id', $proID)
  90. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID]);
  91. })
  92. ->orderBy('name_last', 'asc')
  93. ->orderBy('name_first', 'asc')
  94. ->get();
  95. return view('app/patients', ['patients' => $patients]);
  96. }
  97. public function newPatient(Request $request)
  98. {
  99. return view('app/new-patient');
  100. }
  101. public function mc(Request $request, $fragment = "")
  102. {
  103. $page = "/";
  104. if ($fragment) {
  105. $page = '/' . $fragment;
  106. }
  107. return view('app/mc', compact('page'));
  108. }
  109. }