]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp
Fix firebird store plugin compilation errors
[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 "firebird_store.h"
30 #include "stg/ibpp.h"
31
32 //-----------------------------------------------------------------------------
33 int FIREBIRD_STORE::GetTariffsList(vector<string> * tariffsList) const
34 {
35 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
36
37 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
38 IBPP::Statement st = IBPP::StatementFactory(db, tr);
39
40 string name;
41
42 try
43     {
44     tr->Start();
45     st->Execute("select name from tb_tariffs");
46     while (st->Fetch())
47         {
48         st->Get(1, name);
49         tariffsList->push_back(name);
50         }
51     tr->Commit();
52     }
53
54 catch (IBPP::Exception & ex)
55     {
56     tr->Rollback();
57     strError = "IBPP exception";
58     printfd(__FILE__, ex.what());
59     return -1;
60     }
61
62 return 0;
63 }
64 //-----------------------------------------------------------------------------
65 int FIREBIRD_STORE::AddTariff(const string & name) const
66 {
67 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
68
69 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
70 IBPP::Statement st = IBPP::StatementFactory(db, tr);
71
72 try
73     {
74     tr->Start();
75     st->Prepare("execute procedure sp_add_tariff(?, ?)");
76     st->Set(1, name);
77     st->Set(2, DIR_NUM);
78     st->Execute();
79     tr->Commit();
80     }
81
82 catch (IBPP::Exception & ex)
83     {
84     tr->Rollback();
85     strError = "IBPP exception";
86     printfd(__FILE__, ex.what());
87     return -1;
88     }
89
90 return 0;
91 }
92 //-----------------------------------------------------------------------------
93 int FIREBIRD_STORE::DelTariff(const string & name) const
94 {
95 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
96
97 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
98 IBPP::Statement st = IBPP::StatementFactory(db, tr);
99
100 try
101     {
102     tr->Start();
103     st->Prepare("execute procedure sp_delete_tariff(?)");
104     st->Set(1, name);
105     st->Execute();
106     tr->Commit();
107     }
108
109 catch (IBPP::Exception & ex)
110     {
111     tr->Rollback();
112     strError = "IBPP exception";
113     printfd(__FILE__, ex.what());
114     return -1;
115     }
116
117 return 0;
118 }
119 //-----------------------------------------------------------------------------
120 int FIREBIRD_STORE::SaveTariff(const TARIFF_DATA & td,
121                                const string & tariffName) const
122 {
123 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
124
125 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr);
126 IBPP::Statement st = IBPP::StatementFactory(db, tr);
127
128 int32_t id, i;
129 double pda, pdb, pna, pnb;
130 int threshold;
131
132 try
133     {
134     tr->Start();
135     st->Prepare("select pk_tariff from tb_tariffs where name = ?");
136     st->Set(1, tariffName);
137     st->Execute();
138     if (!st->Fetch())
139     {
140     tr->Rollback();
141     strprintf(&strError, "Tariff \"%s\" not found in database", tariffName.c_str());
142     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
143     return -1;
144     }
145     st->Get(1, id);
146     st->Close();
147     st->Prepare("update tb_tariffs set \
148             fee = ?, \
149             free = ?, \
150             passive_cost = ?, \
151             traff_type = ? \
152             where pk_tariff = ?");
153     st->Set(1, td.tariffConf.fee);
154     st->Set(2, td.tariffConf.free);
155     st->Set(3, td.tariffConf.passiveCost);
156     st->Set(4, td.tariffConf.traffType);
157     st->Set(5, id);
158     st->Execute();
159     st->Close();
160
161     IBPP::Time tb;
162     IBPP::Time te;
163
164     for(i = 0; i < DIR_NUM; i++)
165         {
166
167     tb.SetTime(td.dirPrice[i].hDay, td.dirPrice[i].mDay, 0);
168     te.SetTime(td.dirPrice[i].hNight, td.dirPrice[i].mNight, 0);
169
170         pda = td.dirPrice[i].priceDayA * 1024 * 1024;
171         pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
172
173         if (td.dirPrice[i].singlePrice)
174             {
175             pna = pda;
176             pnb = pdb;
177             }
178         else
179             {
180             pna = td.dirPrice[i].priceNightA;
181             pnb = td.dirPrice[i].priceNightB;
182             }
183
184         if (td.dirPrice[i].noDiscount)
185             {
186             threshold = 0xffFFffFF;
187             }
188         else
189             {
190             threshold = td.dirPrice[i].threshold;
191             }
192
193     st->Prepare("update tb_tariffs_params set \
194             price_day_a = ?, \
195             price_day_b = ?, \
196             price_night_a = ?, \
197             price_night_b = ?, \
198             threshold = ?, \
199             time_day_begins = ?, \
200             time_day_ends = ? \
201              where fk_tariff = ? and dir_num = ?");
202     st->Set(1, pda);
203     st->Set(2, pdb);
204     st->Set(3, pna);
205     st->Set(4, pnb);
206     st->Set(5, threshold);
207     st->Set(6, tb);
208     st->Set(7, te);
209     st->Set(8, id);
210     st->Set(9, i);
211     st->Execute();
212     st->Close();
213         }
214     tr->Commit();
215     }
216
217 catch (IBPP::Exception & ex)
218     {
219     tr->Rollback();
220     strError = "IBPP exception";
221     printfd(__FILE__, ex.what());
222     return -1;
223     }
224
225 return 0;
226 }
227 //-----------------------------------------------------------------------------
228 int FIREBIRD_STORE::RestoreTariff(TARIFF_DATA * td,
229                                   const string & tariffName) const
230 {
231 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
232
233 IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr);
234 IBPP::Statement st = IBPP::StatementFactory(db, tr);
235
236 int32_t id;
237 int16_t dir;
238 int i;
239 IBPP::Time tb, te;
240 int h, m, s;
241
242 td->tariffConf.name = tariffName;
243
244 try
245     {
246     tr->Start();
247     st->Prepare("select * from tb_tariffs where name = ?");
248     st->Set(1, tariffName);
249     st->Execute();
250     if (!st->Fetch())
251         {
252         strError = "Tariff \"" + tariffName + "\" not found in database";
253     printfd(__FILE__, "Tariff '%s' not found in database\n", tariffName.c_str());
254         tr->Rollback();
255         return -1;
256         }
257     st->Get(1, id);
258     st->Get(3, td->tariffConf.fee);
259     st->Get(4, td->tariffConf.free);
260     st->Get(5, td->tariffConf.passiveCost);
261     st->Get(6, td->tariffConf.traffType);
262     st->Close();
263     st->Prepare("select * from tb_tariffs_params where fk_tariff = ?");
264     st->Set(1, id);
265     st->Execute();
266     i = 0;
267     while (st->Fetch())
268     {
269     i++;
270     if (i > DIR_NUM)
271         {
272         strError = "Too mach params for tariff \"" + tariffName + "\"";
273         printfd(__FILE__, "Too mach params for tariff '%s'\n", tariffName.c_str());
274         tr->Rollback();
275         return -1;
276         }
277     st->Get(3, dir);
278     st->Get(4, td->dirPrice[dir].priceDayA);
279     td->dirPrice[dir].priceDayA /= 1024*1024;
280     st->Get(5, td->dirPrice[dir].priceDayB);
281     td->dirPrice[dir].priceDayB /= 1024*1024;
282     st->Get(6, td->dirPrice[dir].priceNightA);
283     td->dirPrice[dir].priceNightA /= 1024*1024;
284     st->Get(7, td->dirPrice[dir].priceNightB);
285     td->dirPrice[dir].priceNightB /= 1024*1024;
286     st->Get(8, td->dirPrice[dir].threshold);
287     if (td->dirPrice[dir].priceDayA == td->dirPrice[dir].priceNightA &&
288         td->dirPrice[dir].priceDayB == td->dirPrice[dir].priceNightB)
289         {
290         td->dirPrice[dir].singlePrice = true;
291         }
292     else
293         {
294         td->dirPrice[dir].singlePrice = false;
295         }
296     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
297         {
298         td->dirPrice[dir].noDiscount = true;
299         }
300     else
301         {
302
303         td->dirPrice[dir].noDiscount = false;
304         }
305     st->Get(9, tb);
306     st->Get(10, te);
307     tb.GetTime(h, m, s);
308     td->dirPrice[dir].hDay = h;
309     td->dirPrice[dir].mDay = m;
310     te.GetTime(h, m, s);
311     td->dirPrice[dir].hNight = h;
312     td->dirPrice[dir].mNight = m;
313     }
314     tr->Commit();
315     }
316
317 catch (IBPP::Exception & ex)
318     {
319     tr->Rollback();
320     strError = "IBPP exception";
321     printfd(__FILE__, ex.what());
322     return -1;
323     }
324
325 return 0;
326 }
327 //-----------------------------------------------------------------------------
328