123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?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')->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(){
- $strategy = $this->mcp_rpm_payment_strategy;
- if(!$strategy){
- return [
- 'billable' => false,
- 'reason' => 'MCP RPM strategy has not been set.'
- ];
- }
-
- $mcpRpmPaymentAmount = $this->mcp_rpm_payment_amount;
- $has16PlusDays = $this->number_of_days_with_remote_measurements >= 16;
- $hasMcpBilled20Minutes = $this->rm_total_time_in_seconds_by_mcp >= 1200;
- $hasMcpInteracted = $this->has_mcp_interacted_with_client_about_rm;
- if($strategy == 'X16_DAYS'){
- //only check for 16 days
- if($has16PlusDays){
- return [
- 'billable' => true,
- 'amount' => $mcpRpmPaymentAmount
- ];
- } else {
- //not billable
- return [
- 'billable' => false,
- 'reason' => "This care month does not have 16 or more measurement days."
- ];
- }
- }
-
- if($strategy == 'X16_DAYS_20_MINS_ON_OWN_MCP_COM_DURING_CM'){
- if ($has16PlusDays && $hasMcpBilled20Minutes && $hasMcpInteracted) {
- return [
- 'billable' => true,
- 'amount' => $mcpRpmPaymentAmount
- ];
- } else {
- if(!$has16PlusDays){
- return [
- 'billable' => false,
- 'reason' => 'Care month does not have 16 or more measurement days.'
- ];
- }
- if(!$hasMcpBilled20Minutes){
- return [
- 'billable' => false,
- 'reason' => 'Care month does not have 20 minutes billed time.'
- ];
- }
- if(!$hasMcpInteracted){
- return [
- 'billable' => false,
- 'reason' => 'Care month does MCP interaction.'
- ];
- }
-
- }
- }
-
- }
- }
|