exchange

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

exchange_do_select_deposits_missing_wire.sql (2129B)


      1 --
      2 -- This file is part of TALER
      3 -- Copyright (C) 2023 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 -- @author: Christian Grothoff
     17 
     18 CREATE OR REPLACE FUNCTION exchange_do_select_deposits_missing_wire(
     19   IN in_min_serial_id INT8)
     20 RETURNS SETOF exchange_do_select_deposits_missing_wire_return_type
     21 LANGUAGE plpgsql
     22 AS $$
     23 DECLARE
     24   missing CURSOR
     25   FOR
     26   SELECT
     27     batch_deposit_serial_id
     28    ,wire_target_h_payto
     29    ,wire_deadline
     30     FROM batch_deposits
     31     WHERE batch_deposit_serial_id > in_min_serial_id
     32     ORDER BY batch_deposit_serial_id ASC;
     33 DECLARE
     34   my_total_val INT8; -- all deposits without wire
     35 DECLARE
     36   my_total_frac INT8; -- all deposits without wire (fraction, not normalized)
     37 DECLARE
     38   my_total taler_amount; -- amount that was originally deposited
     39 DECLARE
     40   my_batch_record RECORD;
     41 DECLARE
     42   i RECORD;
     43 BEGIN
     44 
     45 OPEN missing;
     46 LOOP
     47   FETCH NEXT FROM missing INTO i;
     48   EXIT WHEN NOT FOUND;
     49 
     50   SELECT
     51     SUM((cdep.amount_with_fee).val) AS total_val
     52    ,SUM((cdep.amount_with_fee).frac::INT8) AS total_frac
     53     INTO
     54       my_batch_record
     55     FROM coin_deposits cdep
     56     WHERE cdep.batch_deposit_serial_id = i.batch_deposit_serial_id;
     57 
     58   my_total_val=my_batch_record.total_val;
     59   my_total_frac=my_batch_record.total_frac;
     60 
     61   -- Normalize total amount
     62   my_total.val = my_total_val + my_total_frac / 100000000;
     63   my_total.frac = my_total_frac % 100000000;
     64   RETURN NEXT (
     65        i.batch_deposit_serial_id
     66       ,my_total
     67       ,i.wire_target_h_payto
     68       ,i.wire_deadline);
     69 
     70 END LOOP;
     71 CLOSE missing;
     72 RETURN;
     73 END $$;