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