donau_do_insert_issued_receipts.sql (3131B)
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 DROP FUNCTION IF EXISTS do_insert_issued_receipts; 18 CREATE FUNCTION do_insert_issued_receipts ( 19 IN in_charity_id BIGINT -- charity id which made the issue receipts request 20 ,IN in_blinded_sig BYTEA[] -- blinded signatures 21 ,IN in_receipt_hash BYTEA -- hash over all budi key pairs (primary key) 22 ,IN in_amount taler_amount -- total amount of the requested receipts 23 ,IN in_year INT4 24 ,OUT out_smaller_than_max_per_year BOOLEAN 25 ) 26 LANGUAGE plpgsql 27 AS $$ 28 DECLARE 29 old_receipts_to_date taler_amount; 30 new_receipts_to_date taler_amount; 31 max_per_year taler_amount; 32 my_year INT4; 33 BEGIN 34 -- Get charity values 35 SELECT 36 (chari.receipts_to_date).val 37 ,(chari.receipts_to_date).frac 38 ,(chari.max_per_year).val 39 ,(chari.max_per_year).frac 40 ,current_year 41 INTO 42 old_receipts_to_date.val 43 ,old_receipts_to_date.frac 44 ,max_per_year.val 45 ,max_per_year.frac 46 ,my_year 47 FROM charities chari 48 WHERE charity_id=in_charity_id; 49 50 IF (my_year > in_year) 51 THEN 52 -- server travelled back in time? wild, but 53 -- we closed the books on the previous year, 54 -- so assume balance was exceeded. 55 out_smaller_than_max_per_year = FALSE; 56 RETURN; 57 END IF; 58 IF (my_year < in_year) 59 THEN 60 -- bump current year 61 old_receipts_to_date.val = 0; 62 old_receipts_to_date.frac = 0; 63 END IF; 64 65 -- calculate sum of the recent amount of receipts and the issued amount 66 SELECT * 67 FROM amount_add(old_receipts_to_date, in_amount) 68 INTO new_receipts_to_date; 69 -- check if the new receipts to date is below or equal the limit for the charity 70 IF ( (max_per_year.val > new_receipts_to_date.val) OR 71 ( (max_per_year.val = new_receipts_to_date.val) AND 72 (max_per_year.frac >= new_receipts_to_date.frac) ) ) 73 THEN 74 out_smaller_than_max_per_year=TRUE; 75 UPDATE charities 76 SET receipts_to_date=new_receipts_to_date, 77 current_year=in_year 78 WHERE charity_id=in_charity_id; 79 INSERT INTO receipts_issued ( 80 blinded_sig 81 ,charity_id 82 ,receipt_hash 83 ,amount 84 ) VALUES ( 85 in_blinded_sig 86 ,in_charity_id 87 ,in_receipt_hash 88 ,in_amount 89 ); 90 ELSE 91 out_smaller_than_max_per_year=FALSE; 92 END IF; 93 END $$; 94 95 COMMENT ON FUNCTION do_insert_issued_receipts 96 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';