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 //-----------------------------------------------------------------------------
33 int FIREBIRD_STORE::GetTariffsList(vector<string> * tariffsList) const
35 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
37 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
38 IBPP::Statement st = IBPP::StatementFactory(db, tr);
45 st->Execute("select name from tb_tariffs");
49 tariffsList->push_back(name);
54 catch (IBPP::Exception & ex)
57 strError = "IBPP exception";
58 printfd(__FILE__, ex.what());
64 //-----------------------------------------------------------------------------
65 int FIREBIRD_STORE::AddTariff(const string & name) const
67 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
69 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
70 IBPP::Statement st = IBPP::StatementFactory(db, tr);
75 st->Prepare("execute procedure sp_add_tariff(?, ?)");
82 catch (IBPP::Exception & ex)
85 strError = "IBPP exception";
86 printfd(__FILE__, ex.what());
92 //-----------------------------------------------------------------------------
93 int FIREBIRD_STORE::DelTariff(const string & name) const
95 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
97 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
98 IBPP::Statement st = IBPP::StatementFactory(db, tr);
103 st->Prepare("execute procedure sp_delete_tariff(?)");
109 catch (IBPP::Exception & ex)
112 strError = "IBPP exception";
113 printfd(__FILE__, ex.what());
119 //-----------------------------------------------------------------------------
120 int FIREBIRD_STORE::SaveTariff(const TARIFF_DATA & td,
121 const string & tariffName) const
123 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
125 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
126 IBPP::Statement st = IBPP::StatementFactory(db, tr);
129 double pda, pdb, pna, pnb;
135 st->Prepare("select pk_tariff from tb_tariffs where name = ?");
136 st->Set(1, tariffName);
141 strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
142 printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
147 st->Prepare("update tb_tariffs set \
152 where pk_tariff = ?");
153 st->Set(1, td.tariffConf.fee);
154 st->Set(2, td.tariffConf.free);
155 st->Set(3, td.tariffConf.passiveCost);
156 st->Set(4, td.tariffConf.traffType);
164 for(i = 0; i < DIR_NUM; i++)
167 tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
168 te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
170 pda = td.dirPrice[i].priceDayA * 1024 * 1024;
171 pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
173 if (td.dirPrice[i].singlePrice)
180 pna = td.dirPrice[i].priceNightA;
181 pnb = td.dirPrice[i].priceNightB;
184 if (td.dirPrice[i].noDiscount)
186 threshold = 0xffFFffFF;
190 threshold = td.dirPrice[i].threshold;
193 st->Prepare("update tb_tariffs_params set \
199 time_day_begins = ?, \
201 where fk_tariff = ? and dir_num = ?");
206 st->Set(5, threshold);
217 catch (IBPP::Exception & ex)
220 strError = "IBPP exception";
221 printfd(__FILE__, ex.what());
227 //-----------------------------------------------------------------------------
228 int FIREBIRD_STORE::RestoreTariff(TARIFF_DATA * td,
229 const string & tariffName) const
231 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
233 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
234 IBPP::Statement st = IBPP::StatementFactory(db, tr);
242 td->tariffConf.name = tariffName;
247 st->Prepare("select * from tb_tariffs where name = ?");
248 st->Set(1, tariffName);
252 strError = "Tariff \"" + tariffName + "\" not found in database";
253 printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
258 st->Get(3, td->tariffConf.fee);
259 st->Get(4, td->tariffConf.free);
260 st->Get(5, td->tariffConf.passiveCost);
261 st->Get(6, td->tariffConf.traffType);
263 st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
272 strError = "Too mach params for tariff \"" + tariffName + "\"";
273 printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
278 st->Get(4, td->dirPrice[dir].priceDayA);
279 td->dirPrice[dir].priceDayA /= 1024*1024;
280 st->Get(5, td->dirPrice[dir].priceDayB);
281 td->dirPrice[dir].priceDayB /= 1024*1024;
282 st->Get(6, td->dirPrice[dir].priceNightA);
283 td->dirPrice[dir].priceNightA /= 1024*1024;
284 st->Get(7, td->dirPrice[dir].priceNightB);
285 td->dirPrice[dir].priceNightB /= 1024*1024;
286 st->Get(8, td->dirPrice[dir].threshold);
287 if (td->dirPrice[dir].priceDayA == td->dirPrice[dir].priceNightA &&
288 td->dirPrice[dir].priceDayB == td->dirPrice[dir].priceNightB)
290 td->dirPrice[dir].singlePrice = true;
294 td->dirPrice[dir].singlePrice = false;
296 if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
298 td->dirPrice[dir].noDiscount = true;
303 td->dirPrice[dir].noDiscount = false;
308 td->dirPrice[dir].hDay = h;
309 td->dirPrice[dir].mDay = m;
311 td->dirPrice[dir].hNight = h;
312 td->dirPrice[dir].mNight = m;
317 catch (IBPP::Exception & ex)
320 strError = "IBPP exception";
321 printfd(__FILE__, ex.what());
327 //-----------------------------------------------------------------------------