MusicianIncomeService.php 4.94 KB
<?php

namespace App\Services;

use App\Helper\Response;
use App\Models\Legal\Contract;
use App\Models\Legal\StakeholderSongCollate;

/**
 * Class MusicianIncomeService
 * @package App\Services
 */
class MusicianIncomeService extends Service
{
    /**
     * 收益图表
     * @return \Illuminate\Http\JsonResponse
     */
    public function income()
    {
        $start_month = $this->request->input('start', date('Ym', strtotime('-6 month')));
        $end_month   = $this->request->input('end', date('Ym', strtotime('-1 month')));

        $res = StakeholderSongCollate::query()->custom($this->request->inputs(['song_id']))->identify()
            ->groupBy('month')->orderBy('month')->selectRaw('month, sum(share_amount) as income')->get()->toArray();

        $stat_income = [];
        foreach ($res as $item) {
            if ($item['month'] >= $start_month && $item['month'] <= $end_month) {
                $stat_income[] = $item;
            }
        }

        return Response::success($stat_income);
    }

    /**
     * 账单入账明细
     * @return \Illuminate\Http\JsonResponse
     */
    public function billCollate()
    {
        $collate = $detail = $contract_ids = [];
        StakeholderSongCollate::query()->custom($this->request->inputs(['song_id']))->identify()
            ->orderByDesc('month')->each(function ($item) use (&$collate, &$detail, &$contract_ids){
                $key = "{$item->month}|{$item->channel}";
                if (!isset($detail[$key])) {
                    $detail[$key] = [
                        'month'     =>  $item->month,
                        'channel'   =>  $item->channel,
                    ];
                }

                //汇总
                $collate['deduct_amount']       =   isset($collate['deduct_amount'])        ?   bcadd($collate['deduct_amount'],    $item->deduct_amount,       self::DECIMAL)   :   $item->deduct_amount;
                $collate['real_share_amount']   =   isset($collate['real_share_amount'])    ?   bcadd($collate['real_share_amount'],$item->real_share_amount,   self::DECIMAL)   :   $item->real_share_amount;
                //$collate['cost']              =   isset($collate['cost'])                 ?   bcadd($collate['cost'],             $item->cost_amount,         StakeholderSongCollate::DECIMAL)    :   $item->cost_amount;
                $collate['cost']                =   0.00;

                //抵扣预付
                $detail[$key]['deduct_amount']     = isset($detail[$key]['deduct_amount']) ?
                    bcadd($detail[$key]['deduct_amount'], $item->deduct_amount, self::DECIMAL) : $item->deduct_amount;
                //收益
                $detail[$key]['real_share_amount'] = isset($detail[$key]['real_share_amount']) ?
                    bcadd($detail[$key]['real_share_amount'], $item->real_share_amount, self::DECIMAL) : $item->real_share_amount;
                //抵扣成本
                $detail[$key]['cost'] = 0.00;

                //合同id集合
                $contract_ids[] = $item->contract_id;
            });

        //模式
        $cooperation_types = Contract::query()->whereIn('id', array_unique($contract_ids))->pluck('cooperation_type')->toArray();

        $data = [
            'collect'=> $collate,
            'detail' => array_values($detail), //明细
            'cooperation'=> Contract::cooperation($cooperation_types)
        ];

        return Response::success($data);

    }

    /**
     * 入账记录详情
     * @return \Illuminate\Http\JsonResponse
     */
    public function billCollateDetail()
    {
        $cooperation_types = $detail = [];

        StakeholderSongCollate::query()->with(['song:id,name,singer', 'contract:id,cooperation_type'])->custom($this->request->inputs(['month', 'channel']))->identify()
        ->select(['id', 'song_id', 'month', 'contract_id', 'deduct_amount', 'real_share_amount'])
        ->each(function ($item) use (&$cooperation_types, &$detail){

            if ($item->song && $item->contract) {

                $song_id = $item->song->id;

                if (!isset($detail[$song_id])) {
                    $detail[$song_id] = [
                        'cost'=>'0.00',
                        'song_name'=>$item->song->name,
                        'singer'=>$item->song->singer,
                    ];
                }

                $cooperation_types[] = $item->contract->cooperation_type;
                $detail[$song_id]['deduct_amount'] = isset($detail[$song_id]['deduct_amount']) ? bcadd($detail[$song_id]['deduct_amount'], $item->deduct_amount, self::DECIMAL) : $item->deduct_amount;
                $detail[$song_id]['real_share_amount'] = isset($detail[$song_id]['real_share_amount']) ? bcadd($detail[$song_id]['real_share_amount'], $item->real_share_amount, self::DECIMAL) :  $item->real_share_amount;
            }
        });

        $cooperation = Contract::cooperation($cooperation_types);;

        return Response::success(['list'=>array_values($detail), 'cooperation'=>$cooperation]);

    }



}