123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace App\Models;
- # use Illuminate\Database\Eloquent\Model;
- use DateTime;
- use Illuminate\Support\Collection;
- class CareMonth extends Model
- {
- protected $table = 'care_month';
- public function patient(){
- return $this->hasOne(Client::class, 'id', 'client_id');
- }
- public function client(){
- return $this->hasOne(Client::class, 'id', 'client_id');
- }
- public function mcp(){
- return $this->hasOne(Pro::class, 'id', 'mcp_pro_id');
- }
- public function cmPro(){
- return $this->hasOne(Pro::class, 'id', 'cm_pro_id');
- }
- public function rmmPro(){
- return $this->hasOne(Pro::class, 'id', 'rmm_pro_id');
- }
- public function rmePro(){
- return $this->hasOne(Pro::class, 'id', 'rme_pro_id');
- }
- public function entries() {
- return $this->hasMany(CareMonthEntry::class, 'care_month_id', 'id')
- ->where('cm_or_rm_or_rtm_msk_or_rtm_lung', 'RM')
- ->orderBy('effective_date', 'DESC');
- }
- public function entriesByPro($_proId) {
- return CareMonthEntry::where('care_month_id', $this->id)
- ->where('pro_id', $_proId)
- ->where('cm_or_rm_or_rtm_msk_or_rtm_lung', 'RM')
- ->orderBy('effective_date', 'DESC')
- ->get();
- }
- public function entriesNotByPro($_proId) {
- return CareMonthEntry::where('care_month_id', $this->id)
- ->where('pro_id', '!=', $_proId)
- ->where('cm_or_rm_or_rtm_msk_or_rtm_lung', 'RM')
- ->orderBy('effective_date', 'DESC')
- ->get();
- }
- public function rtmEntries() {
- return $this->hasMany(CareMonthEntry::class, 'care_month_id', 'id')
- ->whereRaw("(cm_or_rm_or_rtm_msk_or_rtm_lung = 'RTM_MSK' OR cm_or_rm_or_rtm_msk_or_rtm_lung = 'RTM_LUNG')")
- ->orderBy('effective_date', 'DESC');
- }
- public function ccmEntries() {
- return $this->hasMany(CareMonthEntry::class, 'care_month_id', 'id')
- ->whereRaw("(cm_or_rm_or_rtm_msk_or_rtm_lung = 'CM')")
- ->orderBy('effective_date', 'DESC');
- }
- public function bills() {
- return $this->hasMany(Bill::class, 'care_month_id', 'id');
- }
- public function claims() {
- return $this->hasMany(Claim::class, 'care_month_id', 'id')->where('status', '<>', 'CANCELLED');
- }
- public function getBillsOfType($_type) {
- $bills = $this->bills;
- $targetBills = new Collection();
- foreach ($bills as $bill) {
- if($bill->cm_or_rm === $_type) {
- $targetBills->add($bill);
- }
- }
- return $targetBills;
- }
- public function rmBill(){
- return $this->hasOne(Bill::class, 'id', 'rm_bill_id');
- }
- public function companyPro()
- {
- return $this->hasOne(CompanyPro::class, 'id', 'company_pro_id');
- }
- public function company()
- {
- return $this->hasOne(Company::class, 'id', 'company_id');
- }
- public function companyProPayer()
- {
- return $this->hasOne(CompanyProPayer::class, 'id', 'company_pro_payer_id');
- }
- public function companyLocation()
- {
- return $this->hasOne(CompanyLocation::class, 'id', 'company_location_id');
- }
- public function cmReasons()
- {
- return $this->hasMany(CareMonthCmRmReason::class, 'care_month_id', 'id')
- ->where('cm_or_rm', 'CM')
- ->orderBy('position_index', 'ASC')
- ->orderBy('code', 'ASC');
- }
- public function rmReasons()
- {
- return $this->hasMany(CareMonthCmRmReason::class, 'care_month_id', 'id')
- ->where('cm_or_rm', 'RM')
- ->orderBy('position_index', 'ASC')
- ->orderBy('code', 'ASC');
- }
- public function rmSetupClaim()
- {
- return $this->hasOne(Claim::class, 'id', 'rm_setup_claim_id')
- ->where('status', '<>', 'CANCELLED');
- }
- public function mostRecentMcpNote()
- {
- return $this->hasOne(Note::class, 'id', 'most_recent_mcp_note_id');
- }
- public function note()
- {
- return $this->hasOne(Note::class, 'id', 'note_id');
- }
- public function mcpRmGenericBill()
- {
- return $this->hasOne(Bill::class, 'id', 'mcp_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
- }
- public function rmmRmGenericBill()
- {
- return $this->hasOne(Bill::class, 'id', 'rmm_rm_generic_bill_id')->where('is_cancelled', false)->where('is_cancelled_by_administrator', false);
- }
- public function showMeasurementDaysWarning(){
- return ($this->daysSinceLastMeasurement() >= 2) || (16 - $this->number_of_days_with_remote_measurements) >= $this->daysTillEndOfMonth();
- }
- public function daysSinceLastMeasurement(){
- if(!$this->most_recent_cellular_measurement_at) {
- return 999;
- }
- $d1 = new DateTime($this->most_recent_cellular_measurement_at);
- return $d1->diff(new DateTime())->days;
- }
- public function daysTillEndOfMonth(){
- return date('t') - date('j');
- }
- public function calculateBillabilityForMcp(){
- $tier1Strategy = $this->mcp_rpm_payment_strategy_tier_one;
- $tier2Strategy = $this->mcp_rpm_payment_strategy_tier_two;
- if(!$tier1Strategy || !$tier2Strategy){
- return [
- 'billable' => false,
- 'reason' => 'MCP RPM strategy has not been set.'
- ];
- }
- $tier = determineTier($this);
- if (!$tier) {
- return [
- 'billable' => false,
- 'reason' => "Patient has not had a visit recent enough to bill for RPM"
- ];
- }
- if($tier == 'TIER_1'){
- return [
- 'billable' => true,
- 'amount' => $this->mcp_rpm_payment_amount_tier_one
- ];
- }
- if($tier == 'TIER_2'){
- return [
- 'billable' => true,
- 'amount' => $this->mcp_rpm_payment_amount_tier_two
- ];
- }
- }
- public function calculateBillabilityForRmm(){
- if(!$this->rmmPro) {
- return [
- 'billable' => false,
- 'reason' => 'RMM not set on the care month.'
- ];
- }
- $tier1Strategy = $this->mcp_rpm_payment_tier_one_strategy;
- $tier2Strategy = $this->mcp_rpm_payment_tier_two_strategy;
- if(!$tier1Strategy || !$tier2Strategy){
- return [
- 'billable' => false,
- 'reason' => 'RPM strategy has not been set.'
- ];
- }
- $tier = determineTier($this);
- $rmmRpmPaymentAmount = $this->rmm_payment_amount;
- if (!$tier) {
- return [
- 'billable' => false,
- 'reason' => "Patient has not had a visit recent enough to bill for RPM"
- ];
- }
- if($tier == 'TIER_1'){
- return [
- 'billable' => true,
- 'amount' => $rmmRpmPaymentAmount
- ];
- }
- if($tier == 'TIER_2'){
- return [
- 'billable' => true,
- 'amount' => $rmmRpmPaymentAmount
- ];
- }
- }
- public function rtmMskTransmissions() {
- return $this->hasMany(ClientRtmTransmission::class, 'care_month_id', 'id')
- ->where('msk_or_lung', 'MSK')
- ->orderBy('effective_date', 'DESC')
- ->orderBy('created_at', 'DESC');
- }
- public function rtmLungTransmissions() {
- return $this->hasMany(ClientRtmTransmission::class, 'care_month_id', 'id')
- ->where('msk_or_lung', 'LUNG')
- ->orderBy('effective_date', 'DESC')
- ->orderBy('created_at', 'DESC');
- }
- public function measurements() {
- return $this->hasMany(Measurement::class, 'care_month_id', 'id')
- ->orderBy('created_at', 'DESC');
- }
- public function tier() {
- return determineTier($this);
- }
- }
|