PracticeManagementController.php 89 KB

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