PracticeManagementController.php 35 KB

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