mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Updated budget provider and budget repository.
This commit is contained in:
49
app/Providers/BudgetServiceProvider.php
Normal file
49
app/Providers/BudgetServiceProvider.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Providers;
|
||||
|
||||
use Auth;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
/**
|
||||
* Class BudgetServiceProvider
|
||||
*
|
||||
* @package FireflyIII\Providers
|
||||
*/
|
||||
class BudgetServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind(
|
||||
'FireflyIII\Repositories\Budget\BudgetRepositoryInterface',
|
||||
function (Application $app, array $arguments) {
|
||||
if (!isset($arguments[0]) && Auth::check()) {
|
||||
return app('FireflyIII\Repositories\Budget\BudgetRepository', [Auth::user()]);
|
||||
} else {
|
||||
if (!isset($arguments[0]) && !Auth::check()) {
|
||||
throw new FireflyException('There is no user present.');
|
||||
}
|
||||
}
|
||||
|
||||
return app('FireflyIII\Repositories\Budget\BudgetRepository', $arguments);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@@ -84,7 +84,6 @@ class FireflyServiceProvider extends ServiceProvider
|
||||
);
|
||||
|
||||
|
||||
$this->app->bind('FireflyIII\Repositories\Budget\BudgetRepositoryInterface', 'FireflyIII\Repositories\Budget\BudgetRepository');
|
||||
$this->app->bind('FireflyIII\Repositories\Category\CategoryRepositoryInterface', 'FireflyIII\Repositories\Category\CategoryRepository');
|
||||
$this->app->bind('FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface', 'FireflyIII\Repositories\Category\SingleCategoryRepository');
|
||||
$this->app->bind('FireflyIII\Repositories\Journal\JournalRepositoryInterface', 'FireflyIII\Repositories\Journal\JournalRepository');
|
||||
|
@@ -3,7 +3,6 @@ declare(strict_types = 1);
|
||||
|
||||
namespace FireflyIII\Repositories\Budget;
|
||||
|
||||
use Auth;
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use FireflyIII\Models\Budget;
|
||||
@@ -12,12 +11,14 @@ use FireflyIII\Models\LimitRepetition;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Shared\ComponentRepository;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Input;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class BudgetRepository
|
||||
@@ -26,6 +27,19 @@ use Input;
|
||||
*/
|
||||
class BudgetRepository extends ComponentRepository implements BudgetRepositoryInterface
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/**
|
||||
* BillRepository constructor.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
public function __construct(User $user)
|
||||
{
|
||||
Log::debug('Constructed bill repository for user #' . $user->id . ' (' . $user->email . ')');
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
@@ -83,7 +97,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
public function getActiveBudgets()
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()->where('active', 1)->get();
|
||||
$set = $this->user->budgets()->where('active', 1)->get();
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
@@ -108,7 +122,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))
|
||||
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))
|
||||
->where('budgets.user_id', Auth::user()->id)
|
||||
->where('budgets.user_id', $this->user->id)
|
||||
->get(['limit_repetitions.*', 'budget_limits.budget_id']);
|
||||
}
|
||||
|
||||
@@ -125,7 +139,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
{
|
||||
$budgetIds = $budgets->pluck('id')->toArray();
|
||||
|
||||
$set = Auth::user()->budgets()
|
||||
$set = $this->user->budgets()
|
||||
->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d'))
|
||||
@@ -150,7 +164,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
public function getBudgets()
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()->get();
|
||||
$set = $this->user->budgets()->get();
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
@@ -176,7 +190,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()
|
||||
$set = $this->user->budgets()
|
||||
->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||
->leftJoin(
|
||||
@@ -238,7 +252,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
$budgetIds = $budgets->pluck('id')->toArray();
|
||||
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()
|
||||
$set = $this->user->budgets()
|
||||
->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||
->leftJoin(
|
||||
@@ -294,7 +308,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()
|
||||
$set = $this->user
|
||||
->budgets()
|
||||
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budgets.id')
|
||||
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
@@ -355,7 +369,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
*/
|
||||
public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end)
|
||||
{
|
||||
$set = Auth::user()->budgets()
|
||||
$set = $this->user->budgets()
|
||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.budget_id', '=', 'budgets.id')
|
||||
->leftJoin('transaction_journals', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
@@ -392,7 +406,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
public function getInactiveBudgets()
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()->where('active', 0)->get();
|
||||
$set = $this->user->budgets()->where('active', 0)->get();
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
@@ -446,7 +460,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
*/
|
||||
public function getWithoutBudget(Carbon $start, Carbon $end)
|
||||
{
|
||||
return Auth::user()
|
||||
return $this->user
|
||||
->transactionjournals()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
@@ -467,7 +481,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
*/
|
||||
public function getWithoutBudgetSum(Carbon $start, Carbon $end): string
|
||||
{
|
||||
$entry = Auth::user()
|
||||
$entry = $this->user
|
||||
->transactionjournals()
|
||||
->whereNotIn(
|
||||
'transaction_journals.id', function (QueryBuilder $query) use ($start, $end) {
|
||||
@@ -518,7 +532,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
{
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
/** @var Collection $query */
|
||||
$query = Auth::user()->transactionJournals()
|
||||
$query = $this->user->transactionJournals()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||
@@ -559,7 +573,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
{
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$budgetIds = $budgets->pluck('id')->toArray();
|
||||
$set = Auth::user()->transactionjournals()
|
||||
$set = $this->user->transactionjournals()
|
||||
->leftJoin(
|
||||
'transactions AS t_from', function (JoinClause $join) {
|
||||
$join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0);
|
||||
|
Reference in New Issue
Block a user