PracticeManagementController.php 32 KB

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