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