numerus/deploy/remove_collection.sql
jordi fita mas ebb073166a Subsume collections into payments, and record payments in negative
This is to handle refunds, which are invoices with negative amounts,
that can be both issued or received (i.e., an “expense”).

The API provided by PostgreSQL is mostly the same, and internally it
deals with negatives, so the Go package only had to change selects of
collection.
2025-01-30 23:24:16 +01:00

41 lines
1002 B
PL/PgSQL

-- Deploy numerus:remove_collection to pg
-- requires: roles
-- requires: schema_numerus
-- requires: invoice_payment
-- requires: payment
-- requires: payment_attachment
-- requires: update_invoice_collection_status
begin;
set search_path to numerus, public;
create or replace function remove_collection(collection_slug uuid) returns void as
$$
declare
cid integer;
iid integer;
begin
select payment_id into cid from payment where slug = collection_slug;
if not found then
return;
end if;
delete from invoice_payment where payment_id = cid returning invoice_id into iid;
if iid is not null then
perform update_invoice_collection_status(null, iid, 0);
end if;
delete from payment_attachment where payment_id = cid;
delete from payment where payment_id = cid;
end
$$
language plpgsql
;
revoke execute on function remove_collection(uuid) from public;
grant execute on function remove_collection(uuid) to invoicer;
grant execute on function remove_collection(uuid) to admin;
commit;