CareMonth.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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')->orderBy('effective_date', 'DESC');
  29. }
  30. public function bills() {
  31. return $this->hasMany(Bill::class, 'care_month_id', 'id');
  32. }
  33. public function claims() {
  34. return $this->hasMany(Claim::class, 'care_month_id', 'id')->where('status', '<>', 'CANCELLED');
  35. }
  36. public function getBillsOfType($_type) {
  37. $bills = $this->bills;
  38. $targetBills = new Collection();
  39. foreach ($bills as $bill) {
  40. if($bill->cm_or_rm === $_type) {
  41. $targetBills->add($bill);
  42. }
  43. }
  44. return $targetBills;
  45. }
  46. public function rmBill(){
  47. return $this->hasOne(Bill::class, 'id', 'rm_bill_id');
  48. }
  49. public function companyPro()
  50. {
  51. return $this->hasOne(CompanyPro::class, 'id', 'company_pro_id');
  52. }
  53. public function company()
  54. {
  55. return $this->hasOne(Company::class, 'id', 'company_id');
  56. }
  57. public function companyProPayer()
  58. {
  59. return $this->hasOne(CompanyProPayer::class, 'id', 'company_pro_payer_id');
  60. }
  61. public function companyLocation()
  62. {
  63. return $this->hasOne(CompanyLocation::class, 'id', 'company_location_id');
  64. }
  65. public function cmReasons()
  66. {
  67. return $this->hasMany(CareMonthCmRmReason::class, 'care_month_id', 'id')
  68. ->where('cm_or_rm', 'CM')
  69. ->orderBy('position_index', 'ASC')
  70. ->orderBy('code', 'ASC');
  71. }
  72. public function rmReasons()
  73. {
  74. return $this->hasMany(CareMonthCmRmReason::class, 'care_month_id', 'id')
  75. ->where('cm_or_rm', 'RM')
  76. ->orderBy('position_index', 'ASC')
  77. ->orderBy('code', 'ASC');
  78. }
  79. public function rmSetupClaim()
  80. {
  81. return $this->hasOne(Claim::class, 'id', 'rm_setup_claim_id')
  82. ->where('status', '<>', 'CANCELLED');
  83. }
  84. public function mostRecentMcpNote()
  85. {
  86. return $this->hasOne(Note::class, 'id', 'most_recent_mcp_note_id');
  87. }
  88. public function note()
  89. {
  90. return $this->hasOne(Note::class, 'id', 'note_id');
  91. }
  92. public function mcpRmGenericBill()
  93. {
  94. return $this->hasOne(Bill::class, 'id', 'mcp_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
  95. }
  96. public function rmmRmGenericBill()
  97. {
  98. return $this->hasOne(Bill::class, 'id', 'rmm_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
  99. }
  100. public function showMeasurementDaysWarning(){
  101. return ($this->daysSinceLastMeasurement() >= 2) || (16 - $this->number_of_days_with_remote_measurements) >= $this->daysTillEndOfMonth();
  102. }
  103. public function daysSinceLastMeasurement(){
  104. if(!$this->most_recent_cellular_measurement_at) {
  105. return 999;
  106. }
  107. $d1 = new DateTime($this->most_recent_cellular_measurement_at);
  108. return $d1->diff(new DateTime())->days;
  109. }
  110. public function daysTillEndOfMonth(){
  111. return date('t') - date('j');
  112. }
  113. public function calculateBillabilityForMcp(){
  114. $strategy = $this->mcp->mcp_rpm_payment_strategy;
  115. if(!$strategy){
  116. return [
  117. 'billable' => false,
  118. 'reason' => 'MCP RPM strategy has not been set.'
  119. ];
  120. }
  121. $mcpRpmPaymentAmount = $this->mcp->mcp_rpm_payment_amount;
  122. $has16PlusDays = $this->number_of_days_with_remote_measurements >= 16;
  123. $hasMcpBilled20Minutes = $this->rm_total_time_in_seconds_by_mcp >= 1200;
  124. $hasMcpInteracted = $this->has_mcp_interacted_with_client_about_rm;
  125. if($strategy == 'X16_DAYS'){
  126. //only check for 16 days
  127. if($has16PlusDays){
  128. return [
  129. 'billable' => true,
  130. 'amount' => $mcpRpmPaymentAmount
  131. ];
  132. } else {
  133. //not billable
  134. return [
  135. 'billable' => false,
  136. 'reason' => "This care month does not have 16 or more measurement days."
  137. ];
  138. }
  139. }
  140. if($strategy == 'X16_DAYS_20_MINS_ON_OWN_MCP_COM_DURING_CM'){
  141. if ($has16PlusDays && $hasMcpBilled20Minutes && $hasMcpInteracted) {
  142. return [
  143. 'billable' => true,
  144. 'amount' => $mcpRpmPaymentAmount
  145. ];
  146. } else {
  147. if(!$has16PlusDays){
  148. return [
  149. 'billable' => false,
  150. 'reason' => 'Care month does not have 16 or more measurement days.'
  151. ];
  152. }
  153. if(!$hasMcpBilled20Minutes){
  154. return [
  155. 'billable' => false,
  156. 'reason' => 'Care month does not have 20 minutes billed time.'
  157. ];
  158. }
  159. if(!$hasMcpInteracted){
  160. return [
  161. 'billable' => false,
  162. 'reason' => 'Care month does MCP interaction.'
  163. ];
  164. }
  165. }
  166. }
  167. }
  168. }