McpController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Appointment;
  4. use App\Models\BDTDevice;
  5. use App\Models\CareMonth;
  6. use App\Models\Client;
  7. use App\Models\ClientBDTDevice;
  8. use App\Models\ClientInfoLine;
  9. use App\Models\Erx;
  10. use App\Models\Facility;
  11. use App\Models\Handout;
  12. use App\Models\IncomingReport;
  13. use App\Models\MBClaim;
  14. use App\Models\MBPayer;
  15. use App\Models\Note;
  16. use App\Models\NoteTemplate;
  17. use App\Models\Pro;
  18. use App\Models\Product;
  19. use App\Models\ProProAccess;
  20. use App\Models\SectionTemplate;
  21. use App\Models\Shipment;
  22. use App\Models\SupplyOrder;
  23. use App\Models\Ticket;
  24. use Illuminate\Http\Request;
  25. use Illuminate\Support\Facades\DB;
  26. use Illuminate\Support\Facades\File;
  27. use App\Models\Bill;
  28. use App\Models\ClientSMS;
  29. use Illuminate\Support\Facades\Http;
  30. use PDF;
  31. class McpController extends Controller
  32. {
  33. public function patients(Request $request)
  34. {
  35. $filters = $request->all();
  36. $patients = Client::whereNull('shadow_pro_id')->where('mcp_pro_id', $this->performer->pro->id);
  37. // filters
  38. /*
  39. array:18 [▼
  40. "age_category" => "LESS_THAN"
  41. "age_value_1" => "34"
  42. "age_value_2" => null
  43. "sex" => "M"
  44. "bmi_category" => "BETWEEN"
  45. "bmi_value_1" => "20"
  46. "bmi_value_2" => "25"
  47. "last_visit_category" => "LESS_THAN"
  48. "last_visit_value_1" => "2021-10-14"
  49. "last_visit_value_2" => null
  50. "next_appointment_category" => "LESS_THAN"
  51. "next_appointment_value_1" => "2021-10-15"
  52. "status" => "ACTIVE"
  53. "last_weighed_in_category" => "EXACTLY"
  54. "last_weighed_in_value_1" => "2021-10-07"
  55. "last_bp_category" => "BETWEEN"
  56. "last_bp_value_1" => "2021-10-01"
  57. "last_bp_value_2" => "2021-10-31"
  58. ]
  59. */
  60. if ($request->input('name')) {
  61. $name = trim($request->input('name'));
  62. if ($name) {
  63. $patients = $patients->where(function ($q) use ($name) {
  64. $q->where('name_first', 'ILIKE', '%' . $name . '%')
  65. ->orWhere('name_last', 'ILIKE', '%' . $name . '%');
  66. });
  67. }
  68. }
  69. $this->filterMultiQuery($request, $patients, 'age_in_years', 'age_category', 'age_value_1', 'age_value_2');
  70. $this->filterSimpleQuery($request, $patients, 'sex', 'sex');
  71. $this->filterMultiQuery($request, $patients, 'usual_bmi', 'bmi_category', 'bmi_value_1', 'bmi_value_2');
  72. $this->filterMultiQuery($request, $patients, 'most_recent_weight_at', 'last_weighed_in_category', 'last_weighed_in_value_1', 'last_weighed_in_value_2');
  73. $this->filterMultiQuery($request, $patients, 'most_recent_bp_at', 'last_bp_category', 'last_bp_value_1', 'last_bp_value_2');
  74. switch($request->input('status')) {
  75. case 'ACTIVE':
  76. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', true);
  77. break;
  78. case 'AWAITING_VISIT':
  79. $patients->where('is_active', true)->where('has_mcp_done_onboarding_visit', false);
  80. break;
  81. case 'INACTIVE':
  82. $patients->where('is_active', '<>', true);
  83. break;
  84. }
  85. $patients = $patients->orderBy('created_at', 'DESC')->paginate(20);
  86. return view('app.mcp.patients', compact('patients', 'filters'));
  87. }
  88. private function filterSimpleQuery(Request $request, $query, $columnName, $valueName) {
  89. if($request->input($valueName)) {
  90. $query->where($columnName, $request->input($valueName));
  91. }
  92. }
  93. private function filterMultiQuery(Request $request, $query, $columnName, $keyName, $valueName1, $valueName2) {
  94. switch($request->input($keyName)) {
  95. case 'EXACTLY':
  96. if($request->input($valueName1)) {
  97. $query->where($columnName, $request->input($valueName1));
  98. }
  99. break;
  100. case 'LESS_THAN':
  101. if($request->input($valueName1)) {
  102. $query->where($columnName, '<', $request->input($valueName1));
  103. }
  104. break;
  105. case 'GREATER_THAN':
  106. if($request->input($valueName1)) {
  107. $query->where($columnName, '>', $request->input($valueName1));
  108. }
  109. break;
  110. case 'BETWEEN':
  111. if($request->input($valueName1) && $request->input($valueName2)) {
  112. $query
  113. ->where($columnName, '>=', $request->input($valueName1))
  114. ->where($columnName, '<=', $request->input($valueName2));
  115. }
  116. break;
  117. case 'NOT_BETWEEN':
  118. if($request->input($valueName1) && $request->input($valueName2)) {
  119. $query
  120. ->where(function ($q) use ($request, $columnName, $valueName1, $valueName2) {
  121. $q->where($columnName, '<', $request->input($valueName1))
  122. ->orWhere($columnName, '>', $request->input($valueName2));
  123. });
  124. }
  125. break;
  126. }
  127. }
  128. public function notes(Request $request)
  129. {
  130. $filters = $request->all();
  131. $notes = Note::query();
  132. $notes = $notes->where('hcp_pro_id', $this->performer->pro->id);
  133. $this->filterMultiQuery($request, $notes, 'effective_time', 'date_category', 'date_value_1', 'date_value_2');
  134. $this->filterSimpleQuery($request, $notes, 'new_or_fu_or_na', 'new_or_fu_or_na');
  135. $notes = $notes->orderBy('created_at', 'DESC')->paginate(20);
  136. return view('app.mcp.notes', compact('notes','filters'));
  137. }
  138. public function appointments(Request $request)
  139. {
  140. $filters = $request->all();
  141. $appointments = Appointment::where('pro_id', $this->performer->pro->id);
  142. $this->filterMultiQuery($request, $appointments, 'raw_date', 'date_category', 'date_value_1', 'date_value_2');
  143. $this->filterSimpleQuery($request, $appointments, 'status', 'status');
  144. $appointments = $appointments->orderBy('end_time', 'DESC')->paginate(20);
  145. return view('app.mcp.appointments', compact('appointments', 'filters'));
  146. }
  147. public function bills(Request $request)
  148. {
  149. $filters = $request->all();
  150. $bills = Bill::where('hcp_pro_id', $this->performer->pro->id);
  151. $this->filterMultiQuery($request, $bills, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  152. $status = $request->get('status');
  153. if($status){
  154. if($status == 'CANCELLED') $bills = $bills->where('is_cancelled', true);
  155. if($status == 'NOT_CANCELLED') $bills = $bills->where('is_cancelled', false);
  156. }
  157. $bills = $bills->orderBy('created_at', 'DESC')->paginate(20);
  158. return view('app.mcp.bills', compact('bills', 'filters'));
  159. }
  160. public function erx_and_orders(Request $request)
  161. {
  162. $filters = $request->all();
  163. $erxAndOrders = Erx::query();
  164. $erxAndOrders = $erxAndOrders->where('hcp_pro_id', $this->performer->pro->id);
  165. $this->filterMultiQuery($request, $erxAndOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  166. $this->filterSimpleQuery($request, $erxAndOrders, 'pro_declared_status', 'status');
  167. $erxAndOrders = $erxAndOrders->orderBy('created_at', 'DESC')->paginate(20);
  168. return view('app.mcp.erx_and_orders', compact('erxAndOrders', 'filters'));
  169. }
  170. public function reports(Request $request)
  171. {
  172. $filters = $request->all();
  173. $reports = IncomingReport::where('hcp_pro_id', $this->performer->pro->id);
  174. $this->filterMultiQuery($request, $reports, 'report_date', 'date_category', 'date_value_1', 'date_value_2');
  175. $status = $request->get('status');
  176. if($status){
  177. if($status == 'SIGNED') $reports = $reports->where('has_hcp_pro_signed', true);
  178. if($status == 'NOT_SIGNED') $reports = $reports->where('has_hcp_pro_signed', false);
  179. }
  180. $reports = $reports->orderBy('created_at', 'DESC')->paginate(20);
  181. return view('app.mcp.reports', compact('reports', 'filters'));
  182. }
  183. public function supply_orders(Request $request)
  184. {
  185. $filters = $request->all();
  186. $supplyOrders = SupplyOrder::select('supply_order.*')->where('supply_order.signed_by_pro_id', $this->performer->pro->id);
  187. $this->filterMultiQuery($request, $supplyOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  188. $status = $request->get('status');
  189. if($status){
  190. if($status == 'CLEARED_FOR_SHIPMENT'){
  191. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', true);
  192. }elseif($status == 'NOT_CLEARED_FOR_SHIPMENT'){
  193. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', false);
  194. }elseif($status == 'CANCELLED'){
  195. $supplyOrders = $supplyOrders->where('is_cancelled', true);
  196. }else{
  197. $supplyOrders = $supplyOrders->join('shipment', 'shipment.id', '=', 'supply_order.shipment_id')->where('shipment.status', $status);
  198. }
  199. }
  200. $supplyOrders = $supplyOrders->orderBy('created_at', 'DESC')->paginate(20);
  201. return view('app.mcp.supply_orders', compact('supplyOrders', 'filters'));
  202. }
  203. public function client_messages(Request $request)
  204. {
  205. $filters = $request->all();
  206. $clientMessages = ClientSMS::select('client_sms.*')
  207. ->join('client', 'client.id', '=', 'client_sms.client_id')
  208. ->where('client.mcp_pro_id', $this->performer->pro->id);
  209. $this->filterMultiQuery($request, $clientMessages, 'client_sms.created_at', 'date_category', 'date_value_1', 'date_value_2');
  210. $this->filterSimpleQuery($request, $clientMessages, 'sms_status', 'sms_status');
  211. $clientMessages = $clientMessages->orderBy('client_sms.created_at', 'DESC');
  212. $clientMessages = $clientMessages->paginate(20);
  213. return view('app.mcp.client_messages', compact('clientMessages', 'filters'));
  214. }
  215. public function new_patients_awaiting_visit(Request $request){
  216. $data = [
  217. 'records' => Client::where('mcp_pro_id', $this->performer->pro->id)
  218. ->where('has_mcp_done_onboarding_visit', '!=', 'YES')
  219. ->orderBy('created_at')
  220. ->get()
  221. ];
  222. return view('app.mcp.new_patients_awaiting_visit', $data);
  223. }
  224. public function notes_pending_signature(Request $request){
  225. $data = [
  226. 'records' => Note::where('hcp_pro_id', $this->performer->pro->id)
  227. ->where('is_cancelled', '<>', true)
  228. ->where('is_signed_by_hcp', '<>', true)
  229. ->where('is_core_note', false)
  230. ->orderBy('created_at')
  231. ->get()
  232. ];
  233. return view('app.mcp.notes_pending_signature', $data);
  234. }
  235. public function notes_pending_billing(Request $request){
  236. $data = [
  237. 'records' => Note::where('hcp_pro_id', $this->performer->pro->id)
  238. ->where('is_cancelled', '<>', true)
  239. ->where('is_signed_by_hcp', true)
  240. ->where('is_billing_marked_done', '<>', true)
  241. ->orderBy('created_at')
  242. ->get()
  243. ];
  244. return view('app.mcp.notes_pending_billing', $data);
  245. }
  246. public function reports_pending_signature(Request $request){
  247. $data = [
  248. 'records' => IncomingReport::where('hcp_pro_id', $this->performer->pro->id)
  249. ->where('has_hcp_pro_signed', '<>', true)
  250. ->where('is_entry_error', '<>', true)
  251. ->orderBy('created_at')
  252. ->get()
  253. ];
  254. return view('app.mcp.reports_pending_signature', $data);
  255. }
  256. public function patients_without_appointments(Request $request){
  257. $patients = $this->performer->pro->get_patients_without_appointment_query()->paginate(20);
  258. return view('app.mcp.patients_without_appointments', compact('patients'));
  259. }
  260. public function patients_overdue_for_visit(Request $request){
  261. $patients = $this->performer->pro->get_patients_overdue_for_visit_query()->paginate(20);
  262. return view('app.mcp.patients_overdue_for_visit', compact('patients'));
  263. }
  264. public function cancelled_appointments_pending_review(Request $request){
  265. $data = [];
  266. return view('app.mcp.cancelled_appointments_pending_review', $data);
  267. }
  268. public function cancelled_bills_pending_review(Request $request){
  269. $data = [
  270. 'records' => Bill::where('hcp_pro_id', $this->performer->pro->id)
  271. ->where('bill_service_type', 'NOTE')
  272. ->where('is_cancelled', true)
  273. ->where('is_cancellation_acknowledged', '<>', true)
  274. ->orderBy('created_at')
  275. ->get()
  276. ];
  277. return view('app.mcp.cancelled_bills_pending_review', $data);
  278. }
  279. public function cancelled_supply_orders_pending_review(Request $request){
  280. $data = [
  281. 'records' => SupplyOrder::where('signed_by_pro_id', $this->performer->pro->id)
  282. ->where('is_cancelled', true)
  283. ->where('is_cancellation_acknowledged', '<>', true)
  284. ->orderBy('created_at')
  285. ->get()
  286. ];
  287. return view('app.mcp.cancelled_supply_orders_pending_review', $data);
  288. }
  289. public function erx_and_orders_pending_signature(Request $request){
  290. $data = [
  291. 'records' => Erx::where('hcp_pro_id', $this->performer->pro->id)
  292. ->where('pro_declared_status', '<>', 'CANCELLED')
  293. ->where('has_hcp_pro_signed', '<>', true)
  294. ->orderBy('created_at')
  295. ->get()
  296. ];
  297. return view('app.mcp.erx_and_orders_pending_signature', $data);
  298. }
  299. public function supply_orders_pending_signature(Request $request){
  300. $data = [
  301. 'records' => SupplyOrder::where('created_by_pro_id', $this->performer->pro->id)
  302. ->whereNull('signed_by_pro_id')
  303. ->where('is_cancelled', '<>', true)
  304. ->orderBy('created_at')
  305. ->get()
  306. ];
  307. return view('app.mcp.supply_orders_pending_signature', $data);
  308. }
  309. public function measurements_pending_stamping(Request $request){
  310. $data = [
  311. 'records' => CareMonth::where('mcp_pro_id', $this->performer->pro->id)
  312. ->where('rm_num_measurements_not_stamped_by_mcp', '>', 0)
  313. ->whereNotNull('rm_num_measurements_not_stamped_by_mcp')
  314. ->orderBy('created_at', 'DESC')
  315. ->paginate(15)
  316. ];
  317. return view('app.mcp.measurements_pending_stamping', $data);
  318. }
  319. }