Various extensions to recurring transactions.

This commit is contained in:
James Cole 2018-06-26 18:49:33 +02:00
parent 7591f3fa29
commit 5d01955133
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
12 changed files with 88 additions and 17 deletions

View File

@ -64,15 +64,5 @@ class Kernel extends ConsoleKernel
{
// create recurring transactions.
$schedule->job(new CreateRecurringTransactions(new Carbon))->daily();
// send test email.
$schedule->call(
function () {
$ipAddress = '127.0.0.1';
/** @var User $user */
$user = User::find(1);
event(new AdminRequestedTestMessage($user, $ipAddress));
}
)->daily();
}
}

View File

@ -27,6 +27,7 @@ namespace FireflyIII\Http\Controllers\Recurring;
use Carbon\Carbon;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\RecurrenceFormRequest;
use FireflyIII\Models\RecurrenceRepetition;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use Illuminate\Http\Request;
@ -89,10 +90,17 @@ class CreateController extends Controller
'until_date' => trans('firefly.repeat_until_date'),
'times' => trans('firefly.repeat_times'),
];
// what to do in the weekend?
$weekendResponses = [
RecurrenceRepetition::WEEKEND_DO_NOTHING => trans('firefly.do_nothing'),
RecurrenceRepetition::WEEKEND_SKIP_CREATION => trans('firefly.skip_transaction'),
RecurrenceRepetition::WEEKEND_TO_FRIDAY => trans('firefly.jump_to_friday'),
RecurrenceRepetition::WEEKEND_TO_MONDAY => trans('firefly.jump_to_monday'),
];
// flash some data:
$hasOldInput = null !== $request->old('_token');
$preFilled = [
$preFilled = [
'first_date' => $tomorrow->format('Y-m-d'),
'transaction_type' => $hasOldInput ? $request->old('transaction_type') : 'withdrawal',
'active' => $hasOldInput ? (bool)$request->old('active') : true,
@ -100,7 +108,9 @@ class CreateController extends Controller
];
$request->session()->flash('preFilled', $preFilled);
return view('recurring.create', compact('tomorrow', 'oldRepetitionType', 'preFilled', 'repetitionEnds', 'defaultCurrency', 'budgets'));
return view(
'recurring.create', compact('tomorrow', 'oldRepetitionType', 'weekendResponses', 'preFilled', 'repetitionEnds', 'defaultCurrency', 'budgets')
);
}
/**

View File

@ -110,6 +110,7 @@ class IndexController extends Controller
$repetition->repetition_type = $repetitionType;
$repetition->repetition_moment = $repetitionMoment;
$repetition->repetition_skip = (int)$request->get('skip');
$repetition->weekend = (int)$request->get('weekend');
$actualEnd = clone $end;
switch ($endsAt) {
@ -222,8 +223,9 @@ class IndexController extends Controller
'daily' => ['label' => trans('firefly.recurring_daily'), 'selected' => 0 === strpos($preSelected, 'daily')],
$weekly => ['label' => trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]), 'selected' => 0 === strpos($preSelected, 'weekly')],
$monthly => ['label' => trans('firefly.recurring_monthly', ['dayOfMonth' => $date->day]), 'selected' => 0 === strpos($preSelected, 'monthly')],
$ndom => ['label' => trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]),'selected' => 0 === strpos($preSelected, 'ndom')],
$yearly => ['label' => trans('firefly.recurring_yearly', ['date' => $yearlyDate]),'selected' => 0 === strpos($preSelected, 'yearly')],
$ndom => ['label' => trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]),
'selected' => 0 === strpos($preSelected, 'ndom')],
$yearly => ['label' => trans('firefly.recurring_yearly', ['date' => $yearlyDate]), 'selected' => 0 === strpos($preSelected, 'yearly')],
];
}

View File

@ -89,7 +89,7 @@ class CreateRecurringTransactions implements ShouldQueue
Log::debug('Now running report thing.');
// will now send email to users.
foreach ($result as $userId => $journals) {
// random bunch to make mail.
//// random bunch to make mail.
$journals = TransactionJournal::where('user_id', $userId)->inRandomOrder()->take(1)->get();
event(new RequestedReportOnJournals($userId, $journals));
}

View File

@ -34,6 +34,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @property string $repetition_type
* @property string $repetition_moment
* @property int $repetition_skip
* @property int $weekend
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $deleted_at
* @property \Carbon\Carbon $updated_at
@ -41,10 +42,18 @@ use Illuminate\Database\Eloquent\SoftDeletes;
*/
class RecurrenceRepetition extends Model
{
/** @var int */
public const WEEKEND_DO_NOTHING = 1;
/** @var int */
public const WEEKEND_SKIP_CREATION = 2;
/** @var int */
public const WEEKEND_TO_FRIDAY = 3;
/** @var int */
public const WEEKEND_TO_MONDAY = 4;
use SoftDeletes;
/** @var array */
protected $casts
= [
= [
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
@ -52,7 +61,7 @@ class RecurrenceRepetition extends Model
'repetition_moment' => 'string',
'repetition_skip' => 'int',
];
protected $fillable = ['recurrence_id', 'repetition_type', 'repetition_moment', 'repetition_skip'];
protected $fillable = ['recurrence_id', 'weekend', 'repetition_type', 'repetition_moment', 'repetition_skip'];
/** @var string */
protected $table = 'recurrences_repetitions';

View File

@ -262,6 +262,8 @@ class RecurringRepository implements RecurringRepositoryInterface
}
break;
}
// filter out all the weekend days:
$return = $this->filterWeekends($repetition, $return);
return $return;
}
@ -492,4 +494,43 @@ class RecurringRepository implements RecurringRepositoryInterface
return $service->update($recurrence, $data);
}
/**
* Filters out all weekend entries, if necessary.
*
* @param RecurrenceRepetition $repetition
* @param array $dates
*
* @return array
*/
protected function filterWeekends(RecurrenceRepetition $repetition, array $dates): array
{
if ($repetition->weekend === RecurrenceRepetition::WEEKEND_DO_NOTHING) {
return $dates;
}
$return = [];
/** @var Carbon $date */
foreach ($dates as $date) {
$isWeekend = $date->isWeekend();
// set back to Friday?
if ($isWeekend && $repetition->weekend === RecurrenceRepetition::WEEKEND_TO_FRIDAY) {
$clone = clone $date;
$clone->subDays(7 - $date->dayOfWeekIso);
$return[] = $clone;
}
// postpone to Monday?
if ($isWeekend && $repetition->weekend === RecurrenceRepetition::WEEKEND_TO_MONDAY) {
$clone = clone $date;
$clone->addDays(8 - $date->dayOfWeekIso);
$return[] = $clone;
}
// otherwise, ignore the date!
}
// filter unique dates?
return $return;
}
}

