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);
 
  58     st->Execute("select name from tb_tariffs");
 
  62         tariffsList->push_back(name);
 
  67 catch (IBPP::Exception & ex)
 
  70     strError = "IBPP exception";
 
  71     printfd(__FILE__, ex.what());
 
  77 //-----------------------------------------------------------------------------
 
  78 int FIREBIRD_STORE::AddTariff(const std::string & name) const
 
  80 STG_LOCKER lock(&mutex);
 
  82 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
  83 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
  88     st->Prepare("execute procedure sp_add_tariff(?, ?)");
 
  95 catch (IBPP::Exception & ex)
 
  98     strError = "IBPP exception";
 
  99     printfd(__FILE__, ex.what());
 
 105 //-----------------------------------------------------------------------------
 
 106 int FIREBIRD_STORE::DelTariff(const std::string & name) const
 
 108 STG_LOCKER lock(&mutex);
 
 110 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
 111 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 116     st->Prepare("execute procedure sp_delete_tariff(?)");
 
 122 catch (IBPP::Exception & ex)
 
 125     strError = "IBPP exception";
 
 126     printfd(__FILE__, ex.what());
 
 132 //-----------------------------------------------------------------------------
 
 133 int FIREBIRD_STORE::SaveTariff(const STG::TariffData & td,
 
 134                                const std::string & tariffName) const
 
 136 STG_LOCKER lock(&mutex);
 
 138 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
 139 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 144     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
 
 145     st->Set(1, tariffName);
 
 150     strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
 
 151     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
 
 158     std::string query = "update tb_tariffs set \
 
 164     if (schemaVersion > 0)
 
 165         query += ", period = ?";
 
 166     if (schemaVersion > 1)
 
 167         query += ", change_policy = ?, \
 
 168                     change_policy_timeout = ?";
 
 170     query += " where pk_tariff = ?";
 
 174     st->Set(1, td.tariffConf.fee);
 
 175     st->Set(2, td.tariffConf.free);
 
 176     st->Set(3, td.tariffConf.passiveCost);
 
 177     st->Set(4, td.tariffConf.traffType);
 
 179     if (schemaVersion > 0)
 
 181         st->Set(5, STG::Tariff::toString(td.tariffConf.period));
 
 185     if (schemaVersion > 1)
 
 187         st->Set(6, STG::Tariff::toString(td.tariffConf.changePolicy));
 
 188         IBPP::Timestamp policyTimeout;
 
 189         time_t2ts(td.tariffConf.changePolicyTimeout, &policyTimeout);
 
 190         st->Set(7, policyTimeout);
 
 201     for(int i = 0; i < DIR_NUM; i++)
 
 204         tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
 
 205         te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
 
 207         double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
 
 208         double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
 
 212         if (td.dirPrice[i].singlePrice)
 
 219             pna = td.dirPrice[i].priceNightA;
 
 220             pnb = td.dirPrice[i].priceNightB;
 
 224         if (td.dirPrice[i].noDiscount)
 
 226             threshold = 0xffFFffFF;
 
 230             threshold = td.dirPrice[i].threshold;
 
 233         st->Prepare("update tb_tariffs_params set \
 
 239                 time_day_begins = ?, \
 
 241                 where fk_tariff = ? and dir_num = ?");
 
 246         st->Set(5, threshold);
 
 257 catch (IBPP::Exception & ex)
 
 260     strError = "IBPP exception";
 
 261     printfd(__FILE__, ex.what());
 
 267 //-----------------------------------------------------------------------------
 
 268 int FIREBIRD_STORE::RestoreTariff(STG::TariffData * td,
 
 269                                   const std::string & tariffName) const
 
 271 STG_LOCKER lock(&mutex);
 
 273 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
 
 274 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 277 td->tariffConf.name = tariffName;
 
 282     st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
 
 283     st->Set(1, tariffName);
 
 287         strError = "Tariff \"" + tariffName + "\" not found in database";
 
 288     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
 
 294     st->Get(3, td->tariffConf.fee);
 
 295     st->Get(4, td->tariffConf.free);
 
 296     st->Get(5, td->tariffConf.passiveCost);
 
 297     td->tariffConf.traffType = STG::Tariff::fromInt(Get<int>(st, 6));
 
 298     if (schemaVersion > 0)
 
 299         td->tariffConf.period = STG::Tariff::parsePeriod(Get<std::string>(st, 7));
 
 300     if (schemaVersion > 1)
 
 302         td->tariffConf.changePolicy = STG::Tariff::parseChangePolicy(Get<std::string>(st, 8));
 
 303         td->tariffConf.changePolicyTimeout = ts2time_t(Get<IBPP::Timestamp>(st, 9));
 
 306     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
 
 315         strError = "Too mach params for tariff \"" + tariffName + "\"";
 
 316         printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
 
 322     st->Get(4, td->dirPrice[dir].priceDayA);
 
 323     td->dirPrice[dir].priceDayA /= 1024*1024;
 
 324     st->Get(5, td->dirPrice[dir].priceDayB);
 
 325     td->dirPrice[dir].priceDayB /= 1024*1024;
 
 326     st->Get(6, td->dirPrice[dir].priceNightA);
 
 327     td->dirPrice[dir].priceNightA /= 1024*1024;
 
 328     st->Get(7, td->dirPrice[dir].priceNightB);
 
 329     td->dirPrice[dir].priceNightB /= 1024*1024;
 
 330     st->Get(8, td->dirPrice[dir].threshold);
 
 331     if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
 
 332         std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
 
 334         td->dirPrice[dir].singlePrice = true;
 
 338         td->dirPrice[dir].singlePrice = false;
 
 340     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
 
 342         td->dirPrice[dir].noDiscount = true;
 
 347         td->dirPrice[dir].noDiscount = false;
 
 355     td->dirPrice[dir].hDay = h;
 
 356     td->dirPrice[dir].mDay = m;
 
 358     td->dirPrice[dir].hNight = h;
 
 359     td->dirPrice[dir].mNight = m;
 
 364 catch (IBPP::Exception & ex)
 
 367     strError = "IBPP exception";
 
 368     printfd(__FILE__, ex.what());
 
 374 //-----------------------------------------------------------------------------