merchant

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

lock_product.c (4346B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 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  * @file src/backenddb/lock_product.c
     18  * @brief Implementation of the lock_product function for Postgres
     19  * @author Iván Ávalos
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_pq_lib.h>
     23 #include "merchant-database/lock_product.h"
     24 #include "helper.h"
     25 
     26 
     27 enum GNUNET_DB_QueryStatus
     28 TALER_MERCHANTDB_lock_product (
     29   struct TALER_MERCHANTDB_PostgresContext *pg,
     30   const char *instance_id,
     31   const char *product_id,
     32   const struct GNUNET_Uuid *uuid,
     33   uint64_t quantity,
     34   uint32_t quantity_frac,
     35   struct GNUNET_TIME_Timestamp expiration_time)
     36 {
     37   struct GNUNET_PQ_QueryParam params[] = {
     38     GNUNET_PQ_query_param_string (product_id),
     39     GNUNET_PQ_query_param_auto_from_type (uuid),
     40     GNUNET_PQ_query_param_uint64 (&quantity),
     41     GNUNET_PQ_query_param_uint32 (&quantity_frac),
     42     GNUNET_PQ_query_param_timestamp (&expiration_time),
     43     GNUNET_PQ_query_param_end
     44   };
     45 
     46   GNUNET_assert (NULL != pg->current_merchant_id);
     47   GNUNET_assert (0 == strcmp (instance_id,
     48                               pg->current_merchant_id));
     49   check_connection (pg);
     50   TMH_PQ_prepare_anon (pg,
     51                        "WITH tmp AS"
     52                        "  (SELECT"
     53                        "     mi.product_serial"
     54                        "    ,mi.total_stock"
     55                        "    ,mi.total_stock_frac"
     56                        "    ,mi.total_sold"
     57                        "    ,mi.total_sold_frac"
     58                        "    ,mi.total_lost"
     59                        "    ,mi.total_lost_frac"
     60                        "    ,mi.allow_fractional_quantity"
     61                        "   FROM merchant_inventory mi"
     62                        "   WHERE mi.product_id=$1)"
     63                        "INSERT INTO merchant_inventory_locks"
     64                        "(product_serial"
     65                        ",lock_uuid"
     66                        ",total_locked"
     67                        ",total_locked_frac"
     68                        ",expiration)"
     69                        " SELECT tmp.product_serial, $2, $3::INT8, $4::INT4, $5"
     70                        "   FROM tmp"
     71                        "  WHERE (tmp.allow_fractional_quantity OR $4 = 0)"
     72                        "    AND (tmp.total_stock = 9223372036854775807"
     73                        "         OR ("
     74                        "           (tmp.total_stock::NUMERIC * 1000000"
     75                        "            + tmp.total_stock_frac::NUMERIC)"
     76                        "           - (tmp.total_sold::NUMERIC * 1000000"
     77                        "              + tmp.total_sold_frac::NUMERIC)"
     78                        "           - (tmp.total_lost::NUMERIC * 1000000"
     79                        "              + tmp.total_lost_frac::NUMERIC)"
     80                        "           >= "
     81                        "             (($3::NUMERIC * 1000000) + $4::NUMERIC)"
     82                        "           + (SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000"
     83                        "                                   + total_locked_frac::NUMERIC), 0)"
     84                        "                FROM merchant_inventory_locks mil"
     85                        "                WHERE mil.product_serial = tmp.product_serial)"
     86                        "           + (SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000"
     87                        "                                   + total_locked_frac::NUMERIC), 0)"
     88                        "                FROM merchant_order_locks mol"
     89                        "                WHERE mol.product_serial = tmp.product_serial)"
     90                        "         ))");
     91   return GNUNET_PQ_eval_prepared_non_select (pg->conn,
     92                                              "",
     93                                              params);
     94 }