Make it possible to search by interest date.

This commit is contained in:
James Cole
2022-03-27 08:48:30 +02:00
parent ff7f4e5bba
commit 679e72e5e2
8 changed files with 240 additions and 72 deletions

View File

@@ -142,6 +142,49 @@ trait TimeCollection
return $this;
}
/**
* @param Carbon $start
* @param Carbon $end
* @param string $field
* @return GroupCollectorInterface
*/
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);
$filter = function (int $index, array $object) use ($field, $start, $end): bool {
foreach ($object['transactions'] as $transaction) {
if (array_key_exists('interest_date', $transaction) && $transaction['interest_date'] instanceof Carbon
) {
return $transaction['interest_date']->gte($start) && $transaction['interest_date']->lte($end);
}
}
return true;
};
$this->postFilters[] = $filter;
return $this;
}
/**
* @inheritDoc
*/
public function withMetaDate(string $field): GroupCollectorInterface
{
$this->joinMetaDataTables();
$this->query->where('journal_meta.name', '=', $field);
$this->query->whereNotNull('journal_meta.data');
return $this;
}
/**
* Collect transactions updated on a specific date.
*