PracticeManagementController.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\AppSession;
  4. use App\Models\BillingReport;
  5. use App\Models\ClaimEDI;
  6. use App\Models\Handout;
  7. use App\Models\MBClaim;
  8. use App\Models\Measurement;
  9. use App\Models\Bill;
  10. use App\Models\Claim;
  11. use App\Models\Client;
  12. use App\Models\McpRequest;
  13. use App\Models\Note;
  14. use App\Models\Pack;
  15. use App\Models\Pro;
  16. use App\Models\Product;
  17. use App\Models\ProFavorite;
  18. use App\Models\ProGeneralAvailability;
  19. use App\Models\ProProAccess;
  20. use App\Models\ProRate;
  21. use App\Models\ProSpecificAvailability;
  22. use App\Models\ProSpecificUnavailability;
  23. use App\Models\ProTextShortcut;
  24. use App\Models\ProTransaction;
  25. use App\Models\Shipment;
  26. use App\Models\SupplyOrder;
  27. use App\Models\Ticket;
  28. use App\Models\ClientMeasurementDaysPerMonth;
  29. use Illuminate\Support\Facades\DB;
  30. use Illuminate\Support\Facades\Http;
  31. use PDF;
  32. use DateTime;
  33. use DateTimeZone;
  34. use Illuminate\Http\Request;
  35. class PracticeManagementController extends Controller
  36. {
  37. public function remoteMonitoringReport(Request $request)
  38. {
  39. $rows = null;
  40. $proID = $this->performer()->pro->id;
  41. $isAdmin = $this->performer()->pro->pro_type == 'ADMIN';
  42. $rows = $isAdmin ? ClientMeasurementDaysPerMonth::all() : ClientMeasurementDaysPerMonth::where('mcp_pro_id', $proID)->orderBy('year_month', 'asc')->orderBy('num_of_days_with_measurement', 'asc')->get();
  43. return view ('app.practice-management.remote-monitoring-report', compact('rows'));
  44. }
  45. public function billingReport(Request $request)
  46. {
  47. $rows = BillingReport::paginate(50);
  48. return view('app.practice-management.billing-report', compact('rows'));
  49. }
  50. public function dashboard(Request $request)
  51. {
  52. return view('app.practice-management.dashboard');
  53. }
  54. public function rates(Request $request, $selectedProUid = 'all')
  55. {
  56. $proUid = $selectedProUid ? $selectedProUid : 'all';
  57. $rates = ProRate::where('is_active', true);
  58. if ($proUid !== 'all') {
  59. $selectedPro = Pro::where('uid', $proUid)->first();
  60. $rates = $rates->where('pro_id', $selectedPro->id);
  61. }
  62. $rates = $rates->orderBy('pro_id', 'asc')->get();
  63. $pros = $this->pros;
  64. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  65. }
  66. public function previousBills(Request $request)
  67. {
  68. return view('app.practice-management.previous-bills');
  69. }
  70. public function financialTransactions(Request $request)
  71. {
  72. $transactions = ProTransaction::where('pro_id', $this->performer()->pro->id)->orderBy('created_at', 'desc')->get();
  73. return view('app.practice-management.financial-transactions', compact('transactions'));
  74. }
  75. public function pendingBillsToSign(Request $request)
  76. {
  77. return view('app.practice-management.pending-bills-to-sign');
  78. }
  79. public function HR(Request $request)
  80. {
  81. return view('app.practice-management.hr');
  82. }
  83. public function directDepositSettings(Request $request)
  84. {
  85. return view('app.practice-management.direct-deposit-settings');
  86. }
  87. public function w9(Request $request)
  88. {
  89. return view('app.practice-management.w9');
  90. }
  91. public function contract(Request $request)
  92. {
  93. return view('app.practice-management.contract');
  94. }
  95. public function notes(Request $request, $filter = '')
  96. {
  97. $proID = $this->performer()->pro->id;
  98. $query = Note::where('hcp_pro_id', $proID);
  99. switch ($filter) {
  100. case 'not-yet-signed':
  101. $query = $query->where('is_signed_by_hcp', false);
  102. break;
  103. case 'without-bills':
  104. $query = $query->where('is_signed_by_hcp', true)->where('is_cancelled', false)->whereDoesntHave('bills');
  105. break;
  106. // more cases can be added as needed
  107. default:
  108. break;
  109. }
  110. $notes = $query->orderBy('created_at', 'desc')->get();
  111. return view('app.practice-management.notes', compact('notes', 'filter'));
  112. }
  113. public function naBillableSignedNotes(Request $request)
  114. {
  115. $notes = Note
  116. ::where('is_signed_by_hcp', TRUE)
  117. ->where('ally_pro_id', $this->performer()->pro->id)
  118. ->where('is_cancelled', FALSE)
  119. ->whereRaw("
  120. (
  121. SELECT count(bill.id)
  122. FROM bill WHERE
  123. bill.is_cancelled = FALSE AND
  124. bill.generic_pro_id = {$this->performer()->pro->id} AND
  125. bill.note_id = note.id
  126. ) = 0
  127. ");
  128. $notes = $notes->orderBy('created_at', 'desc')->get();
  129. return view('app.practice-management.na-billable-signed-notes', compact('notes'));
  130. }
  131. public function bills(Request $request, $filter = '')
  132. {
  133. $proID = $this->performer()->pro->id;
  134. $query = Bill::where('is_cancelled', false);
  135. switch ($filter) {
  136. case 'not-yet-signed':
  137. $query = $query
  138. ->where(function ($q) use ($proID) {
  139. $q->where(function ($q2) use ($proID) {
  140. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  141. })
  142. ->orWhere(function ($q2) use ($proID) {
  143. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  144. })
  145. ->orWhere(function ($q2) use ($proID) {
  146. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  147. })
  148. ->orWhere(function ($q2) use ($proID) {
  149. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  150. });
  151. });
  152. break;
  153. case 'previous':
  154. $query = $query
  155. ->where(function ($q) use ($proID) {
  156. $q->where(function ($q2) use ($proID) {
  157. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  158. })
  159. ->orWhere(function ($q2) use ($proID) {
  160. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  161. })
  162. ->orWhere(function ($q2) use ($proID) {
  163. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  164. })
  165. ->orWhere(function ($q2) use ($proID) {
  166. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  167. });
  168. });
  169. break;
  170. // more cases can be added as needed
  171. default:
  172. break;
  173. }
  174. $bills = $query->orderBy('created_at', 'desc')->get();
  175. return view('app.practice-management.bills', compact('bills', 'filter'));
  176. }
  177. public function unacknowledgedCancelledBills(Request $request)
  178. {
  179. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  180. ->where('is_cancelled', true)
  181. ->where('is_cancellation_acknowledged', false)
  182. ->orderBy('created_at', 'desc')
  183. ->get();
  184. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  185. }
  186. public function myTickets(Request $request, $filter = 'open')
  187. {
  188. $performer = $this->performer();
  189. $myTickets = Ticket::where(function ($q) use ($performer) {
  190. $q->where('assigned_pro_id', $performer->pro_id)
  191. ->orWhere('manager_pro_id', $performer->pro_id)
  192. ->orWhere('ordering_pro_id', $performer->pro_id)
  193. ->orWhere('initiating_pro_id', $performer->pro_id);
  194. });
  195. if ($filter === 'open') {
  196. $myTickets = $myTickets->where('is_open', true);
  197. } else if ($filter === 'closed') {
  198. $myTickets = $myTickets->where('is_open', false);
  199. }
  200. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  201. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  202. }
  203. public function myTextShortcuts(Request $request)
  204. {
  205. $myTextShortcuts = DB::table('pro_text_shortcut')
  206. ->leftJoin('pro', 'pro_text_shortcut.pro_id', '=', 'pro.id')
  207. ->select(
  208. 'pro_text_shortcut.uid',
  209. 'pro_text_shortcut.shortcut',
  210. 'pro_text_shortcut.text',
  211. 'pro.name_first',
  212. 'pro.name_last'
  213. )
  214. ->where('pro_text_shortcut.is_removed', false);
  215. if($this->performer()->pro->pro_type !== 'ADMIN') {
  216. $myTextShortcuts = $myTextShortcuts->where('pro_id', $this->performer()->pro_id);
  217. }
  218. $myTextShortcuts = $myTextShortcuts
  219. ->orderBy('pro.name_last')
  220. ->orderBy('pro.name_first')
  221. ->get();
  222. return view('app.practice-management.my-text-shortcuts', compact('myTextShortcuts'));
  223. }
  224. public function myFavorites(Request $request, $filter = 'all')
  225. {
  226. $performer = $this->performer();
  227. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  228. ->where('is_removed', false);
  229. if ($filter !== 'all') {
  230. $myFavorites = $myFavorites->where('category', $filter);
  231. }
  232. $myFavorites = $myFavorites
  233. ->orderBy('category', 'asc')
  234. ->orderBy('position_index', 'asc')
  235. ->get();
  236. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  237. }
  238. public function proAvailability(Request $request, $proUid = null)
  239. {
  240. $performer = $this->performer();
  241. $pro = $performer->pro;
  242. if ($proUid) {
  243. $pro = Pro::where('uid', $proUid)->first();
  244. }
  245. if ($request->get('pro_uid')) {
  246. $proUid = $request->get('pro_uid');
  247. $pro = Pro::where('uid', $proUid)->first();
  248. }
  249. $selectedProUid = $pro->uid;
  250. $pros = $this->pros;
  251. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  252. $generalAvailabilities = [
  253. 'MONDAY' => [],
  254. 'TUESDAY' => [],
  255. 'WEDNESDAY' => [],
  256. 'THURSDAY' => [],
  257. 'FRIDAY' => [],
  258. 'SATURDAY' => [],
  259. 'SUNDAY' => [],
  260. ];
  261. foreach ($generalAvailabilitiesList as $ga) {
  262. if ($ga->day_of_week == 'MONDAY') {
  263. $generalAvailabilities['MONDAY'][] = $ga;
  264. }
  265. if ($ga->day_of_week == 'TUESDAY') {
  266. $generalAvailabilities['TUESDAY'][] = $ga;
  267. }
  268. if ($ga->day_of_week == 'WEDNESDAY') {
  269. $generalAvailabilities['WEDNESDAY'][] = $ga;
  270. }
  271. if ($ga->day_of_week == 'THURSDAY') {
  272. $generalAvailabilities['THURSDAY'][] = $ga;
  273. }
  274. if ($ga->day_of_week == 'FRIDAY') {
  275. $generalAvailabilities['FRIDAY'][] = $ga;
  276. }
  277. if ($ga->day_of_week == 'SATURDAY') {
  278. $generalAvailabilities['SATURDAY'][] = $ga;
  279. }
  280. if ($ga->day_of_week == 'SUNDAY') {
  281. $generalAvailabilities['SUNDAY'][] = $ga;
  282. }
  283. }
  284. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  285. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  286. //events for the calendar
  287. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  288. $endDateTime = new DateTime($startDate);
  289. $endDateTime->modify('+6 day');
  290. $endDate = $endDateTime->format("Y-m-d");
  291. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  292. $events = json_encode($eventsData);
  293. return view(
  294. 'app.practice-management.pro-availability',
  295. compact(
  296. 'pros',
  297. 'generalAvailabilities',
  298. 'specificAvailabilities',
  299. 'specificUnavailabilities',
  300. 'events',
  301. 'selectedProUid'
  302. )
  303. );
  304. }
  305. public function loadAvailability(Request $request, $proUid)
  306. {
  307. $performer = $this->performer();
  308. $pro = $performer->pro;
  309. $startDate = $request->get('start');
  310. $endDate = $request->get('end');
  311. $selectedPro = Pro::where('uid', $proUid)->first();
  312. return $selectedPro->getAvailabilityEvents($startDate, $endDate);
  313. }
  314. public function proAvailabilityFilter(Request $request)
  315. {
  316. $proUid = $request->get('proUid');
  317. return ['success' => true, 'data' => $proUid];
  318. }
  319. // video call page (RHS)
  320. // generic call handle (no uid)
  321. // specific call handle (uid of client)
  322. public function meet(Request $request, $uid = false)
  323. {
  324. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  325. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  326. if (!empty($client)) {
  327. return view('app.video.call-minimal', compact('session', 'client'));
  328. }
  329. return view('app.video.call-agora-v2', compact('session', 'client'));
  330. }
  331. // check video page
  332. public function checkVideo(Request $request, $uid)
  333. {
  334. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  335. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  336. $publish = false;
  337. return view('app.video.check-video-minimal', compact('session', 'client'));
  338. }
  339. public function getParticipantInfo(Request $request)
  340. {
  341. $sid = intval($request->get('uid')) - 1000000;
  342. $session = AppSession::where('id', $sid)->first();
  343. $result = [
  344. "type" => '',
  345. "name" => ''
  346. ];
  347. if ($session) {
  348. $result["type"] = $session->session_type;
  349. switch ($session->session_type) {
  350. case 'PRO':
  351. $pro = Pro::where('id', $session->pro_id)->first();
  352. $result["name"] = $pro->displayName();
  353. break;
  354. case 'CLIENT':
  355. $client = Client::where('id', $session->client_id)->first();
  356. $result["name"] = $client->displayName();
  357. break;
  358. }
  359. }
  360. return json_encode($result);
  361. }
  362. // ajax ep used by the video page
  363. // this is needed bcoz meet() is used not
  364. // just for the client passed to the view
  365. public function getOpentokSessionKey(Request $request, $uid)
  366. {
  367. $client = Client::where('uid', $uid)->first();
  368. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  369. }
  370. // poll to check if there are patients with active mcp requests
  371. public function getPatientsInQueue(Request $request)
  372. {
  373. $myInitiatives = $this->performer->pro->initiatives;
  374. if ($myInitiatives) {
  375. $myInitiatives = strtoupper($myInitiatives);
  376. }
  377. $myInitiativesList = explode('|', $myInitiatives);
  378. $myForeignLanguages = $this->performer->pro->foreign_languages;
  379. if ($myForeignLanguages) {
  380. $myForeignLanguages = strtoupper($myForeignLanguages);
  381. }
  382. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  383. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  384. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  385. })
  386. ->where(function ($query) use ($myForeignLanguagesList) {
  387. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  388. })->limit(3)->get();
  389. $results = [];
  390. foreach ($clients as $client) {
  391. $results[] = [
  392. 'clientUid' => $client->uid,
  393. 'name' => $client->displayName(),
  394. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  395. ];
  396. }
  397. return json_encode($results);
  398. }
  399. public function currentWork(Request $request)
  400. {
  401. return view('app/current-work');
  402. }
  403. public function calendar(Request $request, $proUid = null)
  404. {
  405. $pros = Pro::all();
  406. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  407. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  408. $accessibleProIds = [];
  409. foreach ($accessiblePros as $accessiblePro) {
  410. $accessibleProIds[] = $accessiblePro->id;
  411. }
  412. $accessibleProIds[] = $this->pro->id;
  413. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  414. }
  415. return view('app.practice-management.calendar', compact('pros'));
  416. }
  417. public function cellularDeviceManager(Request $request, $proUid = null)
  418. {
  419. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  420. $performerPro = $this->performer->pro;
  421. $targetPro = null;
  422. $allPros = [];
  423. $expectedForHcp = null;
  424. if ($performerPro->pro_type == 'ADMIN') {
  425. $allPros = Pro::all();
  426. $targetPro = Pro::where('uid', $proUid)->first();
  427. } else {
  428. $targetPro = $performerPro;
  429. }
  430. $clients = [];
  431. if ($targetPro) {
  432. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  433. } else {
  434. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  435. }
  436. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  437. }
  438. public function treatmentServiceUtil(Request $request)
  439. {
  440. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  441. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  442. $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"));
  443. return view('app.practice-management.treatment-services-util', compact(
  444. 'view_treatment_service_utilization_org',
  445. 'view_treatment_service_utilization',
  446. 'view_treatment_service_utilization_by_patient'));
  447. }
  448. public function processingBillMatrix(Request $request, $proUid = null)
  449. {
  450. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  451. $performerPro = $this->performer->pro;
  452. $targetPro = null;
  453. $allPros = [];
  454. if ($performerPro->pro_type == 'ADMIN') {
  455. $allPros = Pro::all();
  456. $targetPro = Pro::where('uid', $proUid)->first();
  457. } else {
  458. $targetPro = $performerPro;
  459. }
  460. $bills = [];
  461. if ($targetPro) {
  462. $bills = Bill::where('hcp_pro_id', $targetPro->id)->
  463. where('has_hcp_been_paid', false)->
  464. where('is_cancelled', false)->
  465. where('is_signed_by_hcp', true)->
  466. orderBy('effective_date', 'desc')->paginate();
  467. } else {
  468. $bills = Bill::where('has_hcp_been_paid', false)->
  469. where('is_cancelled', false)->
  470. where('is_signed_by_hcp', true)->
  471. orderBy('effective_date', 'desc')->
  472. paginate();
  473. }
  474. $viewData = [
  475. 'bills' => $bills,
  476. 'allPros' => $allPros,
  477. 'targetPro' => $targetPro,
  478. 'performerPro' => $performerPro,
  479. 'proUid' => $proUid
  480. ];
  481. return view('app.practice-management.processing-bill-matrix', $viewData);
  482. }
  483. public function hcpBillMatrix(Request $request, $proUid = null)
  484. {
  485. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  486. $performerPro = $this->performer->pro;
  487. $targetPro = null;
  488. $allPros = [];
  489. $expectedForHcp = null;
  490. if ($performerPro->pro_type == 'ADMIN') {
  491. $allPros = Pro::all();
  492. $targetPro = Pro::where('uid', $proUid)->first();
  493. } else {
  494. $targetPro = $performerPro;
  495. }
  496. $rows = [];
  497. if ($targetPro) {
  498. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  499. } else {
  500. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  501. }
  502. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  503. }
  504. public function billingManager(Request $request, $proUid = null)
  505. {
  506. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  507. $performerPro = $this->performer->pro;
  508. $targetPro = null;
  509. $allPros = [];
  510. $expectedForHcp = null;
  511. if ($performerPro->pro_type == 'ADMIN') {
  512. $allPros = Pro::all();
  513. $targetPro = Pro::where('uid', $proUid)->first();
  514. } else {
  515. $targetPro = $performerPro;
  516. }
  517. $notes = [];
  518. if ($targetPro) {
  519. $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;
  520. $notes = Note::where('hcp_pro_id', $targetPro->id);
  521. } else {
  522. $notes = Note::where('id', '>', 0);
  523. }
  524. if($request->input('date')) {
  525. $notes = $notes->where('effective_dateest', $request->input('date'));
  526. }
  527. $filters = [];
  528. $filters['bills_created'] = $request->input('bills_created');
  529. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  530. $filters['bills_resolved'] = $request->input('bills_resolved');
  531. $filters['bills_closed'] = $request->input('bills_closed');
  532. $filters['claims_created'] = $request->input('claims_created');
  533. $filters['claims_closed'] = $request->input('claims_closed');
  534. if ($filters['bills_created']) {
  535. $notes->where(
  536. 'bill_total_expected',
  537. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  538. 0);
  539. }
  540. if ($filters['is_billing_marked_done']) {
  541. $notes->where(
  542. 'is_billing_marked_done',
  543. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  544. true);
  545. }
  546. if ($filters['bills_resolved']) {
  547. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  548. if ($filters['bills_resolved'] === 'yes') {
  549. $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');
  550. } elseif ($filters['bills_resolved'] === 'no') {
  551. $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');
  552. }
  553. }
  554. if ($filters['bills_closed']) {
  555. $notes->where(
  556. 'is_bill_closed',
  557. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  558. true);
  559. }
  560. if ($filters['claims_created']) {
  561. $notes->where(
  562. 'claim_total_expected',
  563. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  564. 0);
  565. }
  566. if ($filters['claims_closed']) {
  567. $notes->where(
  568. 'is_claim_closed',
  569. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  570. true);
  571. }
  572. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(10);
  573. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  574. }
  575. public function billMatrix(Request $request)
  576. {
  577. $bClients = [];
  578. $bHCPPros = [];
  579. $bNAPros = [];
  580. $filters = [];
  581. $filters['client'] = $request->input('client');
  582. $filters['service'] = $request->input('service');
  583. $filters['hcp'] = $request->input('hcp');
  584. $filters['hcp_paid'] = $request->input('hcp_paid');
  585. $filters['expected_op'] = $request->input('expected_op');
  586. $filters['expected_value'] = $request->input('expected_value');
  587. $filters['paid_op'] = $request->input('paid_op');
  588. $filters['paid_value'] = $request->input('paid_value');
  589. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  590. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  591. $filters['hcp_sign'] = $request->input('hcp_sign');
  592. $filters['verified'] = $request->input('verified');
  593. $filters['cancelled'] = $request->input('cancelled');
  594. $bills = Bill::orderBy('effective_date')->paginate();
  595. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  596. }
  597. public function medicarePartBClaims(Request $request)
  598. {
  599. $medicarePartBOnly = $request->get("medicare_part_b");
  600. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  601. //Only medicare claims
  602. $claims = [];
  603. foreach ($allClaims as $claim) {
  604. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  605. $claims[] = $claim;
  606. }
  607. }
  608. $claimEDIs = ClaimEDI::all();
  609. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  610. }
  611. // Generate PDF
  612. public function downloadClaims()
  613. {
  614. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  615. view()->share('claims', $claims);
  616. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  617. return $pdf->download('pdf_file.pdf');
  618. }
  619. public function tickets(Request $request, $proUid = null)
  620. {
  621. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  622. return view('app.practice-management.tickets', compact('tickets'));
  623. }
  624. public function supplyOrders(Request $request)
  625. {
  626. // counts
  627. $counts = $this->getSupplyOrderCounts();
  628. // so clients
  629. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  630. $soClientIDs = array_map(function ($_x) {
  631. return $_x->client_id;
  632. }, $soClientIDs);
  633. $soClients = Client::whereIn('id', $soClientIDs)->get();
  634. // so products
  635. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  636. $soProductIDs = array_map(function ($_x) {
  637. return $_x->product_id;
  638. }, $soProductIDs);
  639. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  640. $filters = [];
  641. $filters['client'] = $request->input('client');
  642. $filters['product'] = $request->input('product');
  643. $filters['reason'] = $request->input('reason');
  644. $filters['cu_memo'] = $request->input('cu_memo');
  645. $filters['pro_sign'] = $request->input('pro_sign');
  646. $filters['client_sign'] = $request->input('client_sign');
  647. $filters['shipment'] = $request->input('shipment');
  648. $filters['lot_number'] = $request->input('lot_number');
  649. $filters['imei'] = $request->input('imei');
  650. $filters['cancelled'] = $request->input('cancelled');
  651. $supplyOrders = SupplyOrder::where('id', '>', 0);
  652. // apply filters
  653. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  654. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  655. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  656. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  657. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  658. if ($filters['client_sign']) {
  659. if ($filters['client_sign'] === 'signed')
  660. $supplyOrders->where('is_signed_by_client', true);
  661. elseif ($filters['client_sign'] === 'waived')
  662. $supplyOrders->where('is_client_signature_waived', true);
  663. else
  664. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  665. }
  666. if ($filters['shipment']) {
  667. if ($filters['shipment'] === 'not_cleared_for_shipment')
  668. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  669. elseif ($filters['shipment'] === 'cleared_for_shipment')
  670. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  671. else
  672. $supplyOrders
  673. ->whereNotNull('shipment_id')
  674. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  675. }
  676. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  677. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  678. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  679. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  680. return view('app.practice-management.supply-orders',
  681. compact('supplyOrders', 'filters',
  682. 'soClients', 'soProducts', 'counts'
  683. )
  684. );
  685. }
  686. public function shipments(Request $request, $filter = null)
  687. {
  688. // counts
  689. $counts = $this->getShipmentCounts();
  690. // so clients
  691. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  692. $shClientIDs = array_map(function ($_x) {
  693. return $_x->client_id;
  694. }, $shClientIDs);
  695. $shClients = Client::whereIn('id', $shClientIDs)->get();
  696. $shipments = Shipment::where('id', '>', 0);
  697. $filters = [];
  698. $filters['client'] = $request->input('client');
  699. $filters['courier'] = $request->input('courier');
  700. $filters['tracking_num'] = $request->input('tracking_num');
  701. $filters['label'] = $request->input('label');
  702. $filters['status'] = $request->input('status');
  703. $filters['cancelled'] = $request->input('cancelled');
  704. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  705. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  706. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  707. if ($filters['label']) {
  708. if ($filters['label'] === 'yes')
  709. $shipments->whereNotNull('label_system_file_id');
  710. else
  711. $shipments->whereNull('label_system_file_id');
  712. }
  713. if ($filters['status']) $shipments->where('status', $filters['status']);
  714. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  715. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  716. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  717. }
  718. public function cellularMeasurements(Request $request)
  719. {
  720. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  721. return view('app.practice-management.cellular-measurements', compact('measurements'));
  722. }
  723. // v2 supply-orders & shipments management (wh)
  724. public function supplyOrdersReadyToShip(Request $request)
  725. {
  726. $counts = $this->getSupplyOrderCounts();
  727. $supplyOrders = SupplyOrder
  728. ::where('is_cleared_for_shipment', true)
  729. ->where('is_cancelled', false)
  730. ->whereNull('shipment_id')
  731. ->join('client', 'client.id', '=', 'supply_order.client_id')
  732. ->orderBy('client.name_last', 'ASC')
  733. ->orderBy('client.name_first', 'ASC')
  734. ->orderBy('supply_order.client_id', 'ASC')
  735. ->orderBy('supply_order.mailing_address_full', 'ASC')
  736. ->orderBy('supply_order.created_at', 'ASC')
  737. ->select('supply_order.*')
  738. ->paginate();
  739. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  740. }
  741. public function supplyOrdersShipmentUnderway(Request $request)
  742. {
  743. $counts = $this->getSupplyOrderCounts();
  744. $supplyOrders = SupplyOrder
  745. ::where('is_cancelled', false)
  746. ->whereNotNull('shipment_id')
  747. ->orderBy('client_id', 'ASC')
  748. ->orderBy('mailing_address_full', 'ASC')
  749. ->orderBy('created_at', 'ASC')
  750. ->paginate();
  751. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  752. }
  753. public function supplyOrdersHanging(Request $request)
  754. {
  755. $counts = $this->getSupplyOrderCounts();
  756. $supplyOrders = SupplyOrder
  757. ::select('supply_order.*')
  758. ->leftJoin('shipment', function($join) {
  759. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  760. })
  761. ->where('shipment.status', 'CANCELLED')
  762. ->where('supply_order.is_cancelled', false)
  763. ->orderBy('supply_order.client_id', 'ASC')
  764. ->orderBy('supply_order.mailing_address_full', 'ASC')
  765. ->orderBy('supply_order.created_at', 'ASC')
  766. ->paginate();
  767. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  768. }
  769. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  770. {
  771. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  772. ->where('is_cancelled', true)
  773. ->where('is_cancellation_acknowledged', false)
  774. ->orderBy('created_at', 'desc')
  775. ->paginate();
  776. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  777. }
  778. public function supplyOrdersUnsigned(Request $request)
  779. {
  780. $supplyOrders = SupplyOrder
  781. ::where('is_cancelled', false)
  782. ->where('is_signed_by_pro', false)
  783. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  784. ->orderBy('created_at', 'desc')
  785. ->paginate();
  786. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  787. }
  788. private function getSupplyOrderCounts()
  789. {
  790. return [
  791. "supplyOrders" => SupplyOrder::count(),
  792. "supplyOrdersReadyToShip" => SupplyOrder
  793. ::where('is_cleared_for_shipment', true)
  794. ->where('is_cancelled', false)
  795. ->whereNull('shipment_id')->count(),
  796. "supplyOrdersShipmentUnderway" => SupplyOrder
  797. ::where('is_cancelled', false)
  798. ->whereNotNull('shipment_id')->count(),
  799. "supplyOrdersHanging" => SupplyOrder
  800. ::leftJoin('shipment', function($join) {
  801. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  802. })
  803. ->where('shipment.status', 'CANCELLED')
  804. ->where('supply_order.is_cancelled', false)
  805. ->count(),
  806. ];
  807. }
  808. public function shipmentsReadyToPrint(Request $request)
  809. {
  810. $counts = $this->getShipmentCounts();
  811. $shipments = Shipment
  812. ::where('is_cancelled', false)
  813. ->where('status', 'CREATED')
  814. ->orderBy('created_at', 'ASC')
  815. ->paginate();
  816. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  817. }
  818. public function shipmentsShipmentUnderway(Request $request)
  819. {
  820. $counts = $this->getShipmentCounts();
  821. $shipments = Shipment
  822. ::where('is_cancelled', false)
  823. ->where('status', 'PRINTED')
  824. ->orderBy('created_at', 'ASC')
  825. ->paginate();
  826. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  827. }
  828. private function getShipmentCounts()
  829. {
  830. return [
  831. "shipments" => Shipment::count(),
  832. "shipmentsReadyToPrint" => Shipment
  833. ::where('is_cancelled', false)
  834. ->where('status', 'CREATED')
  835. ->count(),
  836. "shipmentsWaitingForPicker" => Shipment
  837. ::where('is_cancelled', false)
  838. ->where('status', 'PRINTED')
  839. ->count()
  840. ];
  841. }
  842. public function shipment(Request $request, Shipment $shipment)
  843. {
  844. return view('app.practice-management.shipment', compact('shipment'));
  845. }
  846. public function shipmentsMultiPrint(Request $request, $ids)
  847. {
  848. $ids = array_map(function ($_x) {
  849. return intval($_x);
  850. }, explode("|", $ids));
  851. $shipments = Shipment::whereIn('id', $ids)->get();
  852. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  853. }
  854. public function patientClaimSummary(Request $request, $proUid = null)
  855. {
  856. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  857. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  858. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  859. $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;
  860. $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;
  861. $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;
  862. $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;
  863. $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;
  864. $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;
  865. $performerPro = $this->performer->pro;
  866. $allPros = [];
  867. if ($performerPro->pro_type == 'ADMIN') {
  868. $allPros = Pro::all();
  869. } else {
  870. $allPros = [$performerPro];
  871. }
  872. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  873. $patientsQuery = Client::where('is_dummy', '=', false)
  874. ->whereNull('shadow_pro_id')
  875. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  876. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  877. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  878. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  879. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  880. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  881. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  882. if ($proUid) {
  883. $mcpPro = Pro::where('uid', $proUid)->first();
  884. if ($mcpPro) {
  885. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  886. }
  887. }
  888. $patientsQuery->whereRaw('(SELECT COUNT(*) FROM note where note.client_id = client.id) > 0');
  889. $patientsQuery->orderBy('notes_total', 'DESC');
  890. $patients = $patientsQuery->paginate(50);
  891. $data = [
  892. 'patients' => $patients,
  893. 'proUid' => $proUid,
  894. 'allPros' => $allPros,
  895. 'notesTotal' => $notesTotal,
  896. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  897. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  898. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  899. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  900. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  901. 'patientsTotal' => $patientsTotal,
  902. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  903. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  904. ];
  905. return view('app.practice-management.patient-claim-summary', $data);
  906. }
  907. public function claims(Request $request){
  908. $status = $request->get('status');
  909. $claims = [];
  910. if(!$status){
  911. $claims = Claim::orderBy('created_at', 'DESC')->paginate();
  912. }else{
  913. $claims = Claim::where('status', $status)->orderBy('created_at', 'DESC')->paginate();
  914. }
  915. return view('app.practice-management.claims', compact('claims', 'status'));
  916. }
  917. public function processClaims(Request $request) {
  918. $status = '';
  919. $q = $request->input('q') ? $request->input('q') : '';
  920. $from = $request->input('from') ? $request->input('from') : '1900-01-01';
  921. $to = $request->input('to') ? $request->input('to') : '2100-01-01';
  922. $params = [
  923. 'q' => '%' . $q . '%',
  924. 'from' => $from,
  925. 'to' => $to
  926. ];
  927. $hcpPro = $request->input('hcp') ? Pro::where('uid', $request->input('hcp'))->first() : null;
  928. if($hcpPro) {
  929. $params['hcp'] = $hcpPro->id;
  930. }
  931. $claims = DB::select(DB::raw("
  932. SELECT claim.uid AS uid,
  933. DATE(claim.created_at) AS created,
  934. claim.status,
  935. client.uid AS client_uid,
  936. client.cell_number AS client_phone,
  937. (client.name_last || ' ' || client.name_first) AS client ,
  938. client.chart_number AS client_chart_number,
  939. cp.id AS claim_pro_id,
  940. (cp.name_last || ' ' || cp.name_first) AS claim_pro,
  941. sp.id AS status_pro_id,
  942. (sp.name_last || ' ' || sp.name_first) AS status_pro,
  943. note.uid AS note_uid,
  944. note.method,
  945. note.new_or_fu_or_na,
  946. -- claim.status_updated_at,
  947. (DATE(claim.status_updated_at) || ' ' ||
  948. LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
  949. LPAD(EXTRACT(minute FROM claim.status_updated_at)::text, 2, '0')) AS status_updated_at,
  950. (SELECT string_agg(claim_line.cpt, ', ') FROM claim_line where claim_id = claim.id) AS cpts,
  951. (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,
  952. ROUND(claim.expected_total, 3) as expected_total
  953. FROM claim
  954. join client on claim.client_id = client.id
  955. join pro cp on claim.pro_id = cp.id
  956. left join note on claim.note_id = note.id
  957. left join app_session on claim.status_updated_by_session_id = app_session.id
  958. left join pro sp on app_session.pro_id = sp.id
  959. --WHERE claim.status IS NULL OR claim.status = 'NEW'
  960. WHERE (claim.status is NULL OR claim.status NOT IN ('CANCELLED', 'ABANDONED'))
  961. -- AND claim.current_version_id IS NOT NULL
  962. AND (client.name_first ILIKE :q OR
  963. client.name_last ILIKE :q OR
  964. client.chart_number ILIKE :q OR
  965. client.mcn ILIKE :q)
  966. AND (claim.created_at >= :from AND claim.created_at <= :to)
  967. " . ($hcpPro ? "AND claim.pro_id = :hcp" : '') . "
  968. AND claim.id IN (SELECT mb_claim.claim_id FROM mb_claim)
  969. ORDER BY claim.created_at ASC
  970. --OFFSET 0 LIMIT 15
  971. "), $params);
  972. if($request->input('json')) {
  973. return json_encode($claims);
  974. }
  975. return view('app.practice-management.process-claims', compact('claims', 'status'));
  976. }
  977. public function processNotes(Request $request) {
  978. $mode = $request->input('mode') ? $request->input('mode') : '1';
  979. if(($mode < 1 || $mode > 5) && $mode != 8) $mode = 1;
  980. $counts = [
  981. "picked" => Note::where('is_cancelled', false)
  982. ->whereNotNull('current_note_pickup_for_processing_id')
  983. ->count(),
  984. "bad" => Note::where('is_cancelled', false)
  985. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  986. ->count(),
  987. // not yet signed
  988. "mode-1" => Note::where('is_cancelled', false)
  989. ->where('is_signed_by_hcp', false)
  990. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  991. // ->whereNull('current_note_pickup_for_processing_id')
  992. ->count(),
  993. // billing not marked done
  994. "mode-2" => Note::where('is_cancelled', false)
  995. ->where('is_signed_by_hcp', true)
  996. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  997. // ->whereNull('current_note_pickup_for_processing_id')
  998. ->where('is_billing_marked_done', false)
  999. ->count(),
  1000. // billing not closed
  1001. "mode-3" => Note::where('is_cancelled', false)
  1002. ->where('is_signed_by_hcp', true)
  1003. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1004. // ->whereNull('current_note_pickup_for_processing_id')
  1005. ->where('is_billing_marked_done', true)
  1006. ->where('is_bill_closed', false)
  1007. ->count(),
  1008. // claiming not closed
  1009. "mode-4" => Note::where('is_cancelled', false)
  1010. ->where('is_signed_by_hcp', true)
  1011. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1012. // ->whereNull('current_note_pickup_for_processing_id')
  1013. ->where('is_billing_marked_done', true)
  1014. ->where('is_bill_closed', true)
  1015. ->where('is_claim_closed', false)
  1016. ->count(),
  1017. // has unsubmitted claims
  1018. "mode-5" => Note::where('is_cancelled', false)
  1019. ->where('is_signed_by_hcp', true)
  1020. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1021. // ->whereNull('current_note_pickup_for_processing_id')
  1022. ->where('is_billing_marked_done', true)
  1023. ->where('is_bill_closed', true)
  1024. ->where('is_claim_closed', true)
  1025. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1026. ->count(),
  1027. // has unsubmitted claims MARKED GOOD!
  1028. "mode-8" => Note::where('is_cancelled', false)
  1029. ->where('is_signed_by_hcp', true)
  1030. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1031. // ->whereNull('current_note_pickup_for_processing_id')
  1032. ->where('is_billing_marked_done', true)
  1033. ->where('is_bill_closed', true)
  1034. // ->where('is_claim_closed', true)
  1035. // ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1036. ->count(),
  1037. // all good
  1038. "mode-6" => Note::where('is_cancelled', false)
  1039. ->where('is_signed_by_hcp', true)
  1040. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1041. // ->whereNull('current_note_pickup_for_processing_id')
  1042. ->where('is_billing_marked_done', true)
  1043. ->where('is_bill_closed', true)
  1044. ->where('is_claim_closed', true)
  1045. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1046. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1047. ->count(),
  1048. "mode-7" => Note::where('is_cancelled', false)
  1049. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1050. ->count(),
  1051. ];
  1052. return view('app.practice-management.process-notes', compact('mode', 'counts'));
  1053. }
  1054. public function getNextNote(Request $request, $mode)
  1055. {
  1056. $note = null;
  1057. switch (+$mode) {
  1058. case 1:
  1059. $note = Note::where('is_cancelled', false)
  1060. ->where('is_signed_by_hcp', false)
  1061. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1062. ->whereNull('current_note_pickup_for_processing_id')
  1063. ->orderBy('effective_dateest', 'DESC')
  1064. ->first();
  1065. break;
  1066. case 2:
  1067. $note = Note::where('is_cancelled', false)
  1068. ->where('is_signed_by_hcp', true)
  1069. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1070. ->whereNull('current_note_pickup_for_processing_id')
  1071. ->where('is_billing_marked_done', false)
  1072. ->orderBy('effective_dateest', 'DESC')
  1073. ->first();
  1074. break;
  1075. case 3:
  1076. $note = Note::where('is_cancelled', false)
  1077. ->where('is_signed_by_hcp', true)
  1078. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1079. ->whereNull('current_note_pickup_for_processing_id')
  1080. ->where('is_billing_marked_done', true)
  1081. ->where('is_bill_closed', false)
  1082. ->orderBy('effective_dateest', 'DESC')
  1083. ->first();
  1084. break;
  1085. case 4:
  1086. $note = Note::where('is_cancelled', false)
  1087. ->where('is_signed_by_hcp', true)
  1088. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1089. ->whereNull('current_note_pickup_for_processing_id')
  1090. ->where('is_billing_marked_done', true)
  1091. ->where('is_bill_closed', true)
  1092. ->where('is_claim_closed', false)
  1093. ->orderBy('effective_dateest', 'DESC')
  1094. ->first();
  1095. break;
  1096. case 5:
  1097. $note = Note::where('is_cancelled', false)
  1098. ->where('is_signed_by_hcp', true)
  1099. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1100. ->whereNull('current_note_pickup_for_processing_id')
  1101. ->where('is_billing_marked_done', true)
  1102. ->where('is_bill_closed', true)
  1103. ->where('is_claim_closed', true)
  1104. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1105. ->orderBy('effective_dateest', 'DESC')
  1106. ->first();
  1107. break;
  1108. case 8:
  1109. $note = Note::where('is_cancelled', false)
  1110. ->where('is_signed_by_hcp', true)
  1111. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1112. // ->whereNull('current_note_pickup_for_processing_id')
  1113. ->where('is_billing_marked_done', true)
  1114. ->where('is_bill_closed', true)
  1115. // ->where('is_claim_closed', true)
  1116. // ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1117. ->orderBy('effective_dateest', 'DESC')
  1118. ->first();
  1119. break;
  1120. }
  1121. if($note) {
  1122. $note->client_uid = $note->client->uid;
  1123. }
  1124. return json_encode($note);
  1125. }
  1126. public function pickedNotes(Request $request) {
  1127. $counts = [
  1128. "unpicked" => Note::where('is_cancelled', false)
  1129. ->whereNull('current_note_pickup_for_processing_id')
  1130. ->count(),
  1131. ];
  1132. $notes = Note::where('is_cancelled', false)
  1133. ->whereNotNull('current_note_pickup_for_processing_id')
  1134. ->orderBy('effective_dateest', 'ASC')
  1135. ->paginate();
  1136. return view('app.practice-management.picked-notes', compact('counts', 'notes'));
  1137. }
  1138. public function badNotes(Request $request) {
  1139. $counts = [
  1140. "unpicked" => Note::where('is_cancelled', false)
  1141. ->whereNull('current_note_pickup_for_processing_id')
  1142. ->count(),
  1143. ];
  1144. $notes = Note::where('is_cancelled', false)
  1145. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1146. ->orderBy('effective_dateest', 'ASC')
  1147. ->paginate();
  1148. return view('app.practice-management.bad-notes', compact('counts', 'notes'));
  1149. }
  1150. public function doneNotes(Request $request) {
  1151. $counts = [
  1152. "unpicked" => Note::where('is_cancelled', false)
  1153. ->whereNull('current_note_pickup_for_processing_id')
  1154. ->count(),
  1155. ];
  1156. $notes = Note::where('is_cancelled', false)
  1157. ->where('is_signed_by_hcp', true)
  1158. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1159. ->whereNull('current_note_pickup_for_processing_id')
  1160. ->where('is_billing_marked_done', true)
  1161. ->where('is_bill_closed', true)
  1162. ->where('is_claim_closed', true)
  1163. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1164. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1165. ->orderBy('effective_dateest', 'ASC')
  1166. ->paginate();
  1167. return view('app.practice-management.done-notes', compact('counts', 'notes'));
  1168. }
  1169. public function currentMbClaim(Request $request, $claimUid) {
  1170. $claim = Claim::where('uid', $claimUid)->first();
  1171. return json_encode(MBClaim::where('claim_version_id', $claim->currentVersion->id)->first());
  1172. }
  1173. public function currentClaimLines(Request $request, $claimUid) {
  1174. $claim = Claim::where('uid', $claimUid)->first();
  1175. if($request->input('json')) {
  1176. foreach ($claim->lines as $line) {
  1177. $line->expected_total = round($line->expected_total, 3);
  1178. $x = $line->claimLineIcds;
  1179. }
  1180. return json_encode($claim->lines);
  1181. }
  1182. return view('app.practice-management._claim-lines', compact('claim'));
  1183. }
  1184. public function packsMultiPrint(Request $request) {
  1185. $packs = Pack
  1186. ::select('pack.*')
  1187. ->leftJoin('shipment', function($join) {
  1188. $join->on('pack.shipment_id', '=', 'shipment.id');
  1189. })
  1190. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  1191. ->where(function ($query) {
  1192. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  1193. })
  1194. ->whereNotNull('pack.label_system_file_id')
  1195. ->orderBy('pack.created_at', 'ASC')
  1196. ->get();
  1197. return view('app.practice-management.packs-multi-print', compact('packs'));
  1198. }
  1199. public function packsMultiPDF(Request $request, $ids) {
  1200. $ids = array_map(function ($_x) {
  1201. return intval($_x);
  1202. }, explode("|", $ids));
  1203. $packs = Pack::whereIn('id', $ids)->get();
  1204. }
  1205. public function handouts(Request $request) {
  1206. $handouts = Handout::orderBy('display_name')->get();
  1207. return view('app.practice-management.handouts', compact('handouts'));
  1208. }
  1209. private function callJava($request, $endPoint, $data)
  1210. {
  1211. $url = config('stag.backendUrl') . $endPoint;
  1212. $response = Http::asForm()
  1213. ->withHeaders([
  1214. 'sessionKey' => $request->cookie('sessionKey')
  1215. ])
  1216. ->post($url, $data)
  1217. ->body();
  1218. dd($response);
  1219. return $response;
  1220. }
  1221. public function genericBills(Request $request)
  1222. {
  1223. return view('app.practice-management.generic-bills');
  1224. }
  1225. }