<?php

namespace Modules\Payment\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Modules\Main\Models\Agent\Agent;
use Modules\Main\Models\Company\Company;
use Modules\Main\Models\Complex;
class Payment extends Model
{
    use HasFactory;
    protected $fillable = [
        'paymentable_type',
        'paymentable_id',
        'complex_id',
        'value'
    ];
    public function getPaymentablePercent()
    {
        //наследуется
        if ((int) $this->value == -1)
        {
            if ($this->paymentable_type == 'Company')
            {
                return false;
            }

            if ($this->paymentable_type == 'Agent')
            {
                $agent = Agent::find($this->paymentable_id);
                $company = $agent->company;
                $parentPaymentable = $company->getPaymentable(Complex::find($this->complex_id));
                return $parentPaymentable->getPaymentablePercent();
            }
        }
        //не установлено
        if ($this->value == null)
        {
            return false;
        }

        return $this->value;
    }
}
