Esempio n. 1
0
 /**
  * @param Carbon     $start
  * @param Carbon     $end
  * @param Collection $accounts
  *
  * @return BudgetCollection
  */
 public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts)
 {
     $object = new BudgetCollection();
     /** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
     $repository = app('FireflyIII\\Repositories\\Budget\\BudgetRepositoryInterface');
     $set = $repository->getBudgets();
     $allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
     $allTotalSpent = $repository->spentAllPerDayForAccounts($accounts, $start, $end);
     bcscale(2);
     foreach ($set as $budget) {
         $repetitions = $allRepetitions->filter(function (LimitRepetition $rep) use($budget) {
             return $rep->budget_id == $budget->id;
         });
         $totalSpent = isset($allTotalSpent[$budget->id]) ? $allTotalSpent[$budget->id] : [];
         // no repetition(s) for this budget:
         if ($repetitions->count() == 0) {
             $spent = array_sum($totalSpent);
             $budgetLine = new BudgetLine();
             $budgetLine->setBudget($budget);
             $budgetLine->setOverspent($spent);
             $object->addOverspent($spent);
             $object->addBudgetLine($budgetLine);
             continue;
         }
         // one or more repetitions for budget:
         /** @var LimitRepetition $repetition */
         foreach ($repetitions as $repetition) {
             $budgetLine = new BudgetLine();
             $budgetLine->setBudget($budget);
             $budgetLine->setRepetition($repetition);
             $expenses = $this->getSumOfRange($start, $end, $totalSpent);
             // 200 en -100 is 100, vergeleken met 0 === 1
             // 200 en -200 is 0, vergeleken met 0 === 0
             // 200 en -300 is -100, vergeleken met 0 === -1
             $left = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : 0;
             $spent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0';
             $overspent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? '0' : bcadd($expenses, $repetition->amount);
             $budgetLine->setLeft($left);
             $budgetLine->setSpent($expenses);
             $budgetLine->setOverspent($overspent);
             $budgetLine->setBudgeted($repetition->amount);
             $object->addBudgeted($repetition->amount);
             $object->addSpent($spent);
             $object->addLeft($left);
             $object->addOverspent($overspent);
             $object->addBudgetLine($budgetLine);
         }
     }
     // stuff outside of budgets:
     $noBudget = $repository->getWithoutBudgetSum($start, $end);
     $budgetLine = new BudgetLine();
     $budgetLine->setOverspent($noBudget);
     $budgetLine->setSpent($noBudget);
     $object->addOverspent($noBudget);
     $object->addBudgetLine($budgetLine);
     return $object;
 }
Esempio n. 2
0
 /**
  * @param Carbon  $start
  * @param Carbon  $end
  * @param boolean $shared
  *
  * @return BudgetCollection
  */
 public function getBudgetReport(Carbon $start, Carbon $end, $shared)
 {
     $object = new BudgetCollection();
     /** @var \FireflyIII\Repositories\Budget\BudgetRepositoryInterface $repository */
     $repository = app('FireflyIII\\Repositories\\Budget\\BudgetRepositoryInterface');
     $set = $repository->getBudgets();
     bcscale(2);
     foreach ($set as $budget) {
         $repetitions = $repository->getBudgetLimitRepetitions($budget, $start, $end);
         // no repetition(s) for this budget:
         if ($repetitions->count() == 0) {
             $spent = $repository->balanceInPeriod($budget, $start, $end, $shared);
             $budgetLine = new BudgetLine();
             $budgetLine->setBudget($budget);
             $budgetLine->setOverspent($spent);
             $object->addOverspent($spent);
             $object->addBudgetLine($budgetLine);
             continue;
         }
         // one or more repetitions for budget:
         /** @var LimitRepetition $repetition */
         foreach ($repetitions as $repetition) {
             $budgetLine = new BudgetLine();
             $budgetLine->setBudget($budget);
             $budgetLine->setRepetition($repetition);
             $expenses = $repository->balanceInPeriod($budget, $repetition->startdate, $repetition->enddate, $shared);
             $left = $expenses < $repetition->amount ? bcsub($repetition->amount, $expenses) : 0;
             $spent = $expenses > $repetition->amount ? 0 : $expenses;
             $overspent = $expenses > $repetition->amount ? bcsub($expenses, $repetition->amount) : 0;
             $budgetLine->setLeft($left);
             $budgetLine->setSpent($spent);
             $budgetLine->setOverspent($overspent);
             $budgetLine->setBudgeted($repetition->amount);
             $object->addBudgeted($repetition->amount);
             $object->addSpent($spent);
             $object->addLeft($left);
             $object->addOverspent($overspent);
             $object->addBudgetLine($budgetLine);
         }
     }
     // stuff outside of budgets:
     $noBudget = $repository->getWithoutBudgetSum($start, $end);
     $budgetLine = new BudgetLine();
     $budgetLine->setOverspent($noBudget);
     $object->addOverspent($noBudget);
     $object->addBudgetLine($budgetLine);
     return $object;
 }
 /**
  * @param Carbon     $start
  * @param Carbon     $end
  * @param Collection $accounts
  *
  * @return BudgetCollection
  */
 public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts) : BudgetCollection
 {
     $object = new BudgetCollection();
     $set = $this->repository->getBudgets();
     /** @var Budget $budget */
     foreach ($set as $budget) {
         $repetitions = $budget->limitrepetitions()->before($end)->after($start)->get();
         // no repetition(s) for this budget:
         if ($repetitions->count() == 0) {
             // spent for budget in time range:
             $spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);
             if ($spent > 0) {
                 $budgetLine = new BudgetLine();
                 $budgetLine->setBudget($budget)->setOverspent($spent);
                 $object->addOverspent($spent)->addBudgetLine($budgetLine);
             }
             continue;
         }
         // one or more repetitions for budget:
         /** @var LimitRepetition $repetition */
         foreach ($repetitions as $repetition) {
             $data = $this->calculateExpenses($budget, $repetition, $accounts);
             $budgetLine = new BudgetLine();
             $budgetLine->setBudget($budget)->setRepetition($repetition)->setLeft($data['left'])->setSpent($data['expenses'])->setOverspent($data['overspent'])->setBudgeted(strval($repetition->amount));
             $object->addBudgeted(strval($repetition->amount))->addSpent($data['spent'])->addLeft($data['left'])->addOverspent($data['overspent'])->addBudgetLine($budgetLine);
         }
     }
     // stuff outside of budgets:
     $noBudget = $this->repository->spentInPeriodWithoutBudget($accounts, $start, $end);
     $budgetLine = new BudgetLine();
     $budgetLine->setOverspent($noBudget)->setSpent($noBudget);
     $object->addOverspent($noBudget)->addBudgetLine($budgetLine);
     return $object;
 }