CareMonth.php 8.5 KB

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