Note.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 followUpAppointment()
  32. {
  33. return $this->hasOne(Appointment::class, 'id', 'follow_up_appointment_id');
  34. }
  35. public function cmSetupClaim()
  36. {
  37. return $this->hasOne(Claim::class, 'id', 'cm_setup_claim_id')
  38. ->where('status', '<>', 'CANCELLED');
  39. }
  40. // Signed/Billed/Verified/Processed/Cancelled
  41. public function overallStatus()
  42. {
  43. $status = 'New';
  44. if ($this->is_cancelled) $status = 'Cancelled';
  45. else {
  46. if ($this->is_billing_marked_done) $status = 'Billed';
  47. else if ($this->is_signed_by_hcp) $status = 'Signed';
  48. }
  49. return $status;
  50. }
  51. public function bills()
  52. {
  53. return $this->hasMany(Bill::class, 'note_id', 'id')
  54. ->where('bill_service_type', '<>', 'GENERIC')
  55. ->orderBy('id', 'asc');
  56. }
  57. public function addendums()
  58. {
  59. return $this->hasMany(NoteAddendum::class, 'note_id', 'id')->where('is_removed', false);
  60. }
  61. public function sections()
  62. {
  63. return $this->hasMany(Section::class, 'note_id', 'id')
  64. ->where('is_active', true)
  65. ->orderBy('position_index', 'asc');
  66. }
  67. public function segments()
  68. {
  69. return $this->hasMany(Segment::class, 'note_id', 'id')
  70. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  71. //->where('is_active', true)
  72. ->orderBy('position_index', 'asc');
  73. }
  74. public function segmentsLeft()
  75. {
  76. return $this->hasMany(Segment::class, 'note_id', 'id')
  77. ->whereRaw("(left_or_right IS NULL OR left_or_right = 'LEFT')")
  78. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  79. //->where('is_active', true)
  80. ->orderBy('position_index', 'asc');
  81. }
  82. public function segmentsRight()
  83. {
  84. return $this->hasMany(Segment::class, 'note_id', 'id')
  85. ->where('left_or_right', 'RIGHT')
  86. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  87. //->where('is_active', true)
  88. ->orderBy('position_index', 'asc');
  89. }
  90. public function coreSegment()
  91. {
  92. return $this->hasOne(Segment::class, 'id', 'core_segment_id');
  93. }
  94. public function getSegmentByInternalName($_name) {
  95. $segmentTemplate = SegmentTemplate::where('internal_name', $_name)->first();
  96. if(!!$segmentTemplate) {
  97. return Segment::where('note_id', $this->id)
  98. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  99. ->where('segment_template_id', $segmentTemplate->id)
  100. ->first();
  101. }
  102. return null;
  103. }
  104. public function reasons()
  105. {
  106. $reasons = [];
  107. for ($i = 1; $i <= 4; $i++) {
  108. if($this->{'note_reason_icd' . $i}) {
  109. $reason = new \stdClass();
  110. $reason->code = $this->{'note_reason_icd' . $i};
  111. $reason->description = $this->{'note_reason_icd' . $i . 'description'};
  112. $reasons[] = $reason;
  113. }
  114. }
  115. return $reasons;
  116. }
  117. public function reasonsLog() {
  118. return $this->hasMany(NoteReasons::class, 'note_id', 'id')->orderBy('created_at', 'DESC');
  119. }
  120. public function claims()
  121. {
  122. return $this->hasMany(Claim::class, 'note_id', 'id')->orderBy('created_at', 'DESC');
  123. }
  124. public function summary()
  125. {
  126. $parts = [];
  127. foreach ($this->sections as $section) {
  128. $parts[] = $section->summary_html;
  129. }
  130. $parts = array_map(function($_part) {
  131. return '<div class="note-section-summary">' . $_part . '</div>';
  132. }, $parts);
  133. return implode("", $parts);
  134. }
  135. public function hcpCompany()
  136. {
  137. return $this->hasOne(Company::class, 'id', 'hcp_company_id');
  138. }
  139. public function hcpCompanyProPayer()
  140. {
  141. return $this->hasOne(CompanyProPayer::class, 'id', 'hcp_company_pro_payer_id');
  142. }
  143. public function hcpCompanyLocation()
  144. {
  145. return $this->hasOne(CompanyLocation::class, 'id', 'hcp_company_location_id');
  146. }
  147. public function flaggedForSupervisingPhysicianReviewBySession(){
  148. return $this->hasOne(AppSession::class, 'id', 'flagged_for_supervising_physician_review_by_session_id');
  149. }
  150. public function stampedBySupervisingPhysicianBySession(){
  151. return $this->hasOne(AppSession::class, 'id', 'stamped_by_by_supervising_physician_by_session_id');
  152. }
  153. public function currentNotePickupForProcessing() {
  154. return $this->hasOne(NotePickupForProcessing::class, 'id', 'current_note_pickup_for_processing_id');
  155. }
  156. public function visitTemplate()
  157. {
  158. return $this->hasOne(VisitTemplate::class, 'id', 'visit_template_id');
  159. }
  160. public function hasTreatmentServicesBillByHCP()
  161. {
  162. $bills = Bill::where('note_id', $this->id)
  163. ->where('bill_service_type', '<>', 'GENERIC')
  164. ->where('code', 'Treatment Services')
  165. ->where('is_cancelled', false)
  166. ->count();
  167. return !!$bills;
  168. }
  169. public function segmentsWithPendingSummarySuggestion() {
  170. return $this->hasMany(Segment::class, 'note_id', 'id')
  171. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  172. ->where('is_active', true)
  173. ->whereHas('proposedSegmentSummarySuggestion', function($sugQuery) {
  174. return $sugQuery->where('status', '=', 'PENDING');
  175. })
  176. ->orderBy('position_index', 'asc');
  177. }
  178. public function hasUnacknowledgedCancelledBillsByPro(Pro $pro) {
  179. return Bill::where('note_id', $this->id)
  180. ->where('hcp_pro_id', $pro->id)
  181. ->where('bill_service_type', '<>', 'GENERIC')
  182. ->where('is_cancelled', true)
  183. ->where('is_cancelled_by_administrator', true)
  184. ->where('is_cancellation_acknowledged', false)
  185. ->count();
  186. }
  187. public function hasClaims() {
  188. return Claim::where('note_id', $this->id)
  189. ->where('status', '!=', 'CANCELLED')
  190. ->count();
  191. }
  192. public function isProPhysicianSupervisor($proID){
  193. $companyProIds = CompanyPro::where('pro_id', $proID)->pluck('id')->toArray();
  194. $noteCompanyProId = $this->hcpCompanyPro->supervising_physician_company_pro_id;
  195. if(!$noteCompanyProId) return false;
  196. return in_array($noteCompanyProId, $companyProIds);
  197. }
  198. }