firefly-iii/app/Helpers/Collector/Extensions/AccountCollection.php

270 lines
9.4 KiB
PHP
Raw Normal View History

2020-03-21 02:54:38 -05:00
<?php
2020-06-30 12:05:35 -05:00
2020-03-21 02:54:38 -05:00
/**
* AccountCollection.php
* Copyright (c) 2020 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/>.
*/
2020-06-30 12:05:35 -05:00
declare(strict_types=1);
2020-03-21 02:54:38 -05:00
namespace FireflyIII\Helpers\Collector\Extensions;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Support\Collection;
/**
* Trait AccountCollection
*/
trait AccountCollection
{
/**
2022-09-25 08:31:58 -05:00
* These accounts must not be included.
2020-03-21 02:54:38 -05:00
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
2020-03-21 02:54:38 -05:00
*
* @return GroupCollectorInterface
*/
2022-09-25 08:31:58 -05:00
public function excludeAccounts(Collection $accounts): GroupCollectorInterface
2020-03-21 02:54:38 -05:00
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
2022-09-25 08:31:58 -05:00
$this->query->whereNotIn('source.account_id', $accountIds);
2020-03-21 02:54:38 -05:00
$this->query->whereNotIn('destination.account_id', $accountIds);
2022-09-25 08:31:58 -05:00
app('log')->debug(sprintf('GroupCollector: excludeAccounts: %s', implode(', ', $accountIds)));
2020-03-21 02:54:38 -05:00
}
return $this;
}
2021-03-28 04:46:23 -05:00
2020-03-21 02:54:38 -05:00
/**
2022-09-25 08:31:58 -05:00
* These accounts must not be destination accounts.
2020-03-21 02:54:38 -05:00
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
2020-03-21 02:54:38 -05:00
*
* @return GroupCollectorInterface
*/
2022-09-25 08:31:58 -05:00
public function excludeDestinationAccounts(Collection $accounts): GroupCollectorInterface
2020-03-21 02:54:38 -05:00
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
2022-09-25 08:31:58 -05:00
$this->query->whereNotIn('destination.account_id', $accountIds);
2020-03-21 02:54:38 -05:00
2022-09-25 08:31:58 -05:00
app('log')->debug(sprintf('GroupCollector: excludeDestinationAccounts: %s', implode(', ', $accountIds)));
2020-03-21 02:54:38 -05:00
}
return $this;
}
2021-03-28 04:46:23 -05:00
/**
2022-09-25 08:31:58 -05:00
* These accounts must not be source accounts.
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
2022-09-25 08:31:58 -05:00
public function excludeSourceAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->whereNotIn('source.account_id', $accountIds);
2022-09-25 08:31:58 -05:00
app('log')->debug(sprintf('GroupCollector: excludeSourceAccounts: %s', implode(', ', $accountIds)));
}
return $this;
}
2020-03-21 02:54:38 -05:00
/**
* Define which accounts can be part of the source and destination transactions.
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
2020-03-21 02:54:38 -05:00
*
* @return GroupCollectorInterface
*/
public function setAccounts(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->orWhereIn('destination.account_id', $accountIds);
}
);
2021-02-22 11:40:46 -06:00
//app('log')->debug(sprintf('GroupCollector: setAccounts: %s', implode(', ', $accountIds)));
}
return $this;
}
/**
2022-09-25 08:31:58 -05:00
* Both source AND destination must be in this list of accounts.
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
*
* @return GroupCollectorInterface
*/
2022-09-25 08:31:58 -05:00
public function setBothAccounts(Collection $accounts): GroupCollectorInterface
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
$this->query->where(
static function (EloquentBuilder $query) use ($accountIds) {
2022-09-25 08:31:58 -05:00
$query->whereIn('source.account_id', $accountIds);
$query->whereIn('destination.account_id', $accountIds);
}
);
2022-09-25 08:31:58 -05:00
app('log')->debug(sprintf('GroupCollector: setBothAccounts: %s', implode(', ', $accountIds)));
2020-03-21 02:54:38 -05:00
}
return $this;
}
/**
2022-09-25 08:31:58 -05:00
* Define which accounts can be part of the source and destination transactions.
2020-03-21 02:54:38 -05:00
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
2020-03-21 02:54:38 -05:00
*
* @return GroupCollectorInterface
*/
2022-09-25 08:31:58 -05:00
public function setDestinationAccounts(Collection $accounts): GroupCollectorInterface
2020-03-21 02:54:38 -05:00
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
2022-09-25 08:31:58 -05:00
$this->query->whereIn('destination.account_id', $accountIds);
app('log')->debug(sprintf('GroupCollector: setDestinationAccounts: %s', implode(', ', $accountIds)));
2020-03-21 02:54:38 -05:00
}
return $this;
}
2021-03-28 04:46:23 -05:00
2020-03-21 02:54:38 -05:00
/**
2022-09-25 08:31:58 -05:00
* Define which accounts can NOT be part of the source and destination transactions.
2020-03-21 02:54:38 -05:00
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
2020-03-21 02:54:38 -05:00
*
* @return GroupCollectorInterface
*/
2022-09-25 08:31:58 -05:00
public function setNotAccounts(Collection $accounts): GroupCollectorInterface
2020-03-21 02:54:38 -05:00
{
if ($accounts->count() > 0) {
$accountIds = $accounts->pluck('id')->toArray();
2022-09-25 08:31:58 -05:00
$this->query->where(
static function (EloquentBuilder $query) use ($accountIds) {
$query->whereNotIn('source.account_id', $accountIds);
$query->whereNotIn('destination.account_id', $accountIds);
}
);
//app('log')->debug(sprintf('GroupCollector: setAccounts: %s', implode(', ', $accountIds)));
2020-03-21 02:54:38 -05:00
}
return $this;
}
2021-03-28 04:46:23 -05:00
2020-03-21 02:54:38 -05:00
/**
* Define which accounts can be part of the source and destination transactions.
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
2020-03-21 02:54:38 -05:00
*
* @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;
}
2021-03-28 04:46:23 -05:00
2020-03-21 02:54:38 -05:00
/**
* Either account can be set, but NOT both. This effectively excludes internal transfers.
*
2022-12-29 12:41:57 -06:00
* @param Collection $accounts
2020-03-21 02:54:38 -05:00
*
* @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;
}
2021-03-28 04:46:23 -05:00
2020-03-21 02:54:38 -05:00
/**
* Will include the source and destination account names and types.
*
* @return GroupCollectorInterface
*/
public function withAccountInformation(): GroupCollectorInterface
{
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';
$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:
2022-03-21 00:31:38 -05:00
$this->fields[] = 'dest_account.name as destination_account_name';
$this->fields[] = 'dest_account.iban as destination_account_iban';
$this->fields[] = 'dest_account_type.type as destination_account_type';
2020-03-21 02:54:38 -05:00
$this->hasAccountInfo = true;
}
return $this;
}
}