mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-30 06:50:49 +00:00 
			
		
		
		
	Moving stuff around, optimising charts.
This commit is contained in:
		| @@ -8,9 +8,15 @@ use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface; | ||||
| use FireflyIII\Http\Controllers\Controller; | ||||
| use FireflyIII\Models\Budget; | ||||
| use FireflyIII\Models\LimitRepetition; | ||||
| use FireflyIII\Models\TransactionJournal; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; | ||||
| use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; | ||||
| use FireflyIII\Support\CacheProperties; | ||||
| use Illuminate\Support\Collection; | ||||
| use Log; | ||||
| use Navigation; | ||||
| use Preferences; | ||||
| use Response; | ||||
|  | ||||
| /** | ||||
|  * Class BudgetController | ||||
| @@ -43,22 +49,47 @@ class BudgetController extends Controller | ||||
|      */ | ||||
|     public function budget(BudgetRepositoryInterface $repository, Budget $budget) | ||||
|     { | ||||
|         $first = $repository->firstUseDate($budget); | ||||
|         $range = Preferences::get('viewRange', '1M')->data; | ||||
|         $last  = session('end', new Carbon); | ||||
|  | ||||
|         $cache = new CacheProperties(); | ||||
|         $cache->addProperty($first); | ||||
|         $cache->addProperty($last); | ||||
|         $cache->addProperty('budget'); | ||||
|  | ||||
|         if ($cache->has()) { | ||||
|             //return Response::json($cache->get()); | ||||
|         } | ||||
|  | ||||
|         $final = clone $last; | ||||
|         $final->addYears(2); | ||||
|  | ||||
|         $budgetCollection = new Collection([$budget]); | ||||
|         $last             = Navigation::endOfX($last, $range, $final); // not to overshoot. | ||||
|         $entries          = new Collection; | ||||
|         Log::debug('---- now at chart'); | ||||
|         while ($first < $last) { | ||||
|  | ||||
|             // periodspecific dates: | ||||
|             $currentStart = Navigation::startOfPeriod($first, $range); | ||||
|             $currentEnd   = Navigation::endOfPeriod($first, $range); | ||||
|             // sub another day because reasons. | ||||
|             $currentEnd->subDay(); | ||||
|             $spent        = $repository->spentInPeriod($budgetCollection, new Collection, $currentStart, $currentEnd); | ||||
|             $entry        = [$first, ($spent * -1)]; | ||||
|  | ||||
|             $entries->push($entry); | ||||
|             $first = Navigation::addPeriod($first, $range, 0); | ||||
|         } | ||||
|  | ||||
|         $data = $this->generator->budgetLimit($entries, 'month'); | ||||
|         $cache->store($data); | ||||
|  | ||||
|         return Response::json($data); | ||||
|  | ||||
|  | ||||
|         /** | ||||
|          * // dates and times | ||||
|          * $first = $repository->getFirstBudgetLimitDate($budget); | ||||
|          * $range = Preferences::get('viewRange', '1M')->data; | ||||
|          * $last  = session('end', new Carbon); | ||||
|          * | ||||
|          * // chart properties for cache: | ||||
|          * $cache = new CacheProperties(); | ||||
|          * $cache->addProperty($first); | ||||
|          * $cache->addProperty($last); | ||||
|          * $cache->addProperty('budget'); | ||||
|          * if ($cache->has()) { | ||||
|          * | ||||
|          * //return Response::json($cache->get()); | ||||
|          * } | ||||
|          * | ||||
|          * $final = clone $last; | ||||
|          * $final->addYears(2); | ||||
|          * $last    = Navigation::endOfX($last, $range, $final); | ||||
| @@ -96,43 +127,34 @@ class BudgetController extends Controller | ||||
|      */ | ||||
|     public function budgetLimit(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition) | ||||
|     { | ||||
|         /** | ||||
|          * $start = clone $repetition->startdate; | ||||
|          * $end   = $repetition->enddate; | ||||
|          * | ||||
|          * // chart properties for cache: | ||||
|          * $cache = new CacheProperties(); | ||||
|          * $cache->addProperty($start); | ||||
|          * $cache->addProperty($end); | ||||
|          * $cache->addProperty('budget'); | ||||
|          * $cache->addProperty('limit'); | ||||
|          * $cache->addProperty($budget->id); | ||||
|          * $cache->addProperty($repetition->id); | ||||
|          * if ($cache->has()) { | ||||
|          * return Response::json($cache->get()); | ||||
|          * } | ||||
|          * | ||||
|          * $set     = $repository->spentPerDay($budget, $start, $end, new Collection); | ||||
|          * $entries = new Collection; | ||||
|          * $amount  = $repetition->amount; | ||||
|          * | ||||
|          * // get sum (har har)! | ||||
|          * while ($start <= $end) { | ||||
|          * $formatted = $start->format('Y-m-d'); | ||||
|          * $sum       = $set[$formatted] ?? '0'; | ||||
|          * | ||||
|          * // Sum of expenses on this day: | ||||
|          * $amount = round(bcadd(strval($amount), $sum), 2); | ||||
|          * $entries->push([clone $start, $amount]); | ||||
|          * $start->addDay(); | ||||
|          * } | ||||
|          * | ||||
|          * $data = $this->generator->budgetLimit($entries, 'monthAndDay'); | ||||
|          * $cache->store($data); | ||||
|          * | ||||
|          * return Response::json($data); | ||||
|          **/ | ||||
|         $start = clone $repetition->startdate; | ||||
|         $end   = $repetition->enddate; | ||||
|         $cache = new CacheProperties(); | ||||
|         $cache->addProperty($start); | ||||
|         $cache->addProperty($end); | ||||
|         $cache->addProperty('budget-limit'); | ||||
|         $cache->addProperty($budget->id); | ||||
|         $cache->addProperty($repetition->id); | ||||
|  | ||||
|         if ($cache->has()) { | ||||
|             // return Response::json($cache->get()); | ||||
|         } | ||||
|  | ||||
|         $entries          = new Collection; | ||||
|         $amount           = $repetition->amount; | ||||
|         $budgetCollection = new Collection([$budget]); | ||||
|         Log::debug('amount starts ' . $amount); | ||||
|         while ($start <= $end) { | ||||
|             $spent  = $repository->spentInPeriod($budgetCollection, new Collection, $start, $start); | ||||
|             $amount = bcadd($amount, $spent); | ||||
|             $entries->push([clone $start, round($amount, 2)]); | ||||
|  | ||||
|             $start->addDay(); | ||||
|         } | ||||
|         $data = $this->generator->budgetLimit($entries, 'monthAndDay'); | ||||
|         $cache->store($data); | ||||
|  | ||||
|         return Response::json($data); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -146,19 +168,75 @@ class BudgetController extends Controller | ||||
|      */ | ||||
|     public function frontpage(BudgetRepositoryInterface $repository, ARI $accountRepository) | ||||
|     { | ||||
|         $start = session('start', Carbon::now()->startOfMonth()); | ||||
|         $end   = session('end', Carbon::now()->endOfMonth()); | ||||
|         // chart properties for cache: | ||||
|         $cache = new CacheProperties(); | ||||
|         $cache->addProperty($start); | ||||
|         $cache->addProperty($end); | ||||
|         $cache->addProperty('budget'); | ||||
|         $cache->addProperty('all'); | ||||
|         if ($cache->has()) { | ||||
|             return Response::json($cache->get()); | ||||
|         } | ||||
|         $budgets     = $repository->getActiveBudgets(); | ||||
|         $repetitions = $repository->getAllBudgetLimitRepetitions($start, $end); | ||||
|         $allEntries  = new Collection; | ||||
|         $format      = strval(trans('config.month_and_day')); | ||||
|  | ||||
|         /** @var Budget $budget */ | ||||
|         foreach ($budgets as $budget) { | ||||
|             // get relevant repetitions: | ||||
|             $name = $budget->name; | ||||
|             $reps = $repetitions->filter( | ||||
|                 function (LimitRepetition $repetition) use ($budget, $start, $end) { | ||||
|                     if ($repetition->startdate < $end && $repetition->enddate > $start && $repetition->budget_id === $budget->id) { | ||||
|                         return $repetition; | ||||
|                     } | ||||
|                 } | ||||
|             ); | ||||
|             if ($reps->count() === 0) { | ||||
|                 $amount    = '0'; | ||||
|                 $left      = '0'; | ||||
|                 $spent     = $repository->spentInPeriod(new Collection([$budget]), new Collection, $start, $end); | ||||
|                 $overspent = '0'; | ||||
|                 $allEntries->push([$name, $left, $spent, $overspent, $amount, $spent]); | ||||
|             } | ||||
|             /** @var LimitRepetition $repetition */ | ||||
|             foreach ($reps as $repetition) { | ||||
|                 $expenses = $repository->spentInPeriod(new Collection([$budget]), new Collection, $repetition->startdate, $repetition->enddate); | ||||
|                 if ($reps->count() > 1) { | ||||
|                     $name = $budget->name . ' ' . trans( | ||||
|                             'firefly.between_dates', | ||||
|                             ['start' => $repetition->startdate->formatLocalized($format), 'end' => $repetition->enddate->formatLocalized($format)] | ||||
|                         ); | ||||
|                 } | ||||
|                 $amount    = $repetition->amount; | ||||
|                 $left      = bccomp(bcadd($amount, $expenses), '0') < 1 ? '0' : bcadd($amount, $expenses); | ||||
|                 $spent     = bccomp(bcadd($amount, $expenses), '0') < 1 ? bcmul($amount, '-1') : $expenses; | ||||
|                 $overspent = bccomp(bcadd($amount, $expenses), '0') < 1 ? bcadd($amount, $expenses) : '0'; | ||||
|                 $allEntries->push([$name, $left, $spent, $overspent, $amount, $spent]); | ||||
|             } | ||||
|  | ||||
|         } | ||||
|  | ||||
|         $list = $repository->journalsInPeriodWithoutBudget(new Collection, $start, $end); | ||||
|         $sum  = '0'; | ||||
|         /** @var TransactionJournal $entry */ | ||||
|         foreach ($list as $entry) { | ||||
|             $sum = bcadd(TransactionJournal::amount($entry), $sum); | ||||
|         } | ||||
|         $allEntries->push([trans('firefly.no_budget'), '0', '0', $sum, '0', '0']); | ||||
|         $data = $this->generator->frontpage($allEntries); | ||||
|         $cache->store($data); | ||||
|  | ||||
|         return Response::json($data); | ||||
|  | ||||
|  | ||||
|         /** | ||||
|          * $start = session('start', Carbon::now()->startOfMonth()); | ||||
|          * $end   = session('end', Carbon::now()->endOfMonth()); | ||||
|          * | ||||
|          * // chart properties for cache: | ||||
|          * $cache = new CacheProperties(); | ||||
|          * $cache->addProperty($start); | ||||
|          * $cache->addProperty($end); | ||||
|          * $cache->addProperty('budget'); | ||||
|          * $cache->addProperty('all'); | ||||
|          * if ($cache->has()) { | ||||
|          * return Response::json($cache->get()); | ||||
|          * } | ||||
|          * | ||||
|          * | ||||
|          * | ||||
|          * $budgets    = $repository->getBudgetsAndLimitsInRange($start, $end); | ||||
|          * $allEntries = new Collection; | ||||
| @@ -379,8 +457,10 @@ class BudgetController extends Controller | ||||
|      * | ||||
|      * @return \Illuminate\Http\JsonResponse | ||||
|      */ | ||||
|     public function year(BudgetRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts) | ||||
|     { | ||||
|     public | ||||
|     function year( | ||||
|         BudgetRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts | ||||
|     ) { | ||||
|         /** | ||||
|          * // chart properties for cache: | ||||
|          * $cache = new CacheProperties(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user