Update some things for recurring transactions.

This commit is contained in:
James Cole 2018-06-28 06:31:31 +02:00
parent d378e7e897
commit 57cf7f6f0d
5 changed files with 36 additions and 13 deletions

View File

@ -25,9 +25,7 @@ declare(strict_types=1);
namespace FireflyIII\Console;
use Carbon\Carbon;
use FireflyIII\Events\AdminRequestedTestMessage;
use FireflyIII\Jobs\CreateRecurringTransactions;
use FireflyIII\User;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -62,7 +60,6 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// create recurring transactions.
$schedule->job(new CreateRecurringTransactions(new Carbon))->daily();
}
}

View File

@ -24,7 +24,8 @@ class RequestedReportOnJournals
/**
* Create a new event instance.
*
* @return void
* @param int $userId
* @param Collection $journals
*/
public function __construct(int $userId, Collection $journals)
{

View File

@ -51,7 +51,7 @@ class CreateRecurringTransactions implements ShouldQueue
*/
public function handle(): void
{
Log::debug('Now at start of CreateRecurringTransactions() job.');
Log::debug(sprintf('Now at start of CreateRecurringTransactions() job for %s.', $this->date->format('D d M Y')));
$recurrences = $this->repository->getAll();
Log::debug(sprintf('Count of collection is %d', $recurrences->count()));
@ -125,9 +125,6 @@ class CreateRecurringTransactions implements ShouldQueue
$startDate = clone $recurrence->first_date;
if (null !== $recurrence->latest_date && $recurrence->latest_date->gte($startDate)) {
$startDate = clone $recurrence->latest_date;
// jump to a day later.
$startDate->addDay();
}
return $startDate;
@ -250,12 +247,19 @@ class CreateRecurringTransactions implements ShouldQueue
);
// start looping from $startDate to today perhaps we have a hit?
$occurrences = $this->repository->getOccurrencesInRange($repetition, $recurrence->first_date, $this->date);
// add two days to $this->date so we always include the weekend.
$includeWeekend = clone $this->date;
$includeWeekend->addDays(2);
$occurrences = $this->repository->getOccurrencesInRange($repetition, $recurrence->first_date, $includeWeekend);
Log::debug(
sprintf(
'Calculated %d occurrences between %s and %s', \count($occurrences), $recurrence->first_date->format('Y-m-d'), $this->date->format('Y-m-d')
'Calculated %d occurrences between %s and %s',
\count($occurrences),
$recurrence->first_date->format('Y-m-d'),
$includeWeekend->format('Y-m-d')
), $this->debugArray($occurrences)
);
unset($includeWeekend);
$result = $this->handleOccurrences($recurrence, $occurrences);
$collection = $collection->merge($result);

View File

@ -65,7 +65,13 @@ class ReportNewJournalsMail extends Mailable
*/
public function build(): self
{
$subject = $this->journals->count() === 1
? 'Firefly III has created a new transaction'
: sprintf(
'Firefly III has created new %d transactions', $this->journals->count()
);
return $this->view('emails.report-new-journals-html')->text('emails.report-new-journals-text')
->subject('Firefly III has created new transactions');
->subject($subject);
}
}

View File

@ -547,6 +547,8 @@ class RecurringRepository implements RecurringRepositoryInterface
protected function filterWeekends(RecurrenceRepetition $repetition, array $dates): array
{
if ($repetition->weekend === RecurrenceRepetition::WEEKEND_DO_NOTHING) {
Log::debug('Repetition will not be filtered on weekend days.');
return $dates;
}
$return = [];
@ -555,29 +557,42 @@ class RecurringRepository implements RecurringRepositoryInterface
$isWeekend = $date->isWeekend();
if (!$isWeekend) {
$return[] = clone $date;
Log::debug(sprintf('Date is %s, not a weekend date.', $date->format('D d M Y')));
continue;
}
// is weekend and must set back to Friday?
if ($isWeekend && $repetition->weekend === RecurrenceRepetition::WEEKEND_TO_FRIDAY) {
if ($repetition->weekend === RecurrenceRepetition::WEEKEND_TO_FRIDAY) {
$clone = clone $date;
$clone->addDays(5 - $date->dayOfWeekIso);
Log::debug(
sprintf('Date is %s, and this is in the weekend, so corrected to %s (Friday).', $date->format('D d M Y'), $clone->format('D d M Y'))
);
$return[] = clone $clone;
continue;
}
// postpone to Monday?
if ($isWeekend && $repetition->weekend === RecurrenceRepetition::WEEKEND_TO_MONDAY) {
if ($repetition->weekend === RecurrenceRepetition::WEEKEND_TO_MONDAY) {
$clone = clone $date;
$clone->addDays(8 - $date->dayOfWeekIso);
Log::debug(
sprintf('Date is %s, and this is in the weekend, so corrected to %s (Monday).', $date->format('D d M Y'), $clone->format('D d M Y'))
);
$return[] = $clone;
continue;
}
Log::debug(sprintf('Date is %s, removed from final result', $date->format('D d M Y')));
}
// filter unique dates
Log::debug(sprintf('Count before filtering: %d', \count($dates)));
$collection = new Collection($return);
$filtered = $collection->unique();
$return = $filtered->toArray();
Log::debug(sprintf('Count after filtering: %d', \count($return)));
return $return;
}
}