mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-30 12:43:57 -06:00
Slowly move away from using the raw 'transactions.amount' field.
This commit is contained in:
parent
2d86390bc1
commit
cff08d19eb
@ -223,6 +223,21 @@ class TransactionJournal extends Model
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Account
|
||||
*/
|
||||
public function getDestinationAccountAttribute()
|
||||
{
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($this->transactions()->get() as $transaction) {
|
||||
if (floatval($transaction->amount) > 0) {
|
||||
return $transaction->account;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->transactions()->first()->account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
@ -273,24 +288,6 @@ class TransactionJournal extends Model
|
||||
return $query->where('transaction_journals.date', '<=', $date->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param EloquentBuilder $query
|
||||
* @param $amount
|
||||
*/
|
||||
public function scopeLessThan(EloquentBuilder $query, $amount)
|
||||
{
|
||||
if (is_null($this->joinedTransactions)) {
|
||||
$query->leftJoin(
|
||||
'transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id'
|
||||
);
|
||||
$this->joinedTransactions = true;
|
||||
}
|
||||
|
||||
$query->where('transactions.amount', '<=', $amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
@ -304,24 +301,6 @@ class TransactionJournal extends Model
|
||||
return $query->where('date', '=', $date->format('Y-m-d'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the account to which the money was moved.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @param EloquentBuilder $query
|
||||
* @param Account $account
|
||||
*/
|
||||
public function scopeToAccountIs(EloquentBuilder $query, Account $account)
|
||||
{
|
||||
$query->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '>', 0);
|
||||
}
|
||||
);
|
||||
$query->where('transactions.account_id', $account->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
|
@ -287,7 +287,7 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
*/
|
||||
public function getTransfersInRange(Account $account, Carbon $start, Carbon $end)
|
||||
{
|
||||
return TransactionJournal::whereIn(
|
||||
$set = TransactionJournal::whereIn(
|
||||
'id', function (Builder $q) use ($account, $start, $end) {
|
||||
$q->select('transaction_journals.id')
|
||||
->from('transactions')
|
||||
@ -297,11 +297,19 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
->where('transaction_journals.user_id', Auth::user()->id)
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->where('transactions.amount', '>', 0)
|
||||
->where('transaction_types.type', 'Transfer');
|
||||
|
||||
}
|
||||
)->get();
|
||||
$filtered = $set->filter(
|
||||
function (TransactionJournal $journal) use ($account) {
|
||||
if ($journal->destination_account->id == $account->id) {
|
||||
return $journal;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -108,16 +108,10 @@ class BillRepository implements BillRepositoryInterface
|
||||
public function getJournals(Bill $bill)
|
||||
{
|
||||
return $bill->transactionjournals()->withRelevantData()
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.amount', '>', 0);
|
||||
}
|
||||
)
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.order', 'ASC')
|
||||
->orderBy('transaction_journals.id', 'DESC')
|
||||
->get(['transaction_journals.*', 'transactions.amount']);
|
||||
->get(['transaction_journals.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -201,7 +201,7 @@ class CategoryRepository implements CategoryRepositoryInterface
|
||||
->before($end)
|
||||
->after($start)
|
||||
->transactionTypes(['Withdrawal'])
|
||||
->lessThan(0)
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')
|
||||
->leftJoin(
|
||||
'account_meta', function (JoinClause $join) {
|
||||
|
@ -76,15 +76,19 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
*/
|
||||
public function getAmountBefore(TransactionJournal $journal, Transaction $transaction)
|
||||
{
|
||||
return floatval(
|
||||
$transaction->account->transactions()->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||
)
|
||||
->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d'))
|
||||
->where('transaction_journals.order', '>=', $journal->order)
|
||||
->where('transaction_journals.id', '!=', $journal->id)
|
||||
->sum('transactions.amount')
|
||||
);
|
||||
$set = $transaction->account->transactions()->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||
)
|
||||
->where('transaction_journals.date', '<=', $journal->date->format('Y-m-d'))
|
||||
->where('transaction_journals.order', '>=', $journal->order)
|
||||
->where('transaction_journals.id', '!=', $journal->id)
|
||||
->get(['transactions.*']);
|
||||
$sum = 0;
|
||||
foreach($set as $entry) {
|
||||
$sum += $entry->amount;
|
||||
}
|
||||
return $sum;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -78,9 +78,13 @@ class TagRepository implements TagRepositoryInterface
|
||||
|
||||
/** @var Tag $tag */
|
||||
foreach ($tags as $tag) {
|
||||
$transfer = $tag->transactionjournals()->after($start)->before($end)->toAccountIs($account)->transactionTypes(['Transfer'])->first();
|
||||
if ($transfer) {
|
||||
$amount += $transfer->amount;
|
||||
$journals = $tag->transactionjournals()->after($start)->before($end)->transactionTypes(['Transfer'])->get(['transaction_journals.*']);
|
||||
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
if ($journal->destination_account->id == $account->id) {
|
||||
$amount += $journal->amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,11 +34,15 @@ class Steam
|
||||
$firstDate = is_null($firstDateObject) ? clone $date : new Carbon($firstDateObject->date);
|
||||
$date = $date < $firstDate ? $firstDate : $date;
|
||||
|
||||
$balance = floatval(
|
||||
$account->transactions()->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||
)->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->sum('transactions.amount')
|
||||
);
|
||||
|
||||
$set = $account->transactions()->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||
)->where('transaction_journals.date', '<=', $date->format('Y-m-d'))->get(['transactions.*']);
|
||||
$balance = 0;
|
||||
foreach($set as $entry) {
|
||||
$balance += $entry->amount;
|
||||
}
|
||||
|
||||
if (!$ignoreVirtualBalance) {
|
||||
$balance += floatval($account->virtual_balance);
|
||||
}
|
||||
|
@ -20,25 +20,6 @@ class Budget extends Twig_Extension
|
||||
*/
|
||||
public function getFunctions()
|
||||
{
|
||||
$functions[] = new Twig_SimpleFunction(
|
||||
'spentInRepetition', function (LimitRepetition $repetition) {
|
||||
$sum = DB::table('transactions')
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->where('transaction_journals.date', '>=', $repetition->startdate->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $repetition->enddate->format('Y-m-d'))
|
||||
->where('transaction_journals.user_id', Auth::user()->id)
|
||||
->whereNull('transactions.deleted_at')
|
||||
->where('transactions.amount', '>', 0)
|
||||
->where('limit_repetitions.id', '=', $repetition->id)
|
||||
->sum('transactions.amount');
|
||||
|
||||
return floatval($sum);
|
||||
}
|
||||
);
|
||||
|
||||
$functions[] = new Twig_SimpleFunction(
|
||||
'spentInRepetitionCorrected', function (LimitRepetition $repetition) {
|
||||
$sum
|
||||
|
Loading…
Reference in New Issue
Block a user