-- Deploy camper:tax to pg
-- requires: roles
-- requires: schema_camper
-- requires: user_profile
-- requires: company
-- requires: tax_rate
-- requires: tax_class

begin;

set search_path to camper, public;

create table tax (
	tax_id integer generated by default as identity primary key,
	company_id integer not null references company,
	tax_class_id integer not null references tax_class,
	name text not null constraint name_not_empty check(length(trim(name)) > 0),
	rate tax_rate not null
);

grant select, insert, update, delete on table tax to employee;
grant select, insert, update, delete on table tax to admin;

alter table tax enable row level security;

create policy company_policy
on tax
using (
	exists(
		select 1
		from company_user
		join user_profile using (user_id)
		where company_user.company_id = tax.company_id
	)
);

commit;
