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 if (schemaVersion > 0)
151 st->Prepare("update tb_tariffs set \
157 where pk_tariff = ?");
158 st->Set(1, td.tariffConf.fee);
159 st->Set(2, td.tariffConf.free);
160 st->Set(3, td.tariffConf.passiveCost);
161 st->Set(4, td.tariffConf.traffType);
162 st->Set(5, TARIFF::PeriodToString(td.tariffConf.period));
167 st->Prepare("update tb_tariffs set \
172 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);
185 for(i = 0; i < DIR_NUM; i++)
188 tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
189 te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
191 pda = td.dirPrice[i].priceDayA * 1024 * 1024;
192 pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
194 if (td.dirPrice[i].singlePrice)
201 pna = td.dirPrice[i].priceNightA;
202 pnb = td.dirPrice[i].priceNightB;
205 if (td.dirPrice[i].noDiscount)
207 threshold = 0xffFFffFF;
211 threshold = td.dirPrice[i].threshold;
214 st->Prepare("update tb_tariffs_params set \
220 time_day_begins = ?, \
222 where fk_tariff = ? and dir_num = ?");
227 st->Set(5, threshold);
238 catch (IBPP::Exception & ex)
241 strError = "IBPP exception";
242 printfd(__FILE__, ex.what());
248 //-----------------------------------------------------------------------------
249 int FIREBIRD_STORE::RestoreTariff(TARIFF_DATA * td,
250 const std::string & tariffName) const
252 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
254 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
255 IBPP::Statement st = IBPP::StatementFactory(db, tr);
263 td->tariffConf.name = tariffName;
268 st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
269 st->Set(1, tariffName);
273 strError = "Tariff \"" + tariffName + "\" not found in database";
274 printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
279 st->Get(3, td->tariffConf.fee);
280 st->Get(4, td->tariffConf.free);
281 st->Get(5, td->tariffConf.passiveCost);
282 st->Get(6, td->tariffConf.traffType);
283 if (schemaVersion > 0)
287 td->tariffConf.period = TARIFF::StringToPeriod(period);
290 st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
299 strError = "Too mach params for tariff \"" + tariffName + "\"";
300 printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
305 st->Get(4, td->dirPrice[dir].priceDayA);
306 td->dirPrice[dir].priceDayA /= 1024*1024;
307 st->Get(5, td->dirPrice[dir].priceDayB);
308 td->dirPrice[dir].priceDayB /= 1024*1024;
309 st->Get(6, td->dirPrice[dir].priceNightA);
310 td->dirPrice[dir].priceNightA /= 1024*1024;
311 st->Get(7, td->dirPrice[dir].priceNightB);
312 td->dirPrice[dir].priceNightB /= 1024*1024;
313 st->Get(8, td->dirPrice[dir].threshold);
314 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 &&
315 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3)
317 td->dirPrice[dir].singlePrice = true;
321 td->dirPrice[dir].singlePrice = false;
323 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
325 td->dirPrice[dir].noDiscount = true;
330 td->dirPrice[dir].noDiscount = false;
335 td->dirPrice[dir].hDay = h;
336 td->dirPrice[dir].mDay = m;
338 td->dirPrice[dir].hNight = h;
339 td->dirPrice[dir].mNight = m;
344 catch (IBPP::Exception & ex)
347 strError = "IBPP exception";
348 printfd(__FILE__, ex.what());
354 //-----------------------------------------------------------------------------