<?php

namespace App\Models;

# use Illuminate\Database\Eloquent\Model;

class Pro extends Model
{
    protected $table = 'pro';

    public function displayName() {
        $name = [];
        if(!empty($this->name_last)) $name[] = $this->name_last;
        if(!empty($this->name_first)) $name[] = $this->name_first;
        if(!count($name)) {
            $name = $this->name_display;
        }
        else {
            $name = implode(", ", $name);
        }
        return $name;
    }

    public function cmBills()
    {
        return $this->hasMany(Bill::class, 'cm_pro_id');
    }


    public function hcpBills()
    {
        return $this->hasMany(Bill::class, 'hcp_pro_id');
    }

    public function lastPayment() {
        return ProTransaction
            ::where('pro_id', $this->id)
            ->where('plus_or_minus', 'PLUS')
            ->orderBy('created_at', 'desc')
            ->first();
    }

    public function hasRates() {
        $numRates = ProRate::where('is_active', true)->where('pro_id', $this->id)->count();
        return $numRates > 0;
    }

    public function cmRates() {
        return ProRate::distinct('code')
            ->where('is_active', true)
            ->where('pro_id', $this->id)
            ->where('code', 'LIKE', 'CM%')
            ->get();
    }

    public function rmRates() {
        return ProRate::distinct('code')
            ->where('is_active', true)
            ->where('pro_id', $this->id)
            ->where('code', 'LIKE', 'RM%')
            ->get();
    }

    public function noteRates() {
        return ProRate::distinct('code')
            ->where('is_active', true)
            ->where('pro_id', $this->id)
            ->where('code', 'NOT LIKE', 'CM%')
            ->where('code', 'NOT LIKE', 'RM%')
            ->get();
    }

    public function shortcuts() {
        return $this->hasMany(ProTextShortcut::class, 'pro_id');
    }
}
