CareMonth.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 mcpRmGenericBill()
  119. {
  120. return $this->hasOne(Bill::class, 'id', 'mcp_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
  121. }
  122. public function rmmRmGenericBill()
  123. {
  124. return $this->hasOne(Bill::class, 'id', 'rmm_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
  125. }
  126. public function showMeasurementDaysWarning(){
  127. return ($this->daysSinceLastMeasurement() >= 2) || (16 - $this->number_of_days_with_remote_measurements) >= $this->daysTillEndOfMonth();
  128. }
  129. public function daysSinceLastMeasurement(){
  130. if(!$this->most_recent_cellular_measurement_at) {
  131. return 999;
  132. }
  133. $d1 = new DateTime($this->most_recent_cellular_measurement_at);
  134. return $d1->diff(new DateTime())->days;
  135. }
  136. public function daysTillEndOfMonth(){
  137. return date('t') - date('j');
  138. }
  139. public function calculateBillabilityForMcp(){
  140. $strategy = $this->mcp_rpm_payment_strategy;
  141. if(!$strategy){
  142. return [
  143. 'billable' => false,
  144. 'reason' => 'MCP RPM strategy has not been set.'
  145. ];
  146. }
  147. $mcpRpmPaymentAmount = $this->mcp_rpm_payment_amount;
  148. $has16PlusDays = $this->number_of_days_with_remote_measurements >= 16;
  149. $hasMcpBilled20Minutes = $this->rm_total_time_in_seconds_by_mcp >= 1200;
  150. $hasMcpInteracted = $this->has_mcp_interacted_with_client_about_rm;
  151. 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')) {
  152. return [
  153. 'billable' => false,
  154. 'reason' => "Patient has not had a visit recent enough to bill for RPM"
  155. ];
  156. }
  157. if($strategy == 'X16_DAYS'){
  158. //only check for 16 days
  159. if($has16PlusDays){
  160. return [
  161. 'billable' => true,
  162. 'amount' => $mcpRpmPaymentAmount
  163. ];
  164. } else {
  165. //not billable
  166. return [
  167. 'billable' => false,
  168. 'reason' => "This care month does not have 16 or more measurement days."
  169. ];
  170. }
  171. }
  172. if($strategy == 'X16_DAYS_20_MINS_ON_OWN_MCP_COM_DURING_CM'){
  173. if ($has16PlusDays && $hasMcpBilled20Minutes && $hasMcpInteracted) {
  174. return [
  175. 'billable' => true,
  176. 'amount' => $mcpRpmPaymentAmount
  177. ];
  178. } else {
  179. if(!$has16PlusDays){
  180. return [
  181. 'billable' => false,
  182. 'reason' => 'Care month does not have 16 or more measurement days.'
  183. ];
  184. }
  185. if(!$hasMcpBilled20Minutes){
  186. return [
  187. 'billable' => false,
  188. 'reason' => 'Care month does not have 20 minutes billed time.'
  189. ];
  190. }
  191. if(!$hasMcpInteracted){
  192. return [
  193. 'billable' => false,
  194. 'reason' => 'Care month does MCP interaction.'
  195. ];
  196. }
  197. }
  198. }
  199. }
  200. public function calculateBillabilityForRmm(){
  201. if(!$this->rmmPro) {
  202. return [
  203. 'billable' => false,
  204. 'reason' => 'RMM not set on the care month.'
  205. ];
  206. }
  207. $strategy = $this->rmm_payment_strategy;
  208. if(!$strategy){
  209. return [
  210. 'billable' => false,
  211. 'reason' => 'RPM strategy has not been set.'
  212. ];
  213. }
  214. $rmmRpmPaymentAmount = $this->rmm_payment_amount;
  215. $has16PlusDays = $this->number_of_days_with_remote_measurements >= 16;
  216. $hasRmmBilled20Minutes = $this->rm_total_time_in_seconds_by_rmm_pro >= 1200;
  217. $hasMcpInteracted = $this->has_mcp_interacted_with_client_about_rm;
  218. 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')) {
  219. return [
  220. 'billable' => false,
  221. 'reason' => "Patient has not had a visit recent enough to bill for RPM"
  222. ];
  223. }
  224. if($strategy == 'X16_DAYS'){
  225. //only check for 16 days
  226. if($has16PlusDays){
  227. return [
  228. 'billable' => true,
  229. 'amount' => $rmmRpmPaymentAmount
  230. ];
  231. } else {
  232. //not billable
  233. return [
  234. 'billable' => false,
  235. 'reason' => "This care month does not have 16 or more measurement days."
  236. ];
  237. }
  238. }
  239. if($strategy == 'X16_DAYS_20_MINS_ON_OWN_MCP_COM_DURING_CM'){
  240. if ($has16PlusDays && $hasRmmBilled20Minutes && $hasMcpInteracted) {
  241. return [
  242. 'billable' => true,
  243. 'amount' => $rmmRpmPaymentAmount
  244. ];
  245. } else {
  246. if(!$has16PlusDays){
  247. return [
  248. 'billable' => false,
  249. 'reason' => 'Care month does not have 16 or more measurement days.'
  250. ];
  251. }
  252. if(!$hasRmmBilled20Minutes){
  253. return [
  254. 'billable' => false,
  255. 'reason' => 'Care month does not have 20 minutes in entries.'
  256. ];
  257. }
  258. if(!$hasMcpInteracted){
  259. return [
  260. 'billable' => false,
  261. 'reason' => 'Care month does not have MCP interaction.'
  262. ];
  263. }
  264. }
  265. }
  266. }
  267. public function rtmMskTransmissions() {
  268. return $this->hasMany(ClientRtmTransmission::class, 'care_month_id', 'id')
  269. ->where('msk_or_lung', 'MSK')
  270. ->orderBy('effective_date', 'DESC')
  271. ->orderBy('created_at', 'DESC');
  272. }
  273. public function rtmLungTransmissions() {
  274. return $this->hasMany(ClientRtmTransmission::class, 'care_month_id', 'id')
  275. ->where('msk_or_lung', 'LUNG')
  276. ->orderBy('effective_date', 'DESC')
  277. ->orderBy('created_at', 'DESC');
  278. }
  279. }