2  *    This program is free software; you can redistribute it and/or modify
 
   3  *    it under the terms of the GNU General Public License as published by
 
   4  *    the Free Software Foundation; either version 2 of the License, or
 
   5  *    (at your option) any later version.
 
   7  *    This program is distributed in the hope that it will be useful,
 
   8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  10  *    GNU General Public License for more details.
 
  12  *    You should have received a copy of the GNU General Public License
 
  13  *    along with this program; if not, write to the Free Software
 
  14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
 
  22  *  Tariffs manipulation methods
 
  25  *  $Date: 2007/12/23 13:39:59 $
 
  29 #include "firebird_store.h"
 
  32 #include "stg/tariff.h"
 
  33 #include "stg/tariff_conf.h"
 
  34 #include "stg/common.h"
 
  41 const int pt_mega = 1024 * 1024;
 
  45 //-----------------------------------------------------------------------------
 
  46 int FIREBIRD_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
 
  48 STG_LOCKER lock(&mutex);
 
  50 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
 
  51 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
  56     st->Execute("select name from tb_tariffs");
 
  61         tariffsList->push_back(name);
 
  66 catch (IBPP::Exception & ex)
 
  69     strError = "IBPP exception";
 
  70     printfd(__FILE__, ex.what());
 
  76 //-----------------------------------------------------------------------------
 
  77 int FIREBIRD_STORE::AddTariff(const std::string & name) const
 
  79 STG_LOCKER lock(&mutex);
 
  81 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
  82 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
  87     st->Prepare("execute procedure sp_add_tariff(?, ?)");
 
  94 catch (IBPP::Exception & ex)
 
  97     strError = "IBPP exception";
 
  98     printfd(__FILE__, ex.what());
 
 104 //-----------------------------------------------------------------------------
 
 105 int FIREBIRD_STORE::DelTariff(const std::string & name) const
 
 107 STG_LOCKER lock(&mutex);
 
 109 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
 110 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 115     st->Prepare("execute procedure sp_delete_tariff(?)");
 
 121 catch (IBPP::Exception & ex)
 
 124     strError = "IBPP exception";
 
 125     printfd(__FILE__, ex.what());
 
 131 //-----------------------------------------------------------------------------
 
 132 int FIREBIRD_STORE::SaveTariff(const STG::TariffData & td,
 
 133                                const std::string & tariffName) const
 
 135 STG_LOCKER lock(&mutex);
 
 137 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
 138 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 143     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
 
 144     st->Set(1, tariffName);
 
 149     strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
 
 150     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
 
 157     std::string query = "update tb_tariffs set \
 
 163     if (schemaVersion > 0)
 
 164         query += ", period = ?";
 
 165     if (schemaVersion > 1)
 
 166         query += ", change_policy = ?, \
 
 167                     change_policy_timeout = ?";
 
 169     query += " where pk_tariff = ?";
 
 173     st->Set(1, td.tariffConf.fee);
 
 174     st->Set(2, td.tariffConf.free);
 
 175     st->Set(3, td.tariffConf.passiveCost);
 
 176     st->Set(4, td.tariffConf.traffType);
 
 178     if (schemaVersion > 0)
 
 180         st->Set(5, STG::Tariff::toString(td.tariffConf.period));
 
 184     if (schemaVersion > 1)
 
 186         st->Set(6, STG::Tariff::toString(td.tariffConf.changePolicy));
 
 187         IBPP::Timestamp policyTimeout;
 
 188         time_t2ts(td.tariffConf.changePolicyTimeout, &policyTimeout);
 
 189         st->Set(7, policyTimeout);
 
 200     for(int i = 0; i < DIR_NUM; i++)
 
 203         tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
 
 204         te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
 
 206         double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
 
 207         double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
 
 211         if (td.dirPrice[i].singlePrice)
 
 218             pna = td.dirPrice[i].priceNightA;
 
 219             pnb = td.dirPrice[i].priceNightB;
 
 223         if (td.dirPrice[i].noDiscount)
 
 225             threshold = 0xffFFffFF;
 
 229             threshold = td.dirPrice[i].threshold;
 
 232         st->Prepare("update tb_tariffs_params set \
 
 238                 time_day_begins = ?, \
 
 240                 where fk_tariff = ? and dir_num = ?");
 
 245         st->Set(5, threshold);
 
 256 catch (IBPP::Exception & ex)
 
 259     strError = "IBPP exception";
 
 260     printfd(__FILE__, ex.what());
 
 266 //-----------------------------------------------------------------------------
 
 267 int FIREBIRD_STORE::RestoreTariff(STG::TariffData * td,
 
 268                                   const std::string & tariffName) const
 
 270 STG_LOCKER lock(&mutex);
 
 272 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
 
 273 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 276 td->tariffConf.name = tariffName;
 
 281     st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
 
 282     st->Set(1, tariffName);
 
 286         strError = "Tariff \"" + tariffName + "\" not found in database";
 
 287     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
 
 293     st->Get(3, td->tariffConf.fee);
 
 294     st->Get(4, td->tariffConf.free);
 
 295     st->Get(5, td->tariffConf.passiveCost);
 
 296     td->tariffConf.traffType = STG::Tariff::fromInt(Get<int>(st, 6));
 
 297     if (schemaVersion > 0)
 
 298         td->tariffConf.period = STG::Tariff::parsePeriod(Get<std::string>(st, 7));
 
 299     if (schemaVersion > 1)
 
 301         td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(Get<std::string>(st, 8));
 
 302         td->tariffConf.changePolicyTimeout = ts2time_t(Get<IBPP::Timestamp>(st, 9));
 
 305     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
 
 314         strError = "Too mach params for tariff \"" + tariffName + "\"";
 
 315         printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
 
 321     st->Get(4, td->dirPrice[dir].priceDayA);
 
 322     td->dirPrice[dir].priceDayA /= 1024*1024;
 
 323     st->Get(5, td->dirPrice[dir].priceDayB);
 
 324     td->dirPrice[dir].priceDayB /= 1024*1024;
 
 325     st->Get(6, td->dirPrice[dir].priceNightA);
 
 326     td->dirPrice[dir].priceNightA /= 1024*1024;
 
 327     st->Get(7, td->dirPrice[dir].priceNightB);
 
 328     td->dirPrice[dir].priceNightB /= 1024*1024;
 
 329     st->Get(8, td->dirPrice[dir].threshold);
 
 330     if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
 
 331         std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
 
 333         td->dirPrice[dir].singlePrice = true;
 
 337         td->dirPrice[dir].singlePrice = false;
 
 339     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
 
 341         td->dirPrice[dir].noDiscount = true;
 
 346         td->dirPrice[dir].noDiscount = false;
 
 354     td->dirPrice[dir].hDay = h;
 
 355     td->dirPrice[dir].mDay = m;
 
 357     td->dirPrice[dir].hNight = h;
 
 358     td->dirPrice[dir].mNight = m;
 
 363 catch (IBPP::Exception & ex)
 
 366     strError = "IBPP exception";
 
 367     printfd(__FILE__, ex.what());
 
 373 //-----------------------------------------------------------------------------