forked from voipmonitor/sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilling.h
319 lines (293 loc) · 7.05 KB
/
billing.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#ifndef BILLING_H
#define BILLING_H
#include "tools.h"
#include "sql_db.h"
#include "country_detect.h"
enum eBilingSide {
_billing_side_src,
_billing_side_dst
};
enum eBilingTypeAssignment {
_billing_ta_operator,
_billing_ta_customer
};
enum eBillingHolidayType {
_billing_holiday_na,
_billing_holiday_fixed,
_billing_holiday_movable,
_billing_holiday_easter_monday,
_billing_holiday_easter_friday
};
class cBillingAssignment {
public:
cBillingAssignment(eBilingTypeAssignment typeAssignment);
void load(SqlDb_row *row);
bool isSensorOk(SqlDb *sqlDb = NULL);
void loadCond(SqlDb *sqlDb = NULL);
bool checkIP(u_int32_t ip);
bool checkNumber(const char *number);
private:
eBilingTypeAssignment typeAssignment;
unsigned id;
unsigned billing_rule_id;
string name;
bool limitation_for_sensors;
CheckInternational checkInternational;
ListIP list_ip;
ListPhoneNumber list_number;
friend class cBillingAssignments;
friend class cBilling;
};
class cBillingAssignments {
public:
cBillingAssignments();
~cBillingAssignments();
void load(SqlDb *sqlDb = NULL);
void clear(bool useLock = true);
unsigned findBillingRuleIdForIP(u_int32_t ip, eBilingTypeAssignment typeAssignment,
unsigned *assignment_id);
unsigned findBillingRuleIdForNumber(const char *number, eBilingTypeAssignment typeAssignment,
unsigned *assignment_id, CountryPrefixes *countryPrefixes);
private:
void lock() {
while(__sync_lock_test_and_set(&_sync, 1));
}
void unlock() {
__sync_lock_release(&_sync);
}
private:
map<unsigned, cBillingAssignment*> operators;
map<unsigned, cBillingAssignment*> customers;
volatile int _sync;
friend class cBilling;
};
class cBillingExclude {
public:
cBillingExclude(bool agregation = false);
void load(SqlDb *sqlDb = NULL);
bool checkIP(u_int32_t ip, eBilingSide side);
bool checkNumber(const char *number, eBilingSide side);
private:
void lock() {
while(__sync_lock_test_and_set(&_sync, 1));
}
void unlock() {
__sync_lock_release(&_sync);
}
private:
bool agregation;
ListIP list_ip_src;
ListIP list_ip_dst;
ListPhoneNumber list_number_src;
ListPhoneNumber list_number_dst;
volatile int _sync;
};
class cStateHolidays {
public:
struct sHoliday {
sHoliday() {
type = _billing_holiday_fixed;
memset(&day, 0, sizeof(day));
}
void load(SqlDb_row *row);
bool isHoliday(tm &day, const char *timezone);
eBillingHolidayType type;
tm day;
};
public:
void load(SqlDb_row *row);
void loadHolidays(SqlDb *sqlDb = NULL);
bool isHoliday(tm &day, const char *timezone);
private:
unsigned id;
string name;
string country_code;
list<sHoliday> holidays;
friend class cStatesHolidays;
};
class cStatesHolidays {
public:
cStatesHolidays();
void load(SqlDb *sqlDb = NULL);
bool isHoliday(unsigned id, tm &day, const char *timezone);
private:
void lock() {
while(__sync_lock_test_and_set(&_sync, 1));
}
void unlock() {
__sync_lock_release(&_sync);
}
private:
map<unsigned, cStateHolidays> holidays;
volatile int _sync;
friend class cBilling;
};
class cPeakDefinition {
public:
cPeakDefinition();
void load(SqlDb_row *row, const char *fieldNamePrefix = NULL);
bool peakCheck(tm &time, cStateHolidays *holidays, tm *toTime, const char *timezone);
private:
bool enable;
unsigned peak_starts_hour;
unsigned peak_starts_minute;
unsigned peak_ends_hour;
unsigned peak_ends_minute;
unsigned weekend_start;
friend class cBillingRuleNumber;
friend class cBillingRule;
};
class cBillingRuleNumber {
public:
void load(SqlDb_row *row);
private:
string name;
string number_prefix;
string number_fixed;
cPeakDefinition peak_definition;
double price;
double price_peak;
unsigned t1;
unsigned t2;
friend class cBillingRule;
};
class cBillingRule {
public:
void load(SqlDb_row *row);
void loadNumbers(SqlDb *sqlDb = NULL);
double billing(time_t time, unsigned duration, const char *number, const char *number_normalized,
cStateHolidays *holidays, const char *timezone);
private:
unsigned id;
string name;
unsigned holiday_id;
cPeakDefinition peak_definition;
double price;
double price_peak;
unsigned t1;
unsigned t2;
bool default_customer;
string currency_code;
unsigned currency_id;
string timezone_name;
list<cBillingRuleNumber> numbers;
friend class cBillingRules;
friend class cBilling;
};
class cBillingRules {
public:
cBillingRules();
~cBillingRules();
void load(SqlDb *sqlDb = NULL);
void clear(bool useLock = true);
unsigned getDefaultCustomerBillingId();
private:
void lock() {
while(__sync_lock_test_and_set(&_sync, 1));
}
void unlock() {
__sync_lock_release(&_sync);
}
private:
map<unsigned, cBillingRule*> rules;
volatile int _sync;
friend class cBilling;
};
struct sBillingAgregationSettings {
bool enable_by_ip;
bool enable_by_number;
unsigned week_start;
unsigned hours_history_in_days;
unsigned days_history_in_weeks;
unsigned weeks_history_in_months;
unsigned months_history_in_years;
};
class cBillingAgregationSettings {
public:
cBillingAgregationSettings();
void load(SqlDb *sqlDb = NULL);
void clear();
sBillingAgregationSettings getAgregSettings() {
return(settings);
}
private:
sBillingAgregationSettings settings;
friend class cBilling;
};
class cCurrency {
public:
struct sCurrencyItem {
unsigned id;
string code;
string name;
string country_code;
bool main_currency;
double exchange_rate;
};
public:
cCurrency();
void load(SqlDb *sqlDb = NULL);
void clear(bool useLock = true);
double getExchangeRateToMainCurency(unsigned from_id);
private:
void lock() {
while(__sync_lock_test_and_set(&_sync, 1));
}
void unlock() {
__sync_lock_release(&_sync);
}
private:
list<sCurrencyItem> items;
volatile int _sync;
};
class cBilling {
public:
struct sAgregationTypePart {
string type;
string type_part;
bool week;
unsigned limit;
};
public:
cBilling();
~cBilling();
void load();
bool billing(time_t time, unsigned duration,
u_int32_t ip_src, u_int32_t ip_dst,
const char *number_src, const char *number_dst,
double *operator_price, double *customer_price,
unsigned *operator_currency_id, unsigned *customer_currency_id,
unsigned *operator_id, unsigned *customer_id);
list<string> saveAgregation(time_t time,
u_int32_t ip_src, u_int32_t ip_dst,
const char *number_src, const char *number_dst,
double operator_price, double customer_price,
unsigned operator_currency_id, unsigned customer_currency_id);
sBillingAgregationSettings getAgregSettings() {
return(agreg_settings->settings);
}
static vector<sAgregationTypePart> getAgregTypeParts(sBillingAgregationSettings *settings);
private:
void lock() {
while(__sync_lock_test_and_set(&_sync, 1));
}
void unlock() {
__sync_lock_release(&_sync);
}
private:
cBillingRules *rules;
cBillingAssignments *assignments;
cBillingExclude *exclude;
cStatesHolidays *holidays;
CountryPrefixes *countryPrefixes;
CheckInternational *checkInternational;
cBillingExclude *agreg_exclude;
cBillingAgregationSettings *agreg_settings;
cCurrency *currency;
string gui_timezone;
volatile int _sync;
};
void initBilling();
void termBilling();
void refreshBilling();
#endif //BILLING_H