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"
37 const int pt_mega = 1024 * 1024;
41 //-----------------------------------------------------------------------------
42 int FIREBIRD_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
44 STG_LOCKER lock(&mutex);
46 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
47 IBPP::Statement st = IBPP::StatementFactory(db, tr);
54 st->Execute("select name from tb_tariffs");
58 tariffsList->push_back(name);
63 catch (IBPP::Exception & ex)
66 strError = "IBPP exception";
67 printfd(__FILE__, ex.what());
73 //-----------------------------------------------------------------------------
74 int FIREBIRD_STORE::AddTariff(const std::string & name) const
76 STG_LOCKER lock(&mutex);
78 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
79 IBPP::Statement st = IBPP::StatementFactory(db, tr);
84 st->Prepare("execute procedure sp_add_tariff(?, ?)");
91 catch (IBPP::Exception & ex)
94 strError = "IBPP exception";
95 printfd(__FILE__, ex.what());
101 //-----------------------------------------------------------------------------
102 int FIREBIRD_STORE::DelTariff(const std::string & name) const
104 STG_LOCKER lock(&mutex);
106 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
107 IBPP::Statement st = IBPP::StatementFactory(db, tr);
112 st->Prepare("execute procedure sp_delete_tariff(?)");
118 catch (IBPP::Exception & ex)
121 strError = "IBPP exception";
122 printfd(__FILE__, ex.what());
128 //-----------------------------------------------------------------------------
129 int FIREBIRD_STORE::SaveTariff(const TARIFF_DATA & td,
130 const std::string & tariffName) const
132 STG_LOCKER lock(&mutex);
134 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
135 IBPP::Statement st = IBPP::StatementFactory(db, tr);
140 st->Prepare("select pk_tariff from tb_tariffs where name = ?");
141 st->Set(1, tariffName);
146 strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
147 printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
153 if (schemaVersion == 1)
155 st->Prepare("update tb_tariffs set \
161 where pk_tariff = ?");
162 st->Set(1, td.tariffConf.fee);
163 st->Set(2, td.tariffConf.free);
164 st->Set(3, td.tariffConf.passiveCost);
165 st->Set(4, td.tariffConf.traffType);
166 st->Set(5, TARIFF::PeriodToString(td.tariffConf.period));
169 else if (schemaVersion > 1)
171 st->Prepare("update tb_tariffs set \
178 where pk_tariff = ?");
179 st->Set(1, td.tariffConf.fee);
180 st->Set(2, td.tariffConf.free);
181 st->Set(3, td.tariffConf.passiveCost);
182 st->Set(4, td.tariffConf.traffType);
183 st->Set(5, TARIFF::PeriodToString(td.tariffConf.period));
184 st->Set(6, TARIFF::ChangePolicyToString(td.tariffConf.changePolicy));
189 st->Prepare("update tb_tariffs set \
194 where pk_tariff = ?");
195 st->Set(1, td.tariffConf.fee);
196 st->Set(2, td.tariffConf.free);
197 st->Set(3, td.tariffConf.passiveCost);
198 st->Set(4, td.tariffConf.traffType);
207 for(int i = 0; i < DIR_NUM; i++)
210 tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
211 te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
213 double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
214 double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
218 if (td.dirPrice[i].singlePrice)
225 pna = td.dirPrice[i].priceNightA;
226 pnb = td.dirPrice[i].priceNightB;
230 if (td.dirPrice[i].noDiscount)
232 threshold = 0xffFFffFF;
236 threshold = td.dirPrice[i].threshold;
239 st->Prepare("update tb_tariffs_params set \
245 time_day_begins = ?, \
247 where fk_tariff = ? and dir_num = ?");
252 st->Set(5, threshold);
263 catch (IBPP::Exception & ex)
266 strError = "IBPP exception";
267 printfd(__FILE__, ex.what());
273 //-----------------------------------------------------------------------------
274 int FIREBIRD_STORE::RestoreTariff(TARIFF_DATA * td,
275 const std::string & tariffName) const
277 STG_LOCKER lock(&mutex);
279 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
280 IBPP::Statement st = IBPP::StatementFactory(db, tr);
283 td->tariffConf.name = tariffName;
288 st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
289 st->Set(1, tariffName);
293 strError = "Tariff \"" + tariffName + "\" not found in database";
294 printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
300 st->Get(3, td->tariffConf.fee);
301 st->Get(4, td->tariffConf.free);
302 st->Get(5, td->tariffConf.passiveCost);
303 //st->Get(6, td->tariffConf.traffType);
304 td->tariffConf.traffType = TARIFF::IntToTraffType(Get<int>(st, 6));
305 if (schemaVersion > 0)
306 td->tariffConf.period = TARIFF::StringToPeriod(Get<std::string>(st, 7));
307 if (schemaVersion > 1)
308 td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(Get<std::string>(st, 8));
310 st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
319 strError = "Too mach params for tariff \"" + tariffName + "\"";
320 printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
326 st->Get(4, td->dirPrice[dir].priceDayA);
327 td->dirPrice[dir].priceDayA /= 1024*1024;
328 st->Get(5, td->dirPrice[dir].priceDayB);
329 td->dirPrice[dir].priceDayB /= 1024*1024;
330 st->Get(6, td->dirPrice[dir].priceNightA);
331 td->dirPrice[dir].priceNightA /= 1024*1024;
332 st->Get(7, td->dirPrice[dir].priceNightB);
333 td->dirPrice[dir].priceNightB /= 1024*1024;
334 st->Get(8, td->dirPrice[dir].threshold);
335 if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 / pt_mega &&
336 std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
338 td->dirPrice[dir].singlePrice = true;
342 td->dirPrice[dir].singlePrice = false;
344 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
346 td->dirPrice[dir].noDiscount = true;
351 td->dirPrice[dir].noDiscount = false;
359 td->dirPrice[dir].hDay = h;
360 td->dirPrice[dir].mDay = m;
362 td->dirPrice[dir].hNight = h;
363 td->dirPrice[dir].mNight = m;
368 catch (IBPP::Exception & ex)
371 strError = "IBPP exception";
372 printfd(__FILE__, ex.what());
378 //-----------------------------------------------------------------------------