HomeController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 postSetPassword(Request $request) {
  59. try {
  60. $url = env('BACKEND_URL', 'http://localhost:8080/api') . '/pro/selfPutPassword';
  61. $data = [
  62. 'newPassword' => $request->input('newPassword'),
  63. 'newPasswordConfirmation' => $request->input('newPasswordConfirmation'),
  64. ];
  65. $response = Http::asForm()
  66. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  67. ->post($url, $data)
  68. ->json();
  69. if(!isset($response['success']) || !$response['success']){
  70. $message = 'API error';
  71. if(isset($response['error'])) {
  72. $message = $response['error'];
  73. if(isset($response['path'])) $message .= ': ' . $response['path'];
  74. }
  75. else if(isset($response['message'])) $message = $response['message'];
  76. return redirect('/set_password')
  77. ->withInput()
  78. ->with('message', $message);
  79. }
  80. return redirect('/');
  81. } catch (\Exception $e) {
  82. return redirect()->back()
  83. ->with('message', 'Unable to process your request at the moment. Please try again later.')
  84. ->withInput($request->input());
  85. }
  86. }
  87. public function postSetSecurityQuestions(Request $request) {
  88. try {
  89. $url = env('BACKEND_URL', 'http://localhost:8080/api') . '/pro/selfPutSecurityQuestions';
  90. $data = [
  91. 'securityQuestion1' => $request->input('securityQuestion1'),
  92. 'securityAnswer1' => $request->input('securityAnswer1'),
  93. 'securityQuestion2' => $request->input('securityQuestion2'),
  94. 'securityAnswer2' => $request->input('securityAnswer2'),
  95. ];
  96. $response = Http::asForm()
  97. ->withHeaders(['sessionKey'=>$request->cookie('sessionKey')])
  98. ->post($url, $data)
  99. ->json();
  100. if(!isset($response['success']) || !$response['success']){
  101. $message = 'API error';
  102. if(isset($response['error'])) {
  103. $message = $response['error'];
  104. if(isset($response['path'])) $message .= ': ' . $response['path'];
  105. }
  106. else if(isset($response['message'])) $message = $response['message'];
  107. return redirect('/set_password')
  108. ->withInput()
  109. ->with('message', $message);
  110. }
  111. return redirect('/');
  112. } catch (\Exception $e) {
  113. return redirect()->back()
  114. ->with('message', 'Unable to process your request at the moment. Please try again later.')
  115. ->withInput($request->input());
  116. }
  117. }
  118. public function dashboard(Request $request)
  119. {
  120. //patients where performer is the mcp
  121. $performer = $this->performer();
  122. $performerProID = $performer->pro->id;
  123. $keyNumbers = [];
  124. $totalPatients = Client::where('mcp_pro_id', $performer->pro->id)->count();
  125. $keyNumbers['totalPatients'] = $totalPatients;
  126. $patientNotSeenYet = Client::where('mcp_pro_id', $performer->pro->id)
  127. ->where(function ($query) {
  128. $query->where('has_mcp_done_onboarding_visit', 'UNKNOWN')
  129. ->orWhere('has_mcp_done_onboarding_visit', 'NO');
  130. })->count();
  131. $keyNumbers['patientsNotSeenYet'] = $patientNotSeenYet;
  132. $pendingBillsToSign = Bill::where(function ($query) use ($performerProID) {
  133. $query->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false)->where('is_cancelled', false);
  134. })
  135. ->orWhere(function ($query) use ($performerProID) {
  136. $query->where('cm_pro_id', $performerProID)->where('is_signed_by_cm', false)->where('is_cancelled', false);;
  137. })->orWhere(function ($query) use ($performerProID) {
  138. $query->where('rme_pro_id', $performerProID)->where('is_signed_by_rme', false)->where('is_cancelled', false);;
  139. })->orWhere(function ($query) use ($performerProID) {
  140. $query->where('rmm_pro_id', $performerProID)->where('is_signed_by_rmm', false)->where('is_cancelled', false);;
  141. })->count();
  142. $keyNumbers['pendingBillsToSign'] = $pendingBillsToSign;
  143. $pendingNotesToSign = Note::where(function ($query) use ($performerProID) {
  144. $query->where('hcp_pro_id', $performerProID)->where('is_signed_by_hcp', false)->where('is_cancelled', false);;
  145. })
  146. ->orWhere(function ($query) use ($performerProID) {
  147. $query->where('ally_pro_id', $performerProID)->where('is_signed_by_ally', false)->where('is_cancelled', false);;
  148. })->count();
  149. $keyNumbers['pendingNotesToSign'] = $pendingNotesToSign;
  150. $reimbursement = [];
  151. $reimbursement["currentBalance"] = '$' . $performer->pro->balance;
  152. $reimbursement["nextPaymentDate"] = '--';
  153. $lastPayment = ProTransaction::where('pro_id', $performerProID)->where('plus_or_minus', 'PLUS')->orderBy('created_at', 'DESC')->first();
  154. if ($lastPayment) {
  155. $reimbursement["lastPayment"] = '$' . $lastPayment->amount;
  156. $reimbursement["lastPaymentDate"] = $lastPayment->created_at;
  157. } else {
  158. $reimbursement["lastPayment"] = '--';
  159. $reimbursement["lastPaymentDate"] = '--';
  160. }
  161. //if today is < 15th, next payment is 15th, else nextPayment is
  162. $today = strtotime(date('Y-m-d'));
  163. $todayDate = date('j', $today);
  164. $todayMonth = date('m', $today);
  165. $todayYear = date('Y', $today);
  166. if ($todayDate < 15) {
  167. $nextPaymentDate = new DateTime();
  168. $nextPaymentDate->setDate($todayYear, $todayMonth, 15);
  169. $reimbursement['nextPaymentDate'] = $nextPaymentDate->format('m/d/Y');
  170. } else {
  171. $nextPaymentDate = new \DateTime();
  172. $lastDayOfMonth = date('t', $today);
  173. $nextPaymentDate->setDate($todayYear, $todayMonth, $lastDayOfMonth);
  174. $reimbursement['nextPaymentDate'] = $nextPaymentDate->format('m/d/Y');
  175. }
  176. //expectedPay
  177. $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;
  178. $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;
  179. $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;
  180. $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;
  181. $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;
  182. $totalExpectedAmount = $expectedForHcp + $expectedForCm + $expectedForRme + $expectedForRmm + $expectedForNa;
  183. $reimbursement['nextPaymentAmount'] = '$' . $totalExpectedAmount;
  184. $clientsWithAppointments = Client::where("mcp_pro_id", $performerProID)
  185. ->whereNotNull('next_mcp_appointment')->get();
  186. $appointments = [];
  187. foreach ($clientsWithAppointments as $client) {
  188. $appointment = [
  189. 'client_uid' => $client->uid,
  190. 'title' => $client->name_first . ' ' . $client->name_last,
  191. 'start' => $client->next_mcp_appointment
  192. ];
  193. $appointments[] = $appointment;
  194. }
  195. return view('app/dashboard', compact('keyNumbers', 'reimbursement', 'appointments'));
  196. }
  197. public function patients(Request $request, $filter = '')
  198. {
  199. $proID = $this->performer()->pro->id;
  200. if($this->performer()->pro->pro_type === 'ADMIN') {
  201. $query = Client::where('id', '>', 0);
  202. }
  203. else {
  204. $query = Client::where(function ($q) use($proID) {
  205. $q->where('mcp_pro_id', $proID)
  206. ->orWhere('cm_pro_id', $proID)
  207. ->orWhere('rmm_pro_id', $proID)
  208. ->orWhere('rme_pro_id', $proID)
  209. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID]);
  210. });
  211. }
  212. switch ($filter) {
  213. case 'not-yet-seen':
  214. $query = $query->where('has_mcp_done_onboarding_visit', '<>', 'YES');
  215. break;
  216. // more cases can be added as needed
  217. default:
  218. break;
  219. }
  220. $patients = $query->orderBy('name_last', 'asc')->orderBy('name_first', 'asc')->get();
  221. return view('app/patients', compact('patients', 'filter'));
  222. }
  223. public function patientsSuggest(Request $request) {
  224. $term = $request->input('term') ? trim($request->input('term')) : '';
  225. if(empty($term)) return '';
  226. $clients = Client::where(function ($q) use($term) {
  227. $q->where('name_first', 'ILIKE', '%' . $term . '%')
  228. ->orWhere('name_last', 'ILIKE', '%' . $term . '%');
  229. })->get();
  230. return view('app/patient-suggest', compact('clients'));
  231. }
  232. public function unmappedSMS(Request $request, $filter = '') {
  233. $proID = $this->performer()->pro->id;
  234. if($this->performer()->pro->pro_type === 'ADMIN') {
  235. $query = Client::where('id', '>', 0);
  236. }
  237. else {
  238. $query = Client::where(function ($q) use($proID) {
  239. $q->where('mcp_pro_id', $proID)
  240. ->orWhere('cm_pro_id', $proID)
  241. ->orWhere('rmm_pro_id', $proID)
  242. ->orWhere('rme_pro_id', $proID)
  243. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID]);
  244. });
  245. }
  246. $patients = $query->orderBy('name_last', 'asc')->orderBy('name_first', 'asc')->get();
  247. $unmappedSMS = ClientSMS::where('client_id', null)->where('incoming_or_outgoing', 'INCOMING')->get();
  248. return view('app/unmapped-sms', compact('unmappedSMS', 'patients'));
  249. }
  250. public function newPatient(Request $request)
  251. {
  252. return view('app/new-patient');
  253. }
  254. public function mc(Request $request, $fragment = "")
  255. {
  256. $page = "/";
  257. if ($fragment) {
  258. $page = '/' . $fragment;
  259. }
  260. return view('app/mc', compact('page'));
  261. }
  262. public function blank(Request $request)
  263. {
  264. return view('app/blank');
  265. }
  266. }