123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- <?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 pcp()
- {
- return $this->hasOne(Pro::class, 'id', 'physician_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 rme()
- {
- return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
- }
- public function rms()
- {
- return $this->hasOne(Pro::class, 'id', 'rms_pro_id');
- }
- public function rmg()
- {
- return $this->hasOne(Pro::class, 'id', 'rmg_pro_id');
- }
- public function defaultNaPro()
- {
- return $this->hasOne(Pro::class, 'id', 'default_na_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 notesAscending()
- {
- return $this->hasMany(Note::class, 'client_id', 'id')
- ->orderBy('effective_dateest', 'asc');
- }
- public function activeNotes()
- {
- return $this->hasMany(Note::class, 'client_id', 'id')
- ->where('is_cancelled', false)
- ->orderBy('effective_dateest', 'desc');
- }
- public function cancelledNotes()
- {
- return $this->hasMany(Note::class, 'client_id', 'id')
- ->where('is_cancelled', true)
- ->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)
- ->orderByRaw('ts DESC NULLS LAST');
- }
- public function nonZeroMeasurements()
- {
- return $this->hasMany(Measurement::class, 'client_id', 'id')
- /*->distinct('label')*/
- ->where('is_removed', false)
- ->where('is_cellular_zero', false)
- ->orderBy('effective_date', 'desc');
- }
- public function getNonZeroBpMeasurements(){
- return $this->hasMany(Measurement::class, 'client_id', 'id')
- /*->distinct('label')*/
- ->where('is_removed', false)
- ->where('label', '=', 'BP')
- ->where('sbp_mm_hg', '>', 0)
- ->where('dbp_mm_hg', '>', 0)
- ->orderBy('ts', 'desc');
- }
- public function getNonZeroWeightMeasurements(){
- return $this->hasMany(Measurement::class, 'client_id', 'id')
- /*->distinct('label')*/
- ->where('is_removed', false)
- ->where('label', '=', 'Wt. (lbs.)')
- ->where('numeric_value', '>', 0)
- ->orderBy('ts', 'desc');
- }
- public function currentCareMonth()
- {
- $cmStartDate = strtotime(date('Y-m-d'));
- $month = date("n", $cmStartDate);
- $year = date("Y", $cmStartDate);
- return CareMonth
- ::where('client_id', $this->id)
- ->whereRaw('EXTRACT(MONTH FROM start_date) = ?', [$month])
- ->whereRaw('EXTRACT(YEAR FROM start_date) = ?', [$year])
- ->first();
- }
- public function measurementsInCareMonth(CareMonth $careMonth)
- {
- $cmStartDate = strtotime($careMonth->start_date);
- $month = date("n", $cmStartDate);
- $year = date("Y", $cmStartDate);
- $measurements = Measurement
- ::where('client_id', $this->id)
- ->whereRaw('EXTRACT(MONTH FROM effective_date) = ?', [$month])
- ->whereRaw('EXTRACT(YEAR FROM effective_date) = ?', [$year])
- ->where('is_removed', false)
- ->orderBy('ts', 'desc')
- ->get();
- return $measurements;
- }
- public function allMeasurements()
- {
- return $this->hasMany(Measurement::class, 'client_id', 'id')
- ->where('is_removed', false)
- ->whereNull('parent_measurement_id')
- ->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 incomingReports() {
- return $this->hasMany(IncomingReport::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 appointmentsForProByStatus($forPro = 'all', $status = 'all')
- {
- $appointments = Appointment::where('client_id', $this->id);
- if($forPro !== 'all') {
- $forPro = Pro::where('uid', $forPro)->first();
- $appointments = $appointments->where('pro_id', $forPro->id);
- }
- if($status !== 'ALL') {
- $appointments = $appointments->where('status', $status);
- }
- $appointments = $appointments->orderBy('start_time', 'desc');
- return $appointments->get();
- }
- 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 appointmentsFromLastWeek()
- {
- $dateLastWeek = date_sub(date_create(), date_interval_create_from_date_string("7 days"));
- $dateLastWeek = date_format($dateLastWeek, "Y-m-d");
- return $this->hasMany(Appointment::class, 'client_id', 'id')
- ->where('raw_date', '>=', $dateLastWeek)
- ->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 deactivatedDevices()
- {
- return $this->hasMany(ClientBDTDevice::class, 'client_id', 'id')
- ->where('is_active', false)
- ->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');
- }
- public function clientPrograms()
- {
- return $this->hasMany(ClientProgram::class, 'client_id', 'id')
- ->where('is_active', true)
- ->orderBy('title', 'desc');
- }
- public function tickets()
- {
- return $this->hasMany(Ticket::class, 'client_id', 'id')
- ->orderBy('is_open', 'desc')
- ->orderBy('created_at', 'desc');
- }
- public function mcpDisplayName()
- {
- }
- public function rmeDisplayName()
- {
- }
- public function firstCellularBPDevice()
- {
- $devices = $this->devices();
- $x = null;
- foreach($devices as $device){
- if($device->device->category == 'BP'){
- $x = $device;
- continue;
- }
- }
- return $x;
- }
- public function getFirstCellularBPMeasurementAt()
- {
- }
- public function getLatestCellularBPMeasurementAt()
- {
- }
- public function getTotalCellularBPMeasurements()
- {
- }
- public function firstCellularWeightDevice()
- {
- }
- public function getFirstCellularWeightMeasurementAt()
- {
- }
- public function getLatestCellularWeightMeasurementAt()
- {
- }
- public function getTotalCellularWeightMeasurements()
- {
- }
- public function prosWithAccess()
- {
- $pros = [];
- // directly associated pros
- $pro = $this->mcp;
- if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'MCP'];
- $pro = $this->pcp;
- if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'PCP (Physician)'];
- $pro = $this->cm;
- if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'CM'];
- $pro = $this->rmm;
- if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RMM'];
- $pro = $this->rme;
- if ($pro && $pro->id) $pros[] = ["pro" => $pro->displayName(), "association" => 'RME'];
- // via client pro access
- $cpAccesses = ClientProAccess::where('client_id', $this->id)->where('is_active', true)->get();
- foreach ($cpAccesses as $cpAccess) {
- if (!$cpAccess->pro) continue;
- $pros[] = ["pro" => $cpAccess->pro->displayName(), "association" => 'Via Client-Pro Access'];
- }
- // via appointments
- $appointments = Appointment::where('client_id', $this->id)->get();
- foreach ($appointments as $appointment) {
- if (!$appointment->pro) continue;
- $pros[] = ["pro" => $appointment->pro->displayName(), "association" => 'Via Appointment: ' . $appointment->raw_date];
- }
- // via client program
- $clientPrograms = ClientProgram::where('client_id', $this->id)->where('is_active', true)->get();
- foreach ($clientPrograms as $clientProgram) {
- if ($clientProgram->mcp)
- $pros[] = ["pro" => $clientProgram->mcp->displayName(), "association" => 'Program MCP: ' . $clientProgram->title];
- if ($clientProgram->manager)
- $pros[] = ["pro" => $clientProgram->manager->displayName(), "association" => 'Program Manager: ' . $clientProgram->title];
- }
- // sort by pro name
- $name = array_column($pros, 'pro');
- array_multisort($name, SORT_ASC, $pros);
- return $pros;
- }
- public function mcpRequests()
- {
- return $this->hasMany(McpRequest::class, 'for_client_id', 'id')
- ->orderBy('created_at', 'desc');
- }
- public function eligibleRefreshes()
- {
- return $this->hasMany(ClientEligibleRefresh::class, 'client_id', 'id')
- ->orderBy('created_at', 'desc');
- }
- public function mbPayerValidationResults()
- {
- return $this->hasMany(ClientMBPayerValidationResult::class, 'client_id', 'id')
- ->orderBy('created_at', 'desc');
- }
- public function payer()
- {
- return $this->hasOne(MBPayer::class, 'id', 'mb_payer_id');
- }
- public function supplyOrders()
- {
- return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
- ->orderBy('created_at', 'desc');
- }
- public function activeSupplyOrders()
- {
- return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
- ->where('is_cancelled', false)
- ->orderBy('created_at', 'desc');
- }
- public function cancelledSupplyOrders()
- {
- return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
- ->where('is_cancelled', true)
- ->orderBy('created_at', 'desc');
- }
- public function readyToShipSupplyOrders()
- {
- return $this->hasMany(SupplyOrder::class, 'client_id', 'id')
- ->where('is_cancelled', false)
- ->where('is_cleared_for_shipment', true)
- ->whereNull('shipment_id')
- ->orderBy('created_at', 'desc');
- }
- public function shipments()
- {
- return $this->hasMany(Shipment::class, 'client_id', 'id')
- ->where('is_cancelled', false)
- ->orderBy('created_at', 'desc');
- }
- public function numSignedNotes() {
- return Note::where('client_id', $this->id)
- ->where('is_cancelled', false)
- ->where('is_signed_by_hcp', true)
- ->count();
- }
- public function accountClient() {
- return $this->hasOne(AccountClient::class, 'client_id', 'id')->where('is_removed', false);
- }
- public function smsReminders()
- {
- return $this->hasMany(SimpleSMSReminder::class, 'client_id', 'id')
- ->orderBy('created_at', 'asc');
- }
- public function measurementConfirmationNumbers()
- {
- return $this->hasMany(MeasurementConfirmationNumber::class, 'client_id', 'id')
- ->orderBy('created_at', 'asc');
- }
- public function shadowOfPro() {
- return $this->hasOne(Pro::class, 'id', 'shadow_pro_id');
- }
- public function clientTags()
- {
- return $this->hasMany(ClientTag::class, 'client_id', 'id')
- ->where('is_cancelled', false)
- ->orderBy('tag', 'asc');
- }
- }
|