merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

pg_update_money_pot.sql (2370B)


      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_update_money_pot;
     19 CREATE FUNCTION merchant_do_update_money_pot (
     20   IN in_instance_id TEXT,
     21   IN in_money_pot_serial INT8,
     22   IN in_name TEXT,
     23   IN in_description TEXT,
     24   IN in_old_totals taler_amount_currency[], -- can be NULL!
     25   IN in_new_totals taler_amount_currency[], -- can be NULL!
     26   OUT out_conflict_total BOOL,
     27   OUT out_conflict_name BOOL,
     28   OUT out_not_found BOOL)
     29 LANGUAGE plpgsql
     30 AS $$
     31 DECLARE
     32   my_merchant_id INT8;
     33 BEGIN
     34 
     35 SELECT merchant_serial
     36   INTO my_merchant_id
     37   FROM merchant_instances
     38   WHERE merchant_id=in_instance_id;
     39 
     40 IF NOT FOUND
     41 THEN
     42   -- If instance does not exist, pot cannot exist
     43   out_conflict_total = FALSE;
     44   out_conflict_name = FALSE;
     45   out_not_found = TRUE;
     46   RETURN;
     47 END IF;
     48 
     49 BEGIN
     50   UPDATE merchant_money_pots SET
     51     money_pot_name=in_name
     52    ,money_pot_description=in_description
     53    ,pot_totals=COALESCE(in_new_totals, pot_totals)
     54    WHERE merchant_serial=my_merchant_id
     55      AND money_pot_serial=in_money_pot_serial
     56      AND ( (in_old_totals IS NULL) OR (pot_totals=in_old_totals) );
     57   IF NOT FOUND
     58   THEN
     59     -- Check if pot_total was the problem
     60     PERFORM FROM merchant_money_pots
     61            WHERE merchant_serial=my_merchant_id
     62              AND money_pot_serial=in_money_pot_serial;
     63     out_conflict_total = FOUND;
     64     out_not_found = NOT FOUND;
     65     out_conflict_name = FALSE;
     66   ELSE
     67     out_conflict_total = FALSE;
     68     out_not_found = FALSE;
     69     out_conflict_name = FALSE;
     70   END IF;
     71   RETURN;
     72 EXCEPTION
     73   -- money_pot_name already used
     74   WHEN unique_violation
     75   THEN
     76     out_conflict_name = TRUE;
     77     out_conflict_total = FALSE;
     78     out_not_found = FALSE;
     79     RETURN;
     80 END;
     81 
     82 END $$;