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 $
 
  31 #include "firebird_store.h"
 
  34 //-----------------------------------------------------------------------------
 
  35 int FIREBIRD_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
 
  37 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
  39 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
 
  40 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
  47     st->Execute("select name from tb_tariffs");
 
  51         tariffsList->push_back(name);
 
  56 catch (IBPP::Exception & ex)
 
  59     strError = "IBPP exception";
 
  60     printfd(__FILE__, ex.what());
 
  66 //-----------------------------------------------------------------------------
 
  67 int FIREBIRD_STORE::AddTariff(const std::string & name) const
 
  69 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
  71 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
  72 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
  77     st->Prepare("execute procedure sp_add_tariff(?, ?)");
 
  84 catch (IBPP::Exception & ex)
 
  87     strError = "IBPP exception";
 
  88     printfd(__FILE__, ex.what());
 
  94 //-----------------------------------------------------------------------------
 
  95 int FIREBIRD_STORE::DelTariff(const std::string & name) const
 
  97 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
  99 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
 100 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 105     st->Prepare("execute procedure sp_delete_tariff(?)");
 
 111 catch (IBPP::Exception & ex)
 
 114     strError = "IBPP exception";
 
 115     printfd(__FILE__, ex.what());
 
 121 //-----------------------------------------------------------------------------
 
 122 int FIREBIRD_STORE::SaveTariff(const TARIFF_DATA & td,
 
 123                                const std::string & tariffName) const
 
 125 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 127 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
 
 128 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 131 double pda, pdb, pna, pnb;
 
 137     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
 
 138     st->Set(1, tariffName);
 
 143     strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
 
 144     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
 
 149     st->Prepare("update tb_tariffs set \
 
 154             where pk_tariff = ?");
 
 155     st->Set(1, td.tariffConf.fee);
 
 156     st->Set(2, td.tariffConf.free);
 
 157     st->Set(3, td.tariffConf.passiveCost);
 
 158     st->Set(4, td.tariffConf.traffType);
 
 166     for(i = 0; i < DIR_NUM; i++)
 
 169     tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
 
 170     te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
 
 172         pda = td.dirPrice[i].priceDayA * 1024 * 1024;
 
 173         pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
 
 175         if (td.dirPrice[i].singlePrice)
 
 182             pna = td.dirPrice[i].priceNightA;
 
 183             pnb = td.dirPrice[i].priceNightB;
 
 186         if (td.dirPrice[i].noDiscount)
 
 188             threshold = 0xffFFffFF;
 
 192             threshold = td.dirPrice[i].threshold;
 
 195     st->Prepare("update tb_tariffs_params set \
 
 201             time_day_begins = ?, \
 
 203              where fk_tariff = ? and dir_num = ?");
 
 208     st->Set(5, threshold);
 
 219 catch (IBPP::Exception & ex)
 
 222     strError = "IBPP exception";
 
 223     printfd(__FILE__, ex.what());
 
 229 //-----------------------------------------------------------------------------
 
 230 int FIREBIRD_STORE::RestoreTariff(TARIFF_DATA * td,
 
 231                                   const std::string & tariffName) const
 
 233 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 235 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
 
 236 IBPP::Statement st = IBPP::StatementFactory(db, tr);
 
 244 td->tariffConf.name = tariffName;
 
 249     st->Prepare("select * from tb_tariffs where name = ?");
 
 250     st->Set(1, tariffName);
 
 254         strError = "Tariff \"" + tariffName + "\" not found in database";
 
 255     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
 
 260     st->Get(3, td->tariffConf.fee);
 
 261     st->Get(4, td->tariffConf.free);
 
 262     st->Get(5, td->tariffConf.passiveCost);
 
 263     st->Get(6, td->tariffConf.traffType);
 
 265     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
 
 274         strError = "Too mach params for tariff \"" + tariffName + "\"";
 
 275         printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
 
 280     st->Get(4, td->dirPrice[dir].priceDayA);
 
 281     td->dirPrice[dir].priceDayA /= 1024*1024;
 
 282     st->Get(5, td->dirPrice[dir].priceDayB);
 
 283     td->dirPrice[dir].priceDayB /= 1024*1024;
 
 284     st->Get(6, td->dirPrice[dir].priceNightA);
 
 285     td->dirPrice[dir].priceNightA /= 1024*1024;
 
 286     st->Get(7, td->dirPrice[dir].priceNightB);
 
 287     td->dirPrice[dir].priceNightB /= 1024*1024;
 
 288     st->Get(8, td->dirPrice[dir].threshold);
 
 289     if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 &&
 
 290         std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3)
 
 292         td->dirPrice[dir].singlePrice = true;
 
 296         td->dirPrice[dir].singlePrice = false;
 
 298     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
 
 300         td->dirPrice[dir].noDiscount = true;
 
 305         td->dirPrice[dir].noDiscount = false;
 
 310     td->dirPrice[dir].hDay = h;
 
 311     td->dirPrice[dir].mDay = m;
 
 313     td->dirPrice[dir].hNight = h;
 
 314     td->dirPrice[dir].mNight = m;
 
 319 catch (IBPP::Exception & ex)
 
 322     strError = "IBPP exception";
 
 323     printfd(__FILE__, ex.what());
 
 329 //-----------------------------------------------------------------------------