PracticeManagementController.php 65 KB

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