PracticeManagementController.php 88 KB

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