Note.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Models;
  3. # use Illuminate\Database\Eloquent\Model;
  4. class Note extends Model
  5. {
  6. protected $table = "note";
  7. public function client()
  8. {
  9. return $this->hasOne(Client::class, 'id', 'client_id');
  10. }
  11. public function hcpPro()
  12. {
  13. return $this->hasOne(Pro::class, 'id', 'hcp_pro_id');
  14. }
  15. public function hcpCompanyPro()
  16. {
  17. return $this->hasOne(CompanyPro::class, 'id', 'hcp_company_pro_id');
  18. }
  19. public function noteTemplate()
  20. {
  21. return $this->hasOne(NoteTemplate::class, 'id', 'note_template_id');
  22. }
  23. public function createdSession()
  24. {
  25. return $this->hasOne(AppSession::class, 'id', 'created_by_session_id');
  26. }
  27. public function allyPro()
  28. {
  29. return $this->hasOne(Pro::class, 'id', 'ally_pro_id');
  30. }
  31. public function cmSetupClaim()
  32. {
  33. return $this->hasOne(Claim::class, 'id', 'cm_setup_claim_id')
  34. ->where('status', '<>', 'CANCELLED');
  35. }
  36. // Signed/Billed/Verified/Processed/Cancelled
  37. public function overallStatus()
  38. {
  39. $status = 'New';
  40. if ($this->is_cancelled) $status = 'Cancelled';
  41. else {
  42. if ($this->is_billing_marked_done) $status = 'Billed';
  43. else if ($this->is_signed_by_hcp) $status = 'Signed';
  44. }
  45. return $status;
  46. }
  47. public function bills()
  48. {
  49. return $this->hasMany(Bill::class, 'note_id', 'id')
  50. ->where('bill_service_type', '<>', 'GENERIC')
  51. ->orderBy('id', 'asc');
  52. }
  53. public function addendums()
  54. {
  55. return $this->hasMany(NoteAddendum::class, 'note_id', 'id')->where('is_removed', false);
  56. }
  57. public function sections()
  58. {
  59. return $this->hasMany(Section::class, 'note_id', 'id')
  60. ->where('is_active', true)
  61. ->orderBy('position_index', 'asc');
  62. }
  63. public function segments()
  64. {
  65. return $this->hasMany(Segment::class, 'note_id', 'id')
  66. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  67. ->where('is_active', true)
  68. ->orderBy('position_index', 'asc');
  69. }
  70. public function segmentsLeft()
  71. {
  72. return $this->hasMany(Segment::class, 'note_id', 'id')
  73. ->whereRaw("(left_or_right IS NULL OR left_or_right = 'LEFT')")
  74. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  75. ->where('is_active', true)
  76. ->orderBy('position_index', 'asc');
  77. }
  78. public function segmentsRight()
  79. {
  80. return $this->hasMany(Segment::class, 'note_id', 'id')
  81. ->where('left_or_right', 'RIGHT')
  82. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  83. ->where('is_active', true)
  84. ->orderBy('position_index', 'asc');
  85. }
  86. public function coreSegment()
  87. {
  88. return $this->hasOne(Segment::class, 'id', 'core_segment_id');
  89. }
  90. public function getSegmentByInternalName($_name) {
  91. $segmentTemplate = SegmentTemplate::where('internal_name', $_name)->first();
  92. if(!!$segmentTemplate) {
  93. return Segment::where('note_id', $this->id)
  94. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  95. ->where('segment_template_id', $segmentTemplate->id)
  96. ->first();
  97. }
  98. return null;
  99. }
  100. public function reasons()
  101. {
  102. return $this->hasMany(NoteReason::class, 'note_id', 'id');
  103. }
  104. public function claims()
  105. {
  106. return $this->hasMany(Claim::class, 'note_id', 'id')->orderBy('created_at', 'DESC');
  107. }
  108. public function summary()
  109. {
  110. $parts = [];
  111. foreach ($this->sections as $section) {
  112. $parts[] = $section->summary_html;
  113. }
  114. $parts = array_map(function($_part) {
  115. return '<div class="note-section-summary">' . $_part . '</div>';
  116. }, $parts);
  117. return implode("", $parts);
  118. }
  119. public function hcpCompany()
  120. {
  121. return $this->hasOne(Company::class, 'id', 'hcp_company_id');
  122. }
  123. public function hcpCompanyProPayer()
  124. {
  125. return $this->hasOne(CompanyProPayer::class, 'id', 'hcp_company_pro_payer_id');
  126. }
  127. public function hcpCompanyLocation()
  128. {
  129. return $this->hasOne(CompanyLocation::class, 'id', 'hcp_company_location_id');
  130. }
  131. public function currentNotePickupForProcessing() {
  132. return $this->hasOne(NotePickupForProcessing::class, 'id', 'current_note_pickup_for_processing_id');
  133. }
  134. public function visitTemplate()
  135. {
  136. return $this->hasOne(VisitTemplate::class, 'id', 'visit_template_id');
  137. }
  138. public function hasTreatmentServicesBillByHCP()
  139. {
  140. $bills = Bill::where('note_id', $this->id)
  141. ->where('bill_service_type', '<>', 'GENERIC')
  142. ->where('code', 'Treatment Services')
  143. ->where('is_cancelled', false)
  144. ->count();
  145. return !!$bills;
  146. }
  147. }