Pro.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. <?php
  2. namespace App\Models;
  3. # use Illuminate\Database\Eloquent\Model;
  4. use App\Helpers\TimeLine;
  5. use DateTime;
  6. use Exception;
  7. use Illuminate\Support\Facades\DB;
  8. class Pro extends Model
  9. {
  10. protected $table = 'pro';
  11. public function displayName() {
  12. $name = [];
  13. if(!empty($this->name_display)) return $this->name_display;
  14. if(!empty($this->name_last)) $name[] = $this->name_last;
  15. if(!empty($this->name_first)) $name[] = $this->name_first;
  16. if(!count($name)) {
  17. $name = $this->name_display;
  18. }
  19. else {
  20. $name = implode(", ", $name);
  21. }
  22. return $name;
  23. }
  24. public function initials() {
  25. $characters = [];
  26. if(!empty($this->name_first)) $characters[] = $this->name_first[0];
  27. if(!empty($this->name_last)) $characters[] = $this->name_last[0];
  28. return strtolower(implode("", $characters));
  29. }
  30. public function debitBills()
  31. {
  32. return $this->hasMany(Bill::class, 'debit_pro_id');
  33. }
  34. public function cmBills()
  35. {
  36. return $this->hasMany(Bill::class, 'cm_pro_id');
  37. }
  38. public function hcpBills()
  39. {
  40. return $this->hasMany(Bill::class, 'hcp_pro_id');
  41. }
  42. public function teamsWhereAssistant()
  43. {
  44. return $this->hasMany(ProTeam::class, 'assistant_pro_id', 'id');
  45. }
  46. public function isDefaultNA()
  47. {
  48. return $this->is_considered_for_dna;
  49. }
  50. public function lastPayment() {
  51. return ProTransaction
  52. ::where('pro_id', $this->id)
  53. ->where('plus_or_minus', 'PLUS')
  54. ->orderBy('created_at', 'desc')
  55. ->first();
  56. }
  57. public function hasRates() {
  58. $numRates = ProRate::where('is_active', true)->where('pro_id', $this->id)->count();
  59. return $numRates > 0;
  60. }
  61. public function cmRates() {
  62. return ProRate::distinct('code')
  63. ->where('is_active', true)
  64. ->where('pro_id', $this->id)
  65. ->where('code', 'LIKE', 'CM%')
  66. ->get();
  67. }
  68. public function rmRates() {
  69. return ProRate::distinct('code')
  70. ->where('is_active', true)
  71. ->where('pro_id', $this->id)
  72. ->where('code', 'LIKE', 'RM%')
  73. ->get();
  74. }
  75. public function noteRates() {
  76. return ProRate::distinct('code')
  77. ->where('is_active', true)
  78. ->where('pro_id', $this->id)
  79. ->where('code', 'NOT LIKE', 'CM%')
  80. ->where('code', 'NOT LIKE', 'RM%')
  81. ->where('responsibility', '<>', 'GENERIC')
  82. ->get();
  83. }
  84. public function genericRates() {
  85. return ProRate::distinct('code')
  86. ->where('is_active', true)
  87. ->where('pro_id', $this->id)
  88. ->where('responsibility', 'GENERIC')
  89. ->get();
  90. }
  91. public function recentDebits() {
  92. return ProTransaction
  93. ::where('pro_id', $this->id)
  94. ->where('plus_or_minus', 'PLUS')
  95. ->orderBy('created_at', 'desc')
  96. ->skip(0)->take(4)->get();
  97. }
  98. public function shortcuts() {
  99. return $this->hasMany(ProTextShortcut::class, 'pro_id')->where('is_removed', false);
  100. }
  101. public function allShortcuts() {
  102. $myId = $this->id;
  103. $shortcuts = ProTextShortcut::where('is_removed', false)
  104. ->where(function ($query2) use ($myId) {
  105. $query2
  106. ->where('pro_id', $myId)
  107. ->orWhereNull('pro_id');
  108. })
  109. ->orderBy('shortcut')
  110. ->get();
  111. return $shortcuts;
  112. }
  113. public function noteTemplates() {
  114. return $this->hasMany(NoteTemplatePro::class, 'pro_id')
  115. ->where('is_removed', false)
  116. ->orderBy('position_index', 'asc');
  117. }
  118. public function visitTemplates() {
  119. //TODO: use visit access
  120. return VisitTemplate::where('is_active', true)->get();
  121. }
  122. public function currentWork() {
  123. return ProClientWork::where('pro_id', $this->id)->where('is_active', true)->first();
  124. }
  125. public function isWorkingOnClient($_client) {
  126. $count = ProClientWork::where('pro_id', $this->id)->where('client_id', $_client->id)->where('is_active', true)->count();
  127. return $count > 0;
  128. }
  129. public function canvasCustomItems($_key) {
  130. return ClientCanvasDataCustomItem::where('key', $_key)
  131. ->where('pro_id', $this->id)
  132. ->where('is_removed', false)
  133. ->orderBy('label')
  134. ->get();
  135. }
  136. /**
  137. * @param $_start - YYYY-MM-DD
  138. * @param $_end - YYYY-MM-DD
  139. * @param string $_timezone - defaults to EASTERN
  140. * @param string $_availableBG - defaults to #00a
  141. * @param string $_unavailableBG - defaults to #a00
  142. * @return array
  143. * @throws Exception
  144. */
  145. public function getAvailabilityEvents($_start, $_end, $_timezone = 'EASTERN', $_availableBG = '#00a', $_unavailableBG = '#a00') {
  146. $_start .= ' 00:00:00';
  147. $_end .= ' 23:59:59';
  148. // get availability data
  149. $proGenAvail = ProGeneralAvailability
  150. ::where('is_cancelled', false)
  151. ->where('pro_id', $this->id)
  152. ->get();
  153. $proSpecAvail = ProSpecificAvailability
  154. ::where('is_cancelled', false)
  155. ->where('pro_id', $this->id)
  156. ->where(function ($query) use ($_start, $_end) {
  157. $query
  158. ->where(function ($query2) use ($_start, $_end) {
  159. $query2
  160. ->where('start_time', '>=', $_start)
  161. ->where('start_time', '<=', $_end);
  162. })
  163. ->orWhere(function ($query2) use ($_start, $_end) {
  164. $query2
  165. ->where('end_time', '>=', $_start)
  166. ->where('end_time', '<=', $_end);
  167. });
  168. })
  169. ->get();
  170. $proSpecUnavail = ProSpecificUnavailability
  171. ::where('is_cancelled', false)
  172. ->where('pro_id', $this->id)
  173. ->where(function ($query) use ($_start, $_end) {
  174. $query
  175. ->where(function ($query2) use ($_start, $_end) {
  176. $query2
  177. ->where('start_time', '>=', $_start)
  178. ->where('start_time', '<=', $_end);
  179. })
  180. ->orWhere(function ($query2) use ($_start, $_end) {
  181. $query2
  182. ->where('end_time', '>=', $_start)
  183. ->where('end_time', '<=', $_end);
  184. });
  185. })
  186. ->get();
  187. // default GA
  188. // if no gen avail, assume mon to fri, 9 to 7
  189. /*if (count($proGenAvail) === 0) {
  190. $dayNames = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
  191. foreach ($dayNames as $dayName) {
  192. $item = new \stdClass();
  193. $item->day_of_week = $dayName;
  194. $item->timezone = $_timezone;
  195. $item->start_time = '09:00:00';
  196. $item->end_time = '18:00:00';
  197. $proGenAvail->push($item);
  198. }
  199. }*/
  200. // create timeline
  201. $phpTZ = appTZtoPHPTZ($_timezone);
  202. $phpTZObject = new \DateTimeZone($phpTZ);
  203. $startDate = new \DateTime($_start, $phpTZObject);
  204. $endDate = new \DateTime($_end, $phpTZObject);
  205. $proTimeLine = new TimeLine($startDate, $endDate);
  206. // General availability
  207. $period = new \DatePeriod($startDate, \DateInterval::createFromDateString('1 day'), $endDate);
  208. $days = [];
  209. foreach ($period as $day) {
  210. $days[] = [
  211. "day" => strtoupper($day->format("l")), // SUNDAY, etc.
  212. "date" => $day->format("Y-m-d"), // 2020-10-04, etc.
  213. ];
  214. }
  215. foreach ($days as $day) {
  216. $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
  217. return $record->day_of_week === $day["day"];
  218. });
  219. foreach ($proGenAvailForTheDay as $ga) {
  220. $gaStart = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  221. $parts = explode(":", $ga->start_time);
  222. $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  223. $gaStart->setTimezone($phpTZObject);
  224. $gaEnd = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  225. $parts = explode(":", $ga->end_time);
  226. $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  227. $gaEnd->setTimezone($phpTZObject);
  228. $proTimeLine->addAvailability($gaStart, $gaEnd);
  229. }
  230. }
  231. // specific availability
  232. foreach ($proSpecAvail as $sa) {
  233. $saStart = new \DateTime($sa->start_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  234. $saStart->setTimezone($phpTZObject);
  235. $saEnd = new \DateTime($sa->end_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  236. $saEnd->setTimezone($phpTZObject);
  237. $proTimeLine->addAvailability($saStart, $saEnd);
  238. }
  239. // specific unavailability
  240. foreach ($proSpecUnavail as $sua) {
  241. $suaStart = new \DateTime($sua->start_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  242. $suaStart->setTimezone($phpTZObject);
  243. $suaEnd = new \DateTime($sua->end_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  244. $suaEnd->setTimezone($phpTZObject);
  245. $proTimeLine->removeAvailability($suaStart, $suaEnd);
  246. }
  247. $events = [];
  248. // availability
  249. foreach ($proTimeLine->getAvailable() as $item) {
  250. $eStart = new \DateTime('@' . $item->start);
  251. $eStart->setTimezone($phpTZObject);
  252. $eEnd = new \DateTime('@' . $item->end);
  253. $eEnd->setTimezone($phpTZObject);
  254. $events[] = [
  255. "type" => "availability",
  256. "start" => $eStart->format('Y-m-d H:i:s'),
  257. "end" => $eEnd->format('Y-m-d H:i:s'),
  258. "editable" => false,
  259. "backgroundColor" => $_availableBG
  260. ];
  261. }
  262. // unavailability
  263. foreach ($proTimeLine->getUnavailable() as $item) {
  264. $eStart = new \DateTime('@' . $item->start);
  265. $eStart->setTimezone($phpTZObject);
  266. $eEnd = new \DateTime('@' . $item->end);
  267. $eEnd->setTimezone($phpTZObject);
  268. $events[] = [
  269. "type" => "unavailability",
  270. "start" => $eStart->format('Y-m-d H:i:s'),
  271. "end" => $eEnd->format('Y-m-d H:i:s'),
  272. "editable" => false,
  273. "backgroundColor" => $_unavailableBG
  274. ];
  275. }
  276. return $events;
  277. }
  278. public function getMyClientIds($_search = false) {
  279. $clients = $this->getAccessibleClientsQuery($_search)->get();
  280. $clientIds = [];
  281. foreach($clients as $client){
  282. $clientIds[] = $client->id;
  283. }
  284. return $clientIds;
  285. }
  286. public function favoritesByCategory($_category) {
  287. return ProFavorite::where('pro_id', $this->id)
  288. ->where('is_removed', false)
  289. ->where('category', $_category)
  290. ->orderBy('category', 'asc')
  291. ->orderBy('position_index', 'asc')
  292. ->get();
  293. }
  294. public function favoritesByCategoryDecoded($_category) {
  295. $favorites = ProFavorite::where('pro_id', $this->id)
  296. ->where('is_removed', false)
  297. ->where('category', $_category)
  298. ->orderBy('category', 'asc')
  299. ->orderBy('position_index', 'asc')
  300. ->get();
  301. foreach ($favorites as $favorite) {
  302. if ($favorite->data) {
  303. $favorite->data = json_decode($favorite->data);
  304. }
  305. }
  306. return $favorites;
  307. }
  308. function get_patients_count_as_mcp() {
  309. $query = Client::whereNull('shadow_pro_id');
  310. return $query->where('mcp_pro_id', $this->id)->count();
  311. }
  312. function get_new_patients_awaiting_visit_count_as_mcp() {
  313. $query = Client::whereNull('shadow_pro_id');
  314. return $query->where('mcp_pro_id', $this->id)
  315. ->where('has_mcp_done_onboarding_visit', '!=', 'YES')
  316. ->count();
  317. }
  318. function get_notes_pending_signature_count_as_mcp() {
  319. return Note::where('hcp_pro_id', $this->id)
  320. ->where('is_cancelled', '<>', true)
  321. ->where('is_core_note', '<>', true)
  322. ->where('is_signed_by_hcp', '<>', true)
  323. ->count();
  324. }
  325. function get_notes_pending_billing_count_as_mcp() {
  326. return Note::where('hcp_pro_id', $this->id)
  327. ->where('is_cancelled', '<>', true)
  328. ->where('is_signed_by_hcp', true)
  329. ->where('is_billing_marked_done', '<>', true)
  330. ->count();
  331. }
  332. function get_measurements_awaiting_review_count_as_mcp() {
  333. $result = DB::select(DB::raw("
  334. SELECT SUM(rm_num_measurements_not_stamped_by_mcp) AS count
  335. FROM care_month
  336. WHERE mcp_pro_id = :pro_id
  337. AND rm_num_measurements_not_stamped_by_mcp IS NOT NULL
  338. AND rm_num_measurements_not_stamped_by_mcp > 0;
  339. "), ["pro_id" => $this->id]);
  340. if($result) return $result[0]->count;
  341. }
  342. function get_bills_pending_signature_count_as_mcp(){
  343. $pendingBillsToSign = Bill::where('bill_service_type', '<>', 'CARE_MONTH')->where(function ($query) {
  344. $query->where('hcp_pro_id', $this->id)->where('is_signed_by_hcp', false)->where('is_cancelled', false);
  345. })
  346. ->orWhere(function ($query) {
  347. $query->where('cm_pro_id', $this->id)->where('is_signed_by_cm', false)->where('is_cancelled', false);
  348. })->orWhere(function ($query) {
  349. $query->where('rme_pro_id', $this->id)->where('is_signed_by_rme', false)->where('is_cancelled', false);
  350. })->orWhere(function ($query) {
  351. $query->where('rmm_pro_id', $this->id)->where('is_signed_by_rmm', false)->where('is_cancelled', false);
  352. })->orWhere(function ($query) {
  353. $query->where('generic_pro_id', $this->id)->where('is_signed_by_generic_pro', false)->where('is_cancelled', false);
  354. })->count();
  355. return $pendingBillsToSign;
  356. }
  357. function get_incoming_reports_pending_signature_count_as_mcp() {
  358. return IncomingReport::where('hcp_pro_id', $this->id)
  359. ->where('has_hcp_pro_signed', '<>', true)
  360. ->where('is_entry_error', '<>', true)
  361. ->count();
  362. }
  363. function get_patients_without_appointment_query() {
  364. return Client::where('mcp_pro_id', $this->id)
  365. ->whereNull('today_mcp_appointment_date')
  366. ->where(function($q){
  367. $q->whereNull('next_mcp_appointment_id')
  368. ->orWhere('next_mcp_appointment_date', '<=', DB::raw('NOW()::DATE'));
  369. });
  370. }
  371. function get_patients_without_appointment_for_dna_query() {
  372. return Client::where('default_na_pro_id', $this->id)
  373. ->whereNull('today_mcp_appointment_date')
  374. ->where(function($q){
  375. $q->whereNull('next_mcp_appointment_id')
  376. ->orWhere('next_mcp_appointment_date', '<=', DB::raw('NOW()::DATE'));
  377. });
  378. }
  379. function get_patients_without_appointment_count_as_mcp() {
  380. return $this->get_patients_without_appointment_query()->count();
  381. }
  382. function get_patients_overdue_for_visit_query() {
  383. return Client::where('mcp_pro_id', $this->id)
  384. ->where(function($q){
  385. $q->whereNull('most_recent_completed_mcp_note_id')
  386. ->orWhere('most_recent_completed_mcp_note_date', '<', DB::raw("NOW()::DATE - INTERVAL '45 DAY'"));
  387. });
  388. }
  389. function get_patients_overdue_for_visit_for_dna_query() {
  390. return Client::where('default_na_pro_id', $this->id)
  391. ->where(function($q){
  392. $q->whereNull('most_recent_completed_mcp_note_id')
  393. ->orWhere('most_recent_completed_mcp_note_date', '<', DB::raw("NOW()::DATE - INTERVAL '45 DAY'"));
  394. });
  395. }
  396. function get_patients_overdue_count_as_mcp() {
  397. return $this->get_patients_overdue_for_visit_query()->count();
  398. }
  399. function get_patients_without_remote_measurement_in_48_hours_count_as_mcp() {
  400. return DB::select("SELECT COUNT(*) as count FROM client WHERE ((most_recent_cellular_measurement_at+ interval '48 hour')::timestamp < NOW() OR most_recent_cellular_measurement_id IS NULL) AND client.mcp_pro_id = :mcp_pro_id AND is_active IS TRUE", ['mcp_pro_id'=>$this->id])[0]->count;
  401. }
  402. function get_cancelled_appointments_pending_acknowledgement_count_as_mcp_query() {
  403. // SELECT * FROM appointment WHERE hcp_pro_id = :me.id AND status = 'REJECTED' AND wasAcknowledgedByAppointmentPro IS NOT TRUE;
  404. return Appointment::where('pro_id', $this->id)
  405. ->where('latest_confirmation_decision_enum', 'REJECTED')
  406. ->where('is_decision_acknowledgement_from_appointment_pro_pending', true);
  407. }
  408. function get_cancelled_appointments_pending_acknowledgement_count_as_mcp() {
  409. // SELECT * FROM appointment WHERE hcp_pro_id = :me.id AND status = 'REJECTED' AND wasAcknowledgedByAppointmentPro IS NOT TRUE;
  410. return $this->get_cancelled_appointments_pending_acknowledgement_count_as_mcp_query()->count();
  411. }
  412. function get_cancelled_bills_awaiting_review_count_as_mcp() {
  413. // SELECT * FROM bill WHERE bill_service_type = 'NOTE' AND is_cancelled IS TRUE AND isCancellationAcknowledged IS FALSE;
  414. return Bill::where('hcp_pro_id', $this->id)
  415. ->where('bill_service_type', 'NOTE')
  416. ->where('is_cancelled', true)
  417. ->where('is_cancellation_acknowledged', '<>', true)
  418. ->count();
  419. }
  420. function get_cancelled_supply_orders_awaiting_review_count_as_mcp() {
  421. // SELECT * FROM supply_order WHERE signed_by_pro_id = :me.id AND is_cancelled IS TRUE AND isCancellationAcknowledged IS NOT TRUE;
  422. return SupplyOrder::where('signed_by_pro_id', $this->id)
  423. ->where('is_cancelled', true)
  424. ->where('is_cancellation_acknowledged', '<>', true)
  425. ->count();
  426. }
  427. function get_erx_and_orders_awaiting_signature_count_as_mcp() {
  428. // SELECT * FROM erx WHERE hcp_pro_id = :me.id AND pro_declared_status <> 'CANCELLED' AND hasHcpProSigned IS NOT TRUE;
  429. return Erx::where('hcp_pro_id', $this->id)
  430. ->where('pro_declared_status', '<>', 'CANCELLED')
  431. ->where('has_hcp_pro_signed', '<>', true)
  432. ->count();
  433. }
  434. function get_supply_orders_awaiting_signature_count_as_mcp() {
  435. // SELECT supply_order.id FROM supply_order WHERE signed_by_pro_id IS NOT TRUE AND is_cancelled IS NOT TRUE AND created_by_pro_id = :me.id;
  436. return SupplyOrder::where('created_by_pro_id', $this->id)
  437. ->whereNull('signed_by_pro_id')
  438. ->where('is_cancelled', '<>', true)
  439. ->count();
  440. }
  441. function get_supply_orders_awaiting_shipment_count_as_mcp() {
  442. return SupplyOrder::where('created_by_pro_id', $this->id)
  443. ->where('is_signed_by_pro', true)
  444. ->where('is_cleared_for_shipment', true)
  445. ->whereNull('shipment_id')
  446. ->count();
  447. }
  448. function get_birthdays_today_as_mcp(){
  449. return;
  450. $queryClients = $this->performer()->pro->getAccessibleClientsQuery();
  451. $keyNumbers['patientsHavingBirthdayToday'] = $queryClients
  452. ->whereRaw('EXTRACT(DAY from dob) = ?', [date('d')])
  453. ->whereRaw('EXTRACT(MONTH from dob) = ?', [date('m')])
  454. ->count();
  455. $reimbursement = [];
  456. $reimbursement["currentBalance"] = $performer->pro->balance;
  457. $reimbursement["nextPaymentDate"] = '--';
  458. $lastPayment = ProTransaction::where('pro_id', $performerProID)->where('plus_or_minus', 'PLUS')->orderBy('created_at', 'DESC')->first();
  459. if ($lastPayment) {
  460. $reimbursement["lastPayment"] = $lastPayment->amount;
  461. $reimbursement["lastPaymentDate"] = $lastPayment->created_at;
  462. } else {
  463. $reimbursement["lastPayment"] = '--';
  464. $reimbursement["lastPaymentDate"] = '--';
  465. }
  466. }
  467. public function getAppointmentsPendingStatusChangeAck() {
  468. return Appointment::where('pro_id', $this->id)
  469. ->where('is_status_acknowledgement_from_appointment_pro_pending', true)
  470. //->where('raw_date', '>=', DB::raw('NOW()'))
  471. ->orderBy('raw_date', 'asc')
  472. ->get();
  473. }
  474. public function getAppointmentsPendingDecisionAck() {
  475. return Appointment::where('pro_id', $this->id)
  476. ->where('is_decision_acknowledgement_from_appointment_pro_pending', true)
  477. //->where('raw_date', '>=', DB::raw('NOW()'))
  478. ->orderBy('raw_date', 'asc')
  479. ->get();
  480. }
  481. public function getAppointmentsPendingTimeChangeAck() {
  482. return Appointment::where('pro_id', $this->id)
  483. ->where('is_time_change_acknowledgement_from_appointment_pro_pending', true)
  484. //->where('raw_date', '>=', DB::raw('NOW()'))
  485. ->orderBy('raw_date', 'asc')
  486. ->get();
  487. }
  488. public function getAccessibleClientsQuery($_search = false) {
  489. $proID = $this->id;
  490. $query = Client::whereNull('shadow_pro_id');
  491. if ($this->pro_type === 'ADMIN' && ($_search ? $this->can_see_any_client_via_search : $this->can_see_all_clients_in_list)) {
  492. $query = $query->where('id', '>', 0);
  493. } else {
  494. $query = $query->where(function ($q) use ($proID) {
  495. $q->where('mcp_pro_id', $proID)
  496. ->orWhere('cm_pro_id', $proID)
  497. ->orWhere('rmm_pro_id', $proID)
  498. ->orWhere('rme_pro_id', $proID)
  499. ->orWhere('physician_pro_id', $proID)
  500. ->orWhere('default_na_pro_id', $proID)
  501. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID])
  502. ->orWhereRaw('id IN (SELECT client_id FROM appointment WHERE status NOT IN (\'CANCELLED\') AND pro_id = ?)', [$proID])
  503. ->orWhereRaw('id IN (SELECT mcp_pro_id FROM client_program WHERE client_id = client.id AND is_active = TRUE)')
  504. ->orWhereRaw('id IN (SELECT manager_pro_id FROM client_program WHERE client_id = client.id AND is_active = TRUE)')
  505. ->orWhereRaw('id IN (SELECT client_id FROM note WHERE ally_pro_id = ? AND is_cancelled = FALSE)', [$proID]);;
  506. });
  507. }
  508. return $query;
  509. }
  510. public function canAccess($_patientUid) {
  511. $proID = $this->id;
  512. if ($this->pro_type === 'ADMIN') {
  513. return true;
  514. }
  515. $canAccess = Client::select('uid')
  516. ->where('uid', $_patientUid)
  517. ->where(function ($q) use ($proID) {
  518. $q->whereNull('mcp_pro_id')
  519. ->orWhere('mcp_pro_id', $proID)
  520. ->orWhere('cm_pro_id', $proID)
  521. ->orWhere('rmm_pro_id', $proID)
  522. ->orWhere('rme_pro_id', $proID)
  523. ->orWhere('physician_pro_id', $proID)
  524. ->orWhere('default_na_pro_id', $proID)
  525. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID])
  526. ->orWhereRaw('id IN (SELECT client_id FROM appointment WHERE status NOT IN (\'CANCELLED\') AND pro_id = ?)', [$proID])
  527. ->orWhereRaw('id IN (SELECT mcp_pro_id FROM client_program WHERE client_id = client.id AND is_active = TRUE)')
  528. ->orWhereRaw('id IN (SELECT manager_pro_id FROM client_program WHERE client_id = client.id AND is_active = TRUE)')
  529. ->orWhereRaw('id IN (SELECT client_id FROM note WHERE ally_pro_id = ? AND is_cancelled = FALSE)', [$proID]);
  530. })->count();
  531. return !!$canAccess;
  532. }
  533. public function canAddCPMEntryForMeasurement(Measurement $measurement, Pro $pro)
  534. {
  535. // check if client has any programs where this measurement type is allowed
  536. $allowed = false;
  537. $client = $measurement->client;
  538. $clientPrograms = $client->clientPrograms;
  539. if($pro->pro_type !== 'ADMIN') {
  540. $clientPrograms = $clientPrograms->filter(function($_clientProgram) use ($pro) {
  541. return $_clientProgram->manager_pro_id === $pro->id;
  542. });
  543. }
  544. if(count($clientPrograms)) {
  545. foreach ($clientPrograms as $clientProgram) {
  546. if(strpos(strtolower($clientProgram->measurement_labels), '|' . strtolower($measurement->label) . '|') !== FALSE) {
  547. $allowed = true;
  548. break;
  549. }
  550. }
  551. }
  552. return $allowed ? $clientPrograms : FALSE;
  553. }
  554. public function getUnstampedMeasurementsFromCurrentMonth($_countOnly, $skip, $limit)
  555. {
  556. date_default_timezone_set('US/Eastern');
  557. $start = strtotime(date('Y-m-01'));
  558. $end = date_add(date_create(date('Y-m-01')), date_interval_create_from_date_string("1 month"))->getTimestamp();
  559. $start *= 1000;
  560. $end *= 1000;
  561. $measurementsQuery = Measurement::where('measurement.is_active', true)
  562. ->join('client', 'client.id', '=', 'measurement.client_id')
  563. ->whereNotNull('measurement.client_bdt_measurement_id')
  564. ->whereNotNull('measurement.ts')
  565. ->where('measurement.is_cellular_zero', false)
  566. ->where('measurement.ts', '>=', $start)
  567. ->where('measurement.ts', '<', $end)
  568. ->whereIn('measurement.client_id', $this->getMyClientIds())
  569. ->where(function ($q) {
  570. $q
  571. ->where(function ($q2) {
  572. $q2
  573. ->where('client.mcp_pro_id', $this->id)
  574. ->where('measurement.has_been_stamped_by_mcp', false);
  575. })
  576. ->orWhere(function ($q2) {
  577. $q2
  578. ->where('client.default_na_pro_id', $this->id)
  579. ->where('measurement.has_been_stamped_by_non_hcp', false);
  580. })
  581. ->orWhere(function ($q2) {
  582. $q2
  583. ->where('client.rmm_pro_id', $this->id)
  584. ->where('measurement.has_been_stamped_by_rmm', false);
  585. })
  586. ->orWhere(function ($q2) {
  587. $q2
  588. ->where('client.rme_pro_id', $this->id)
  589. ->where('measurement.has_been_stamped_by_rme', false);
  590. });
  591. });
  592. if($_countOnly) {
  593. return $measurementsQuery->count();
  594. }
  595. $x = [];
  596. $measurements = $measurementsQuery
  597. ->orderBy('ts', 'desc')
  598. ->skip($skip)
  599. ->take($limit)
  600. ->get();
  601. // eager load stuff needed in JS
  602. foreach ($measurements as $measurement) {
  603. // if ($measurement->client_bdt_measurement_id) {
  604. // $measurement->bdtMeasurement = $measurement->clientBDTMeasurement->measurement;
  605. // }
  606. unset($measurement->clientBDTMeasurement); // we do not need this travelling to the frontend
  607. $client = [
  608. "uid" => $measurement->client->uid,
  609. "name" => $measurement->client->displayName(),
  610. ];
  611. $measurement->patient = $client;
  612. $measurement->careMonth = $measurement->client->currentCareMonth();
  613. $measurement->timestamp = friendlier_date_time($measurement->created_at);
  614. unset($measurement->client); // we do not need this travelling to the frontend
  615. if(@$measurement->detail_json) unset($measurement->detail_json);
  616. if(@$measurement->canvas_data) unset($measurement->canvas_data);
  617. if(@$measurement->latest_measurements) unset($measurement->latest_measurements);
  618. if(@$measurement->info_lines) unset($measurement->info_lines);
  619. if(@$measurement->canvas_data_backup) unset($measurement->canvas_data_backup);
  620. if(@$measurement->migrated_canvas_data_backup) unset($measurement->migrated_canvas_data_backup);
  621. // if($measurement->label == 'SBP' || $measurement->label = 'DBP'){
  622. // continue;
  623. // }
  624. $x[] = $measurement;
  625. }
  626. // dd($measurements);
  627. return $measurements;
  628. }
  629. public function getMeasurements($_onlyUnstamped = true)
  630. {
  631. $measurementsQuery = Measurement::where('is_removed', false);
  632. if ($this->pro_type != 'ADMIN') {
  633. $measurementsQuery
  634. ->whereIn('client_id', $this->getMyClientIds());
  635. }
  636. if ($_onlyUnstamped) {
  637. $measurementsQuery
  638. ->whereNotNull('client_bdt_measurement_id')
  639. ->whereNotNull('ts')
  640. ->where('is_cellular_zero', false)
  641. ->where(function ($q) {
  642. $q->whereNull('status')
  643. ->orWhere(function ($q2) {
  644. $q2->where('status', '<>', 'ACK')
  645. ->where('status', '<>', 'INVALID_ACK');
  646. });
  647. });
  648. }
  649. $x = [];
  650. $measurements = $measurementsQuery->orderBy('ts', 'desc')->paginate(50);
  651. // eager load stuff needed in JS
  652. foreach ($measurements as $measurement) {
  653. // if ($measurement->client_bdt_measurement_id) {
  654. // $measurement->bdtMeasurement = $measurement->clientBDTMeasurement->measurement;
  655. // }
  656. unset($measurement->clientBDTMeasurement); // we do not need this travelling to the frontend
  657. $client = [
  658. "uid" => $measurement->client->uid,
  659. "name" => $measurement->client->displayName(),
  660. ];
  661. $measurement->patient = $client;
  662. $measurement->careMonth = $measurement->client->currentCareMonth();
  663. $measurement->timestamp = friendly_date_time($measurement->created_at);
  664. unset($measurement->client); // we do not need this travelling to the frontend
  665. // if($measurement->label == 'SBP' || $measurement->label = 'DBP'){
  666. // continue;
  667. // }
  668. $x[] = $measurement;
  669. }
  670. return $measurements;
  671. }
  672. public function companyPros()
  673. {
  674. return $this->hasMany(CompanyPro::class, 'pro_id', 'id')
  675. ->where('is_active', true);
  676. }
  677. public function companyProPayers()
  678. {
  679. return $this->hasMany(CompanyProPayer::class, 'pro_id', 'id');
  680. }
  681. public function isAssociatedWithMCPayer() {
  682. $companyProPayers = $this->companyProPayers;
  683. $foundMC = false;
  684. if($companyProPayers) {
  685. foreach ($companyProPayers as $companyProPayer) {
  686. if($companyProPayer->payer && $companyProPayer->payer->is_medicare) {
  687. $foundMC = true;
  688. break;
  689. }
  690. }
  691. }
  692. return $foundMC;
  693. }
  694. public function isAssociatedWithNonMCPayer($_payerID) {
  695. $companyProPayers = $this->companyProPayers;
  696. $foundNonMC = false;
  697. if($companyProPayers) {
  698. foreach ($companyProPayers as $companyProPayer) {
  699. if($companyProPayer->payer && !$companyProPayer->payer->is_medicare && $companyProPayer->payer->id === $_payerID) {
  700. $foundNonMC = true;
  701. break;
  702. }
  703. }
  704. }
  705. return $foundNonMC;
  706. }
  707. public function companyLocations() {
  708. $companyProPayers = $this->companyProPayers;
  709. $companyIDs = [];
  710. foreach ($companyProPayers as $companyProPayer) {
  711. $companyIDs[] = $companyProPayer->company_id;
  712. }
  713. $locations = [];
  714. if(count($companyIDs)) {
  715. $locations = CompanyLocation::whereIn('id', $companyIDs)->get();
  716. }
  717. return $locations;
  718. }
  719. public function shadowClient() {
  720. return $this->hasOne(Client::class, 'id', 'shadow_client_id');
  721. }
  722. public function defaultCompanyPro() {
  723. return $this->hasOne(CompanyPro::class, 'id', 'default_company_pro_id');
  724. }
  725. public function currentNotePickupForProcessing() {
  726. return $this->hasOne(NotePickupForProcessing::class, 'id', 'current_note_pickup_for_processing_id');
  727. }
  728. public function get_patients_not_seen_in_45_days_count_as_mcp(){
  729. return 0;
  730. }
  731. //DNA_DASHBOARD
  732. //queries
  733. private function patientsQueryAsDna(){
  734. // WHERE na_pro_id = :me.id
  735. return Client::where('default_na_pro_id', $this->id);
  736. }
  737. private function patientsAwaitingMcpVisitQueryAsDna(){
  738. // WHERE has_mcp_done_onboarding_visit <> 'YES'
  739. return Client::where('default_na_pro_id', $this->id)->where('has_mcp_done_onboarding_visit', '<>', 'YES');
  740. }
  741. private function patientsWithoutAppointmentQueryAsDna(){
  742. // WHERE today_mcp_appointment_date IS NULL AND next_mcp_appointment_date IS NULL
  743. return Client::where('default_na_pro_id', $this->id)
  744. ->whereNull('today_mcp_appointment_date')
  745. ->whereNull('next_mcp_appointment_date');
  746. }
  747. private function encountersPendingMyReviewQueryAsDna(){
  748. // WHERE ally_pro_id = me.id AND is_cancelled IS NOT TRUE AND is_signed_by_hcp IS TRUE AND is_signed_by_ally IS NOT TRUE;
  749. return Note::where('ally_pro_id', $this->id)
  750. ->where('is_cancelled', '<>', true)
  751. ->where('is_signed_by_hcp', true)
  752. ->where('is_signed_by_ally','<>', true);
  753. }
  754. private function encountersInProgressQueryAsDna(){
  755. // SELECT * FROM note WHERE ally_pro_id = me.id AND is_cancelled IS NOT TRUE AND is_signed_by_hcp IS NOT TRUE ORDER BY effective_dateest DESC, created_at DESC;
  756. return Note::where('ally_pro_id', $this->id)
  757. ->where('is_cancelled', '<>', true)
  758. ->where('is_signed_by_hcp', '<>', true);
  759. }
  760. private function appointmentsPendingConfirmationQueryAsDna(){
  761. // WHERE client_id IN (SELECT id FROM client WHERE default_na_pro_id = :me.id) AND status = 'PENDING'
  762. $myId = $this->id;
  763. return Appointment::whereHas('client', function($clientQuery) use ($myId) {
  764. return $clientQuery->where('default_na_pro_id', $myId);
  765. })->where('status', 'PENDING');
  766. }
  767. private function cancelledAppointmentsPendingAckQueryAsDna(){
  768. // WHERE client_id IN (SELECT id FROM client WHERE default_na_pro_id = :me.id) AND status = 'CANCELLED' AND is_status_acknowledgement_from_default_na_pending IS TRUE;
  769. $myId = $this->id;
  770. return Appointment::whereHas('client', function($clientQuery) use ($myId) {
  771. return $clientQuery->where('default_na_pro_id', $myId);
  772. })->where('status', 'CANCELLED')
  773. ->where('is_status_acknowledgement_from_default_na_pending', true);
  774. }
  775. private function reportsPendingAckQueryAsDna(){
  776. // WHERE client_id IN (SELECT id FROM client WHERE default_na_pro_id = :me.id) AND has_na_pro_signed IS FALSE AND is_entry_error
  777. $myId = $this->id;
  778. return IncomingReport::whereHas('client',function($clientQuery) use ($myId) {
  779. return $clientQuery->where('default_na_pro_id', $myId);
  780. })->where('has_na_pro_signed', '<>', true)
  781. ->where('is_entry_error','<>', true);
  782. }
  783. private function supplyOrdersPendingMyAckQueryAsDna(){
  784. // WHERE client_id IN (SELECT id FROM client WHERE default_na_pro_id = :me.id) AND has_na_pro_signed IS FALSE AND is_signed_by_pro IS TRUE AND is_cancelled IS NOT TRUE;
  785. $myId = $this->id;
  786. return SupplyOrder::whereHas('client',function($clientQuery) use ($myId) {
  787. return $clientQuery->where('default_na_pro_id', $myId);
  788. })->where('has_na_pro_signed', '<>', true)
  789. ->where('is_signed_by_pro', true)
  790. ->where('is_cancelled', '<>', true);
  791. }
  792. private function supplyOrdersPendingHcpApprovalQueryAsDna(){
  793. // WHERE client_id IN (SELECT id FROM client WHERE default_na_pro_id = :me.id) AND has_na_pro_signed IS TRUE AND is_signed_by_pro IS NOT TRUE AND is_cancelled IS NOT TRUE;
  794. $myId = $this->id;
  795. return SupplyOrder::whereHas('client',function($clientQuery) use ($myId) {
  796. return $clientQuery->where('default_na_pro_id', $myId);
  797. })->where('has_na_pro_signed', true)
  798. ->where('is_signed_by_pro','<>', true)
  799. ->where('is_cancelled', '<>', true);
  800. }
  801. //counts
  802. public function patientsCountAsDna(){
  803. return $this->patientsQueryAsDna()->count();
  804. }
  805. public function patientsAwaitingMcpVisitCountAsDna(){
  806. return $this->patientsAwaitingMcpVisitQueryAsDna()->count();
  807. }
  808. public function patientsWithoutAppointmentCountAsDna(){
  809. return $this->patientsWithoutAppointmentQueryAsDna()->count();
  810. }
  811. public function encountersPendingMyReviewCountAsDna(){
  812. return $this->encountersPendingMyReviewQueryAsDna()->count();
  813. }
  814. public function encountersInProgressCountAsDna(){
  815. return $this->encountersInProgressQueryAsDna()->count();
  816. }
  817. public function appointmentsPendingConfirmationCountAsDna(){
  818. return $this->appointmentsPendingConfirmationQueryAsDna()->count();
  819. }
  820. public function cancelledAppointmentsPendingAckCountAsDna(){
  821. return $this->cancelledAppointmentsPendingAckQueryAsDna()->count();
  822. }
  823. public function reportsPendingAckCountAsDna(){
  824. return $this->reportsPendingAckQueryAsDna()->count();
  825. }
  826. public function supplyOrdersPendingMyAckCountAsDna(){
  827. return $this->supplyOrdersPendingMyAckQueryAsDna()->count();
  828. }
  829. public function supplyOrdersPendingHcpApprovalCountAsDna(){
  830. return $this->supplyOrdersPendingHcpApprovalQueryAsDna()->count();
  831. }
  832. //records
  833. private $DNA_RESULTS_PAGE_SIZE = 50;
  834. public function patientsRecordsAsDna(){
  835. return $this->patientsQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  836. }
  837. public function patientsAwaitingMcpVisitRecordsAsDna(){
  838. return $this->patientsAwaitingMcpVisitQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  839. }
  840. public function patientsWithoutAppointmentRecordsAsDna(){
  841. return $this->patientsWithoutAppointmentQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  842. }
  843. public function encountersPendingMyReviewRecordsAsDna(){
  844. return $this->encountersPendingMyReviewQueryAsDna()
  845. ->orderBy('effective_dateest', 'desc')
  846. ->orderBy('created_at', 'desc')
  847. ->paginate($this->DNA_RESULTS_PAGE_SIZE);
  848. }
  849. public function encountersInProgressRecordsAsDna(){
  850. return $this->encountersInProgressQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  851. }
  852. public function appointmentsPendingConfirmationRecordsAsDna(){
  853. return $this->appointmentsPendingConfirmationQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  854. }
  855. public function cancelledAppointmentsPendingAckRecordsAsDna(){
  856. return $this->cancelledAppointmentsPendingAckQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  857. }
  858. public function reportsPendingAckRecordsAsDna(){
  859. return $this->reportsPendingAckQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  860. }
  861. public function supplyOrdersPendingMyAckRecordsAsDna(){
  862. return $this->supplyOrdersPendingMyAckQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  863. }
  864. public function supplyOrdersPendingHcpApprovalRecordsAsDna(){
  865. return $this->supplyOrdersPendingHcpApprovalQueryAsDna()->paginate($this->DNA_RESULTS_PAGE_SIZE);
  866. }
  867. public function measurementsPendingReviewAsDna(){
  868. //Measurements Pending Review
  869. // SELECT * FROM measurement WHERE client_id IN (SELECT id FROM client WHERE rmm_pro_id = :me.id)
  870. // AND has_been_stamped_by_rmm IS FALSE AND is_cellular IS TRUE AND is_cellular_zero IS NOT TRUE AND is_active IS TRUE ORDER BY ts DESC;
  871. $myId = $this->id;
  872. return Measurement::whereHas('client',function($clientQuery) use ($myId) {
  873. return $clientQuery->where('rmm_pro_id', $myId);
  874. })
  875. ->where('has_been_stamped_by_rmm', '<>', true)
  876. ->where('is_cellular', true)
  877. ->where('is_cellular_zero', '<>', true)
  878. ->where('is_active', true)
  879. ->orderBy('ts', 'DESC')
  880. ->paginate(15);
  881. }
  882. public function getProcessingAmountAsDna(){
  883. $expectedForCm = DB::select(DB::raw("SELECT coalesce(SUM(cm_expected_payment_amount),0) as expected_pay FROM bill WHERE cm_pro_id = :performerProID AND has_cm_been_paid = false AND is_signed_by_cm IS TRUE AND is_cancelled = false"), ['performerProID' => $this->id])[0]->expected_pay;
  884. $expectedForRme = DB::select(DB::raw("SELECT coalesce(SUM(rme_expected_payment_amount),0) as expected_pay FROM bill WHERE rme_pro_id = :performerProID AND has_rme_been_paid = false AND is_signed_by_rme IS TRUE AND is_cancelled = false"), ['performerProID' => $this->id])[0]->expected_pay;
  885. $expectedForRmm = DB::select(DB::raw("SELECT coalesce(SUM(rmm_expected_payment_amount),0) as expected_pay FROM bill WHERE rmm_pro_id = :performerProID AND has_rmm_been_paid = false AND is_signed_by_rmm IS TRUE AND is_cancelled = false"), ['performerProID' => $this->id])[0]->expected_pay;
  886. $expectedForNa = DB::select(DB::raw("SELECT coalesce(SUM(generic_pro_expected_payment_amount),0) as expected_pay FROM bill WHERE generic_pro_id = :performerProID AND has_generic_pro_been_paid = false AND is_signed_by_generic_pro IS TRUE AND is_cancelled = false"), ['performerProID' => $this->id])[0]->expected_pay;
  887. $totalExpectedAmount = $expectedForCm + $expectedForRme + $expectedForRmm + $expectedForNa;
  888. return $totalExpectedAmount;
  889. }
  890. public function getNextPaymentDateAsDna(){
  891. $nextPaymentDate = '--';
  892. //if today is < 15th, next payment is 15th, else nextPayment is
  893. $today = strtotime(date('Y-m-d'));
  894. $todayDate = date('j', $today);
  895. $todayMonth = date('m', $today);
  896. $todayYear = date('Y', $today);
  897. if ($todayDate < 15) {
  898. $nextPaymentDate = new DateTime();
  899. $nextPaymentDate->setDate($todayYear, $todayMonth, 15);
  900. $nextPaymentDate = $nextPaymentDate->format('m/d/Y');
  901. } else {
  902. $nextPaymentDate = new \DateTime();
  903. $lastDayOfMonth = date('t', $today);
  904. $nextPaymentDate->setDate($todayYear, $todayMonth, $lastDayOfMonth);
  905. $nextPaymentDate = $nextPaymentDate->format('m/d/Y');
  906. }
  907. return $nextPaymentDate;
  908. }
  909. public function clientSmsesAsDna(){
  910. $myId = $this->id;
  911. return ClientSMS::whereHas('client', function($clientQuery) use ($myId){
  912. return $clientQuery->where('default_na_pro_id', $myId);
  913. })
  914. ->orderBy('created_at', 'DESC')
  915. ->paginate(15);
  916. }
  917. public function clientMemosAsDna(){
  918. $naClientMemos = DB::select(
  919. DB::raw("
  920. SELECT c.uid as client_uid, c.name_first, c.name_last,
  921. cm.uid, cm.content, cm.created_at
  922. FROM client c join client_memo cm on c.id = cm.client_id
  923. WHERE
  924. c.default_na_pro_id = {$this->id}
  925. ORDER BY cm.created_at DESC
  926. ")
  927. );
  928. return $naClientMemos;
  929. }
  930. public function getAppointmentsPendingStatusChangeAckAsDna() {
  931. $myId = $this->id;
  932. return Appointment::whereHas('client', function($clientQuery) use ($myId){
  933. return $clientQuery->where('default_na_pro_id', $myId);
  934. })
  935. ->where('is_status_acknowledgement_from_appointment_pro_pending', true)
  936. ->where('raw_date', '>=', DB::raw('NOW()'))
  937. ->orderBy('raw_date', 'asc')
  938. ->get();
  939. }
  940. public function getAppointmentsPendingDecisionAckAsDna() {
  941. $myId = $this->id;
  942. return Appointment::whereHas('client', function($clientQuery) use ($myId){
  943. return $clientQuery->where('default_na_pro_id', $myId);
  944. })
  945. ->where('is_decision_acknowledgement_from_appointment_pro_pending', true)
  946. ->where('raw_date', '>=', DB::raw('NOW()'))
  947. ->orderBy('raw_date', 'asc')
  948. ->get();
  949. }
  950. public function getAppointmentsPendingTimeChangeAckAsDna() {
  951. $myId = $this->id;
  952. return Appointment::whereHas('client', function($clientQuery) use ($myId){
  953. return $clientQuery->where('default_na_pro_id', $myId);
  954. })
  955. ->where('is_time_change_acknowledgement_from_appointment_pro_pending', true)
  956. ->where('raw_date', '>=', DB::raw('NOW()'))
  957. ->orderBy('raw_date', 'asc')
  958. ->get();
  959. }
  960. function myGenericBills() {
  961. return Bill::where('bill_service_type', 'GENERIC')
  962. ->where('generic_pro_id', $this->id)
  963. ->orderBy('created_at', 'DESC')->get();
  964. }
  965. }