CareMonth.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Models;
  3. # use Illuminate\Database\Eloquent\Model;
  4. use DateTime;
  5. use Illuminate\Support\Collection;
  6. class CareMonth extends Model
  7. {
  8. protected $table = 'care_month';
  9. public function patient(){
  10. return $this->hasOne(Client::class, 'id', 'client_id');
  11. }
  12. public function client(){
  13. return $this->hasOne(Client::class, 'id', 'client_id');
  14. }
  15. public function mcp(){
  16. return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
  17. }
  18. public function cmPro(){
  19. return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
  20. }
  21. public function rmmPro(){
  22. return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
  23. }
  24. public function rmePro(){
  25. return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
  26. }
  27. public function entries() {
  28. return $this->hasMany(CareMonthEntry::class, 'care_month_id', 'id')
  29. ->where('cm_or_rm_or_rtm_msk_or_rtm_lung', 'RM')
  30. ->orderBy('effective_date', 'DESC');
  31. }
  32. public function entriesByPro($_proId) {
  33. return CareMonthEntry::where('care_month_id', $this->id)
  34. ->where('pro_id', $_proId)
  35. ->where('cm_or_rm_or_rtm_msk_or_rtm_lung', 'RM')
  36. ->orderBy('effective_date', 'DESC')
  37. ->get();
  38. }
  39. public function entriesNotByPro($_proId) {
  40. return CareMonthEntry::where('care_month_id', $this->id)
  41. ->where('pro_id', '!=', $_proId)
  42. ->where('cm_or_rm_or_rtm_msk_or_rtm_lung', 'RM')
  43. ->orderBy('effective_date', 'DESC')
  44. ->get();
  45. }
  46. public function rtmEntries() {
  47. return $this->hasMany(CareMonthEntry::class, 'care_month_id', 'id')
  48. ->whereRaw("(cm_or_rm_or_rtm_msk_or_rtm_lung = 'RTM_MSK' OR cm_or_rm_or_rtm_msk_or_rtm_lung = 'RTM_LUNG')")
  49. ->orderBy('effective_date', 'DESC');
  50. }
  51. public function ccmEntries() {
  52. return $this->hasMany(CareMonthEntry::class, 'care_month_id', 'id')
  53. ->whereRaw("(cm_or_rm_or_rtm_msk_or_rtm_lung = 'CM')")
  54. ->orderBy('effective_date', 'DESC');
  55. }
  56. public function bills() {
  57. return $this->hasMany(Bill::class, 'care_month_id', 'id');
  58. }
  59. public function claims() {
  60. return $this->hasMany(Claim::class, 'care_month_id', 'id')->where('status', '<>', 'CANCELLED');
  61. }
  62. public function getBillsOfType($_type) {
  63. $bills = $this->bills;
  64. $targetBills = new Collection();
  65. foreach ($bills as $bill) {
  66. if($bill->cm_or_rm === $_type) {
  67. $targetBills->add($bill);
  68. }
  69. }
  70. return $targetBills;
  71. }
  72. public function rmBill(){
  73. return $this->hasOne(Bill::class, 'id', 'rm_bill_id');
  74. }
  75. public function companyPro()
  76. {
  77. return $this->hasOne(CompanyPro::class, 'id', 'company_pro_id');
  78. }
  79. public function company()
  80. {
  81. return $this->hasOne(Company::class, 'id', 'company_id');
  82. }
  83. public function companyProPayer()
  84. {
  85. return $this->hasOne(CompanyProPayer::class, 'id', 'company_pro_payer_id');
  86. }
  87. public function companyLocation()
  88. {
  89. return $this->hasOne(CompanyLocation::class, 'id', 'company_location_id');
  90. }
  91. public function cmReasons()
  92. {
  93. return $this->hasMany(CareMonthCmRmReason::class, 'care_month_id', 'id')
  94. ->where('cm_or_rm', 'CM')
  95. ->orderBy('position_index', 'ASC')
  96. ->orderBy('code', 'ASC');
  97. }
  98. public function rmReasons()
  99. {
  100. return $this->hasMany(CareMonthCmRmReason::class, 'care_month_id', 'id')
  101. ->where('cm_or_rm', 'RM')
  102. ->orderBy('position_index', 'ASC')
  103. ->orderBy('code', 'ASC');
  104. }
  105. public function rmSetupClaim()
  106. {
  107. return $this->hasOne(Claim::class, 'id', 'rm_setup_claim_id')
  108. ->where('status', '<>', 'CANCELLED');
  109. }
  110. public function mostRecentMcpNote()
  111. {
  112. return $this->hasOne(Note::class, 'id', 'most_recent_mcp_note_id');
  113. }
  114. public function note()
  115. {
  116. return $this->hasOne(Note::class, 'id', 'note_id');
  117. }
  118. public function mcpInteractionNote()
  119. {
  120. return $this->hasOne(Note::class, 'id', 'mcp_rm_interaction_note_id');
  121. }
  122. public function mcpRmGenericBill()
  123. {
  124. return $this->hasOne(Bill::class, 'id', 'mcp_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
  125. }
  126. public function rmmRmGenericBill()
  127. {
  128. return $this->hasOne(Bill::class, 'id', 'rmm_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
  129. }
  130. public function showMeasurementDaysWarning(){
  131. return ($this->daysSinceLastMeasurement() >= 2) || (16 - $this->number_of_days_with_remote_measurements) >= $this->daysTillEndOfMonth();
  132. }
  133. public function daysSinceLastMeasurement(){
  134. if(!$this->most_recent_cellular_measurement_at) {
  135. return 999;
  136. }
  137. $d1 = new DateTime($this->most_recent_cellular_measurement_at);
  138. return $d1->diff(new DateTime())->days;
  139. }
  140. public function daysTillEndOfMonth(){
  141. return date('t') - date('j');
  142. }
  143. public function calculateBillabilityForMcp(){
  144. $tier1Strategy = $this->mcp_rpm_payment_strategy_tier_one;
  145. $tier2Strategy = $this->mcp_rpm_payment_strategy_tier_two;
  146. if(!$tier1Strategy || !$tier2Strategy){
  147. return [
  148. 'billable' => false,
  149. 'reason' => 'MCP RPM strategy has not been set.'
  150. ];
  151. }
  152. $tier = determineTier($this);
  153. if (!$tier) {
  154. return [
  155. 'billable' => false,
  156. 'reason' => "Patient has not had a visit recent enough to bill for RPM"
  157. ];
  158. }
  159. if($tier == 'TIER_1'){
  160. return [
  161. 'billable' => true,
  162. 'amount' => $this->mcp_rpm_payment_amount_tier_one
  163. ];
  164. }
  165. if($tier == 'TIER_2'){
  166. return [
  167. 'billable' => true,
  168. 'amount' => $this->mcp_rpm_payment_amount_tier_two
  169. ];
  170. }
  171. }
  172. public function calculateBillabilityForRmm(){
  173. if(!$this->rmmPro) {
  174. return [
  175. 'billable' => false,
  176. 'reason' => 'RMM not set on the care month.'
  177. ];
  178. }
  179. $tier1Strategy = $this->mcp_rpm_payment_tier_one_strategy;
  180. $tier2Strategy = $this->mcp_rpm_payment_tier_two_strategy;
  181. if(!$tier1Strategy || !$tier2Strategy){
  182. return [
  183. 'billable' => false,
  184. 'reason' => 'RPM strategy has not been set.'
  185. ];
  186. }
  187. $tier = determineTier($this);
  188. $rmmRpmPaymentAmount = $this->rmm_payment_amount;
  189. if (!$tier) {
  190. return [
  191. 'billable' => false,
  192. 'reason' => "Patient has not had a visit recent enough to bill for RPM"
  193. ];
  194. }
  195. if($tier == 'TIER_1'){
  196. return [
  197. 'billable' => true,
  198. 'amount' => $rmmRpmPaymentAmount
  199. ];
  200. }
  201. if($tier == 'TIER_2'){
  202. return [
  203. 'billable' => true,
  204. 'amount' => $rmmRpmPaymentAmount
  205. ];
  206. }
  207. }
  208. public function rtmMskTransmissions() {
  209. return $this->hasMany(ClientRtmTransmission::class, 'care_month_id', 'id')
  210. ->where('msk_or_lung', 'MSK')
  211. ->orderBy('effective_date', 'DESC')
  212. ->orderBy('created_at', 'DESC');
  213. }
  214. public function rtmLungTransmissions() {
  215. return $this->hasMany(ClientRtmTransmission::class, 'care_month_id', 'id')
  216. ->where('msk_or_lung', 'LUNG')
  217. ->orderBy('effective_date', 'DESC')
  218. ->orderBy('created_at', 'DESC');
  219. }
  220. public function measurements() {
  221. return $this->hasMany(Measurement::class, 'care_month_id', 'id')
  222. ->orderBy('created_at', 'DESC');
  223. }
  224. public function tier() {
  225. return determineTier($this);
  226. }
  227. }