]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp
325e7d7f10b48c79978a04f52655f65a4f9d653c
[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 double pda, pdb, pna, pnb;
132 int threshold;
133
134 try
135     {
136     tr->Start();
137     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
138     st->Set(1, tariffName);
139     st->Execute();
140     if (!st->Fetch())
141     {
142     tr->Rollback();
143     strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
144     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
145     return -1;
146     }
147     st->Get(1, id);
148     st->Close();
149     if (schemaVersion > 0)
150         {
151         st->Prepare("update tb_tariffs set \
152                 fee = ?, \
153                 free = ?, \
154                 passive_cost = ?, \
155                 traff_type = ?, \
156                 period = ? \
157                 where pk_tariff = ?");
158         st->Set(1, td.tariffConf.fee);
159         st->Set(2, td.tariffConf.free);
160         st->Set(3, td.tariffConf.passiveCost);
161         st->Set(4, td.tariffConf.traffType);
162         st->Set(5, TARIFF::PeriodToString(td.tariffConf.period));
163         st->Set(6, id);
164         }
165     else
166         {
167         st->Prepare("update tb_tariffs set \
168                 fee = ?, \
169                 free = ?, \
170                 passive_cost = ?, \
171                 traff_type = ? \
172                 where pk_tariff = ?");
173         st->Set(1, td.tariffConf.fee);
174         st->Set(2, td.tariffConf.free);
175         st->Set(3, td.tariffConf.passiveCost);
176         st->Set(4, td.tariffConf.traffType);
177         st->Set(5, id);
178         }
179     st->Execute();
180     st->Close();
181
182     IBPP::Time tb;
183     IBPP::Time te;
184
185     for(i = 0; i < DIR_NUM; i++)
186         {
187
188     tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
189     te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
190
191         pda = td.dirPrice[i].priceDayA * 1024 * 1024;
192         pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
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         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, __FILE__, __LINE__);
253
254 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
255 IBPP::Statement st = IBPP::StatementFactory(db, tr);
256
257 int32_t id;
258 int16_t dir;
259 int i;
260 IBPP::Time tb, te;
261 int h, m, s;
262
263 td->tariffConf.name = tariffName;
264
265 try
266     {
267     tr->Start();
268     st->Prepare("select * from tb_tariffs where name = ?"); // TODO: explicit field order!
269     st->Set(1, tariffName);
270     st->Execute();
271     if (!st->Fetch())
272         {
273         strError = "Tariff \"" + tariffName + "\" not found in database";
274     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
275         tr->Rollback();
276         return -1;
277         }
278     st->Get(1, id);
279     st->Get(3, td->tariffConf.fee);
280     st->Get(4, td->tariffConf.free);
281     st->Get(5, td->tariffConf.passiveCost);
282     st->Get(6, td->tariffConf.traffType);
283     if (schemaVersion > 0)
284         {
285         std::string period;
286         st->Get(7, period);
287         td->tariffConf.period = TARIFF::StringToPeriod(period);
288         }
289     st->Close();
290     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
291     st->Set(1, id);
292     st->Execute();
293     i = 0;
294     while (st->Fetch())
295     {
296     i++;
297     if (i > DIR_NUM)
298         {
299         strError = "Too mach params for tariff \"" + tariffName + "\"";
300         printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
301         tr->Rollback();
302         return -1;
303         }
304     st->Get(3, dir);
305     st->Get(4, td->dirPrice[dir].priceDayA);
306     td->dirPrice[dir].priceDayA /= 1024*1024;
307     st->Get(5, td->dirPrice[dir].priceDayB);
308     td->dirPrice[dir].priceDayB /= 1024*1024;
309     st->Get(6, td->dirPrice[dir].priceNightA);
310     td->dirPrice[dir].priceNightA /= 1024*1024;
311     st->Get(7, td->dirPrice[dir].priceNightB);
312     td->dirPrice[dir].priceNightB /= 1024*1024;
313     st->Get(8, td->dirPrice[dir].threshold);
314     if (std::fabs(td->dirPrice[dir].priceDayA - td->dirPrice[dir].priceNightA) < 1.0e-3 &&
315         std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3)
316         {
317         td->dirPrice[dir].singlePrice = true;
318         }
319     else
320         {
321         td->dirPrice[dir].singlePrice = false;
322         }
323     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
324         {
325         td->dirPrice[dir].noDiscount = true;
326         }
327     else
328         {
329
330         td->dirPrice[dir].noDiscount = false;
331         }
332     st->Get(9, tb);
333     st->Get(10, te);
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