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