taqua rewriting, RT#12181
[freeside.git] / FS / bin / freeside-cdrrewrited
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw( $conf );
5 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
6 use FS::UID qw( adminsuidsetup );
7 use FS::Record qw( qsearch qsearchs );
8 #use FS::cdr;
9 #use FS::cust_pkg;
10 #use FS::queue;
11
12 my $user = shift or die &usage;
13
14 #daemonize1('freeside-sprepaidd', $user); #keep unique pid files w/multi installs
15 daemonize1('freeside-cdrrewrited');
16
17 drop_root();
18
19 adminsuidsetup($user);
20
21 logfile( "%%%FREESIDE_LOG%%%/cdrrewrited-log.". $FS::UID::datasrc );
22
23 daemonize2();
24
25 $conf = new FS::Conf;
26
27 die "not running; cdr-asterisk_forward_rewrite, cdr-charged_party_rewrite ".
28     " and cdr-taqua-accountcode_rewrite conf options are all off\n"
29   unless _shouldrun();
30
31 #--
32
33 while (1) {
34
35   #hmm... don't want to do an expensive search with an ever-growing bunch
36   # of unprocessed CDRs during the month... better to mark them all as
37   # rewritten "skipped", i.e. why we're a daemon in the first place
38   # instead of just doing this search like normal CDRs
39
40   my $found = 0;
41   foreach my $cdr ( 
42     qsearch( {
43       'table'     => 'cdr',
44       'extra_sql' => 'FOR UPDATE',
45       'hashref'   => {},
46       'extra_sql' => 'WHERE freesidestatus IS NULL'.
47                      ' AND freesiderewritestatus IS NULL'.
48                      ' LIMIT 1024', #arbitrary, but don't eat too much memory
49     } )
50   ) {
51
52     $found = 1;
53     my @status = ();
54
55     if ( $conf->exists('cdr-asterisk_forward_rewrite')
56          && $cdr->dstchannel =~ /^Local\/(\d+)/i && $1 ne $cdr->dst
57        )
58     {
59
60       my $dst = $1;
61
62       warn "dst ". $cdr->dst. " does not match dstchannel $dst ".
63            "(". $cdr->dstchannel. "); rewriting CDR as a forwarded call";
64
65       $cdr->charged_party($cdr->dst);
66       $cdr->dst($dst);
67       $cdr->amaflags(2);
68
69       push @status, 'asterisk_forward';
70
71     }
72
73     if ( $conf->exists('cdr-charged_party_rewrite') && ! $cdr->charged_party ) {
74
75       $cdr->set_charged_party;
76       push @status, 'charged_party';
77
78     }
79
80     if ( $conf->exists('cdr-taqua-accountcode_rewrite')
81          && $cdr->lastapp eq 'acctcode' && $cdr->cdrtypenum == 1
82        )
83     {
84
85       #find the matching CDR
86       my $primary = qsearchs('cdr', {
87         'sessionnum'  => $cdr->sessionnum,
88         'src'         => $cdr->subscriber,
89         #'accountcode' => '',
90       });
91
92       unless ( $primary ) {
93         warn "WARNING: can't find primary CDR with session ". $cdr->sessionnum.
94              ", src ". $cdr->subscriber. "; will keep trying\n";
95         next;
96       }
97
98       $primary->accountcode( $cdr->lastdata );
99       #$primary->freesiderewritestatus( 'taqua-accountcode-primary' );
100       my $error = $primary->replace;
101       if ( $error ) {
102         warn "WARNING: error rewriting primary CDR (will retry): $error\n";
103         next;
104       }
105
106       push @status, 'taqua-accountcode';
107       $cdr->status('done'); #so it doesn't try to rate
108     }
109
110     $cdr->freesiderewritestatus(
111       scalar(@status) ? join('/', @status) : 'skipped'
112     );
113
114     my $error = $cdr->replace;
115
116     if ( $error ) {
117       warn "WARNING: error rewriting CDR (will retry in 30 seconds):".
118            " $error\n";
119       sleep 30; #i dunno, wait and see if the database comes back?
120     }
121
122     last if sigterm() || sigint();
123
124   }
125
126   myexit() if sigterm() || sigint();
127   #sleep 1 unless $found;
128   sleep 5 unless $found;
129
130 }
131
132 #--
133
134 sub _shouldrun {
135      $conf->exists('cdr-asterisk_forward_rewrite')
136   || $conf->exists('cdr-charged_party_rewrite')
137   || $conf->exists('cdr-taqua-accountcode_rewrite');
138 }
139
140 sub usage { 
141   die "Usage:\n\n  freeside-cdrrewrited user\n";
142 }
143
144 =head1 NAME
145
146 freeside-cdrrewrited - Real-time daemon for CDR rewriting
147
148 =head1 SYNOPSIS
149
150   freeside-cdrrewrited
151
152 =head1 DESCRIPTION
153
154 Runs continuously, searches for CDRs and does forwarded-call rewriting if any
155 of the "cdr-asterisk_forward_rewrite", "cdr-charged_party_rewrite" or
156 "cdr-taqua-accountcode_rewrite" config options are enabled.
157
158 =head1 SEE ALSO
159
160 =cut
161
162 1;