Feature - Add support for a fiscal year period defined in user settings.

* Add "This fiscal year", "Last fiscal year" as date range options in Transaction Details to filter transactions to those periods
* Add fiscal year ranges to Statistics & Trend Analysis
* Add "fiscal year start date" to user profile settings, allowing the user to select any date of the calendar year as the start of the fiscal year
* Add "fiscal year format" to user profile settings, allowing the user to specify how financial year date labels should appear

Implementation notes:
* The default fiscal year start is January 1 and the default fiscal year display format is "FY 2025"
* Fiscal year start date (month number & day number) are stored together in db as a uint16, high byte & low byte respectively
* February 29 is disallowed as a fiscal year start date, since it is never used as a convention in any country
* Jest is added to the project as a dev dependency, for unit tests in frontend

Signed-off-by: Sebastian Reategui <seb.reategui@gmail.com>
This commit is contained in:
Sebastian Reategui
2025-06-05 12:36:46 +10:00
committed by mayswind
parent 70eea8ff33
commit b94dc8eb83
42 changed files with 3417 additions and 105 deletions
+26
View File
@@ -0,0 +1,26 @@
package validators
import (
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
"github.com/mayswind/ezbookkeeping/pkg/core"
)
// ValidateFiscalYearStart validates if a fiscal year start date is valid
func ValidateFiscalYearStart(fl validator.FieldLevel) bool {
date, ok := fl.Field().Interface().(core.FiscalYearStart)
if !ok {
return false
}
// Use the core functionality to validate
_, _, err := date.GetMonthDay()
return err == nil
}
// RegisterFiscalYearStartValidator registers the fiscal year start date validator
func RegisterFiscalYearStartValidator() {
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("validFiscalYearStart", ValidateFiscalYearStart)
}
}
@@ -0,0 +1,68 @@
package validators
import (
"testing"
"github.com/go-playground/validator/v10"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/stretchr/testify/assert"
)
type fiscalYearStartContainer struct {
FiscalYearStart core.FiscalYearStart `validate:"validFiscalYearStart"`
}
func TestValidateFiscalYearStart_ValidValues(t *testing.T) {
validate := validator.New()
validate.RegisterValidation("validFiscalYearStart", ValidateFiscalYearStart)
testCases := []struct {
name string
value core.FiscalYearStart
}{
{"January 1", 0x0101}, // January 1
{"December 31", 0x0C1F}, // December 31
{"July 1", 0x0701}, // July 1
{"April 15", 0x040F}, // April 15
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
container := fiscalYearStartContainer{FiscalYearStart: tc.value}
err := validate.Struct(container)
assert.Nil(t, err)
})
}
}
func TestValidateFiscalYearStart_InvalidValues(t *testing.T) {
validate := validator.New()
validate.RegisterValidation("validFiscalYearStart", ValidateFiscalYearStart)
testCases := []struct {
name string
value core.FiscalYearStart
}{
{"Zero value", 0}, // Zero value
{"Month 0", 0x0001}, // Month 0 (invalid)
{"Month 13", 0x0D01}, // Month 13 (invalid)
{"Day 0", 0x0100}, // Day 0 (invalid)
{"January 32", 0x0120}, // January 32 (invalid)
{"February 29", 0x021D}, // February 29 (not permitted)
{"February 30", 0x021E}, // February 30 (invalid)
{"April 31", 0x041F}, // April 31 (invalid)
{"June 31", 0x061F}, // June 31 (invalid)
{"September 31", 0x091F}, // September 31 (invalid)
{"November 32", 0x0B20}, // November 32 (invalid)
{"Invalid month 255", 0xFF01}, // Invalid month
{"Invalid day 255", 0x01FF}, // Invalid day
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
container := fiscalYearStartContainer{FiscalYearStart: tc.value}
err := validate.Struct(container)
assert.NotNil(t, err)
})
}
}