stray closing /TABLE in the no-ticket case
[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 %svc_phone = (); # phonenum => svc_phone
38 our %pkgnum = ();    # phonenum => pkgnum
39 our %cust_pkg = ();  # pkgnum   => cust_pkg (NOT phonenum => cust_pkg!)
40 our %pkgpart = ();   # phonenum => pkgpart
41 our %part_pkg = ();  # pkgpart  => part_pkg
42
43 #some false laziness w/freeside-cdrrewrited
44
45 while (1) {
46
47   my $found = 0;
48   foreach my $cdr (
49     qsearch( {
50       'table'     => 'cdr',
51       'hashref'   => { 'freesidestatus' => '' },
52       'extra_sql' => $extra_sql.
53                      ' LIMIT 1024'. #arbitrary, but don't eat too much memory
54                      ' FOR UPDATE',
55     } )
56
57   ) {
58
59     $found = 1;
60
61     #find the matching service - some weird false laziness w/svc_phone::get_cdrs
62
63     #in charged_party or src
64     #hmm... edge case; get_cdrs rating will match a src if a charged_party is
65     # present #but doesn't match a service...
66     my $number = $cdr->charged_party || $cdr->src;
67
68     #technically default_prefix. phonenum or phonenum (or default_prefix without the + . phonenum)
69     #but for now we're just assuming default_prefix is +1
70     my $prefix = '+1'; #$options{'default_prefix'};
71
72     $number = substr($number, length($prefix))
73       if $prefix eq substr($number, 0, length($prefix));
74     if ( $prefix && $prefix =~ /^\+(\d+)$/ ) {
75       $prefix = $1;
76       $number = substr($number, length($prefix))
77         if $prefix eq substr($number, 0, length($prefix));
78     }
79
80     unless ( $svc_phone{$number} ) {
81       #only phone number matching supported right now
82       my $svc_phone = qsearchs('svc_phone', { 'phonenum' => $number } );
83       unless ( $svc_phone ) {
84         #XXX set freesideratestatus or something so we don't keep retrying?
85         next;
86       }
87
88       $svc_phone{$number} = $svc_phone;
89
90     }
91
92     unless ( $pkgnum{$number} ) {
93
94       my $cust_pkg = $svc_phone{$number}->cust_svc->cust_pkg;
95       unless ( $cust_pkg ) {
96         #XXX unlinked svc_phone?
97         # warn and also set freesideratestatus or somesuch?
98         next;
99       }
100
101       $pkgnum{$number} = $cust_pkg->pkgnum;
102       $cust_pkg{$cust_pkg->pkgnum} ||= $cust_pkg;
103
104     }
105
106     unless ( $pkgpart{$number} ) {
107
108       #get the package, search through the part_pkg and linked for a voip_cdr def w/matching cdrtypenum (or no use_cdrtypenum)
109       my @part_pkg =
110         grep { $_->plan eq 'voip_cdr'
111                  && ( ! length($_->option_cacheable('use_cdrtypenum'))
112                       || $_->option_cacheable('use_cdrtypenum')
113                            eq $cdr->cdrtypenum #eq otherwise 0 matches ''
114                     )
115                  && ( ! length($_->option_cacheable('ignore_cdrtypenum'))
116                       || $_->option_cacheable('ignore_cdrtypenum')
117                            ne $cdr->cdrtypenum #ne otherwise 0 matches ''
118                     )
119
120              }
121           $cust_pkg{ $pkgnum{$number} }->part_pkg->self_and_bill_linked;
122
123       if ( ! @part_pkg ) {
124         #XXX no package for this CDR
125         # warn and also set freesideratestatus or somesuch?
126         #  or at least warn
127         next;
128       } elsif ( scalar(@part_pkg) > 1 ) {
129         warn "multiple package could rate CDR ". $cdr->acctid. "\n";
130         # and also set freesideratestatus or somesuch?
131         next;
132       }
133
134       $pkgpart{$number} = $part_pkg[0]->pkgpart;
135       $part_pkg{ $part_pkg[0]->pkgpart } ||= $part_pkg[0];
136
137     } 
138
139     if ( $part_pkg{ $pkgpart{$number} }->option('min_included') ) {
140       #then we can't prerate this CDR
141       #some sort of warning?
142       # (sucks if you're depending on credit limit fraud warnings)
143       next;
144     }
145     
146     my $error = $cdr->rate(
147       'part_pkg' => $part_pkg{ $pkgpart{$number} },
148       'cust_pkg' => $cust_pkg{ $pkgnum{$number} },
149       'svcnum'   => $svc_phone{$number}->svcnum,
150     );
151     if ( $error ) {
152       warn "Can't prerate CDR ". $cdr->acctid. ' to '. $cdr->dst. ": $error";
153       #could be an included minutes CDR, so don't sleep 30;
154     } else {
155
156       #this could get expensive on a per-call basis
157       # trigger in a separate process with less frequency?
158       
159       my $cust_main = $cust_pkg{ $pkgnum{$number} }->cust_main;
160
161       my $error = $cust_main->check_credit_limit;
162       if ( $error ) {
163         #"should never happen" normally, but as a daemon, better to survive
164         # e.g. database going away and coming back and resume doing our thing
165         warn $error;
166         sleep 30;
167       }
168
169     }
170
171     last if sigterm() || sigint();
172
173   }
174
175   myexit() if sigterm() || sigint();
176   sleep 5 unless $found;
177
178 }
179
180 #--
181
182 sub _shouldrun {
183   $conf->exists('cdr-prerate');
184 }
185
186 sub usage { 
187   die "Usage:\n\n  freeside-cdrrewrited user\n";
188 }
189
190 =head1 NAME
191
192 freeside-cdrrated - Real-time daemon for CDR rating
193
194 =head1 SYNOPSIS
195
196   freeside-cdrrated
197
198 =head1 DESCRIPTION
199
200 Runs continuously, searches for CDRs and which can be pre-rated, and rates them.
201
202 =head1 SEE ALSO
203
204 cdr-prerate configuration setting
205
206 =cut
207
208 1;
209