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);
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);
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);
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);
127 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
128 IBPP::Statement st = IBPP::StatementFactory(db, tr);
133 st->Prepare("select pk_tariff from tb_tariffs where name = ?");
134 st->Set(1, tariffName);
139 strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
140 printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
146 if (schemaVersion > 0)
148 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);
159 st->Set(5, TARIFF::PeriodToString(td.tariffConf.period));
164 st->Prepare("update tb_tariffs set \
169 where pk_tariff = ?");
170 st->Set(1, td.tariffConf.fee);
171 st->Set(2, td.tariffConf.free);
172 st->Set(3, td.tariffConf.passiveCost);
173 st->Set(4, td.tariffConf.traffType);
182 for(int i = 0; i < DIR_NUM; i++)
185 tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
186 te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
188 double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
189 double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
193 if (td.dirPrice[i].singlePrice)
200 pna = td.dirPrice[i].priceNightA;
201 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);
254 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
255 IBPP::Statement st = IBPP::StatementFactory(db, tr);
258 td->tariffConf.name = tariffName;
263 st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
264 st->Set(1, tariffName);
268 strError = "Tariff \"" + tariffName + "\" not found in database";
269 printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
275 st->Get(3, td->tariffConf.fee);
276 st->Get(4, td->tariffConf.free);
277 st->Get(5, td->tariffConf.passiveCost);
278 //st->Get(6, td->tariffConf.traffType);
279 td->tariffConf.traffType = TARIFF::IntToTraffType(Get<int>(st, 6));
280 if (schemaVersion > 0)
281 td->tariffConf.period = TARIFF::StringToPeriod(Get<std::string>(st, 7));
283 st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
292 strError = "Too mach params for tariff \"" + tariffName + "\"";
293 printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
299 st->Get(4, td->dirPrice[dir].priceDayA);
300 td->dirPrice[dir].priceDayA /= 1024*1024;
301 st->Get(5, td->dirPrice[dir].priceDayB);
302 td->dirPrice[dir].priceDayB /= 1024*1024;
303 st->Get(6, td->dirPrice[dir].priceNightA);
304 td->dirPrice[dir].priceNightA /= 1024*1024;
305 st->Get(7, td->dirPrice[dir].priceNightB);
306 td->dirPrice[dir].priceNightB /= 1024*1024;
307 st->Get(8, td->dirPrice[dir].threshold);
308 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 &&
309 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3)
311 td->dirPrice[dir].singlePrice = true;
315 td->dirPrice[dir].singlePrice = false;
317 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
319 td->dirPrice[dir].noDiscount = true;
324 td->dirPrice[dir].noDiscount = false;
332 td->dirPrice[dir].hDay = h;
333 td->dirPrice[dir].mDay = m;
335 td->dirPrice[dir].hNight = h;
336 td->dirPrice[dir].mNight = m;
341 catch (IBPP::Exception & ex)
344 strError = "IBPP exception";
345 printfd(__FILE__, ex.what());
351 //-----------------------------------------------------------------------------