mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-05-03 20:56:21 +00:00
Building split transactions and fixing tests.
This commit is contained in:
@@ -15,6 +15,7 @@ use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
@@ -232,23 +233,20 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
*/
|
||||
public function getFrontpageTransactions(Account $account, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
$set = $this->user
|
||||
$query = $this->user
|
||||
->transactionjournals()
|
||||
->expanded()
|
||||
->where('source_account.id', $account->id)
|
||||
// ->with(['transactions'])
|
||||
// ->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
// ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id')->where('accounts.id', $account->id)
|
||||
// ->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id')
|
||||
// ->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||
->before($end)
|
||||
->after($start)
|
||||
// ->orderBy('transaction_journals.date', 'DESC')
|
||||
// ->orderBy('transaction_journals.order', 'ASC')
|
||||
// ->orderBy('transaction_journals.id', 'DESC')
|
||||
->take(10)
|
||||
// ->get(['transaction_journals.*', 'transaction_currencies.symbol', 'transaction_types.type']);
|
||||
->get(TransactionJournal::queryFields());
|
||||
->after($start);
|
||||
|
||||
// expand query:
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
}
|
||||
)->where('source.account_id', $account->id);
|
||||
|
||||
$set = $query->get(TransactionJournal::queryFields());
|
||||
|
||||
return $set;
|
||||
}
|
||||
@@ -290,13 +288,15 @@ class AccountRepository implements AccountRepositoryInterface
|
||||
$offset = ($page - 1) * $pageSize;
|
||||
$query = $this->user
|
||||
->transactionJournals()
|
||||
->expanded()
|
||||
->where(
|
||||
function (Builder $q) use ($account) {
|
||||
$q->where('source_account.id', $account->id);
|
||||
$q->orWhere('destination_account.id', $account->id);
|
||||
}
|
||||
);
|
||||
->expanded();
|
||||
|
||||
// expand query:
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
}
|
||||
)->where('source.account_id', $account->id);
|
||||
|
||||
|
||||
$count = $query->count();
|
||||
$set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
|
||||
|
||||
@@ -568,32 +568,9 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
public function getValidRepetitions(Budget $budget, Carbon $start, Carbon $end, LimitRepetition $ignore) : Collection
|
||||
{
|
||||
$query = $budget->limitrepetitions()
|
||||
->where( // valid when either of these are true:
|
||||
function ($q) use ($start, $end) {
|
||||
$q->where(
|
||||
function ($query) use ($start, $end) {
|
||||
// starts before start time, and the end also after start time.
|
||||
$query->where('limit_repetitions.startdate', '<=', $start->format('Y-m-d 00:00:00'));
|
||||
$query->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
);
|
||||
$q->orWhere(
|
||||
function ($query) use ($start, $end) {
|
||||
// end after end time, and start is before end time
|
||||
$query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'));
|
||||
$query->where('limit_repetitions.enddate', '>=', $end->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
);
|
||||
// start is after start and end is before end
|
||||
$q->orWhere(
|
||||
function ($query) use ($start, $end) {
|
||||
// end after end time, and start is before end time
|
||||
$query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'));
|
||||
$query->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d 00:00:00'));
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
// starts before start time, and the end also after start time.
|
||||
->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00'))
|
||||
->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'));
|
||||
if (!is_null($ignore->id)) {
|
||||
$query->where('limit_repetitions.id', '!=', $ignore->id);
|
||||
}
|
||||
@@ -683,7 +660,7 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
}
|
||||
)
|
||||
->whereIn('transactions.account_id', $ids)
|
||||
->having('transaction_count', '=', 1)
|
||||
//->having('transaction_count', '=', 1) TODO check if this still works
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->first(
|
||||
[
|
||||
|
||||
@@ -10,6 +10,7 @@ use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Shared\ComponentRepository;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
@@ -90,9 +91,16 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
||||
->after($start)
|
||||
->groupBy('transaction_journals.date');
|
||||
|
||||
$query->leftJoin(
|
||||
'transactions as destination', function (JoinClause $join) {
|
||||
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$query->whereIn('destination_account.id', $ids);
|
||||
$query->whereIn('destination.account.id', $ids);
|
||||
}
|
||||
|
||||
$result = $query->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`destination`.`amount`) AS `sum`')]);
|
||||
@@ -258,13 +266,17 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
||||
->before($end)
|
||||
->after($start)
|
||||
->groupBy('transaction_journals.date');
|
||||
$query->leftJoin(
|
||||
'transactions as source', function (JoinClause $join) {
|
||||
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
||||
}
|
||||
);
|
||||
|
||||
if ($accounts->count() > 0) {
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$query->whereIn('source_account.id', $ids);
|
||||
$query->whereIn('source.account_id', $ids);
|
||||
}
|
||||
|
||||
|
||||
$result = $query->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`source`.`amount`) AS `sum`')]);
|
||||
|
||||
$return = [];
|
||||
|
||||
@@ -336,31 +336,36 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
{
|
||||
$sourceAccount = null;
|
||||
$destinationAccount = null;
|
||||
Log::debug('Now in storeAccounts()');
|
||||
switch ($type->type) {
|
||||
case TransactionType::WITHDRAWAL:
|
||||
Log::debug('Now in storeAccounts()::withdrawal');
|
||||
list($sourceAccount, $destinationAccount) = $this->storeWithdrawalAccounts($data);
|
||||
break;
|
||||
|
||||
case TransactionType::DEPOSIT:
|
||||
Log::debug('Now in storeAccounts()::deposit');
|
||||
list($sourceAccount, $destinationAccount) = $this->storeDepositAccounts($data);
|
||||
|
||||
break;
|
||||
case TransactionType::TRANSFER:
|
||||
Log::debug('Now in storeAccounts()::transfer');
|
||||
$sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['source_account_id'])->first();
|
||||
$destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first();
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException('Did not recognise transaction type.');
|
||||
}
|
||||
Log::debug('Now in storeAccounts(), continued.');
|
||||
|
||||
if (is_null($destinationAccount)) {
|
||||
Log::error('"to"-account is null, so we cannot continue!', ['data' => $data]);
|
||||
throw new FireflyException('"to"-account is null, so we cannot continue!');
|
||||
Log::error('"destination"-account is null, so we cannot continue!', ['data' => $data]);
|
||||
throw new FireflyException('"destination"-account is null, so we cannot continue!');
|
||||
}
|
||||
|
||||
if (is_null($sourceAccount)) {
|
||||
Log::error('"from"-account is null, so we cannot continue!', ['data' => $data]);
|
||||
throw new FireflyException('"from"-account is null, so we cannot continue!');
|
||||
Log::error('"source"-account is null, so we cannot continue!', ['data' => $data]);
|
||||
throw new FireflyException('"source"-account is null, so we cannot continue!');
|
||||
|
||||
}
|
||||
|
||||
@@ -402,8 +407,10 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
private function storeWithdrawalAccounts(array $data): array
|
||||
{
|
||||
$sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['source_account_id'])->first(['accounts.*']);
|
||||
Log::debug('Now in storeWithdrawalAccounts() with ', ['name' => $data['destination_account_name'], 'len' => strlen($data['destination_account_name'])]);
|
||||
|
||||
if (strlen($data['destination_account_name']) > 0) {
|
||||
Log::debug('Now in storeWithdrawalAccounts()');
|
||||
$destinationType = AccountType::where('type', 'Expense account')->first();
|
||||
$destinationAccount = Account::firstOrCreateEncrypted(
|
||||
[
|
||||
@@ -413,6 +420,7 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
'active' => 1,
|
||||
]
|
||||
);
|
||||
Log::debug('Errors: ', ['err' => $destinationAccount->getErrors()->toArray(), 'id' => $destinationAccount->id]);
|
||||
|
||||
return [$sourceAccount, $destinationAccount];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user