CareMonth.php 10 KB

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