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