Contract.php 6.59 KB
<?php

namespace App\Models\Legal;

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

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

    /**
     * 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 $right
     * @return string
     */
    public static function getModel($cooperation_type, $right)
    {
        switch ($cooperation_type) {
            case 1:
                return "一次性费用" . self::formatCost($right);
                break;
            case 2:
                return "分成" . self::formatDivide($right);
                break;
            case 3:
                return '分成' . self::formatDivide($right) .'+预付'. self::formatPrepaid($right);
                break;
            case 5:
                return '分成' . self::formatDivide($right) .'+一次性费用'. self::formatCost($right);
                break;
            case 6:
                return '抵扣成本后再分成' . self::formatDivide($right);
                break;
            case 7:
                return '分成' . self::formatDivide($right) . '+预付' .self::formatPrepaid($right). '+一次性费用' . self::formatCost($right);
                break;
            case 8:
                return '抵扣成本后分成' .self::formatDivide($right). '+预付' . self::formatPrepaid($right);
                break;
            case 9:
                return '一次性费用' . self::formatCost($right) .'+抵扣成本后分成' . self::formatDivide($right);
                break;
        }
    }

    /**
     * 一次性费用
     * @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;
        }
    }


    /**
     * 分成
     * @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 \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function files()
    {
        return $this->hasMany(ContractFile::class, 'contract_id', 'id');
    }

    /**
     * @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');
    }
}