-- Deploy numerus:quote_product_amount to pg
-- requires: roles
-- requires: schema_numerus
-- requires: quote_product
-- requires: quote_product_tax

begin;

set search_path to numerus, public;

create or replace view quote_product_amount as
select quote_product_id
     , round(price * quantity * (1 - discount_rate))::integer as subtotal
     , max(round(price * quantity * (1 - discount_rate))::integer) + coalesce(sum(round(round(price * quantity * (1 - discount_rate))::integer * tax_rate)::integer)::integer, 0) as total
from quote_product
left join quote_product_tax using (quote_product_id)
group by quote_product_id, price, quantity, discount_rate
;

grant select on table quote_product_amount to invoicer;
grant select on table quote_product_amount to admin;

commit;
