MusicianIncomeService.php
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?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->only(['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->only(['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->only(['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]);
    }
}