2015-02-22 09:46:21 +01:00
|
|
|
<?php
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* BudgetRepository.php
|
2017-10-21 08:40:00 +02:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2017-10-21 08:40:00 +02: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 14:44:05 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2015-02-22 09:46:21 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Budget;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2018-06-24 08:33:06 +02:00
|
|
|
use Exception;
|
2015-02-22 09:46:21 +01:00
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\Models\BudgetLimit;
|
2018-07-22 08:27:18 +02:00
|
|
|
use FireflyIII\Models\RuleAction;
|
|
|
|
use FireflyIII\Models\RuleTrigger;
|
2019-08-31 09:35:35 +02:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2019-01-11 16:57:40 +01:00
|
|
|
use FireflyIII\Services\Internal\Destroy\BudgetDestroyService;
|
2016-03-03 08:44:20 +01:00
|
|
|
use FireflyIII\User;
|
2015-04-05 10:36:28 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2017-11-24 17:05:22 +01:00
|
|
|
use Log;
|
2015-02-22 09:46:21 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class BudgetRepository.
|
2018-07-25 19:43:02 +02:00
|
|
|
*
|
2015-02-22 09:46:21 +01:00
|
|
|
*/
|
2016-05-06 06:15:46 +02:00
|
|
|
class BudgetRepository implements BudgetRepositoryInterface
|
2015-02-22 09:46:21 +01:00
|
|
|
{
|
2016-03-03 08:44:20 +01:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
2018-09-03 08:41:03 +02:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2018-12-15 07:59:02 +01:00
|
|
|
if ('testing' === config('app.env')) {
|
2019-06-07 18:20:15 +02:00
|
|
|
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
|
2018-09-03 08:41:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-22 18:04:26 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* // it's 5.
|
|
|
|
*/
|
|
|
|
public function cleanupBudgets(): bool
|
|
|
|
{
|
|
|
|
// delete limits with amount 0:
|
|
|
|
try {
|
|
|
|
BudgetLimit::where('amount', 0)->delete();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
|
|
|
|
}
|
|
|
|
Budget::where('order', 0)->update(['order' => 100]);
|
|
|
|
|
|
|
|
// 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!
|
|
|
|
try {
|
|
|
|
BudgetLimit::find($budgetLimit->id)->delete();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::debug(sprintf('Could not delete budget limit: %s', $e->getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$count[$key] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-22 15:40:13 +01:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
2016-04-05 22:00:03 +02:00
|
|
|
* @return bool
|
2015-02-22 15:40:13 +01:00
|
|
|
*/
|
2016-04-05 22:00:03 +02:00
|
|
|
public function destroy(Budget $budget): bool
|
2015-02-22 15:40:13 +01:00
|
|
|
{
|
2019-01-11 16:57:40 +01:00
|
|
|
/** @var BudgetDestroyService $service */
|
|
|
|
$service = app(BudgetDestroyService::class);
|
|
|
|
$service->destroy($budget);
|
2015-02-22 15:40:13 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-17 17:05:16 +01:00
|
|
|
/**
|
2019-08-22 18:04:26 +02:00
|
|
|
* @param int|null $budgetId
|
2019-03-17 17:05:16 +01:00
|
|
|
* @param string|null $budgetName
|
|
|
|
*
|
|
|
|
* @return Budget|null
|
|
|
|
*/
|
2019-04-06 08:10:50 +02:00
|
|
|
public function findBudget(?int $budgetId, ?string $budgetName): ?Budget
|
2019-03-17 17:05:16 +01:00
|
|
|
{
|
|
|
|
Log::debug('Now in findBudget()');
|
2019-04-06 08:10:50 +02:00
|
|
|
Log::debug(sprintf('Searching for budget with ID #%d...', $budgetId));
|
|
|
|
$result = $this->findNull((int)$budgetId);
|
2019-05-24 05:28:41 +02:00
|
|
|
if (null === $result && null !== $budgetName && '' !== $budgetName) {
|
2019-03-17 17:05:16 +01:00
|
|
|
Log::debug(sprintf('Searching for budget with name %s...', $budgetName));
|
|
|
|
$result = $this->findByName((string)$budgetName);
|
|
|
|
}
|
|
|
|
if (null !== $result) {
|
|
|
|
Log::debug(sprintf('Found budget #%d: %s', $result->id, $result->name));
|
|
|
|
}
|
|
|
|
Log::debug(sprintf('Found result is null? %s', var_export(null === $result, true)));
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-22 18:04:26 +02:00
|
|
|
* Find budget by name.
|
2019-03-17 17:05:16 +01:00
|
|
|
*
|
2019-08-22 18:04:26 +02:00
|
|
|
* @param string|null $name
|
2019-03-17 17:05:16 +01:00
|
|
|
*
|
|
|
|
* @return Budget|null
|
|
|
|
*/
|
2019-08-22 18:04:26 +02:00
|
|
|
public function findByName(?string $name): ?Budget
|
2019-03-17 17:05:16 +01:00
|
|
|
{
|
2019-08-22 18:04:26 +02:00
|
|
|
if (null === $name) {
|
2019-03-17 17:05:16 +01:00
|
|
|
return null;
|
|
|
|
}
|
2019-08-22 18:04:26 +02:00
|
|
|
$query = sprintf('%%%s%%', $name);
|
2019-03-17 17:05:16 +01:00
|
|
|
|
2019-08-22 18:04:26 +02:00
|
|
|
return $this->user->budgets()->where('name', 'LIKE', $query)->first();
|
2019-03-17 17:05:16 +01:00
|
|
|
}
|
|
|
|
|
2018-02-16 15:19:19 +01:00
|
|
|
/**
|
2019-08-22 18:04:26 +02:00
|
|
|
* Find a budget or return NULL
|
2018-02-16 15:19:19 +01:00
|
|
|
*
|
2019-08-22 18:04:26 +02:00
|
|
|
* @param int $budgetId |null
|
2018-02-16 15:19:19 +01:00
|
|
|
*
|
|
|
|
* @return Budget|null
|
|
|
|
*/
|
2019-08-22 18:04:26 +02:00
|
|
|
public function findNull(int $budgetId = null): ?Budget
|
2018-02-16 15:19:19 +01:00
|
|
|
{
|
2019-08-22 18:04:26 +02:00
|
|
|
if (null === $budgetId) {
|
2018-07-05 18:02:02 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-22 18:04:26 +02:00
|
|
|
return $this->user->budgets()->find($budgetId);
|
2016-07-24 18:47:55 +02:00
|
|
|
}
|
|
|
|
|
2016-05-06 10:32:26 +02:00
|
|
|
/**
|
|
|
|
* This method returns the oldest journal or transaction date known to this budget.
|
|
|
|
* Will cache result.
|
|
|
|
*
|
|
|
|
* @param Budget $budget
|
2018-07-25 19:43:02 +02:00
|
|
|
*
|
2016-05-06 10:32:26 +02:00
|
|
|
* @return Carbon
|
2019-08-17 10:47:29 +02:00
|
|
|
*
|
2016-05-06 10:32:26 +02:00
|
|
|
*/
|
2018-07-23 21:49:15 +02:00
|
|
|
public function firstUseDate(Budget $budget): ?Carbon
|
2016-05-06 10:32:26 +02:00
|
|
|
{
|
2018-07-23 21:49:15 +02:00
|
|
|
$oldest = null;
|
2016-08-26 09:30:52 +02:00
|
|
|
$journal = $budget->transactionJournals()->orderBy('date', 'ASC')->first();
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null !== $journal) {
|
2016-05-06 10:32:26 +02: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 12:25:49 +01:00
|
|
|
if (null !== $transaction) {
|
2016-05-11 17:17:43 +02:00
|
|
|
$carbon = new Carbon($transaction->date);
|
|
|
|
$oldest = $carbon < $oldest ? $carbon : $oldest;
|
2016-05-06 10:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $oldest;
|
|
|
|
}
|
|
|
|
|
2015-12-27 21:17:04 +01:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 22:00:03 +02:00
|
|
|
public function getActiveBudgets(): Collection
|
2015-12-27 21:17:04 +01:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2018-10-17 15:18:09 +02:00
|
|
|
$set = $this->user->budgets()->where('active', 1)
|
2019-08-31 09:35:35 +02:00
|
|
|
->orderBy('order', 'ASC')
|
2019-05-24 05:28:41 +02:00
|
|
|
->orderBy('name', 'ASC')
|
2018-10-17 15:18:09 +02:00
|
|
|
->get();
|
2015-12-27 21:17:04 +01:00
|
|
|
|
|
|
|
return $set;
|
2015-12-31 17:46:34 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 15:21:27 +01:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-05-06 10:32:26 +02:00
|
|
|
public function getBudgets(): Collection
|
2016-01-20 15:21:27 +01:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2019-05-24 05:28:41 +02:00
|
|
|
$set = $this->user->budgets()->orderBy('order', 'DESC')
|
2019-05-04 20:58:43 +02:00
|
|
|
->orderBy('name', 'ASC')->get();
|
2016-01-20 15:21:27 +01:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
2015-04-05 10:36:28 +02:00
|
|
|
|
2018-05-07 20:35:14 +02:00
|
|
|
/**
|
|
|
|
* Get all budgets with these ID's.
|
|
|
|
*
|
|
|
|
* @param array $budgetIds
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getByIds(array $budgetIds): Collection
|
|
|
|
{
|
|
|
|
return $this->user->budgets()->whereIn('id', $budgetIds)->get();
|
|
|
|
}
|
|
|
|
|
2016-05-06 10:32:26 +02:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getInactiveBudgets(): Collection
|
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2019-05-24 05:28:41 +02:00
|
|
|
$set = $this->user->budgets()->orderBy('order', 'DESC')
|
2019-05-04 20:58:43 +02:00
|
|
|
->orderBy('name', 'ASC')->where('active', 0)->get();
|
2016-05-06 10:32:26 +02:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2019-03-02 14:12:09 +01:00
|
|
|
/**
|
|
|
|
* @param string $query
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function searchBudget(string $query): Collection
|
|
|
|
{
|
|
|
|
|
2019-07-01 20:22:35 +02:00
|
|
|
$search = $this->user->budgets();
|
|
|
|
if ('' !== $query) {
|
|
|
|
$search->where('name', 'LIKE', sprintf('%%%s%%', $query));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $search->get();
|
2019-03-02 14:12:09 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 15:18:09 +02:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
2019-08-22 18:04:26 +02:00
|
|
|
* @param int $order
|
2018-10-17 15:18:09 +02:00
|
|
|
*/
|
|
|
|
public function setBudgetOrder(Budget $budget, int $order): void
|
|
|
|
{
|
|
|
|
$budget->order = $order;
|
|
|
|
$budget->save();
|
|
|
|
}
|
|
|
|
|
2017-01-30 16:46:30 +01:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
2018-07-22 18:50:27 +02:00
|
|
|
public function setUser(User $user): void
|
2017-01-30 16:46:30 +01:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
|
|
|
|
2016-01-20 15:21:27 +01:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
2016-04-05 22:00:03 +02:00
|
|
|
public function store(array $data): Budget
|
2016-01-20 15:21:27 +01:00
|
|
|
{
|
|
|
|
$newBudget = new Budget(
|
|
|
|
[
|
2016-10-23 12:19:32 +02:00
|
|
|
'user_id' => $this->user->id,
|
2016-01-20 15:21:27 +01:00
|
|
|
'name' => $data['name'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$newBudget->save();
|
|
|
|
|
|
|
|
return $newBudget;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
2019-08-22 18:04:26 +02:00
|
|
|
* @param array $data
|
2016-01-20 15:21:27 +01:00
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
2016-04-05 22:00:03 +02:00
|
|
|
public function update(Budget $budget, array $data): Budget
|
2016-01-20 15:21:27 +01:00
|
|
|
{
|
2018-07-25 19:43:02 +02:00
|
|
|
$oldName = $budget->name;
|
2016-01-20 15:21:27 +01:00
|
|
|
$budget->name = $data['name'];
|
|
|
|
$budget->active = $data['active'];
|
|
|
|
$budget->save();
|
2018-07-25 19:43:02 +02:00
|
|
|
$this->updateRuleTriggers($oldName, $data['name']);
|
|
|
|
$this->updateRuleActions($oldName, $data['name']);
|
2018-07-22 08:27:18 +02:00
|
|
|
app('preferences')->mark();
|
|
|
|
|
2016-01-20 15:21:27 +01:00
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
|
2019-06-04 20:42:11 +02:00
|
|
|
/**
|
2019-08-22 18:04:26 +02:00
|
|
|
* @param string $oldName
|
|
|
|
* @param string $newName
|
2019-06-04 20:42:11 +02:00
|
|
|
*/
|
2019-08-22 18:04:26 +02:00
|
|
|
private function updateRuleActions(string $oldName, string $newName): void
|
2019-06-04 20:42:11 +02:00
|
|
|
{
|
2019-08-22 18:04:26 +02:00
|
|
|
$types = ['set_budget',];
|
|
|
|
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
|
|
|
|
->where('rules.user_id', $this->user->id)
|
|
|
|
->whereIn('rule_actions.action_type', $types)
|
|
|
|
->where('rule_actions.action_value', $oldName)
|
|
|
|
->get(['rule_actions.*']);
|
|
|
|
Log::debug(sprintf('Found %d actions to update.', $actions->count()));
|
|
|
|
/** @var RuleAction $action */
|
|
|
|
foreach ($actions as $action) {
|
|
|
|
$action->action_value = $newName;
|
|
|
|
$action->save();
|
|
|
|
Log::debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
|
2019-06-04 20:42:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $oldName
|
|
|
|
* @param string $newName
|
|
|
|
*/
|
|
|
|
private function updateRuleTriggers(string $oldName, string $newName): void
|
|
|
|
{
|
|
|
|
$types = ['budget_is',];
|
|
|
|
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
|
|
|
|
->where('rules.user_id', $this->user->id)
|
|
|
|
->whereIn('rule_triggers.trigger_type', $types)
|
|
|
|
->where('rule_triggers.trigger_value', $oldName)
|
|
|
|
->get(['rule_triggers.*']);
|
|
|
|
Log::debug(sprintf('Found %d triggers to update.', $triggers->count()));
|
|
|
|
/** @var RuleTrigger $trigger */
|
|
|
|
foreach ($triggers as $trigger) {
|
|
|
|
$trigger->trigger_value = $newName;
|
|
|
|
$trigger->save();
|
|
|
|
Log::debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
|
|
|
|
}
|
|
|
|
}
|
2015-03-29 08:14:32 +02:00
|
|
|
}
|