Note.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 receivedPayments()
  129. {
  130. return $this->hasMany(ReceivedPayment::class, 'note_id', 'id')->orderBy('created_at', 'DESC');
  131. }
  132. public function summary()
  133. {
  134. $parts = [];
  135. foreach ($this->sections as $section) {
  136. $parts[] = $section->summary_html;
  137. }
  138. $parts = array_map(function($_part) {
  139. return '<div class="note-section-summary">' . $_part . '</div>';
  140. }, $parts);
  141. return implode("", $parts);
  142. }
  143. public function hcpCompany()
  144. {
  145. return $this->hasOne(Company::class, 'id', 'hcp_company_id');
  146. }
  147. public function hcpCompanyProPayer()
  148. {
  149. return $this->hasOne(CompanyProPayer::class, 'id', 'hcp_company_pro_payer_id');
  150. }
  151. public function hcpCompanyLocation()
  152. {
  153. return $this->hasOne(CompanyLocation::class, 'id', 'hcp_company_location_id');
  154. }
  155. public function flaggedForSupervisingPhysicianReviewBySession(){
  156. return $this->hasOne(AppSession::class, 'id', 'flagged_for_supervising_physician_review_by_session_id');
  157. }
  158. public function stampedBySupervisingPhysicianBySession(){
  159. return $this->hasOne(AppSession::class, 'id', 'stamped_by_by_supervising_physician_by_session_id');
  160. }
  161. public function currentNotePickupForProcessing() {
  162. return $this->hasOne(NotePickupForProcessing::class, 'id', 'current_note_pickup_for_processing_id');
  163. }
  164. public function visitTemplate()
  165. {
  166. return $this->hasOne(VisitTemplate::class, 'id', 'visit_template_id');
  167. }
  168. public function hasTreatmentServicesBillByHCP()
  169. {
  170. $bills = Bill::where('note_id', $this->id)
  171. ->where('bill_service_type', '<>', 'GENERIC')
  172. ->where('code', 'Treatment Services')
  173. ->where('is_cancelled', false)
  174. ->count();
  175. return !!$bills;
  176. }
  177. public function segmentsWithPendingSummarySuggestion() {
  178. return $this->hasMany(Segment::class, 'note_id', 'id')
  179. ->where('id', '!=', $this->core_segment_id) // dont include core-segment
  180. ->where('is_active', true)
  181. ->whereHas('proposedSegmentSummarySuggestion', function($sugQuery) {
  182. return $sugQuery->where('status', '=', 'PENDING');
  183. })
  184. ->orderBy('position_index', 'asc');
  185. }
  186. public function hasUnacknowledgedCancelledBillsByPro(Pro $pro) {
  187. return Bill::where('note_id', $this->id)
  188. ->where('hcp_pro_id', $pro->id)
  189. ->where('bill_service_type', '<>', 'GENERIC')
  190. ->where('is_cancelled', true)
  191. ->where('is_cancelled_by_administrator', true)
  192. ->where('is_cancellation_acknowledged', false)
  193. ->count();
  194. }
  195. public function hasClaims() {
  196. return Claim::where('note_id', $this->id)
  197. ->where('status', '!=', 'CANCELLED')
  198. ->count();
  199. }
  200. public function isProPhysicianSupervisor($proID){
  201. $companyProIds = CompanyPro::where('pro_id', $proID)->pluck('id')->toArray();
  202. $noteCompanyProId = $this->hcpCompanyPro->supervising_physician_company_pro_id;
  203. if(!$noteCompanyProId) return false;
  204. return in_array($noteCompanyProId, $companyProIds);
  205. }
  206. public function cmSetupManagerSignatureStatusUpdatedBySession(){
  207. return $this->hasOne(AppSession::class, 'id', 'cm_setup_manager_signature_status_updated_by_session_id');
  208. }
  209. }