Pro.php 45 KB

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