CareMonth.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  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');
  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');
  95. }
  96. public function rmmRmGenericBill()
  97. {
  98. return $this->hasOne(Bill::class, 'id', 'rmm_rm_generic_bill_id');
  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. }