PracticeManagementController.php 82 KB

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