donau_do_insert_issued_receipts.sql (2578B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2024 Taler Systems SA 4 -- 5 -- TALER is free software; you can redistribute it and/or modify it under the 6 -- terms of the GNU General Public License as published by the Free Software 7 -- Foundation; either version 3, or (at your option) any later version. 8 -- 9 -- TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 -- A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 -- 13 -- You should have received a copy of the GNU General Public License along with 14 -- TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 -- 16 17 CREATE OR REPLACE FUNCTION do_insert_issued_receipts ( 18 IN in_charity_id BIGINT -- charity id which made the issue receitps request 19 ,IN in_blinded_sig BYTEA[] -- blinded signatures 20 ,IN in_receipt_hash BYTEA -- hash over all budi key pairs (primary key) 21 ,IN in_amount taler_amount -- total amount of the requested receipts 22 ,OUT out_smaller_than_max_per_year BOOLEAN 23 ) 24 LANGUAGE plpgsql 25 AS $$ 26 DECLARE 27 old_receipts_to_date taler_amount; 28 new_receipts_to_date taler_amount; 29 max_per_year taler_amount; 30 BEGIN 31 -- Get charity values 32 SELECT 33 (chari.receipts_to_date).val 34 ,(chari.receipts_to_date).frac 35 ,(chari.max_per_year).val 36 ,(chari.max_per_year).frac 37 INTO 38 old_receipts_to_date.val 39 ,old_receipts_to_date.frac 40 ,max_per_year.val 41 ,max_per_year.frac 42 FROM charities chari 43 WHERE charity_id=in_charity_id; 44 -- calculate sum of the recent amount of receipts and the issued amount 45 SELECT * 46 FROM amount_add(old_receipts_to_date, in_amount) 47 INTO new_receipts_to_date; 48 -- check if the new receipts to date is below or equal the limit for the charity 49 IF ( (max_per_year.val > new_receipts_to_date.val) OR 50 ( (max_per_year.val = new_receipts_to_date.val) AND 51 (max_per_year.frac > new_receipts_to_date.frac) ) ) 52 THEN 53 out_smaller_than_max_per_year=TRUE; 54 UPDATE charities 55 SET receipts_to_date=new_receipts_to_date 56 WHERE charity_id=in_charity_id; 57 INSERT INTO receipts_issued (blinded_sig, charity_id, receipt_hash, amount) 58 VALUES (in_blinded_sig, in_charity_id, in_receipt_hash, in_amount); 59 ELSE 60 out_smaller_than_max_per_year=FALSE; 61 END IF; 62 END $$; 63 COMMIT; 64 65 COMMENT ON FUNCTION do_insert_issued_receipts 66 IS 'This is a transaction for updating the current amount of receipts of a year of a charity and saves the receipts request what makes it idempotent';