From bf80679a9b53ecc9589835a4353537452d61c342 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Tue, 12 Dec 2017 19:30:36 +0300 Subject: [PATCH] fixed quarterly #141 --- app/Http/Controllers/Dashboard/Dashboard.php | 46 +++++++++++++------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/app/Http/Controllers/Dashboard/Dashboard.php b/app/Http/Controllers/Dashboard/Dashboard.php index 14a769c05..6b1aaec21 100644 --- a/app/Http/Controllers/Dashboard/Dashboard.php +++ b/app/Http/Controllers/Dashboard/Dashboard.php @@ -270,38 +270,54 @@ class Dashboard extends Controller $date_format = 'Y-m'; - $n = 1; - if ($period == 'quarter') { + if ($period == 'month') { + $n = 1; + $start_date = $start->format($date_format); + $end_date = $end->format($date_format); + $next_date = $start_date; + } else { $n = 3; + $start_date = $start->quarter; + $end_date = $end->quarter; + $next_date = $start_date; } - $start_date = $start->format($date_format); - $end_date = $end->format($date_format); - $next_date = $start_date; - - $totals[$start_date] = 0; - - do { - $next_date = Date::parse($next_date)->addMonths($n)->format($date_format); + $s = clone $start; + //$totals[$start_date] = 0; + while ($next_date <= $end_date) { $totals[$next_date] = 0; - } while ($next_date < $end_date); + + if ($period == 'month') { + $next_date = $s->addMonths($n)->format($date_format); + } else { + if (isset($totals[4])) { + break; + } + + $next_date = $s->addMonths($n)->quarter; + } + } $items_1 = $m1::whereBetween('paid_at', [$start, $end])->get(); - $this->setCashFlowTotals($totals, $items_1, $date_format); + $this->setCashFlowTotals($totals, $items_1, $date_format, $period); $items_2 = $m2::whereBetween('paid_at', [$start, $end])->get(); - $this->setCashFlowTotals($totals, $items_2, $date_format); + $this->setCashFlowTotals($totals, $items_2, $date_format, $period); return $totals; } - private function setCashFlowTotals(&$totals, $items, $date_format) + private function setCashFlowTotals(&$totals, $items, $date_format, $period) { foreach ($items as $item) { - $i = Date::parse($item->paid_at)->format($date_format); + if ($period == 'month') { + $i = Date::parse($item->paid_at)->format($date_format); + } else { + $i = Date::parse($item->paid_at)->quarter; + } $totals[$i] += $item->getConvertedAmount(); }