]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp
d6ff1a3aed90d842cd01c83d0b8307a4b7e44b20
[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, __FILE__, __LINE__);
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, __FILE__, __LINE__);
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, __FILE__, __LINE__);
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, __FILE__, __LINE__);
126
127 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
128 IBPP::Statement st = IBPP::StatementFactory(db, tr);
129
130 int32_t id, i;
131
132 try
133     {
134     tr->Start();
135     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
136     st->Set(1, tariffName);
137     st->Execute();
138     if (!st->Fetch())
139     {
140     tr->Rollback();
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());
143     return -1;
144     }
145     st->Get(1, id);
146     st->Close();
147     if (schemaVersion > 0)
148         {
149         st->Prepare("update tb_tariffs set \
150                 fee = ?, \
151                 free = ?, \
152                 passive_cost = ?, \
153                 traff_type = ?, \
154                 period = ? \
155                 where pk_tariff = ?");
156         st->Set(1, td.tariffConf.fee);
157         st->Set(2, td.tariffConf.free);
158         st->Set(3, td.tariffConf.passiveCost);
159         st->Set(4, td.tariffConf.traffType);
160         st->Set(5, TARIFF::PeriodToString(td.tariffConf.period));
161         st->Set(6, id);
162         }
163     else
164         {
165         st->Prepare("update tb_tariffs set \
166                 fee = ?, \
167                 free = ?, \
168                 passive_cost = ?, \
169                 traff_type = ? \
170                 where pk_tariff = ?");
171         st->Set(1, td.tariffConf.fee);
172         st->Set(2, td.tariffConf.free);
173         st->Set(3, td.tariffConf.passiveCost);
174         st->Set(4, td.tariffConf.traffType);
175         st->Set(5, id);
176         }
177     st->Execute();
178     st->Close();
179
180     IBPP::Time tb;
181     IBPP::Time te;
182
183     for(i = 0; i < DIR_NUM; i++)
184         {
185
186         tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
187         te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
188
189         double pda = td.dirPrice[i].priceDayA * 1024 * 1024;
190         double pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
191         double pna = 0;
192         double pnb = 0;
193
194         if (td.dirPrice[i].singlePrice)
195             {
196             pna = pda;
197             pnb = pdb;
198             }
199         else
200             {
201             pna = td.dirPrice[i].priceNightA;
202             pnb = td.dirPrice[i].priceNightB;
203             }
204
205         int threshold = 0;
206         if (td.dirPrice[i].noDiscount)
207             {
208             threshold = 0xffFFffFF;
209             }
210         else
211             {
212             threshold = td.dirPrice[i].threshold;
213             }
214
215         st->Prepare("update tb_tariffs_params set \
216                 price_day_a = ?, \
217                 price_day_b = ?, \
218                 price_night_a = ?, \
219                 price_night_b = ?, \
220                 threshold = ?, \
221                 time_day_begins = ?, \
222                 time_day_ends = ? \
223                 where fk_tariff = ? and dir_num = ?");
224         st->Set(1, pda);
225         st->Set(2, pdb);
226         st->Set(3, pna);
227         st->Set(4, pnb);
228         st->Set(5, threshold);
229         st->Set(6, tb);
230         st->Set(7, te);
231         st->Set(8, id);
232         st->Set(9, i);
233         st->Execute();
234         st->Close();
235         }
236     tr->Commit();
237     }
238
239 catch (IBPP::Exception & ex)
240     {
241     tr->Rollback();
242     strError = "IBPP exception";
243     printfd(__FILE__, ex.what());
244     return -1;
245     }
246
247 return 0;
248 }
249 //-----------------------------------------------------------------------------
250 int FIREBIRD_STORE::RestoreTariff(TARIFF_DATA * td,
251                                   const std::string & tariffName) const
252 {
253 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
254
255 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
256 IBPP::Statement st = IBPP::StatementFactory(db, tr);
257
258
259 td->tariffConf.name = tariffName;
260
261 try
262     {
263     tr->Start();
264     st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
265     st->Set(1, tariffName);
266     st->Execute();
267     if (!st->Fetch())
268         {
269         strError = "Tariff \"" + tariffName + "\" not found in database";
270     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
271         tr->Rollback();
272         return -1;
273         }
274     int32_t id;
275     st->Get(1, id);
276     st->Get(3, td->tariffConf.fee);
277     st->Get(4, td->tariffConf.free);
278     st->Get(5, td->tariffConf.passiveCost);
279     st->Get(6, td->tariffConf.traffType);
280     if (schemaVersion > 0)
281         {
282         std::string period;
283         st->Get(7, period);
284         td->tariffConf.period = TARIFF::StringToPeriod(period);
285         }
286     st->Close();
287     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
288     st->Set(1, id);
289     st->Execute();
290     int i = 0;
291     while (st->Fetch())
292     {
293     i++;
294     if (i > DIR_NUM)
295         {
296         strError = "Too mach params for tariff \"" + tariffName + "\"";
297         printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
298         tr->Rollback();
299         return -1;
300         }
301     int16_t dir;
302     st->Get(3, dir);
303     st->Get(4, td->dirPrice[dir].priceDayA);
304     td->dirPrice[dir].priceDayA /= 1024*1024;
305     st->Get(5, td->dirPrice[dir].priceDayB);
306     td->dirPrice[dir].priceDayB /= 1024*1024;
307     st->Get(6, td->dirPrice[dir].priceNightA);
308     td->dirPrice[dir].priceNightA /= 1024*1024;
309     st->Get(7, td->dirPrice[dir].priceNightB);
310     td->dirPrice[dir].priceNightB /= 1024*1024;
311     st->Get(8, td->dirPrice[dir].threshold);
312     if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 &&
313         std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3)
314         {
315         td->dirPrice[dir].singlePrice = true;
316         }
317     else
318         {
319         td->dirPrice[dir].singlePrice = false;
320         }
321     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
322         {
323         td->dirPrice[dir].noDiscount = true;
324         }
325     else
326         {
327
328         td->dirPrice[dir].noDiscount = false;
329         }
330     IBPP::Time tb;
331     st->Get(9, tb);
332     IBPP::Time te;
333     st->Get(10, te);
334     int h, m, s;
335     tb.GetTime(h, m, s);
336     td->dirPrice[dir].hDay = h;
337     td->dirPrice[dir].mDay = m;
338     te.GetTime(h, m, s);
339     td->dirPrice[dir].hNight = h;
340     td->dirPrice[dir].mNight = m;
341     }
342     tr->Commit();
343     }
344
345 catch (IBPP::Exception & ex)
346     {
347     tr->Rollback();
348     strError = "IBPP exception";
349     printfd(__FILE__, ex.what());
350     return -1;
351     }
352
353 return 0;
354 }
355 //-----------------------------------------------------------------------------
356