numerus/deploy/invoice_amount.sql

33 lines
751 B
MySQL
Raw Normal View History

-- Deploy numerus:invoice_amount to pg
-- requires: schema_numerus
-- requires: invoice_product
-- requires: invoice_tax_amount
begin;
set search_path to numerus, public;
create or replace view invoice_amount as
with taxable as (
select invoice_id
, sum(round(price * quantity * (1 - discount_rate))::integer)::integer as subtotal
from invoice_product
group by invoice_id
), taxes as (
select invoice_id
, sum(amount)::integer as tax_amount
from invoice_tax_amount
group by invoice_id
)
select invoice_id
, subtotal
, subtotal + coalesce(tax_amount, 0) as total
from taxable
left join taxes using (invoice_id)
;
grant select on table invoice_amount to invoicer;
grant select on table invoice_amount to admin;
commit;