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',
42 #loop over them, double check duplicates, insert the rest
44 my $content = $ns->responseContent;
48 #<a href="/tac2/cdr/20090219201719000016%40SkyNet360.Com">20090219201719000016@SkyNet360.Com</a>
49 # <form method="post" action="/tac2/cdr/20090219201719000016%40SkyNet360.Com">
51 s/^.*?<form class="" method="post" action="\/tac2\/cdr\/(\d{4})(\d+)\%40[^"]*">//is )
54 my $cdrid = ($1-1900). $2; #2009 -> 109 so we fit in a bigint
56 unless ( $cdrs ) { #inefficient
57 my $dsth = dbh->prepare(
58 'SELECT cdrid FROM cdr WHERE cdrid IS NOT NULL AND cdrid = ?'
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;
67 warn "$cdrid\n" if $DEBUG > 1;
69 $content =~ s/(.*?)<\/form>//is;
73 while ( $cdr_content =~
74 s/.*?<input name="(\w+)" type="\w+" value="([^"]+)" \/>//is )
76 warn " $1 => $2\n" if $DEBUG > 2;
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'},
93 'cdrbatch' => $cdrbatch,
96 my $error = $cdr->insert;
104 "Usage: \n cdr-netsapiens.import user exportnum\n";
110 orig_req_host => residential.skynet360.com
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
122 time_start => 2009-02-19 20:17:19.0
124 orig_from_user => 9046384544
126 term_logi_uri => sip:7865457300@residential.skynet360.com
128 orig_from_uri => sip:9046384544@63.251.149.5
130 orig_logi_uri => sip:9046384544@63.251.149.5
132 time_insert => 2009-02-19 15:17:38.0
133 orig_to_user => 7865457300
134 rly_prt_a => 63.251.149.18:21972
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
140 orig_req_uri => sip:7865457300@residential.skynet360.com
141 orig_to_uri => sip:7865457300@63.251.149.18
143 orig_ip => 63.251.149.5:57326
144 release_text => Orig: Cancel
146 time_ringing => 2009-02-19 20:17:19
149 orig_req_host => residential.skynet360.com
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
162 time_start => 2009-02-19 20:17:19.0
164 orig_from_user => 9046384544
166 term_logi_uri => sip:7865457300@residential.skynet360.com
168 orig_from_uri => sip:9046384544@63.251.149.5
170 orig_logi_uri => sip:9046384544@63.251.149.5
172 time_insert => 2009-02-19 15:17:38.0
173 orig_to_user => 7865457300
174 rly_prt_a => 63.251.149.18:21972
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
180 orig_req_uri => sip:7865457300@residential.skynet360.com
181 orig_to_uri => sip:7865457300@63.251.149.18
183 orig_ip => 63.251.149.5:57326
184 release_text => Orig: Cancel
186 time_ringing => 2009-02-19 20:17:19
189 list of freeside CDR fields, useful ones marked with *
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)
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)