Client.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Relations\HasOne;
  4. use Illuminate\Support\Collection;
  5. # use Illuminate\Database\Eloquent\Model;
  6. class Client extends Model
  7. {
  8. protected $table = 'client';
  9. public function displayName() {
  10. return $this->name_last . ', '. $this->name_first;
  11. }
  12. public function mcp() {
  13. return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
  14. }
  15. public function cm() {
  16. return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
  17. }
  18. public function rmm() {
  19. return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
  20. }
  21. public function prosInMeetingWith() {
  22. return Pro::where('in_meeting_with_client_id', $this->id)->get();
  23. }
  24. public function notes() {
  25. return $this->hasMany(Note::class, 'client_id', 'id')
  26. ->orderBy('effective_dateest', 'desc');
  27. }
  28. public function activeNotes() {
  29. return $this->hasMany(Note::class, 'client_id', 'id')
  30. ->where('is_cancelled', false)
  31. ->orderBy('effective_dateest', 'desc');
  32. }
  33. public function cancelledNotes() {
  34. return $this->hasMany(Note::class, 'client_id', 'id')
  35. ->where('is_cancelled', true)
  36. ->orderBy('effective_dateest', 'desc');
  37. }
  38. public function sections() {
  39. return $this->hasMany(Section::class, 'client_id', 'id')
  40. ->where('is_active', true)
  41. ->orderBy('created_at', 'asc');
  42. }
  43. public function handouts() {
  44. $mappings = HandoutClient::where('client_id', $this->id)->get();
  45. $handouts = new Collection();
  46. foreach ($mappings as $mapping) {
  47. $handout = Handout::where('id', $mapping->handout_id)->first();
  48. $handout->handout_client_uid = $mapping->uid;
  49. $handouts->add($handout);
  50. }
  51. $handouts = $handouts->sortBy('created_at');
  52. return $handouts;
  53. }
  54. public function duplicateOf() {
  55. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  56. }
  57. public function actionItems () {
  58. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  59. ->orderBy('action_item_category', 'asc')
  60. ->orderBy('created_at', 'desc');
  61. }
  62. public function infoLines() {
  63. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  64. }
  65. public function measurements() {
  66. return $this->hasMany(Measurement::class, 'client_id', 'id')
  67. ->distinct('label')
  68. ->where('is_removed', false)
  69. ->orderBy('label', 'asc')
  70. ->orderBy('effective_date', 'desc');
  71. }
  72. public function smses() {
  73. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  74. ->orderBy('created_at', 'desc');
  75. }
  76. public function documents() {
  77. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  78. ->orderBy('created_at', 'desc');
  79. }
  80. public function smsNumbers() {
  81. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  82. ->orderBy('created_at', 'desc');
  83. }
  84. public function nextMcpAppointment() {
  85. if($this->mcp) {
  86. return Appointment::where('client_id', $this->id)
  87. ->where('pro_id', $this->mcp->id)
  88. ->where('start_time', '>=', date('Y-m-d'))
  89. ->orderBy('start_time', 'asc')
  90. ->first();
  91. }
  92. return false;
  93. }
  94. public function appointments() {
  95. return $this->hasMany(Appointment::class, 'client_id', 'id')
  96. ->orderBy('start_time', 'desc');
  97. }
  98. public function upcomingAppointments() {
  99. return $this->hasMany(Appointment::class, 'client_id', 'id')
  100. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  101. ->whereIn('status', ['CREATED', 'CONFIRMED'])
  102. ->orderBy('start_time', 'desc');
  103. }
  104. public function memos() {
  105. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  106. ->where('is_cancelled', false)
  107. ->orderBy('created_at', 'desc');
  108. }
  109. public function devices() {
  110. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  111. ->where('is_active', true)
  112. ->orderBy('created_at', 'desc');
  113. }
  114. public function hasDevice($_device) {
  115. $count = ClientBDTDevice::where('client_id', $this->id)
  116. ->where('device_id', $_device->id)
  117. ->where('is_active', true)
  118. ->count();
  119. return !!$count;
  120. }
  121. public function deviceMeasurements() {
  122. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  123. ->orderBy('created_at', 'desc');
  124. }
  125. public function activeMcpRequest() {
  126. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  127. }
  128. }