diff --git a/app/Generator/Chart/Account/AccountChartGenerator.php b/app/Generator/Chart/Account/AccountChartGenerator.php index 79b36e4bab..b43145f761 100644 --- a/app/Generator/Chart/Account/AccountChartGenerator.php +++ b/app/Generator/Chart/Account/AccountChartGenerator.php @@ -1,9 +1,10 @@ description; + $paidAmount += floatval($entry->amount); + } + + // loop unpaid: + /** @var Bill $entry */ + foreach ($unpaid as $entry) { + $description = $entry[0]->name . ' (' . $entry[1]->format('jS M Y') . ')'; + $amount = ($entry[0]->amount_max + $entry[0]->amount_min) / 2; + $unpaidDescriptions[] = $description; + $unpaidAmount += $amount; + unset($amount, $description); + } + + $chart = new GChart; + $chart->addColumn(trans('firefly.name'), 'string'); + $chart->addColumn(trans('firefly.amount'), 'number'); + + $chart->addRow(trans('firefly.unpaid') . ': ' . join(', ', $unpaidDescriptions), $unpaidAmount); + $chart->addRow(trans('firefly.paid') . ': ' . join(', ', $paidDescriptions), $paidAmount); + + $chart->generate(); + + return $chart->getData(); + } + + /** + * @param Bill $bill + * @param Collection $entries + * + * @return mixed + */ + public function single(Bill $bill, Collection $entries) + { + // make chart: + $chart = new GChart; + $chart->addColumn(trans('firefly.date'), 'date'); + $chart->addColumn(trans('firefly.maxAmount'), 'number'); + $chart->addColumn(trans('firefly.minAmount'), 'number'); + $chart->addColumn(trans('firefly.billEntry'), 'number'); + + /** @var TransactionJournal $result */ + foreach ($entries as $result) { + $chart->addRow(clone $result->date, floatval($bill->amount_max), floatval($bill->amount_min), floatval($result->amount)); + } + + $chart->generate(); + + return $chart->getData(); + } +} \ No newline at end of file diff --git a/app/Generator/Chart/Budget/BudgetChartGenerator.php b/app/Generator/Chart/Budget/BudgetChartGenerator.php new file mode 100644 index 0000000000..2a9033ed00 --- /dev/null +++ b/app/Generator/Chart/Budget/BudgetChartGenerator.php @@ -0,0 +1,43 @@ +addColumn(trans('firefly.period'), 'date'); + $chart->addColumn(trans('firefly.spent'), 'number'); + + /** @var array $entry */ + foreach ($entries as $entry) { + $chart->addRow($entry[0], $entry[1]); + } + + $chart->generate(); + + return $chart->getData(); + } + + /** + * @param Collection $entries + * + * @return array + */ + public function budgetLimit(Collection $entries) + { + $chart = new GChart; + $chart->addColumn(trans('firefly.day'), 'date'); + $chart->addColumn(trans('firefly.left'), 'number'); + + foreach ($entries as $entry) { + + $chart->addRow($entry[0], $entry[1]); + } + $chart->generate(); + + return $chart->getData(); + } + + /** + * @param Collection $entries + * + * @return array + */ + public function frontpage(Collection $entries) + { + $chart = new GChart; + $chart->addColumn(trans('firefly.budget'), 'string'); + $chart->addColumn(trans('firefly.left'), 'number'); + $chart->addColumn(trans('firefly.spent'), 'number'); + $chart->addColumn(trans('firefly.overspent'), 'number'); + + /** @var array $entry */ + foreach ($entries as $entry) { + if ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0) { + $chart->addRow($entry[0], $entry[1], $entry[2], $entry[3]); + } + } + + + $chart->generate(); + + return $chart->getData(); + + } + + /** + * @param Collection $budgets + * @param Collection $entries + * + * @return array + */ + public function year(Collection $budgets, Collection $entries) + { + $chart = new GChart; + // add columns: + $chart->addColumn(trans('firefly.month'), 'date'); + foreach ($budgets as $budget) { + $chart->addColumn($budget->name, 'number'); + } + + /** @var array $entry */ + foreach ($entries as $entry) { + + $chart->addRowArray($entry); + } + + $chart->generate(); + + return $chart->getData(); + } +} \ No newline at end of file diff --git a/app/Generator/Chart/Category/CategoryChartGenerator.php b/app/Generator/Chart/Category/CategoryChartGenerator.php new file mode 100644 index 0000000000..bd0a30e5e7 --- /dev/null +++ b/app/Generator/Chart/Category/CategoryChartGenerator.php @@ -0,0 +1,44 @@ +addColumn(trans('firefly.period'), 'date'); + $chart->addColumn(trans('firefly.spent'), 'number'); + + /** @var array $entry */ + foreach ($entries as $entry) { + $chart->addRow($entry[0], $entry[1]); + } + + $chart->generate(); + + return $chart->getData(); + } + + /** + * @param Collection $entries + * + * @return array + */ + public function frontpage(Collection $entries) + { + $chart = new GChart; + $chart->addColumn(trans('firefly.category'), 'string'); + $chart->addColumn(trans('firefly.spent'), 'number'); + + + /** @var array $entry */ + foreach ($entries as $entry) { + $sum = $entry['sum']; + if ($sum != 0) { + $chart->addRow($entry['name'], $sum); + } + } + + $chart->generate(); + + return $chart->getData(); + } + + /** + * @param Collection $entries + * + * @return array + */ + public function month(Collection $entries) + { + $chart = new GChart; + + $chart->addColumn(trans('firefly.period'), 'date'); + $chart->addColumn(trans('firefly.spent'), 'number'); + + /** @var array $entry */ + foreach ($entries as $entry) { + $chart->addRow($entry[0], $entry[1]); + } + $chart->generate(); + + return $chart->getData(); + } + + /** + * @param Collection $entries + * + * @return array + */ + public function year(Collection $categories, Collection $entries) + { + $chart = new GChart; + + $chart->addColumn(trans('firefly.month'), 'date'); + foreach ($categories as $category) { + $chart->addColumn($category->name, 'number'); + } + /** @var array $entry */ + foreach ($entries as $entry) { + $chart->addRowArray($entry); + } + + } +} \ No newline at end of file diff --git a/app/Generator/Chart/PiggyBank/GooglePiggyBankChartGenerator.php b/app/Generator/Chart/PiggyBank/GooglePiggyBankChartGenerator.php new file mode 100644 index 0000000000..22f4639d18 --- /dev/null +++ b/app/Generator/Chart/PiggyBank/GooglePiggyBankChartGenerator.php @@ -0,0 +1,41 @@ +addColumn(trans('firefly.date'), 'date'); + $chart->addColumn(trans('firefly.balance'), 'number'); + + $sum = '0'; + bcscale(2); + + foreach ($set as $entry) { + $sum = bcadd($sum, $entry->sum); + $chart->addRow(new Carbon($entry->date), $sum); + } + + $chart->generate(); + + return $chart->getData(); + } +} \ No newline at end of file diff --git a/app/Generator/Chart/PiggyBank/PiggyBankChartGenerator.php b/app/Generator/Chart/PiggyBank/PiggyBankChartGenerator.php new file mode 100644 index 0000000000..6f7b48a149 --- /dev/null +++ b/app/Generator/Chart/PiggyBank/PiggyBankChartGenerator.php @@ -0,0 +1,20 @@ +addColumn(trans('firefly.month'), 'date'); + $chart->addColumn(trans('firefly.income'), 'number'); + $chart->addColumn(trans('firefly.expenses'), 'number'); + + /** @var array $entry */ + foreach ($entries as $entry) { + $chart->addRowArray($entry); + } + $chart->generate(); + + return $chart->getData(); + } + + /** + * @param string $income + * @param string $expense + * @param int $count + * + * @return array + */ + public function yearInOutSummarized($income, $expense, $count) + { + $chart = new GChart; + + $chart->addColumn(trans('firefly.summary'), 'string'); + $chart->addColumn(trans('firefly.income'), 'number'); + $chart->addColumn(trans('firefly.expenses'), 'number'); + $chart->addRow(trans('firefly.sum'), $income, $expense); + $chart->addRow(trans('firefly.average'), ($income / $count), ($expense / $count)); + + $chart->generate(); + + return $chart->getData(); + } +} \ No newline at end of file diff --git a/app/Generator/Chart/Report/ReportChartGenerator.php b/app/Generator/Chart/Report/ReportChartGenerator.php new file mode 100644 index 0000000000..623fa7e93a --- /dev/null +++ b/app/Generator/Chart/Report/ReportChartGenerator.php @@ -0,0 +1,31 @@ +startOfMonth()); - $end = Session::get('end', Carbon::now()->endOfMonth()); + $start = Session::get('start', Carbon::now()->startOfMonth()); + $end = Session::get('end', Carbon::now()->endOfMonth()); // chart properties for cache: $cache = new CacheProperties(); diff --git a/app/Http/Controllers/Chart/BillController.php b/app/Http/Controllers/Chart/BillController.php index 7a4c2670bb..9d6934277a 100644 --- a/app/Http/Controllers/Chart/BillController.php +++ b/app/Http/Controllers/Chart/BillController.php @@ -2,14 +2,13 @@ namespace FireflyIII\Http\Controllers\Chart; +use App; use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Bill; -use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Bill\BillRepositoryInterface; use FireflyIII\Support\CacheProperties; -use Grumpydictator\Gchart\GChart; use Illuminate\Support\Collection; use Response; use Session; @@ -22,60 +21,30 @@ use Steam; */ class BillController extends Controller { + + /** @var \FireflyIII\Generator\Chart\Bill\BillChartGenerator */ + protected $generator; + /** - * Shows the overview for a bill. The min/max amount and matched journals. * - * @param GChart $chart - * @param BillRepositoryInterface $repository - * @param Bill $bill - * - * @return \Symfony\Component\HttpFoundation\Response */ - public function single(GChart $chart, BillRepositoryInterface $repository, Bill $bill) + public function __construct() { - - $chart->addColumn(trans('firefly.date'), 'date'); - $chart->addColumn(trans('firefly.maxAmount'), 'number'); - $chart->addColumn(trans('firefly.minAmount'), 'number'); - $chart->addColumn(trans('firefly.billEntry'), 'number'); - - $cache = new CacheProperties; - $cache->addProperty('single'); - $cache->addProperty('bill'); - $cache->addProperty($bill->id); - if ($cache->has()) { - return Response::json($cache->get()); // @codeCoverageIgnore - } - - // get first transaction or today for start: - $results = $repository->getJournals($bill); - /** @var TransactionJournal $result */ - foreach ($results as $result) { - $chart->addRow(clone $result->date, floatval($bill->amount_max), floatval($bill->amount_min), floatval($result->amount)); - } - - $chart->generate(); - - $data = $chart->getData(); - $cache->store($data); - - return Response::json($data); + parent::__construct(); + // create chart generator: + $this->generator = App::make('FireflyIII\Generator\Chart\Bill\BillChartGenerator'); } /** * Shows all bills and whether or not theyve been paid this month (pie chart). * - * @param GChart $chart - * * @param BillRepositoryInterface $repository * @param AccountRepositoryInterface $accounts * * @return \Symfony\Component\HttpFoundation\Response */ - public function frontpage(GChart $chart, BillRepositoryInterface $repository, AccountRepositoryInterface $accounts) + public function frontpage(BillRepositoryInterface $repository, AccountRepositoryInterface $accounts) { - $chart->addColumn(trans('firefly.name'), 'string'); - $chart->addColumn(trans('firefly.amount'), 'number'); $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); @@ -94,11 +63,7 @@ class BillController extends Controller $bills = $repository->getActiveBills(); $paid = new Collection; // journals. $unpaid = new Collection; // bills - // loop paid and create single entry: - $paidDescriptions = []; - $paidAmount = 0; - $unpaidDescriptions = []; - $unpaidAmount = 0; + /** @var Bill $bill */ foreach ($bills as $bill) { @@ -136,30 +101,35 @@ class BillController extends Controller } } + // build chart: + $data = $this->generator->frontpage($paid, $unpaid); + $cache->store($data); - /** @var TransactionJournal $entry */ - foreach ($paid as $entry) { + return Response::json($data); + } - $paidDescriptions[] = $entry->description; - $paidAmount += floatval($entry->amount); + /** + * Shows the overview for a bill. The min/max amount and matched journals. + * + * @param BillRepositoryInterface $repository + * @param Bill $bill + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function single(BillRepositoryInterface $repository, Bill $bill) + { + $cache = new CacheProperties; + $cache->addProperty('single'); + $cache->addProperty('bill'); + $cache->addProperty($bill->id); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore } - // loop unpaid: - /** @var Bill $entry */ - foreach ($unpaid as $entry) { - $description = $entry[0]->name . ' (' . $entry[1]->format('jS M Y') . ')'; - $amount = ($entry[0]->amount_max + $entry[0]->amount_min) / 2; - $unpaidDescriptions[] = $description; - $unpaidAmount += $amount; - unset($amount, $description); - } + // get first transaction or today for start: + $results = $repository->getJournals($bill); - $chart->addRow(trans('firefly.unpaid') . ': ' . join(', ', $unpaidDescriptions), $unpaidAmount); - $chart->addRow(trans('firefly.paid') . ': ' . join(', ', $paidDescriptions), $paidAmount); - - $chart->generate(); - - $data = $chart->getData(); + $data = $this->generator->single($bill, $results); $cache->store($data); return Response::json($data); diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 517f9e56f9..4793ce3c92 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -2,6 +2,7 @@ namespace FireflyIII\Http\Controllers\Chart; +use App; use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Budget; @@ -22,19 +23,30 @@ use Session; */ class BudgetController extends Controller { + + /** @var \FireflyIII\Generator\Chart\Budget\BudgetChartGenerator */ + protected $generator; + + /** + * + */ + public function __construct() + { + parent::__construct(); + // create chart generator: + $this->generator = App::make('FireflyIII\Generator\Chart\Budget\BudgetChartGenerator'); + } + /** - * @param GChart $chart * @param BudgetRepositoryInterface $repository * @param Budget $budget * * @return \Symfony\Component\HttpFoundation\Response */ - public function budget(GChart $chart, BudgetRepositoryInterface $repository, Budget $budget) + public function budget(BudgetRepositoryInterface $repository, Budget $budget) { - $chart->addColumn(trans('firefly.period'), 'date'); - $chart->addColumn(trans('firefly.spent'), 'number'); - + // dates and times $first = $repository->getFirstBudgetLimitDate($budget); $range = Preferences::get('viewRange', '1M')->data; $last = Session::get('end', new Carbon); @@ -47,33 +59,23 @@ class BudgetController extends Controller $cache->addProperty($first); $cache->addProperty($last); $cache->addProperty('budget'); - $cache->addProperty('budget'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } + $entries = new Collection; while ($first < $last) { $end = Navigation::addPeriod($first, $range, 0); $end->subDay(); - - // start date for chart. $chartDate = clone $end; $chartDate->startOfMonth(); - $spent = $repository->spentInPeriodCorrected($budget, $first, $end); - $chart->addRow($chartDate, $spent); - + $entries->push([$chartDate, $spent]); $first = Navigation::addPeriod($first, $range, 0); - - } - $chart->generate(); - - $data = $chart->getData(); - - + $data = $this->generator->budget($entries); $cache->store($data); return Response::json($data); @@ -89,7 +91,7 @@ class BudgetController extends Controller * * @return \Symfony\Component\HttpFoundation\Response */ - public function budgetLimit(GChart $chart, BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition) + public function budgetLimit(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition) { $start = clone $repetition->startdate; $end = $repetition->enddate; @@ -106,11 +108,8 @@ class BudgetController extends Controller return Response::json($cache->get()); // @codeCoverageIgnore } - $chart->addColumn(trans('firefly.day'), 'date'); - $chart->addColumn(trans('firefly.left'), 'number'); - - - $amount = $repetition->amount; + $entries = new Collection; + $amount = $repetition->amount; while ($start <= $end) { /* @@ -118,12 +117,11 @@ class BudgetController extends Controller */ $sum = $repository->expensesOnDayCorrected($budget, $start); $amount += $sum; - $chart->addRow(clone $start, $amount); + $entries->push([clone $start, $amount]); $start->addDay(); } - $chart->generate(); - $data = $chart->getData(); + $data = $this->generator->budgetLimit($entries); $cache->store($data); return Response::json($data); @@ -133,18 +131,12 @@ class BudgetController extends Controller /** * Shows a budget list with spent/left/overspent. * - * @param GChart $chart * @param BudgetRepositoryInterface $repository * * @return \Symfony\Component\HttpFoundation\Response */ - public function frontpage(GChart $chart, BudgetRepositoryInterface $repository) + public function frontpage(BudgetRepositoryInterface $repository) { - $chart->addColumn(trans('firefly.budget'), 'string'); - $chart->addColumn(trans('firefly.left'), 'number'); - $chart->addColumn(trans('firefly.spent'), 'number'); - $chart->addColumn(trans('firefly.overspent'), 'number'); - $budgets = $repository->getBudgets(); $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); @@ -160,6 +152,7 @@ class BudgetController extends Controller return Response::json($cache->get()); // @codeCoverageIgnore } + bcscale(2); /** @var Budget $budget */ foreach ($budgets as $budget) { @@ -171,32 +164,27 @@ class BudgetController extends Controller } /** @var LimitRepetition $repetition */ foreach ($repetitions as $repetition) { - $expenses = $repository->spentInPeriodCorrected($budget, $repetition->startdate, $repetition->enddate, true); - $left = $expenses < floatval($repetition->amount) ? floatval($repetition->amount) - $expenses : 0; - $spent = $expenses > floatval($repetition->amount) ? floatval($repetition->amount) : $expenses; - $overspent = $expenses > floatval($repetition->amount) ? $expenses - floatval($repetition->amount) : 0; - $allEntries->push( - [$budget->name . ' (' . $repetition->startdate->formatLocalized($this->monthAndDayFormat) . ')', - $left, - $spent, - $overspent - ] - ); + $expenses = $repository->spentInPeriodCorrected($budget, $repetition->startdate, $repetition->enddate, true); + // $left can be less than zero. + // $overspent can be more than zero ( = overspending) + + $left = max(bcsub($repetition->amount, $expenses), 0); // limited at zero. + $overspent = max(bcsub($expenses, $repetition->amount), 0); // limited at zero. + $date = $repetition->startdate->formatLocalized($this->monthAndDayFormat); + $name = $budget->name . ' (' . $date . ')'; + + // $spent is maxed to the repetition amount: + $spent = $expenses > $repetition->amount ? $repetition->amount : $expenses; + + + $allEntries->push([$name, $left, $spent, $overspent]); } } $noBudgetExpenses = $repository->getWithoutBudgetSum($start, $end) * -1; $allEntries->push([trans('firefly.noBudget'), 0, 0, $noBudgetExpenses]); - foreach ($allEntries as $entry) { - if ($entry[1] != 0 || $entry[2] != 0 || $entry[3] != 0) { - $chart->addRow($entry[0], $entry[1], $entry[2], $entry[3]); - } - } - - $chart->generate(); - - $data = $chart->getData(); + $data = $this->generator->frontpage($allEntries); $cache->store($data); return Response::json($data); @@ -206,14 +194,13 @@ class BudgetController extends Controller /** * Show a yearly overview for a budget. * - * @param GChart $chart * @param BudgetRepositoryInterface $repository * @param $year * @param bool $shared * * @return \Symfony\Component\HttpFoundation\Response */ - public function year(GChart $chart, BudgetRepositoryInterface $repository, $year, $shared = false) + public function year(BudgetRepositoryInterface $repository, $year, $shared = false) { $start = new Carbon($year . '-01-01'); $end = new Carbon($year . '-12-31'); @@ -230,17 +217,12 @@ class BudgetController extends Controller return Response::json($cache->get()); // @codeCoverageIgnore } - // add columns: - $chart->addColumn(trans('firefly.month'), 'date'); - foreach ($budgets as $budget) { - $chart->addColumn($budget->name, 'number'); - } + $entries = new Collection; while ($start < $end) { // month is the current end of the period: $month = clone $start; $month->endOfMonth(); - // make a row: $row = [clone $start]; // each budget, fill the row: @@ -248,14 +230,11 @@ class BudgetController extends Controller $spent = $repository->spentInPeriodCorrected($budget, $start, $month, $shared); $row[] = $spent; } - $chart->addRowArray($row); - + $entries->push($row); $start->endOfMonth()->addDay(); } - $chart->generate(); - - $data = $chart->getData(); + $data = $this->generator->year($budgets, $entries); $cache->store($data); return Response::json($data); diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index b7c4bdb800..866ff6a4d0 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -3,12 +3,13 @@ namespace FireflyIII\Http\Controllers\Chart; +use App; use Carbon\Carbon; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Category; use FireflyIII\Repositories\Category\CategoryRepositoryInterface; use FireflyIII\Support\CacheProperties; -use Grumpydictator\Gchart\GChart; +use Illuminate\Support\Collection; use Navigation; use Preferences; use Response; @@ -21,42 +22,61 @@ use Session; */ class CategoryController extends Controller { + /** @var \FireflyIII\Generator\Chart\Category\CategoryChartGenerator */ + protected $generator; + + /** + * + */ + public function __construct() + { + parent::__construct(); + // create chart generator: + $this->generator = App::make('FireflyIII\Generator\Chart\Category\CategoryChartGenerator'); + } /** * Show an overview for a category for all time, per month/week/year. * - * @param GChart $chart * @param CategoryRepositoryInterface $repository * @param Category $category * * @return \Symfony\Component\HttpFoundation\Response */ - public function all(GChart $chart, CategoryRepositoryInterface $repository, Category $category) + public function all(CategoryRepositoryInterface $repository, Category $category) { // oldest transaction in category: $start = $repository->getFirstActivityDate($category); $range = Preferences::get('viewRange', '1M')->data; - // jump to start of week / month / year / etc $start = Navigation::startOfPeriod($start, $range); + $end = new Carbon; - $chart->addColumn(trans('firefly.period'), 'date'); - $chart->addColumn(trans('firefly.spent'), 'number'); + $entries = new Collection; - $end = new Carbon; - while ($start <= $end) { - - $currentEnd = Navigation::endOfPeriod($start, $range); - $spent = $repository->spentInPeriodCorrected($category, $start, $currentEnd); - $chart->addRow(clone $start, $spent); - - $start = Navigation::addPeriod($start, $range, 0); + // chart properties for cache: + $cache = new CacheProperties(); + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('all'); + $cache->addProperty('categories'); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore } - $chart->generate(); + while ($start <= $end) { + $currentEnd = Navigation::endOfPeriod($start, $range); + $spent = $repository->spentInPeriodCorrected($category, $start, $currentEnd); + $entries->push([clone $start, $spent]); + $start = Navigation::addPeriod($start, $range, 0); - return Response::json($chart->getData()); + } + + $data = $this->generator->all($entries); + $cache->store($data); + + return Response::json($data); } @@ -64,15 +84,12 @@ class CategoryController extends Controller /** * Show this month's category overview. * - * @param GChart $chart * @param CategoryRepositoryInterface $repository * * @return \Symfony\Component\HttpFoundation\Response */ - public function frontpage(GChart $chart, CategoryRepositoryInterface $repository) + public function frontpage(CategoryRepositoryInterface $repository) { - $chart->addColumn(trans('firefly.category'), 'string'); - $chart->addColumn(trans('firefly.spent'), 'number'); $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); @@ -87,11 +104,10 @@ class CategoryController extends Controller return Response::json($cache->get()); // @codeCoverageIgnore } - $set = $repository->getCategoriesAndExpensesCorrected($start, $end); - + $array = $repository->getCategoriesAndExpensesCorrected($start, $end); // sort by callback: uasort( - $set, + $array, function ($left, $right) { if ($left['sum'] == $right['sum']) { return 0; @@ -100,48 +116,48 @@ class CategoryController extends Controller return ($left['sum'] < $right['sum']) ? 1 : -1; } ); - - - foreach ($set as $entry) { - $sum = floatval($entry['sum']); - if ($sum != 0) { - $chart->addRow($entry['name'], $sum); - } - } - - $chart->generate(); - - $data = $chart->getData(); - $cache->store($data); + $set = new Collection($array); + $data = $this->generator->frontpage($set); return Response::json($data); } /** - * @param GChart $chart * @param CategoryRepositoryInterface $repository * @param Category $category * * @return \Symfony\Component\HttpFoundation\Response */ - public function month(GChart $chart, CategoryRepositoryInterface $repository, Category $category) + public function month(CategoryRepositoryInterface $repository, Category $category) { $start = clone Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); - $chart->addColumn(trans('firefly.period'), 'date'); - $chart->addColumn(trans('firefly.spent'), 'number'); + // chart properties for cache: + $cache = new CacheProperties; + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty($category->id); + $cache->addProperty('category'); + $cache->addProperty('month'); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore + } + $entries = new Collection; + while ($start <= $end) { $spent = $repository->spentOnDaySumCorrected($category, $start); - $chart->addRow(clone $start, $spent); + + $entries->push([clone $start, $spent]); $start->addDay(); } - $chart->generate(); + $data = $this->generator->month($entries); + $cache->store($data); - return Response::json($chart->getData()); + return Response::json($data); } @@ -149,25 +165,31 @@ class CategoryController extends Controller /** * This chart will only show expenses. * - * @param GChart $chart * @param CategoryRepositoryInterface $repository * @param $year * @param bool $shared * * @return \Symfony\Component\HttpFoundation\Response */ - public function year(GChart $chart, CategoryRepositoryInterface $repository, $year, $shared = false) + public function year(CategoryRepositoryInterface $repository, $year, $shared = false) { - $start = new Carbon($year . '-01-01'); - $end = new Carbon($year . '-12-31'); + $start = new Carbon($year . '-01-01'); + $end = new Carbon($year . '-12-31'); + + + // chart properties for cache: + $cache = new CacheProperties; + $cache->addProperty($start); + $cache->addProperty($end); + $cache->addProperty('category'); + $cache->addProperty('year'); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore + } + $shared = $shared == 'shared' ? true : false; $categories = $repository->getCategories(); - - // add columns: - $chart->addColumn(trans('firefly.month'), 'date'); - foreach ($categories as $category) { - $chart->addColumn($category->name, 'number'); - } + $entries = new Collection; while ($start < $end) { // month is the current end of the period: @@ -181,13 +203,14 @@ class CategoryController extends Controller $spent = $repository->spentInPeriodCorrected($category, $start, $month, $shared); $row[] = $spent; } - $chart->addRowArray($row); + $entries->push($row); $start->addMonth(); } - $chart->generate(); + $data = $this->generator->year($categories, $entries); + $cache->store($data); - return Response::json($chart->getData()); + return Response::json($data); } } diff --git a/app/Http/Controllers/Chart/PiggyBankController.php b/app/Http/Controllers/Chart/PiggyBankController.php index 7428757c79..22e2a0790e 100644 --- a/app/Http/Controllers/Chart/PiggyBankController.php +++ b/app/Http/Controllers/Chart/PiggyBankController.php @@ -2,11 +2,11 @@ namespace FireflyIII\Http\Controllers\Chart; -use Carbon\Carbon; +use App; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\PiggyBank; use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; -use Grumpydictator\Gchart\GChart; +use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; use Response; @@ -18,32 +18,44 @@ use Response; */ class PiggyBankController extends Controller { + + /** @var \FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGenerator */ + protected $generator; + + /** + * + */ + public function __construct() + { + parent::__construct(); + // create chart generator: + $this->generator = App::make('FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGenerator'); + } + /** * Shows the piggy bank history. * - * @param GChart $chart * @param PiggyBankRepositoryInterface $repository * @param PiggyBank $piggyBank * * @return \Symfony\Component\HttpFoundation\Response */ - public function history(GChart $chart, PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) + public function history(PiggyBankRepositoryInterface $repository, PiggyBank $piggyBank) { - $chart->addColumn(trans('firefly.date'), 'date'); - $chart->addColumn(trans('firefly.balance'), 'number'); - - /** @var Collection $set */ - $set = $repository->getEventSummarySet($piggyBank); - $sum = 0; - - foreach ($set as $entry) { - $sum += floatval($entry->sum); - $chart->addRow(new Carbon($entry->date), $sum); + // chart properties for cache: + $cache = new CacheProperties; + $cache->addProperty('piggy-history'); + $cache->addProperty($piggyBank->id); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore } - $chart->generate(); + /** @var Collection $set */ + $set = new Collection($repository->getEventSummarySet($piggyBank)); + $data = $this->generator->history($set); + $cache->store($data); - return Response::json($chart->getData()); + return Response::json($data); } } diff --git a/app/Http/Controllers/Chart/ReportController.php b/app/Http/Controllers/Chart/ReportController.php index a474f1ac51..7e1b435e8d 100644 --- a/app/Http/Controllers/Chart/ReportController.php +++ b/app/Http/Controllers/Chart/ReportController.php @@ -3,10 +3,12 @@ namespace FireflyIII\Http\Controllers\Chart; +use App; use Carbon\Carbon; use FireflyIII\Helpers\Report\ReportQueryInterface; use FireflyIII\Http\Controllers\Controller; -use Grumpydictator\Gchart\GChart; +use FireflyIII\Support\CacheProperties; +use Illuminate\Support\Collection; use Response; /** @@ -17,85 +19,108 @@ use Response; class ReportController extends Controller { + /** @var \FireflyIII\Generator\Chart\Report\ReportChartGenerator */ + protected $generator; + + /** + * + */ + public function __construct() + { + parent::__construct(); + // create chart generator: + $this->generator = App::make('FireflyIII\Generator\Chart\Report\ReportChartGenerator'); + } + /** * Summarizes all income and expenses, per month, for a given year. * - * @param GChart $chart * @param ReportQueryInterface $query * @param $year * @param bool $shared * * @return \Symfony\Component\HttpFoundation\Response */ - public function yearInOut(GChart $chart, ReportQueryInterface $query, $year, $shared = false) + public function yearInOut(ReportQueryInterface $query, $year, $shared = false) { // get start and end of year $start = new Carbon($year . '-01-01'); $end = new Carbon($year . '-12-31'); $shared = $shared == 'shared' ? true : false; - $chart->addColumn(trans('firefly.month'), 'date'); - $chart->addColumn(trans('firefly.income'), 'number'); - $chart->addColumn(trans('firefly.expenses'), 'number'); + // chart properties for cache: + $cache = new CacheProperties; + $cache->addProperty('yearInOut'); + $cache->addProperty($year); + $cache->addProperty($shared); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore + } + $entries = new Collection; while ($start < $end) { $month = clone $start; $month->endOfMonth(); // total income and total expenses: - $incomeSum = floatval($query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount')); - $expenseSum = floatval($query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount')); + $incomeSum = $query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount'); + $expenseSum = $query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount'); - $chart->addRow(clone $start, $incomeSum, $expenseSum); + $entries->push([clone $start, $incomeSum, $expenseSum]); $start->addMonth(); } - $chart->generate(); - return Response::json($chart->getData()); + $data = $this->generator->yearInOut($entries); + $cache->store($data); + + return Response::json($data); } /** * Summarizes all income and expenses for a given year. Gives a total and an average. * - * @param GChart $chart * @param ReportQueryInterface $query * @param $year * @param bool $shared * * @return \Symfony\Component\HttpFoundation\Response */ - public function yearInOutSummarized(GChart $chart, ReportQueryInterface $query, $year, $shared = false) + public function yearInOutSummarized(ReportQueryInterface $query, $year, $shared = false) { + + // chart properties for cache: + $cache = new CacheProperties; + $cache->addProperty('yearInOutSummarized'); + $cache->addProperty($year); + $cache->addProperty($shared); + if ($cache->has()) { + return Response::json($cache->get()); // @codeCoverageIgnore + } + $start = new Carbon($year . '-01-01'); $end = new Carbon($year . '-12-31'); $shared = $shared == 'shared' ? true : false; - $income = 0; - $expense = 0; + $income = '0'; + $expense = '0'; $count = 0; - $chart->addColumn(trans('firefly.summary'), 'string'); - $chart->addColumn(trans('firefly.income'), 'number'); - $chart->addColumn(trans('firefly.expenses'), 'number'); + bcscale(2); while ($start < $end) { $month = clone $start; $month->endOfMonth(); // total income and total expenses: - $income += floatval($query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount')); - $expense += floatval($query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount')); + $income = bcadd($income, $query->incomeInPeriodCorrected($start, $month, $shared)->sum('amount')); + $expense = bcadd($expense, $query->expenseInPeriodCorrected($start, $month, $shared)->sum('amount')); $count++; $start->addMonth(); } - // add total + average: - $chart->addRow(trans('firefly.sum'), $income, $expense); - $count = $count > 0 ? $count : 1; - $chart->addRow(trans('firefly.average'), ($income / $count), ($expense / $count)); + $data = $this->generator->yearInOutSummarized($income, $expense, $count); + $cache->store($data); - $chart->generate(); - - return Response::json($chart->getData()); + return Response::json($data); } } diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php index 1f8c4d0a3a..b05e112dec 100644 --- a/app/Providers/FireflyServiceProvider.php +++ b/app/Providers/FireflyServiceProvider.php @@ -93,6 +93,11 @@ class FireflyServiceProvider extends ServiceProvider // make Google charts: $this->app->bind('FireflyIII\Generator\Chart\Account\AccountChartGenerator', 'FireflyIII\Generator\Chart\Account\GoogleAccountChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Bill\BillChartGenerator', 'FireflyIII\Generator\Chart\Bill\GoogleBillChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Budget\BudgetChartGenerator', 'FireflyIII\Generator\Chart\Budget\GoogleBudgetChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Category\CategoryChartGenerator', 'FireflyIII\Generator\Chart\Category\GoogleCategoryChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\PiggyBank\PiggyBankChartGenerator', 'FireflyIII\Generator\Chart\PiggyBank\GooglePiggyBankChartGenerator'); + $this->app->bind('FireflyIII\Generator\Chart\Report\ReportChartGenerator', 'FireflyIII\Generator\Chart\Report\GoogleReportChartGenerator'); $this->app->bind('FireflyIII\Helpers\Help\HelpInterface', 'FireflyIII\Helpers\Help\Help');