]> git.stg.codes - stg.git/blob - stargazer/tariff_impl.cpp
Port to CMake, get rid of os_int.h.
[stg.git] / stargazer / tariff_impl.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 "tariff_impl.h"
32
33 #include "stg_timer.h"
34 #include "stg/common.h"
35
36 #include <ctime>
37 #include <algorithm> // std::max
38
39 //-----------------------------------------------------------------------------
40 TARIFF_IMPL & TARIFF_IMPL::operator=(const TARIFF_DATA & td)
41 {
42 tariffData = td;
43 return *this;
44 }
45 //-----------------------------------------------------------------------------
46 double TARIFF_IMPL::GetPriceWithTraffType(uint64_t up,
47                                      uint64_t down,
48                                      int dir,
49                                      time_t t) const
50 {
51 return GetPriceWithoutFreeMb(dir, GetTraffByType(up, down) / (1024 * 1024), t);
52 }
53 //-----------------------------------------------------------------------------
54 int64_t TARIFF_IMPL::GetTraffByType(uint64_t up, uint64_t down) const
55 {
56 switch (tariffData.tariffConf.traffType)
57     {
58     case TRAFF_UP:
59         return up;
60
61     case TRAFF_DOWN:
62         return down;
63
64     case TRAFF_MAX:
65         return std::max(up, down);
66
67     default:  //TRAFF_UP_DOWN:
68         return up + down;
69     }
70 }
71 //-----------------------------------------------------------------------------
72 int TARIFF_IMPL::GetThreshold(int dir) const
73 {
74     return tariffData.dirPrice[dir].threshold;
75 }
76 //-----------------------------------------------------------------------------
77 void TARIFF_IMPL::Print() const
78 {
79 printfd(__FILE__, "Traiff name: %s\n", tariffData.tariffConf.name.c_str());
80 }
81 //-----------------------------------------------------------------------------
82 int TARIFF_IMPL::Interval(int dir, time_t t) const
83 {
84 // Start of the day (and end of the night) in sec from 00:00:00
85 int s1 = tariffData.dirPrice[dir].hDay * 3600 +
86          tariffData.dirPrice[dir].mDay * 60;
87 // Start of the night (and end of the day) in sec from 00:00:00
88 int s2 = tariffData.dirPrice[dir].hNight * 3600 +
89          tariffData.dirPrice[dir].mNight * 60;
90
91 struct tm * lt;
92
93 lt = localtime(&t);
94
95 // Position of time t in sec from 00:00:00
96 // Ignoring seconds due to minute precision
97 int lts = lt->tm_hour * 3600 + lt->tm_min * 60;
98
99 if (s1 < s2)
100     {
101     // Normal situation (00:00:00 is a night)
102     if (lts > s1 && lts < s2)
103         return TARIFF_DAY;
104     else
105         return TARIFF_NIGHT;
106     }
107 else
108     {
109     // Not so common but possible situation (00:00:00 is a day)
110     if (lts < s1 && lts > s2)
111         return TARIFF_NIGHT;
112     else
113         return TARIFF_DAY;
114     }
115 }
116 //-----------------------------------------------------------------------------
117 double TARIFF_IMPL::GetPriceWithoutFreeMb(int dir, int64_t mb, time_t t) const
118 {
119 int interval = Interval(dir, t);
120
121 /*
122  * 0011 - NB
123  * *01* - NA
124  * 0**1 - DB
125  * **** - DA
126  */
127
128 bool nd = tariffData.dirPrice[dir].noDiscount;
129 bool sp = tariffData.dirPrice[dir].singlePrice;
130 bool th = (interval == TARIFF_NIGHT);
131 bool tr = (mb > tariffData.dirPrice[dir].threshold);
132
133 if (!nd && !sp && th && tr)
134     return tariffData.dirPrice[dir].priceNightB;
135 else if (!nd && tr)
136     return tariffData.dirPrice[dir].priceDayB;
137 else if (!sp && th)
138     return tariffData.dirPrice[dir].priceNightA;
139 else
140     return tariffData.dirPrice[dir].priceDayA;
141 }
142 //-----------------------------------------------------------------------------
143 std::string TARIFF_IMPL::TariffChangeIsAllowed(const TARIFF & to, time_t currentTime) const
144 {
145 time_t timeout = GetChangePolicyTimeout();
146 if (currentTime > timeout && timeout != 0)
147     return "";
148 switch (GetChangePolicy())
149     {
150     case TARIFF::ALLOW:
151         return "";
152     case TARIFF::TO_CHEAP:
153         if (to.GetFee() < GetFee())
154             return "";
155         else
156             return "New tariff '" + to.GetName() + "' is more expensive than current tariff '" + GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
157     case TARIFF::TO_EXPENSIVE:
158         if (to.GetFee() >= GetFee())
159             return "";
160         else
161             return "New tariff '" + to.GetName() + "' is more cheap than current tariff '" + GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
162     case TARIFF::DENY:
163         return "Current tariff '" + GetName() + "', new tariff '" + to.GetName() + "'. The policy is '" + TARIFF::ChangePolicyToString(GetChangePolicy()) + "'.";
164     }
165 return "";
166 }
167 //-----------------------------------------------------------------------------