-
Notifications
You must be signed in to change notification settings - Fork 1
/
billing.h
370 lines (344 loc) · 8.79 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#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, SqlDb *sqlDb = NULL);
bool isSensorOk(SqlDb *sqlDb = NULL);
void loadCond(SqlDb *sqlDb = NULL);
bool checkIP(vmIP 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(vmIP 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(vmIP ip, eBilingSide side);
bool checkNumber(const char *number, eBilingSide side);
bool checkDomain(const char *domain, 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;
ListCheckString list_domain_src;
ListCheckString list_domain_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:
enum eNumberFormat {
_number_format_na,
_number_format_original,
_number_format_normalized,
_number_format_both
};
enum eNumberType {
_number_type_na,
_number_type_local,
_number_type_international,
_number_type_both
};
public:
cBillingRuleNumber();
~cBillingRuleNumber();
void load(SqlDb_row *row);
void regexp_create();
static eNumberFormat numberFormatEnum(const char *str);
static eNumberType numberTypeEnum(const char *str);
static string numberFormatString(eNumberFormat numbFormat);
static string numberTypeString(eNumberType numbType);
private:
string name;
string number_prefix;
string number_fixed;
string number_regex;
cPeakDefinition peak_definition;
double price;
double price_peak;
unsigned t1;
unsigned t2;
eNumberFormat use_for_number_format;
eNumberType use_for_number_type;
cRegExp *regexp;
friend class cBillingRule;
};
class cBillingRule {
public:
~cBillingRule();
void load(SqlDb_row *row);
void loadNumbers(SqlDb *sqlDb = NULL);
void freeNumbers();
double billing(time_t time, unsigned duration, const char *number, const char *number_normalized,
bool isLocalNumber, cStateHolidays *holidays, const char *timezone,
vector<string> *debug = NULL);
private:
unsigned id;
string name;
unsigned holiday_id;
cPeakDefinition peak_definition;
double price;
double price_peak;
unsigned t1;
unsigned t2;
cBillingRuleNumber::eNumberFormat use_for_number_format;
cBillingRuleNumber::eNumberType use_for_number_type;
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;
bool enable_by_domain;
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);
string getCurrencyCode(unsigned 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(SqlDb *sqlDb = NULL);
bool isSet() {
return(set);
}
bool billing(time_t time, unsigned duration,
vmIP ip_src, vmIP ip_dst,
const char *number_src, const char *number_dst,
const char *domain_src, const char *domain_dst,
double *operator_price, double *customer_price,
unsigned *operator_currency_id, unsigned *customer_currency_id,
unsigned *operator_id, unsigned *customer_id,
vector<string> *operator_debug = NULL, vector<string> *customer_debug = NULL);
bool saveAggregation(time_t time,
vmIP ip_src, vmIP ip_dst,
const char *number_src, const char *number_dst,
const char *domain_src, const char *domain_dst,
double operator_price, double customer_price,
unsigned operator_currency_id, unsigned customer_currency_id,
list<string> *inserts);
sBillingAgregationSettings getAgregSettings() {
return(agreg_settings->settings);
}
static vector<sAgregationTypePart> getAgregTypeParts(sBillingAgregationSettings *settings);
string getCurrencyCode(unsigned id);
string test(string calls_string, bool json_rslt);
string test(const char *id, const char *time, const char *duration,
const char *ip_src, const char *ip_dst,
const char *number_src, const char *number_dst,
const char *domain_src, const char *domain_dst,
const char *sensor_id,
const char *verify_operator_price, const char *verify_customer_price,
bool json_rslt);
private:
void lock() {
while(__sync_lock_test_and_set(&_sync, 1));
}
void unlock() {
__sync_lock_release(&_sync);
}
private:
bool set;
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(SqlDb *sqlDb);
void termBilling();
void refreshBilling();
#endif //BILLING_H