PracticeManagementController.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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\Measurement;
  7. use App\Models\Bill;
  8. use App\Models\Claim;
  9. use App\Models\Client;
  10. use App\Models\McpRequest;
  11. use App\Models\Note;
  12. use App\Models\Pro;
  13. use App\Models\Product;
  14. use App\Models\ProFavorite;
  15. use App\Models\ProGeneralAvailability;
  16. use App\Models\ProProAccess;
  17. use App\Models\ProRate;
  18. use App\Models\ProSpecificAvailability;
  19. use App\Models\ProSpecificUnavailability;
  20. use App\Models\ProTextShortcut;
  21. use App\Models\ProTransaction;
  22. use App\Models\Shipment;
  23. use App\Models\SupplyOrder;
  24. use App\Models\Ticket;
  25. use Illuminate\Support\Facades\DB;
  26. use PDF;
  27. use DateTime;
  28. use DateTimeZone;
  29. use Illuminate\Http\Request;
  30. class PracticeManagementController extends Controller
  31. {
  32. public function billingReport(Request $request)
  33. {
  34. $rows = BillingReport::paginate(200);
  35. return view('app.practice-management.billing-report', compact('rows'));
  36. }
  37. public function dashboard(Request $request)
  38. {
  39. return view('app.practice-management.dashboard');
  40. }
  41. public function rates(Request $request, $selectedProUid = 'all')
  42. {
  43. $proUid = $selectedProUid ? $selectedProUid : 'all';
  44. $rates = ProRate::where('is_active', true);
  45. if ($proUid !== 'all') {
  46. $selectedPro = Pro::where('uid', $proUid)->first();
  47. $rates = $rates->where('pro_id', $selectedPro->id);
  48. }
  49. $rates = $rates->orderBy('pro_id', 'asc')->get();
  50. $pros = $this->pros;
  51. return view('app.practice-management.rates', compact('rates', 'pros', 'selectedProUid'));
  52. }
  53. public function previousBills(Request $request)
  54. {
  55. return view('app.practice-management.previous-bills');
  56. }
  57. public function financialTransactions(Request $request)
  58. {
  59. $transactions = ProTransaction::where('pro_id', $this->performer()->pro->id)->orderBy('created_at', 'desc')->get();
  60. return view('app.practice-management.financial-transactions', compact('transactions'));
  61. }
  62. public function pendingBillsToSign(Request $request)
  63. {
  64. return view('app.practice-management.pending-bills-to-sign');
  65. }
  66. public function HR(Request $request)
  67. {
  68. return view('app.practice-management.hr');
  69. }
  70. public function directDepositSettings(Request $request)
  71. {
  72. return view('app.practice-management.direct-deposit-settings');
  73. }
  74. public function w9(Request $request)
  75. {
  76. return view('app.practice-management.w9');
  77. }
  78. public function contract(Request $request)
  79. {
  80. return view('app.practice-management.contract');
  81. }
  82. public function notes(Request $request, $filter = '')
  83. {
  84. $proID = $this->performer()->pro->id;
  85. $query = Note::where('hcp_pro_id', $proID);
  86. switch ($filter) {
  87. case 'not-yet-signed':
  88. $query = $query->where('is_signed_by_hcp', false);
  89. break;
  90. // more cases can be added as needed
  91. default:
  92. break;
  93. }
  94. $notes = $query->orderBy('created_at', 'desc')->get();
  95. return view('app.practice-management.notes', compact('notes', 'filter'));
  96. }
  97. public function bills(Request $request, $filter = '')
  98. {
  99. $proID = $this->performer()->pro->id;
  100. $query = Bill::where('is_cancelled', false);
  101. switch ($filter) {
  102. case 'not-yet-signed':
  103. $query = $query
  104. ->where(function ($q) use ($proID) {
  105. $q->where(function ($q2) use ($proID) {
  106. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', false);
  107. })
  108. ->orWhere(function ($q2) use ($proID) {
  109. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', false);
  110. })
  111. ->orWhere(function ($q2) use ($proID) {
  112. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', false);
  113. })
  114. ->orWhere(function ($q2) use ($proID) {
  115. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', false);
  116. });
  117. });
  118. break;
  119. case 'previous':
  120. $query = $query
  121. ->where(function ($q) use ($proID) {
  122. $q->where(function ($q2) use ($proID) {
  123. $q2->where('hcp_pro_id', $proID)->where('is_signed_by_hcp', true);
  124. })
  125. ->orWhere(function ($q2) use ($proID) {
  126. $q2->where('cm_pro_id', $proID)->where('is_signed_by_cm', true);
  127. })
  128. ->orWhere(function ($q2) use ($proID) {
  129. $q2->where('rme_pro_id', $proID)->where('is_signed_by_rme', true);
  130. })
  131. ->orWhere(function ($q2) use ($proID) {
  132. $q2->where('rmm_pro_id', $proID)->where('is_signed_by_rmm', true);
  133. });
  134. });
  135. break;
  136. // more cases can be added as needed
  137. default:
  138. break;
  139. }
  140. $bills = $query->orderBy('created_at', 'desc')->get();
  141. return view('app.practice-management.bills', compact('bills', 'filter'));
  142. }
  143. public function unacknowledgedCancelledBills(Request $request)
  144. {
  145. $bills = Bill::where('hcp_pro_id', $this->performer()->pro->id)
  146. ->where('is_cancelled', true)
  147. ->where('is_cancellation_acknowledged', false)
  148. ->orderBy('created_at', 'desc')
  149. ->get();
  150. return view('app.practice-management.unacknowledged-cancelled-bills', compact('bills'));
  151. }
  152. public function myTickets(Request $request, $filter = 'open')
  153. {
  154. $performer = $this->performer();
  155. $myTickets = Ticket::where(function ($q) use ($performer) {
  156. $q->where('assigned_pro_id', $performer->pro_id)
  157. ->orWhere('manager_pro_id', $performer->pro_id)
  158. ->orWhere('ordering_pro_id', $performer->pro_id)
  159. ->orWhere('initiating_pro_id', $performer->pro_id);
  160. });
  161. if ($filter === 'open') {
  162. $myTickets = $myTickets->where('is_open', true);
  163. } else if ($filter === 'closed') {
  164. $myTickets = $myTickets->where('is_open', false);
  165. }
  166. $myTickets = $myTickets->orderBy('created_at', 'desc')->get();
  167. return view('app.practice-management.my-tickets', compact('myTickets', 'filter'));
  168. }
  169. public function myTextShortcuts(Request $request)
  170. {
  171. $performer = $this->performer();
  172. $myTextShortcuts = ProTextShortcut::where('pro_id', $performer->pro_id)->where('is_removed', false)->get();
  173. return view('app.practice-management.my-text-shortcuts', compact('myTextShortcuts'));
  174. }
  175. public function myFavorites(Request $request, $filter = 'all')
  176. {
  177. $performer = $this->performer();
  178. $myFavorites = ProFavorite::where('pro_id', $performer->pro_id)
  179. ->where('is_removed', false);
  180. if ($filter !== 'all') {
  181. $myFavorites = $myFavorites->where('category', $filter);
  182. }
  183. $myFavorites = $myFavorites
  184. ->orderBy('category', 'asc')
  185. ->orderBy('position_index', 'asc')
  186. ->get();
  187. return view('app.practice-management.my-favorites', compact('myFavorites', 'filter'));
  188. }
  189. public function proAvailability(Request $request, $proUid = null)
  190. {
  191. $performer = $this->performer();
  192. $pro = $performer->pro;
  193. if ($proUid) {
  194. $pro = Pro::where('uid', $proUid)->first();
  195. }
  196. if ($request->get('pro_uid')) {
  197. $proUid = $request->get('pro_uid');
  198. $pro = Pro::where('uid', $proUid)->first();
  199. }
  200. $selectedProUid = $pro->uid;
  201. $pros = $this->pros;
  202. $generalAvailabilitiesList = ProGeneralAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('created_at', 'asc')->get();
  203. $generalAvailabilities = [
  204. 'MONDAY' => [],
  205. 'TUESDAY' => [],
  206. 'WEDNESDAY' => [],
  207. 'THURSDAY' => [],
  208. 'FRIDAY' => [],
  209. 'SATURDAY' => [],
  210. 'SUNDAY' => [],
  211. ];
  212. foreach ($generalAvailabilitiesList as $ga) {
  213. if ($ga->day_of_week == 'MONDAY') {
  214. $generalAvailabilities['MONDAY'][] = $ga;
  215. }
  216. if ($ga->day_of_week == 'TUESDAY') {
  217. $generalAvailabilities['TUESDAY'][] = $ga;
  218. }
  219. if ($ga->day_of_week == 'WEDNESDAY') {
  220. $generalAvailabilities['WEDNESDAY'][] = $ga;
  221. }
  222. if ($ga->day_of_week == 'THURSDAY') {
  223. $generalAvailabilities['THURSDAY'][] = $ga;
  224. }
  225. if ($ga->day_of_week == 'FRIDAY') {
  226. $generalAvailabilities['FRIDAY'][] = $ga;
  227. }
  228. if ($ga->day_of_week == 'SATURDAY') {
  229. $generalAvailabilities['SATURDAY'][] = $ga;
  230. }
  231. if ($ga->day_of_week == 'SUNDAY') {
  232. $generalAvailabilities['SUNDAY'][] = $ga;
  233. }
  234. }
  235. $specificAvailabilities = ProSpecificAvailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time')->get();
  236. $specificUnavailabilities = ProSpecificUnavailability::where('pro_id', $pro->id)->where('is_cancelled', false)->orderBy('start_time', 'asc')->get();
  237. //events for the calendar
  238. $startDate = date('Y-m-d', strtotime("sunday -1 week"));
  239. $endDateTime = new DateTime($startDate);
  240. $endDateTime->modify('+6 day');
  241. $endDate = $endDateTime->format("Y-m-d");
  242. $eventsData = $pro->getAvailabilityEvents($startDate, $endDate);
  243. $events = json_encode($eventsData);
  244. return view(
  245. 'app.practice-management.pro-availability',
  246. compact(
  247. 'pros',
  248. 'generalAvailabilities',
  249. 'specificAvailabilities',
  250. 'specificUnavailabilities',
  251. 'events',
  252. 'selectedProUid'
  253. )
  254. );
  255. }
  256. public function loadAvailability(Request $request, $proUid)
  257. {
  258. $performer = $this->performer();
  259. $pro = $performer->pro;
  260. $startDate = $request->get('start');
  261. $endDate = $request->get('end');
  262. $selectedPro = Pro::where('uid', $proUid)->first();
  263. return $selectedPro->getAvailabilityEvents($startDate, $endDate);
  264. }
  265. public function proAvailabilityFilter(Request $request)
  266. {
  267. $proUid = $request->get('proUid');
  268. return ['success' => true, 'data' => $proUid];
  269. }
  270. // video call page (RHS)
  271. // generic call handle (no uid)
  272. // specific call handle (uid of client)
  273. public function meet(Request $request, $uid = false)
  274. {
  275. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  276. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  277. if (!empty($client)) {
  278. return view('app.video.call-minimal', compact('session', 'client'));
  279. }
  280. return view('app.video.call-agora-v2', compact('session', 'client'));
  281. }
  282. // check video page
  283. public function checkVideo(Request $request, $uid)
  284. {
  285. $session = AppSession::where('session_key', $request->cookie('sessionKey'))->first();
  286. $client = !empty($uid) ? Client::where('uid', $uid)->first() : null;
  287. $publish = false;
  288. return view('app.video.check-video-minimal', compact('session', 'client'));
  289. }
  290. public function getParticipantInfo(Request $request)
  291. {
  292. $sid = intval($request->get('uid')) - 1000000;
  293. $session = AppSession::where('id', $sid)->first();
  294. $result = [
  295. "type" => '',
  296. "name" => ''
  297. ];
  298. if ($session) {
  299. $result["type"] = $session->session_type;
  300. switch ($session->session_type) {
  301. case 'PRO':
  302. $pro = Pro::where('id', $session->pro_id)->first();
  303. $result["name"] = $pro->displayName();
  304. break;
  305. case 'CLIENT':
  306. $client = Client::where('id', $session->client_id)->first();
  307. $result["name"] = $client->displayName();
  308. break;
  309. }
  310. }
  311. return json_encode($result);
  312. }
  313. // ajax ep used by the video page
  314. // this is needed bcoz meet() is used not
  315. // just for the client passed to the view
  316. public function getOpentokSessionKey(Request $request, $uid)
  317. {
  318. $client = Client::where('uid', $uid)->first();
  319. return json_encode(["data" => $client ? $client->opentok_session_id : '']);
  320. }
  321. // poll to check if there are patients with active mcp requests
  322. public function getPatientsInQueue(Request $request)
  323. {
  324. $myInitiatives = $this->performer->pro->initiatives;
  325. if ($myInitiatives) {
  326. $myInitiatives = strtoupper($myInitiatives);
  327. }
  328. $myInitiativesList = explode('|', $myInitiatives);
  329. $myForeignLanguages = $this->performer->pro->foreign_languages;
  330. if ($myForeignLanguages) {
  331. $myForeignLanguages = strtoupper($myForeignLanguages);
  332. }
  333. $myForeignLanguagesList = explode('|', $myForeignLanguages);
  334. $clients = Client::whereNotNull('active_mcp_request_id')->where(function ($query) use ($myInitiativesList) {
  335. $query->whereNull('initiative')->orWhereIn('initiative', $myInitiativesList);
  336. })
  337. ->where(function ($query) use ($myForeignLanguagesList) {
  338. $query->whereNull('preferred_foreign_language')->orWhereIn('preferred_foreign_language', $myForeignLanguagesList);
  339. })->limit(3)->get();
  340. $results = [];
  341. foreach ($clients as $client) {
  342. $results[] = [
  343. 'clientUid' => $client->uid,
  344. 'name' => $client->displayName(),
  345. 'initials' => substr($client->name_first, 0, 1) . substr($client->name_last, 0, 1)
  346. ];
  347. }
  348. return json_encode($results);
  349. }
  350. public function currentWork(Request $request)
  351. {
  352. return view('app/current-work');
  353. }
  354. public function calendar(Request $request, $proUid = null)
  355. {
  356. $pros = Pro::all();
  357. if ($this->pro && $this->pro->pro_type != 'ADMIN') {
  358. $accessiblePros = ProProAccess::where('owner_pro_id', $this->pro->id);
  359. $accessibleProIds = [];
  360. foreach ($accessiblePros as $accessiblePro) {
  361. $accessibleProIds[] = $accessiblePro->id;
  362. }
  363. $accessibleProIds[] = $this->pro->id;
  364. $pros = Pro::whereIn('id', $accessibleProIds)->get();
  365. }
  366. return view('app.practice-management.calendar', compact('pros'));
  367. }
  368. public function cellularDeviceManager(Request $request, $proUid = null)
  369. {
  370. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  371. $performerPro = $this->performer->pro;
  372. $targetPro = null;
  373. $allPros = [];
  374. $expectedForHcp = null;
  375. if ($performerPro->pro_type == 'ADMIN') {
  376. $allPros = Pro::all();
  377. $targetPro = Pro::where('uid', $proUid)->first();
  378. } else {
  379. $targetPro = $performerPro;
  380. }
  381. $clients = [];
  382. if ($targetPro) {
  383. $clients = Client::where('mcp_pro_id', $targetPro->id)->orderBy('created_at', 'desc')->paginate(100);
  384. } else {
  385. $clients = Client::orderBy('created_at', 'desc')->paginate(100);
  386. }
  387. return view('app.practice-management.cellular-device-manager', compact('clients', 'allPros', 'targetPro', 'proUid'));
  388. }
  389. public function treatmentServiceUtil(Request $request)
  390. {
  391. $view_treatment_service_utilization_org = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization_org ORDER BY effective_date DESC"));
  392. $view_treatment_service_utilization = DB::select(DB::raw("SELECT * FROM view_treatment_service_utilization ORDER BY effective_date DESC, total_hrs DESC"));
  393. $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"));
  394. return view('app.practice-management.treatment-services-util', compact(
  395. 'view_treatment_service_utilization_org',
  396. 'view_treatment_service_utilization',
  397. 'view_treatment_service_utilization_by_patient'));
  398. }
  399. public function processingBillMatrix(Request $request, $proUid = null)
  400. {
  401. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  402. $performerPro = $this->performer->pro;
  403. $targetPro = null;
  404. $allPros = [];
  405. if ($performerPro->pro_type == 'ADMIN') {
  406. $allPros = Pro::all();
  407. $targetPro = Pro::where('uid', $proUid)->first();
  408. } else {
  409. $targetPro = $performerPro;
  410. }
  411. $bills = [];
  412. if ($targetPro) {
  413. $bills = Bill::where('hcp_pro_id', $targetPro->id)->
  414. where('has_hcp_been_paid', false)->
  415. where('is_cancelled', false)->
  416. where('is_signed_by_hcp', true)->
  417. orderBy('effective_date', 'desc')->paginate();
  418. } else {
  419. $bills = Bill::where('has_hcp_been_paid', false)->
  420. where('is_cancelled', false)->
  421. where('is_signed_by_hcp', true)->
  422. orderBy('effective_date', 'desc')->
  423. paginate();
  424. }
  425. $viewData = [
  426. 'bills' => $bills,
  427. 'allPros' => $allPros,
  428. 'targetPro' => $targetPro,
  429. 'performerPro' => $performerPro,
  430. 'proUid' => $proUid
  431. ];
  432. return view('app.practice-management.processing-bill-matrix', $viewData);
  433. }
  434. public function hcpBillMatrix(Request $request, $proUid = null)
  435. {
  436. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  437. $performerPro = $this->performer->pro;
  438. $targetPro = null;
  439. $allPros = [];
  440. $expectedForHcp = null;
  441. if ($performerPro->pro_type == 'ADMIN') {
  442. $allPros = Pro::all();
  443. $targetPro = Pro::where('uid', $proUid)->first();
  444. } else {
  445. $targetPro = $performerPro;
  446. }
  447. $rows = [];
  448. if ($targetPro) {
  449. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  450. } else {
  451. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  452. }
  453. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  454. }
  455. public function billingManager(Request $request, $proUid = null)
  456. {
  457. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  458. $performerPro = $this->performer->pro;
  459. $targetPro = null;
  460. $allPros = [];
  461. $expectedForHcp = null;
  462. if ($performerPro->pro_type == 'ADMIN') {
  463. $allPros = Pro::all();
  464. $targetPro = Pro::where('uid', $proUid)->first();
  465. } else {
  466. $targetPro = $performerPro;
  467. }
  468. $notes = [];
  469. if ($targetPro) {
  470. $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;
  471. $notes = Note::where('hcp_pro_id', $targetPro->id);
  472. } else {
  473. $notes = Note::where('id', '>', 0);
  474. }
  475. $filters = [];
  476. $filters['bills_created'] = $request->input('bills_created');
  477. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  478. $filters['bills_resolved'] = $request->input('bills_resolved');
  479. $filters['bills_closed'] = $request->input('bills_closed');
  480. $filters['claims_created'] = $request->input('claims_created');
  481. $filters['claims_closed'] = $request->input('claims_closed');
  482. if ($filters['bills_created']) {
  483. $notes->where(
  484. 'bill_total_expected',
  485. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  486. 0);
  487. }
  488. if ($filters['is_billing_marked_done']) {
  489. $notes->where(
  490. 'is_billing_marked_done',
  491. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  492. true);
  493. }
  494. if ($filters['bills_resolved']) {
  495. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  496. if ($filters['bills_resolved'] === 'yes') {
  497. $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');
  498. } elseif ($filters['bills_resolved'] === 'no') {
  499. $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');
  500. }
  501. }
  502. if ($filters['bills_closed']) {
  503. $notes->where(
  504. 'is_bill_closed',
  505. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  506. true);
  507. }
  508. if ($filters['claims_created']) {
  509. $notes->where(
  510. 'claim_total_expected',
  511. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  512. 0);
  513. }
  514. if ($filters['claims_closed']) {
  515. $notes->where(
  516. 'is_claim_closed',
  517. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  518. true);
  519. }
  520. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate();
  521. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  522. }
  523. public function billMatrix(Request $request)
  524. {
  525. $bClients = [];
  526. $bHCPPros = [];
  527. $bNAPros = [];
  528. $filters = [];
  529. $filters['client'] = $request->input('client');
  530. $filters['service'] = $request->input('service');
  531. $filters['hcp'] = $request->input('hcp');
  532. $filters['hcp_paid'] = $request->input('hcp_paid');
  533. $filters['expected_op'] = $request->input('expected_op');
  534. $filters['expected_value'] = $request->input('expected_value');
  535. $filters['paid_op'] = $request->input('paid_op');
  536. $filters['paid_value'] = $request->input('paid_value');
  537. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  538. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  539. $filters['hcp_sign'] = $request->input('hcp_sign');
  540. $filters['verified'] = $request->input('verified');
  541. $filters['cancelled'] = $request->input('cancelled');
  542. $bills = Bill::orderBy('effective_date')->paginate();
  543. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  544. }
  545. public function medicarePartBClaims(Request $request)
  546. {
  547. $medicarePartBOnly = $request->get("medicare_part_b");
  548. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  549. //Only medicare claims
  550. $claims = [];
  551. foreach ($allClaims as $claim) {
  552. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  553. $claims[] = $claim;
  554. }
  555. }
  556. $claimEDIs = ClaimEDI::all();
  557. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  558. }
  559. // Generate PDF
  560. public function downloadClaims()
  561. {
  562. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  563. view()->share('claims', $claims);
  564. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  565. return $pdf->download('pdf_file.pdf');
  566. }
  567. public function tickets(Request $request, $proUid = null)
  568. {
  569. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  570. return view('app.practice-management.tickets', compact('tickets'));
  571. }
  572. public function supplyOrders(Request $request)
  573. {
  574. // counts
  575. $counts = $this->getSupplyOrderCounts();
  576. // so clients
  577. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  578. $soClientIDs = array_map(function ($_x) {
  579. return $_x->client_id;
  580. }, $soClientIDs);
  581. $soClients = Client::whereIn('id', $soClientIDs)->get();
  582. // so products
  583. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  584. $soProductIDs = array_map(function ($_x) {
  585. return $_x->product_id;
  586. }, $soProductIDs);
  587. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  588. $filters = [];
  589. $filters['client'] = $request->input('client');
  590. $filters['product'] = $request->input('product');
  591. $filters['reason'] = $request->input('reason');
  592. $filters['cu_memo'] = $request->input('cu_memo');
  593. $filters['pro_sign'] = $request->input('pro_sign');
  594. $filters['client_sign'] = $request->input('client_sign');
  595. $filters['shipment'] = $request->input('shipment');
  596. $filters['lot_number'] = $request->input('lot_number');
  597. $filters['imei'] = $request->input('imei');
  598. $filters['cancelled'] = $request->input('cancelled');
  599. $supplyOrders = SupplyOrder::where('id', '>', 0);
  600. // apply filters
  601. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  602. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  603. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  604. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  605. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  606. if ($filters['client_sign']) {
  607. if ($filters['client_sign'] === 'signed')
  608. $supplyOrders->where('is_signed_by_client', true);
  609. elseif ($filters['client_sign'] === 'waived')
  610. $supplyOrders->where('is_client_signature_waived', true);
  611. else
  612. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  613. }
  614. if ($filters['shipment']) {
  615. if ($filters['shipment'] === 'not_cleared_for_shipment')
  616. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  617. elseif ($filters['shipment'] === 'cleared_for_shipment')
  618. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  619. else
  620. $supplyOrders
  621. ->whereNotNull('shipment_id')
  622. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  623. }
  624. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  625. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  626. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  627. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  628. return view('app.practice-management.supply-orders',
  629. compact('supplyOrders', 'filters',
  630. 'soClients', 'soProducts', 'counts'
  631. )
  632. );
  633. }
  634. public function shipments(Request $request, $filter = null)
  635. {
  636. // counts
  637. $counts = $this->getShipmentCounts();
  638. // so clients
  639. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  640. $shClientIDs = array_map(function ($_x) {
  641. return $_x->client_id;
  642. }, $shClientIDs);
  643. $shClients = Client::whereIn('id', $shClientIDs)->get();
  644. $shipments = Shipment::where('id', '>', 0);
  645. $filters = [];
  646. $filters['client'] = $request->input('client');
  647. $filters['courier'] = $request->input('courier');
  648. $filters['tracking_num'] = $request->input('tracking_num');
  649. $filters['label'] = $request->input('label');
  650. $filters['status'] = $request->input('status');
  651. $filters['cancelled'] = $request->input('cancelled');
  652. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  653. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  654. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  655. if ($filters['label']) {
  656. if ($filters['label'] === 'yes')
  657. $shipments->whereNotNull('label_system_file_id');
  658. else
  659. $shipments->whereNull('label_system_file_id');
  660. }
  661. if ($filters['status']) $shipments->where('status', $filters['status']);
  662. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  663. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  664. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  665. }
  666. public function cellularMeasurements(Request $request)
  667. {
  668. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  669. return view('app.practice-management.cellular-measurements', compact('measurements'));
  670. }
  671. // v2 supply-orders & shipments management (wh)
  672. public function supplyOrdersReadyToShip(Request $request)
  673. {
  674. $counts = $this->getSupplyOrderCounts();
  675. $supplyOrders = SupplyOrder
  676. ::where('is_cleared_for_shipment', true)
  677. ->where('is_cancelled', false)
  678. ->whereNull('shipment_id')
  679. ->join('client', 'client.id', '=', 'supply_order.client_id')
  680. ->orderBy('client.name_last', 'ASC')
  681. ->orderBy('client.name_first', 'ASC')
  682. ->orderBy('supply_order.client_id', 'ASC')
  683. ->orderBy('supply_order.mailing_address_full', 'ASC')
  684. ->orderBy('supply_order.created_at', 'ASC')
  685. ->select('supply_order.*')
  686. ->paginate();
  687. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  688. }
  689. public function supplyOrdersShipmentUnderway(Request $request)
  690. {
  691. $counts = $this->getSupplyOrderCounts();
  692. $supplyOrders = SupplyOrder
  693. ::where('is_cancelled', false)
  694. ->whereNotNull('shipment_id')
  695. ->orderBy('client_id', 'ASC')
  696. ->orderBy('mailing_address_full', 'ASC')
  697. ->orderBy('created_at', 'ASC')
  698. ->paginate();
  699. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  700. }
  701. private function getSupplyOrderCounts()
  702. {
  703. return [
  704. "supplyOrders" => SupplyOrder::count(),
  705. "supplyOrdersReadyToShip" => SupplyOrder
  706. ::where('is_cleared_for_shipment', true)
  707. ->where('is_cancelled', false)
  708. ->whereNull('shipment_id')->count(),
  709. "supplyOrdersShipmentUnderway" => SupplyOrder
  710. ::where('is_cancelled', false)
  711. ->whereNotNull('shipment_id')->count()
  712. ];
  713. }
  714. public function shipmentsReadyToPrint(Request $request)
  715. {
  716. $counts = $this->getShipmentCounts();
  717. $shipments = Shipment
  718. ::where('is_cancelled', false)
  719. ->where('status', 'CREATED')
  720. ->orderBy('created_at', 'ASC')
  721. ->paginate();
  722. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  723. }
  724. public function shipmentsShipmentUnderway(Request $request)
  725. {
  726. $counts = $this->getShipmentCounts();
  727. $shipments = Shipment
  728. ::where('is_cancelled', false)
  729. ->where('status', 'PRINTED')
  730. ->orderBy('created_at', 'ASC')
  731. ->paginate();
  732. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  733. }
  734. private function getShipmentCounts()
  735. {
  736. return [
  737. "shipments" => Shipment::count(),
  738. "shipmentsReadyToPrint" => Shipment
  739. ::where('is_cancelled', false)
  740. ->where('status', 'CREATED')
  741. ->count(),
  742. "shipmentsWaitingForPicker" => Shipment
  743. ::where('is_cancelled', false)
  744. ->where('status', 'PRINTED')
  745. ->count()
  746. ];
  747. }
  748. public function shipment(Request $request, Shipment $shipment)
  749. {
  750. return view('app.practice-management.shipment', compact('shipment'));
  751. }
  752. public function shipmentsMultiPrint(Request $request, $ids)
  753. {
  754. $ids = array_map(function ($_x) {
  755. return intval($_x);
  756. }, explode("|", $ids));
  757. $shipments = Shipment::whereIn('id', $ids)->get();
  758. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  759. }
  760. public function patientClaimSummary(Request $request, $proUid = null)
  761. {
  762. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  763. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  764. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  765. $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;
  766. $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;
  767. $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;
  768. $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;
  769. $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;
  770. $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;
  771. $performerPro = $this->performer->pro;
  772. $allPros = [];
  773. if ($performerPro->pro_type == 'ADMIN') {
  774. $allPros = Pro::all();
  775. } else {
  776. $allPros = [$performerPro];
  777. }
  778. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  779. $patientsQuery = Client::where('is_dummy', '=', false)
  780. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  781. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  782. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  783. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  784. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  785. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  786. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  787. if ($proUid) {
  788. $mcpPro = Pro::where('uid', $proUid)->first();
  789. if ($mcpPro) {
  790. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  791. }
  792. }
  793. $patients = $patientsQuery->paginate(50);
  794. $data = [
  795. 'patients' => $patients,
  796. 'proUid' => $proUid,
  797. 'allPros' => $allPros,
  798. 'notesTotal' => $notesTotal,
  799. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  800. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  801. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  802. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  803. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  804. 'patientsTotal' => $patientsTotal,
  805. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  806. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  807. ];
  808. return view('app.practice-management.patient-claim-summary', $data);
  809. }
  810. }