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