HomeController.php 15 KB

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