diff --git a/pkg/dashboard.go b/pkg/dashboard.go index 30a2966..825b9a1 100644 --- a/pkg/dashboard.go +++ b/pkg/dashboard.go @@ -1,15 +1,18 @@ package pkg import ( + "fmt" "github.com/julienschmidt/httprouter" "net/http" ) const ( - YearPeriod = "year" - QuarterPeriod = "quarter" - MonthPeriod = "month" - YesteryearPeriod = "yesteryear" + MonthPeriod = "month" + YestermonthPeriod = "yestermonth" + QuarterPeriod = "quarter" + YesterquarterPeriod = "yesterquarter" + YearPeriod = "year" + YesteryearPeriod = "yesteryear" ) type DashboardPage struct { @@ -30,21 +33,27 @@ func ServeDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) http.Error(w, err.Error(), http.StatusBadRequest) return } - periodStart := "30 DAYS" - periodEnd := "0 DAYS" + periodStart := "date_trunc('month', current_date)::date" + periodEnd := "current_date" switch filters.Period.Selected { - case YearPeriod: - periodStart = "1 YEAR" + case YestermonthPeriod: + periodStart = "date_trunc('month', current_date - interval '1 month')::date" + periodEnd = "(date_trunc('month', current_date) - interval '1 day')::date" case QuarterPeriod: - periodStart = "3 MONTHS" + periodStart = "date_trunc('quarter', current_date)::date" + case YesterquarterPeriod: + periodStart = "date_trunc('quarter', current_date - interval '3 months')::date" + periodEnd = "(date_trunc('quarter', current_date) - interval '1 day')::date" + case YearPeriod: + periodStart = "date_trunc('year', current_date)::date" case YesteryearPeriod: - periodStart = "2 YEARS" - periodEnd = "1 YEAR" + periodStart = "date_trunc('year', current_date - interval '1 year')::date" + periodEnd = "(date_trunc('year', current_date) - interval '1 day')::date" case "": filters.Period.Selected = MonthPeriod } conn := getConn(r) - rows := conn.MustQuery(r.Context(), ` + rows := conn.MustQuery(r.Context(), fmt.Sprintf(` select to_price(0, decimal_digits) as sales , to_price(coalesce(invoice.total, 0), decimal_digits) as income , to_price(coalesce(expense.total, 0), decimal_digits) as expenses @@ -56,13 +65,13 @@ func ServeDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) select company_id, sum(total)::integer as total from invoice join invoice_amount using (invoice_id) - where invoice_date between CURRENT_DATE - $2::interval and CURRENT_DATE - $3::interval + where invoice_date between %[1]s and %[2]s group by company_id ) as invoice using (company_id) left join ( select company_id, sum(amount)::integer as total from expense - where invoice_date between CURRENT_DATE - $2::interval and CURRENT_DATE - $3::interval + where invoice_date between %[1]s and %[2]s group by company_id ) as expense using (company_id) left join ( @@ -73,7 +82,7 @@ func ServeDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) join invoice_tax_amount using (invoice_id) join tax using (tax_id) join tax_class using (tax_class_id) - where invoice_date between CURRENT_DATE - $2::interval and CURRENT_DATE - $3::interval + where invoice_date between %[1]s and %[2]s group by invoice.company_id ) as invoice_tax using (company_id) left join ( @@ -84,12 +93,12 @@ func ServeDashboard(w http.ResponseWriter, r *http.Request, _ httprouter.Params) join expense_tax_amount using (expense_id) join tax using (tax_id) join tax_class using (tax_class_id) - where invoice_date between CURRENT_DATE - $2::interval and CURRENT_DATE - $3::interval + where invoice_date between %[1]s and %[2]s group by expense.company_id ) as expense_tax using (company_id) join currency using (currency_code) where company_id = $1 - `, company.Id, periodStart, periodEnd) + `, periodStart, periodEnd), company.Id) defer rows.Close() dashboard := &DashboardPage{ @@ -122,19 +131,27 @@ func newDashboardFilterForm(locale *Locale, company *Company) *dashboardFilterFo Label: pgettext("input", "Period", locale), Options: []*RadioOption{ { - Label: pgettext("period option", "Year", locale), - Value: YearPeriod, + Label: pgettext("period option", "Month", locale), + Value: MonthPeriod, + }, + { + Label: pgettext("period option", "Previous month", locale), + Value: YestermonthPeriod, }, { Label: pgettext("period option", "Quarter", locale), Value: QuarterPeriod, }, { - Label: pgettext("period option", "Month", locale), - Value: MonthPeriod, + Label: pgettext("period option", "Previous quarter", locale), + Value: YesterquarterPeriod, }, { - Label: pgettext("period option", "Previous Year", locale), + Label: pgettext("period option", "Year", locale), + Value: YearPeriod, + }, + { + Label: pgettext("period option", "Previous year", locale), Value: YesteryearPeriod, }, }, diff --git a/po/ca.po b/po/ca.po index dff2842..6d18dcd 100644 --- a/po/ca.po +++ b/po/ca.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: numerus\n" "Report-Msgid-Bugs-To: jordi@tandem.blog\n" -"POT-Creation-Date: 2023-05-17 11:59+0200\n" +"POT-Creation-Date: 2023-05-19 13:55+0200\n" "PO-Revision-Date: 2023-01-18 17:08+0100\n" "Last-Translator: jordi fita mas \n" "Language-Team: Catalan \n" @@ -739,29 +739,39 @@ msgstr "La confirmació no és igual a la contrasenya." msgid "Selected language is not valid." msgstr "Heu seleccionat un idioma que no és vàlid." -#: pkg/dashboard.go:112 +#: pkg/dashboard.go:131 msgctxt "input" msgid "Period" msgstr "Període" -#: pkg/dashboard.go:115 -msgctxt "period option" -msgid "Year" -msgstr "Any" - -#: pkg/dashboard.go:119 -msgctxt "period option" -msgid "Quarter" -msgstr "Trimestre" - -#: pkg/dashboard.go:123 +#: pkg/dashboard.go:134 msgctxt "period option" msgid "Month" msgstr "Mes" -#: pkg/dashboard.go:127 +#: pkg/dashboard.go:138 msgctxt "period option" -msgid "Previous Year" +msgid "Previous month" +msgstr "Mes anterior" + +#: pkg/dashboard.go:142 +msgctxt "period option" +msgid "Quarter" +msgstr "Trimestre" + +#: pkg/dashboard.go:146 +msgctxt "period option" +msgid "Previous quarter" +msgstr "Trimestre anterior" + +#: pkg/dashboard.go:150 +msgctxt "period option" +msgid "Year" +msgstr "Any" + +#: pkg/dashboard.go:154 +msgctxt "period option" +msgid "Previous year" msgstr "Any anterior" #: pkg/expenses.go:129 diff --git a/po/es.po b/po/es.po index 591674b..6ad97b5 100644 --- a/po/es.po +++ b/po/es.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: numerus\n" "Report-Msgid-Bugs-To: jordi@tandem.blog\n" -"POT-Creation-Date: 2023-05-17 11:59+0200\n" +"POT-Creation-Date: 2023-05-19 13:55+0200\n" "PO-Revision-Date: 2023-01-18 17:45+0100\n" "Last-Translator: jordi fita mas \n" "Language-Team: Spanish \n" @@ -739,29 +739,39 @@ msgstr "La confirmación no corresponde con la contraseña." msgid "Selected language is not valid." msgstr "Habéis escogido un idioma que no es válido." -#: pkg/dashboard.go:112 +#: pkg/dashboard.go:131 msgctxt "input" msgid "Period" msgstr "Periodo" -#: pkg/dashboard.go:115 -msgctxt "period option" -msgid "Year" -msgstr "Año" - -#: pkg/dashboard.go:119 -msgctxt "period option" -msgid "Quarter" -msgstr "Trimestre" - -#: pkg/dashboard.go:123 +#: pkg/dashboard.go:134 msgctxt "period option" msgid "Month" msgstr "Mes" -#: pkg/dashboard.go:127 +#: pkg/dashboard.go:138 msgctxt "period option" -msgid "Previous Year" +msgid "Previous month" +msgstr "Mes anterior" + +#: pkg/dashboard.go:142 +msgctxt "period option" +msgid "Quarter" +msgstr "Trimestre" + +#: pkg/dashboard.go:146 +msgctxt "period option" +msgid "Previous quarter" +msgstr "Trimestre anterior" + +#: pkg/dashboard.go:150 +msgctxt "period option" +msgid "Year" +msgstr "Año" + +#: pkg/dashboard.go:154 +msgctxt "period option" +msgid "Previous year" msgstr "Año anterior" #: pkg/expenses.go:129