donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

pg_iterate_submitted_receipts.c (2856B)


      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 donaudb/pg_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 "pg_iterate_submitted_receipts.h"
     26 #include "pg_helper.h"
     27 #include "donaudb_plugin.h"
     28 #include "donau_pq_lib.h"
     29 
     30 
     31 enum GNUNET_DB_QueryStatus
     32 DH_PG_iterate_submitted_receipts (
     33   void *cls,
     34   const uint64_t donation_year,
     35   const struct DONAU_HashDonorTaxId *h_donor_tax_id,
     36   struct TALER_Amount *total_donations)
     37 {
     38   struct PostgresClosure *pg = cls;
     39 
     40   struct GNUNET_PQ_QueryParam params[] = {
     41     GNUNET_PQ_query_param_uint64 (&donation_year),
     42     GNUNET_PQ_query_param_auto_from_type (h_donor_tax_id),
     43     GNUNET_PQ_query_param_end
     44   };
     45   uint64_t valueSum = 0;
     46   uint64_t fractionSum = 0;
     47   struct GNUNET_PQ_ResultSpec rs[] = {
     48     GNUNET_PQ_result_spec_uint64 ("valueSum",
     49                                   &valueSum),
     50     GNUNET_PQ_result_spec_uint64 ("fractionSum",
     51                                   &fractionSum),
     52     GNUNET_PQ_result_spec_end
     53   };
     54   enum GNUNET_DB_QueryStatus qs;
     55 
     56   PREPARE (pg,
     57            "lookup_submitted_receipts",
     58            "SELECT "
     59            " CAST(SUM((donation_units.value).val) AS INT8) AS valueSum"
     60            ",CAST(SUM(CAST((donation_units.value).frac AS INT8)) AS INT8) AS fractionSum"
     61            " FROM receipts_submitted ref"
     62            " JOIN donation_units USING (h_donation_unit_pub)"
     63            " WHERE donation_year=$1"
     64            " AND h_tax_number=$2");
     65   qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     66                                                  "lookup_submitted_receipts",
     67                                                  params,
     68                                                  rs);
     69   if (qs < 0)
     70     return qs;
     71   valueSum += fractionSum / TALER_AMOUNT_FRAC_BASE;
     72   fractionSum %= TALER_AMOUNT_FRAC_BASE;
     73   TALER_amount_set_zero (pg->currency,
     74                          total_donations);
     75   total_donations->value = valueSum;
     76   total_donations->fraction = fractionSum;
     77   return qs;
     78 }