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