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

662 lines
21 KiB
PHP
Raw Permalink Normal View History

2020-03-21 03:01:14 -05:00
<?php
2020-06-30 12:05:35 -05:00
2020-03-21 03:01:14 -05:00
/**
* TimeCollection.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 03:01:14 -05:00
namespace FireflyIII\Helpers\Collector\Extensions;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
/**
* Trait TimeCollection
*/
trait TimeCollection
{
2022-03-21 00:31:38 -05:00
public function dayAfter(string $day): GroupCollectorInterface
{
$this->query->whereDay('transaction_journals.date', '>=', $day);
2023-12-20 12:35:52 -06:00
2022-03-21 00:31:38 -05:00
return $this;
}
public function dayBefore(string $day): GroupCollectorInterface
{
$this->query->whereDay('transaction_journals.date', '<=', $day);
2023-12-20 12:35:52 -06:00
2022-03-21 00:31:38 -05:00
return $this;
}
public function dayIs(string $day): GroupCollectorInterface
{
$this->query->whereDay('transaction_journals.date', '=', $day);
2023-12-20 12:35:52 -06:00
2022-03-21 00:31:38 -05:00
return $this;
}
public function dayIsNot(string $day): GroupCollectorInterface
{
$this->query->whereDay('transaction_journals.date', '!=', $day);
2023-12-20 12:35:52 -06:00
return $this;
}
2022-09-25 08:31:58 -05:00
public function excludeMetaDateRange(Carbon $start, Carbon $end, string $field): GroupCollectorInterface
{
if ($end < $start) {
[$start, $end] = [$end, $start];
}
$end = clone $end; // this is so weird, but it works if $end and $start secretly point to the same object.
2022-09-25 08:31:58 -05:00
$end->endOfDay();
$start->startOfDay();
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $start, $end): bool {
2022-09-25 08:31:58 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon) {
return $transaction[$field]->lt($start) || $transaction[$field]->gt($end);
}
}
return false;
};
$this->postFilters[] = $filter;
return $this;
}
2023-06-21 05:34:58 -05:00
public function withMetaDate(string $field): GroupCollectorInterface
{
$this->joinMetaDataTables();
$this->query->where('journal_meta.name', '=', $field);
$this->query->whereNotNull('journal_meta.data');
return $this;
}
2022-09-25 08:31:58 -05:00
public function excludeObjectRange(Carbon $start, Carbon $end, string $field): GroupCollectorInterface
{
$after = $start->format('Y-m-d 00:00:00');
$before = $end->format('Y-m-d 23:59:59');
$this->query->where(sprintf('transaction_journals.%s', $field), '<', $after);
$this->query->orWhere(sprintf('transaction_journals.%s', $field), '>', $before);
return $this;
}
public function excludeRange(Carbon $start, Carbon $end): GroupCollectorInterface
{
if ($end < $start) {
[$start, $end] = [$end, $start];
}
$startStr = $start->format('Y-m-d 00:00:00');
$endStr = $end->format('Y-m-d 23:59:59');
$this->query->where('transaction_journals.date', '<', $startStr);
$this->query->orWhere('transaction_journals.date', '>', $endStr);
return $this;
}
2022-03-27 09:03:50 -05:00
public function metaDayAfter(string $day, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $day): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return $transaction[$field]->day >= (int)$day;
2022-03-27 09:03:50 -05:00
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function metaDayBefore(string $day, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $day): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return $transaction[$field]->day <= (int)$day;
2022-03-27 09:03:50 -05:00
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function metaDayIs(string $day, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $day): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return (int)$day === $transaction[$field]->day;
2022-03-27 09:03:50 -05:00
}
}
return false;
};
$this->postFilters[] = $filter;
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function metaDayIsNot(string $day, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $day): bool {
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return (int)$day !== $transaction[$field]->day;
}
}
return false;
};
$this->postFilters[] = $filter;
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-27 09:03:50 -05:00
public function metaMonthAfter(string $month, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $month): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return $transaction[$field]->month >= (int)$month;
2022-03-27 09:03:50 -05:00
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function metaMonthBefore(string $month, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $month): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return $transaction[$field]->month <= (int)$month;
2022-03-27 09:03:50 -05:00
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function metaMonthIs(string $month, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $month): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return (int)$month === $transaction[$field]->month;
2022-03-27 09:03:50 -05:00
}
}
return false;
};
$this->postFilters[] = $filter;
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function metaMonthIsNot(string $month, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $month): bool {
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return (int)$month !== $transaction[$field]->month;
}
}
return false;
};
$this->postFilters[] = $filter;
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-27 09:03:50 -05:00
public function metaYearAfter(string $year, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $year): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return $transaction[$field]->year >= (int)$year;
2022-03-27 09:03:50 -05:00
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function metaYearBefore(string $year, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $year): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return $transaction[$field]->year <= (int)$year;
2022-03-27 09:03:50 -05:00
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function metaYearIs(string $year, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $year): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return $year === (string)$transaction[$field]->year;
2022-03-27 09:03:50 -05:00
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function metaYearIsNot(string $year, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $year): bool {
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-12-29 12:41:57 -06:00
return $year !== (string)$transaction[$field]->year;
}
}
2023-12-20 12:35:52 -06:00
return true;
};
$this->postFilters[] = $filter;
return $this;
}
2022-03-21 00:31:38 -05:00
public function monthAfter(string $month): GroupCollectorInterface
{
$this->query->whereMonth('transaction_journals.date', '>=', $month);
2023-12-20 12:35:52 -06:00
2022-03-21 00:31:38 -05:00
return $this;
}
public function monthBefore(string $month): GroupCollectorInterface
{
$this->query->whereMonth('transaction_journals.date', '<=', $month);
2023-12-20 12:35:52 -06:00
2022-03-21 00:31:38 -05:00
return $this;
}
public function monthIs(string $month): GroupCollectorInterface
{
$this->query->whereMonth('transaction_journals.date', '=', $month);
2023-12-20 12:35:52 -06:00
2022-03-21 00:31:38 -05:00
return $this;
2022-03-27 09:03:50 -05:00
}
public function monthIsNot(string $month): GroupCollectorInterface
{
$this->query->whereMonth('transaction_journals.date', '!=', $month);
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-27 09:03:50 -05:00
public function objectDayAfter(string $day, string $field): GroupCollectorInterface
{
$this->query->whereDay(sprintf('transaction_journals.%s', $field), '>=', $day);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
2022-03-21 00:31:38 -05:00
2022-03-27 09:03:50 -05:00
public function objectDayBefore(string $day, string $field): GroupCollectorInterface
{
$this->query->whereDay(sprintf('transaction_journals.%s', $field), '<=', $day);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function objectDayIs(string $day, string $field): GroupCollectorInterface
{
$this->query->whereDay(sprintf('transaction_journals.%s', $field), '=', $day);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function objectDayIsNot(string $day, string $field): GroupCollectorInterface
{
$this->query->whereDay(sprintf('transaction_journals.%s', $field), '!=', $day);
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-27 09:03:50 -05:00
public function objectMonthAfter(string $month, string $field): GroupCollectorInterface
{
$this->query->whereMonth(sprintf('transaction_journals.%s', $field), '>=', $month);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function objectMonthBefore(string $month, string $field): GroupCollectorInterface
{
$this->query->whereMonth(sprintf('transaction_journals.%s', $field), '<=', $month);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function objectMonthIs(string $month, string $field): GroupCollectorInterface
{
$this->query->whereMonth(sprintf('transaction_journals.%s', $field), '=', $month);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function objectMonthIsNot(string $month, string $field): GroupCollectorInterface
{
$this->query->whereMonth(sprintf('transaction_journals.%s', $field), '!=', $month);
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-27 09:03:50 -05:00
public function objectYearAfter(string $year, string $field): GroupCollectorInterface
{
$this->query->whereYear(sprintf('transaction_journals.%s', $field), '>=', $year);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function objectYearBefore(string $year, string $field): GroupCollectorInterface
{
$this->query->whereYear(sprintf('transaction_journals.%s', $field), '<=', $year);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
}
public function objectYearIs(string $year, string $field): GroupCollectorInterface
{
$this->query->whereYear(sprintf('transaction_journals.%s', $field), '=', $year);
2023-12-20 12:35:52 -06:00
2022-03-27 09:03:50 -05:00
return $this;
2022-03-21 00:31:38 -05:00
}
public function objectYearIsNot(string $year, string $field): GroupCollectorInterface
{
$this->query->whereYear(sprintf('transaction_journals.%s', $field), '!=', $year);
2023-12-20 12:35:52 -06:00
return $this;
}
2020-03-21 03:01:14 -05:00
/**
* Collect transactions after a specific date.
*/
public function setAfter(Carbon $date): GroupCollectorInterface
{
$afterStr = $date->format('Y-m-d 00:00:00');
$this->query->where('transaction_journals.date', '>=', $afterStr);
return $this;
}
/**
* Collect transactions before a specific date.
*/
public function setBefore(Carbon $date): GroupCollectorInterface
{
2024-01-05 11:09:19 -06:00
$beforeStr = $date->format('Y-m-d 23:59:59');
2020-03-21 03:01:14 -05:00
$this->query->where('transaction_journals.date', '<=', $beforeStr);
return $this;
}
/**
* Collect transactions created on a specific date.
*/
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);
return $this;
}
/**
* Set the end time of the results to return.
*/
public function setEnd(Carbon $end): GroupCollectorInterface
{
// always got to end of day / start of day for ranges.
$endStr = $end->format('Y-m-d 23:59:59');
$this->query->where('transaction_journals.date', '<=', $endStr);
return $this;
}
2022-03-27 09:03:50 -05:00
public function setMetaAfter(Carbon $date, string $field): GroupCollectorInterface
2020-03-21 03:01:14 -05:00
{
2022-03-27 09:03:50 -05:00
$this->withMetaDate($field);
$date->startOfDay();
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $date): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
return $transaction[$field]->gte($date);
}
}
2020-03-21 03:01:14 -05:00
2022-03-27 09:03:50 -05:00
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function setMetaBefore(Carbon $date, string $field): GroupCollectorInterface
{
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $date): bool {
2022-03-27 09:03:50 -05:00
foreach ($object['transactions'] as $transaction) {
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
return $transaction[$field]->lte($date);
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
public function setMetaDateRange(Carbon $start, Carbon $end, string $field): GroupCollectorInterface
{
if ($end < $start) {
[$start, $end] = [$end, $start];
}
$end = clone $end; // this is so weird, but it works if $end and $start secretly point to the same object.
$end->endOfDay();
$start->startOfDay();
$this->withMetaDate($field);
2023-11-28 11:57:10 -06:00
$filter = static function (array $object) use ($field, $start, $end): bool {
foreach ($object['transactions'] as $transaction) {
2022-03-27 09:03:50 -05:00
if (array_key_exists($field, $transaction) && $transaction[$field] instanceof Carbon
) {
2022-03-27 09:03:50 -05:00
return $transaction[$field]->gte($start) && $transaction[$field]->lte($end);
}
}
2022-03-27 09:03:50 -05:00
return false;
};
$this->postFilters[] = $filter;
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-27 09:03:50 -05:00
public function setObjectAfter(Carbon $date, string $field): GroupCollectorInterface
{
2022-03-27 09:03:50 -05:00
$afterStr = $date->format('Y-m-d 00:00:00');
$this->query->where(sprintf('transaction_journals.%s', $field), '>=', $afterStr);
return $this;
}
public function setObjectBefore(Carbon $date, string $field): GroupCollectorInterface
{
$afterStr = $date->format('Y-m-d 00:00:00');
$this->query->where(sprintf('transaction_journals.%s', $field), '<=', $afterStr);
2023-12-20 12:35:52 -06:00
return $this;
2022-03-27 09:03:50 -05:00
}
public function setObjectRange(Carbon $start, Carbon $end, string $field): GroupCollectorInterface
{
$after = $start->format('Y-m-d 00:00:00');
$before = $end->format('Y-m-d 23:59:59');
$this->query->where(sprintf('transaction_journals.%s', $field), '>=', $after);
$this->query->where(sprintf('transaction_journals.%s', $field), '<=', $before);
return $this;
}
/**
* Set the start and end time of the results to return.
*
* Can either or both be NULL
2022-03-27 09:03:50 -05:00
*/
public function setRange(?Carbon $start, ?Carbon $end): GroupCollectorInterface
2022-03-27 09:03:50 -05:00
{
if (null !== $start && null !== $end && $end < $start) {
2022-03-27 09:03:50 -05:00
[$start, $end] = [$end, $start];
}
// always got to end of day / start of day for ranges.
$startStr = $start?->format('Y-m-d 00:00:00');
$endStr = $end?->format('Y-m-d 23:59:59');
2022-03-27 09:03:50 -05:00
if (null !== $start) {
$this->query->where('transaction_journals.date', '>=', $startStr);
}
if (null !== $end) {
$this->query->where('transaction_journals.date', '<=', $endStr);
}
return $this;
}
/**
* Set the start time of the results to return.
*/
public function setStart(Carbon $start): GroupCollectorInterface
{
$startStr = $start->format('Y-m-d 00:00:00');
$this->query->where('transaction_journals.date', '>=', $startStr);
return $this;
}
2020-03-21 03:01:14 -05:00
/**
* Collect transactions updated on a specific date.
*/
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);
return $this;
}
public function yearAfter(string $year): GroupCollectorInterface
{
$this->query->whereYear('transaction_journals.date', '>=', $year);
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-21 00:31:38 -05:00
public function yearBefore(string $year): GroupCollectorInterface
{
2022-03-21 00:31:38 -05:00
$this->query->whereYear('transaction_journals.date', '<=', $year);
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-21 00:31:38 -05:00
public function yearIs(string $year): GroupCollectorInterface
{
2022-03-21 00:31:38 -05:00
$this->query->whereYear('transaction_journals.date', '=', $year);
2023-12-20 12:35:52 -06:00
return $this;
}
2022-03-27 09:03:50 -05:00
public function yearIsNot(string $year): GroupCollectorInterface
{
$this->query->whereYear('transaction_journals.date', '!=', $year);
2023-12-20 12:35:52 -06:00
return $this;
}
2020-05-03 12:38:03 -05:00
}