99ea675947e8bbfae2fcfa628c76f4fb7138e94f
[freeside.git] / FS / bin / freeside-cdrrated
1 #!/usr/bin/perl -w
2
3 use strict;
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 );
7 use FS::cdr;
8 use FS::svc_phone;
9 use FS::part_pkg;
10
11 my $user = shift or die &usage;
12
13 daemonize1('freeside-cdrrated');
14
15 drop_root();
16
17 adminsuidsetup($user);
18
19 logfile( "%%%FREESIDE_LOG%%%/cdrrated-log.". $FS::UID::datasrc );
20
21 daemonize2();
22
23 our $conf = new FS::Conf;
24
25 die "not running; cdr-prerate conf option is off\n"
26   unless _shouldrun();
27
28 #--
29
30 my $extra_sql = '';
31 my @cdrtypenums = $conf->config('cdr-prerate-cdrtypenums');
32 if ( @cdrtypenums ) {
33   $extra_sql .= ' AND cdrtypenum IN ('. join(',', @cdrtypenums ). ')';
34 }
35
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 = (); # phonenum => part_pkg
41
42 #some false laziness w/freeside-cdrrewrited
43
44 while (1) {
45
46   my $found = 0;
47   foreach my $cdr (
48     qsearch( {
49       'table'     => 'cdr',
50       'hashref'   => { 'freesidestatus' => '' },
51       'extra_sql' => $extra_sql.
52                      ' LIMIT 1024'. #arbitrary, but don't eat too much memory
53                      ' FOR UPDATE',
54     } )
55
56   ) {
57
58     $found = 1;
59
60     #find the matching service - some weird false laziness w/svc_phone::get_cdrs
61
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;
66
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'};
70
71     $number = substr($number, length($prefix))
72       if $prefix eq substr($number, 0, length($prefix));
73     if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
74       $prefix = $1;
75       $number = substr($number, length($prefix))
76         if $prefix eq substr($number, 0, length($prefix));
77     }
78
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?
84         next;
85       }
86
87       $svcnum{$number} = $svc_phone->svcnum;
88
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?
93         next;
94       }
95
96       $pkgnum{$number} = $cust_pkg->pkgnum;
97       $cust_pkg{$cust_pkg->pkgnum} ||= $cust_pkg;
98
99       #get the package, search through the part_pkg and linked for a voip_cdr def w/matching cdrtypenum (or no use_cdrtypenum)
100       my @part_pkg =
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 ''
105                     )
106                  && ( ! length($_->option_cacheable('ignore_cdrtypenum'))
107                       || $_->option_cacheable('ignore_cdrtypenum')
108                            ne $cdr->cdrtypenum #ne otherwise 0 matches ''
109                     )
110
111              }
112           $cust_pkg->part_pkg->self_and_bill_linked;
113
114       if ( ! @part_pkg ) {
115         #XXX no package for this CDR
116         # warn and also set freesideratestatus or somesuch?
117         #  or at least warn
118         next;
119       } elsif ( scalar(@part_pkg) > 1 ) {
120         warn "multiple package could rate CDR ". $cdr->acctid. "\n";
121         # and also set freesideratestatus or somesuch?
122         next;
123       }
124
125       $pkgpart{$number} = $part_pkg[0]->pkgpart;
126       $part_pkg{ $part_pkg[0]->pkgpart } ||= $part_pkg[0];
127
128     } 
129
130     #unless ( $part_pkg{$pkgpart{$number}} ) {
131     #}
132
133     #XXX if $part_pkg->option('min_included') then we can't prerate this CDR
134     
135     my $error = $cdr->rate(
136       'part_pkg' => $part_pkg{ $pkgpart{$number} },
137       'cust_pkg' => $cust_pkg{ $pkgnum{$number} },
138       'svcnum'   => $svcnum{$number},
139     );
140     if ( $error ) {
141       #XXX ???
142       warn $error;
143       sleep 30;
144     }
145
146     last if sigterm() || sigint();
147
148   }
149
150   myexit() if sigterm() || sigint();
151   sleep 5 unless $found;
152
153 }
154
155 #--
156
157 sub _shouldrun {
158   $conf->exists('cdr-prerate');
159 }
160
161 sub usage { 
162   die "Usage:\n\n  freeside-cdrrewrited user\n";
163 }
164
165 =head1 NAME
166
167 freeside-cdrrated - Real-time daemon for CDR rating
168
169 =head1 SYNOPSIS
170
171   freeside-cdrrated
172
173 =head1 DESCRIPTION
174
175 Runs continuously, searches for CDRs and which can be pre-rated, and rates them.
176
177 =head1 SEE ALSO
178
179 cdr-prerate configuration setting
180
181 =cut
182
183 1;
184