mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Removed unused budget methods.
This commit is contained in:
@@ -24,9 +24,7 @@ use FireflyIII\Models\TransactionJournal;
|
|||||||
use FireflyIII\Models\TransactionType;
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Query\JoinClause;
|
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
|
||||||
use Navigation;
|
use Navigation;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
|
||||||
@@ -435,92 +433,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $budgets
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string
|
|
||||||
{
|
|
||||||
// collect amount of transaction journals, which is easy:
|
|
||||||
$budgetIds = $budgets->pluck('id')->toArray();
|
|
||||||
|
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
|
||||||
|
|
||||||
Log::debug(sprintf('spentInPeriod: Now in spentInPeriod for these budgets (%d): ', count($budgetIds)), $budgetIds);
|
|
||||||
Log::debug('spentInPeriod: and these accounts: ', $accountIds);
|
|
||||||
Log::debug(sprintf('spentInPeriod: Start date is "%s", end date is "%s"', $start->format('Y-m-d'), $end->format('Y-m-d')));
|
|
||||||
|
|
||||||
$fromJournalsQuery = TransactionJournal::leftJoin(
|
|
||||||
'budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'
|
|
||||||
)
|
|
||||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
|
||||||
->leftJoin(
|
|
||||||
'transactions', function (JoinClause $join) {
|
|
||||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where(
|
|
||||||
'transactions.amount', '<', 0
|
|
||||||
);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
|
||||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
|
||||||
->whereNull('transaction_journals.deleted_at')
|
|
||||||
->whereNull('transactions.deleted_at')
|
|
||||||
->where('transaction_journals.user_id', $this->user->id)
|
|
||||||
->where('transaction_types.type', 'Withdrawal');
|
|
||||||
|
|
||||||
// add budgets:
|
|
||||||
if ($budgets->count() > 0) {
|
|
||||||
$fromJournalsQuery->whereIn('budget_transaction_journal.budget_id', $budgetIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add accounts:
|
|
||||||
if ($accounts->count() > 0) {
|
|
||||||
$fromJournalsQuery->whereIn('transactions.account_id', $accountIds);
|
|
||||||
}
|
|
||||||
$first = strval($fromJournalsQuery->sum('transactions.amount'));
|
|
||||||
Log::debug(sprintf('spentInPeriod: Result from first query: %s', $first));
|
|
||||||
unset($fromJournalsQuery);
|
|
||||||
|
|
||||||
// collect amount from transactions:
|
|
||||||
/**
|
|
||||||
* select transactions.id, budget_transaction.budget_id , transactions.amount
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* and budget_transaction.budget_id in (1,61)
|
|
||||||
* and transactions.account_id in (2)
|
|
||||||
*/
|
|
||||||
$fromTransactionsQuery = Transaction::leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'transactions.id')
|
|
||||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
||||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
|
||||||
->whereNull('transactions.deleted_at')
|
|
||||||
->whereNull('transaction_journals.deleted_at')
|
|
||||||
->where('transactions.amount', '<', 0)
|
|
||||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
|
||||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
|
||||||
->where('transaction_journals.user_id', $this->user->id)
|
|
||||||
->where('transaction_types.type', 'Withdrawal');
|
|
||||||
|
|
||||||
// add budgets:
|
|
||||||
if ($budgets->count() > 0) {
|
|
||||||
$fromTransactionsQuery->whereIn('budget_transaction.budget_id', $budgetIds);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add accounts:
|
|
||||||
if ($accounts->count() > 0) {
|
|
||||||
$fromTransactionsQuery->whereIn('transactions.account_id', $accountIds);
|
|
||||||
}
|
|
||||||
$second = strval($fromTransactionsQuery->sum('transactions.amount'));
|
|
||||||
Log::debug(sprintf('spentInPeriod: Result from second query: %s', $second));
|
|
||||||
|
|
||||||
Log::debug(sprintf('spentInPeriod: FINAL: %s', bcadd($first, $second)));
|
|
||||||
|
|
||||||
return bcadd($first, $second);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $budgets
|
* @param Collection $budgets
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
@@ -535,10 +447,10 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
$collector = app(JournalCollectorInterface::class, [$this->user]);
|
$collector = app(JournalCollectorInterface::class, [$this->user]);
|
||||||
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setBudgets($budgets);
|
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setBudgets($budgets);
|
||||||
|
|
||||||
if($accounts->count() > 0) {
|
if ($accounts->count() > 0) {
|
||||||
$collector->setAccounts($accounts);
|
$collector->setAccounts($accounts);
|
||||||
}
|
}
|
||||||
if($accounts->count() === 0) {
|
if ($accounts->count() === 0) {
|
||||||
$collector->setAllAssetAccounts();
|
$collector->setAllAssetAccounts();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -555,62 +467,31 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function spentInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): string
|
public function spentInPeriodWithoutBudgetCollector(Collection $accounts, Carbon $start, Carbon $end): string
|
||||||
{
|
{
|
||||||
$types = [TransactionType::WITHDRAWAL];
|
/** @var JournalCollectorInterface $collector */
|
||||||
$query = $this->user->transactionJournals()
|
$collector = app(JournalCollectorInterface::class, [$this->user]);
|
||||||
->distinct()
|
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
|
||||||
->transactionTypes($types)
|
|
||||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
|
||||||
->leftJoin(
|
|
||||||
'transactions as source', function (JoinClause $join) {
|
|
||||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
->leftJoin(
|
|
||||||
'transactions as destination', function (JoinClause $join) {
|
|
||||||
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
|
||||||
}
|
|
||||||
)
|
|
||||||
->leftJoin('budget_transaction', 'source.id', '=', 'budget_transaction.transaction_id')
|
|
||||||
->whereNull('budget_transaction_journal.id')
|
|
||||||
->whereNull('budget_transaction.id')
|
|
||||||
->before($end)
|
|
||||||
->after($start)
|
|
||||||
->whereNull('source.deleted_at')
|
|
||||||
->whereNull('destination.deleted_at')
|
|
||||||
->where('transaction_journals.completed', 1);
|
|
||||||
|
|
||||||
if ($accounts->count() > 0) {
|
if ($accounts->count() > 0) {
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
$collector->setAccounts($accounts);
|
||||||
$query->where(
|
|
||||||
// source.account_id in accountIds XOR destination.account_id in accountIds
|
|
||||||
function (Builder $sourceXorDestinationQuery) use ($accountIds) {
|
|
||||||
$sourceXorDestinationQuery->where(
|
|
||||||
function (Builder $inSourceButNotDestinationQuery) use ($accountIds) {
|
|
||||||
$inSourceButNotDestinationQuery->whereIn('source.account_id', $accountIds)
|
|
||||||
->whereNotIn('destination.account_id', $accountIds);
|
|
||||||
}
|
}
|
||||||
)->orWhere(
|
if ($accounts->count() === 0) {
|
||||||
function (Builder $inDestinationButNotSourceQuery) use ($accountIds) {
|
$collector->setAllAssetAccounts();
|
||||||
$inDestinationButNotSourceQuery->whereIn('destination.account_id', $accountIds)
|
}
|
||||||
->whereNotIn('source.account_id', $accountIds);
|
|
||||||
|
$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'));
|
||||||
}
|
|
||||||
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
|
|
||||||
$sum = '0';
|
|
||||||
if (count($ids) > 0) {
|
|
||||||
$sum = strval(
|
|
||||||
$this->user->transactions()
|
|
||||||
->whereIn('transaction_journal_id', $ids)
|
|
||||||
->where('amount', '<', '0')
|
|
||||||
->whereNull('transactions.deleted_at')
|
|
||||||
->sum('amount')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $sum;
|
return $sum;
|
||||||
}
|
}
|
||||||
@@ -690,40 +571,4 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
|
|
||||||
return $limit;
|
return $limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function spentInPeriodWithoutBudgetCollector(Collection $accounts, Carbon $start, Carbon $end): string
|
|
||||||
{
|
|
||||||
/** @var JournalCollectorInterface $collector */
|
|
||||||
$collector = app(JournalCollectorInterface::class, [$this->user]);
|
|
||||||
$collector->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
|
|
||||||
|
|
||||||
if ($accounts->count() > 0) {
|
|
||||||
$collector->setAccounts($accounts);
|
|
||||||
}
|
|
||||||
if ($accounts->count() === 0) {
|
|
||||||
$collector->setAllAssetAccounts();
|
|
||||||
}
|
|
||||||
|
|
||||||
$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'));
|
|
||||||
|
|
||||||
return $sum;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -149,16 +149,6 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): bool;
|
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): bool;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $budgets
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $budgets
|
* @param Collection $budgets
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
@@ -169,15 +159,6 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function spentInPeriodCollector(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string;
|
public function spentInPeriodCollector(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Collection $accounts
|
|
||||||
* @param Carbon $start
|
|
||||||
* @param Carbon $end
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function spentInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): string;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
|
Reference in New Issue
Block a user