PracticeManagementController.php 33 KB

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