Files
firefly-iii/app/Helpers/Collector/GroupCollector.php

1297 lines
42 KiB
PHP
Raw Normal View History

<?php
/**
* GroupCollector.php
2020-01-28 08:46:01 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Helpers\Collector;
use Carbon\Carbon;
2019-05-30 06:23:25 +02:00
use Carbon\Exceptions\InvalidDateException;
use Exception;
2019-03-25 15:14:09 +01:00
use FireflyIII\Models\Bill;
use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\Tag;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionGroup;
2019-08-23 09:41:31 +02:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
2019-05-30 06:23:25 +02:00
use Log;
/**
* Class GroupCollector
2019-08-23 09:41:31 +02:00
*
2019-06-21 19:10:02 +02:00
* @codeCoverageIgnore
*/
class GroupCollector implements GroupCollectorInterface
{
/** @var array The standard fields to select. */
private $fields;
/** @var bool Will be set to true if query result contains account information. (see function withAccountInformation). */
2019-06-21 19:10:02 +02:00
private $hasAccountInfo;
/** @var bool Will be true if query result includes bill information. */
private $hasBillInformation;
/** @var bool Will be true if query result contains budget info. */
private $hasBudgetInformation;
/** @var bool Will be true if query result contains category info. */
private $hasCatInformation;
2019-03-25 15:14:09 +01:00
/** @var bool Will be true of the query has the tag info tables joined. */
private $hasJoinedTagTables;
/** @var bool Will be true for attachments */
private $hasJoinedAttTables;
/** @var int The maximum number of results. */
private $limit;
/** @var int The page to return. */
private $page;
/** @var HasMany The query object. */
private $query;
/** @var int Total number of results. */
private $total;
/** @var User The user object. */
private $user;
2019-11-09 08:07:42 +01:00
/** @var array */
private $integerFields;
/**
* Group collector constructor.
*/
public function __construct()
{
if ('testing' === config('app.env')) {
app('log')->warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
2019-06-21 19:10:02 +02:00
$this->hasAccountInfo = false;
$this->hasCatInformation = false;
$this->hasBudgetInformation = false;
$this->hasBillInformation = false;
$this->hasJoinedTagTables = false;
$this->hasJoinedAttTables = false;
$this->integerFields = [
2019-11-09 08:07:42 +01:00
'transaction_group_id',
'user_id',
'transaction_journal_id',
'transaction_type_id',
'order',
'source_transaction_id',
'source_account_id',
'currency_id',
'currency_decimal_places',
'foreign_currency_id',
'foreign_currency_decimal_places',
'destination_transaction_id',
'destination_account_id',
'category_id',
'budget_id',
2019-11-09 08:07:42 +01:00
];
$this->total = 0;
$this->fields = [
# group
'transaction_groups.id as transaction_group_id',
'transaction_groups.user_id as user_id',
'transaction_groups.created_at as created_at',
'transaction_groups.updated_at as updated_at',
'transaction_groups.title as transaction_group_title',
# journal
'transaction_journals.id as transaction_journal_id',
'transaction_journals.transaction_type_id',
'transaction_journals.description',
'transaction_journals.date',
'transaction_journals.order',
2019-08-23 06:42:49 +02:00
# types
'transaction_types.type as transaction_type_type',
# source info (always present)
'source.id as source_transaction_id',
'source.account_id as source_account_id',
'source.reconciled',
# currency info:
'source.amount as amount',
2019-03-25 15:14:09 +01:00
'source.transaction_currency_id as currency_id',
'currency.code as currency_code',
2019-04-18 20:05:40 +02:00
'currency.name as currency_name',
'currency.symbol as currency_symbol',
2019-03-25 15:14:09 +01:00
'currency.decimal_places as currency_decimal_places',
# foreign currency info
'source.foreign_amount as foreign_amount',
'source.foreign_currency_id as foreign_currency_id',
2019-03-25 15:14:09 +01:00
'foreign_currency.code as foreign_currency_code',
2019-06-22 10:25:34 +02:00
'foreign_currency.name as foreign_currency_name',
'foreign_currency.symbol as foreign_currency_symbol',
2019-03-25 15:14:09 +01:00
'foreign_currency.decimal_places as foreign_currency_decimal_places',
# destination account info (always present)
'destination.account_id as destination_account_id',
];
}
2019-03-30 07:09:52 +01:00
/**
2019-08-23 09:41:31 +02:00
* Get transactions with a specific amount.
2019-03-30 07:09:52 +01:00
*
2019-08-23 09:41:31 +02:00
* @param string $amount
*
* @return GroupCollectorInterface
2019-03-30 07:09:52 +01:00
*/
2019-08-23 09:41:31 +02:00
public function amountIs(string $amount): GroupCollectorInterface
2019-03-30 07:09:52 +01:00
{
2019-08-23 09:41:31 +02:00
$this->query->where(
function (EloquentBuilder $q) use ($amount) {
$q->where('source.amount', app('steam')->negative($amount));
}
);
2019-03-30 07:09:52 +01:00
2019-08-23 09:41:31 +02:00
return $this;
}
/**
* Get transactions where the amount is less than.
*
* @param string $amount
*
* @return GroupCollectorInterface
*/
public function amountLess(string $amount): GroupCollectorInterface
{
$this->query->where(
function (EloquentBuilder $q) use ($amount) {
$q->where('destination.amount', '<', app('steam')->positive($amount));
}
);
return $this;
}
/**
* Get transactions where the amount is more than.
*
* @param string $amount
*
* @return GroupCollectorInterface
*/
public function amountMore(string $amount): GroupCollectorInterface
{
$this->query->where(
function (EloquentBuilder $q) use ($amount) {
$q->where('destination.amount', '>', app('steam')->positive($amount));
}
);
return $this;
}
/**
*
*/
public function dumpQuery(): void
{
echo $this->query->toSql();
echo '<pre>';
print_r($this->query->getBindings());
echo '</pre>';
}
/**
* These accounts must not be destination accounts.
*
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
public function excludeDestinationAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->whereNotIn('destination.account_id', $accountIds);
app('log')->debug(sprintf('GroupCollector: excludeDestinationAccounts: %s', implode(', ', $accountIds)));
}
return $this;
}
/**
* These accounts must not be source accounts.
*
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
public function excludeSourceAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->whereNotIn('source.account_id', $accountIds);
app('log')->debug(sprintf('GroupCollector: excludeSourceAccounts: %s', implode(', ', $accountIds)));
}
return $this;
}
/**
* Return the transaction journals without group information. Is useful in some instances.
*
* @return array
*/
public function getExtractedJournals(): array
{
$selection = $this->getGroups();
$return = [];
/** @var array $group */
foreach ($selection as $group) {
$count = count($group['transactions']);
foreach ($group['transactions'] as $journalId => $journal) {
$journal['group_title'] = $group['title'];
$journal['journals_in_group'] = $count;
$return[$journalId] = $journal;
}
}
return $return;
2019-03-30 07:09:52 +01:00
}
/**
* Return the groups.
*
* @return Collection
*/
public function getGroups(): Collection
{
//$start = microtime(true);
/** @var Collection $result */
$result = $this->query->get($this->fields);
//$end = round(microtime(true) - $start, 5);
2019-08-17 21:02:02 +02:00
// log info about query time.
2019-08-18 13:01:47 +02:00
//Log::info(sprintf('Query took Firefly III %s seconds', $end));
//Log::info($this->query->toSql(), $this->query->getBindings());
// now to parse this into an array.
$collection = $this->parseArray($result);
$this->total = $collection->count();
2019-08-17 07:47:39 +02:00
// now filter the array according to the page and the limit (if necessary)
if (null !== $this->limit && null !== $this->page) {
2019-08-23 09:41:31 +02:00
$offset = ($this->page - 1) * $this->limit;
2019-08-23 09:41:31 +02:00
return $collection->slice($offset, $this->limit);
2019-08-17 07:47:39 +02:00
}
return $collection;
}
2019-08-23 09:41:31 +02:00
/**
* Same as getGroups but everything is in a paginator.
*
* @return LengthAwarePaginator
*/
public function getPaginatedGroups(): LengthAwarePaginator
{
$set = $this->getGroups();
return new LengthAwarePaginator($set, $this->total, $this->limit, $this->page);
}
/**
* Return the sum of all journals.
* TODO ignores the currency.
*
* @return string
*/
public function getSum(): string
{
$journals = $this->getExtractedJournals();
$sum = '0';
/** @var array $journal */
foreach ($journals as $journal) {
$amount = (string)$journal['amount'];
$sum = bcadd($sum, $amount);
}
return $sum;
}
/**
* Define which accounts can be part of the source and destination transactions.
*
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
public function setAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->where(
2019-06-21 19:10:02 +02:00
static function (EloquentBuilder $query) use ($accountIds) {
$query->whereIn('source.account_id', $accountIds);
$query->orWhereIn('destination.account_id', $accountIds);
}
);
app('log')->debug(sprintf('GroupCollector: setAccounts: %s', implode(', ', $accountIds)));
}
return $this;
}
2019-08-14 19:06:05 +02:00
/**
2019-08-23 09:41:31 +02:00
* Collect transactions after a specific date.
2019-08-14 19:06:05 +02:00
*
2019-08-23 09:41:31 +02:00
* @param Carbon $date
2019-08-14 19:06:05 +02:00
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function setAfter(Carbon $date): GroupCollectorInterface
2019-08-14 19:06:05 +02:00
{
2019-08-23 09:41:31 +02:00
$afterStr = $date->format('Y-m-d 00:00:00');
$this->query->where('transaction_journals.date', '>=', $afterStr);
Log::debug(sprintf('GroupCollector range is now after %s (inclusive)', $afterStr));
2019-08-14 19:06:05 +02:00
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Collect transactions before a specific date.
2019-08-14 19:06:05 +02:00
*
2019-08-23 09:41:31 +02:00
* @param Carbon $date
2019-08-14 19:06:05 +02:00
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function setBefore(Carbon $date): GroupCollectorInterface
2019-08-14 19:06:05 +02:00
{
2019-08-23 09:41:31 +02:00
$beforeStr = $date->format('Y-m-d 00:00:00');
$this->query->where('transaction_journals.date', '<=', $beforeStr);
Log::debug(sprintf('GroupCollector range is now before %s (inclusive)', $beforeStr));
2019-08-14 19:06:05 +02:00
return $this;
}
2019-03-25 15:14:09 +01:00
/**
* Limit the search to a specific bill.
*
* @param Bill $bill
*
* @return GroupCollectorInterface
*/
public function setBill(Bill $bill): GroupCollectorInterface
{
2019-05-31 13:35:33 +02:00
$this->withBillInformation();
2019-03-25 15:14:09 +01:00
$this->query->where('transaction_journals.bill_id', '=', $bill->id);
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Limit the search to a specific set of bills.
*
* @param Collection $bills
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function setBills(Collection $bills): GroupCollectorInterface
{
2019-08-23 09:41:31 +02:00
$this->withBillInformation();
$this->query->whereIn('transaction_journals.bill_id', $bills->pluck('id')->toArray());
return $this;
}
2019-09-03 22:35:41 +02:00
/**
* Both source AND destination must be in this list of accounts.
*
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
public function setBothAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->where(
static function (EloquentBuilder $query) use ($accountIds) {
$query->whereIn('source.account_id', $accountIds);
$query->whereIn('destination.account_id', $accountIds);
}
);
app('log')->debug(sprintf('GroupCollector: setBothAccounts: %s', implode(', ', $accountIds)));
}
return $this;
}
2019-03-25 15:14:09 +01:00
/**
* Limit the search to a specific budget.
*
* @param Budget $budget
*
* @return GroupCollectorInterface
*/
public function setBudget(Budget $budget): GroupCollectorInterface
{
$this->withBudgetInformation();
$this->query->where('budgets.id', $budget->id);
return $this;
}
/**
* Limit the search to a specific set of budgets.
*
* @param Collection $budgets
*
* @return GroupCollectorInterface
*/
public function setBudgets(Collection $budgets): GroupCollectorInterface
{
2019-05-30 06:23:25 +02:00
if ($budgets->count() > 0) {
$this->withBudgetInformation();
$this->query->whereIn('budgets.id', $budgets->pluck('id')->toArray());
}
2019-03-25 15:14:09 +01:00
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Limit the search to a specific bunch of categories.
2019-03-25 15:14:09 +01:00
*
2019-08-23 09:41:31 +02:00
* @param Collection $categories
2019-03-25 15:14:09 +01:00
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function setCategories(Collection $categories): GroupCollectorInterface
2019-03-25 15:14:09 +01:00
{
2019-08-23 09:41:31 +02:00
if ($categories->count() > 0) {
$this->withCategoryInformation();
$this->query->whereIn('categories.id', $categories->pluck('id')->toArray());
}
2019-03-25 15:14:09 +01:00
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Limit the search to a specific category.
*
* @param Category $category
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function setCategory(Category $category): GroupCollectorInterface
{
2019-08-23 09:41:31 +02:00
$this->withCategoryInformation();
$this->query->where('categories.id', $category->id);
return $this;
}
2019-03-25 15:14:09 +01:00
/**
* Limit results to a specific currency, either foreign or normal one.
*
* @param TransactionCurrency $currency
*
* @return GroupCollectorInterface
*/
public function setCurrency(TransactionCurrency $currency): GroupCollectorInterface
{
$this->query->where(
function (EloquentBuilder $q) use ($currency) {
$q->where('source.transaction_currency_id', $currency->id);
$q->orWhere('source.foreign_currency_id', $currency->id);
}
);
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Define which accounts can be part of the source and destination transactions.
2019-03-25 15:14:09 +01:00
*
2019-08-23 09:41:31 +02:00
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
public function setDestinationAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->whereIn('destination.account_id', $accountIds);
2019-09-03 22:35:41 +02:00
app('log')->debug(sprintf('GroupCollector: setDestinationAccounts: %s', implode(', ', $accountIds)));
2019-08-23 09:41:31 +02:00
}
return $this;
}
/**
* Limit the result to a specific transaction group.
*
* @param TransactionGroup $transactionGroup
*
* @return GroupCollectorInterface
*/
public function setGroup(TransactionGroup $transactionGroup): GroupCollectorInterface
{
$this->query->where('transaction_groups.id', $transactionGroup->id);
return $this;
}
/**
* Limit the result to a set of specific journals.
*
* @param array $journalIds
2019-03-25 15:14:09 +01:00
*
* @return GroupCollectorInterface
*/
public function setJournalIds(array $journalIds): GroupCollectorInterface
{
if (count($journalIds) > 0) {
2019-03-25 15:14:09 +01:00
$this->query->whereIn('transaction_journals.id', $journalIds);
}
return $this;
}
/**
* Limit the number of returned entries.
*
* @param int $limit
*
* @return GroupCollectorInterface
*/
public function setLimit(int $limit): GroupCollectorInterface
{
$this->limit = $limit;
app('log')->debug(sprintf('GroupCollector: The limit is now %d', $limit));
return $this;
}
/**
* Set the page to get.
*
* @param int $page
*
* @return GroupCollectorInterface
*/
public function setPage(int $page): GroupCollectorInterface
{
2019-08-13 18:45:44 +02:00
$page = 0 === $page ? 1 : $page;
$this->page = $page;
2019-08-13 18:45:44 +02:00
app('log')->debug(sprintf('GroupCollector: page is now %d', $page));
return $this;
}
/**
* Set the start and end time of the results to return.
*
* @param Carbon $start
* @param Carbon $end
*
* @return GroupCollectorInterface
*/
public function setRange(Carbon $start, Carbon $end): GroupCollectorInterface
{
if ($end < $start) {
[$start, $end] = [$end, $start];
}
$startStr = $start->format('Y-m-d H:i:s');
$endStr = $end->format('Y-m-d H:i:s');
$this->query->where('transaction_journals.date', '>=', $startStr);
$this->query->where('transaction_journals.date', '<=', $endStr);
app('log')->debug(sprintf('GroupCollector range is now %s - %s (inclusive)', $startStr, $endStr));
return $this;
}
2019-08-23 09:41:31 +02:00
/**
* Search for words in descriptions.
*
* @param array $array
*
* @return GroupCollectorInterface
*/
public function setSearchWords(array $array): GroupCollectorInterface
{
$this->query->where(
function (EloquentBuilder $q) use ($array) {
$q->where(
function (EloquentBuilder $q1) use ($array) {
foreach ($array as $word) {
$keyword = sprintf('%%%s%%', $word);
$q1->where('transaction_journals.description', 'LIKE', $keyword);
}
}
);
$q->orWhere(
function (EloquentBuilder $q2) use ($array) {
foreach ($array as $word) {
$keyword = sprintf('%%%s%%', $word);
$q2->where('transaction_groups.title', 'LIKE', $keyword);
}
}
);
}
);
return $this;
}
/**
* Define which accounts can be part of the source and destination transactions.
*
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
public function setSourceAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->whereIn('source.account_id', $accountIds);
app('log')->debug(sprintf('GroupCollector: setSourceAccounts: %s', implode(', ', $accountIds)));
}
return $this;
}
2019-03-25 15:14:09 +01:00
/**
* Limit results to a specific tag.
*
* @param Tag $tag
*
* @return GroupCollectorInterface
*/
public function setTag(Tag $tag): GroupCollectorInterface
{
2019-07-04 17:31:47 +02:00
$this->withTagInformation();
2019-03-25 15:14:09 +01:00
$this->query->where('tag_transaction_journal.tag_id', $tag->id);
return $this;
}
2019-08-23 09:41:31 +02:00
/**
* Limit results to a specific set of tags.
*
* @param Collection $tags
*
* @return GroupCollectorInterface
*/
public function setTags(Collection $tags): GroupCollectorInterface
{
$this->withTagInformation();
$this->query->whereIn('tag_transaction_journal.tag_id', $tags->pluck('id')->toArray());
return $this;
}
2019-03-30 07:09:52 +01:00
/**
* Limit the search to one specific transaction group.
*
* @param TransactionGroup $transactionGroup
*
* @return GroupCollectorInterface
*/
public function setTransactionGroup(TransactionGroup $transactionGroup): GroupCollectorInterface
{
$this->query->where('transaction_groups.id', $transactionGroup->id);
return $this;
2019-03-30 07:09:52 +01:00
}
/**
* Limit the included transaction types.
*
* @param array $types
*
* @return GroupCollectorInterface
*/
public function setTypes(array $types): GroupCollectorInterface
{
$this->query->whereIn('transaction_types.type', $types);
return $this;
}
/**
* Set the user object and start the query.
*
* @param User $user
*
* @return GroupCollectorInterface
*/
public function setUser(User $user): GroupCollectorInterface
{
$this->user = $user;
$this->startQuery();
return $this;
}
/**
2019-05-31 13:35:33 +02:00
* Automatically include all stuff required to make API calls work.
*
* @return GroupCollectorInterface
*/
2019-05-31 13:35:33 +02:00
public function withAPIInformation(): GroupCollectorInterface
2019-03-25 15:14:09 +01:00
{
// include source + destination account name and type.
$this->withAccountInformation()
// include category ID + name (if any)
->withCategoryInformation()
// include budget ID + name (if any)
->withBudgetInformation()
// include bill ID + name (if any)
->withBillInformation();
return $this;
}
/**
* Will include the source and destination account names and types.
*
* @return GroupCollectorInterface
*/
public function withAccountInformation(): GroupCollectorInterface
{
2019-06-21 19:10:02 +02:00
if (false === $this->hasAccountInfo) {
// join source account table
$this->query->leftJoin('accounts as source_account', 'source_account.id', '=', 'source.account_id');
// join source account type table
$this->query->leftJoin('account_types as source_account_type', 'source_account_type.id', '=', 'source_account.account_type_id');
// add source account fields:
$this->fields[] = 'source_account.name as source_account_name';
2019-03-25 15:14:09 +01:00
$this->fields[] = 'source_account.iban as source_account_iban';
$this->fields[] = 'source_account_type.type as source_account_type';
// same for dest
$this->query->leftJoin('accounts as dest_account', 'dest_account.id', '=', 'destination.account_id');
$this->query->leftJoin('account_types as dest_account_type', 'dest_account_type.id', '=', 'dest_account.account_type_id');
// and add fields:
$this->fields[] = 'dest_account.name as destination_account_name';
2019-03-25 15:14:09 +01:00
$this->fields[] = 'dest_account.iban as destination_account_iban';
$this->fields[] = 'dest_account_type.type as destination_account_type';
2019-06-21 19:10:02 +02:00
$this->hasAccountInfo = true;
}
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Will include bill name + ID, if any.
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function withBillInformation(): GroupCollectorInterface
{
2019-08-23 09:41:31 +02:00
if (false === $this->hasBillInformation) {
// join bill table
$this->query->leftJoin('bills', 'bills.id', '=', 'transaction_journals.bill_id');
// add fields
$this->fields[] = 'bills.id as bill_id';
$this->fields[] = 'bills.name as bill_name';
$this->hasBillInformation = true;
2019-05-30 06:23:25 +02:00
}
return $this;
}
2019-03-25 15:14:09 +01:00
/**
2019-08-23 09:41:31 +02:00
* Will include budget ID + name, if any.
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function withBudgetInformation(): GroupCollectorInterface
{
2019-08-23 09:41:31 +02:00
if (false === $this->hasBudgetInformation) {
// join link table
$this->query->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
// join cat table
$this->query->leftJoin('budgets', 'budget_transaction_journal.budget_id', '=', 'budgets.id');
// add fields
$this->fields[] = 'budgets.id as budget_id';
$this->fields[] = 'budgets.name as budget_name';
$this->hasBudgetInformation = true;
}
2019-05-31 13:35:33 +02:00
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Will include category ID + name, if any.
2019-05-31 13:35:33 +02:00
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function withCategoryInformation(): GroupCollectorInterface
2019-05-31 13:35:33 +02:00
{
2019-08-23 09:41:31 +02:00
if (false === $this->hasCatInformation) {
// join link table
$this->query->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
// join cat table
$this->query->leftJoin('categories', 'category_transaction_journal.category_id', '=', 'categories.id');
// add fields
$this->fields[] = 'categories.id as category_id';
$this->fields[] = 'categories.name as category_name';
$this->hasCatInformation = true;
}
2019-05-31 13:35:33 +02:00
return $this;
}
/**
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function withTagInformation(): GroupCollectorInterface
2019-05-31 13:35:33 +02:00
{
2019-08-23 09:41:31 +02:00
$this->fields[] = 'tags.id as tag_id';
$this->fields[] = 'tags.tag as tag_name';
$this->fields[] = 'tags.date as tag_date';
$this->fields[] = 'tags.description as tag_description';
$this->fields[] = 'tags.latitude as tag_latitude';
$this->fields[] = 'tags.longitude as tag_longitude';
$this->fields[] = 'tags.zoomLevel as tag_zoom_level';
2019-05-31 13:35:33 +02:00
2019-08-23 09:41:31 +02:00
$this->joinTagTables();
2019-05-31 13:35:33 +02:00
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Limit results to a transactions without a budget..
2019-05-31 13:35:33 +02:00
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function withoutBudget(): GroupCollectorInterface
2019-05-31 13:35:33 +02:00
{
2019-08-23 09:41:31 +02:00
$this->withBudgetInformation();
2019-05-31 13:35:33 +02:00
$this->query->where(
2019-08-23 09:41:31 +02:00
function (EloquentBuilder $q) {
$q->whereNull('budget_transaction_journal.budget_id');
2019-05-31 13:35:33 +02:00
}
);
return $this;
}
/**
2019-08-23 09:41:31 +02:00
* Limit results to a transactions without a category.
2019-05-31 13:35:33 +02:00
*
* @return GroupCollectorInterface
*/
2019-08-23 09:41:31 +02:00
public function withoutCategory(): GroupCollectorInterface
2019-05-31 13:35:33 +02:00
{
2019-08-23 09:41:31 +02:00
$this->withCategoryInformation();
2019-05-31 13:35:33 +02:00
$this->query->where(
2019-08-23 09:41:31 +02:00
function (EloquentBuilder $q) {
$q->whereNull('category_transaction_journal.category_id');
2019-05-31 13:35:33 +02:00
}
);
return $this;
}
2019-11-09 08:07:42 +01:00
/**
* Convert a selected set of fields to arrays.
*
* @param array $array
*
* @return array
*/
private function convertToInteger(array $array): array
{
foreach ($this->integerFields as $field) {
$array[$field] = isset($array[$field]) ? (int)$array[$field] : null;
}
return $array;
}
2019-05-31 13:35:33 +02:00
/**
2019-08-23 09:41:31 +02:00
* Join table to get tag information.
2019-05-31 13:35:33 +02:00
*/
2019-08-23 09:41:31 +02:00
private function joinTagTables(): void
2019-05-31 13:35:33 +02:00
{
2019-08-23 09:41:31 +02:00
if (false === $this->hasJoinedTagTables) {
// join some extra tables:
$this->hasJoinedTagTables = true;
$this->query->leftJoin('tag_transaction_journal', 'tag_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
$this->query->leftJoin('tags', 'tag_transaction_journal.tag_id', '=', 'tags.id');
}
2019-05-31 13:35:33 +02:00
}
/**
* Join table to get attachment information.
*/
private function joinAttachmentTables(): void
{
if (false === $this->hasJoinedAttTables) {
// join some extra tables:
$this->hasJoinedAttTables = true;
$this->query->leftJoin('attachments', 'attachments.attachable_id', '=', 'transaction_journals.id')
2020-02-23 07:38:23 +01:00
->where(
static function (EloquentBuilder $q1) {
$q1->where('attachments.attachable_type', TransactionJournal::class);
$q1->orWhereNull('attachments.attachable_type');
}
);
}
}
2019-05-31 13:35:33 +02:00
/**
2019-09-03 22:35:41 +02:00
* @param array $existingJournal
2019-09-01 14:42:27 +02:00
* @param TransactionJournal $newJournal
2019-05-31 13:35:33 +02:00
*
2019-08-23 09:41:31 +02:00
* @return array
2019-05-31 13:35:33 +02:00
*/
2019-09-01 14:42:27 +02:00
private function mergeTags(array $existingJournal, TransactionJournal $newJournal): array
2019-05-31 13:35:33 +02:00
{
2019-09-01 14:42:27 +02:00
$newArray = $newJournal->toArray();
2019-08-23 09:41:31 +02:00
if (isset($newArray['tag_id'])) { // assume the other fields are present as well.
2019-09-01 14:42:27 +02:00
$tagId = (int)$newJournal['tag_id'];
2019-05-31 13:35:33 +02:00
2019-08-23 09:41:31 +02:00
$tagDate = null;
try {
$tagDate = Carbon::parse($newArray['tag_date']);
} catch (InvalidDateException $e) {
Log::debug(sprintf('Could not parse date: %s', $e->getMessage()));
}
2019-07-04 17:31:47 +02:00
2019-08-23 09:41:31 +02:00
$existingJournal['tags'][$tagId] = [
'id' => (int)$newArray['tag_id'],
'name' => $newArray['tag_name'],
'date' => $tagDate,
'description' => $newArray['tag_description'],
];
}
2019-07-04 17:31:47 +02:00
2019-08-23 09:41:31 +02:00
return $existingJournal;
2019-07-04 17:31:47 +02:00
}
/**
* @param array $existingJournal
* @param TransactionJournal $newJournal
*
* @return array
*/
private function mergeAttachments(array $existingJournal, TransactionJournal $newJournal): array
{
$newArray = $newJournal->toArray();
if (isset($newArray['attachment_id'])) {
$attachmentId = (int)$newJournal['tag_id'];
$existingJournal['attachments'][$attachmentId] = [
'id' => $attachmentId,
];
}
return $existingJournal;
}
2019-05-31 13:35:33 +02:00
/**
* @param Collection $collection
*
* @return Collection
*/
private function parseArray(Collection $collection): Collection
{
$groups = [];
2019-08-23 09:41:31 +02:00
/** @var TransactionJournal $augumentedJournal */
foreach ($collection as $augumentedJournal) {
$groupId = $augumentedJournal->transaction_group_id;
2019-08-17 07:47:39 +02:00
2019-05-31 13:35:33 +02:00
if (!isset($groups[$groupId])) {
// make new array
$parsedGroup = $this->parseAugmentedJournal($augumentedJournal);
2019-05-31 13:35:33 +02:00
$groupArray = [
2019-11-09 08:07:42 +01:00
'id' => (int)$augumentedJournal->transaction_group_id,
'user_id' => (int)$augumentedJournal->user_id,
2019-11-10 07:26:10 +01:00
'title' => $augumentedJournal->transaction_group_title,
2019-07-04 17:31:47 +02:00
'transaction_type' => $parsedGroup['transaction_type_type'],
'count' => 1,
'sums' => [],
'transactions' => [],
2019-05-31 13:35:33 +02:00
];
2019-08-23 09:41:31 +02:00
$journalId = (int)$augumentedJournal->transaction_journal_id;
2019-07-04 17:31:47 +02:00
$groupArray['transactions'][$journalId] = $parsedGroup;
2019-05-31 13:35:33 +02:00
$groups[$groupId] = $groupArray;
continue;
}
// or parse the rest.
2019-08-23 09:41:31 +02:00
$journalId = (int)$augumentedJournal->transaction_journal_id;
2019-05-31 13:35:33 +02:00
$groups[$groupId]['count']++;
if (isset($groups[$groupId]['transactions'][$journalId])) {
// append data to existing group + journal (for multiple tags or multiple attachments)
2019-08-23 09:41:31 +02:00
$groups[$groupId]['transactions'][$journalId] = $this->mergeTags($groups[$groupId]['transactions'][$journalId], $augumentedJournal);
$groups[$groupId]['transactions'][$journalId] = $this->mergeAttachments($groups[$groupId]['transactions'][$journalId], $augumentedJournal);
2019-05-31 13:35:33 +02:00
}
if (!isset($groups[$groupId]['transactions'][$journalId])) {
// create second, third, fourth split:
$groups[$groupId]['transactions'][$journalId] = $this->parseAugmentedJournal($augumentedJournal);
2019-05-31 13:35:33 +02:00
}
2019-08-17 07:47:39 +02:00
2019-05-31 13:35:33 +02:00
}
2019-08-17 07:47:39 +02:00
2019-05-31 13:35:33 +02:00
$groups = $this->parseSums($groups);
return new Collection($groups);
}
/**
2019-08-23 09:41:31 +02:00
* @param TransactionJournal $augumentedJournal
2019-05-31 13:35:33 +02:00
*
* @return array
*/
private function parseAugmentedJournal(TransactionJournal $augumentedJournal): array
2019-05-31 13:35:33 +02:00
{
$result = $augumentedJournal->toArray();
$result['tags'] = [];
$result['attachments'] = [];
2019-06-21 19:10:02 +02:00
try {
$result['date'] = new Carbon($result['date']);
$result['created_at'] = new Carbon($result['created_at']);
$result['updated_at'] = new Carbon($result['updated_at']);
} catch (Exception $e) {
Log::error($e->getMessage());
}
2019-11-09 08:07:42 +01:00
// convert values to integers:
$result = $this->convertToInteger($result);
2019-05-31 13:35:33 +02:00
$result['reconciled'] = 1 === (int)$result['reconciled'];
2019-08-23 09:41:31 +02:00
if (isset($augumentedJournal['tag_id'])) { // assume the other fields are present as well.
$tagId = (int)$augumentedJournal['tag_id'];
2019-05-31 13:35:33 +02:00
$tagDate = null;
try {
2019-08-23 09:41:31 +02:00
$tagDate = Carbon::parse($augumentedJournal['tag_date']);
2019-05-31 13:35:33 +02:00
} catch (InvalidDateException $e) {
Log::debug(sprintf('Could not parse date: %s', $e->getMessage()));
}
$result['tags'][$tagId] = [
'id' => (int)$result['tag_id'],
'name' => $result['tag_name'],
'date' => $tagDate,
'description' => $result['tag_description'],
];
}
// also merge attachments:
if (isset($augumentedJournal['attachment_id'])) {
$attachmentId = (int)$augumentedJournal['attachment_id'];
$result['attachments'][$attachmentId] = [
'id' => $attachmentId,
];
}
2019-05-31 13:35:33 +02:00
return $result;
}
/**
* @param array $groups
*
* @return array
*/
private function parseSums(array $groups): array
{
/**
2019-08-23 09:41:31 +02:00
* @var int $groudId
2019-05-31 13:35:33 +02:00
* @var array $group
*/
foreach ($groups as $groudId => $group) {
/** @var array $transaction */
foreach ($group['transactions'] as $transaction) {
$currencyId = (int)$transaction['currency_id'];
// set default:
if (!isset($groups[$groudId]['sums'][$currencyId])) {
$groups[$groudId]['sums'][$currencyId]['currency_id'] = $currencyId;
$groups[$groudId]['sums'][$currencyId]['currency_code'] = $transaction['currency_code'];
$groups[$groudId]['sums'][$currencyId]['currency_symbol'] = $transaction['currency_symbol'];
$groups[$groudId]['sums'][$currencyId]['currency_decimal_places'] = $transaction['currency_decimal_places'];
$groups[$groudId]['sums'][$currencyId]['amount'] = '0';
}
2019-08-09 18:06:43 +02:00
$groups[$groudId]['sums'][$currencyId]['amount'] = bcadd($groups[$groudId]['sums'][$currencyId]['amount'], $transaction['amount'] ?? '0');
2019-05-31 13:35:33 +02:00
if (null !== $transaction['foreign_amount'] && null !== $transaction['foreign_currency_id']) {
$currencyId = (int)$transaction['foreign_currency_id'];
// set default:
if (!isset($groups[$groudId]['sums'][$currencyId])) {
$groups[$groudId]['sums'][$currencyId]['currency_id'] = $currencyId;
$groups[$groudId]['sums'][$currencyId]['currency_code'] = $transaction['foreign_currency_code'];
$groups[$groudId]['sums'][$currencyId]['currency_symbol'] = $transaction['foreign_currency_symbol'];
$groups[$groudId]['sums'][$currencyId]['currency_decimal_places'] = $transaction['foreign_currency_decimal_places'];
$groups[$groudId]['sums'][$currencyId]['amount'] = '0';
}
2019-08-23 09:41:31 +02:00
$groups[$groudId]['sums'][$currencyId]['amount'] = bcadd(
$groups[$groudId]['sums'][$currencyId]['amount'], $transaction['foreign_amount'] ?? '0'
);
2019-05-31 13:35:33 +02:00
}
}
}
return $groups;
}
/**
* Build the query.
*/
private function startQuery(): void
{
app('log')->debug('GroupCollector::startQuery');
$this->query = $this->user
2019-08-23 09:41:31 +02:00
//->transactionGroups()
//->leftJoin('transaction_journals', 'transaction_journals.transaction_group_id', 'transaction_groups.id')
->transactionJournals()
->leftJoin('transaction_groups', 'transaction_journals.transaction_group_id', 'transaction_groups.id')
2019-05-31 13:35:33 +02:00
// join source transaction.
->leftJoin(
'transactions as source', function (JoinClause $join) {
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')
->where('source.amount', '<', 0);
}
)
// join destination transaction
->leftJoin(
'transactions as destination', function (JoinClause $join) {
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')
->where('destination.amount', '>', 0);
}
)
// left join transaction type.
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
->leftJoin('transaction_currencies as currency', 'currency.id', '=', 'source.transaction_currency_id')
->leftJoin('transaction_currencies as foreign_currency', 'foreign_currency.id', '=', 'source.foreign_currency_id')
->whereNull('transaction_groups.deleted_at')
->whereNull('transaction_journals.deleted_at')
->whereNull('source.deleted_at')
->whereNull('destination.deleted_at')
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->orderBy('transaction_journals.description', 'DESC')
->orderBy('source.amount', 'DESC');
}
2019-09-06 17:18:55 +02:00
/**
* Either account can be set, but NOT both. This effectively excludes internal transfers.
*
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
public function setXorAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->where(
static function (EloquentBuilder $q1) use ($accountIds) {
// sourceAccount is in the set, and destination is NOT.
$q1->where(
static function (EloquentBuilder $q2) use ($accountIds) {
$q2->whereIn('source.account_id', $accountIds);
$q2->whereNotIn('destination.account_id', $accountIds);
}
);
// destination is in the set, and source is NOT
$q1->orWhere(
static function (EloquentBuilder $q3) use ($accountIds) {
$q3->whereNotIn('source.account_id', $accountIds);
$q3->whereIn('destination.account_id', $accountIds);
}
);
}
);
app('log')->debug(sprintf('GroupCollector: setXorAccounts: %s', implode(', ', $accountIds)));
}
return $this;
}
2019-09-14 06:22:02 +02:00
/**
* Collect transactions created on a specific date.
*
* @param Carbon $date
*
* @return GroupCollectorInterface
*/
public function setCreatedAt(Carbon $date): GroupCollectorInterface
{
$after = $date->format('Y-m-d 00:00:00');
$before = $date->format('Y-m-d 23:59:59');
$this->query->where('transaction_journals.created_at', '>=', $after);
$this->query->where('transaction_journals.created_at', '<=', $before);
Log::debug(sprintf('GroupCollector created_at is now after %s (inclusive)', $after));
return $this;
}
/**
* Collect transactions updated on a specific date.
*
* @param Carbon $date
*
* @return GroupCollectorInterface
*/
public function setUpdatedAt(Carbon $date): GroupCollectorInterface
{
$after = $date->format('Y-m-d 00:00:00');
$before = $date->format('Y-m-d 23:59:59');
$this->query->where('transaction_journals.updated_at', '>=', $after);
$this->query->where('transaction_journals.updated_at', '<=', $before);
Log::debug(sprintf('GroupCollector created_at is now after %s (inclusive)', $after));
return $this;
}
/**
* @inheritDoc
*/
public function withAttachmentInformation(): GroupCollectorInterface
{
$this->fields[] = 'attachments.id as attachment_id';
$this->joinAttachmentTables();
return $this;
}
2019-08-17 12:09:03 +02:00
}