From 32fdab42170c7324191f61afe3b98869c2fca423 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Tue, 21 Feb 2023 13:57:40 +0100 Subject: [PATCH] Reindex product indices when removing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When updating the product list, i forgot to change the index in the product field’s names and, therefore, i created invoices with only the products until the first gap. --- pkg/invoices.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pkg/invoices.go b/pkg/invoices.go index 0ab895d..170085d 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -264,11 +264,12 @@ func (form *invoiceForm) Validate() bool { func (form *invoiceForm) Update() { products := form.Products form.Products = nil - index := 0 - for _, product := range products { + for n, product := range products { if product.Quantity.Val != "0" { + if n != len(form.Products) { + product.Reindex(len(form.Products)) + } form.Products = append(form.Products, product) - index++ } } } @@ -290,29 +291,24 @@ type invoiceProductForm struct { } func newInvoiceProductForm(index int, company *Company, locale *Locale, taxOptions []*SelectOption) *invoiceProductForm { - suffix := "." + strconv.Itoa(index) - return &invoiceProductForm{ + form := &invoiceProductForm{ locale: locale, company: company, ProductId: &InputField{ - Name: "product.id" + suffix, Label: pgettext("input", "Id", locale), Type: "hidden", Required: true, }, Name: &InputField{ - Name: "product.name" + suffix, Label: pgettext("input", "Name", locale), Type: "text", Required: true, }, Description: &InputField{ - Name: "product.description" + suffix, Label: pgettext("input", "Description", locale), Type: "textarea", }, Price: &InputField{ - Name: "product.price" + suffix, Label: pgettext("input", "Price", locale), Type: "number", Required: true, @@ -322,7 +318,6 @@ func newInvoiceProductForm(index int, company *Company, locale *Locale, taxOptio }, }, Quantity: &InputField{ - Name: "product.quantity" + suffix, Label: pgettext("input", "Quantity", locale), Type: "number", Required: true, @@ -331,7 +326,6 @@ func newInvoiceProductForm(index int, company *Company, locale *Locale, taxOptio }, }, Discount: &InputField{ - Name: "product.discount" + suffix, Label: pgettext("input", "Discount (%)", locale), Type: "number", Required: true, @@ -341,12 +335,24 @@ func newInvoiceProductForm(index int, company *Company, locale *Locale, taxOptio }, }, Tax: &SelectField{ - Name: "product.tax" + suffix, Label: pgettext("input", "Taxes", locale), Multiple: true, Options: taxOptions, }, } + form.Reindex(index) + return form +} + +func (form *invoiceProductForm) Reindex(index int) { + suffix := "." + strconv.Itoa(index) + form.ProductId.Name = "product.id" + suffix + form.Name.Name = "product.name" + suffix + form.Description.Name = "product.description" + suffix + form.Price.Name = "product.price" + suffix + form.Quantity.Name = "product.quantity" + suffix + form.Discount.Name = "product.discount" + suffix + form.Tax.Name = "product.tax" + suffix } func (form *invoiceProductForm) Parse(r *http.Request) error {