3 # */5 * * * /home/ivan/freeside/bin/cdr-netsapiens.import ivan exportnum
9 use FS::UID qw(adminsuidsetup dbh);
10 use FS::Record qw(qsearchs);
16 my $user = shift or die &usage;
19 my $exportnum = shift or die &usage;
20 my $part_export = qsearchs('part_export', { 'exportnum' => $exportnum } )
21 or die "unknown exportnum $exportnum\n";
23 my $cdrbatch = 'NetSapiens import '. time2str('%Y-%m-%d %x', $^T);
29 #find max time_release
30 my $sth = dbh->prepare('SELECT MAX(enddate) FROM cdr') #XXX and imported from this netsapens switch
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
37 my $ns = $part_export->ns_command( 'GET', '/cdr/',
38 'time_release' => "$time_release,",
39 '_sort' => '+time_release',
43 #loop over them, double check duplicates, insert the rest
45 my $content = $ns->responseContent;
49 #<a href="/tac2/cdr/20090219201719000016%40SkyNet360.Com">20090219201719000016@SkyNet360.Com</a>
50 # <form method="post" action="/tac2/cdr/20090219201719000016%40SkyNet360.Com">
52 s/^.*?<form class="" method="post" action="\/tac2\/cdr\/(\d{4})(\d+)\%40[^"]*">//is )
55 my $cdrid = ($1-1900). $2; #2009 -> 109 so we fit in a bigint
57 unless ( $cdrs ) { #inefficient
58 my $dsth = dbh->prepare(
59 'SELECT cdrid FROM cdr WHERE cdrid IS NOT NULL AND cdrid = ?'
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;
68 warn "$cdrid\n" if $DEBUG > 1;
70 $content =~ s/(.*?)<\/form>//is;
74 while ( $cdr_content =~
75 s/.*?<input name="(\w+)" type="\w+" value="([^"]+)" \/>//is )
77 warn " $1 => $2\n" if $DEBUG > 2;
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'},
94 'cdrbatch' => $cdrbatch,
97 my $error = $cdr->insert;
105 "Usage: \n cdr-netsapiens.import user exportnum\n";
111 orig_req_host => residential.skynet360.com
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
123 time_start => 2009-02-19 20:17:19.0
125 orig_from_user => 9046384544
127 term_logi_uri => sip:7865457300@residential.skynet360.com
129 orig_from_uri => sip:9046384544@63.251.149.5
131 orig_logi_uri => sip:9046384544@63.251.149.5
133 time_insert => 2009-02-19 15:17:38.0
134 orig_to_user => 7865457300
135 rly_prt_a => 63.251.149.18:21972
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
141 orig_req_uri => sip:7865457300@residential.skynet360.com
142 orig_to_uri => sip:7865457300@63.251.149.18
144 orig_ip => 63.251.149.5:57326
145 release_text => Orig: Cancel
147 time_ringing => 2009-02-19 20:17:19
150 orig_req_host => residential.skynet360.com
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
163 time_start => 2009-02-19 20:17:19.0
165 orig_from_user => 9046384544
167 term_logi_uri => sip:7865457300@residential.skynet360.com
169 orig_from_uri => sip:9046384544@63.251.149.5
171 orig_logi_uri => sip:9046384544@63.251.149.5
173 time_insert => 2009-02-19 15:17:38.0
174 orig_to_user => 7865457300
175 rly_prt_a => 63.251.149.18:21972
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
181 orig_req_uri => sip:7865457300@residential.skynet360.com
182 orig_to_uri => sip:7865457300@63.251.149.18
184 orig_ip => 63.251.149.5:57326
185 release_text => Orig: Cancel
187 time_ringing => 2009-02-19 20:17:19
190 list of freeside CDR fields, useful ones marked with *
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)
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)