PracticeManagementController.php 72 KB

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