View File

@ -92,6 +92,7 @@ class ChangesForV475 extends Migration
$table->string('repetition_type', 50);
$table->string('repetition_moment', 50);
$table->smallInteger('repetition_skip', false, true);
$table->smallInteger('weekend', false, true);
$table->foreign('recurrence_id')->references('id')->on('recurrences')->onDelete('cascade');
}

View File

@ -67,6 +67,7 @@ function showRepCalendar() {
newEventsUri += '&end_date=' + $('#ffInput_repeat_until').val();
newEventsUri += '&reps=' + $('#ffInput_repetitions').val();
newEventsUri += '&first_date=' + $('#ffInput_first_date').val();
newEventsUri += '&weekend=' + $('#ffInput_weekend').val();
// remove all event sources from calendar:
calendar.fullCalendar('removeEventSources');

View File

@ -1261,5 +1261,11 @@ return [
'updated_recurrence' => 'Updated recurring transaction ":title"',
'recurrence_is_inactive' => 'This recurring transaction is not active and will not generate new transactions.',
'delete_recurring' => 'Delete recurring transaction ":title"',
'new_recurring_transaction' => 'New recurring transaction',
'help_weekend' => 'What should Firefly III do when the recurring transaction falls on a Saturday or Sunday?',
'do_nothing' => 'Just create the transaction',
'skip_transaction' => 'Skip the occurence',
'jump_to_friday' => 'Create the transaction on the previous Friday instead',
'jump_to_monday' => 'Create the transaction on the next Monday instead',
'recurrence_deleted' => 'Recurring transaction ":title" deleted',
];

View File

@ -236,5 +236,6 @@ return [
'repetition_end' => 'Repetition ends',
'repetitions' => 'Repetitions',
'calendar' => 'Calendar',
'weekend' => 'Weekend',
];

View File

@ -105,6 +105,15 @@
</div>
</a>
</li>
<li>
<a href="{{ route('recurring.create') }}">
<i class="menu-icon fa fa-paint-brush bg-teal"></i>
<div class="menu-info">
<h4 class="control-sidebar-subheading">{{ 'new_recurring_transaction'|_ }}</h4>
</div>
</a>
</li>
</ul>

View File

@ -21,6 +21,7 @@
{{ ExpandedForm.select('repetition_type', [], null, {helpText: trans('firefly.change_date_other_options')}) }}
{{ ExpandedForm.number('skip', 0) }}
{{ ExpandedForm.select('repetition_end', repetitionEnds) }}
{{ ExpandedForm.select('weekend', weekendResponses, null, {helpText: trans('firefly.help_weekend')}) }}
{{ ExpandedForm.date('repeat_until',null) }}
{{ ExpandedForm.number('repetitions',null) }}