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