CareMonth.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 rtmEntries() {
  33. return $this->hasMany(CareMonthEntry::class, 'care_month_id', 'id')
  34. ->whereRaw("(cm_or_rm_or_rtm_msk_or_rtm_lung = 'RTM_MSK' OR cm_or_rm_or_rtm_msk_or_rtm_lung = 'RTM_LUNG')")
  35. ->orderBy('effective_date', 'DESC');
  36. }
  37. public function bills() {
  38. return $this->hasMany(Bill::class, 'care_month_id', 'id');
  39. }
  40. public function claims() {
  41. return $this->hasMany(Claim::class, 'care_month_id', 'id')->where('status', '<>', 'CANCELLED');
  42. }
  43. public function getBillsOfType($_type) {
  44. $bills = $this->bills;
  45. $targetBills = new Collection();
  46. foreach ($bills as $bill) {
  47. if($bill->cm_or_rm === $_type) {
  48. $targetBills->add($bill);
  49. }
  50. }
  51. return $targetBills;
  52. }
  53. public function rmBill(){
  54. return $this->hasOne(Bill::class, 'id', 'rm_bill_id');
  55. }
  56. public function companyPro()
  57. {
  58. return $this->hasOne(CompanyPro::class, 'id', 'company_pro_id');
  59. }
  60. public function company()
  61. {
  62. return $this->hasOne(Company::class, 'id', 'company_id');
  63. }
  64. public function companyProPayer()
  65. {
  66. return $this->hasOne(CompanyProPayer::class, 'id', 'company_pro_payer_id');
  67. }
  68. public function companyLocation()
  69. {
  70. return $this->hasOne(CompanyLocation::class, 'id', 'company_location_id');
  71. }
  72. public function cmReasons()
  73. {
  74. return $this->hasMany(CareMonthCmRmReason::class, 'care_month_id', 'id')
  75. ->where('cm_or_rm', 'CM')
  76. ->orderBy('position_index', 'ASC')
  77. ->orderBy('code', 'ASC');
  78. }
  79. public function rmReasons()
  80. {
  81. return $this->hasMany(CareMonthCmRmReason::class, 'care_month_id', 'id')
  82. ->where('cm_or_rm', 'RM')
  83. ->orderBy('position_index', 'ASC')
  84. ->orderBy('code', 'ASC');
  85. }
  86. public function rmSetupClaim()
  87. {
  88. return $this->hasOne(Claim::class, 'id', 'rm_setup_claim_id')
  89. ->where('status', '<>', 'CANCELLED');
  90. }
  91. public function mostRecentMcpNote()
  92. {
  93. return $this->hasOne(Note::class, 'id', 'most_recent_mcp_note_id');
  94. }
  95. public function note()
  96. {
  97. return $this->hasOne(Note::class, 'id', 'note_id');
  98. }
  99. public function mcpRmGenericBill()
  100. {
  101. return $this->hasOne(Bill::class, 'id', 'mcp_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
  102. }
  103. public function rmmRmGenericBill()
  104. {
  105. return $this->hasOne(Bill::class, 'id', 'rmm_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
  106. }
  107. public function showMeasurementDaysWarning(){
  108. return ($this->daysSinceLastMeasurement() >= 2) || (16 - $this->number_of_days_with_remote_measurements) >= $this->daysTillEndOfMonth();
  109. }
  110. public function daysSinceLastMeasurement(){
  111. if(!$this->most_recent_cellular_measurement_at) {
  112. return 999;
  113. }
  114. $d1 = new DateTime($this->most_recent_cellular_measurement_at);
  115. return $d1->diff(new DateTime())->days;
  116. }
  117. public function daysTillEndOfMonth(){
  118. return date('t') - date('j');
  119. }
  120. public function calculateBillabilityForMcp(){
  121. $strategy = $this->mcp_rpm_payment_strategy;
  122. if(!$strategy){
  123. return [
  124. 'billable' => false,
  125. 'reason' => 'MCP RPM strategy has not been set.'
  126. ];
  127. }
  128. $mcpRpmPaymentAmount = $this->mcp_rpm_payment_amount;
  129. $has16PlusDays = $this->number_of_days_with_remote_measurements >= 16;
  130. $hasMcpBilled20Minutes = $this->rm_total_time_in_seconds_by_mcp >= 1200;
  131. $hasMcpInteracted = $this->has_mcp_interacted_with_client_about_rm;
  132. if (is_null($this->days_between_most_recent_mcp_note_date_and_end_of_care_month) || $this->days_between_most_recent_mcp_note_date_and_end_of_care_month > 120) {
  133. return [
  134. 'billable' => false,
  135. 'reason' => "Patient has not had a visit recent enough to bill for RPM"
  136. ];
  137. }
  138. if($strategy == 'X16_DAYS'){
  139. //only check for 16 days
  140. if($has16PlusDays){
  141. return [
  142. 'billable' => true,
  143. 'amount' => $mcpRpmPaymentAmount
  144. ];
  145. } else {
  146. //not billable
  147. return [
  148. 'billable' => false,
  149. 'reason' => "This care month does not have 16 or more measurement days."
  150. ];
  151. }
  152. }
  153. if($strategy == 'X16_DAYS_20_MINS_ON_OWN_MCP_COM_DURING_CM'){
  154. if ($has16PlusDays && $hasMcpBilled20Minutes && $hasMcpInteracted) {
  155. return [
  156. 'billable' => true,
  157. 'amount' => $mcpRpmPaymentAmount
  158. ];
  159. } else {
  160. if(!$has16PlusDays){
  161. return [
  162. 'billable' => false,
  163. 'reason' => 'Care month does not have 16 or more measurement days.'
  164. ];
  165. }
  166. if(!$hasMcpBilled20Minutes){
  167. return [
  168. 'billable' => false,
  169. 'reason' => 'Care month does not have 20 minutes billed time.'
  170. ];
  171. }
  172. if(!$hasMcpInteracted){
  173. return [
  174. 'billable' => false,
  175. 'reason' => 'Care month does MCP interaction.'
  176. ];
  177. }
  178. }
  179. }
  180. }
  181. public function calculateBillabilityForRmm(){
  182. if(!$this->rmmPro) {
  183. return [
  184. 'billable' => false,
  185. 'reason' => 'RMM not set on the care month.'
  186. ];
  187. }
  188. $strategy = $this->rmm_payment_strategy;
  189. if(!$strategy){
  190. return [
  191. 'billable' => false,
  192. 'reason' => 'RPM strategy has not been set.'
  193. ];
  194. }
  195. $rmmRpmPaymentAmount = $this->rmm_payment_amount;
  196. $has16PlusDays = $this->number_of_days_with_remote_measurements >= 16;
  197. $hasRmmBilled20Minutes = $this->rm_total_time_in_seconds_by_rmm_pro >= 1200;
  198. $hasMcpInteracted = $this->has_mcp_interacted_with_client_about_rm;
  199. if (is_null($this->days_between_most_recent_mcp_note_date_and_end_of_care_month) || $this->days_between_most_recent_mcp_note_date_and_end_of_care_month > 120) {
  200. return [
  201. 'billable' => false,
  202. 'reason' => "Patient has not had a visit recent enough to bill for RPM"
  203. ];
  204. }
  205. if($strategy == 'X16_DAYS'){
  206. //only check for 16 days
  207. if($has16PlusDays){
  208. return [
  209. 'billable' => true,
  210. 'amount' => $rmmRpmPaymentAmount
  211. ];
  212. } else {
  213. //not billable
  214. return [
  215. 'billable' => false,
  216. 'reason' => "This care month does not have 16 or more measurement days."
  217. ];
  218. }
  219. }
  220. if($strategy == 'X16_DAYS_20_MINS_ON_OWN_MCP_COM_DURING_CM'){
  221. if ($has16PlusDays && $hasRmmBilled20Minutes && $hasMcpInteracted) {
  222. return [
  223. 'billable' => true,
  224. 'amount' => $rmmRpmPaymentAmount
  225. ];
  226. } else {
  227. if(!$has16PlusDays){
  228. return [
  229. 'billable' => false,
  230. 'reason' => 'Care month does not have 16 or more measurement days.'
  231. ];
  232. }
  233. if(!$hasRmmBilled20Minutes){
  234. return [
  235. 'billable' => false,
  236. 'reason' => 'Care month does not have 20 minutes in entries.'
  237. ];
  238. }
  239. if(!$hasMcpInteracted){
  240. return [
  241. 'billable' => false,
  242. 'reason' => 'Care month does not have MCP interaction.'
  243. ];
  244. }
  245. }
  246. }
  247. }
  248. public function rtmMskTransmissions() {
  249. return $this->hasMany(ClientRtmTransmission::class, 'care_month_id', 'id')
  250. ->where('msk_or_lung', 'MSK')
  251. ->orderBy('effective_date', 'DESC');
  252. }
  253. public function rtmLungTransmissions() {
  254. return $this->hasMany(ClientRtmTransmission::class, 'care_month_id', 'id')
  255. ->where('msk_or_lung', 'LUNG')
  256. ->orderBy('effective_date', 'DESC');
  257. }
  258. }