mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-05 12:12:18 +00:00
Various code reshuffelling.
This commit is contained in:
@@ -69,6 +69,14 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
$service->destroy($recurrence);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function destroyAll(): void
|
||||
{
|
||||
$this->user->recurrences()->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all of the user's recurring transactions.
|
||||
*
|
||||
@@ -378,6 +386,50 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
return $this->filterWeekends($repetition, $occurrences);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the next X iterations starting on the date given in $date.
|
||||
* Returns an array of Carbon objects.
|
||||
*
|
||||
* Only returns them of they are after $afterDate
|
||||
*
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
* @param Carbon $afterDate
|
||||
* @param int $count
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getXOccurrencesSince(RecurrenceRepetition $repetition, Carbon $date, Carbon $afterDate, int $count): array
|
||||
{
|
||||
Log::debug('Now in getXOccurrencesSince()');
|
||||
$skipMod = $repetition->repetition_skip + 1;
|
||||
$occurrences = [];
|
||||
if ('daily' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXDailyOccurrencesSince($date, $afterDate, $count, $skipMod);
|
||||
}
|
||||
if ('weekly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXWeeklyOccurrencesSince($date, $afterDate, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('monthly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXMonthlyOccurrencesSince($date, $afterDate, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('ndom' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXNDomOccurrencesSince($date, $afterDate, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('yearly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXYearlyOccurrencesSince($date, $afterDate, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
|
||||
// filter out all the weekend days:
|
||||
$occurrences = $this->filterWeekends($repetition, $occurrences);
|
||||
|
||||
// filter out everything if "repeat_until" is set.
|
||||
$repeatUntil = $repetition->recurrence->repeat_until;
|
||||
|
||||
return $this->filterMaxDate($repeatUntil, $occurrences);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the repetition in a string that is user readable.
|
||||
*
|
||||
@@ -437,6 +489,21 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function searchRecurrence(string $query, int $limit): Collection
|
||||
{
|
||||
$search = $this->user->recurrences();
|
||||
if ('' !== $query) {
|
||||
$search->where('recurrences.title', 'LIKE', sprintf('%%%s%%', $query));
|
||||
}
|
||||
$search
|
||||
->orderBy('recurrences.title', 'ASC');
|
||||
|
||||
return $search->take($limit)->get(['id', 'title', 'description']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user for in repository.
|
||||
*
|
||||
@@ -466,96 +533,6 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $data
|
||||
*
|
||||
* @return Recurrence
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function update(Recurrence $recurrence, array $data): Recurrence
|
||||
{
|
||||
/** @var RecurrenceUpdateService $service */
|
||||
$service = app(RecurrenceUpdateService::class);
|
||||
|
||||
return $service->update($recurrence, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the next X iterations starting on the date given in $date.
|
||||
* Returns an array of Carbon objects.
|
||||
*
|
||||
* Only returns them of they are after $afterDate
|
||||
*
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
* @param Carbon $afterDate
|
||||
* @param int $count
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getXOccurrencesSince(RecurrenceRepetition $repetition, Carbon $date, Carbon $afterDate, int $count): array
|
||||
{
|
||||
Log::debug('Now in getXOccurrencesSince()');
|
||||
$skipMod = $repetition->repetition_skip + 1;
|
||||
$occurrences = [];
|
||||
if ('daily' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXDailyOccurrencesSince($date, $afterDate, $count, $skipMod);
|
||||
}
|
||||
if ('weekly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXWeeklyOccurrencesSince($date, $afterDate, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('monthly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXMonthlyOccurrencesSince($date, $afterDate, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('ndom' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXNDomOccurrencesSince($date, $afterDate, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
if ('yearly' === $repetition->repetition_type) {
|
||||
$occurrences = $this->getXYearlyOccurrencesSince($date, $afterDate, $count, $skipMod, $repetition->repetition_moment);
|
||||
}
|
||||
|
||||
// filter out all the weekend days:
|
||||
$occurrences = $this->filterWeekends($repetition, $occurrences);
|
||||
|
||||
// filter out everything if "repeat_until" is set.
|
||||
$repeatUntil = $repetition->recurrence->repeat_until;
|
||||
|
||||
return $this->filterMaxDate($repeatUntil, $occurrences);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon|null $max
|
||||
* @param array $occurrences
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function filterMaxDate(?Carbon $max, array $occurrences): array
|
||||
{
|
||||
if (null === $max) {
|
||||
return $occurrences;
|
||||
}
|
||||
$filtered = [];
|
||||
foreach ($occurrences as $date) {
|
||||
if ($date->lte($max)) {
|
||||
$filtered[] = $date;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function destroyAll(): void
|
||||
{
|
||||
$this->user->recurrences()->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
@@ -581,17 +558,40 @@ class RecurringRepository implements RecurringRepositoryInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* Update a recurring transaction.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param array $data
|
||||
*
|
||||
* @return Recurrence
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function searchRecurrence(string $query, int $limit): Collection
|
||||
public function update(Recurrence $recurrence, array $data): Recurrence
|
||||
{
|
||||
$search = $this->user->recurrences();
|
||||
if ('' !== $query) {
|
||||
$search->where('recurrences.title', 'LIKE', sprintf('%%%s%%', $query));
|
||||
}
|
||||
$search
|
||||
->orderBy('recurrences.title', 'ASC');
|
||||
/** @var RecurrenceUpdateService $service */
|
||||
$service = app(RecurrenceUpdateService::class);
|
||||
|
||||
return $search->take($limit)->get(['id', 'title', 'description']);
|
||||
return $service->update($recurrence, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon|null $max
|
||||
* @param array $occurrences
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function filterMaxDate(?Carbon $max, array $occurrences): array
|
||||
{
|
||||
if (null === $max) {
|
||||
return $occurrences;
|
||||
}
|
||||
$filtered = [];
|
||||
foreach ($occurrences as $date) {
|
||||
if ($date->lte($max)) {
|
||||
$filtered[] = $date;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered;
|
||||
}
|
||||
}
|
||||
|
@@ -39,28 +39,6 @@ use Illuminate\Support\Collection;
|
||||
*/
|
||||
interface RecurringRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Destroy all recurring transactions.
|
||||
*/
|
||||
public function destroyAll(): void;
|
||||
|
||||
/**
|
||||
* @param string $query
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchRecurrence(string $query, int $limit): Collection;
|
||||
|
||||
/**
|
||||
* Calculate how many transactions are to be expected from this recurrence.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @return int
|
||||
*/
|
||||
public function totalTransactions(Recurrence $recurrence, RecurrenceRepetition $repetition): int;
|
||||
|
||||
/**
|
||||
* Destroy a recurring transaction.
|
||||
*
|
||||
@@ -68,6 +46,11 @@ interface RecurringRepositoryInterface
|
||||
*/
|
||||
public function destroy(Recurrence $recurrence): void;
|
||||
|
||||
/**
|
||||
* Destroy all recurring transactions.
|
||||
*/
|
||||
public function destroyAll(): void;
|
||||
|
||||
/**
|
||||
* Returns all of the user's recurring transactions.
|
||||
*
|
||||
@@ -142,6 +125,7 @@ interface RecurringRepositoryInterface
|
||||
|
||||
/**
|
||||
* @param RecurrenceTransaction $transaction
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getPiggyBank(RecurrenceTransaction $transaction): ?int;
|
||||
@@ -179,8 +163,8 @@ interface RecurringRepositoryInterface
|
||||
* @param Carbon $date
|
||||
* @param int $count
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getXOccurrences(RecurrenceRepetition $repetition, Carbon $date, int $count): array;
|
||||
|
||||
@@ -192,13 +176,13 @@ interface RecurringRepositoryInterface
|
||||
*
|
||||
* @param RecurrenceRepetition $repetition
|
||||
* @param Carbon $date
|
||||
* @param Carbon $afterDate
|
||||
* @param Carbon $afterDate
|
||||
* @param int $count
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getXOccurrencesSince(RecurrenceRepetition $repetition, Carbon $date,Carbon $afterDate, int $count): array;
|
||||
public function getXOccurrencesSince(RecurrenceRepetition $repetition, Carbon $date, Carbon $afterDate, int $count): array;
|
||||
|
||||
/**
|
||||
* Parse the repetition in a string that is user readable.
|
||||
@@ -209,6 +193,14 @@ interface RecurringRepositoryInterface
|
||||
*/
|
||||
public function repetitionDescription(RecurrenceRepetition $repetition): string;
|
||||
|
||||
/**
|
||||
* @param string $query
|
||||
* @param int $limit
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function searchRecurrence(string $query, int $limit): Collection;
|
||||
|
||||
/**
|
||||
* Set user for in repository.
|
||||
*
|
||||
@@ -220,11 +212,22 @@ interface RecurringRepositoryInterface
|
||||
* Store a new recurring transaction.
|
||||
*
|
||||
* @param array $data
|
||||
* @throws FireflyException
|
||||
*
|
||||
* @return Recurrence
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function store(array $data): Recurrence;
|
||||
|
||||
/**
|
||||
* Calculate how many transactions are to be expected from this recurrence.
|
||||
*
|
||||
* @param Recurrence $recurrence
|
||||
* @param RecurrenceRepetition $repetition
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function totalTransactions(Recurrence $recurrence, RecurrenceRepetition $repetition): int;
|
||||
|
||||
/**
|
||||
* Update a recurring transaction.
|
||||
*
|
||||
|
Reference in New Issue
Block a user