diff --git a/deploy/edit_invoice.sql b/deploy/edit_invoice.sql index 39551ff..5f4ab53 100644 --- a/deploy/edit_invoice.sql +++ b/deploy/edit_invoice.sql @@ -20,6 +20,7 @@ $$ declare iid integer; products_to_keep integer[]; + products_to_delete integer[]; company integer; ccode text; product edited_invoice_product; @@ -78,7 +79,16 @@ begin join unnest(product.tax) as ptax(tax_id) using (tax_id); end loop; - delete from invoice_product where invoice_id = iid and not (invoice_product_id = any(products_to_keep)); + select array_agg(invoice_product_id) + into products_to_delete + from invoice_product + where invoice_id = iid + and not (invoice_product_id = any(products_to_keep)); + + if array_length(products_to_delete, 1) > 0 then + delete from invoice_product_tax where invoice_product_id = any(products_to_delete); + delete from invoice_product where invoice_product_id = any(products_to_delete); + end if; delete from invoice_tag where invoice_id = iid; diff --git a/test/edit_invoice.sql b/test/edit_invoice.sql index 4f35157..6720723 100644 --- a/test/edit_invoice.sql +++ b/test/edit_invoice.sql @@ -93,7 +93,8 @@ values (19, 15, 7, 'P1.0', 1100) ; insert into invoice_product_tax (invoice_product_id, tax_id, tax_rate) -values (20, 4, 0.21) +values (19, 4, 0.21) + , (20, 4, 0.21) , (21, 3, -0.07) , (21, 4, 0.21) , (22, 3, -0.15)