2015-02-22 02:46:21 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* BudgetRepository.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2015-02-22 02:46:21 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Budget;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2016-11-26 00:18:20 -06:00
|
|
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
2017-09-27 01:45:27 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
2016-12-22 09:36:56 -06:00
|
|
|
use FireflyIII\Models\AvailableBudget;
|
2015-02-22 02:46:21 -06:00
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2016-10-29 09:11:54 -05:00
|
|
|
use FireflyIII\Models\Transaction;
|
2016-12-22 09:36:56 -06:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2016-03-02 06:55:48 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2015-12-09 18:39:50 -06:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2017-09-27 01:45:27 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2016-03-03 01:44:20 -06:00
|
|
|
use FireflyIII\User;
|
2016-10-06 14:18:43 -05:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2015-04-05 03:36:28 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2017-11-24 10:05:22 -06:00
|
|
|
use Log;
|
2016-11-26 01:41:15 -06:00
|
|
|
use Navigation;
|
2016-11-19 06:37:44 -06:00
|
|
|
use stdClass;
|
2015-02-22 02:46:21 -06:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class BudgetRepository.
|
2015-02-22 02:46:21 -06:00
|
|
|
*/
|
2016-05-05 23:15:46 -05:00
|
|
|
class BudgetRepository implements BudgetRepositoryInterface
|
2015-02-22 02:46:21 -06:00
|
|
|
{
|
2016-03-03 01:44:20 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2016-06-16 13:52:59 -05:00
|
|
|
/**
|
|
|
|
* @return bool
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Exception
|
2016-06-16 13:52:59 -05:00
|
|
|
*/
|
|
|
|
public function cleanupBudgets(): bool
|
|
|
|
{
|
|
|
|
// delete limits with amount 0:
|
|
|
|
BudgetLimit::where('amount', 0)->delete();
|
|
|
|
|
2018-02-19 13:32:33 -06:00
|
|
|
// do the clean up by hand because Sqlite can be tricky with this.
|
|
|
|
$budgetLimits = BudgetLimit::orderBy('created_at', 'DESC')->get(['id', 'budget_id', 'start_date', 'end_date']);
|
|
|
|
$count = [];
|
|
|
|
/** @var BudgetLimit $budgetLimit */
|
|
|
|
foreach ($budgetLimits as $budgetLimit) {
|
|
|
|
$key = $budgetLimit->budget_id . '-' . $budgetLimit->start_date->format('Y-m-d') . $budgetLimit->end_date->format('Y-m-d');
|
|
|
|
if (isset($count[$key])) {
|
|
|
|
// delete it!
|
|
|
|
BudgetLimit::find($budgetLimit->id)->delete();
|
2017-10-19 11:35:19 -05:00
|
|
|
}
|
2018-02-19 13:32:33 -06:00
|
|
|
$count[$key] = true;
|
2017-10-19 11:35:19 -05:00
|
|
|
}
|
|
|
|
|
2016-06-16 13:52:59 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-05 04:49:06 -05:00
|
|
|
/**
|
|
|
|
* This method collects various info on budgets, used on the budget page and on the index.
|
|
|
|
*
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
|
|
|
*/
|
|
|
|
public function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
// get account information
|
|
|
|
/** @var AccountRepositoryInterface $accountRepository */
|
|
|
|
$accountRepository = app(AccountRepositoryInterface::class);
|
2017-11-12 01:42:25 -06:00
|
|
|
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
|
2017-11-18 09:30:45 -06:00
|
|
|
$defaultCurrency = app('amount')->getDefaultCurrency();
|
2017-10-05 04:49:06 -05:00
|
|
|
$return = [];
|
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
|
|
|
$budgetId = $budget->id;
|
|
|
|
$return[$budgetId] = [
|
|
|
|
'spent' => $this->spentInPeriod(new Collection([$budget]), $accounts, $start, $end),
|
|
|
|
'budgeted' => '0',
|
|
|
|
'currentRep' => false,
|
|
|
|
];
|
|
|
|
$budgetLimits = $this->getBudgetLimits($budget, $start, $end);
|
|
|
|
$otherLimits = new Collection;
|
|
|
|
|
|
|
|
// get all the budget limits relevant between start and end and examine them:
|
|
|
|
/** @var BudgetLimit $limit */
|
|
|
|
foreach ($budgetLimits as $limit) {
|
|
|
|
if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end)
|
|
|
|
) {
|
|
|
|
$return[$budgetId]['currentLimit'] = $limit;
|
2017-11-18 04:32:35 -06:00
|
|
|
$return[$budgetId]['budgeted'] = round($limit->amount, $defaultCurrency->decimal_places);
|
2017-10-05 04:49:06 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// otherwise it's just one of the many relevant repetitions:
|
|
|
|
$otherLimits->push($limit);
|
|
|
|
}
|
|
|
|
$return[$budgetId]['otherLimits'] = $otherLimits;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2015-02-22 08:40:13 -06:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
2016-04-05 15:00:03 -05:00
|
|
|
* @return bool
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Exception
|
2015-02-22 08:40:13 -06:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function destroy(Budget $budget): bool
|
2015-02-22 08:40:13 -06:00
|
|
|
{
|
|
|
|
$budget->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:37:44 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Filters entries from the result set generated by getBudgetPeriodReport.
|
2016-11-19 06:37:44 -06:00
|
|
|
*
|
|
|
|
* @param Collection $set
|
|
|
|
* @param int $budgetId
|
|
|
|
* @param array $periods
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function filterAmounts(Collection $set, int $budgetId, array $periods): array
|
|
|
|
{
|
|
|
|
$arr = [];
|
|
|
|
$keys = array_keys($periods);
|
|
|
|
foreach ($keys as $period) {
|
|
|
|
/** @var stdClass $object */
|
|
|
|
$result = $set->filter(
|
|
|
|
function (TransactionJournal $object) use ($budgetId, $period) {
|
|
|
|
$result = strval($object->period_marker) === strval($period) && $budgetId === intval($object->budget_id);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$amount = '0';
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $result->first()) {
|
2016-11-19 06:37:44 -06:00
|
|
|
$amount = $result->first()->sum_of_period;
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr[$period] = $amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
2016-04-01 06:17:07 -05:00
|
|
|
/**
|
|
|
|
* Find a budget.
|
|
|
|
*
|
|
|
|
* @param int $budgetId
|
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
|
|
|
public function find(int $budgetId): Budget
|
|
|
|
{
|
|
|
|
$budget = $this->user->budgets()->find($budgetId);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $budget) {
|
2016-04-01 06:17:07 -05:00
|
|
|
$budget = new Budget;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
|
2016-07-24 11:47:55 -05:00
|
|
|
/**
|
|
|
|
* Find a budget.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
2018-02-16 08:19:19 -06:00
|
|
|
* @return Budget|null
|
2016-07-24 11:47:55 -05:00
|
|
|
*/
|
2018-02-16 08:19:19 -06:00
|
|
|
public function findByName(string $name): ?Budget
|
2016-07-24 11:47:55 -05:00
|
|
|
{
|
2016-08-11 03:21:32 -05:00
|
|
|
$budgets = $this->user->budgets()->get(['budgets.*']);
|
2016-07-24 11:47:55 -05:00
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
|
|
|
if ($budget->name === $name) {
|
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 08:19:19 -06:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a budget or return NULL
|
|
|
|
*
|
|
|
|
* @param int $budgetId
|
|
|
|
*
|
|
|
|
* @return Budget|null
|
|
|
|
*/
|
|
|
|
public function findNull(int $budgetId): ?Budget
|
|
|
|
{
|
|
|
|
return $this->user->budgets()->find($budgetId);
|
2016-07-24 11:47:55 -05:00
|
|
|
}
|
|
|
|
|
2016-05-06 03:32:26 -05:00
|
|
|
/**
|
|
|
|
* This method returns the oldest journal or transaction date known to this budget.
|
|
|
|
* Will cache result.
|
|
|
|
*
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function firstUseDate(Budget $budget): Carbon
|
|
|
|
{
|
|
|
|
$oldest = Carbon::create()->startOfYear();
|
2016-08-26 02:30:52 -05:00
|
|
|
$journal = $budget->transactionJournals()->orderBy('date', 'ASC')->first();
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $journal) {
|
2016-05-06 03:32:26 -05:00
|
|
|
$oldest = $journal->date < $oldest ? $journal->date : $oldest;
|
|
|
|
}
|
|
|
|
|
|
|
|
$transaction = $budget
|
|
|
|
->transactions()
|
|
|
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.id')
|
|
|
|
->orderBy('transaction_journals.date', 'ASC')->first(['transactions.*', 'transaction_journals.date']);
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $transaction) {
|
2016-05-11 10:17:43 -05:00
|
|
|
$carbon = new Carbon($transaction->date);
|
|
|
|
$oldest = $carbon < $oldest ? $carbon : $oldest;
|
2016-05-06 03:32:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $oldest;
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:17:04 -06:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getActiveBudgets(): Collection
|
2015-12-27 14:17:04 -06:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->user->budgets()->where('active', 1)->get();
|
2015-12-27 14:17:04 -06:00
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
2015-12-31 10:46:34 -06:00
|
|
|
}
|
|
|
|
|
2015-04-06 02:15:59 -05:00
|
|
|
/**
|
2016-05-06 03:32:26 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
2015-04-06 02:15:59 -05:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-12-30 01:41:48 -06:00
|
|
|
public function getAllBudgetLimits(Carbon $start, Carbon $end): Collection
|
2015-04-06 02:15:59 -05:00
|
|
|
{
|
2016-12-30 01:41:48 -06:00
|
|
|
$set = BudgetLimit::leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
|
|
|
->with(['budget'])
|
|
|
|
->where('budgets.user_id', $this->user->id)
|
|
|
|
->where(
|
2016-12-30 02:02:48 -06:00
|
|
|
function (Builder $q5) use ($start, $end) {
|
|
|
|
$q5->where(
|
|
|
|
function (Builder $q1) use ($start, $end) {
|
|
|
|
$q1->where(
|
|
|
|
function (Builder $q2) use ($start, $end) {
|
|
|
|
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00'));
|
|
|
|
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->orWhere(
|
|
|
|
function (Builder $q3) use ($start, $end) {
|
|
|
|
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00'));
|
|
|
|
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
);
|
2016-12-30 01:41:48 -06:00
|
|
|
}
|
|
|
|
)
|
|
|
|
->orWhere(
|
2016-12-30 02:02:48 -06:00
|
|
|
function (Builder $q4) use ($start, $end) {
|
|
|
|
// or start is before start AND end is after end.
|
|
|
|
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 00:00:00'));
|
|
|
|
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00'));
|
2016-12-30 01:41:48 -06:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-12-30 02:02:48 -06:00
|
|
|
)->get(['budget_limits.*']);
|
2015-05-19 23:50:15 -05:00
|
|
|
|
2015-07-09 04:13:38 -05:00
|
|
|
return $set;
|
2015-04-05 03:36:28 -05:00
|
|
|
}
|
|
|
|
|
2016-12-22 09:36:56 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string
|
|
|
|
{
|
|
|
|
$amount = '0';
|
|
|
|
$availableBudget = $this->user->availableBudgets()
|
|
|
|
->where('transaction_currency_id', $currency->id)
|
2018-03-07 13:21:51 -06:00
|
|
|
->where('start_date', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('end_date', $end->format('Y-m-d 00:00:00'))->first();
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $availableBudget) {
|
2016-12-22 09:36:56 -06:00
|
|
|
$amount = strval($availableBudget->amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $amount;
|
|
|
|
}
|
|
|
|
|
2016-12-29 13:48:33 -06:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getBudgetLimits(Budget $budget, Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
|
|
|
$set = $budget->budgetLimits()
|
|
|
|
->where(
|
2016-12-30 02:02:48 -06:00
|
|
|
function (Builder $q5) use ($start, $end) {
|
|
|
|
$q5->where(
|
|
|
|
function (Builder $q1) use ($start, $end) {
|
|
|
|
// budget limit ends within period
|
|
|
|
$q1->where(
|
|
|
|
function (Builder $q2) use ($start, $end) {
|
|
|
|
$q2->where('budget_limits.end_date', '>=', $start->format('Y-m-d 00:00:00'));
|
|
|
|
$q2->where('budget_limits.end_date', '<=', $end->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
)
|
|
|
|
// budget limit start within period
|
|
|
|
->orWhere(
|
|
|
|
function (Builder $q3) use ($start, $end) {
|
|
|
|
$q3->where('budget_limits.start_date', '>=', $start->format('Y-m-d 00:00:00'));
|
|
|
|
$q3->where('budget_limits.start_date', '<=', $end->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
);
|
2016-12-29 13:48:33 -06:00
|
|
|
}
|
|
|
|
)
|
|
|
|
->orWhere(
|
2016-12-30 02:02:48 -06:00
|
|
|
function (Builder $q4) use ($start, $end) {
|
|
|
|
// or start is before start AND end is after end.
|
|
|
|
$q4->where('budget_limits.start_date', '<=', $start->format('Y-m-d 00:00:00'));
|
|
|
|
$q4->where('budget_limits.end_date', '>=', $end->format('Y-m-d 00:00:00'));
|
|
|
|
}
|
|
|
|
);
|
2016-12-30 01:41:48 -06:00
|
|
|
}
|
2017-01-05 02:07:04 -06:00
|
|
|
)->orderBy('budget_limits.start_date', 'DESC')->get(['budget_limits.*']);
|
2016-12-29 13:48:33 -06:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-11-19 06:37:44 -06:00
|
|
|
/**
|
2016-12-03 14:03:20 -06:00
|
|
|
* This method is being used to generate the budget overview in the year/multi-year report. Its used
|
|
|
|
* in both the year/multi-year budget overview AND in the accompanying chart.
|
2016-11-19 06:37:44 -06:00
|
|
|
*
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
2016-11-26 00:18:20 -06:00
|
|
|
* @return array
|
2016-11-19 06:37:44 -06:00
|
|
|
*/
|
2016-12-04 11:02:19 -06:00
|
|
|
public function getBudgetPeriodReport(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array
|
2016-11-19 06:37:44 -06:00
|
|
|
{
|
2016-11-26 01:41:15 -06:00
|
|
|
$carbonFormat = Navigation::preferredCarbonFormat($start, $end);
|
2016-12-03 13:38:13 -06:00
|
|
|
$data = [];
|
|
|
|
// prep data array:
|
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
|
|
|
$data[$budget->id] = [
|
|
|
|
'name' => $budget->name,
|
|
|
|
'sum' => '0',
|
|
|
|
'entries' => [],
|
|
|
|
];
|
2016-11-19 06:37:44 -06:00
|
|
|
}
|
2016-12-03 13:38:13 -06:00
|
|
|
|
|
|
|
// get all transactions:
|
2016-11-26 00:18:20 -06:00
|
|
|
/** @var JournalCollectorInterface $collector */
|
|
|
|
$collector = app(JournalCollectorInterface::class);
|
2016-12-03 13:38:13 -06:00
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end);
|
|
|
|
$collector->setBudgets($budgets);
|
2016-11-26 00:18:20 -06:00
|
|
|
$transactions = $collector->getJournals();
|
|
|
|
|
2016-12-03 13:38:13 -06:00
|
|
|
// loop transactions:
|
|
|
|
/** @var Transaction $transaction */
|
2016-11-26 00:18:20 -06:00
|
|
|
foreach ($transactions as $transaction) {
|
2016-12-03 13:38:13 -06:00
|
|
|
$budgetId = max(intval($transaction->transaction_journal_budget_id), intval($transaction->transaction_budget_id));
|
|
|
|
$date = $transaction->date->format($carbonFormat);
|
|
|
|
$data[$budgetId]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date] ?? '0', $transaction->transaction_amount);
|
2016-11-19 06:37:44 -06:00
|
|
|
}
|
2016-12-03 14:03:20 -06:00
|
|
|
|
2016-11-26 00:18:20 -06:00
|
|
|
return $data;
|
2016-11-19 06:37:44 -06:00
|
|
|
}
|
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-05-06 03:32:26 -05:00
|
|
|
public function getBudgets(): Collection
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2016-05-06 03:32:26 -05:00
|
|
|
$set = $this->user->budgets()->get();
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
2015-04-05 03:36:28 -05:00
|
|
|
|
2016-05-06 03:32:26 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getInactiveBudgets(): Collection
|
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
|
|
|
$set = $this->user->budgets()->where('active', 0)->get();
|
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-12-04 11:02:19 -06:00
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array
|
|
|
|
{
|
|
|
|
$carbonFormat = Navigation::preferredCarbonFormat($start, $end);
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
|
|
|
$collector = app(JournalCollectorInterface::class);
|
|
|
|
$collector->setAccounts($accounts)->setRange($start, $end);
|
|
|
|
$collector->setTypes([TransactionType::WITHDRAWAL]);
|
|
|
|
$collector->withoutBudget();
|
|
|
|
$transactions = $collector->getJournals();
|
|
|
|
$result = [
|
|
|
|
'entries' => [],
|
|
|
|
'name' => strval(trans('firefly.no_budget')),
|
|
|
|
'sum' => '0',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
$date = $transaction->date->format($carbonFormat);
|
|
|
|
|
|
|
|
if (!isset($result['entries'][$date])) {
|
|
|
|
$result['entries'][$date] = '0';
|
|
|
|
}
|
|
|
|
$result['entries'][$date] = bcadd($result['entries'][$date], $transaction->transaction_amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-12-22 09:36:56 -06:00
|
|
|
/**
|
|
|
|
* @param TransactionCurrency $currency
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param string $amount
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): bool
|
|
|
|
{
|
|
|
|
$availableBudget = $this->user->availableBudgets()
|
|
|
|
->where('transaction_currency_id', $currency->id)
|
2018-03-07 13:21:51 -06:00
|
|
|
->where('start_date', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('end_date', $end->format('Y-m-d 00:00:00'))->first();
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $availableBudget) {
|
2016-12-22 09:36:56 -06:00
|
|
|
$availableBudget = new AvailableBudget;
|
|
|
|
$availableBudget->user()->associate($this->user);
|
|
|
|
$availableBudget->transactionCurrency()->associate($currency);
|
2018-02-04 02:22:52 -06:00
|
|
|
$availableBudget->start_date = $start->format('Y-m-d 00:00:00');
|
|
|
|
$availableBudget->end_date = $end->format('Y-m-d 00:00:00');
|
2016-12-22 09:36:56 -06:00
|
|
|
}
|
|
|
|
$availableBudget->amount = $amount;
|
|
|
|
$availableBudget->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-30 09:46:30 -06:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function setUser(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2017-01-05 02:07:04 -06:00
|
|
|
/**
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-05 02:08:35 -06:00
|
|
|
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string
|
2017-01-05 02:07:04 -06:00
|
|
|
{
|
|
|
|
/** @var JournalCollectorInterface $collector */
|
2017-02-05 09:16:15 -06:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
|
|
|
$collector->setUser($this->user);
|
2017-10-14 00:58:29 -05:00
|
|
|
$collector->setRange($start, $end)->setBudgets($budgets)->withBudgetInformation();
|
2017-01-05 02:07:04 -06:00
|
|
|
|
2017-01-05 02:07:56 -06:00
|
|
|
if ($accounts->count() > 0) {
|
2017-01-05 02:07:04 -06:00
|
|
|
$collector->setAccounts($accounts);
|
|
|
|
}
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $accounts->count()) {
|
2017-01-05 02:07:04 -06:00
|
|
|
$collector->setAllAssetAccounts();
|
|
|
|
}
|
|
|
|
|
|
|
|
$set = $collector->getJournals();
|
|
|
|
$sum = strval($set->sum('transaction_amount'));
|
|
|
|
|
|
|
|
return $sum;
|
|
|
|
}
|
|
|
|
|
2016-05-11 10:17:43 -05:00
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-05 02:10:04 -06:00
|
|
|
public function spentInPeriodWoBudget(Collection $accounts, Carbon $start, Carbon $end): string
|
2016-05-11 10:17:43 -05:00
|
|
|
{
|
2017-01-05 02:07:56 -06:00
|
|
|
/** @var JournalCollectorInterface $collector */
|
2017-02-05 09:16:15 -06:00
|
|
|
$collector = app(JournalCollectorInterface::class);
|
|
|
|
$collector->setUser($this->user);
|
2017-01-05 02:07:56 -06:00
|
|
|
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
|
2016-05-11 10:17:43 -05:00
|
|
|
|
|
|
|
if ($accounts->count() > 0) {
|
2017-01-05 02:07:56 -06:00
|
|
|
$collector->setAccounts($accounts);
|
2016-05-11 10:17:43 -05:00
|
|
|
}
|
2017-11-15 05:25:49 -06:00
|
|
|
if (0 === $accounts->count()) {
|
2017-01-05 02:07:56 -06:00
|
|
|
$collector->setAllAssetAccounts();
|
2016-08-02 12:42:41 -05:00
|
|
|
}
|
2016-05-06 03:32:26 -05:00
|
|
|
|
2017-01-05 02:07:56 -06:00
|
|
|
$set = $collector->getJournals();
|
|
|
|
$set = $set->filter(
|
|
|
|
function (Transaction $transaction) {
|
|
|
|
if (bccomp($transaction->transaction_amount, '0') === -1) {
|
|
|
|
return $transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$sum = strval($set->sum('transaction_amount'));
|
|
|
|
|
2016-05-05 23:15:46 -05:00
|
|
|
return $sum;
|
|
|
|
}
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function store(array $data): Budget
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
$newBudget = new Budget(
|
|
|
|
[
|
2016-10-23 05:19:32 -05:00
|
|
|
'user_id' => $this->user->id,
|
2016-01-20 08:21:27 -06:00
|
|
|
'name' => $data['name'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$newBudget->save();
|
|
|
|
|
|
|
|
return $newBudget;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function update(Budget $budget, array $data): Budget
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
// update the account:
|
|
|
|
$budget->name = $data['name'];
|
|
|
|
$budget->active = $data['active'];
|
|
|
|
$budget->save();
|
|
|
|
|
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
2016-04-28 03:59:36 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2017-11-18 04:32:35 -06:00
|
|
|
* @param string $amount
|
2016-01-20 08:21:27 -06:00
|
|
|
*
|
|
|
|
* @return BudgetLimit
|
2017-12-22 11:32:43 -06:00
|
|
|
*
|
2017-12-17 07:30:53 -06:00
|
|
|
* @throws \Exception
|
2016-01-20 08:21:27 -06:00
|
|
|
*/
|
2017-11-18 04:32:35 -06:00
|
|
|
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $amount): BudgetLimit
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
2018-02-19 13:32:33 -06:00
|
|
|
$this->cleanupBudgets();
|
2017-10-15 07:38:12 -05:00
|
|
|
// count the limits:
|
|
|
|
$limits = $budget->budgetlimits()
|
2018-02-04 02:22:52 -06:00
|
|
|
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
2017-10-15 07:38:12 -05:00
|
|
|
->get(['budget_limits.*'])->count();
|
2017-11-24 10:05:22 -06:00
|
|
|
Log::debug(sprintf('Found %d budget limits.', $limits));
|
2016-12-29 13:52:02 -06:00
|
|
|
// there might be a budget limit for these dates:
|
2016-01-20 08:21:27 -06:00
|
|
|
/** @var BudgetLimit $limit */
|
2016-04-28 03:59:36 -05:00
|
|
|
$limit = $budget->budgetlimits()
|
2018-02-04 02:22:52 -06:00
|
|
|
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
2016-12-29 13:52:02 -06:00
|
|
|
->first(['budget_limits.*']);
|
2016-04-28 03:59:36 -05:00
|
|
|
|
2017-10-15 07:38:12 -05:00
|
|
|
// if more than 1 limit found, delete the others:
|
2017-11-15 05:25:49 -06:00
|
|
|
if ($limits > 1 && null !== $limit) {
|
2017-11-24 10:05:22 -06:00
|
|
|
Log::debug(sprintf('Found more than 1, delete all except #%d', $limit->id));
|
2017-10-15 07:38:12 -05:00
|
|
|
$budget->budgetlimits()
|
2018-02-04 02:22:52 -06:00
|
|
|
->where('budget_limits.start_date', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('budget_limits.end_date', $end->format('Y-m-d 00:00:00'))
|
2017-10-15 07:38:12 -05:00
|
|
|
->where('budget_limits.id', '!=', $limit->id)->delete();
|
|
|
|
}
|
|
|
|
|
2016-04-28 03:59:36 -05:00
|
|
|
// delete if amount is zero.
|
2017-11-24 10:05:22 -06:00
|
|
|
// Returns 0 if the two operands are equal,
|
|
|
|
// 1 if the left_operand is larger than the right_operand, -1 otherwise.
|
|
|
|
if (null !== $limit && bccomp($amount, '0') <= 0) {
|
|
|
|
Log::debug(sprintf('%s is zero, delete budget limit #%d', $amount, $limit->id));
|
2016-04-28 03:59:36 -05:00
|
|
|
$limit->delete();
|
|
|
|
|
|
|
|
return new BudgetLimit;
|
|
|
|
}
|
|
|
|
// update if exists:
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $limit) {
|
2017-11-24 10:05:22 -06:00
|
|
|
Log::debug(sprintf('Existing budget limit is #%d, update this to amount %s', $limit->id, $amount));
|
2016-04-28 03:59:36 -05:00
|
|
|
$limit->amount = $amount;
|
2016-01-20 08:21:27 -06:00
|
|
|
$limit->save();
|
|
|
|
|
2016-04-28 03:59:36 -05:00
|
|
|
return $limit;
|
2016-01-20 08:21:27 -06:00
|
|
|
}
|
2017-11-24 10:05:22 -06:00
|
|
|
Log::debug('No existing budget limit, create a new one');
|
2016-12-29 13:52:02 -06:00
|
|
|
// or create one and return it.
|
2016-04-28 03:59:36 -05:00
|
|
|
$limit = new BudgetLimit;
|
|
|
|
$limit->budget()->associate($budget);
|
2018-02-04 02:22:52 -06:00
|
|
|
$limit->start_date = $start->format('Y-m-d 00:00:00');
|
|
|
|
$limit->end_date = $end->format('Y-m-d 00:00:00');
|
2016-12-29 13:52:02 -06:00
|
|
|
$limit->amount = $amount;
|
2016-04-28 03:59:36 -05:00
|
|
|
$limit->save();
|
2017-11-24 10:05:22 -06:00
|
|
|
Log::debug(sprintf('Created new budget limit with ID #%d and amount %s', $limit->id, $amount));
|
2016-04-28 03:59:36 -05:00
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
return $limit;
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|