Skip to content

Firefly III: a personal finances manager

Expand and fix create recurrence form.

James Coleaf5a3ee7d666
diff --git a/app/Http/Controllers/Json/RecurrenceController.php b/app/Http/Controllers/Json/RecurrenceController.php
index ae601b4e40..64761613fd 100644
--- a/app/Http/Controllers/Json/RecurrenceController.php
+++ b/app/Http/Controllers/Json/RecurrenceController.php
@@ -67,47 +67,64 @@ final class RecurrenceController extends Controller
*/
public function events(Request $request): JsonResponse
{
- $occurrences = [];
- $return = [];
- $start = Carbon::createFromFormat('Y-m-d', $request->input('start'));
- $end = Carbon::createFromFormat('Y-m-d', $request->input('end'));
- $firstDate = Carbon::createFromFormat('Y-m-d', $request->input('first_date'));
- $endDate = '' !== (string) $request->input('end_date') ? Carbon::createFromFormat('Y-m-d', $request->input('end_date')) : null;
- $endsAt = (string) $request->input('ends');
- $repetitionType = explode(',', (string) $request->input('type'))[0];
- $repetitions = (int) $request->input('reps');
- $weekend = (int) $request->input('weekend');
- $repetitionMoment = '';
- $skip = (int) $request->input('skip');
- $skip = $skip < 0 || $skip > 31 ? 0 : $skip;
- $weekend = $weekend < 1 || $weekend > 4 ? 1 : $weekend;
+ $occurrences = [];
+ $return = [];
+ $start = null;
+ $end = null;
+ $firstDate = null;
+ try {
+ $start = Carbon::createFromFormat('Y-m-d', $request->input('start'));
+ } catch (InvalidFormatException $e) {
+ Log::debug($e->getMessage()); // not that interesting
+ }
+ try {
+ $end = Carbon::createFromFormat('Y-m-d', $request->input('end'));
+ } catch (InvalidFormatException $e) {
+ Log::debug($e->getMessage()); // not that interesting
+ }
+ try {
+ $firstDate = Carbon::createFromFormat('Y-m-d', $request->input('first_date'));
+ } catch (InvalidFormatException $e) {
+ Log::debug($e->getMessage()); // not that interesting
+ }
+ $endDate = '' !== (string)$request->input('end_date') ? Carbon::createFromFormat('Y-m-d', $request->input('end_date')) : null;
+ $endsAt = (string)$request->input('ends');
+ $repetitionType = explode(',', (string)$request->input('type'))[0];
+ $repetitions = (int)$request->input('reps');
+ $weekend = (int)$request->input('weekend');
+ $repetitionMoment = '';
+ $skip = (int)$request->input('skip');
+ $skip = $skip < 0 || $skip > 31 ? 0 : $skip;
+ $weekend = $weekend < 1 || $weekend > 4 ? 1 : $weekend;
+
+ $start = session()->get('start');
if (!$endDate instanceof Carbon) {
// safety catch:
$endDate = now()->addYear();
}
- if (!$start instanceof Carbon || !$end instanceof Carbon || !$firstDate instanceof Carbon) {
+ if (!$start instanceof Carbon || !$firstDate instanceof Carbon) {
return response()->json();
}
$start->startOfDay();
// if $firstDate is beyond $end, simply return an empty array.
- if ($firstDate->gt($end)) {
+ if ($firstDate->gt($endDate)) {
return response()->json();
}
// if $firstDate is beyond start, use that one:
- $actualStart = clone $firstDate;
+ $actualStart = clone $firstDate;
if ('weekly' === $repetitionType || 'monthly' === $repetitionType) {
- $repetitionMoment = explode(',', (string) $request->input('type'))[1] ?? '1';
+ $repetitionMoment = explode(',', (string)$request->input('type'))[1] ?? '1';
}
if ('ndom' === $repetitionType) {
$repetitionMoment = str_ireplace('ndom,', '', $request->input('type'));
}
if ('yearly' === $repetitionType) {
- $repetitionMoment = explode(',', (string) $request->input('type'))[1] ?? '2025-01-01';
+ $repetitionMoment = explode(',', (string)$request->input('type'))[1] ?? '2025-01-01';
}
$actualStart->startOfDay();
$repetition = new RecurrenceRepetition();
@@ -115,7 +132,7 @@ final class RecurrenceController extends Controller
$repetition->repetition_moment = $repetitionMoment;
$repetition->repetition_skip = $skip;
$repetition->weekend = $weekend;
- $actualEnd = clone $end;
+ $actualEnd = clone $endDate;
if ('until_date' === $endsAt) {
$actualEnd = $endDate;
@@ -133,7 +150,7 @@ final class RecurrenceController extends Controller
foreach ($occurrences as $current) {
if ($current->gte($start)) {
$event = [
- 'id' => $repetitionType.$firstDate->format('Ymd'),
+ 'id' => $repetitionType . $firstDate->format('Ymd'),
'title' => 'X',
'allDay' => true,
'start' => $current->format('Y-m-d'),
@@ -153,8 +170,8 @@ final class RecurrenceController extends Controller
*/
public function suggest(Request $request): JsonResponse
{
- $string = '' === (string) $request->input('date') ? Carbon::now()->format('Y-m-d') : (string) $request->input('date');
- $today = today(config('app.timezone'))->startOfDay();
+ $string = '' === (string)$request->input('date') ? Carbon::now()->format('Y-m-d') : (string)$request->input('date');
+ $today = today(config('app.timezone'))->startOfDay();
try {
$date = Carbon::createFromFormat('Y-m-d', $string, config('app.timezone'));
@@ -165,37 +182,37 @@ final class RecurrenceController extends Controller
return response()->json();
}
$date->startOfDay();
- $preSelected = (string) $request->input('pre_select');
+ $preSelected = (string)$request->input('pre_select');
$locale = Steam::getLocale();
Log::debug(sprintf('date = %s, today = %s. date > today? %s', $date->toAtomString(), $today->toAtomString(), var_export($date > $today, true)));
- Log::debug(sprintf('past = true? %s', var_export('true' === (string) $request->input('past'), true)));
+ Log::debug(sprintf('past = true? %s', var_export('true' === (string)$request->input('past'), true)));
- $result = [];
- if ($date > $today || 'true' === (string) $request->input('past')) {
+ $result = [];
+ if ($date > $today || 'true' === (string)$request->input('past')) {
Log::debug('Will fill dropdown.');
$weekly = sprintf('weekly,%s', $date->dayOfWeekIso);
$monthly = sprintf('monthly,%s', $date->day);
- $dayOfWeek = (string) trans(sprintf('config.dow_%s', $date->dayOfWeekIso));
+ $dayOfWeek = (string)trans(sprintf('config.dow_%s', $date->dayOfWeekIso));
$ndom = sprintf('ndom,%s,%s', $date->weekOfMonth, $date->dayOfWeekIso);
$yearly = sprintf('yearly,%s', $date->format('Y-m-d'));
- $yearlyDate = $date->isoFormat((string) trans('config.month_and_day_no_year_js', [], $locale));
+ $yearlyDate = $date->isoFormat((string)trans('config.month_and_day_no_year_js', [], $locale));
$result = [
- 'daily' => ['label' => (string) trans('firefly.recurring_daily'), 'selected' => str_starts_with($preSelected, 'daily')],
+ 'daily' => ['label' => (string)trans('firefly.recurring_daily'), 'selected' => str_starts_with($preSelected, 'daily')],
$weekly => [
- 'label' => (string) trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]),
+ 'label' => (string)trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]),
'selected' => str_starts_with($preSelected, 'weekly'),
],
$monthly => [
- 'label' => (string) trans('firefly.recurring_monthly', ['dayOfMonth' => $date->day]),
+ 'label' => (string)trans('firefly.recurring_monthly', ['dayOfMonth' => $date->day]),
'selected' => str_starts_with($preSelected, 'monthly'),
],
$ndom => [
- 'label' => (string) trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]),
+ 'label' => (string)trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]),
'selected' => str_starts_with($preSelected, 'ndom'),
],
$yearly => [
- 'label' => (string) trans('firefly.recurring_yearly', ['date' => $yearlyDate]),
+ 'label' => (string)trans('firefly.recurring_yearly', ['date' => $yearlyDate]),
'selected' => str_starts_with($preSelected, 'yearly'),
],
];
diff --git a/public/v1/js/ff/recurring/create.js b/public/v1/js/ff/recurring/create.js
index b7917c95f5..06b1328933 100644
--- a/public/v1/js/ff/recurring/create.js
+++ b/public/v1/js/ff/recurring/create.js
@@ -25,8 +25,6 @@ var calendar;
$(document).ready(function () {
"use strict";
$(".content-wrapper form input:enabled:visible:first").first().focus().select();
- // initializeButtons();
- // initializeAutoComplete();
// respondToFirstDateChange();
// respondToRepetitionEnd();
// $('.switch-button').on('click', switchTransactionType);
@@ -108,7 +106,7 @@ function respondToFirstDateChange() {
// preselected value:
var preSelected = oldRepetitionType;
- if(preSelected === '') {
+ if('' === preSelected) {
preSelected = select.val();
}
@@ -137,8 +135,8 @@ function parseRepetitionSuggestions(data) {
}
function initializeAutoComplete() {
- initTagsAC();
- // initExpenseAC();
+ //initTagsAC();
+ //initExpenseAC();
// initRevenueAC();
// initCategoryAC();
}
@@ -216,7 +214,7 @@ function updateFormFields() {
}
if (transactionType === 'transfer') {
- // $('#source_name_holder').hide(); // source name no longer exists.
+ // $('#source_name_holder').hide(); // source name no longer exists.
$('#deposit_source_id_holder').hide(); // new one!
$('#source_id_holder').show();
diff --git a/resources/assets/v3/js/pages/recurring/create.js b/resources/assets/v3/js/pages/recurring/create.js
index 1e95a40e7b..fa2096bfe4 100644
--- a/resources/assets/v3/js/pages/recurring/create.js
+++ b/resources/assets/v3/js/pages/recurring/create.js
@@ -24,15 +24,214 @@ import dates from '../shared/dates.js';
import Autocomplete from "bootstrap5-autocomplete";
import Tags from "bootstrap5-tags";
+import {Calendar} from 'fullcalendar';
+import themePlugin from 'fullcalendar/themes/monarch'; // YOUR THEME
+import dayGridPlugin from 'fullcalendar/daygrid';
+import timeGridPlugin from 'fullcalendar/timegrid';
+import listPlugin from 'fullcalendar/list';
+
+// stylesheets
+import 'fullcalendar/skeleton.css'; // ALWAYS NEED SKELETON
+import 'fullcalendar/themes/monarch/theme.css'; // YOUR THEME
+import 'fullcalendar/themes/monarch/palettes/purple.css'; // YOUR THEME'S PALETTE
+
+let calendarEl = document.getElementById('recurring_calendar');
+let calendar = new Calendar(calendarEl, {
+ plugins: [themePlugin, dayGridPlugin, timeGridPlugin, listPlugin],
+ initialView: 'dayGridMonth',
+ headerToolbar: {
+ left: 'prev,next today',
+ center: 'title',
+ right: 'dayGridMonth,timeGridWeek,listWeek'
+ }
+});
+calendar.render();
+
+
let create = function () {
return {
-
+ calendar: null,
+ eventSource: null,
init() {
console.log('Init recurring create');
this.createTagField();
this.createAutocomplete();
+ this.createButtonSwitcher();
+ this.switchTransactionType('withdrawal');
+ this.respondToFirstDateChange();
+ this.respondToRepetitionEnd();
+ document.getElementById('ffInput_first_date').addEventListener('change', this.respondToFirstDateChange.bind(this));
+ document.getElementById('ffInput_repetition_end').addEventListener('change', this.respondToRepetitionEnd.bind(this));
+
+ // TODO create calendar
+ this.calendar = new Calendar(calendarEl, {
+ plugins: [dayGridPlugin],
+ initialView: 'dayGridMonth'
+ });
+
+ document.getElementById('calendar-link').addEventListener('click', this.showRepCalendar);
+ },
+ showRepCalendar() {
+
+ // pre-append URL with repetition info:
+ let newEventsUrl = eventsUrl + '?type=' + document.getElementById('ffInput_repetition_type').value;
+ newEventsUrl += '&skip=' + document.getElementById('ffInput_skip').value;
+ newEventsUrl += '&ends=' + document.getElementById('ffInput_repetition_end').value;
+ newEventsUrl += '&end_date=' + document.getElementById('ffInput_repeat_until').value;
+ newEventsUrl += '&reps=' + document.getElementById('ffInput_repetitions').value;
+ newEventsUrl += '&first_date=' + document.getElementById('ffInput_first_date').value;
+ newEventsUrl += '&weekend=' + document.getElementById('ffInput_weekend').value;
+
+ console.log(newEventsUrl);
+
+ // remove all event sources from calendar:
+ let eventSource = new EventSource(newEventsUrl);
+ calendar.removeAllEventSources();
+ calendar.addEventSource(eventSource);
+ $('#calendarModal').modal('show');
+
+ return false;
+ },
+ respondToRepetitionEnd() {
+ var obj = document.getElementById('ffInput_repetition_end');
+ var value = obj.value;
+ switch (value) {
+ case 'forever':
+ document.getElementById('repeat_until_holder').style.display = 'none';
+ document.getElementById('repetitions_holder').style.display = 'none';
+ break;
+ case 'until_date':
+ document.getElementById('repeat_until_holder').style.display = 'block';
+ document.getElementById('repetitions_holder').style.display = 'none';
+
+ break;
+ case 'times':
+ document.getElementById('repeat_until_holder').style.display = 'none';
+ document.getElementById('repetitions_holder').style.display = 'block';
+ break;
+ }
},
- createTagField(){
+ respondToFirstDateChange() {
+ let obj = document.getElementById('ffInput_first_date');
+ let select = document.getElementById('ffInput_repetition_type');
+ let date = obj.value;
+ select.disabled = true;
+
+ // preselected value:
+ var preSelected = oldRepetitionType;
+ if (preSelected === '') {
+ preSelected = select.value;
+ }
+
+ $.getJSON(suggestUrl, {date: date, pre_select: preSelected, past: 'true'}).fail(function () {
+ console.error('Could not load repetition suggestions');
+ alert('Could not load repetition suggestions. Please enter a valid date.');
+ }).done(this.parseRepetitionSuggestions);
+ },
+ parseRepetitionSuggestions(data) {
+ let select = document.getElementById('ffInput_repetition_type');
+ select.innerHTML = '';
+ let opt;
+ for (var k in data) {
+ if (data.hasOwnProperty(k)) {
+ console.log('label: ' + data[k].label + ', selected: ' + data[k].selected);
+ opt = document.createElement('option');
+ opt.value = k;
+ opt.label = data[k].label;
+ opt.text = data[k].label;
+ if (data[k].selected) {
+ opt.selected = true;
+ }
+ select.appendChild(opt);
+ }
+ }
+ select.disabled = false;
+ },
+
+
+ createButtonSwitcher() {
+ console.log('Now in initializeButtons()');
+ let list = document.getElementsByClassName('switch-button');
+ for (let i = 0; i < list.length; i++) {
+
+ list[i].addEventListener('click', (event) => {
+ let transactionType = event.currentTarget.dataset.value;
+ // console.log('Clicked value is ' + transactionType);
+ this.switchTransactionType(transactionType);
+ return false;
+ });
+ }
+ // $.each($('.switch-button'), function (i, v) {
+ // //var btn = $(v);
+ //
+ // if (btn.data('value') === transactionType) {
+ // btn.addClass('btn-info disabled').removeClass('btn-default');
+ // $('input[name="transaction_type"]').val(transactionType);
+ // } else {
+ // btn.removeClass('btn-info disabled').addClass('btn-default');
+ // }
+ // });
+ // updateFormFields();
+ },
+ switchTransactionType(transactionType) {
+ let list = document.getElementsByClassName('switch-button');
+ for (let i = 0; i < list.length; i++) {
+ let currentType = list[i].dataset.value;
+ console.log('Selected is ' + currentType + ', form type is ' + transactionType);
+ if (currentType === transactionType) {
+ list[i].classList.add('btn-primary');
+ list[i].classList.remove('btn-secondary');
+ }
+ if (currentType !== transactionType) {
+ list[i].classList.remove('btn-primary');
+ list[i].classList.add('btn-secondary');
+ }
+ }
+ if ('withdrawal' === transactionType) {
+ // hide source account name:
+ document.getElementById('deposit_source_id_holder').style.display = 'none';
+
+ // // show source account ID:
+ document.getElementById('source_id_holder').style.display = 'block';
+
+ // show destination name:
+ document.getElementById('withdrawal_destination_id_holder').style.display = 'block';
+
+ // // hide destination ID:
+ document.getElementById('destination_id_holder').style.display = 'none';
+
+ // show budget + bill
+ document.getElementById('budget_id_holder').style.display = 'block';
+ document.getElementById('bill_id_holder').style.display = 'block';
+
+ // hide piggy bank:
+ document.getElementById('piggy_bank_id_holder').style.display = 'none';
+ }
+
+ if (transactionType === 'deposit') {
+ document.getElementById('deposit_source_id_holder').style.display = 'block';
+
+ document.getElementById('source_id_holder').style.display = 'none';
+
+ document.getElementById('withdrawal_destination_id_holder').style.display = 'none';
+
+ document.getElementById('destination_id_holder').style.display = 'block';
+ document.getElementById('budget_id_holder').style.display = 'none';
+ document.getElementById('bill_id_holder').style.display = 'none';
+ document.getElementById('piggy_bank_id_holder').style.display = 'none';
+ }
+
+ if (transactionType === 'transfer') {
+ document.getElementById('deposit_source_id_holder').style.display = 'none';
+ document.getElementById('source_id_holder').style.display = 'block';
+ document.getElementById('withdrawal_destination_id_holder').style.display = 'none';
+ document.getElementById('destination_id_holder').style.display = 'block';
+ document.getElementById('budget_id_holder').style.display = 'none';
+ document.getElementById('bill_id_holder').style.display = 'none';
+ document.getElementById('piggy_bank_id_holder').style.display = 'block';
+ }
+ },
+ createTagField() {
let token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
Tags.init('#ffInput_tags',
{
@@ -52,7 +251,6 @@ let create = function () {
}
}
}
-
);
},
createAutocomplete() {
@@ -67,14 +265,14 @@ let create = function () {
valueField: 'name',
liveServer: true,
fetchOptions: {
- method: 'GET',
- credentials: 'include',
- headers: {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json',
- 'X-CSRF-TOKEN': token
- }
- }
+ method: 'GET',
+ credentials: 'include',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Accept': 'application/json',
+ 'X-CSRF-TOKEN': token
+ }
+ }
});
},
}
diff --git a/resources/views/recurring/create.blade.php b/resources/views/recurring/create.blade.php
index 40063e5143..005760b324 100644
--- a/resources/views/recurring/create.blade.php
+++ b/resources/views/recurring/create.blade.php
@@ -4,6 +4,7 @@
<form action="{{ route('recurring.store') }}" method="post" id="store" class="form-horizontal"
enctype="multipart/form-data" x-data="create">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
+ <input type="hidden" name="transaction_type" value="withdrawal">
{{-- row with recurrence information --}}
<div class="row">
@@ -28,8 +29,7 @@
<div class="input-group has-validation">
<label for="inputEmail3" class="col-sm-3 col-form-label">{{ trans('form.calendar') }}</label>
<div class="col-sm-9">
- <span type="text" readonly class="form-control-plaintext">
- <a href="#" id="calendar-link">{{ __('firefly.click_for_calendar') }}</a></span>
+ <button class="btn btn-outline-secondary" type="button" id="calendar-link">{{ __('firefly.click_for_calendar') }}</button>
</div>
</div>
</div>
@@ -75,25 +75,24 @@
<div class="card-body">
<p><em>{{ __('firefly.mandatory_fields_for_tranaction') }}</em></p>
{{-- three buttons to distinguish type of transaction --}}
- <div class="form-group" id="name_holder">
- <label for="ffInput_type" class="col-sm-4 control-label">
- {{ trans('form.transaction_type') }}
- </label>
-
- <div class="col-sm-8">
- <div class="btn-group btn-group-sm">
- <a href="#" class="btn btn-outline-secondary switch-button"
- data-value="withdrawal">{{ __('firefly.withdrawal') }}</a>
- <a href="#" class="btn btn-outline-secondary switch-button"
- data-value="deposit">{{ __('firefly.deposit') }}</a>
- <a href="#" class="btn btn-outline-secondary switch-button"
- data-value="transfer">{{ __('firefly.transfer') }}</a>
+ <div class="row mb-3" id="transaction_type_holder">
+ <div class="input-group">
+ <label for="ffInput_transaction_type" class="col-sm-3 col-form-label has-validation">{{ trans('form.transaction_type') }}</label>
+
+ <div class="col-sm-9 pt-1">
+ <div class="btn-group btn-group-sm">
+ <button type="button" class="btn btn-secondary switch-button"
+ data-value="withdrawal">{{ __('firefly.withdrawal') }}</button>
+ <button type="button" class="btn btn-secondary switch-button"
+ data-value="deposit">{{ __('firefly.deposit') }}</button>
+ <button type="button" class="btn btn-secondary switch-button"
+ data-value="transfer">{{ __('firefly.transfer') }}</button>
+ </div>
</div>
</div>
</div>
- <input type="hidden" name="transaction_type" value="">
- {{-- end of three buttons --}}
+ {{-- end of three buttons --}}
{!! ExpandedForm::text('transaction_description') !!}
{{-- transaction information (mandatory) --}}
{!! CurrencyForm::currencyList('transaction_currency_id', $primaryCurrency->id) !!}
@@ -168,7 +167,7 @@
</div>
<div class="card-footer text-end">
<button type="submit" class="btn btn-success">
- {{ __('store_new_recurrence') }}
+ {{ __('firefly.store_new_recurrence') }}
</button>
</div>
</div>
@@ -211,6 +210,11 @@
</div>
@endsection
@section('scripts')
+ <script type="text/javascript" nonce="{{ $JS_NONCE }}">
+ var suggestUrl = "{{ route('recurring.suggest') }}";
+ var oldRepetitionType = "{{ $oldRepetitionType }}";
+ var eventsUrl = "{{ route('recurring.events') }}";
+ </script>
@vite(['js/pages/recurring/create.js'])
<!--
<script type="text/javascript" src="v1/js/lib/bootstrap-simple-autocomplete.js?v={{ $FF_BUILD_TIME }}" nonce="{{ $JS_NONCE }}"></script>
@@ -218,8 +222,8 @@
nonce="{{ $JS_NONCE }}"></script>
<script type="text/javascript" nonce="{{ $JS_NONCE }}">
var transactionType = "{{ $preFilled['transaction_type'] }}";
- var suggestUrl = "{{ route('recurring.suggest') }}";
- var eventsUrl = "{{ route('recurring.events') }}";
+
+
var oldRepetitionType = "{{ $oldRepetitionType }}";
</script>
<script type="text/javascript" src="v1/js/ff/recurring/create.js?v={{ $FF_BUILD_TIME }}"