123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Support\Collection;
- # use Illuminate\Database\Eloquent\Model;
- class Client extends Model
- {
- protected $table = 'client';
- public function displayName() {
- return $this->name_last . ', '. $this->name_first;
- }
- public function mcp() {
- return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
- }
- public function cm() {
- return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
- }
- public function rmm() {
- return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
- }
- public function prosInMeetingWith() {
- return Pro::where('in_meeting_with_client_id', $this->id)->get();
- }
- public function notes() {
- return $this->hasMany(Note::class, 'client_id', 'id')->orderBy('effective_dateest', 'desc');
- }
- public function sections() {
- return $this->hasMany(Section::class, 'client_id', 'id')
- ->where('is_active', true)
- ->orderBy('created_at', 'asc');
- }
- public function handouts() {
- $mappings = HandoutClient::where('client_id', $this->id)->get();
- $handouts = new Collection();
- foreach ($mappings as $mapping) {
- $handout = Handout::where('id', $mapping->handout_id)->first();
- $handout->handout_client_uid = $mapping->uid;
- $handouts->add($handout);
- }
- $handouts = $handouts->sortBy('created_at');
- return $handouts;
- }
- public function duplicateOf() {
- return $this->hasOne(Client::class, 'id', 'duplicate_of_client_id');
- }
- public function actionItems () {
- return $this->hasMany(ActionItem::class, 'client_id', 'id')
- ->orderBy('action_item_category', 'asc')
- ->orderBy('created_at', 'desc');
- }
- public function infoLines() {
- return $this->hasMany(ClientInfoLine::class, 'client_id', 'id')->orderBy('created_at', 'desc');
- }
- public function measurements() {
- return $this->hasMany(Measurement::class, 'client_id', 'id')
- ->distinct('label')
- ->where('is_removed', false)
- ->orderBy('label', 'asc')
- ->orderBy('effective_date', 'desc');
- }
- public function smses() {
- return $this->hasMany(ClientSMS::class, 'client_id', 'id')
- ->orderBy('created_at', 'desc');
- }
- public function documents() {
- return $this->hasMany(ClientDocument::class, 'client_id', 'id')
- ->orderBy('created_at', 'desc');
- }
- public function smsNumbers() {
- return $this->hasMany(ClientSMSNumber::class, 'client_id', 'id')
- ->orderBy('created_at', 'desc');
- }
- public function nextMcpAppointment() {
- if($this->mcp) {
- return Appointment::where('client_id', $this->id)
- ->where('pro_id', $this->mcp->id)
- ->where('start_time', '>=', date('Y-m-d'))
- ->orderBy('start_time', 'asc')
- ->first();
- }
- return false;
- }
- public function appointments() {
- return $this->hasMany(Appointment::class, 'client_id', 'id')
- ->orderBy('start_time', 'desc');
- }
- public function upcomingAppointments() {
- return $this->hasMany(Appointment::class, 'client_id', 'id')
- // ->where('raw_start_time', '>', date('Y-m-d H:i:s'))
- ->whereIn('status', ['CREATED', 'CONFIRMED'])
- ->orderBy('start_time', 'desc');
- }
- public function memos() {
- return $this->hasMany(ClientMemo::class, 'client_id', 'id')
- ->where('is_cancelled', false)
- ->orderBy('created_at', 'desc');
- }
- public function devices() {
- return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
- ->where('is_active', true)
- ->orderBy('created_at', 'desc');
- }
- public function hasDevice($_device) {
- $count = ClientBDTDevice::where('client_id', $this->id)
- ->where('device_id', $_device->id)
- ->where('is_active', true)
- ->count();
- return !!$count;
- }
- public function deviceMeasurements() {
- return $this->hasMany(ClientBDTMeasurement::class, 'client_id', 'id')
- ->orderBy('created_at', 'desc');
- }
- public function activeMcpRequest() {
- return $this->hasOne(McpRequest::class, 'id', 'active_mcp_request_id');
- }
- }
|