]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp
b00fd1224c24b07d2edc6c5ee864703d84d75fbe
[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 namespace
35 {
36
37 const int pt_mega = 1024 * 1024;
38
39 }
40
41 //-----------------------------------------------------------------------------
42 int FIREBIRD_STORE::GetTariffsList(std::vector<std::string> * tariffsList) const
43 {
44 STG_LOCKER lock(&mutex);
45
46 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
47 IBPP::Statement st = IBPP::StatementFactory(db, tr);
48
49 std::string name;
50
51 try
52     {
53     tr->Start();
54     st->Execute("select name from tb_tariffs");
55     while (st->Fetch())
56         {
57         st->Get(1, name);
58         tariffsList->push_back(name);
59         }
60     tr->Commit();
61     }
62
63 catch (IBPP::Exception & ex)
64     {
65     tr->Rollback();
66     strError = "IBPP exception";
67     printfd(__FILE__, ex.what());
68     return -1;
69     }
70
71 return 0;
72 }
73 //-----------------------------------------------------------------------------
74 int FIREBIRD_STORE::AddTariff(const std::string & name) const
75 {
76 STG_LOCKER lock(&mutex);
77
78 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
79 IBPP::Statement st = IBPP::StatementFactory(db, tr);
80
81 try
82     {
83     tr->Start();
84     st->Prepare("execute procedure sp_add_tariff(?, ?)");
85     st->Set(1, name);
86     st->Set(2, DIR_NUM);
87     st->Execute();
88     tr->Commit();
89     }
90
91 catch (IBPP::Exception & ex)
92     {
93     tr->Rollback();
94     strError = "IBPP exception";
95     printfd(__FILE__, ex.what());
96     return -1;
97     }
98
99 return 0;
100 }
101 //-----------------------------------------------------------------------------
102 int FIREBIRD_STORE::DelTariff(const std::string & name) const
103 {
104 STG_LOCKER lock(&mutex);
105
106 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
107 IBPP::Statement st = IBPP::StatementFactory(db, tr);
108
109 try
110     {
111     tr->Start();
112     st->Prepare("execute procedure sp_delete_tariff(?)");
113     st->Set(1, name);
114     st->Execute();
115     tr->Commit();
116     }
117
118 catch (IBPP::Exception & ex)
119     {
120     tr->Rollback();
121     strError = "IBPP exception";
122     printfd(__FILE__, ex.what());
123     return -1;
124     }
125
126 return 0;
127 }
128 //-----------------------------------------------------------------------------
129 int FIREBIRD_STORE::SaveTariff(const TARIFF_DATA & td,
130                                const std::string & tariffName) const
131 {
132 STG_LOCKER lock(&mutex);
133
134 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
135 IBPP::Statement st = IBPP::StatementFactory(db, tr);
136
137 try
138     {
139     tr->Start();
140     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
141     st->Set(1, tariffName);
142     st->Execute();
143     if (!st->Fetch())
144     {
145     tr->Rollback();
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());
148     return -1;
149     }
150     int32_t id;
151     st->Get(1, id);
152     st->Close();
153
154     std::string query = "update tb_tariffs set \
155                             fee = ?, \
156                             free = ?, \
157                             passive_cost = ?, \
158                             traff_type = ?";
159
160     if (schemaVersion > 0)
161         query += ", period = ?";
162     if (schemaVersion > 1)
163         query += ", change_policy = ?";
164
165     query += " where pk_tariff = ?";
166
167     st->Set(1, td.tariffConf.fee);
168     st->Set(2, td.tariffConf.free);
169     st->Set(3, td.tariffConf.passiveCost);
170     st->Set(4, td.tariffConf.traffType);
171
172     if (schemaVersion > 0)
173         st->Set(5, TARIFF::PeriodToString(td.tariffConf.period));
174     if (schemaVersion > 1)
175         st->Set(6, TARIFF::ChangePolicyToString(td.tariffConf.changePolicy));
176
177     st->Execute();
178     st->Close();
179
180     IBPP::Time tb;
181     IBPP::Time te;
182
183     for(int 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);
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     td->tariffConf.traffType = TARIFF::IntToTraffType(Get<int>(st, 6));
281     if (schemaVersion > 0)
282         td->tariffConf.period = TARIFF::StringToPeriod(Get<std::string>(st, 7));
283     if (schemaVersion > 1)
284         td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(Get<std::string>(st, 8));
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 / pt_mega &&
312         std::fabs(td->dirPrice[dir].priceDayB - td->dirPrice[dir].priceNightB) < 1.0e-3 / pt_mega)
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