combine ticket notification scrips, #15353
[freeside.git] / bin / cdr-netsapiens.import
1 #!/usr/bin/perl
2 #
3 # */5 * * * /home/ivan/freeside/bin/cdr-netsapiens.import ivan exportnum
4
5 use strict;
6 use vars qw( $DEBUG );
7 use Date::Format;
8 use REST::Client;
9 use FS::UID qw(adminsuidsetup dbh);
10 use FS::Record qw(qsearchs);
11 use FS::part_export;
12 use FS::cdr;
13
14 $DEBUG = 1;
15
16 my $user = shift or die &usage;
17 adminsuidsetup $user;
18
19 my $exportnum = shift or die &usage;
20 my $part_export = qsearchs('part_export', { 'exportnum' => $exportnum } )
21   or die "unknown exportnum $exportnum\n";
22
23 my $cdrbatch = 'NetSapiens import '. time2str('%Y-%m-%d %x', $^T);
24
25 my $cdrs = 0;
26
27 do {
28
29   #find max time_release
30   my $sth = dbh->prepare('SELECT MAX(enddate) FROM cdr') #XXX and imported from this netsapens switch
31     or die dbh->errstr;
32
33   $sth->execute or die $sth->errstr;
34   my $time_release = time2str('%Y-%m-%d %X', $sth->fetchrow_arrayref->[0]);
35   #retreive CDRs >= this time
36
37   my $ns = $part_export->ns_command( 'GET', '/cdr/',
38                                        'time_release' => "$time_release,",
39                                        '_sort'        => '+time_release',
40                                    );
41
42   #loop over them, double check duplicates, insert the rest
43
44   my $content = $ns->responseContent;
45
46   $cdrs = 0;
47
48   #<a href="/tac2/cdr/20090219201719000016%40SkyNet360.Com">20090219201719000016@SkyNet360.Com</a>
49   #    <form method="post" action="/tac2/cdr/20090219201719000016%40SkyNet360.Com">
50   while ( $content =~
51             s/^.*?<form class="" method="post" action="\/tac2\/cdr\/(\d{4})(\d+)\%40[^"]*">//is )
52   {
53
54     my $cdrid = ($1-1900). $2; #2009 -> 109 so we fit in a bigint
55
56     unless ( $cdrs ) { #inefficient
57       my $dsth = dbh->prepare(
58         'SELECT cdrid FROM cdr WHERE cdrid IS NOT NULL AND cdrid = ?'
59       ) or die dbh->errstr;
60       $dsth->execute($cdrid) or die $dsth->errstr;
61       my $row = $dsth->fetchrow_arrayref;
62       if ( $row && $row->[0] eq $cdrid ) { # == w/ 8 byte int?
63         warn "$cdrid (dup)\n" if $DEBUG > 1;
64         next;
65       }
66     }
67     warn "$cdrid\n" if $DEBUG > 1;
68
69     $content =~ s/(.*?)<\/form>//is;
70     my $cdr_content = $1;
71
72     my %cdr = ();
73     while ( $cdr_content =~
74               s/.*?<input name="(\w+)" type="\w+" value="([^"]+)" \/>//is )
75     {
76       warn "  $1 => $2\n" if $DEBUG > 2;
77       $cdr{$1} = $2;
78     }
79
80     $cdrs++;
81
82     my $cdr = new FS::cdr {
83       'src'       => $cdr{'orig_from_user'}, #orig_sub
84       'dst'       => $cdr{'orig_to_user'}, #term_sub?
85       'startdate' => FS::cdr::_cdr_date_parse($cdr{'time_start'}),
86       'enddate'   => FS::cdr::_cdr_date_parse($cdr{'time_release'}),
87       'duration'  => $cdr{'duration'},
88       'billsec'   => $cdr{'time_talking'},
89       #'disposition' => 
90       #'accountcode' =>
91       #'charged_party'
92       'cdrid'     => $cdrid,
93       'cdrbatch'  => $cdrbatch,
94     };
95
96     my $error = $cdr->insert;
97     die $error if $error;
98
99   }
100
101 } while $cdrs;
102
103 sub usage {
104   "Usage: \n  cdr-netsapiens.import user exportnum\n";
105 }
106
107 __END__
108
109   rly_prt_0 => 23946
110   orig_req_host => residential.skynet360.com
111   batch_dura => 0
112   orig_from_host => 63.251.149.5
113   batch_tim_beg => 2009-02-19 20:17:19
114   term_match => sip:7865457300@residential.skynet360.com
115   term_domain => residential.skynet360.com
116   term_sub => 7865457300
117   orig_req_user => 7865457300
118   orig_callid => 5D1164E6-44E011D6-8C84C368-EA5A0BC4@63.251.149.5
119   term_ip => 63.251.148.137:1453
120   term_to_uri => sip:7865457300@residential.skynet360.com
121   release_code => end
122           time_start => 2009-02-19 20:17:19.0
123   batch_hold => 0
124   orig_from_user => 9046384544
125   time_holding => 0
126   term_logi_uri => sip:7865457300@residential.skynet360.com
127   time_talking => 0
128   orig_from_uri => sip:9046384544@63.251.149.5
129   duration => 0
130   orig_logi_uri => sip:9046384544@63.251.149.5
131   rly_cnt_b => 0
132   time_insert => 2009-02-19 15:17:38.0
133   orig_to_user => 7865457300
134   rly_prt_a => 63.251.149.18:21972
135   cdr_index => 0
136   orig_to_host => 63.251.149.18
137   orig_match => sip:*@63.251.149.5
138         time_release => 2009-02-19 20:17:37
139   codec => G.711 u-law
140   orig_req_uri => sip:7865457300@residential.skynet360.com
141   orig_to_uri => sip:7865457300@63.251.149.18
142   rly_cnt_a => 13
143   orig_ip => 63.251.149.5:57326
144   release_text => Orig: Cancel
145   time_disp => 0
146   time_ringing => 2009-02-19 20:17:19
147   _method => put
148 prt_0 => 23946
149   orig_req_host => residential.skynet360.com
150   batch_dura => 0
151   orig_from_host => 63.251.149.5
152   batch_tim_beg => 2009-02-19 20:17:19
153   term_match => sip:7865457300@residential.skynet360.com
154   term_domain => residential.skynet360.com
155   time_start => 2009-02-19 20:17:19.0
156   term_sub => 7865457300
157   orig_req_user => 7865457300
158   orig_callid => 5D1164E6-44E011D6-8C84C368-EA5A0BC4@63.251.149.5
159   term_ip => 63.251.148.137:1453
160   term_to_uri => sip:7865457300@residential.skynet360.com
161   release_code => end
162   time_start => 2009-02-19 20:17:19.0
163   batch_hold => 0
164   orig_from_user => 9046384544
165   time_holding => 0
166   term_logi_uri => sip:7865457300@residential.skynet360.com
167   time_talking => 0
168   orig_from_uri => sip:9046384544@63.251.149.5
169   duration => 0
170   orig_logi_uri => sip:9046384544@63.251.149.5
171   rly_cnt_b => 0
172   time_insert => 2009-02-19 15:17:38.0
173   orig_to_user => 7865457300
174   rly_prt_a => 63.251.149.18:21972
175   cdr_index => 0
176   orig_to_host => 63.251.149.18
177   orig_match => sip:*@63.251.149.5
178   time_release => 2009-02-19 20:17:37
179   codec => G.711 u-law
180   orig_req_uri => sip:7865457300@residential.skynet360.com
181   orig_to_uri => sip:7865457300@63.251.149.18
182   rly_cnt_a => 13
183   orig_ip => 63.251.149.5:57326
184   release_text => Orig: Cancel
185   time_disp => 0
186   time_ringing => 2009-02-19 20:17:19
187   _method => put
188
189 list of freeside CDR fields, useful ones marked with *
190
191        acctid - primary key
192 *[1]   calldate - Call timestamp (SQL timestamp)
193        clid - Caller*ID with text
194 *      src - Caller*ID number / Source number
195 *      dst - Destination extension
196        dcontext - Destination context
197        channel - Channel used
198        dstchannel - Destination channel if appropriate
199        lastapp - Last application if appropriate
200        lastdata - Last application data
201 *      startdate - Start of call (UNIX-style integer timestamp)
202        answerdate - Answer time of call (UNIX-style integer timestamp)
203 *      enddate - End time of call (UNIX-style integer timestamp)
204 *      duration - Total time in system, in seconds
205 *      billsec - Total time call is up, in seconds
206 *[2]   disposition - What happened to the call: ANSWERED, NO ANSWER, BUSY
207        amaflags - What flags to use: BILL, IGNORE etc, specified on a per
208        channel basis like accountcode.
209 *[3]   accountcode - CDR account number to use: account
210        uniqueid - Unique channel identifier (Unitel/RSLCOM Event ID)
211        userfield - CDR user-defined field
212        cdr_type - CDR type - see FS::cdr_type (Usage = 1, S&E = 7, OC&C = 8)
213 *[4]   charged_party - Service number to be billed
214        upstream_currency - Wholesale currency from upstream
215 *[5]   upstream_price - Wholesale price from upstream
216        upstream_rateplanid - Upstream rate plan ID
217        rated_price - Rated (or re-rated) price
218        distance - km (need units field?)
219        islocal - Local - 1, Non Local = 0
220 *[6]   calltypenum - Type of call - see FS::cdr_calltype
221        description - Description (cdr_type 7&8 only) (used for
222        cust_bill_pkg.itemdesc)
223        quantity - Number of items (cdr_type 7&8 only)
224        carrierid - Upstream Carrier ID (see FS::cdr_carrier)
225        upstream_rateid - Upstream Rate ID
226        svcnum - Link to customer service (see FS::cust_svc)
227        freesidestatus - NULL, done (or something)
228
229 [1] Auto-populated from startdate if not present
230 [2] Package options available to ignore calls without a specific disposition
231 [3] When using 'cdr-charged_party-accountcode' config
232 [4] Auto-populated from src (normal calls) or dst (toll free calls) if not present
233 [5] When using 'upstream_simple' rating method.
234 [6] Set to usage class classnum when using pre-rated CDRs and usage class-based
235     taxation (local/intrastate/interstate/international)
236
237