Contract.php 9.02 KB
<?php

namespace App\Models\Legal;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Contract
 * @package App\Models\Legal
 */
class Contract extends BaseModel
{
    use HasFactory,SoftDeletes;

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'company_id' => 'integer', 'flag' => 'integer', 'cooperation_type' => 'integer', 'authorize_type' => 'integer', 'period' => 'integer', 'division' => 'integer', 'advance_days' => 'integer', 'is_exclusive' => 'integer', 'treaty_id' => 'integer', 'proxy_id' => 'integer', 'start_type' => 'integer', 'status' => 'integer', 'updated_at' => 'datetime', 'created_at' => 'datetime', 'user_id' => 'integer', 'copies_num' => 'integer'];

    /**
     * @var string[]
     */
    public static $right_type = ['l'=>'作词','c'=>'作曲','p'=>'制作','s'=>'表演','a'=>'版权持有人'];


    //合作类型  1:一次性费用  2:分成 3:分成+预付  5: 分成+一次性费用 6:抵扣成本后再分成 7:分成+预付+一次性费用 8:抵扣成本+分成+预付 9:一次性费用+抵扣成本后分成
    /**
     * 抵扣成本模式
     * @var array
     */
    public static $cost_mode = [6, 8, 9];

    /**
     * 预付模式
     * @var array
     */
    public static $prepaid_mode = [3, 7, 8];

    /**
     * @param $str
     * @return array|string
     */
    public static function transformRole($str)
    {
        if (empty($str)) return [];

        $roles = array_unique(str_split(str_replace(',','', $str)));
        sort($roles);

        $rs = [];

        foreach ($roles as $value) {
            if (isset(self::$right_type[$value])) {
                $rs[] = self::$right_type[$value];
            }
        }

        return $rs;
    }

    /**
     * @param $str
     * @return string
     */
    public static function transformRight($str)
    {
        $rs = '';
        $roles = array_unique(str_split(str_replace(',','', $str)));
        sort($roles);

        foreach ($roles as $value) {
            $rs .= (self::$right_type[$value] ?? '') . '+';
        }

        return rtrim($rs, '+');
    }


    /**
     * @param $songs
     * @return string
     */
    public static function relationSong($songs)
    {
        $c = count($songs);

        if ($c > 1) {
            return "《{$songs[0]['name']}》等{$c}首歌";
        } else if ($c == 1) {
            return "《{$songs[0]['name']}》";
        } else {
            return '';
        }
    }

    /**
     * @param $roles
     * @return array
     */
    public static function getRole($right)
    {
        $roles = [];
        foreach ($right as $item) {
            $roles += self::transformRole($item['right_type']);
        }

        return array_unique($roles);
    }

    /**
     * @param $cooperation_type
     * @param $rightgetModel
     * @return string
     */
    public static function getModel($cooperation_type, $right)
    {

        switch ($cooperation_type) {
            case -1:
                return '未填写';
            case 0:
                return '无';
            case 1:
                return "一次性费用" . self::formatCost($right);
            case 2:
                return "分成" . self::formatDivide($right);
            case 3:
                return '分成' . self::formatDivide($right) .'+预付'. self::formatPrepaid($right);
            case 5:
                return '分成' . self::formatDivide($right) .'+一次性费用'. self::formatCost($right);
            case 6:
                return '抵扣成本后再分成' . self::formatDivide($right);
            case 7:
                return '分成' . self::formatDivide($right) . '+预付' .self::formatPrepaid($right). '+一次性费用' . self::formatCost($right);
            case 8:
                return '抵扣成本后分成' .self::formatDivide($right). '+预付' . self::formatPrepaid($right);
            case 9:
                return '一次性费用' . self::formatCost($right) .'+抵扣成本后分成' . self::formatDivide($right);
        }
    }

    /**
     * 费用模式
     * @param $cooperation_type
     * @return string
     */
    public static function getCooperation($cooperation_type)
    {
        switch ($cooperation_type) {
            case 1:
                return "一次性费用";
            case 2:
                return "分成";
            case 3:
                return '分成+预付';
            case 5:
                return '分成+一次性费用';
            case 6:
                return '抵扣成本后再分成';
            case 7:
                return '分成+预付+一次性费用';
            case 8:
                return '抵扣成本后分成+预付';
            case 9:
                return '一次性费用+抵扣成本后分成';
        }
    }

    /**
     * 一次性费用
     * @param $right
     * @return string
     */
    public static function formatCost($right)
    {
        $reward_money = 0;
        foreach ($right as $item) {
            $reward_money += (int)$item['reward_money'];
        }
        return "({$reward_money})";
    }

    /**
     * 计算模式
     * @param $cooperation_types
     * @return int
     */
    public static function cooperation($cooperation_types)
    {
        if (array_intersect($cooperation_types, self::$cost_mode) && array_intersect($cooperation_types, self::$prepaid_mode)) {
            //抵扣和预付都有
            return 1;
        } else if (array_intersect($cooperation_types, self::$cost_mode)) {
            //只有抵扣
            return 2;
        } else if (array_intersect($cooperation_types, self::$prepaid_mode)) {
            //只有预付
            return 3;
        } else {
            //只有分成
            return 0;
        }
    }

    /**
     * 授权区域区域
     * @return string
     */
    public static function getDistrict($district_id)
    {
        if (empty($district_id)) return '';

        $district_ids = explode(',', $district_id);

        $district_data = District::getDistricts();

        foreach ($district_ids as $id) {
            $district[] = $district_data[$id] ?? '';
        }

        return join('|', array_filter($district));
    }

    /**
     * 授权形式
     * @param $is_exclusive
     * @return string
     */
    public static function isExclusive($is_exclusive)
    {
        return $is_exclusive ? '独家' : '非独';
    }

    /**
     * 分成
     * @param $right
     * @return string
     */
    public static function formatDivide($right)
    {
        $str = '';
        foreach ($right as $item) {
            $str .= self::transformRight($item['right_type']) . ($item['proportion'] * 100) . '%,';
        }

        $str =  rtrim($str, ',');
        return "($str)";
    }

    /**
     * 预付
     * @param $right
     * @return string
     */
    public static function formatPrepaid($right)
    {
        $prepaid = 0;
        foreach ($right as $item) {
            $prepaid += (int)$item['prepaid_money'];
        }
        return "({$prepaid})";
    }

    /**
     * @return false|string[]
     */
    public function getServiceTypeNameAttribute()
    {
        $names = [];
        $service_types = explode(',', $this->service_type);
        foreach ($service_types as $val) {
            $names[] = Treaty::$service_type[$val] ?? null;
        }

        return array_filter($names);
    }

    /**
     * @return string
     */
    public function getTreatyTypeNameAttribute()
    {
        switch ($this->treaty_type) {
            case 1:
                return '唱片约艺人';
            case 2:
                return '全约艺人';
            case 3:
                return 'mcn达人';
        }
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function files()
    {
        return $this->hasMany(ContractFile::class, 'contract_id', 'id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function filesNew()
    {
        return $this->hasMany(FileMap::class, 'link_id', 'id')->where('link', 'copyright');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function filesNewTreaty()
    {
        return $this->hasMany(FileMap::class, 'link_id', 'id')->where('link', 'treaty');
    }


    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function moreSongs()
    {
        return $this->belongsToMany(Song::class, 'contract_song', 'contract_id', 'song_id')
            ->select('songs.id', 'songs.custom_id', 'songs.name', 'songs.singer');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function detail()
    {
        return $this->hasOne(ContractDetail::class, 'contract_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function stakeholderContract()
    {
        return $this->hasMany(StakeholderContract::class, 'contract_id');
    }
}