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
|
#!/usr/bin/perl -w
use strict;
use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
use FS::UID qw( adminsuidsetup );
use FS::Record qw( qsearch qsearchs );
use FS::cdr;
use FS::svc_phone;
use FS::part_pkg;
my $user = shift or die &usage;
daemonize1('freeside-cdrrated');
drop_root();
adminsuidsetup($user);
logfile( "%%%FREESIDE_LOG%%%/cdrrated-log.". $FS::UID::datasrc );
daemonize2();
our $conf = new FS::Conf;
die "not running; cdr-prerate conf option is off\n"
unless _shouldrun();
#--
my $extra_sql = '';
my @cdrtypenums = $conf->config('cdr-prerate-cdrtypenums');
if ( @cdrtypenums ) {
$extra_sql .= ' AND cdrtypenum IN ('. join(',', @cdrtypenums ). ')';
}
#our %svcnum = (); # phonenum => svcnum
our %svc_phone = (); # phonenum => svc_phone
our %pkgnum = (); # phonenum => pkgnum
our %cust_pkg = (); # pkgnum => cust_pkg (NOT phonenum => cust_pkg!)
our %pkgpart = (); # phonenum => pkgpart
our %part_pkg = (); # pkgpart => part_pkg
#some false laziness w/freeside-cdrrewrited
while (1) {
my $found = 0;
foreach my $cdr (
qsearch( {
'table' => 'cdr',
'hashref' => { 'freesidestatus' => '' },
'extra_sql' => $extra_sql.
' LIMIT 1024'. #arbitrary, but don't eat too much memory
' FOR UPDATE',
} )
) {
$found = 1;
#find the matching service - some weird false laziness w/svc_phone::get_cdrs
#in charged_party or src
#hmm... edge case; get_cdrs rating will match a src if a charged_party is
# present #but doesn't match a service...
my $number = $cdr->charged_party || $cdr->src;
#technically default_prefix. phonenum or phonenum (or default_prefix without the + . phonenum)
#but for now we're just assuming default_prefix is +1
my $prefix = '+1'; #$options{'default_prefix'};
$number = substr($number, length($prefix))
if $prefix eq substr($number, 0, length($prefix));
if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
$prefix = $1;
$number = substr($number, length($prefix))
if $prefix eq substr($number, 0, length($prefix));
}
unless ( $svc_phone{$number} ) {
#only phone number matching supported right now
my $svc_phone = qsearchs('svc_phone', { 'phonenum' => $number } );
unless ( $svc_phone ) {
#XXX set freesideratestatus or something so we don't keep retrying?
next;
}
$svc_phone{$number} = $svc_phone;
}
unless ( $pkgnum{$number} ) {
my $cust_pkg = $svc_phone{$number}->cust_svc->cust_pkg;
unless ( $cust_pkg ) {
#XXX unlinked svc_phone?
# warn and also set freesideratestatus or somesuch?
next;
}
$pkgnum{$number} = $cust_pkg->pkgnum;
$cust_pkg{$cust_pkg->pkgnum} ||= $cust_pkg;
}
unless ( $pkgpart{$number} ) {
#get the package, search through the part_pkg and linked for a voip_cdr def w/matching cdrtypenum (or no use_cdrtypenum)
my @part_pkg =
grep { $_->plan eq 'voip_cdr'
&& ( ! length($_->option_cacheable('use_cdrtypenum'))
|| $_->option_cacheable('use_cdrtypenum')
eq $cdr->cdrtypenum #eq otherwise 0 matches ''
)
&& ( ! length($_->option_cacheable('ignore_cdrtypenum'))
|| $_->option_cacheable('ignore_cdrtypenum')
ne $cdr->cdrtypenum #ne otherwise 0 matches ''
)
}
$cust_pkg{ $pkgnum{$number} }->part_pkg->self_and_bill_linked;
if ( ! @part_pkg ) {
#XXX no package for this CDR
# warn and also set freesideratestatus or somesuch?
# or at least warn
next;
} elsif ( scalar(@part_pkg) > 1 ) {
warn "multiple package could rate CDR ". $cdr->acctid. "\n";
# and also set freesideratestatus or somesuch?
next;
}
$pkgpart{$number} = $part_pkg[0]->pkgpart;
$part_pkg{ $part_pkg[0]->pkgpart } ||= $part_pkg[0];
}
if ( $part_pkg{ $pkgpart{$number} }->option('min_included') ) {
#then we can't prerate this CDR
#some sort of warning?
# (sucks if you're depending on credit limit fraud warnings)
next;
}
my $error = $cdr->rate(
'part_pkg' => $part_pkg{ $pkgpart{$number} },
'cust_pkg' => $cust_pkg{ $pkgnum{$number} },
'svcnum' => $svc_phone{$number}->svcnum,
);
if ( $error ) {
warn "Can't prerate CDR ". $cdr->acctid. ' to '. $cdr->dst. ": $error";
#could be an included minutes CDR, so don't sleep 30;
} else {
#this could get expensive on a per-call basis
# trigger in a separate process with less frequency?
my $cust_main = $cust_pkg{ $pkgnum{$number} }->cust_main;
my $error = $cust_main->check_credit_limit;
if ( $error ) {
#"should never happen" normally, but as a daemon, better to survive
# e.g. database going away and coming back and resume doing our thing
warn $error;
sleep 30;
}
}
last if sigterm() || sigint();
}
myexit() if sigterm() || sigint();
sleep 5 unless $found;
}
#--
sub _shouldrun {
$conf->exists('cdr-prerate');
}
sub usage {
die "Usage:\n\n freeside-cdrrewrited user\n";
}
=head1 NAME
freeside-cdrrated - Real-time daemon for CDR rating
=head1 SYNOPSIS
freeside-cdrrated
=head1 DESCRIPTION
Runs continuously, searches for CDRs and which can be pre-rated, and rates them.
=head1 SEE ALSO
cdr-prerate configuration setting
=cut
1;
|