Note.php 7.4 KB

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