PracticeManagementController.php 94 KB

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