Client.php 3.8 KB

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