]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp
Implemented daily fee charge with backward compatibility.
[stg.git] / projects / stargazer / plugins / store / postgresql / postgresql_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.2 $
25  *  $Date: 2009/06/09 12:32:40 $
26  *
27  */
28
29 #include <string>
30 #include <vector>
31 #include <sstream>
32
33 #include <libpq-fe.h>
34
35 #include "postgresql_store.h"
36 #include "stg/locker.h"
37
38 //-----------------------------------------------------------------------------
39 int POSTGRESQL_STORE::GetTariffsList(vector<string> * tariffsList) const
40 {
41 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
42
43 if (PQstatus(connection) != CONNECTION_OK)
44     {
45     printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
46     if (Reset())
47         {
48         strError = "Connection lost";
49         printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
50         return -1;
51         }
52     }
53
54 PGresult * result;
55
56 if (StartTransaction())
57     {
58     printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to start transaction'\n");
59     return -1;
60     }
61
62 result = PQexec(connection, "SELECT name FROM tb_tariffs");
63
64 if (PQresultStatus(result) != PGRES_TUPLES_OK)
65     {
66     strError = PQresultErrorMessage(result);
67     PQclear(result);
68     printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): '%s'\n", strError.c_str());
69     if (RollbackTransaction())
70         {
71         printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to rollback transaction'\n");
72         }
73     return -1;
74     }
75
76 int tuples = PQntuples(result);
77
78 for (int i = 0; i < tuples; ++i)
79     {
80     tariffsList->push_back(PQgetvalue(result, i, 0));
81     }
82
83 PQclear(result);
84
85 if (CommitTransaction())
86     {
87     printfd(__FILE__, "POSTGRESQL_STORE::GetTariffsList(): 'Failed to commit transaction'\n");
88     return -1;
89     }
90
91 return 0;
92 }
93
94 //-----------------------------------------------------------------------------
95 int POSTGRESQL_STORE::AddTariff(const string & name) const
96 {
97 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
98
99 if (PQstatus(connection) != CONNECTION_OK)
100     {
101     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
102     if (Reset())
103         {
104         strError = "Connection lost";
105         printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
106         return -1;
107         }
108     }
109
110 PGresult * result;
111
112 if (StartTransaction())
113     {
114     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to start transaction'\n");
115     return -1;
116     }
117
118 std::string ename = name;
119
120 if (EscapeString(ename))
121     {
122     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
123     if (RollbackTransaction())
124         {
125         printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
126         }
127     return -1;
128     }
129
130 std::stringstream query;
131 query << "SELECT sp_add_tariff('" << ename << "', " << DIR_NUM << ")";
132
133 result = PQexec(connection, query.str().c_str());
134
135 if (PQresultStatus(result) != PGRES_TUPLES_OK)
136     {
137     strError = PQresultErrorMessage(result);
138     PQclear(result);
139     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): '%s'\n", strError.c_str());
140     if (RollbackTransaction())
141         {
142         printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
143         }
144     return -1;
145     }
146
147 PQclear(result);
148
149 if (CommitTransaction())
150     {
151     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to commit transaction'\n");
152     return -1;
153     }
154
155 return 0;
156 }
157 //-----------------------------------------------------------------------------
158 int POSTGRESQL_STORE::DelTariff(const string & name) const
159 {
160 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
161
162 if (PQstatus(connection) != CONNECTION_OK)
163     {
164     printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
165     if (Reset())
166         {
167         strError = "Connection lost";
168         printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
169         return -1;
170         }
171     }
172
173 PGresult * result;
174
175 if (StartTransaction())
176     {
177     printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to start transaction'\n");
178     return -1;
179     }
180
181 std::string ename = name;
182
183 if (EscapeString(ename))
184     {
185     printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to escape name'\n");
186     if (RollbackTransaction())
187         {
188         printfd(__FILE__, "POSTGRESQL_STORE::AddTariff(): 'Failed to rollback transaction'\n");
189         }
190     return -1;
191     }
192
193 std::stringstream query;
194 query << "DELETE FROM tb_tariffs WHERE name = '" << ename << "'";
195
196 result = PQexec(connection, query.str().c_str());
197
198 if (PQresultStatus(result) != PGRES_COMMAND_OK)
199     {
200     strError = PQresultErrorMessage(result);
201     PQclear(result);
202     printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): '%s'\n", strError.c_str());
203     if (RollbackTransaction())
204         {
205         printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to rollback transaction'\n");
206         }
207     return -1;
208     }
209
210 PQclear(result);
211
212 if (CommitTransaction())
213     {
214     printfd(__FILE__, "POSTGRESQL_STORE::DelTariff(): 'Failed to commit transaction'\n");
215     return -1;
216     }
217
218 return 0;
219 }
220 //-----------------------------------------------------------------------------
221 int POSTGRESQL_STORE::SaveTariff(const TARIFF_DATA & td,
222                                  const string & tariffName) const
223 {
224 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
225
226 if (PQstatus(connection) != CONNECTION_OK)
227     {
228     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
229     if (Reset())
230         {
231         strError = "Connection lost";
232         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
233         return -1;
234         }
235     }
236
237 PGresult * result;
238
239 if (StartTransaction())
240     {
241     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to start transaction'\n");
242     return -1;
243     }
244
245 std::string ename = tariffName;
246
247 if (EscapeString(ename))
248     {
249     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to escape name'\n");
250     if (RollbackTransaction())
251         {
252         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
253         }
254     return -1;
255     }
256
257 int32_t id, i;
258 double pda, pdb, pna, pnb;
259 int threshold;
260
261 std::stringstream query;
262 query << "SELECT pk_tariff FROM tb_tariffs WHERE name = '" << ename << "'";
263
264 result = PQexec(connection, query.str().c_str());
265
266 if (PQresultStatus(result) != PGRES_TUPLES_OK)
267     {
268     strError = PQresultErrorMessage(result);
269     PQclear(result);
270     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
271     if (RollbackTransaction())
272         {
273         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
274         }
275     return -1;
276     }
277
278 int tuples = PQntuples(result);
279
280 if (tuples != 1)
281     {
282     strError = "Failed to fetch tariff ID";
283     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
284     PQclear(result);
285     if (RollbackTransaction())
286         {
287         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
288         }
289     return -1;
290     }
291
292 std::stringstream tuple;
293 tuple << PQgetvalue(result, 0, 0);
294
295 PQclear(result);
296
297 tuple >> id;
298
299 query.str("");
300 query << "UPDATE tb_tariffs SET \
301               fee = " << td.tariffConf.fee << ", \
302               free = " << td.tariffConf.free << ", \
303               passive_cost = " << td.tariffConf.passiveCost << ", \
304               traff_type = " << td.tariffConf.traffType;
305
306 if (version > 6)
307     query << ", period = '" << TARIFF::PeriodToString(td.tariffConf.period) << "'";
308
309 query << " WHERE pk_tariff = " << id;
310
311 result = PQexec(connection, query.str().c_str());
312
313 if (PQresultStatus(result) != PGRES_COMMAND_OK)
314     {
315     strError = PQresultErrorMessage(result);
316     PQclear(result);
317     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
318     if (RollbackTransaction())
319         {
320         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
321         }
322     return -1;
323     }
324
325 PQclear(result);
326
327 for(i = 0; i < DIR_NUM; i++)
328     {
329
330     pda = td.dirPrice[i].priceDayA * 1024 * 1024;
331     pdb = td.dirPrice[i].priceDayB * 1024 * 1024;
332
333     if (td.dirPrice[i].singlePrice)
334         {
335         pna = pda;
336         pnb = pdb;
337         }
338     else
339         {
340         pna = td.dirPrice[i].priceNightA * 1024 * 1024;
341         pnb = td.dirPrice[i].priceNightB * 1024 * 1024;
342         }
343
344     if (td.dirPrice[i].noDiscount)
345         {
346         threshold = 0xffFFffFF;
347         }
348     else
349         {
350         threshold = td.dirPrice[i].threshold;
351         }
352
353     std::stringstream query;
354     query << "UPDATE tb_tariffs_params SET \
355                   price_day_a = " << pda << ", \
356                   price_day_b = " << pdb << ", \
357                   price_night_a = " << pna << ", \
358                   price_night_b = " << pnb << ", \
359                   threshold = " << threshold << ", \
360                   time_day_begins = CAST('" << td.dirPrice[i].hDay
361                                             << ":"
362                                             << td.dirPrice[i].mDay << "' AS TIME), \
363                   time_day_ends = CAST('" << td.dirPrice[i].hNight
364                                           << ":"
365                                           << td.dirPrice[i].mNight << "' AS TIME) \
366              WHERE fk_tariff = " << id << " AND dir_num = " << i;
367
368     result = PQexec(connection, query.str().c_str());
369
370     if (PQresultStatus(result) != PGRES_COMMAND_OK)
371         {
372         strError = PQresultErrorMessage(result);
373         PQclear(result);
374         printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): '%s'\n", strError.c_str());
375         if (RollbackTransaction())
376             {
377             printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to rollback transaction'\n");
378             }
379         return -1;
380         }
381
382     PQclear(result);
383     }
384
385 if (CommitTransaction())
386     {
387     printfd(__FILE__, "POSTGRESQL_STORE::SaveTariff(): 'Failed to commit transaction'\n");
388     return -1;
389     }
390
391 return 0;
392 }
393 //-----------------------------------------------------------------------------
394 int POSTGRESQL_STORE::RestoreTariff(TARIFF_DATA * td,
395                                   const string & tariffName) const
396 {
397 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
398
399 if (PQstatus(connection) != CONNECTION_OK)
400     {
401     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Connection lost. Trying to reconnect...'\n", strError.c_str());
402     if (Reset())
403         {
404         strError = "Connection lost";
405         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
406         return -1;
407         }
408     }
409
410 PGresult * result;
411
412 if (StartTransaction())
413     {
414     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to start transaction'\n");
415     return -1;
416     }
417
418 std::string ename = tariffName;
419
420 if (EscapeString(ename))
421     {
422     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to escape name'\n");
423     if (RollbackTransaction())
424         {
425         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
426         }
427     return -1;
428     }
429
430 td->tariffConf.name = tariffName;
431
432 std::stringstream query;
433 query << "SELECT pk_tariff, \
434                  fee, \
435                  free, \
436                  passive_cost, \
437                  traff_type";
438
439 if (version > 6)
440     query << ", period";
441
442 query << " FROM tb_tariffs WHERE name = '" << ename << "'";
443
444 result = PQexec(connection, query.str().c_str());
445
446 if (PQresultStatus(result) != PGRES_TUPLES_OK)
447     {
448     strError = PQresultErrorMessage(result);
449     PQclear(result);
450     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
451     if (RollbackTransaction())
452         {
453         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
454         }
455     return -1;
456     }
457
458 int tuples = PQntuples(result);
459
460 if (tuples != 1)
461     {
462     strError = "Failed to fetch tariff data";
463     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Invalid number of tuples. Wanted 1, actulally %d'\n", tuples);
464     PQclear(result);
465     if (RollbackTransaction())
466         {
467         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
468         }
469     return -1;
470     }
471
472 std::stringstream tuple;
473 tuple << PQgetvalue(result, 0, 0) << " ";
474 tuple << PQgetvalue(result, 0, 1) << " ";
475 tuple << PQgetvalue(result, 0, 2) << " ";
476 tuple << PQgetvalue(result, 0, 3) << " ";
477 tuple << PQgetvalue(result, 0, 4) << " ";
478
479 int id;
480 tuple >> id;
481 tuple >> td->tariffConf.fee;
482 tuple >> td->tariffConf.free;
483 tuple >> td->tariffConf.passiveCost;
484 tuple >> td->tariffConf.traffType;
485
486 if (version > 6)
487     td->tariffConf.period = TARIFF::StringToPeriod(PQgetvalue(result, 0, 5));
488
489 PQclear(result);
490
491 query.str("");
492 query << "SELECT dir_num, \
493                  price_day_a, \
494                  price_day_b, \
495                  price_night_a, \
496                  price_night_b, \
497                  threshold, \
498                  EXTRACT(hour FROM time_day_begins), \
499                  EXTRACT(minute FROM time_day_begins), \
500                  EXTRACT(hour FROM time_day_ends), \
501                  EXTRACT(minute FROM time_day_ends) \
502           FROM tb_tariffs_params \
503           WHERE fk_tariff = " << id;
504
505 result = PQexec(connection, query.str().c_str());
506
507 if (PQresultStatus(result) != PGRES_TUPLES_OK)
508     {
509     strError = PQresultErrorMessage(result);
510     PQclear(result);
511     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): '%s'\n", strError.c_str());
512     if (RollbackTransaction())
513         {
514         printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to rollback transaction'\n");
515         }
516     return -1;
517     }
518
519 tuples = PQntuples(result);
520
521 if (tuples != DIR_NUM)
522     {
523     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Tariff params count and DIR_NUM does not feet (wanted %d, actually %d)'\n", DIR_NUM, tuples);
524     }
525
526 for (int i = 0; i < std::min(tuples, DIR_NUM); ++i)
527     {
528     std::stringstream tuple;
529     tuple << PQgetvalue(result, i, 0) << " ";
530     tuple << PQgetvalue(result, i, 1) << " ";
531     tuple << PQgetvalue(result, i, 2) << " ";
532     tuple << PQgetvalue(result, i, 3) << " ";
533     tuple << PQgetvalue(result, i, 4) << " ";
534     tuple << PQgetvalue(result, i, 5) << " ";
535     tuple << PQgetvalue(result, i, 6) << " ";
536     tuple << PQgetvalue(result, i, 7) << " ";
537     tuple << PQgetvalue(result, i, 8) << " ";
538     tuple << PQgetvalue(result, i, 9) << " ";
539
540     int dir;
541
542     tuple >> dir;
543     tuple >> td->dirPrice[dir].priceDayA;
544     td->dirPrice[dir].priceDayA /= 1024 * 1024;
545     tuple >> td->dirPrice[dir].priceDayB;
546     td->dirPrice[dir].priceDayB /= 1024 * 1024;
547     tuple >> td->dirPrice[dir].priceNightA;
548     td->dirPrice[dir].priceNightA /= 1024 * 1024;
549     tuple >> td->dirPrice[dir].priceNightB;
550     td->dirPrice[dir].priceNightB /= 1024 * 1024;
551     tuple >> td->dirPrice[dir].threshold;
552     tuple >> td->dirPrice[dir].hDay;
553     tuple >> td->dirPrice[dir].mDay;
554     tuple >> td->dirPrice[dir].hNight;
555     tuple >> td->dirPrice[dir].mNight;
556
557     if (td->dirPrice[dir].priceDayA == td->dirPrice[dir].priceNightA &&
558         td->dirPrice[dir].priceDayB == td->dirPrice[dir].priceNightB)
559         {
560         td->dirPrice[dir].singlePrice = true;
561         }
562     else
563         {
564         td->dirPrice[dir].singlePrice = false;
565         }
566     if (td->dirPrice[dir].threshold == (int)0xffFFffFF)
567         {
568         td->dirPrice[dir].noDiscount = true;
569         }
570     else
571         {
572
573         td->dirPrice[dir].noDiscount = false;
574         }
575
576     }
577
578 PQclear(result);
579
580 if (CommitTransaction())
581     {
582     printfd(__FILE__, "POSTGRESQL_STORE::RestoreTariff(): 'Failed to commit transaction'\n");
583     return -1;
584     }
585
586 return 0;
587 }
588 //-----------------------------------------------------------------------------
589