iterate_submitted_receipts.c (2817B)
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 * @file src/donaudb/iterate_submitted_receipts.c 18 * @brief Implementation of the iterate_submitted_receipts function for Postgres 19 * @author Johannes Casaburi 20 */ 21 #include <donau_config.h> 22 #include <taler/taler_error_codes.h> 23 #include <taler/taler_dbevents.h> 24 #include <taler/taler_pq_lib.h> 25 #include "iterate_submitted_receipts.h" 26 #include "helper.h" 27 #include "donau_pq_lib.h" 28 29 30 enum GNUNET_DB_QueryStatus 31 DONAUDB_iterate_submitted_receipts ( 32 struct DONAUDB_PostgresContext *ctx, 33 const uint64_t donation_year, 34 const struct DONAU_HashDonorTaxId *h_donor_tax_id, 35 struct TALER_Amount *total_donations) 36 { 37 struct GNUNET_PQ_QueryParam params[] = { 38 GNUNET_PQ_query_param_uint64 (&donation_year), 39 GNUNET_PQ_query_param_auto_from_type (h_donor_tax_id), 40 GNUNET_PQ_query_param_end 41 }; 42 uint64_t valueSum = 0; 43 uint64_t fractionSum = 0; 44 struct GNUNET_PQ_ResultSpec rs[] = { 45 GNUNET_PQ_result_spec_uint64 ("valueSum", 46 &valueSum), 47 GNUNET_PQ_result_spec_uint64 ("fractionSum", 48 &fractionSum), 49 GNUNET_PQ_result_spec_end 50 }; 51 enum GNUNET_DB_QueryStatus qs; 52 53 PREPARE (ctx, 54 "lookup_submitted_receipts", 55 "SELECT " 56 " CAST(SUM((donation_units.value).val) AS INT8) AS valueSum" 57 ",CAST(SUM(CAST((donation_units.value).frac AS INT8)) AS INT8) AS fractionSum" 58 " FROM receipts_submitted ref" 59 " JOIN donation_units USING (h_donation_unit_pub)" 60 " WHERE donation_year=$1" 61 " AND h_tax_number=$2"); 62 qs = GNUNET_PQ_eval_prepared_singleton_select (ctx->conn, 63 "lookup_submitted_receipts", 64 params, 65 rs); 66 if (qs < 0) 67 return qs; 68 valueSum += fractionSum / TALER_AMOUNT_FRAC_BASE; 69 fractionSum %= TALER_AMOUNT_FRAC_BASE; 70 TALER_amount_set_zero (ctx->currency, 71 total_donations); 72 total_donations->value = valueSum; 73 total_donations->fraction = fractionSum; 74 return qs; 75 }