From 1c6375b51deef92ff93c0af365105186c1198498 Mon Sep 17 00:00:00 2001 From: jordi fita mas Date: Fri, 11 Aug 2023 19:47:10 +0200 Subject: [PATCH] =?UTF-8?q?Do=20not=20give=20=E2=80=9Cfalse=20ID=E2=80=9D?= =?UTF-8?q?=20to=20invoice=20products=20that=20come=20from=20quotations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When adding “free-form products” to quotes they do not have a product ID, but i has coalescing the NULL to zero because product_id is an integer and can not coalesce a nullable integer to an empty string. However, that causes problems when trying to create the invoice for that quote, because it tries to add products that have an ID of 0 and the foreign key, obviously, fail. At first i modified NewInvoiceProductArray.EncodeBinary to check for "0" as well as the empty string, but i realized this was wrong: the problem was because i gave these products an ID when they do not have any. And the solution is to cast product_id to a text, which is what will get converted anyway because i the only thing i do to it is to store to a string-backed InputForm field. Closes #73. --- pkg/invoices.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/invoices.go b/pkg/invoices.go index a23e445..358028c 100644 --- a/pkg/invoices.go +++ b/pkg/invoices.go @@ -958,7 +958,7 @@ func (form *invoiceForm) MustFillFromQuote(ctx context.Context, conn *Conn, slug return false } form.Products = []*invoiceProductForm{} - form.mustAddProductsFromQuery(ctx, conn, "select '', coalesce(product_id, 0), name, description, to_price(price, $2), quantity, (discount_rate * 100)::integer, array_remove(array_agg(tax_id), null) from quote_product left join quote_product_product using (quote_product_id) left join quote_product_tax using (quote_product_id) where quote_id = $1 group by quote_product_id, coalesce(product_id, 0), name, description, discount_rate, price, quantity", quoteId, form.company.DecimalDigits) + form.mustAddProductsFromQuery(ctx, conn, "select '', coalesce(product_id::text, ''), name, description, to_price(price, $2), quantity, (discount_rate * 100)::integer, array_remove(array_agg(tax_id), null) from quote_product left join quote_product_product using (quote_product_id) left join quote_product_tax using (quote_product_id) where quote_id = $1 group by quote_product_id, coalesce(product_id::text, ''), name, description, discount_rate, price, quantity", quoteId, form.company.DecimalDigits) return true }