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