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