]> git.stg.codes - stg.git/blob - projects/stargazer/tariff.cpp
Добавление исходников
[stg.git] / projects / stargazer / tariff.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  *    Date: 07.11.2007
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26  $Revision: 1.11 $
27  $Date: 2010/10/07 16:57:21 $
28  $Author: faust $
29  */
30
31 #include <ctime>
32 #include <algorithm> // std::max
33
34 #include "tariff.h"
35 #include "stg_timer.h"
36
37 //-----------------------------------------------------------------------------
38 TARIFF & TARIFF::operator=(const TARIFF_DATA & td)
39 {
40 tariffData = td;
41 return *this;
42 }
43 //-----------------------------------------------------------------------------
44 TARIFF & TARIFF::operator=(const TARIFF & t)
45 {
46 tariffData = t.tariffData;
47 return *this;
48 }
49 //-----------------------------------------------------------------------------
50 double TARIFF::GetPriceWithTraffType(uint64_t up,
51                                      uint64_t down,
52                                      int dir,
53                                      time_t t) const
54 {
55 return GetPriceWithoutFreeMb(dir, GetTraffByType(up, down) / (1024 * 1024), t);
56 }
57 //-----------------------------------------------------------------------------
58 int64_t TARIFF::GetTraffByType(uint64_t up, uint64_t down) const
59 {
60 switch (tariffData.tariffConf.traffType)
61     {
62     case TRAFF_UP:
63         return up;
64
65     case TRAFF_DOWN:
66         return down;
67
68     case TRAFF_MAX:
69         return std::max(up, down);
70
71     default:  //TRAFF_UP_DOWN:
72         return up + down;
73     }
74 }
75 //-----------------------------------------------------------------------------
76 int TARIFF::GetThreshold(int dir) const
77 {
78     return tariffData.dirPrice[dir].threshold;
79 }
80 //-----------------------------------------------------------------------------
81 void TARIFF::PrintTariff() const
82 {
83 //printfd(__FILE__, "Traiff name: %s\n", tariffConf.name.c_str());
84 //printfd(__FILE__, "Price: %8.3f   %8.3f   \n", dirPrice[0].GetPrice(0, 0), dirPrice[0].GetPrice(1, 0));
85 //printfd(__FILE__, "Price: %8.3f   %8.3f   Thr:%d\n", dirPrice[1].GetPrice(0), dirPrice[1].GetPrice(1), dirPrice[1].GetThreshold());
86 //printfd(__FILE__, "Price: %8.3f   %8.3f   Thr:%d\n", dirPrice[2].GetPrice(0), dirPrice[2].GetPrice(1), dirPrice[2].GetThreshold());
87 //printfd(__FILE__, "Price: %8.3f   %8.3f   Thr:%d\n", dirPrice[3].GetPrice(0), dirPrice[3].GetPrice(1), dirPrice[3].GetThreshold());
88 //printfd(__FILE__, "Free: %8.3f\n", tariffConf.free);
89 }
90 //-----------------------------------------------------------------------------
91 void TARIFF::GetDirPrice(int dir, DIRPRICE_DATA * dd) const
92 {
93 *dd = tariffData.dirPrice[dir];
94 }
95 //-----------------------------------------------------------------------------
96 void TARIFF::GetTariffData(TARIFF_DATA * td) const
97 {
98 *td = tariffData;
99 }
100 //-----------------------------------------------------------------------------
101 int TARIFF::Interval(int dir, time_t t) const
102 {
103 // Start of the day (and end of the night) in sec from 00:00:00
104 int s1 = tariffData.dirPrice[dir].hDay * 3600 +
105          tariffData.dirPrice[dir].mDay * 60;
106 // Start of the night (and end of the day) in sec from 00:00:00
107 int s2 = tariffData.dirPrice[dir].hNight * 3600 +
108          tariffData.dirPrice[dir].mNight * 60;
109
110 struct tm * lt;
111
112 lt = localtime(&t);
113
114 // Position of time t in sec from 00:00:00
115 // Ignoring seconds due to minute precision
116 int lts = lt->tm_hour * 3600 + lt->tm_min * 60;
117
118 if (s1 < s2)
119     {
120     // Normal situation (00:00:00 is a night)
121     if (lts > s1 && lts < s2)
122         return TARIFF_DAY;
123     else
124         return TARIFF_NIGHT;
125     }
126 else
127     {
128     // Not so common but possible situation (00:00:00 is a day)
129     if (lts < s1 && lts > s2)
130         return TARIFF_NIGHT;
131     else
132         return TARIFF_DAY;
133     }
134 }
135 //-----------------------------------------------------------------------------
136 double TARIFF::GetPriceWithoutFreeMb(int dir, int mb, time_t t) const
137 {
138 int interval = Interval(dir, t);
139
140 /*
141  * 0011 - NB
142  * *01* - NA
143  * 0**1 - DB
144  * **** - DA
145  */
146
147 bool nd = tariffData.dirPrice[dir].noDiscount;
148 bool sp = tariffData.dirPrice[dir].singlePrice;
149 bool th = (interval == TARIFF_NIGHT);
150 bool tr = (mb > tariffData.dirPrice[dir].threshold);
151
152 if (!nd && !sp && th && tr)
153     return tariffData.dirPrice[dir].priceNightB;
154 else if (!nd && tr)
155     return tariffData.dirPrice[dir].priceDayB;
156 else if (!sp && th)
157     return tariffData.dirPrice[dir].priceNightA;
158 else
159     return tariffData.dirPrice[dir].priceDayA;
160
161 /*if (tariffData.dirPrice[dir].noDiscount && tariffData.dirPrice[dir].singlePrice)
162     {
163     return tariffData.dirPrice[dir].priceDayA;
164     }
165 else
166     {
167     if (tariffData.dirPrice[dir].noDiscount)
168         {
169         // Without threshold
170         if (interval == TARIFF_DAY)
171             return tariffData.dirPrice[dir].priceDayA;
172         else
173             return tariffData.dirPrice[dir].priceNightA;
174         }
175
176     if (tariffData.dirPrice[dir].singlePrice)
177         {
178         // Without day/night
179         if (mb < tariffData.dirPrice[dir].threshold)
180             return tariffData.dirPrice[dir].priceDayA;
181         else
182             return tariffData.dirPrice[dir].priceDayB;
183         }
184
185     if (mb < tariffData.dirPrice[dir].threshold)
186         {
187         if (interval == TARIFF_DAY)
188             return tariffData.dirPrice[dir].priceDayA;
189         else
190             return tariffData.dirPrice[dir].priceNightA;
191         }
192     else
193         {
194         if (interval == TARIFF_DAY)
195             return tariffData.dirPrice[dir].priceDayB;
196         else
197             return tariffData.dirPrice[dir].priceNightB;
198         }
199     }*/
200 }
201 //-----------------------------------------------------------------------------