increment_money_pots.sql (2862B)
1 -- 2 -- This file is part of TALER 3 -- Copyright (C) 2025 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 18 DROP FUNCTION IF EXISTS merchant_do_increment_money_pots; 19 CREATE FUNCTION merchant_do_increment_money_pots ( 20 IN ina_money_pots_ids INT8[], 21 IN ina_increments merchant.taler_amount_currency[], 22 OUT out_not_found BOOL) 23 LANGUAGE plpgsql 24 AS $$ 25 DECLARE 26 i INT; 27 ini_current_pot_id INT8; 28 ini_current_increment merchant.taler_amount_currency; 29 my_totals merchant.taler_amount_currency[]; 30 currency_found BOOL; 31 j INT; 32 BEGIN 33 34 out_not_found = FALSE; 35 36 IF ( COALESCE(array_length(ina_money_pots_ids, 1), 0) != 37 COALESCE(array_length(ina_increments, 1), 0) ) 38 THEN 39 RAISE EXCEPTION 'Array lengths must match'; 40 END IF; 41 42 FOR i IN 1..COALESCE(array_length(ina_money_pots_ids, 1),0) 43 LOOP 44 ini_current_pot_id = ina_money_pots_ids[i]; 45 ini_current_increment = ina_increments[i]; 46 47 SELECT pot_totals 48 INTO my_totals 49 FROM merchant_money_pots 50 WHERE money_pot_serial = ini_current_pot_id; 51 52 IF NOT FOUND 53 THEN 54 -- If pot does not exist, we just ignore the entire 55 -- requested increment, but update the return value. 56 -- (We may have other pots to update, so we continue 57 -- to iterate!). 58 out_not_found = TRUE; 59 ELSE 60 -- Check if currency exists in pot_totals and update 61 currency_found = FALSE; 62 63 FOR j IN 1..COALESCE(array_length(my_totals, 1), 0) 64 LOOP 65 IF (my_totals[j]).curr = (ini_current_increment).curr 66 THEN 67 my_totals[j].frac 68 = my_totals[j].frac + ini_current_increment.frac; 69 my_totals[j].val 70 = my_totals[j].val + ini_current_increment.val; 71 IF my_totals[j].frac >= 100000000 72 THEN 73 my_totals[j].frac = my_totals[j].frac - 100000000; 74 my_totals[j].val = my_totals[j].val + 1; 75 END IF; 76 currency_found = TRUE; 77 EXIT; -- break out of loop 78 END IF; 79 END LOOP; 80 81 IF NOT currency_found 82 THEN 83 my_totals = array_append(my_totals, ini_current_increment); 84 END IF; 85 86 UPDATE merchant_money_pots 87 SET pot_totals = my_totals 88 WHERE money_pot_serial = ini_current_pot_id; 89 90 END IF; 91 END LOOP; 92 93 END $$;