Add filter form to expenses
This commit is contained in:
parent
1415c3ef10
commit
664088c748
126
pkg/expenses.go
126
pkg/expenses.go
|
@ -7,6 +7,8 @@ import (
|
|||
"html/template"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -21,19 +23,56 @@ type ExpenseEntry struct {
|
|||
|
||||
type expensesIndexPage struct {
|
||||
Expenses []*ExpenseEntry
|
||||
Filters *expenseFilterForm
|
||||
}
|
||||
|
||||
func IndexExpenses(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
||||
conn := getConn(r)
|
||||
locale := getLocale(r)
|
||||
company := mustGetCompany(r)
|
||||
filters := newExpenseFilterForm(r.Context(), conn, locale, company)
|
||||
if err := filters.Parse(r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
page := &expensesIndexPage{
|
||||
Expenses: mustCollectExpenseEntries(r.Context(), conn, company),
|
||||
Expenses: mustCollectExpenseEntries(r.Context(), conn, company, filters),
|
||||
Filters: filters,
|
||||
}
|
||||
mustRenderMainTemplate(w, r, "expenses/index.gohtml", page)
|
||||
}
|
||||
|
||||
func mustCollectExpenseEntries(ctx context.Context, conn *Conn, company *Company) []*ExpenseEntry {
|
||||
rows, err := conn.Query(ctx, `
|
||||
func mustCollectExpenseEntries(ctx context.Context, conn *Conn, company *Company, filters *expenseFilterForm) []*ExpenseEntry {
|
||||
args := []interface{}{company.Id}
|
||||
where := []string{"expense.company_id = $1"}
|
||||
appendWhere := func(expression string, value interface{}) {
|
||||
args = append(args, value)
|
||||
where = append(where, fmt.Sprintf(expression, len(args)))
|
||||
}
|
||||
maybeAppendWhere := func(expression string, value string, conv func(string) interface{}) {
|
||||
if value != "" {
|
||||
if conv == nil {
|
||||
appendWhere(expression, value)
|
||||
} else {
|
||||
appendWhere(expression, conv(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
maybeAppendWhere("contact_id = $%d", filters.Customer.String(), func(v string) interface{} {
|
||||
customerId, _ := strconv.Atoi(filters.Customer.Selected[0])
|
||||
return customerId
|
||||
})
|
||||
maybeAppendWhere("invoice_number = $%d", filters.InvoiceNumber.String(), nil)
|
||||
maybeAppendWhere("invoice_date >= $%d", filters.FromDate.String(), nil)
|
||||
maybeAppendWhere("invoice_date <= $%d", filters.ToDate.String(), nil)
|
||||
if len(filters.Tags.Tags) > 0 {
|
||||
if filters.TagsCondition.Selected == "and" {
|
||||
appendWhere("expense.tags @> $%d", filters.Tags)
|
||||
} else {
|
||||
appendWhere("expense.tags && $%d", filters.Tags)
|
||||
}
|
||||
}
|
||||
rows := conn.MustQuery(ctx, fmt.Sprintf(`
|
||||
select expense.slug
|
||||
, invoice_date
|
||||
, invoice_number
|
||||
|
@ -43,19 +82,15 @@ func mustCollectExpenseEntries(ctx context.Context, conn *Conn, company *Company
|
|||
from expense
|
||||
join contact using (contact_id)
|
||||
join currency using (currency_code)
|
||||
where expense.company_id = $1
|
||||
where (%s)
|
||||
order by invoice_date
|
||||
`, company.Id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
`, strings.Join(where, ") AND (")), args...)
|
||||
defer rows.Close()
|
||||
|
||||
var entries []*ExpenseEntry
|
||||
for rows.Next() {
|
||||
entry := &ExpenseEntry{}
|
||||
err = rows.Scan(&entry.Slug, &entry.InvoiceDate, &entry.InvoiceNumber, &entry.Amount, &entry.InvoicerName, &entry.Tags)
|
||||
if err != nil {
|
||||
if err := rows.Scan(&entry.Slug, &entry.InvoiceDate, &entry.InvoiceNumber, &entry.Amount, &entry.InvoicerName, &entry.Tags); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
entries = append(entries, entry)
|
||||
|
@ -266,3 +301,74 @@ func HandleUpdateExpense(w http.ResponseWriter, r *http.Request, params httprout
|
|||
}
|
||||
htmxRedirect(w, r, companyURI(company, "/expenses"))
|
||||
}
|
||||
|
||||
type expenseFilterForm struct {
|
||||
locale *Locale
|
||||
company *Company
|
||||
Customer *SelectField
|
||||
InvoiceNumber *InputField
|
||||
FromDate *InputField
|
||||
ToDate *InputField
|
||||
Tags *TagsField
|
||||
TagsCondition *ToggleField
|
||||
}
|
||||
|
||||
func newExpenseFilterForm(ctx context.Context, conn *Conn, locale *Locale, company *Company) *expenseFilterForm {
|
||||
return &expenseFilterForm{
|
||||
locale: locale,
|
||||
company: company,
|
||||
Customer: &SelectField{
|
||||
Name: "customer",
|
||||
Label: pgettext("input", "Customer", locale),
|
||||
EmptyLabel: gettext("All customers", locale),
|
||||
Options: mustGetContactOptions(ctx, conn, company),
|
||||
},
|
||||
InvoiceNumber: &InputField{
|
||||
Name: "number",
|
||||
Label: pgettext("input", "Invoice Number", locale),
|
||||
Type: "search",
|
||||
},
|
||||
FromDate: &InputField{
|
||||
Name: "from_date",
|
||||
Label: pgettext("input", "From Date", locale),
|
||||
Type: "date",
|
||||
},
|
||||
ToDate: &InputField{
|
||||
Name: "to_date",
|
||||
Label: pgettext("input", "To Date", locale),
|
||||
Type: "date",
|
||||
},
|
||||
Tags: &TagsField{
|
||||
Name: "tags",
|
||||
Label: pgettext("input", "Tags", locale),
|
||||
},
|
||||
TagsCondition: &ToggleField{
|
||||
Name: "tags_condition",
|
||||
Label: pgettext("input", "Tags Condition", locale),
|
||||
Selected: "and",
|
||||
FirstOption: &ToggleOption{
|
||||
Value: "and",
|
||||
Label: pgettext("tag condition", "All", locale),
|
||||
Description: gettext("Invoices must have all the specified labels.", locale),
|
||||
},
|
||||
SecondOption: &ToggleOption{
|
||||
Value: "or",
|
||||
Label: pgettext("tag condition", "Any", locale),
|
||||
Description: gettext("Invoices must have at least one of the specified labels.", locale),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (form *expenseFilterForm) Parse(r *http.Request) error {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
return err
|
||||
}
|
||||
form.Customer.FillValue(r)
|
||||
form.InvoiceNumber.FillValue(r)
|
||||
form.FromDate.FillValue(r)
|
||||
form.ToDate.FillValue(r)
|
||||
form.Tags.FillValue(r)
|
||||
form.TagsCondition.FillValue(r)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -19,6 +19,21 @@
|
|||
|
||||
{{ define "content" }}
|
||||
{{- /*gotype: dev.tandem.ws/tandem/numerus/pkg.expensesIndexPage*/ -}}
|
||||
<div aria-label="{{( pgettext "Filters" "title" )}}">
|
||||
<form method="GET" action="{{ companyURI "/expenses"}}"
|
||||
data-hx-target="main" data-hx-boost="true" data-hx-trigger="change,search,submit"
|
||||
>
|
||||
{{ with .Filters }}
|
||||
{{ template "select-field" .Customer }}
|
||||
{{ template "input-field" .FromDate }}
|
||||
{{ template "input-field" .ToDate }}
|
||||
{{ template "input-field" .InvoiceNumber }}
|
||||
{{ template "tags-field" .Tags | addTagsAttr (print `data-conditions="` .TagsCondition.Name `-field"`) }}
|
||||
{{ template "toggle-field" .TagsCondition }}
|
||||
{{ end }}
|
||||
<button type="submit">{{( pgettext "Filter" "action" )}}</button>
|
||||
</form>
|
||||
</div>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
|
|
Loading…
Reference in New Issue