HomeController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Lib\Backend;
  4. use App\Models\Appointment;
  5. use App\Models\ClientSMS;
  6. use DateTime;
  7. use App\Models\Client;
  8. use App\Models\Bill;
  9. use App\Models\Note;
  10. use App\Models\ProTransaction;
  11. use GuzzleHttp\Cookie\CookieJar;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Cookie;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Http;
  16. class HomeController extends Controller
  17. {
  18. public function confirmSmsAuthToken(Request $request)
  19. {
  20. return view('app/confirm_sms_auth_token');
  21. }
  22. public function setPassword(Request $request)
  23. {
  24. return view('app/set_password');
  25. }
  26. public function setSecurityQuestions(Request $request)
  27. {
  28. return view('app/set_security_questions');
  29. }
  30. public function postConfirmSmsAuthToken(Request $request) {
  31. try {
  32. $url = env('BACKEND_URL', 'http://localhost:8080/api') . '/session/confirmSmsAuthToken';
  33. $data = [
  34. 'cellNumber' => $request->input('cellNumber'),
  35. 'token' => $request->input('token'),
  36. ];
  37. $response = Http::asForm()
  38. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  39. ->post($url, $data)
  40. ->json();
  41. if(!isset($response['success']) || !$response['success']){
  42. $message = 'API error';
  43. if(isset($response['error'])) {
  44. $message = $response['error'];
  45. if(isset($response['path'])) $message .= ': ' . $response['path'];
  46. }
  47. else if(isset($response['message'])) $message = $response['message'];
  48. return redirect('/confirm_sms_auth_token')
  49. ->withInput()
  50. ->with('message', $message);
  51. }
  52. return redirect('/');
  53. } catch (\Exception $e) {
  54. return redirect()->back()
  55. ->with('message', 'Unable to process your request at the moment. Please try again later.')
  56. ->withInput($request->input());
  57. }
  58. }
  59. public function postSetPassword(Request $request) {
  60. try {
  61. $url = env('BACKEND_URL', 'http://localhost:8080/api') . '/pro/selfPutPassword';
  62. $data = [
  63. 'newPassword' => $request->input('newPassword'),
  64. 'newPasswordConfirmation' => $request->input('newPasswordConfirmation'),
  65. ];
  66. $response = Http::asForm()
  67. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  68. ->post($url, $data)
  69. ->json();
  70. if(!isset($response['success']) || !$response['success']){
  71. $message = 'API error';
  72. if(isset($response['error'])) {
  73. $message = $response['error'];
  74. if(isset($response['path'])) $message .= ': ' . $response['path'];
  75. }
  76. else if(isset($response['message'])) $message = $response['message'];
  77. return redirect('/set_password')
  78. ->withInput()
  79. ->with('message', $message);
  80. }
  81. return redirect('/');
  82. } catch (\Exception $e) {
  83. return redirect()->back()
  84. ->with('message', 'Unable to process your request at the moment. Please try again later.')
  85. ->withInput($request->input());
  86. }
  87. }
  88. public function postSetSecurityQuestions(Request $request) {
  89. try {
  90. $url = env('BACKEND_URL', 'http://localhost:8080/api') . '/pro/selfPutSecurityQuestions';
  91. $data = [
  92. 'securityQuestion1' => $request->input('securityQuestion1'),
  93. 'securityAnswer1' => $request->input('securityAnswer1'),
  94. 'securityQuestion2' => $request->input('securityQuestion2'),
  95. 'securityAnswer2' => $request->input('securityAnswer2'),
  96. ];
  97. $response = Http::asForm()
  98. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  99. ->post($url, $data)
  100. ->json();
  101. if(!isset($response['success']) || !$response['success']){
  102. $message = 'API error';
  103. if(isset($response['error'])) {
  104. $message = $response['error'];
  105. if(isset($response['path'])) $message .= ': ' . $response['path'];
  106. }
  107. else if(isset($response['message'])) $message = $response['message'];
  108. return redirect('/set_password')
  109. ->withInput()
  110. ->with('message', $message);
  111. }
  112. return redirect('/');
  113. } catch (\Exception $e) {
  114. return redirect()->back()
  115. ->with('message', 'Unable to process your request at the moment. Please try again later.')
  116. ->withInput($request->input());
  117. }
  118. }
  119. public function dashboard(Request $request)
  120. {
  121. //patients where performer is the mcp
  122. $performer = $this->performer();
  123. $performerProID = $performer->pro->id;
  124. $keyNumbers = [];
  125. $totalPatients = Client::where('mcp_pro_id', $performer->pro->id)->count();
  126. $keyNumbers['totalPatients'] = $totalPatients;
  127. $patientNotSeenYet = Client::where('mcp_pro_id', $performer->pro->id)
  128. ->where(function ($query) {
  129. $query->where('has_mcp_done_onboarding_visit', 'UNKNOWN')
  130. ->orWhere('has_mcp_done_onboarding_visit', 'NO');
  131. })->count();
  132. $keyNumbers['patientsNotSeenYet'] = $patientNotSeenYet;
  133. $pendingBillsToSign = Bill::where(function ($query) use ($performerProID) {
  134. $query->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false)->where('is_cancelled', false);
  135. })
  136. ->orWhere(function ($query) use ($performerProID) {
  137. $query->where('cm_pro_id', $performerProID)->where('is_signed_by_cm', false)->where('is_cancelled', false);;
  138. })->orWhere(function ($query) use ($performerProID) {
  139. $query->where('rme_pro_id', $performerProID)->where('is_signed_by_rme', false)->where('is_cancelled', false);;
  140. })->orWhere(function ($query) use ($performerProID) {
  141. $query->where('rmm_pro_id', $performerProID)->where('is_signed_by_rmm', false)->where('is_cancelled', false);;
  142. })->count();
  143. $keyNumbers['pendingBillsToSign'] = $pendingBillsToSign;
  144. $pendingNotesToSign = Note::where(function ($query) use ($performerProID) {
  145. $query->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false)->where('is_cancelled', false);;
  146. })
  147. ->orWhere(function ($query) use ($performerProID) {
  148. $query->where('ally_pro_id', $performerProID)->where('is_signed_by_ally', false)->where('is_cancelled', false);;
  149. })->count();
  150. $keyNumbers['pendingNotesToSign'] = $pendingNotesToSign;
  151. $reimbursement = [];
  152. $reimbursement["currentBalance"] = '$' . $performer->pro->balance;
  153. $reimbursement["nextPaymentDate"] = '--';
  154. $lastPayment = ProTransaction::where('pro_id', $performerProID)->where('plus_or_minus', 'PLUS')->orderBy('created_at', 'DESC')->first();
  155. if ($lastPayment) {
  156. $reimbursement["lastPayment"] = '$' . $lastPayment->amount;
  157. $reimbursement["lastPaymentDate"] = $lastPayment->created_at;
  158. } else {
  159. $reimbursement["lastPayment"] = '--';
  160. $reimbursement["lastPaymentDate"] = '--';
  161. }
  162. //if today is < 15th, next payment is 15th, else nextPayment is
  163. $today = strtotime(date('Y-m-d'));
  164. $todayDate = date('j', $today);
  165. $todayMonth = date('m', $today);
  166. $todayYear = date('Y', $today);
  167. if ($todayDate < 15) {
  168. $nextPaymentDate = new DateTime();
  169. $nextPaymentDate->setDate($todayYear, $todayMonth, 15);
  170. $reimbursement['nextPaymentDate'] = $nextPaymentDate->format('m/d/Y');
  171. } else {
  172. $nextPaymentDate = new \DateTime();
  173. $lastDayOfMonth = date('t', $today);
  174. $nextPaymentDate->setDate($todayYear, $todayMonth, $lastDayOfMonth);
  175. $reimbursement['nextPaymentDate'] = $nextPaymentDate->format('m/d/Y');
  176. }
  177. //expectedPay
  178. $expectedForHcp = DB::select(DB::raw("SELECT coalesce(SUM(hcp_expected_payment_amount),0) as expected_pay FROM bill WHERE hcp_pro_id = :performerProID AND has_hcp_been_paid = false AND is_cancelled = false"), ['performerProID' => $performerProID])[0]->expected_pay;
  179. $expectedForCm = DB::select(DB::raw("SELECT coalesce(SUM(cm_expected_payment_amount),0) as expected_pay FROM bill WHERE cm_pro_id = :performerProID AND has_cm_been_paid = false AND is_cancelled = false"), ['performerProID' => $performerProID])[0]->expected_pay;
  180. $expectedForRme = DB::select(DB::raw("SELECT coalesce(SUM(rme_expected_payment_amount),0) as expected_pay FROM bill WHERE rme_pro_id = :performerProID AND has_rme_been_paid = false AND is_cancelled = false"), ['performerProID' => $performerProID])[0]->expected_pay;
  181. $expectedForRmm = DB::select(DB::raw("SELECT coalesce(SUM(rmm_expected_payment_amount),0) as expected_pay FROM bill WHERE rmm_pro_id = :performerProID AND has_rmm_been_paid = false AND is_cancelled = false"), ['performerProID' => $performerProID])[0]->expected_pay;
  182. $expectedForNa = DB::select(DB::raw("SELECT coalesce(SUM(na_expected_payment_amount),0) as expected_pay FROM bill WHERE na_pro_id = :performerProID AND has_na_been_paid = false AND is_cancelled = false"), ['performerProID' => $performerProID])[0]->expected_pay;
  183. $totalExpectedAmount = $expectedForHcp + $expectedForCm + $expectedForRme + $expectedForRmm + $expectedForNa;
  184. $reimbursement['nextPaymentAmount'] = '$' . $totalExpectedAmount;
  185. $appointments = Appointment::where("pro_id", $performerProID)
  186. ->orderBy('start_time', 'asc')
  187. ->get();
  188. foreach ($appointments as $appointment) {
  189. $date = explode(" ", $appointment->start_time)[0];
  190. $appointment->milliseconds = strtotime($date) . '000';
  191. $appointment->dateYMD = date('Y-m-d', strtotime($appointment->start_time));
  192. $appointment->clientName = $appointment->client->displayName();
  193. $appointment->clientInitials = substr($appointment->client->name_first, 0, 1).substr($appointment->client->name_last, 0, 1);
  194. $appointment->friendlyStartTime = friendly_time($appointment->start_time);
  195. $appointment->friendlyEndTime = friendly_time($appointment->end_time);
  196. $appointment->clientSummary = friendly_date_time($appointment->client->dob, false) . ' (' .
  197. $appointment->client->age_in_years . ' y.o' .
  198. ($appointment->client->sex ? ' ' . $appointment->client->sex : '') .
  199. ')';
  200. }
  201. $milliseconds = strtotime(date('Y-m-d')) . '000';
  202. return view('app/dashboard', compact('keyNumbers', 'reimbursement', 'appointments', 'milliseconds'));
  203. }
  204. public function patients(Request $request, $filter = '')
  205. {
  206. $proID = $this->performer()->pro->id;
  207. if($this->performer()->pro->pro_type === 'ADMIN') {
  208. $query = Client::where('id', '>', 0);
  209. }
  210. else {
  211. $query = Client::where(function ($q) use($proID) {
  212. $q->where('mcp_pro_id', $proID)
  213. ->orWhere('cm_pro_id', $proID)
  214. ->orWhere('rmm_pro_id', $proID)
  215. ->orWhere('rme_pro_id', $proID)
  216. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID]);
  217. });
  218. }
  219. switch ($filter) {
  220. case 'not-yet-seen':
  221. $query = $query->where('has_mcp_done_onboarding_visit', '<>', 'YES');
  222. break;
  223. // more cases can be added as needed
  224. default:
  225. break;
  226. }
  227. $patients = $query->orderBy('name_last', 'asc')->orderBy('name_first', 'asc')->get();
  228. return view('app/patients', compact('patients', 'filter'));
  229. }
  230. public function patientsSuggest(Request $request) {
  231. $term = $request->input('term') ? trim($request->input('term')) : '';
  232. if(empty($term)) return '';
  233. $clients = Client::where(function ($q) use($term) {
  234. $q->where('name_first', 'ILIKE', '%' . $term . '%')
  235. ->orWhere('name_last', 'ILIKE', '%' . $term . '%');
  236. })->get();
  237. return view('app/patient-suggest', compact('clients'));
  238. }
  239. public function unmappedSMS(Request $request, $filter = '') {
  240. $proID = $this->performer()->pro->id;
  241. if($this->performer()->pro->pro_type === 'ADMIN') {
  242. $query = Client::where('id', '>', 0);
  243. }
  244. else {
  245. $query = Client::where(function ($q) use($proID) {
  246. $q->where('mcp_pro_id', $proID)
  247. ->orWhere('cm_pro_id', $proID)
  248. ->orWhere('rmm_pro_id', $proID)
  249. ->orWhere('rme_pro_id', $proID)
  250. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID]);
  251. });
  252. }
  253. $patients = $query->orderBy('name_last', 'asc')->orderBy('name_first', 'asc')->get();
  254. $unmappedSMS = ClientSMS::where('client_id', null)->where('incoming_or_outgoing', 'INCOMING')->get();
  255. return view('app/unmapped-sms', compact('unmappedSMS', 'patients'));
  256. }
  257. public function newPatient(Request $request)
  258. {
  259. return view('app/new-patient');
  260. }
  261. public function mc(Request $request, $fragment = "")
  262. {
  263. $page = "/";
  264. if ($fragment) {
  265. $page = '/' . $fragment;
  266. }
  267. return view('app/mc', compact('page'));
  268. }
  269. public function blank(Request $request)
  270. {
  271. return view('app/blank');
  272. }
  273. }