Accept “invalid” quantity, price, and discount on invoice update

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”.
This commit is contained in:
jordi fita mas 2023-04-24 20:40:10 +02:00
parent 7d895fe5f9
commit e93a798223
2 changed files with 25 additions and 2 deletions

View File

@ -661,6 +661,7 @@ func (form *invoiceForm) Update() {
form.Products = nil form.Products = nil
for n, product := range products { for n, product := range products {
if product.Quantity.Val != "0" { if product.Quantity.Val != "0" {
product.Update()
if n != len(form.Products) { if n != len(form.Products) {
product.Index = len(form.Products) product.Index = len(form.Products)
product.Rename() product.Rename()
@ -835,7 +836,9 @@ func (form *invoiceProductForm) Parse(r *http.Request) error {
func (form *invoiceProductForm) Validate() bool { func (form *invoiceProductForm) Validate() bool {
validator := newFormValidator() 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)) 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)) { 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)) 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() 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 { func (form *invoiceProductForm) MustFillFromDatabase(ctx context.Context, conn *Conn, slug string) bool {
return !notFoundErrorOrPanic(conn.QueryRow(ctx, ` return !notFoundErrorOrPanic(conn.QueryRow(ctx, `
select product_id select product_id

View File

@ -17,8 +17,12 @@ func (src NewInvoiceProductArray) EncodeBinary(ci *pgtype.ConnInfo, buf []byte)
} }
var values [][]interface{} var values [][]interface{}
for _, form := range src { for _, form := range src {
var productId interface{} = form.ProductId.Val
if form.ProductId.Val == "" {
productId = nil
}
values = append(values, []interface{}{ values = append(values, []interface{}{
form.ProductId.Val, productId,
form.Name.Val, form.Name.Val,
form.Description.Val, form.Description.Val,
form.Price.Val, form.Price.Val,