From e93a798223e1f263b43a9e161c1b06500b3a6954 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Mon, 24 Apr 2023 20:40:10 +0200 Subject: [PATCH] =?UTF-8?q?Accept=20=E2=80=9Cinvalid=E2=80=9D=20quantity,?= =?UTF-8?q?=20price,=20and=20discount=20on=20invoice=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is for “free products”, where the user adds an invoice row, does not select a product from the search, and clicks the update button. Numerus should select “appropriate” values for those that are left unspecified. I also no longer require the product_id to be an integer; if it is empty, then it is assumed to be a “free product”. --- pkg/invoices.go | 21 ++++++++++++++++++++- pkg/pgtypes.go | 6 +++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/invoices.go b/pkg/invoices.go index 79263a7..be2d762 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -661,6 +661,7 @@ func (form *invoiceForm) Update() { form.Products = nil for n, product := range products { if product.Quantity.Val != "0" { + product.Update() if n != len(form.Products) { product.Index = len(form.Products) product.Rename() @@ -835,7 +836,9 @@ func (form *invoiceProductForm) Parse(r *http.Request) error { func (form *invoiceProductForm) Validate() bool { validator := newFormValidator() - validator.CheckRequiredInput(form.ProductId, gettext("Product ID can not be empty.", form.locale)) + if form.InvoiceProductId.Val != "" { + validator.CheckValidInteger(form.ProductId, 1, math.MaxInt32, gettext("Product ID must be a number greater than zero.", form.locale)) + } validator.CheckRequiredInput(form.Name, gettext("Name can not be empty.", form.locale)) if validator.CheckRequiredInput(form.Price, gettext("Price can not be empty.", form.locale)) { validator.CheckValidDecimal(form.Price, form.company.MinCents(), math.MaxFloat64, gettext("Price must be a number greater than zero.", form.locale)) @@ -851,6 +854,22 @@ func (form *invoiceProductForm) Validate() bool { return validator.AllOK() } +func (form *invoiceProductForm) Update() { + validator := newFormValidator() + if !validator.CheckValidDecimal(form.Price, form.company.MinCents(), math.MaxFloat64, "") { + form.Price.Val = "0.0" + form.Price.Errors = nil + } + if !validator.CheckValidInteger(form.Quantity, 0, math.MaxInt32, "") { + form.Quantity.Val = "1" + form.Quantity.Errors = nil + } + if !validator.CheckValidInteger(form.Discount, 0, 100, "") { + form.Discount.Val = "0" + form.Discount.Errors = nil + } +} + func (form *invoiceProductForm) MustFillFromDatabase(ctx context.Context, conn *Conn, slug string) bool { return !notFoundErrorOrPanic(conn.QueryRow(ctx, ` select product_id diff --git a/pkg/pgtypes.go b/pkg/pgtypes.go index 80bd8b9..9582974 100644 --- a/pkg/pgtypes.go +++ b/pkg/pgtypes.go @@ -17,8 +17,12 @@ func (src NewInvoiceProductArray) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) } var values [][]interface{} for _, form := range src { + var productId interface{} = form.ProductId.Val + if form.ProductId.Val == "" { + productId = nil + } values = append(values, []interface{}{ - form.ProductId.Val, + productId, form.Name.Val, form.Description.Val, form.Price.Val,