McpController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. $notes = Note::paginate(5);
  131. // SELECT * FROM note WHERE client_id IN (SELECT id FROM client WHERE mcp_pro_id = :me.id);
  132. return view('app.mcp.notes', compact('notes'));
  133. }
  134. public function appointments(Request $request)
  135. {
  136. $appointments = Appointment::paginate(5);
  137. return view('app.mcp.appointments', compact('appointments'));
  138. }
  139. public function bills(Request $request)
  140. {
  141. $filters = $request->all();
  142. $bills = Bill::where('hcp_pro_id', $this->performer->pro->id);
  143. $this->filterMultiQuery($request, $bills, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  144. $status = $request->get('status');
  145. if($status){
  146. if($status == 'CANCELLED') $bills = $bills->where('is_cancelled', true);
  147. if($status == 'NOT_CANCELLED') $bills = $bills->where('is_cancelled', false);
  148. }
  149. $bills = $bills->orderBy('created_at', 'DESC')->paginate(20);
  150. return view('app.mcp.bills', compact('bills', 'filters'));
  151. }
  152. public function erx_and_orders(Request $request)
  153. {
  154. $filters = $request->all();
  155. $erxAndOrders = Erx::query();
  156. $erxAndOrders = $erxAndOrders->where('hcp_pro_id', $this->performer->pro->id);
  157. $this->filterMultiQuery($request, $erxAndOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  158. $this->filterSimpleQuery($request, $erxAndOrders, 'pro_declared_status', 'status');
  159. $erxAndOrders = $erxAndOrders->orderBy('created_at', 'DESC')->paginate(20);
  160. return view('app.mcp.erx_and_orders', compact('erxAndOrders', 'filters'));
  161. }
  162. public function reports(Request $request)
  163. {
  164. $data = [];
  165. return view('app.mcp.reports', $data);
  166. }
  167. public function supply_orders(Request $request)
  168. {
  169. $filters = $request->all();
  170. $supplyOrders = SupplyOrder::select('supply_order.*')->where('supply_order.signed_by_pro_id', $this->performer->pro->id);
  171. $this->filterMultiQuery($request, $supplyOrders, 'created_at', 'date_category', 'date_value_1', 'date_value_2');
  172. $status = $request->get('status');
  173. if($status){
  174. if($status == 'CLEARED_FOR_SHIPMENT'){
  175. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', true);
  176. }elseif($status == 'NOT_CLEARED_FOR_SHIPMENT'){
  177. $supplyOrders = $supplyOrders->where('is_cleared_for_shipment', false);
  178. }elseif($status == 'CANCELLED'){
  179. $supplyOrders = $supplyOrders->where('is_cancelled', true);
  180. }else{
  181. $supplyOrders = $supplyOrders->join('shipment', 'shipment.id', '=', 'supply_order.shipment_id')->where('shipment.status', $status);
  182. }
  183. }
  184. $supplyOrders = $supplyOrders->orderBy('created_at', 'DESC')->paginate(20);
  185. return view('app.mcp.supply_orders', compact('supplyOrders', 'filters'));
  186. }
  187. public function client_messages(Request $request)
  188. {
  189. $filters = $request->all();
  190. $clientMessages = ClientSMS::select('client_sms.*')
  191. ->join('client', 'client.id', '=', 'client_sms.client_id')
  192. ->where('client.mcp_pro_id', $this->performer->pro->id);
  193. $this->filterMultiQuery($request, $clientMessages, 'client_sms.created_at', 'date_category', 'date_value_1', 'date_value_2');
  194. $this->filterSimpleQuery($request, $clientMessages, 'sms_status', 'sms_status');
  195. $clientMessages = $clientMessages->orderBy('client_sms.created_at', 'DESC');
  196. $clientMessages = $clientMessages->paginate(20);
  197. return view('app.mcp.client_messages', compact('clientMessages', 'filters'));
  198. }
  199. public function new_patients_awaiting_visit(Request $request){
  200. $data = [
  201. 'records' => Client::where('mcp_pro_id', $this->performer->pro->id)
  202. ->where('has_mcp_done_onboarding_visit', '!=', 'YES')
  203. ->orderBy('created_at')
  204. ->get()
  205. ];
  206. return view('app.mcp.new_patients_awaiting_visit', $data);
  207. }
  208. public function notes_pending_signature(Request $request){
  209. $data = [
  210. 'records' => Note::where('hcp_pro_id', $this->performer->pro->id)
  211. ->where('is_cancelled', '<>', true)
  212. ->where('is_signed_by_hcp', '<>', true)
  213. ->where('is_core_note', false)
  214. ->orderBy('created_at')
  215. ->get()
  216. ];
  217. return view('app.mcp.notes_pending_signature', $data);
  218. }
  219. public function notes_pending_billing(Request $request){
  220. $data = [
  221. 'records' => Note::where('hcp_pro_id', $this->performer->pro->id)
  222. ->where('is_cancelled', '<>', true)
  223. ->where('is_signed_by_hcp', true)
  224. ->where('is_billing_marked_done', '<>', true)
  225. ->orderBy('created_at')
  226. ->get()
  227. ];
  228. return view('app.mcp.notes_pending_billing', $data);
  229. }
  230. public function reports_pending_signature(Request $request){
  231. $data = [
  232. 'records' => IncomingReport::where('hcp_pro_id', $this->performer->pro->id)
  233. ->where('has_hcp_pro_signed', '<>', true)
  234. ->where('is_entry_error', '<>', true)
  235. ->orderBy('created_at')
  236. ->get()
  237. ];
  238. return view('app.mcp.reports_pending_signature', $data);
  239. }
  240. public function patients_without_appointments(Request $request){
  241. $patients = $this->performer->pro->get_patients_without_appointment_query()->paginate(20);
  242. return view('app.mcp.patients_without_appointments', compact('patients'));
  243. }
  244. public function patients_overdue_for_visit(Request $request){
  245. $patients = $this->performer->pro->get_patients_overdue_for_visit_query()->paginate(20);
  246. return view('app.mcp.patients_overdue_for_visit', compact('patients'));
  247. }
  248. public function cancelled_appointments_pending_review(Request $request){
  249. $data = [];
  250. return view('app.mcp.cancelled_appointments_pending_review', $data);
  251. }
  252. public function cancelled_bills_pending_review(Request $request){
  253. $data = [
  254. 'records' => Bill::where('hcp_pro_id', $this->performer->pro->id)
  255. ->where('bill_service_type', 'NOTE')
  256. ->where('is_cancelled', true)
  257. ->where('is_cancellation_acknowledged', '<>', true)
  258. ->orderBy('created_at')
  259. ->get()
  260. ];
  261. return view('app.mcp.cancelled_bills_pending_review', $data);
  262. }
  263. public function cancelled_supply_orders_pending_review(Request $request){
  264. $data = [
  265. 'records' => SupplyOrder::where('signed_by_pro_id', $this->performer->pro->id)
  266. ->where('is_cancelled', true)
  267. ->where('is_cancellation_acknowledged', '<>', true)
  268. ->orderBy('created_at')
  269. ->get()
  270. ];
  271. return view('app.mcp.cancelled_supply_orders_pending_review', $data);
  272. }
  273. public function erx_and_orders_pending_signature(Request $request){
  274. $data = [
  275. 'records' => Erx::where('hcp_pro_id', $this->performer->pro->id)
  276. ->where('pro_declared_status', '<>', 'CANCELLED')
  277. ->where('has_hcp_pro_signed', '<>', true)
  278. ->orderBy('created_at')
  279. ->get()
  280. ];
  281. return view('app.mcp.erx_and_orders_pending_signature', $data);
  282. }
  283. public function supply_orders_pending_signature(Request $request){
  284. $data = [
  285. 'records' => SupplyOrder::where('created_by_pro_id', $this->performer->pro->id)
  286. ->whereNull('signed_by_pro_id')
  287. ->where('is_cancelled', '<>', true)
  288. ->orderBy('created_at')
  289. ->get()
  290. ];
  291. return view('app.mcp.supply_orders_pending_signature', $data);
  292. }
  293. public function measurements_pending_stamping(Request $request){
  294. $data = [
  295. 'records' => CareMonth::where('mcp_pro_id', $this->performer->pro->id)
  296. ->where('rm_num_measurements_not_stamped_by_mcp', '>', 0)
  297. ->whereNotNull('rm_num_measurements_not_stamped_by_mcp')
  298. ->orderBy('created_at', 'DESC')
  299. ->paginate(15)
  300. ];
  301. return view('app.mcp.measurements_pending_stamping', $data);
  302. }
  303. }