Client.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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')->orderBy('effective_dateest', 'desc');
  26. }
  27. public function sections() {
  28. return $this->hasMany(Section::class, 'client_id', 'id')
  29. ->where('is_active', true)
  30. ->orderBy('created_at', 'asc');
  31. }
  32. public function handouts() {
  33. $mappings = HandoutClient::where('client_id', $this->id)->get();
  34. $handouts = new Collection();
  35. foreach ($mappings as $mapping) {
  36. $handout = Handout::where('id', $mapping->handout_id)->first();
  37. $handout->handout_client_uid = $mapping->uid;
  38. $handouts->add($handout);
  39. }
  40. $handouts = $handouts->sortBy('created_at');
  41. return $handouts;
  42. }
  43. public function duplicateOf() {
  44. return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
  45. }
  46. public function actionItems () {
  47. return $this->hasMany(ActionItem::class, 'client_id', 'id')
  48. ->orderBy('action_item_category', 'asc')
  49. ->orderBy('created_at', 'desc');
  50. }
  51. public function infoLines() {
  52. return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
  53. }
  54. public function measurements() {
  55. return $this->hasMany(Measurement::class, 'client_id', 'id')
  56. ->distinct('label')
  57. ->where('is_removed', false)
  58. ->orderBy('label', 'asc')
  59. ->orderBy('effective_date', 'desc');
  60. }
  61. public function smses() {
  62. return $this->hasMany(ClientSMS::class, 'client_id', 'id')
  63. ->orderBy('created_at', 'desc');
  64. }
  65. public function documents() {
  66. return $this->hasMany(ClientDocument::class, 'client_id', 'id')
  67. ->orderBy('created_at', 'desc');
  68. }
  69. public function smsNumbers() {
  70. return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
  71. ->orderBy('created_at', 'desc');
  72. }
  73. public function nextMcpAppointment() {
  74. if($this->mcp) {
  75. return Appointment::where('client_id', $this->id)
  76. ->where('pro_id', $this->mcp->id)
  77. ->where('start_time', '>=', date('Y-m-d'))
  78. ->orderBy('start_time', 'asc')
  79. ->first();
  80. }
  81. return false;
  82. }
  83. public function appointments() {
  84. return $this->hasMany(Appointment::class, 'client_id', 'id')
  85. ->orderBy('start_time', 'desc');
  86. }
  87. public function upcomingAppointments() {
  88. return $this->hasMany(Appointment::class, 'client_id', 'id')
  89. // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
  90. ->orderBy('start_time', 'desc');
  91. }
  92. public function memos() {
  93. return $this->hasMany(ClientMemo::class, 'client_id', 'id')
  94. ->where('is_cancelled', false)
  95. ->orderBy('created_at', 'desc');
  96. }
  97. public function devices() {
  98. return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
  99. ->where('is_active', true)
  100. ->orderBy('created_at', 'desc');
  101. }
  102. public function hasDevice($_device) {
  103. $count = ClientBDTDevice::where('client_id', $this->id)
  104. ->where('device_id', $_device->id)
  105. ->where('is_active', true)
  106. ->count();
  107. return !!$count;
  108. }
  109. public function deviceMeasurements() {
  110. return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
  111. ->orderBy('created_at', 'desc');
  112. }
  113. public function activeMcpRequest() {
  114. return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
  115. }
  116. }