PracticeManagementController.php 88 KB

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