]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp
Simplified STG_LOCKER.
[stg.git] / projects / stargazer / plugins / store / firebird / firebird_store_tariffs.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 /*
22  *  Tariffs manipulation methods
23  *
24  *  $Revision: 1.5 $
25  *  $Date: 2007/12/23 13:39:59 $
26  *
27  */
28
29 #include <cmath>
30
31 #include "firebird_store.h"
32 #include "stg/ibpp.h"
33
34 //-----------------------------------------------------------------------------
35 int FIREBIRD_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
36 {
37 STG_LOCKER lock(&mutex);
38
39 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
40 IBPP::Statement st = IBPP::StatementFactory(db, tr);
41
42 std::string name;
43
44 try
45     {
46     tr->Start();
47     st->Execute("select name from tb_tariffs");
48     while (st->Fetch())
49         {
50         st->Get(1, name);
51         tariffsList->push_back(name);
52         }
53     tr->Commit();
54     }
55
56 catch (IBPP::Exception & ex)
57     {
58     tr->Rollback();
59     strError = "IBPP exception";
60     printfd(__FILE__, ex.what());
61     return -1;
62     }
63
64 return 0;
65 }
66 //-----------------------------------------------------------------------------
67 int FIREBIRD_STORE::AddTariff(const std::string & name) const
68 {
69 STG_LOCKER lock(&mutex);
70
71 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
72 IBPP::Statement st = IBPP::StatementFactory(db, tr);
73
74 try
75     {
76     tr->Start();
77     st->Prepare("execute procedure sp_add_tariff(?, ?)");
78     st->Set(1, name);
79     st->Set(2, DIR_NUM);
80     st->Execute();
81     tr->Commit();
82     }
83
84 catch (IBPP::Exception & ex)
85     {
86     tr->Rollback();
87     strError = "IBPP exception";
88     printfd(__FILE__, ex.what());
89     return -1;
90     }
91
92 return 0;
93 }
94 //-----------------------------------------------------------------------------
95 int FIREBIRD_STORE::DelTariff(const std::string & name) const
96 {
97 STG_LOCKER lock(&mutex);
98
99 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
100 IBPP::Statement st = IBPP::StatementFactory(db, tr);
101
102 try
103     {
104     tr->Start();
105     st->Prepare("execute procedure sp_delete_tariff(?)");
106     st->Set(1, name);
107     st->Execute();
108     tr->Commit();
109     }
110
111 catch (IBPP::Exception & ex)
112     {
113     tr->Rollback();
114     strError = "IBPP exception";
115     printfd(__FILE__, ex.what());
116     return -1;
117     }
118
119 return 0;
120 }
121 //-----------------------------------------------------------------------------
122 int FIREBIRD_STORE::SaveTariff(const TARIFF_DATA & td,
123                                const std::string & tariffName) const
124 {
125 STG_LOCKER lock(&mutex);
126
127 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
128 IBPP::Statement st = IBPP::StatementFactory(db, tr);
129
130 try
131     {
132     tr->Start();
133     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
134     st->Set(1, tariffName);
135     st->Execute();
136     if (!st->Fetch())
137     {
138     tr->Rollback();
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());
141     return -1;
142     }
143     int32_t id;
144     st->Get(1, id);
145     st->Close();
146     if (schemaVersion > 0)
147         {
148         st->Prepare("update tb_tariffs set \
149                 fee = ?, \
150                 free = ?, \
151                 passive_cost = ?, \
152                 traff_type = ?, \
153                 period = ? \
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));
160         st->Set(6, id);
161         }
162     else
163         {
164         st->Prepare("update tb_tariffs set \
165                 fee = ?, \
166                 free = ?, \
167                 passive_cost = ?, \
168                 traff_type = ? \
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);
174         st->Set(5, id);
175         }
176     st->Execute();
177     st->Close();
178
179     IBPP::Time tb;
180     IBPP::Time te;
181
182     for(int i = 0; i < DIR_NUM; i++)
183         {
184
185         tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
186         te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
187
188         double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
189         double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
190         double pna = 0;
191         double pnb = 0;
192
193         if (td.dirPrice[i].singlePrice)
194             {
195             pna = pda;
196             pnb = pdb;
197             }
198         else
199             {
200             pna = td.dirPrice[i].priceNightA;
201             pnb = td.dirPrice[i].priceNightB;
202             }
203
204         int threshold = 0;
205         if (td.dirPrice[i].noDiscount)
206             {
207             threshold = 0xffFFffFF;
208             }
209         else
210             {
211             threshold = td.dirPrice[i].threshold;
212             }
213
214         st->Prepare("update tb_tariffs_params set \
215                 price_day_a = ?, \
216                 price_day_b = ?, \
217                 price_night_a = ?, \
218                 price_night_b = ?, \
219                 threshold = ?, \
220                 time_day_begins = ?, \
221                 time_day_ends = ? \
222                 where fk_tariff = ? and dir_num = ?");
223         st->Set(1, pda);
224         st->Set(2, pdb);
225         st->Set(3, pna);
226         st->Set(4, pnb);
227         st->Set(5, threshold);
228         st->Set(6, tb);
229         st->Set(7, te);
230         st->Set(8, id);
231         st->Set(9, i);
232         st->Execute();
233         st->Close();
234         }
235     tr->Commit();
236     }
237
238 catch (IBPP::Exception & ex)
239     {
240     tr->Rollback();
241     strError = "IBPP exception";
242     printfd(__FILE__, ex.what());
243     return -1;
244     }
245
246 return 0;
247 }
248 //-----------------------------------------------------------------------------
249 int FIREBIRD_STORE::RestoreTariff(TARIFF_DATA * td,
250                                   const std::string & tariffName) const
251 {
252 STG_LOCKER lock(&mutex);
253
254 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
255 IBPP::Statement st = IBPP::StatementFactory(db, tr);
256
257
258 td->tariffConf.name = tariffName;
259
260 try
261     {
262     tr->Start();
263     st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
264     st->Set(1, tariffName);
265     st->Execute();
266     if (!st->Fetch())
267         {
268         strError = "Tariff \"" + tariffName + "\" not found in database";
269     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
270         tr->Rollback();
271         return -1;
272         }
273     int32_t id;
274     st->Get(1, id);
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     if (schemaVersion > 0)
280         {
281         std::string period;
282         st->Get(7, period);
283         td->tariffConf.period = TARIFF::StringToPeriod(period);
284         }
285     st->Close();
286     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
287     st->Set(1, id);
288     st->Execute();
289     int i = 0;
290     while (st->Fetch())
291     {
292     i++;
293     if (i > DIR_NUM)
294         {
295         strError = "Too mach params for tariff \"" + tariffName + "\"";
296         printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
297         tr->Rollback();
298         return -1;
299         }
300     int16_t dir;
301     st->Get(3, dir);
302     st->Get(4, td->dirPrice[dir].priceDayA);
303     td->dirPrice[dir].priceDayA /= 1024*1024;
304     st->Get(5, td->dirPrice[dir].priceDayB);
305     td->dirPrice[dir].priceDayB /= 1024*1024;
306     st->Get(6, td->dirPrice[dir].priceNightA);
307     td->dirPrice[dir].priceNightA /= 1024*1024;
308     st->Get(7, td->dirPrice[dir].priceNightB);
309     td->dirPrice[dir].priceNightB /= 1024*1024;
310     st->Get(8, td->dirPrice[dir].threshold);
311     if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 &&
312         std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3)
313         {
314         td->dirPrice[dir].singlePrice = true;
315         }
316     else
317         {
318         td->dirPrice[dir].singlePrice = false;
319         }
320     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
321         {
322         td->dirPrice[dir].noDiscount = true;
323         }
324     else
325         {
326
327         td->dirPrice[dir].noDiscount = false;
328         }
329     IBPP::Time tb;
330     st->Get(9, tb);
331     IBPP::Time te;
332     st->Get(10, te);
333     int h, m, s;
334     tb.GetTime(h, m, s);
335     td->dirPrice[dir].hDay = h;
336     td->dirPrice[dir].mDay = m;
337     te.GetTime(h, m, s);
338     td->dirPrice[dir].hNight = h;
339     td->dirPrice[dir].mNight = m;
340     }
341     tr->Commit();
342     }
343
344 catch (IBPP::Exception & ex)
345     {
346     tr->Rollback();
347     strError = "IBPP exception";
348     printfd(__FILE__, ex.what());
349     return -1;
350     }
351
352 return 0;
353 }
354 //-----------------------------------------------------------------------------
355