4 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
5 use FS::UID qw( adminsuidsetup );
6 use FS::Record qw( qsearch qsearchs );
11 my $user = shift or die &usage;
13 daemonize1('freeside-cdrrated');
17 adminsuidsetup($user);
19 logfile( "%%%FREESIDE_LOG%%%/cdrrated-log.". $FS::UID::datasrc );
23 our $conf = new FS::Conf;
25 die "not running; cdr-prerate conf option is off\n"
31 my @cdrtypenums = $conf->config('cdr-prerate-cdrtypenums');
33 $extra_sql .= ' AND cdrtypenum IN ('. join(',', @cdrtypenums ). ')';
36 our %svcnum = (); # phonenum => svcnum
37 our %pkgnum = (); # phonenum => pkgnum
38 our %cust_pkg = (); # pkgnum => cust_pkg (NOT phonenum => cust_pkg!)
39 our %pkgpart = (); # phonenum => pkgpart
40 our %part_pkg = (); # pkgpart => part_pkg
42 #some false laziness w/freeside-cdrrewrited
50 'hashref' => { 'freesidestatus' => '' },
51 'extra_sql' => $extra_sql.
52 ' LIMIT 1024'. #arbitrary, but don't eat too much memory
60 #find the matching service - some weird false laziness w/svc_phone::get_cdrs
62 #in charged_party or src
63 #hmm... edge case; get_cdrs rating will match a src if a charged_party is
64 # present #but doesn't match a service...
65 my $number = $cdr->charged_party || $cdr->src;
67 #technically default_prefix. phonenum or phonenum (or default_prefix without the + . phonenum)
68 #but for now we're just assuming default_prefix is +1
69 my $prefix = '+1'; #$options{'default_prefix'};
71 $number = substr($number, length($prefix))
72 if $prefix eq substr($number, 0, length($prefix));
73 if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
75 $number = substr($number, length($prefix))
76 if $prefix eq substr($number, 0, length($prefix));
79 unless ( $svcnum{$number} ) {
80 #only phone number matching supported right now
81 my $svc_phone = qsearchs('svc_phone', { 'phonenum' => $number } );
82 unless ( $svc_phone ) {
83 #XXX set freesideratestatus or something so we don't keep retrying?
87 $svcnum{$number} = $svc_phone->svcnum;
89 my $cust_pkg = $svc_phone->cust_svc->cust_pkg;
90 unless ( $cust_pkg ) {
91 #XXX unlinked svc_phone?
92 # warn and also set freesideratestatus or somesuch?
96 $pkgnum{$number} = $cust_pkg->pkgnum;
97 $cust_pkg{$cust_pkg->pkgnum} ||= $cust_pkg;
99 #get the package, search through the part_pkg and linked for a voip_cdr def w/matching cdrtypenum (or no use_cdrtypenum)
101 grep { $_->plan eq 'voip_cdr'
102 && ( ! length($_->option_cacheable('use_cdrtypenum'))
103 || $_->option_cacheable('use_cdrtypenum')
104 eq $cdr->cdrtypenum #eq otherwise 0 matches ''
106 && ( ! length($_->option_cacheable('ignore_cdrtypenum'))
107 || $_->option_cacheable('ignore_cdrtypenum')
108 ne $cdr->cdrtypenum #ne otherwise 0 matches ''
112 $cust_pkg->part_pkg->self_and_bill_linked;
115 #XXX no package for this CDR
116 # warn and also set freesideratestatus or somesuch?
119 } elsif ( scalar(@part_pkg) > 1 ) {
120 warn "multiple package could rate CDR ". $cdr->acctid. "\n";
121 # and also set freesideratestatus or somesuch?
125 $pkgpart{$number} = $part_pkg[0]->pkgpart;
126 $part_pkg{ $part_pkg[0]->pkgpart } ||= $part_pkg[0];
130 if ( $part_pkg{ $pkgpart{$number} }->option('min_included') ) {
131 #then we can't prerate this CDR
132 #some sort of warning?
133 # (sucks if you're depending on credit limit fraud warnings)
137 my $error = $cdr->rate(
138 'part_pkg' => $part_pkg{ $pkgpart{$number} },
139 'cust_pkg' => $cust_pkg{ $pkgnum{$number} },
140 'svcnum' => $svcnum{$number},
148 #this could get expensive on a per-call basis
149 # trigger in a separate process with less frequency?
151 my $cust_main = $cust_pkg{ $pkgnum{$number} }->cust_main;
153 my $error = $cust_main->check_credit_limit;
155 #"should never happen" normally, but as a daemon, better to survive
156 # e.g. database going away and coming back and resume doing our thing
163 last if sigterm() || sigint();
167 myexit() if sigterm() || sigint();
168 sleep 5 unless $found;
175 $conf->exists('cdr-prerate');
179 die "Usage:\n\n freeside-cdrrewrited user\n";
184 freeside-cdrrated - Real-time daemon for CDR rating
192 Runs continuously, searches for CDRs and which can be pre-rated, and rates them.
196 cdr-prerate configuration setting