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