PracticeManagementController.php 90 KB

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