5 use Date::Parse 'str2time';
6 use Date::Format 'time2str';
7 use FS::UID qw(adminsuidsetup dbh);
15 getopts('e:H:U:P:D:T:V:', \%opt);
16 my $user = shift or die &usage;
18 my $engine = $opt{e} || 'mysql';
20 my $dsn = "dbi:$engine";
21 $dsn .= ":database=$opt{D}"; # if $opt{D};
22 $dsn .= ";host=$opt{H}" if $opt{H};
24 my $dbi = DBI->connect($dsn, $opt{U}, $opt{P})
29 my $fsdbh = FS::UID::dbh;
31 my $table = $opt{T} || 'cdr';
33 # check for existence of freesidestatus
34 if ( $engine =~ /^mysql/ ) {
35 my $status = $dbi->selectall_arrayref("SHOW COLUMNS FROM $table WHERE Field = 'freesidestatus'");
37 warn "Adding freesidestatus column...\n" if $DEBUG;
38 $dbi->do("ALTER TABLE $table ADD COLUMN freesidestatus varchar(32)")
41 warn "freesidestatus column present\n" if $DEBUG;
46 if ( $opt{V} >= 12 ) {
47 push @cols, qw( id start answer end );
49 push @cols, qw( calldate );
53 clid src dst dcontext channel lastapp lastdata duration
54 billsec disposition amaflags accountcode userfield
57 # check for existence of uniqueid
58 if ( $engine =~ /^mysql/ ) {
59 my $status = $dbi->selectall_arrayref("SHOW COLUMNS FROM $table WHERE Field = 'uniqueid'");
61 push @cols, 'uniqueid';
64 #assume there's a uniqueid
65 push @cols, 'uniqueid';
69 'SELECT '.join(',', @cols). " FROM $table WHERE freesidestatus IS NULL";
70 my $sth = $dbi->prepare($sql);
72 warn "Importing ".$sth->rows." records...\n" if $DEBUG;
74 my $cdr_batch = new FS::cdr_batch({
75 'cdrbatch' => 'sql-import-'. time2str('%Y/%m/%d-%T',time),
77 my $error = $cdr_batch->insert;
79 my $cdrbatchnum = $cdr_batch->cdrbatchnum;
84 while ( my $row = $sth->fetchrow_hashref ) {
86 my $cdr = FS::cdr->new($row);
88 if ( $opt{V} >= 12 ) {
89 $cdr->calldate($cdr->start);
90 $cdr->startdate(str2time($cdr->start));
91 $cdr->answerdate(str2time($cdr->answer));
92 $cdr->enddate(str2time($cdr->end));
93 $cdr->cdrid($cdr->id);
95 $cdr->startdate(str2time($cdr->calldate));
98 $cdr->cdrbatchnum($cdrbatchnum);
100 my $error = $cdr->insert;
102 warn "failed import: $error\n";
107 my $usql = "UPDATE $table SET freesidestatus = 'done' WHERE ";
109 if ( $opt{V} >= 12 ) {
110 $usql .= ' id = '. $row->{'id'};
113 $usql .= ' calldate = ? AND src = ? AND dst = ?';
114 @args = ( undef, $row->{'calldate'}, $row->{'src'}, $row->{'dst'}, );
117 if ( $dbi->do($usql, @args) ) {
120 warn "failed to set status: ".$dbi->errstr."\n";
127 warn "Done.\nImported $imports CDRs, marked $updates CDRs as done.\n";
131 "Usage: \n freeside-cdr-asterisk_sql\n\t-e mysql|Pg|... [ -H host ]n\t-D database\n\t[ -T table ]\n\t[ -V asterisk_version]\n\t-U user\n\t-P password\n\tfreesideuser\n";
136 freeside-cdr-asterisk_sql - Import CDRs from an Asterisk SQL database
140 freeside-cdr-asterisk_sql -e mysql|Pg|... [ -H host ] -D database [ -T table ][ -V asterisk_version ] -U user -P password freesideuser
144 Imports CDR records from an Asterisk SQL database.
148 -e: Database engine (default mysql)
152 -T: table name (default cdr)
158 -V: Asterisk version. Values of 12 or higher have a new database schema which no longer includes calldate. Currently defaults to 11, but may default to 12+ or be required in a future, so best to always specify the version.