PracticeManagementController.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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)->where('bill_service_type', '<>', 'CARE_MONTH');
  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. if ($performerPro->pro_type == 'ADMIN') {
  454. $targetPro = Pro::where('uid', $proUid)->first();
  455. } else {
  456. $targetPro = $performerPro;
  457. }
  458. $bills = Bill::where('is_cancelled', false);
  459. if(!$request->input('t') || $request->input('t') === 'hcp') {
  460. $bills = $bills
  461. ->where('has_hcp_been_paid', false)
  462. ->where('is_signed_by_hcp', true);
  463. if ($targetPro) {
  464. $bills = $bills->where('hcp_pro_id', $targetPro->id);
  465. }
  466. }
  467. else if($request->input('t') === 'na') {
  468. $bills = $bills
  469. ->where('has_generic_pro_been_paid', false)
  470. ->where('is_signed_by_generic_pro', true);
  471. if ($targetPro) {
  472. $bills = $bills->where('generic_pro_id', $targetPro->id);
  473. }
  474. }
  475. $filter = $request->input('f');
  476. switch ($filter) {
  477. case 'verified':
  478. $bills = $bills->where('is_verified', true);
  479. break;
  480. case 'not-verified':
  481. $bills = $bills->where('is_verified', false);
  482. break;
  483. }
  484. $bills = $bills->orderBy('effective_date', 'desc')->paginate();
  485. $viewData = [
  486. 'bills' => $bills,
  487. 'targetPro' => $targetPro,
  488. 'performerPro' => $performerPro,
  489. 'proUid' => $proUid
  490. ];
  491. return view('app.practice-management.processing-bill-matrix', $viewData);
  492. }
  493. public function hcpBillMatrix(Request $request, $proUid = null)
  494. {
  495. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  496. $performerPro = $this->performer->pro;
  497. $targetPro = null;
  498. $allPros = [];
  499. $expectedForHcp = null;
  500. if ($performerPro->pro_type == 'ADMIN') {
  501. $allPros = Pro::all();
  502. $targetPro = Pro::where('uid', $proUid)->first();
  503. } else {
  504. $targetPro = $performerPro;
  505. }
  506. $rows = [];
  507. if ($targetPro) {
  508. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report WHERE hcp_pro_id = :targetProID"), ['targetProID' => $targetPro->id]);
  509. } else {
  510. $rows = DB::select(DB::raw("SELECT * FROM aemish_bill_report"));
  511. }
  512. return view('app.practice-management.hcp-bill-matrix', compact('rows', 'allPros', 'expectedForHcp', 'targetPro', 'proUid'));
  513. }
  514. public function billingManager(Request $request, $proUid = null)
  515. {
  516. $proUid = $proUid ? $proUid : $request->get('pro-uid');
  517. $performerPro = $this->performer->pro;
  518. $targetPro = null;
  519. $allPros = [];
  520. $expectedForHcp = null;
  521. if ($performerPro->pro_type == 'ADMIN') {
  522. $allPros = Pro::all();
  523. $targetPro = Pro::where('uid', $proUid)->first();
  524. } else {
  525. $targetPro = $performerPro;
  526. }
  527. $notes = [];
  528. if ($targetPro) {
  529. $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;
  530. $notes = Note::where('hcp_pro_id', $targetPro->id);
  531. } else {
  532. $notes = Note::where('id', '>', 0);
  533. }
  534. if($request->input('date')) {
  535. $notes = $notes->where('effective_dateest', $request->input('date'));
  536. }
  537. $filters = [];
  538. $filters['bills_created'] = $request->input('bills_created');
  539. $filters['is_billing_marked_done'] = $request->input('is_billing_marked_done');
  540. $filters['bills_resolved'] = $request->input('bills_resolved');
  541. $filters['bills_closed'] = $request->input('bills_closed');
  542. $filters['claims_created'] = $request->input('claims_created');
  543. $filters['claims_closed'] = $request->input('claims_closed');
  544. if ($filters['bills_created']) {
  545. $notes->where(
  546. 'bill_total_expected',
  547. ($filters['bills_created'] === 'yes' ? '>' : '<='),
  548. 0);
  549. }
  550. if ($filters['is_billing_marked_done']) {
  551. $notes->where(
  552. 'is_billing_marked_done',
  553. ($filters['is_billing_marked_done'] === 'yes' ? '=' : '!='),
  554. true);
  555. }
  556. if ($filters['bills_resolved']) {
  557. $notes->whereRaw('(SELECT count(id) FROM bill WHERE note_id = note.id) > 0'); // have bills
  558. if ($filters['bills_resolved'] === 'yes') {
  559. $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');
  560. } elseif ($filters['bills_resolved'] === 'no') {
  561. $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');
  562. }
  563. }
  564. if ($filters['bills_closed']) {
  565. $notes->where(
  566. 'is_bill_closed',
  567. ($filters['bills_closed'] === 'yes' ? '=' : '!='),
  568. true);
  569. }
  570. if ($filters['claims_created']) {
  571. $notes->where(
  572. 'claim_total_expected',
  573. ($filters['claims_created'] === 'yes' ? '>' : '<='),
  574. 0);
  575. }
  576. if ($filters['claims_closed']) {
  577. $notes->where(
  578. 'is_claim_closed',
  579. ($filters['claims_closed'] === 'yes' ? '=' : '!='),
  580. true);
  581. }
  582. $notes = $notes->orderBy('effective_dateest', 'desc')->paginate(10);
  583. return view('app.practice-management.billing-manager', compact('notes', 'allPros', 'expectedForHcp', 'targetPro', 'proUid', 'filters'));
  584. }
  585. public function billMatrix(Request $request)
  586. {
  587. $bClients = [];
  588. $bHCPPros = [];
  589. $bNAPros = [];
  590. $filters = [];
  591. $filters['client'] = $request->input('client');
  592. $filters['service'] = $request->input('service');
  593. $filters['hcp'] = $request->input('hcp');
  594. $filters['hcp_paid'] = $request->input('hcp_paid');
  595. $filters['expected_op'] = $request->input('expected_op');
  596. $filters['expected_value'] = $request->input('expected_value');
  597. $filters['paid_op'] = $request->input('paid_op');
  598. $filters['paid_value'] = $request->input('paid_value');
  599. $filters['bal_post_date_op'] = $request->input('bal_post_date_op');
  600. $filters['bal_post_date_value'] = $request->input('bal_post_date_value');
  601. $filters['hcp_sign'] = $request->input('hcp_sign');
  602. $filters['verified'] = $request->input('verified');
  603. $filters['cancelled'] = $request->input('cancelled');
  604. $bills = Bill::orderBy('effective_date')->paginate();
  605. return view('app.practice-management.bill-matrix', compact('bills', 'bClients', 'bHCPPros', 'filters'));
  606. }
  607. public function medicarePartBClaims(Request $request)
  608. {
  609. $medicarePartBOnly = $request->get("medicare_part_b");
  610. $allClaims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->get();
  611. //Only medicare claims
  612. $claims = [];
  613. foreach ($allClaims as $claim) {
  614. if ($claim->client != null && $claim->client->is_part_b_primary == 'YES' && !$claim->edi) {
  615. $claims[] = $claim;
  616. }
  617. }
  618. $claimEDIs = ClaimEDI::all();
  619. return view('app.practice-management.medicare-partb-claims', compact('claims', 'claimEDIs'));
  620. }
  621. // Generate PDF
  622. public function downloadClaims()
  623. {
  624. $claims = Claim::where('was_submitted', false)->orWhere('was_submitted', null)->orderBy('created_at', 'desc')->limit(100)->get();
  625. view()->share('claims', $claims);
  626. $pdf = PDF::loadView('app.practice-management.claims-pdf', $claims);
  627. return $pdf->download('pdf_file.pdf');
  628. }
  629. public function tickets(Request $request, $proUid = null)
  630. {
  631. $tickets = Ticket::orderBy('created_at', 'desc')->paginate();
  632. return view('app.practice-management.tickets', compact('tickets'));
  633. }
  634. public function supplyOrders(Request $request)
  635. {
  636. // counts
  637. $counts = $this->getSupplyOrderCounts();
  638. // so clients
  639. $soClientIDs = DB::table('supply_order')->select('client_id')->distinct()->get()->toArray();
  640. $soClientIDs = array_map(function ($_x) {
  641. return $_x->client_id;
  642. }, $soClientIDs);
  643. $soClients = Client::whereIn('id', $soClientIDs)->get();
  644. // so products
  645. $soProductIDs = DB::table('supply_order')->select('product_id')->distinct()->get()->toArray();
  646. $soProductIDs = array_map(function ($_x) {
  647. return $_x->product_id;
  648. }, $soProductIDs);
  649. $soProducts = Product::whereIn('id', $soProductIDs)->get();
  650. $filters = [];
  651. $filters['client'] = $request->input('client');
  652. $filters['product'] = $request->input('product');
  653. $filters['reason'] = $request->input('reason');
  654. $filters['cu_memo'] = $request->input('cu_memo');
  655. $filters['pro_sign'] = $request->input('pro_sign');
  656. $filters['client_sign'] = $request->input('client_sign');
  657. $filters['shipment'] = $request->input('shipment');
  658. $filters['lot_number'] = $request->input('lot_number');
  659. $filters['imei'] = $request->input('imei');
  660. $filters['cancelled'] = $request->input('cancelled');
  661. $supplyOrders = SupplyOrder::where('id', '>', 0);
  662. // apply filters
  663. if ($filters['client']) $supplyOrders->where('client_id', $filters['client']);
  664. if ($filters['product']) $supplyOrders->where('product_id', $filters['product']);
  665. if ($filters['reason']) $supplyOrders->where('reason', 'ILIKE', '%' . $filters['reason'] . '%');
  666. if ($filters['cu_memo']) $supplyOrders->where('cu_memo', 'ILIKE', '%' . $filters['cu_memo'] . '%');
  667. if ($filters['pro_sign']) $supplyOrders->where('is_signed_by_pro', ($filters['pro_sign'] === 'signed'));
  668. if ($filters['client_sign']) {
  669. if ($filters['client_sign'] === 'signed')
  670. $supplyOrders->where('is_signed_by_client', true);
  671. elseif ($filters['client_sign'] === 'waived')
  672. $supplyOrders->where('is_client_signature_waived', true);
  673. else
  674. $supplyOrders->where('is_client_signature_waived', false)->where('is_signed_by_client', false);
  675. }
  676. if ($filters['shipment']) {
  677. if ($filters['shipment'] === 'not_cleared_for_shipment')
  678. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', false);
  679. elseif ($filters['shipment'] === 'cleared_for_shipment')
  680. $supplyOrders->whereNull('shipment_id')->where('is_cleared_for_shipment', true);
  681. else
  682. $supplyOrders
  683. ->whereNotNull('shipment_id')
  684. ->whereRaw('(SELECT status FROM shipment WHERE id = shipment_id LIMIT 1) = ?', [$filters['shipment']]);
  685. }
  686. if ($filters['lot_number']) $supplyOrders->where('lot_number', 'ILIKE', '%' . $filters['lot_number'] . '%');
  687. if ($filters['imei']) $supplyOrders->where('imei', 'ILIKE', '%' . $filters['imei'] . '%');
  688. if ($filters['cancelled']) $supplyOrders->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  689. $supplyOrders = $supplyOrders->orderBy('created_at', 'desc')->paginate();
  690. return view('app.practice-management.supply-orders',
  691. compact('supplyOrders', 'filters',
  692. 'soClients', 'soProducts', 'counts'
  693. )
  694. );
  695. }
  696. public function shipments(Request $request, $filter = null)
  697. {
  698. // counts
  699. $counts = $this->getShipmentCounts();
  700. // so clients
  701. $shClientIDs = DB::table('shipment')->select('client_id')->distinct()->get()->toArray();
  702. $shClientIDs = array_map(function ($_x) {
  703. return $_x->client_id;
  704. }, $shClientIDs);
  705. $shClients = Client::whereIn('id', $shClientIDs)->get();
  706. $shipments = Shipment::where('id', '>', 0);
  707. $filters = [];
  708. $filters['client'] = $request->input('client');
  709. $filters['courier'] = $request->input('courier');
  710. $filters['tracking_num'] = $request->input('tracking_num');
  711. $filters['label'] = $request->input('label');
  712. $filters['status'] = $request->input('status');
  713. $filters['cancelled'] = $request->input('cancelled');
  714. if ($filters['client']) $shipments->where('client_id', $filters['client']);
  715. if ($filters['courier']) $shipments->where('courier', 'ILIKE', '%' . $filters['courier'] . '%');
  716. if ($filters['tracking_num']) $shipments->where('tracking_number', 'ILIKE', '%' . $filters['tracking_num'] . '%');
  717. if ($filters['label']) {
  718. if ($filters['label'] === 'yes')
  719. $shipments->whereNotNull('label_system_file_id');
  720. else
  721. $shipments->whereNull('label_system_file_id');
  722. }
  723. if ($filters['status']) $shipments->where('status', $filters['status']);
  724. if ($filters['cancelled']) $shipments->where('is_cancelled', ($filters['cancelled'] === 'cancelled'));
  725. $shipments = $shipments->orderBy('created_at', 'desc')->paginate();
  726. return view('app.practice-management.shipments', compact('shipments', 'filters', 'shClients', 'counts'));
  727. }
  728. public function cellularMeasurements(Request $request)
  729. {
  730. $measurements = Measurement::orderBy('ts', 'desc')->whereNotNull('ts')->paginate();
  731. return view('app.practice-management.cellular-measurements', compact('measurements'));
  732. }
  733. // v2 supply-orders & shipments management (wh)
  734. public function supplyOrdersReadyToShip(Request $request)
  735. {
  736. $counts = $this->getSupplyOrderCounts();
  737. $supplyOrders = SupplyOrder
  738. ::where('is_cleared_for_shipment', true)
  739. ->where('is_cancelled', false)
  740. ->whereNull('shipment_id')
  741. ->join('client', 'client.id', '=', 'supply_order.client_id')
  742. ->orderBy('client.name_last', 'ASC')
  743. ->orderBy('client.name_first', 'ASC')
  744. ->orderBy('supply_order.client_id', 'ASC')
  745. ->orderBy('supply_order.mailing_address_full', 'ASC')
  746. ->orderBy('supply_order.created_at', 'ASC')
  747. ->select('supply_order.*')
  748. ->paginate();
  749. return view('app.practice-management.supply-orders-ready-to-ship', compact('supplyOrders', 'counts'));
  750. }
  751. public function supplyOrdersShipmentUnderway(Request $request)
  752. {
  753. $counts = $this->getSupplyOrderCounts();
  754. $supplyOrders = SupplyOrder
  755. ::where('is_cancelled', false)
  756. ->whereNotNull('shipment_id')
  757. ->orderBy('client_id', 'ASC')
  758. ->orderBy('mailing_address_full', 'ASC')
  759. ->orderBy('created_at', 'ASC')
  760. ->paginate();
  761. return view('app.practice-management.supply-orders-shipment-underway', compact('supplyOrders', 'counts'));
  762. }
  763. public function supplyOrdersHanging(Request $request)
  764. {
  765. $counts = $this->getSupplyOrderCounts();
  766. $supplyOrders = SupplyOrder
  767. ::select('supply_order.*')
  768. ->leftJoin('shipment', function($join) {
  769. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  770. })
  771. ->where('shipment.status', 'CANCELLED')
  772. ->where('supply_order.is_cancelled', false)
  773. ->orderBy('supply_order.client_id', 'ASC')
  774. ->orderBy('supply_order.mailing_address_full', 'ASC')
  775. ->orderBy('supply_order.created_at', 'ASC')
  776. ->paginate();
  777. return view('app.practice-management.supply-orders-hanging', compact('supplyOrders', 'counts'));
  778. }
  779. public function supplyOrdersCancelledButUnacknowledged(Request $request)
  780. {
  781. $supplyOrders = SupplyOrder::where('signed_by_pro_id', $this->performer()->pro->id)
  782. ->where('is_cancelled', true)
  783. ->where('is_cancellation_acknowledged', false)
  784. ->orderBy('created_at', 'desc')
  785. ->paginate();
  786. return view('app.practice-management.supply-orders-cancelled-but-unacknowledged', compact('supplyOrders'));
  787. }
  788. public function supplyOrdersUnsigned(Request $request)
  789. {
  790. $supplyOrders = SupplyOrder
  791. ::where('is_cancelled', false)
  792. ->where('is_signed_by_pro', false)
  793. ->whereRaw('created_by_session_id IN (SELECT id FROM app_session WHERE pro_id = ?)', [$this->performer()->pro->id])
  794. ->orderBy('created_at', 'desc')
  795. ->paginate();
  796. return view('app.practice-management.supply-orders-unsigned', compact('supplyOrders'));
  797. }
  798. private function getSupplyOrderCounts()
  799. {
  800. return [
  801. "supplyOrders" => SupplyOrder::count(),
  802. "supplyOrdersReadyToShip" => SupplyOrder
  803. ::where('is_cleared_for_shipment', true)
  804. ->where('is_cancelled', false)
  805. ->whereNull('shipment_id')->count(),
  806. "supplyOrdersShipmentUnderway" => SupplyOrder
  807. ::where('is_cancelled', false)
  808. ->whereNotNull('shipment_id')->count(),
  809. "supplyOrdersHanging" => SupplyOrder
  810. ::leftJoin('shipment', function($join) {
  811. $join->on('supply_order.shipment_id', '=', 'shipment.id');
  812. })
  813. ->where('shipment.status', 'CANCELLED')
  814. ->where('supply_order.is_cancelled', false)
  815. ->count(),
  816. ];
  817. }
  818. public function shipmentsReadyToPrint(Request $request)
  819. {
  820. $counts = $this->getShipmentCounts();
  821. $shipments = Shipment
  822. ::where('is_cancelled', false)
  823. ->where('status', 'CREATED')
  824. ->orderBy('created_at', 'ASC')
  825. ->paginate();
  826. return view('app.practice-management.shipments-ready-to-print', compact('shipments', 'counts'));
  827. }
  828. public function shipmentsShipmentUnderway(Request $request)
  829. {
  830. $counts = $this->getShipmentCounts();
  831. $shipments = Shipment
  832. ::where('is_cancelled', false)
  833. ->where('status', 'PRINTED')
  834. ->orderBy('created_at', 'ASC')
  835. ->paginate();
  836. return view('app.practice-management.shipments-waiting-for-picker', compact('shipments', 'counts'));
  837. }
  838. private function getShipmentCounts()
  839. {
  840. return [
  841. "shipments" => Shipment::count(),
  842. "shipmentsReadyToPrint" => Shipment
  843. ::where('is_cancelled', false)
  844. ->where('status', 'CREATED')
  845. ->count(),
  846. "shipmentsWaitingForPicker" => Shipment
  847. ::where('is_cancelled', false)
  848. ->where('status', 'PRINTED')
  849. ->count()
  850. ];
  851. }
  852. public function shipment(Request $request, Shipment $shipment)
  853. {
  854. return view('app.practice-management.shipment', compact('shipment'));
  855. }
  856. public function shipmentsMultiPrint(Request $request, $ids)
  857. {
  858. $ids = array_map(function ($_x) {
  859. return intval($_x);
  860. }, explode("|", $ids));
  861. $shipments = Shipment::whereIn('id', $ids)->get();
  862. return view('app.practice-management.shipments-multi-print', compact('shipments'));
  863. }
  864. public function patientClaimSummary(Request $request, $proUid = null)
  865. {
  866. $notesTotal = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE"))[0]->count;
  867. $notesTotalWithBillingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_bill_closed IS TRUE"))[0]->count;
  868. $notesTotalWithClaimingClosed = DB::select(DB::raw("SELECT COUNT(*) FROM note WHERE is_cancelled IS NOT TRUE AND is_claim_closed IS TRUE"))[0]->count;
  869. $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;
  870. $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;
  871. $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;
  872. $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;
  873. $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;
  874. $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;
  875. $performerPro = $this->performer->pro;
  876. $allPros = [];
  877. if ($performerPro->pro_type == 'ADMIN') {
  878. $allPros = Pro::all();
  879. } else {
  880. $allPros = [$performerPro];
  881. }
  882. //Patient | MCP | # Notes Total | # Notes without Billing Closed | # Notes without Claiming Closed
  883. $patientsQuery = Client::where('is_dummy', '=', false)
  884. ->whereNull('shadow_pro_id')
  885. ->select('id', 'uid', 'name_first', 'name_last', 'mcp_pro_id', 'is_part_b_primary', 'medicare_advantage_plan',
  886. DB::raw("(SELECT name_first||' '||name_last FROM pro where pro.id = client.mcp_pro_id) as mcp"),
  887. DB::raw("(SELECT uid FROM pro where pro.id = mcp_pro_id) as mcp_pro_uid"),
  888. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id) as notes_total"),
  889. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_bill_closed IS NOT true) as notes_without_billing_closed"),
  890. DB::raw("(SELECT COUNT(*) FROM note where note.client_id = client.id AND is_claim_closed IS NOT true) as notes_without_claiming_closed")
  891. )->orderBy('is_part_b_primary', 'asc')->orderBy('notes_without_claiming_closed', 'desc');
  892. if ($proUid) {
  893. $mcpPro = Pro::where('uid', $proUid)->first();
  894. if ($mcpPro) {
  895. $patientsQuery->where('client.mcp_pro_id', '=', $mcpPro->id);
  896. }
  897. }
  898. $patientsQuery->whereRaw('(SELECT COUNT(*) FROM note where note.client_id = client.id) > 0');
  899. $patientsQuery->orderBy('notes_total', 'DESC');
  900. $patients = $patientsQuery->paginate(50);
  901. $data = [
  902. 'patients' => $patients,
  903. 'proUid' => $proUid,
  904. 'allPros' => $allPros,
  905. 'notesTotal' => $notesTotal,
  906. 'notesTotalWithBillingClosed' => $notesTotalWithBillingClosed,
  907. 'notesTotalWithClaimingClosed' => $notesTotalWithClaimingClosed,
  908. 'notes3rdPartyTotal' => $notes3rdPartyTotal,
  909. 'notes3rdPartyTotalWithBillingClosed' => $notes3rdPartyTotalWithBillingClosed,
  910. 'notes3rdPartyTotalWithClaimingClosed' => $notes3rdPartyTotalWithClaimingClosed,
  911. 'patientsTotal' => $patientsTotal,
  912. 'patientsTotalWithBillingClosed' => $patientsTotalWithBillingClosed,
  913. 'patientsTotalWithClaimingClosed' => $patientsTotalWithClaimingClosed
  914. ];
  915. return view('app.practice-management.patient-claim-summary', $data);
  916. }
  917. public function claims(Request $request){
  918. $status = $request->get('status');
  919. $claims = [];
  920. if(!$status){
  921. $claims = Claim::orderBy('created_at', 'DESC')->paginate();
  922. }else{
  923. $claims = Claim::where('status', $status)->orderBy('created_at', 'DESC')->paginate();
  924. }
  925. return view('app.practice-management.claims', compact('claims', 'status'));
  926. }
  927. public function processClaims(Request $request) {
  928. $status = '';
  929. $q = $request->input('q') ? $request->input('q') : '';
  930. $from = $request->input('from') ? $request->input('from') : '1900-01-01';
  931. $to = $request->input('to') ? $request->input('to') : '2100-01-01';
  932. $params = [
  933. 'q' => '%' . $q . '%',
  934. 'from' => $from,
  935. 'to' => $to
  936. ];
  937. $hcpPro = $request->input('hcp') ? Pro::where('uid', $request->input('hcp'))->first() : null;
  938. if($hcpPro) {
  939. $params['hcp'] = $hcpPro->id;
  940. }
  941. $claims = DB::select(DB::raw("
  942. SELECT claim.uid AS uid,
  943. DATE(claim.created_at) AS created,
  944. claim.status,
  945. client.uid AS client_uid,
  946. client.cell_number AS client_phone,
  947. (client.name_last || ' ' || client.name_first) AS client ,
  948. client.chart_number AS client_chart_number,
  949. cp.id AS claim_pro_id,
  950. (cp.name_last || ' ' || cp.name_first) AS claim_pro,
  951. sp.id AS status_pro_id,
  952. (sp.name_last || ' ' || sp.name_first) AS status_pro,
  953. note.uid AS note_uid,
  954. note.method,
  955. note.new_or_fu_or_na,
  956. -- claim.status_updated_at,
  957. (DATE(claim.status_updated_at) || ' ' ||
  958. LPAD(EXTRACT(hour FROM claim.status_updated_at)::text, 2, '0') || ':' ||
  959. LPAD(EXTRACT(minute FROM claim.status_updated_at)::text, 2, '0')) AS status_updated_at,
  960. (SELECT string_agg(claim_line.cpt, ', ') FROM claim_line where claim_id = claim.id) AS cpts,
  961. (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,
  962. ROUND(claim.expected_total, 3) as expected_total
  963. FROM claim
  964. join client on claim.client_id = client.id
  965. join pro cp on claim.pro_id = cp.id
  966. left join note on claim.note_id = note.id
  967. left join app_session on claim.status_updated_by_session_id = app_session.id
  968. left join pro sp on app_session.pro_id = sp.id
  969. --WHERE claim.status IS NULL OR claim.status = 'NEW'
  970. WHERE (claim.status is NULL OR claim.status NOT IN ('CANCELLED', 'ABANDONED'))
  971. -- AND claim.current_version_id IS NOT NULL
  972. AND (client.name_first ILIKE :q OR
  973. client.name_last ILIKE :q OR
  974. client.chart_number ILIKE :q OR
  975. client.mcn ILIKE :q)
  976. AND (claim.created_at >= :from AND claim.created_at <= :to)
  977. " . ($hcpPro ? "AND claim.pro_id = :hcp" : '') . "
  978. AND claim.id IN (SELECT mb_claim.claim_id FROM mb_claim)
  979. ORDER BY claim.created_at ASC
  980. --OFFSET 0 LIMIT 15
  981. "), $params);
  982. if($request->input('json')) {
  983. return json_encode($claims);
  984. }
  985. return view('app.practice-management.process-claims', compact('claims', 'status'));
  986. }
  987. public function processNotes(Request $request) {
  988. $mode = $request->input('mode') ? $request->input('mode') : '1';
  989. if(($mode < 1 || $mode > 5) && $mode != 8) $mode = 1;
  990. DB::enableQueryLog(); // Enable query log
  991. $test = Note::where('is_cancelled', false)
  992. ->where('is_signed_by_hcp', true)
  993. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  994. ->whereNull('current_note_pickup_for_processing_id')
  995. ->where('is_billing_marked_done', true)
  996. ->where('is_bill_closed', true)
  997. ->where('is_claim_closed', true)
  998. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  999. ->count();
  1000. // var_dump($test);
  1001. // dd(DB::getQueryLog()); // Show results of log
  1002. // exit;
  1003. $counts = [
  1004. "picked" => Note::where('is_cancelled', false)
  1005. ->whereNotNull('current_note_pickup_for_processing_id')
  1006. ->count(),
  1007. "bad" => Note::where('is_cancelled', false)
  1008. ->whereRaw("((detail_json)::json->>'isBad' = 'true')")
  1009. ->count(),
  1010. // not yet signed
  1011. "mode-1" => Note::where('is_cancelled', false)
  1012. ->where('is_signed_by_hcp', false)
  1013. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1014. // ->whereNull('current_note_pickup_for_processing_id')
  1015. ->count(),
  1016. // billing not marked done
  1017. "mode-2" => Note::where('is_cancelled', false)
  1018. ->where('is_signed_by_hcp', true)
  1019. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1020. // ->whereNull('current_note_pickup_for_processing_id')
  1021. ->where('is_billing_marked_done', false)
  1022. ->count(),
  1023. // billing not closed
  1024. "mode-3" => Note::where('is_cancelled', false)
  1025. ->where('is_signed_by_hcp', true)
  1026. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1027. // ->whereNull('current_note_pickup_for_processing_id')
  1028. ->where('is_billing_marked_done', true)
  1029. ->where('is_bill_closed', false)
  1030. ->count(),
  1031. // claiming not closed
  1032. "mode-4" => Note::where('is_cancelled', false)
  1033. ->where('is_signed_by_hcp', true)
  1034. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1035. // ->whereNull('current_note_pickup_for_processing_id')
  1036. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  1037. ->where('is_billing_marked_done', true)
  1038. ->where('is_bill_closed', true)
  1039. ->where('is_claim_closed', false)
  1040. ->count(),
  1041. // has unsubmitted claims
  1042. "mode-5" => Note::where('is_cancelled', false)
  1043. /*->where('is_signed_by_hcp', true)
  1044. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1045. ->whereNull('current_note_pickup_for_processing_id')
  1046. ->where('is_billing_marked_done', true)
  1047. ->where('is_bill_closed', true)
  1048. ->where('is_claim_closed', true)
  1049. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1050. */
  1051. ->where('is_signed_by_hcp', true)
  1052. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1053. // ->whereNull('current_note_pickup_for_processing_id')
  1054. ->where('is_billing_marked_done', true)
  1055. ->where('is_bill_closed', true)
  1056. // ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true')")
  1057. ->where('is_claim_closed', true)
  1058. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0)")
  1059. ->count(),
  1060. // has unsubmitted claims MARKED GOOD!
  1061. "mode-8" => Note::where('is_cancelled', false)
  1062. ->where('is_signed_by_hcp', true)
  1063. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1064. ->whereNull('current_note_pickup_for_processing_id')
  1065. ->where('is_billing_marked_done', true)
  1066. ->where('is_bill_closed', true)
  1067. ->where('is_claim_closed', false)
  1068. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1069. ->count(),
  1070. // all good
  1071. "mode-6" => Note::where('is_cancelled', false)
  1072. ->where('is_signed_by_hcp', true)
  1073. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1074. // ->whereNull('current_note_pickup_for_processing_id')
  1075. ->where('is_billing_marked_done', true)
  1076. ->where('is_bill_closed', true)
  1077. ->where('is_claim_closed', true)
  1078. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id) > 0)")
  1079. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1080. ->count(),
  1081. "mode-7" => Note::where('is_cancelled', false)
  1082. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1083. ->count(),
  1084. ];
  1085. return view('app.practice-management.process-notes', compact('mode', 'counts'));
  1086. }
  1087. public function getNextNote(Request $request, $mode)
  1088. {
  1089. $note = null;
  1090. switch (+$mode) {
  1091. case 1:
  1092. $note = Note::where('is_cancelled', false)
  1093. ->where('is_signed_by_hcp', false)
  1094. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1095. ->whereNull('current_note_pickup_for_processing_id')
  1096. ->orderBy('effective_dateest', 'DESC')
  1097. ->first();
  1098. break;
  1099. case 2:
  1100. $note = Note::where('is_cancelled', false)
  1101. ->where('is_signed_by_hcp', true)
  1102. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1103. ->whereNull('current_note_pickup_for_processing_id')
  1104. ->where('is_billing_marked_done', false)
  1105. ->orderBy('effective_dateest', 'DESC')
  1106. ->first();
  1107. break;
  1108. case 3:
  1109. $note = Note::where('is_cancelled', false)
  1110. ->where('is_signed_by_hcp', true)
  1111. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1112. ->whereNull('current_note_pickup_for_processing_id')
  1113. ->where('is_billing_marked_done', true)
  1114. ->where('is_bill_closed', false)
  1115. ->orderBy('effective_dateest', 'DESC')
  1116. ->first();
  1117. break;
  1118. case 4: // claiming not closed
  1119. DB::enableQueryLog(); // Enable query log
  1120. $note = Note::where('is_cancelled', false)
  1121. ->where('is_signed_by_hcp', true)
  1122. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1123. ->whereNull('current_note_pickup_for_processing_id')
  1124. ->where('is_billing_marked_done', true)
  1125. ->where('is_bill_closed', true)
  1126. ->whereRaw("(((detail_json)::json->>'isGood')::text <> 'true' OR ((detail_json)::json->>'isGood')::text IS NULL)")
  1127. ->where('is_claim_closed', false)
  1128. ->orderBy('effective_dateest', 'DESC')
  1129. ->first();
  1130. // DB::enableQueryLog(); // Enable query log
  1131. // Your Eloquent query executed by using get()
  1132. // dd(DB::getQueryLog()); // Show results of log
  1133. break;
  1134. case 5:
  1135. $note = Note::where('is_cancelled', false)
  1136. ->where('is_signed_by_hcp', true)
  1137. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1138. ->whereNull('current_note_pickup_for_processing_id')
  1139. ->where('is_billing_marked_done', true)
  1140. ->where('is_bill_closed', true)
  1141. ->where('is_claim_closed', true)
  1142. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') > 0")
  1143. ->orderBy('effective_dateest', 'DESC')
  1144. ->first();
  1145. break;
  1146. case 8:
  1147. $note = Note::where('is_cancelled', false)
  1148. ->where('is_signed_by_hcp', true)
  1149. ->whereRaw("((detail_json)::json->>'isGood')::text = 'true'")
  1150. ->whereNull('current_note_pickup_for_processing_id')
  1151. ->where('is_billing_marked_done', true)
  1152. ->where('is_bill_closed', true)
  1153. ->where('is_claim_closed', false)
  1154. ->whereRaw("((SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0)")
  1155. ->orderBy('effective_dateest', 'DESC')
  1156. ->first();
  1157. break;
  1158. }
  1159. if($note) {
  1160. $note->client_uid = $note->client->uid;
  1161. }
  1162. return json_encode($note);
  1163. }
  1164. public function pickedNotes(Request $request) {
  1165. $counts = [
  1166. "unpicked" => Note::where('is_cancelled', false)
  1167. ->whereNull('current_note_pickup_for_processing_id')
  1168. ->count(),
  1169. ];
  1170. $notes = Note::where('is_cancelled', false)
  1171. ->whereNotNull('current_note_pickup_for_processing_id')
  1172. ->orderBy('effective_dateest', 'ASC')
  1173. ->paginate();
  1174. return view('app.practice-management.picked-notes', compact('counts', 'notes'));
  1175. }
  1176. public function badNotes(Request $request) {
  1177. $counts = [
  1178. "unpicked" => Note::where('is_cancelled', false)
  1179. ->whereNull('current_note_pickup_for_processing_id')
  1180. ->count(),
  1181. ];
  1182. $notes = Note::where('is_cancelled', false)
  1183. ->whereRaw("(detail_json)::json->>'isBad' = 'true'")
  1184. ->orderBy('effective_dateest', 'ASC')
  1185. ->paginate();
  1186. return view('app.practice-management.bad-notes', compact('counts', 'notes'));
  1187. }
  1188. public function doneNotes(Request $request) {
  1189. $counts = [
  1190. "unpicked" => Note::where('is_cancelled', false)
  1191. ->whereNull('current_note_pickup_for_processing_id')
  1192. ->count(),
  1193. ];
  1194. $notes = Note::where('is_cancelled', false)
  1195. ->where('is_signed_by_hcp', true)
  1196. ->whereRaw("((detail_json)::json->>'isBad' is null OR ((detail_json)::json->>'isBad')::text != 'true')")
  1197. ->whereNull('current_note_pickup_for_processing_id')
  1198. ->where('is_billing_marked_done', true)
  1199. ->where('is_bill_closed', true)
  1200. ->where('is_claim_closed', true)
  1201. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id) > 0")
  1202. ->whereRaw("(SELECT count(id) FROM claim WHERE note_id = note.id AND is_cancelled IS FALSE AND status != 'CANCELLED' AND status != 'SUBMITTED') = 0")
  1203. ->orderBy('effective_dateest', 'ASC')
  1204. ->paginate();
  1205. return view('app.practice-management.done-notes', compact('counts', 'notes'));
  1206. }
  1207. public function currentMbClaim(Request $request, $claimUid) {
  1208. $claim = Claim::where('uid', $claimUid)->first();
  1209. return json_encode(MBClaim::where('claim_version_id', $claim->currentVersion->id)->first());
  1210. }
  1211. public function currentClaimLines(Request $request, $claimUid) {
  1212. $claim = Claim::where('uid', $claimUid)->first();
  1213. if($request->input('json')) {
  1214. foreach ($claim->lines as $line) {
  1215. $line->expected_total = round($line->expected_total, 3);
  1216. $x = $line->claimLineIcds;
  1217. }
  1218. return json_encode($claim->lines);
  1219. }
  1220. return view('app.practice-management._claim-lines', compact('claim'));
  1221. }
  1222. public function packsMultiPrint(Request $request) {
  1223. $packs = Pack
  1224. ::select('pack.*')
  1225. ->leftJoin('shipment', function($join) {
  1226. $join->on('pack.shipment_id', '=', 'shipment.id');
  1227. })
  1228. ->whereNotIn('shipment.status', ['CANCELLED', 'DISPATCHED'])
  1229. ->where(function ($query) {
  1230. $query->where('pack.status', '<>', 'DELETED')->orWhereNull('pack.status'); // weird, but just the <> isn't working!
  1231. })
  1232. ->whereNotNull('pack.label_system_file_id')
  1233. ->orderBy('pack.created_at', 'ASC')
  1234. ->get();
  1235. return view('app.practice-management.packs-multi-print', compact('packs'));
  1236. }
  1237. public function packsMultiPDF(Request $request, $ids) {
  1238. $ids = array_map(function ($_x) {
  1239. return intval($_x);
  1240. }, explode("|", $ids));
  1241. $packs = Pack::whereIn('id', $ids)->get();
  1242. }
  1243. public function handouts(Request $request) {
  1244. $handouts = Handout::orderBy('display_name')->get();
  1245. return view('app.practice-management.handouts', compact('handouts'));
  1246. }
  1247. private function callJava($request, $endPoint, $data)
  1248. {
  1249. $url = config('stag.backendUrl') . $endPoint;
  1250. $response = Http::asForm()
  1251. ->withHeaders([
  1252. 'sessionKey' => $request->cookie('sessionKey')
  1253. ])
  1254. ->post($url, $data)
  1255. ->body();
  1256. dd($response);
  1257. return $response;
  1258. }
  1259. public function genericBills(Request $request)
  1260. {
  1261. return view('app.practice-management.generic-bills');
  1262. }
  1263. }