mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-26 13:36:15 +00:00 
			
		
		
		
	First attempt at including a budget report.
This commit is contained in:
		| @@ -227,6 +227,7 @@ class ReportHelper implements ReportHelperInterface | |||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param Carbon  $start |      * @param Carbon  $start | ||||||
|      * @param Carbon  $end |      * @param Carbon  $end | ||||||
| @@ -494,4 +495,73 @@ class ReportHelper implements ReportHelperInterface | |||||||
|  |  | ||||||
|         return $object; |         return $object; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param Carbon     $start | ||||||
|  |      * @param Carbon     $end | ||||||
|  |      * @param Collection $accounts | ||||||
|  |      * | ||||||
|  |      * @return BudgetCollection | ||||||
|  |      */ | ||||||
|  |     public function getBudgetReportForList(Carbon $start, Carbon $end, Collection $accounts) | ||||||
|  |     { | ||||||
|  |         $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->balanceInPeriodForList($budget, $start, $end, $accounts); | ||||||
|  |                 $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->balanceInPeriodForList($budget, $repetition->startdate, $repetition->enddate, $accounts); | ||||||
|  |                 $expenses  = $expenses * -1; | ||||||
|  |                 $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); | ||||||
|  |         $budgetLine->setSpent($noBudget); | ||||||
|  |         $object->addOverspent($noBudget); | ||||||
|  |         $object->addBudgetLine($budgetLine); | ||||||
|  |  | ||||||
|  |         return $object; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -73,6 +73,15 @@ interface ReportHelperInterface | |||||||
|      */ |      */ | ||||||
|     public function getBudgetReport(Carbon $start, Carbon $end, $shared); |     public function getBudgetReport(Carbon $start, Carbon $end, $shared); | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param Carbon     $start | ||||||
|  |      * @param Carbon     $end | ||||||
|  |      * @param Collection $accounts | ||||||
|  |      * | ||||||
|  |      * @return BudgetCollection | ||||||
|  |      */ | ||||||
|  |     public function getBudgetReportForList(Carbon $start, Carbon $end, Collection $accounts); | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param Carbon  $start |      * @param Carbon  $start | ||||||
|      * @param Carbon  $end |      * @param Carbon  $end | ||||||
|   | |||||||
| @@ -244,7 +244,7 @@ class ReportController extends Controller | |||||||
|         $accounts = $this->helper->getAccountReportForList($start, $end, $list); |         $accounts = $this->helper->getAccountReportForList($start, $end, $list); | ||||||
|         $incomes  = $this->helper->getIncomeReportForList($start, $end, $list); |         $incomes  = $this->helper->getIncomeReportForList($start, $end, $list); | ||||||
|         $expenses = $this->helper->getExpenseReportForList($start, $end, $list); |         $expenses = $this->helper->getExpenseReportForList($start, $end, $list); | ||||||
|         //        $budgets    = $this->helper->getBudgetReportForList($start, $end, $list); |         $budgets  = $this->helper->getBudgetReportForList($start, $end, $list); | ||||||
|         //        $categories = $this->helper->getCategoryReportForList($start, $end, $list); |         //        $categories = $this->helper->getCategoryReportForList($start, $end, $list); | ||||||
|         //        $balance    = $this->helper->getBalanceReportForList($start, $end, $list); |         //        $balance    = $this->helper->getBalanceReportForList($start, $end, $list); | ||||||
|         //        $bills      = $this->helper->getBillReportForList($start, $end); |         //        $bills      = $this->helper->getBillReportForList($start, $end); | ||||||
|   | |||||||
| @@ -315,6 +315,19 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn | |||||||
|         return bcmul($noBudgetSet, -1); |         return bcmul($noBudgetSet, -1); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param Budget $budget | ||||||
|  |      * @param Carbon $start | ||||||
|  |      * @param Carbon $end | ||||||
|  |      * @param Collection $accounts | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     public function balanceInPeriodForList(Budget $budget, Carbon $start, Carbon $end, Collection $accounts) | ||||||
|  |     { | ||||||
|  |         return $this->commonBalanceInPeriodForList($budget, $start, $end, $accounts); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param Budget $budget |      * @param Budget $budget | ||||||
|      * @param Carbon $start |      * @param Carbon $start | ||||||
|   | |||||||
| @@ -139,6 +139,19 @@ interface BudgetRepositoryInterface | |||||||
|      */ |      */ | ||||||
|     public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, $shared = true); |     public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, $shared = true); | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * | ||||||
|  |      * Same as ::spentInPeriod but corrects journals for a set of accounts | ||||||
|  |      * | ||||||
|  |      * @param Budget     $budget | ||||||
|  |      * @param Carbon     $start | ||||||
|  |      * @param Carbon     $end | ||||||
|  |      * @param Collection $accounts | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     public function balanceInPeriodForList(Budget $budget, Carbon $start, Carbon $end, Collection $accounts); | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param array $data |      * @param array $data | ||||||
|      * |      * | ||||||
|   | |||||||
| @@ -3,9 +3,11 @@ | |||||||
| namespace FireflyIII\Repositories\Shared; | namespace FireflyIII\Repositories\Shared; | ||||||
|  |  | ||||||
| use Carbon\Carbon; | use Carbon\Carbon; | ||||||
|  | use FireflyIII\Models\Account; | ||||||
| use FireflyIII\Models\TransactionType; | use FireflyIII\Models\TransactionType; | ||||||
| use FireflyIII\Support\CacheProperties; | use FireflyIII\Support\CacheProperties; | ||||||
| use Illuminate\Database\Query\JoinClause; | use Illuminate\Database\Query\JoinClause; | ||||||
|  | use Illuminate\Support\Collection; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Class ComponentRepository |  * Class ComponentRepository | ||||||
| @@ -63,4 +65,46 @@ class ComponentRepository | |||||||
|  |  | ||||||
|         return $sum; |         return $sum; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param            $object | ||||||
|  |      * @param Carbon     $start | ||||||
|  |      * @param Carbon     $end | ||||||
|  |      * @param Collection $accounts | ||||||
|  |      * | ||||||
|  |      * @return string | ||||||
|  |      */ | ||||||
|  |     protected function commonBalanceInPeriodForList($object, Carbon $start, Carbon $end, Collection $accounts) | ||||||
|  |     { | ||||||
|  |         $cache = new CacheProperties; // we must cache this. | ||||||
|  |         $cache->addProperty($object->id); | ||||||
|  |         $cache->addProperty(get_class($object)); | ||||||
|  |         $cache->addProperty($start); | ||||||
|  |         $cache->addProperty($end); | ||||||
|  |         $cache->addProperty($accounts); | ||||||
|  |         $cache->addProperty('balanceInPeriodList'); | ||||||
|  |  | ||||||
|  |         $ids = []; | ||||||
|  |         /** @var Account $account */ | ||||||
|  |         foreach ($accounts as $account) { | ||||||
|  |             $ids[] = $account->id; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         if ($cache->has()) { | ||||||
|  |             return $cache->get(); // @codeCoverageIgnore | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         $sum = $object->transactionjournals() | ||||||
|  |                       ->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::DEPOSIT, TransactionType::OPENING_BALANCE]) | ||||||
|  |                       ->before($end) | ||||||
|  |                       ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id') | ||||||
|  |                       ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') | ||||||
|  |                       ->whereIn('accounts.id', $ids) | ||||||
|  |                       ->after($start) | ||||||
|  |                       ->get(['transaction_journals.*'])->sum('amount'); | ||||||
|  |  | ||||||
|  |         $cache->store($sum); | ||||||
|  |  | ||||||
|  |         return $sum; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user