exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

exchange_do_batch_reserves_update.sql (2258B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2014--2022 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 exchange_do_batch_reserves_update(
     18   IN in_reserve_pub BYTEA,
     19   IN in_expiration_date INT8,
     20   IN in_wire_ref INT8,
     21   IN in_credit taler_amount,
     22   IN in_exchange_account_name TEXT,
     23   IN in_wire_source_h_payto BYTEA,
     24   IN in_notify text,
     25   OUT out_duplicate BOOLEAN)
     26 LANGUAGE plpgsql
     27 AS $$
     28 BEGIN
     29   INSERT INTO reserves_in
     30     (reserve_pub
     31     ,wire_reference
     32     ,credit
     33     ,exchange_account_section
     34     ,wire_source_h_payto
     35     ,execution_date)
     36     VALUES
     37     (in_reserve_pub
     38     ,in_wire_ref
     39     ,in_credit
     40     ,in_exchange_account_name
     41     ,in_wire_source_h_payto
     42     ,in_expiration_date)
     43     ON CONFLICT DO NOTHING;
     44   IF FOUND
     45   THEN
     46     --IF THE INSERTION WAS A SUCCESS IT MEANS NO DUPLICATED TRANSACTION
     47     out_duplicate = FALSE;
     48     UPDATE reserves rs
     49       SET
     50          current_balance.frac = (rs.current_balance).frac+in_credit.frac
     51            - CASE
     52              WHEN (rs.current_balance).frac + in_credit.frac >= 100000000
     53                THEN 100000000
     54              ELSE 1
     55              END
     56         ,current_balance.val = (rs.current_balance).val+in_credit.val
     57            + CASE
     58              WHEN (rs.current_balance).frac + in_credit.frac >= 100000000
     59                THEN 1
     60              ELSE 0
     61              END
     62              ,expiration_date=GREATEST(expiration_date,in_expiration_date)
     63              ,gc_date=GREATEST(gc_date,in_expiration_date)
     64    	        WHERE reserve_pub=in_reserve_pub;
     65     EXECUTE FORMAT (
     66       'NOTIFY %s'
     67       ,in_notify);
     68   ELSE
     69     out_duplicate = TRUE;
     70   END IF;
     71   RETURN;
     72 END $$;