*
Showing
1 changed file
with
26 additions
and
16 deletions
... | @@ -18,33 +18,43 @@ class MusicianIncomeService extends Service | ... | @@ -18,33 +18,43 @@ class MusicianIncomeService extends Service |
18 | */ | 18 | */ |
19 | public function income() | 19 | public function income() |
20 | { | 20 | { |
21 | $start_month = $this->request->input('start'); | ||
22 | $end_month = $this->request->input('end'); | ||
23 | 21 | ||
24 | $res = StakeholderSongCollate::query()->custom($this->request->only(['song_id']))->identify() | 22 | $res = StakeholderSongCollate::query()->custom($this->request->only(['song_id']))->identify() |
25 | ->groupBy('month')->orderBy('month')->selectRaw('month, sum(share_amount) as income')->get()->toArray(); | 23 | ->groupBy('month')->orderByDesc('month')->selectRaw('month, sum(share_amount) as income')->get()->toArray(); |
24 | |||
25 | $stat_income = $months = []; | ||
26 | $total_income = '0.00'; | ||
27 | |||
28 | if (empty($res)) { | ||
29 | return Response::success(['list'=>$stat_income, 'total_income'=>$total_income]); | ||
30 | } | ||
31 | |||
32 | $months[] = current($res)['month']; | ||
33 | for ($i = 1; $i< 6; $i++) { | ||
34 | $months[] = date('Ym', strtotime(end($months).'01 -1 month')); | ||
35 | } | ||
26 | 36 | ||
27 | $stat_income = []; | ||
28 | $total_income = '0'; | ||
29 | foreach ($res as $item) { | 37 | foreach ($res as $item) { |
30 | 38 | ||
31 | if (empty($start_month) || empty($end_month)) { | 39 | if (empty($start_month) || empty($end_month)) { |
32 | 40 | ||
33 | if (count($stat_income) < 7) { | 41 | if (count($stat_income) >= 6) break; |
34 | $stat_income[] = $item; | 42 | |
35 | continue; | 43 | $stat_income[$item['month']] = $item; |
36 | } | 44 | $total_income = bcadd($total_income, $item['income'], self::DECIMAL); |
37 | break; | ||
38 | } else { | ||
39 | if ($item['month'] >= $start_month && $item['month'] <= $end_month) { | ||
40 | $stat_income[] = $item; | ||
41 | } | ||
42 | } | 45 | } |
46 | } | ||
43 | 47 | ||
44 | $total_income = bcadd($total_income, $item->income, self::DECIMAL); | 48 | foreach ($months as $month) { |
49 | if (!isset($stat_income[$month])) { | ||
50 | $stat_income[$month] = [ | ||
51 | 'month' => $month, | ||
52 | 'income'=> 0.00 | ||
53 | ]; | ||
54 | } | ||
45 | } | 55 | } |
46 | 56 | ||
47 | return Response::success(['list'=>$stat_income, 'total_income'=>$total_income]); | 57 | return Response::success(['list'=>array_values($stat_income), 'total_income'=>$total_income]); |
48 | } | 58 | } |
49 | 59 | ||
50 | /** | 60 | /** | ... | ... |
-
Please register or sign in to post a comment