PracticeManagementController.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\BillingReport;
  5. use App\Models\ClaimEDI;
  6. use App\Models\Handout;
  7. use App\Models\MBClaim;
  8. use App\Models\Measurement;
  9. use App\Models\Bill;
  10. use App\Models\Claim;
  11. use App\Models\Client;
  12. use App\Models\McpRequest;
  13. use App\Models\Note;
  14. use App\Models\Pack;
  15. use App\Models\Pro;
  16. use App\Models\Product;
  17. use App\Models\ProFavorite;
  18. use App\Models\ProGeneralAvailability;
  19. use App\Models\ProProAccess;
  20. use App\Models\ProRate;
  21. use App\Models\ProSpecificAvailability;
  22. use App\Models\ProSpecificUnavailability;
  23. use App\Models\ProTextShortcut;
  24. use App\Models\ProTransaction;
  25. use App\Models\Shipment;
  26. use App\Models\SupplyOrder;
  27. use App\Models\Ticket;
  28. use App\Models\ClientMeasurementDaysPerMonth;
  29. use Illuminate\Support\Facades\DB;
  30. use Illuminate\Support\Facades\Http;
  31. use PDF;
  32. use DateTime;
  33. use DateTimeZone;
  34. use Illuminate\Http\Request;
  35. class PracticeManagementController extends Controller
  36. {
  37. public function remoteMonitoringReport(Request $request)
  38. {
  39. $rows = null;
  40. $proID = $this->performer()->pro->id;
  41. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  42. $rows = $isAdmin ? ClientMeasurementDaysPerMonth::all() : ClientMeasurementDaysPerMonth::where('mcp_pro_id', $proID)->orderBy('year_month', 'asc')->orderBy('num_of_days_with_measurement', 'asc')->get();
  43. return view ('app.practice-management.remote-monitoring-report', compact('rows'));
  44. }
  45. public function billingReport(Request $request)
  46. {
  47. $rows = BillingReport::paginate(50);
  48. return view('app.practice-management.billing-report', compact('rows'));
  49. }
  50. public function dashboard(Request $request)
  51. {
  52. return view('app.practice-management.dashboard');
  53. }
  54. public function rates(Request $request, $selectedProUid = 'all')
  55. {
  56. $proUid = $selectedProUid ? $selectedProUid : 'all';
  57. $rates = ProRate::where('is_active', true);
  58. if ($proUid !== 'all') {
  59. $selectedPro = Pro::where('uid', $proUid)->first();
  60. $rates = $rates->where('pro_id', $selectedPro->id);
  61. }
  62. $rates = $rates->orderBy('pro_id', 'asc')->get();
  63. $pros = $this->pros;
  64. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  65. }
  66. public function previousBills(Request $request)
  67. {
  68. return view('app.practice-management.previous-bills');
  69. }
  70. public function financialTransactions(Request $request)
  71. {
  72. $transactions = ProTransaction::where('pro_id', $this->performer()->pro->id)->orderBy('created_at', 'desc')->get();
  73. return view('app.practice-management.financial-transactions', compact('transactions'));
  74. }
  75. public function pendingBillsToSign(Request $request)
  76. {
  77. return view('app.practice-management.pending-bills-to-sign');
  78. }
  79. public function HR(Request $request)
  80. {
  81. return view('app.practice-management.hr');
  82. }
  83. public function directDepositSettings(Request $request)
  84. {
  85. return view('app.practice-management.direct-deposit-settings');
  86. }
  87. public function w9(Request $request)
  88. {
  89. return view('app.practice-management.w9');
  90. }
  91. public function contract(Request $request)
  92. {
  93. return view('app.practice-management.contract');
  94. }
  95. public function notes(Request $request, $filter = '')
  96. {
  97. $proID = $this->performer()->pro->id;
  98. $query = Note::where('hcp_pro_id', $proID);
  99. switch ($filter) {
  100. case 'not-yet-signed':
  101. $query = $query->where('is_signed_by_hcp', false);
  102. break;
  103. case 'without-bills':
  104. $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereDoesntHave('bills');
  105. break;
  106. // more cases can be added as needed
  107. default:
  108. break;
  109. }
  110. $notes = $query->orderBy('created_at', 'desc')->get();
  111. return view('app.practice-management.notes', compact('notes', 'filter'));
  112. }
  113. public function bills(Request $request, $filter = '')
  114. {
  115. $proID = $this->performer()->pro->id;
  116. $query = Bill::where('is_cancelled', false);
  117. switch ($filter) {
  118. case 'not-yet-signed':
  119. $query = $query
  120. ->where(function ($q) use ($proID) {
  121. $q->where(function ($q2) use ($proID) {
  122. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  123. })
  124. ->orWhere(function ($q2) use ($proID) {
  125. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  126. })
  127. ->orWhere(function ($q2) use ($proID) {
  128. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  129. })
  130. ->orWhere(function ($q2) use ($proID) {
  131. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  132. });
  133. });
  134. break;
  135. case 'previous':
  136. $query = $query
  137. ->where(function ($q) use ($proID) {
  138. $q->where(function ($q2) use ($proID) {
  139. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  140. })
  141. ->orWhere(function ($q2) use ($proID) {
  142. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  143. })
  144. ->orWhere(function ($q2) use ($proID) {
  145. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  146. })
  147. ->orWhere(function ($q2) use ($proID) {
  148. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  149. });
  150. });
  151. break;
  152. // more cases can be added as needed
  153. default:
  154. break;
  155. }
  156. $bills = $query->orderBy('created_at', 'desc')->get();
  157. return view('app.practice-management.bills', compact('bills', 'filter'));
  158. }
  159. public function unacknowledgedCancelledBills(Request $request)
  160. {
  161. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  162. ->where('is_cancelled', true)
  163. ->where('is_cancellation_acknowledged', false)
  164. ->orderBy('created_at', 'desc')
  165. ->get();
  166. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  167. }
  168. public function myTickets(Request $request, $filter = 'open')
  169. {
  170. $performer = $this->performer();
  171. $myTickets = Ticket::where(function ($q) use ($performer) {
  172. $q->where('assigned_pro_id', $performer->pro_id)
  173. ->orWhere('manager_pro_id', $performer->pro_id)
  174. ->orWhere('ordering_pro_id', $performer->pro_id)
  175. ->orWhere('initiating_pro_id', $performer->pro_id);
  176. });
  177. if ($filter === 'open') {
  178. $myTickets = $myTickets->where('is_open', true);
  179. } else if ($filter === 'closed') {
  180. $myTickets = $myTickets->where('is_open', false);
  181. }
  182. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  183. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  184. }
  185. public function myTextShortcuts(Request $request)
  186. {
  187. $myTextShortcuts = DB::table('pro_text_shortcut')
  188. ->leftJoin('pro', 'pro_text_shortcut.pro_id', '=', 'pro.id')
  189. ->select(
  190. 'pro_text_shortcut.uid',
  191. 'pro_text_shortcut.shortcut',
  192. 'pro_text_shortcut.text',
  193. 'pro.name_first',
  194. 'pro.name_last'
  195. )
  196. ->where('pro_text_shortcut.is_removed', false);
  197. if($this->performer()->pro->pro_type !== 'ADMIN') {
  198. $myTextShortcuts = $myTextShortcuts->where('pro_id', $this->performer()->pro_id);
  199. }
  200. $myTextShortcuts = $myTextShortcuts
  201. ->orderBy('pro.name_last')
  202. ->orderBy('pro.name_first')
  203. ->get();
  204. return view('app.practice-management.my-text-shortcuts', compact('myTextShortcuts'));
  205. }
  206. public function myFavorites(Request $request, $filter = 'all')
  207. {
  208. $performer = $this->performer();
  209. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  210. ->where('is_removed', false);
  211. if ($filter !== 'all') {
  212. $myFavorites = $myFavorites->where('category', $filter);
  213. }
  214. $myFavorites = $myFavorites
  215. ->orderBy('category', 'asc')
  216. ->orderBy('position_index', 'asc')
  217. ->get();
  218. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  219. }
  220. public function proAvailability(Request $request, $proUid = null)
  221. {
  222. $performer = $this->performer();
  223. $pro = $performer->pro;
  224. if ($proUid) {
  225. $pro = Pro::where('uid', $proUid)->first();
  226. }
  227. if ($request->get('pro_uid')) {
  228. $proUid = $request->get('pro_uid');
  229. $pro = Pro::where('uid', $proUid)->first();
  230. }
  231. $selectedProUid = $pro->uid;
  232. $pros = $this->pros;
  233. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  234. $generalAvailabilities = [
  235. 'MONDAY' => [],
  236. 'TUESDAY' => [],
  237. 'WEDNESDAY' => [],
  238. 'THURSDAY' => [],
  239. 'FRIDAY' => [],
  240. 'SATURDAY' => [],
  241. 'SUNDAY' => [],
  242. ];
  243. foreach ($generalAvailabilitiesList as $ga) {
  244. if ($ga->day_of_week == 'MONDAY') {
  245. $generalAvailabilities['MONDAY'][] = $ga;
  246. }
  247. if ($ga->day_of_week == 'TUESDAY') {
  248. $generalAvailabilities['TUESDAY'][] = $ga;
  249. }
  250. if ($ga->day_of_week == 'WEDNESDAY') {
  251. $generalAvailabilities['WEDNESDAY'][] = $ga;
  252. }
  253. if ($ga->day_of_week == 'THURSDAY') {
  254. $generalAvailabilities['THURSDAY'][] = $ga;
  255. }
  256. if ($ga->day_of_week == 'FRIDAY') {
  257. $generalAvailabilities['FRIDAY'][] = $ga;
  258. }
  259. if ($ga->day_of_week == 'SATURDAY') {
  260. $generalAvailabilities['SATURDAY'][] = $ga;
  261. }
  262. if ($ga->day_of_week == 'SUNDAY') {
  263. $generalAvailabilities['SUNDAY'][] = $ga;
  264. }
  265. }
  266. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  267. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  268. //events for the calendar
  269. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  270. $endDateTime = new DateTime($startDate);
  271. $endDateTime->modify('+6 day');
  272. $endDate = $endDateTime->format("Y-m-d");
  273. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  274. $events = json_encode($eventsData);
  275. return view(
  276. 'app.practice-management.pro-availability',
  277. compact(
  278. 'pros',
  279. 'generalAvailabilities',
  280. 'specificAvailabilities',
  281. 'specificUnavailabilities',
  282. 'events',
  283. 'selectedProUid'
  284. )
  285. );
  286. }
  287. public function loadAvailability(Request $request, $proUid)
  288. {
  289. $performer = $this->performer();
  290. $pro = $performer->pro;
  291. $startDate = $request->get('start');
  292. $endDate = $request->get('end');
  293. $selectedPro = Pro::where('uid', $proUid)->first();
  294. return $selectedPro->getAvailabilityEvents($startDate, $endDate);
  295. }
  296. public function proAvailabilityFilter(Request $request)
  297. {
  298. $proUid = $request->get('proUid');
  299. return ['success' => true, 'data' => $proUid];
  300. }
  301. // video call page (RHS)
  302. // generic call handle (no uid)
  303. // specific call handle (uid of client)
  304. public function meet(Request $request, $uid = false)
  305. {
  306. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  307. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  308. if (!empty($client)) {
  309. return view('app.video.call-minimal', compact('session', 'client'));
  310. }
  311. return view('app.video.call-agora-v2', compact('session', 'client'));
  312. }
  313. // check video page
  314. public function checkVideo(Request $request, $uid)
  315. {
  316. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  317. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  318. $publish = false;
  319. return view('app.video.check-video-minimal', compact('session', 'client'));
  320. }
  321. public function getParticipantInfo(Request $request)
  322. {
  323. $sid = intval($request->get('uid')) - 1000000;
  324. $session = AppSession::where('id', $sid)->first();
  325. $result = [
  326. "type" => '',
  327. "name" => ''
  328. ];
  329. if ($session) {
  330. $result["type"] = $session->session_type;
  331. switch ($session->session_type) {
  332. case 'PRO':
  333. $pro = Pro::where('id', $session->pro_id)->first();
  334. $result["name"] = $pro->displayName();
  335. break;
  336. case 'CLIENT':
  337. $client = Client::where('id', $session->client_id)->first();
  338. $result["name"] = $client->displayName();
  339. break;
  340. }
  341. }
  342. return json_encode($result);
  343. }
  344. // ajax ep used by the video page
  345. // this is needed bcoz meet() is used not
  346. // just for the client passed to the view
  347. public function getOpentokSessionKey(Request $request, $uid)
  348. {
  349. $client = Client::where('uid', $uid)->first();
  350. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  351. }
  352. // poll to check if there are patients with active mcp requests
  353. public function getPatientsInQueue(Request $request)
  354. {
  355. $myInitiatives = $this->performer->pro->initiatives;
  356. if ($myInitiatives) {
  357. $myInitiatives = strtoupper($myInitiatives);
  358. }
  359. $myInitiativesList = explode('|', $myInitiatives);
  360. $myForeignLanguages = $this->performer->pro->foreign_languages;
  361. if ($myForeignLanguages) {
  362. $myForeignLanguages = strtoupper($myForeignLanguages);
  363. }
  364. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  365. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  366. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  367. })
  368. ->where(function ($query) use ($myForeignLanguagesList) {
  369. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  370. })->limit(3)->get();
  371. $results = [];
  372. foreach ($clients as $client) {
  373. $results[] = [
  374. 'clientUid' => $client->uid,
  375. 'name' => $client->displayName(),
  376. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  377. ];
  378. }
  379. return json_encode($results);
  380. }
  381. public function currentWork(Request $request)
  382. {
  383. return view('app/current-work');
  384. }
  385. public function calendar(Request $request, $proUid = null)
  386. {
  387. $pros = Pro::all();
  388. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  389. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  390. $accessibleProIds = [];
  391. foreach ($accessiblePros as $accessiblePro) {
  392. $accessibleProIds[] = $accessiblePro->id;
  393. }
  394. $accessibleProIds[] = $this->pro->id;
  395. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  396. }
  397. return view('app.practice-management.calendar', compact('pros'));
  398. }
  399. public function cellularDeviceManager(Request $request, $proUid = null)
  400. {
  401. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  402. $performerPro = $this->performer->pro;
  403. $targetPro = null;
  404. $allPros = [];
  405. $expectedForHcp = null;
  406. if ($performerPro->pro_type == 'ADMIN') {
  407. $allPros = Pro::all();
  408. $targetPro = Pro::where('uid', $proUid)->first();
  409. } else {
  410. $targetPro = $performerPro;
  411. }
  412. $clients = [];
  413. if ($targetPro) {
  414. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  415. } else {
  416. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  417. }
  418. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  419. }
  420. public function treatmentServiceUtil(Request $request)
  421. {
  422. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  423. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  424. $view_treatment_service_utilization_by_patient = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_by_patient ORDER BY pro_lname ASC, pro_fname ASC, hcp_pro_id ASC, total_hrs DESC"));
  425. return view('app.practice-management.treatment-services-util', compact(
  426. 'view_treatment_service_utilization_org',
  427. 'view_treatment_service_utilization',
  428. 'view_treatment_service_utilization_by_patient'));
  429. }
  430. public function processingBillMatrix(Request $request, $proUid = null)
  431. {
  432. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  433. $performerPro = $this->performer->pro;
  434. $targetPro = null;
  435. $allPros = [];
  436. if ($performerPro->pro_type == 'ADMIN') {
  437. $allPros = Pro::all();
  438. $targetPro = Pro::where('uid', $proUid)->first();
  439. } else {
  440. $targetPro = $performerPro;
  441. }
  442. $bills = [];
  443. if ($targetPro) {
  444. $bills = Bill::where('hcp_pro_id', $targetPro->id)->
  445. where('has_hcp_been_paid', false)->
  446. where('is_cancelled', false)->
  447. where('is_signed_by_hcp', true)->
  448. orderBy('effective_date', 'desc')->paginate();
  449. } else {
  450. $bills = Bill::where('has_hcp_been_paid', false)->
  451. where('is_cancelled', false)->
  452. where('is_signed_by_hcp', true)->
  453. orderBy('effective_date', 'desc')->
  454. paginate();
  455. }
  456. $viewData = [
  457. 'bills' => $bills,
  458. 'allPros' => $allPros,
  459. 'targetPro' => $targetPro,
  460. 'performerPro' => $performerPro,
  461. 'proUid' => $proUid
  462. ];
  463. return view('app.practice-management.processing-bill-matrix', $viewData);
  464. }
  465. public function hcpBillMatrix(Request $request, $proUid = null)
  466. {
  467. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  468. $performerPro = $this->performer->pro;
  469. $targetPro = null;
  470. $allPros = [];
  471. $expectedForHcp = null;
  472. if ($performerPro->pro_type == 'ADMIN') {
  473. $allPros = Pro::all();
  474. $targetPro = Pro::where('uid', $proUid)->first();
  475. } else {
  476. $targetPro = $performerPro;
  477. }
  478. $rows = [];
  479. if ($targetPro) {
  480. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  481. } else {
  482. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  483. }
  484. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  485. }
  486. public function billingManager(Request $request, $proUid = null)
  487. {
  488. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  489. $performerPro = $this->performer->pro;
  490. $targetPro = null;
  491. $allPros = [];
  492. $expectedForHcp = null;
  493. if ($performerPro->pro_type == 'ADMIN') {
  494. $allPros = Pro::all();
  495. $targetPro = Pro::where('uid', $proUid)->first();
  496. } else {
  497. $targetPro = $performerPro;
  498. }
  499. $notes = [];
  500. if ($targetPro) {
  501. $expectedForHcp = DB::select(DB::raw("SELECT coalesce(SUM(hcp_expected_payment_amount),0) as expected_pay FROM bill WHERE hcp_pro_id = :targetProID AND is_signed_by_hcp IS TRUE AND is_cancelled = false"), ['targetProID' => $targetPro->id])[0]->expected_pay;
  502. $notes = Note::where('hcp_pro_id', $targetPro->id);
  503. } else {
  504. $notes = Note::where('id', '>', 0);
  505. }
  506. if($request->input('date')) {
  507. $notes = $notes->where('effective_dateest', $request->input('date'));
  508. }
  509. $filters = [];
  510. $filters['bills_created'] = $request->input('bills_created');
  511. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  512. $filters['bills_resolved'] = $request->input('bills_resolved');
  513. $filters['bills_closed'] = $request->input('bills_closed');
  514. $filters['claims_created'] = $request->input('claims_created');
  515. $filters['claims_closed'] = $request->input('claims_closed');
  516. if ($filters['bills_created']) {
  517. $notes->where(
  518. 'bill_total_expected',
  519. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  520. 0);
  521. }
  522. if ($filters['is_billing_marked_done']) {
  523. $notes->where(
  524. 'is_billing_marked_done',
  525. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  526. true);
  527. }
  528. if ($filters['bills_resolved']) {
  529. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  530. if ($filters['bills_resolved'] === 'yes') {
  531. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id AND (is_cancelled = false AND is_verified = false) OR (is_cancelled = TRUE AND is_cancellation_acknowledged = FALSE)) > 0');
  532. } elseif ($filters['bills_resolved'] === 'no') {
  533. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id AND ((is_cancelled = true AND is_cancellation_acknowledged = true) OR is_verified = true)) = 0');
  534. }
  535. }
  536. if ($filters['bills_closed']) {
  537. $notes->where(
  538. 'is_bill_closed',
  539. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  540. true);
  541. }
  542. if ($filters['claims_created']) {
  543. $notes->where(
  544. 'claim_total_expected',
  545. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  546. 0);
  547. }
  548. if ($filters['claims_closed']) {
  549. $notes->where(
  550. 'is_claim_closed',
  551. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  552. true);
  553. }
  554. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(10);
  555. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  556. }
  557. public function billMatrix(Request $request)
  558. {
  559. $bClients = [];
  560. $bHCPPros = [];
  561. $bNAPros = [];
  562. $filters = [];
  563. $filters['client'] = $request->input('client');
  564. $filters['service'] = $request->input('service');
  565. $filters['hcp'] = $request->input('hcp');
  566. $filters['hcp_paid'] = $request->input('hcp_paid');
  567. $filters['expected_op'] = $request->input('expected_op');
  568. $filters['expected_value'] = $request->input('expected_value');
  569. $filters['paid_op'] = $request->input('paid_op');
  570. $filters['paid_value'] = $request->input('paid_value');
  571. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  572. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  573. $filters['hcp_sign'] = $request->input('hcp_sign');
  574. $filters['verified'] = $request->input('verified');
  575. $filters['cancelled'] = $request->input('cancelled');
  576. $bills = Bill::orderBy('effective_date')->paginate();
  577. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  578. }
  579. public function medicarePartBClaims(Request $request)
  580. {
  581. $medicarePartBOnly = $request->get("medicare_part_b");
  582. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  583. //Only medicare claims
  584. $claims = [];
  585. foreach ($allClaims as $claim) {
  586. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  587. $claims[] = $claim;
  588. }
  589. }
  590. $claimEDIs = ClaimEDI::all();
  591. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  592. }
  593. // Generate PDF
  594. public function downloadClaims()
  595. {
  596. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  597. view()->share('claims', $claims);
  598. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  599. return $pdf->download('pdf_file.pdf');
  600. }
  601. public function tickets(Request $request, $proUid = null)
  602. {
  603. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  604. return view('app.practice-management.tickets', compact('tickets'));
  605. }
  606. public function supplyOrders(Request $request)
  607. {
  608. // counts
  609. $counts = $this->getSupplyOrderCounts();
  610. // so clients
  611. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  612. $soClientIDs = array_map(function ($_x) {
  613. return $_x->client_id;
  614. }, $soClientIDs);
  615. $soClients = Client::whereIn('id', $soClientIDs)->get();
  616. // so products
  617. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  618. $soProductIDs = array_map(function ($_x) {
  619. return $_x->product_id;
  620. }, $soProductIDs);
  621. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  622. $filters = [];
  623. $filters['client'] = $request->input('client');
  624. $filters['product'] = $request->input('product');
  625. $filters['reason'] = $request->input('reason');
  626. $filters['cu_memo'] = $request->input('cu_memo');
  627. $filters['pro_sign'] = $request->input('pro_sign');
  628. $filters['client_sign'] = $request->input('client_sign');
  629. $filters['shipment'] = $request->input('shipment');
  630. $filters['lot_number'] = $request->input('lot_number');
  631. $filters['imei'] = $request->input('imei');
  632. $filters['cancelled'] = $request->input('cancelled');
  633. $supplyOrders = SupplyOrder::where('id', '>', 0);
  634. // apply filters
  635. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  636. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  637. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  638. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  639. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  640. if ($filters['client_sign']) {
  641. if ($filters['client_sign'] === 'signed')
  642. $supplyOrders->where('is_signed_by_client', true);
  643. elseif ($filters['client_sign'] === 'waived')
  644. $supplyOrders->where('is_client_signature_waived', true);
  645. else
  646. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  647. }
  648. if ($filters['shipment']) {
  649. if ($filters['shipment'] === 'not_cleared_for_shipment')
  650. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  651. elseif ($filters['shipment'] === 'cleared_for_shipment')
  652. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  653. else
  654. $supplyOrders
  655. ->whereNotNull('shipment_id')
  656. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  657. }
  658. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  659. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  660. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  661. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  662. return view('app.practice-management.supply-orders',
  663. compact('supplyOrders', 'filters',
  664. 'soClients', 'soProducts', 'counts'
  665. )
  666. );
  667. }
  668. public function shipments(Request $request, $filter = null)
  669. {
  670. // counts
  671. $counts = $this->getShipmentCounts();
  672. // so clients
  673. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  674. $shClientIDs = array_map(function ($_x) {
  675. return $_x->client_id;
  676. }, $shClientIDs);
  677. $shClients = Client::whereIn('id', $shClientIDs)->get();
  678. $shipments = Shipment::where('id', '>', 0);
  679. $filters = [];
  680. $filters['client'] = $request->input('client');
  681. $filters['courier'] = $request->input('courier');
  682. $filters['tracking_num'] = $request->input('tracking_num');
  683. $filters['label'] = $request->input('label');
  684. $filters['status'] = $request->input('status');
  685. $filters['cancelled'] = $request->input('cancelled');
  686. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  687. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  688. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  689. if ($filters['label']) {
  690. if ($filters['label'] === 'yes')
  691. $shipments->whereNotNull('label_system_file_id');
  692. else
  693. $shipments->whereNull('label_system_file_id');
  694. }
  695. if ($filters['status']) $shipments->where('status', $filters['status']);
  696. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  697. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  698. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  699. }
  700. public function cellularMeasurements(Request $request)
  701. {
  702. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  703. return view('app.practice-management.cellular-measurements', compact('measurements'));
  704. }
  705. // v2 supply-orders & shipments management (wh)
  706. public function supplyOrdersReadyToShip(Request $request)
  707. {
  708. $counts = $this->getSupplyOrderCounts();
  709. $supplyOrders = SupplyOrder
  710. ::where('is_cleared_for_shipment', true)
  711. ->where('is_cancelled', false)
  712. ->whereNull('shipment_id')
  713. ->join('client', 'client.id', '=', 'supply_order.client_id')
  714. ->orderBy('client.name_last', 'ASC')
  715. ->orderBy('client.name_first', 'ASC')
  716. ->orderBy('supply_order.client_id', 'ASC')
  717. ->orderBy('supply_order.mailing_address_full', 'ASC')
  718. ->orderBy('supply_order.created_at', 'ASC')
  719. ->select('supply_order.*')
  720. ->paginate();
  721. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  722. }
  723. public function supplyOrdersShipmentUnderway(Request $request)
  724. {
  725. $counts = $this->getSupplyOrderCounts();
  726. $supplyOrders = SupplyOrder
  727. ::where('is_cancelled', false)
  728. ->whereNotNull('shipment_id')
  729. ->orderBy('client_id', 'ASC')
  730. ->orderBy('mailing_address_full', 'ASC')
  731. ->orderBy('created_at', 'ASC')
  732. ->paginate();
  733. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  734. }
  735. public function supplyOrdersHanging(Request $request)
  736. {
  737. $counts = $this->getSupplyOrderCounts();
  738. $supplyOrders = SupplyOrder
  739. ::select('supply_order.*')
  740. ->leftJoin('shipment', function($join) {
  741. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  742. })
  743. ->where('shipment.status', 'CANCELLED')
  744. ->where('supply_order.is_cancelled', false)
  745. ->orderBy('supply_order.client_id', 'ASC')
  746. ->orderBy('supply_order.mailing_address_full', 'ASC')
  747. ->orderBy('supply_order.created_at', 'ASC')
  748. ->paginate();
  749. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  750. }
  751. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  752. {
  753. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  754. ->where('is_cancelled', true)
  755. ->where('is_cancellation_acknowledged', false)
  756. ->orderBy('created_at', 'desc')
  757. ->paginate();
  758. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  759. }
  760. public function supplyOrdersUnsigned(Request $request)
  761. {
  762. $supplyOrders = SupplyOrder
  763. ::where('is_cancelled', false)
  764. ->where('is_signed_by_pro', false)
  765. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  766. ->orderBy('created_at', 'desc')
  767. ->paginate();
  768. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  769. }
  770. private function getSupplyOrderCounts()
  771. {
  772. return [
  773. "supplyOrders" => SupplyOrder::count(),
  774. "supplyOrdersReadyToShip" => SupplyOrder
  775. ::where('is_cleared_for_shipment', true)
  776. ->where('is_cancelled', false)
  777. ->whereNull('shipment_id')->count(),
  778. "supplyOrdersShipmentUnderway" => SupplyOrder
  779. ::where('is_cancelled', false)
  780. ->whereNotNull('shipment_id')->count(),
  781. "supplyOrdersHanging" => SupplyOrder
  782. ::leftJoin('shipment', function($join) {
  783. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  784. })
  785. ->where('shipment.status', 'CANCELLED')
  786. ->where('supply_order.is_cancelled', false)
  787. ->count(),
  788. ];
  789. }
  790. public function shipmentsReadyToPrint(Request $request)
  791. {
  792. $counts = $this->getShipmentCounts();
  793. $shipments = Shipment
  794. ::where('is_cancelled', false)
  795. ->where('status', 'CREATED')
  796. ->orderBy('created_at', 'ASC')
  797. ->paginate();
  798. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  799. }
  800. public function shipmentsShipmentUnderway(Request $request)
  801. {
  802. $counts = $this->getShipmentCounts();
  803. $shipments = Shipment
  804. ::where('is_cancelled', false)
  805. ->where('status', 'PRINTED')
  806. ->orderBy('created_at', 'ASC')
  807. ->paginate();
  808. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  809. }
  810. private function getShipmentCounts()
  811. {
  812. return [
  813. "shipments" => Shipment::count(),
  814. "shipmentsReadyToPrint" => Shipment
  815. ::where('is_cancelled', false)
  816. ->where('status', 'CREATED')
  817. ->count(),
  818. "shipmentsWaitingForPicker" => Shipment
  819. ::where('is_cancelled', false)
  820. ->where('status', 'PRINTED')
  821. ->count()
  822. ];
  823. }
  824. public function shipment(Request $request, Shipment $shipment)
  825. {
  826. return view('app.practice-management.shipment', compact('shipment'));
  827. }
  828. public function shipmentsMultiPrint(Request $request, $ids)
  829. {
  830. $ids = array_map(function ($_x) {
  831. return intval($_x);
  832. }, explode("|", $ids));
  833. $shipments = Shipment::whereIn('id', $ids)->get();
  834. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  835. }
  836. public function patientClaimSummary(Request $request, $proUid = null)
  837. {
  838. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  839. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  840. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  841. $notes3rdPartyTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  842. $notes3rdPartyTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND n.is_bill_closed IS TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  843. $notes3rdPartyTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note n LEFT JOIN client c ON n.client_id = c.id WHERE n.is_cancelled IS NOT TRUE AND n.is_claim_closed IS TRUE AND c.is_part_b_primary <> 'YES'"))[0]->count;
  844. $patientsTotal = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  845. $patientsTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) y) AND 0 IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  846. $patientsTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM client WHERE is_active IS TRUE AND 0 NOT IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND note.client_id = client.id) y) AND 0 IN (SELECT c FROM (SELECT COUNT(*) c FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS NOT TRUE AND note.client_id = client.id) x)"))[0]->count;
  847. $performerPro = $this->performer->pro;
  848. $allPros = [];
  849. if ($performerPro->pro_type == 'ADMIN') {
  850. $allPros = Pro::all();
  851. } else {
  852. $allPros = [$performerPro];
  853. }
  854. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  855. $patientsQuery = Client::where('is_dummy', '=', false)
  856. ->whereNull('shadow_pro_id')
  857. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  858. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  859. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  860. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  861. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  862. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  863. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  864. if ($proUid) {
  865. $mcpPro = Pro::where('uid', $proUid)->first();
  866. if ($mcpPro) {
  867. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  868. }
  869. }
  870. $patientsQuery->whereRaw('(SELECT COUNT(*) FROM note where note.client_id = client.id) > 0');
  871. $patientsQuery->orderBy('notes_total', 'DESC');
  872. $patients = $patientsQuery->paginate(50);
  873. $data = [
  874. 'patients' => $patients,
  875. 'proUid' => $proUid,
  876. 'allPros' => $allPros,
  877. 'notesTotal' => $notesTotal,
  878. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  879. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  880. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  881. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  882. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  883. 'patientsTotal' => $patientsTotal,
  884. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  885. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  886. ];
  887. return view('app.practice-management.patient-claim-summary', $data);
  888. }
  889. public function claims(Request $request){
  890. $status = $request->get('status');
  891. $claims = [];
  892. if(!$status){
  893. $claims = Claim::orderBy('created_at', 'DESC')->paginate();
  894. }else{
  895. $claims = Claim::where('status', $status)->orderBy('created_at', 'DESC')->paginate();
  896. }
  897. return view('app.practice-management.claims', compact('claims', 'status'));
  898. }
  899. public function processClaims(Request $request) {
  900. $status = '';
  901. $q = $request->input('q') ? $request->input('q') : '';
  902. $from = $request->input('from') ? $request->input('from') : '1900-01-01';
  903. $to = $request->input('to') ? $request->input('to') : '2100-01-01';
  904. $params = [
  905. 'q' => '%' . $q . '%',
  906. 'from' => $from,
  907. 'to' => $to
  908. ];
  909. $hcpPro = $request->input('hcp') ? Pro::where('uid', $request->input('hcp'))->first() : null;
  910. if($hcpPro) {
  911. $params['hcp'] = $hcpPro->id;
  912. }
  913. $claims = DB::select(DB::raw("
  914. SELECT claim.uid AS uid,
  915. DATE(claim.created_at) AS created,
  916. claim.status,
  917. client.uid AS client_uid,
  918. client.cell_number AS client_phone,
  919. (client.name_last || ' ' || client.name_first) AS client ,
  920. client.chart_number AS client_chart_number,
  921. cp.id AS claim_pro_id,
  922. (cp.name_last || ' ' || cp.name_first) AS claim_pro,
  923. sp.id AS status_pro_id,
  924. (sp.name_last || ' ' || sp.name_first) AS status_pro,
  925. note.uid AS note_uid,
  926. note.method,
  927. note.new_or_fu_or_na,
  928. -- claim.status_updated_at,
  929. (DATE(claim.status_updated_at) || ' ' ||
  930. LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
  931. LPAD(EXTRACT(minute FROM claim.status_updated_at)::text, 2, '0')) AS status_updated_at,
  932. (SELECT string_agg(claim_line.cpt, ', ') FROM claim_line where claim_id = claim.id) AS cpts,
  933. (SELECT COUNT(claim_line_icd.id) FROM claim_line_icd WHERE claim_line_id IN (SELECT id FROM claim_line WHERE claim_id = claim.id)) AS icds,
  934. ROUND(claim.expected_total, 3) as expected_total
  935. FROM claim
  936. join client on claim.client_id = client.id
  937. join pro cp on claim.pro_id = cp.id
  938. left join note on claim.note_id = note.id
  939. left join app_session on claim.status_updated_by_session_id = app_session.id
  940. left join pro sp on app_session.pro_id = sp.id
  941. --WHERE claim.status IS NULL OR claim.status = 'NEW'
  942. WHERE (claim.status is NULL OR claim.status NOT IN ('CANCELLED', 'ABANDONED'))
  943. -- AND claim.current_version_id IS NOT NULL
  944. AND (client.name_first ILIKE :q OR
  945. client.name_last ILIKE :q OR
  946. client.chart_number ILIKE :q OR
  947. client.mcn ILIKE :q)
  948. AND (claim.created_at >= :from AND claim.created_at <= :to)
  949. " . ($hcpPro ? "AND claim.pro_id = :hcp" : '') . "
  950. AND claim.id IN (SELECT mb_claim.claim_id FROM mb_claim)
  951. ORDER BY claim.created_at ASC
  952. --OFFSET 0 LIMIT 15
  953. "), $params);
  954. if($request->input('json')) {
  955. return json_encode($claims);
  956. }
  957. return view('app.practice-management.process-claims', compact('claims', 'status'));
  958. }
  959. public function processNotes(Request $request) {
  960. $mode = $request->input('mode') ? $request->input('mode') : '1';
  961. if($mode < 1 || $mode > 5) $mode = 1;
  962. $counts = [
  963. "picked" => Note::where('is_cancelled', false)
  964. ->whereNotNull('current_note_pickup_for_processing_id')
  965. ->count(),
  966. "mode-1" => Note::where('is_cancelled', false)
  967. ->where('is_signed_by_hcp', false)
  968. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  969. ->whereNull('current_note_pickup_for_processing_id')
  970. ->count(),
  971. "mode-2" => Note::where('is_cancelled', false)
  972. ->where('is_signed_by_hcp', true)
  973. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  974. ->whereNull('current_note_pickup_for_processing_id')
  975. ->where('is_billing_marked_done', false)
  976. ->count(),
  977. "mode-3" => Note::where('is_cancelled', false)
  978. ->where('is_signed_by_hcp', true)
  979. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  980. ->whereNull('current_note_pickup_for_processing_id')
  981. ->where('is_billing_marked_done', true)
  982. ->where('is_bill_closed', false)
  983. ->count(),
  984. "mode-4" => Note::where('is_cancelled', false)
  985. ->where('is_signed_by_hcp', true)
  986. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  987. ->whereNull('current_note_pickup_for_processing_id')
  988. ->where('is_billing_marked_done', true)
  989. ->where('is_bill_closed', true)
  990. ->where('is_claim_closed', false)
  991. ->count(),
  992. "mode-5" => Note::where('is_cancelled', false)
  993. ->where('is_signed_by_hcp', true)
  994. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  995. ->whereNull('current_note_pickup_for_processing_id')
  996. ->where('is_billing_marked_done', true)
  997. ->where('is_bill_closed', true)
  998. ->where('is_claim_closed', true)
  999. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1000. ->count(),
  1001. "mode-6" => Note::where('is_cancelled', false)
  1002. ->where('is_signed_by_hcp', true)
  1003. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1004. ->whereNull('current_note_pickup_for_processing_id')
  1005. ->where('is_billing_marked_done', true)
  1006. ->where('is_bill_closed', true)
  1007. ->where('is_claim_closed', true)
  1008. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1009. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1010. ->count(),
  1011. "mode-7" => Note::where('is_cancelled', false)
  1012. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1013. ->count(),
  1014. ];
  1015. return view('app.practice-management.process-notes', compact('mode', 'counts'));
  1016. }
  1017. public function getNextNote(Request $request, $mode)
  1018. {
  1019. $note = null;
  1020. switch (+$mode) {
  1021. case 1:
  1022. $note = Note::where('is_cancelled', false)
  1023. ->where('is_signed_by_hcp', false)
  1024. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1025. ->whereNull('current_note_pickup_for_processing_id')
  1026. ->orderBy('effective_dateest', 'ASC')
  1027. ->first();
  1028. break;
  1029. case 2:
  1030. $note = Note::where('is_cancelled', false)
  1031. ->where('is_signed_by_hcp', true)
  1032. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1033. ->whereNull('current_note_pickup_for_processing_id')
  1034. ->where('is_billing_marked_done', false)
  1035. ->orderBy('effective_dateest', 'ASC')
  1036. ->first();
  1037. break;
  1038. case 3:
  1039. $note = Note::where('is_cancelled', false)
  1040. ->where('is_signed_by_hcp', true)
  1041. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1042. ->whereNull('current_note_pickup_for_processing_id')
  1043. ->where('is_billing_marked_done', true)
  1044. ->where('is_bill_closed', false)
  1045. ->orderBy('effective_dateest', 'ASC')
  1046. ->first();
  1047. break;
  1048. case 4:
  1049. $note = Note::where('is_cancelled', false)
  1050. ->where('is_signed_by_hcp', true)
  1051. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1052. ->whereNull('current_note_pickup_for_processing_id')
  1053. ->where('is_billing_marked_done', true)
  1054. ->where('is_bill_closed', true)
  1055. ->where('is_claim_closed', false)
  1056. ->orderBy('effective_dateest', 'ASC')
  1057. ->first();
  1058. break;
  1059. case 5:
  1060. $note = Note::where('is_cancelled', false)
  1061. ->where('is_signed_by_hcp', true)
  1062. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1063. ->whereNull('current_note_pickup_for_processing_id')
  1064. ->where('is_billing_marked_done', true)
  1065. ->where('is_bill_closed', true)
  1066. ->where('is_claim_closed', true)
  1067. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1068. ->orderBy('effective_dateest', 'ASC')
  1069. ->first();
  1070. break;
  1071. }
  1072. if($note) {
  1073. $note->client_uid = $note->client->uid;
  1074. }
  1075. return json_encode($note);
  1076. }
  1077. public function pickedNotes(Request $request) {
  1078. $counts = [
  1079. "unpicked" => Note::where('is_cancelled', false)
  1080. ->whereNull('current_note_pickup_for_processing_id')
  1081. ->count(),
  1082. ];
  1083. $notes = Note::where('is_cancelled', false)
  1084. ->whereNotNull('current_note_pickup_for_processing_id')
  1085. ->orderBy('effective_dateest', 'ASC')
  1086. ->paginate();
  1087. return view('app.practice-management.picked-notes', compact('counts', 'notes'));
  1088. }
  1089. public function currentMbClaim(Request $request, $claimUid) {
  1090. $claim = Claim::where('uid', $claimUid)->first();
  1091. return json_encode(MBClaim::where('claim_version_id', $claim->currentVersion->id)->first());
  1092. }
  1093. public function currentClaimLines(Request $request, $claimUid) {
  1094. $claim = Claim::where('uid', $claimUid)->first();
  1095. if($request->input('json')) {
  1096. foreach ($claim->lines as $line) {
  1097. $line->expected_total = round($line->expected_total, 3);
  1098. $x = $line->claimLineIcds;
  1099. }
  1100. return json_encode($claim->lines);
  1101. }
  1102. return view('app.practice-management._claim-lines', compact('claim'));
  1103. }
  1104. public function packsMultiPrint(Request $request) {
  1105. $packs = Pack
  1106. ::select('pack.*')
  1107. ->leftJoin('shipment', function($join) {
  1108. $join->on('pack.shipment_id', '=', 'shipment.id');
  1109. })
  1110. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  1111. ->where(function ($query) {
  1112. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  1113. })
  1114. ->whereNotNull('pack.label_system_file_id')
  1115. ->orderBy('pack.created_at', 'ASC')
  1116. ->get();
  1117. return view('app.practice-management.packs-multi-print', compact('packs'));
  1118. }
  1119. public function packsMultiPDF(Request $request, $ids) {
  1120. $ids = array_map(function ($_x) {
  1121. return intval($_x);
  1122. }, explode("|", $ids));
  1123. $packs = Pack::whereIn('id', $ids)->get();
  1124. }
  1125. public function handouts(Request $request) {
  1126. $handouts = Handout::orderBy('display_name')->get();
  1127. return view('app.practice-management.handouts', compact('handouts'));
  1128. }
  1129. private function callJava($request, $endPoint, $data)
  1130. {
  1131. $url = config('stag.backendUrl') . $endPoint;
  1132. $response = Http::asForm()
  1133. ->withHeaders([
  1134. 'sessionKey' => $request->cookie('sessionKey')
  1135. ])
  1136. ->post($url, $data)
  1137. ->body();
  1138. dd($response);
  1139. return $response;
  1140. }
  1141. }