7a377587866446fabbf6b9c919022c1870a25f89
[freeside.git] / FS / FS / cdr / Import.pm
1 package FS::cdr::Import;
2
3 use strict;
4 use Date::Format 'time2str';
5 use FS::UID qw(adminsuidsetup dbh);
6 use FS::cdr;
7 use DBI;
8 use Getopt::Std;
9
10 use vars qw( $DEBUG );
11 $DEBUG = 0;
12
13 =head1 NAME
14
15 FS::cdr::Import - CDR importing
16
17 =head1 SYNOPSIS
18
19   use FS::cdr::Import;
20
21   FS::cdr::Import->dbi_import(
22     'dbd'         => 'mysql', #Pg, Sybase, etc.
23     'table'       => 'TABLE_NAME',
24     'primary_key' => 'BILLING_ID',
25     'column_map'  => { #freeside => remote_db
26       'freeside_column' => 'remote_db_column',
27       'freeside_column' => sub { my $row = shift; $row->{remote_db_column}; },
28     },
29   );
30
31 =head1 DESCRIPTION
32
33 CDR importing
34
35 =head1 CLASS METHODS
36
37 =item do_cli_import
38
39 =cut
40
41 sub dbi_import {
42   my $class = shift;
43   my %args = @_; #args are specifed by the script using this sub
44
45   my %opt; #opt is specified for each install / run of the script
46   getopts('H:U:P:D:T:c:L:', \%opt);
47   my $user = shift(@ARGV) or die $class->cli_usage;
48
49   $opt{D} ||= $args{database};
50
51   my $dsn = 'dbi:'. $args{dbd};
52   #$dsn .= ":host=$opt{H}"; #if $opt{H};
53   $dsn .= ":server=$opt{H}"; #if $opt{H};
54   $dsn .= ";database=$opt{D}" if $opt{D};
55
56   my $dbi = DBI->connect($dsn, $opt{U}, $opt{P}) 
57     or die $DBI::errstr;
58
59   adminsuidsetup $user;
60
61   #my $fsdbh = FS::UID::dbh;
62
63   my $table = $opt{T} || $args{table};
64   my $pkey = $args{primary_key};
65
66   #just doing this manually with IVR MSSQL databases for now
67   #  # check for existence of freesidestatus
68   #  my $status = $dbi->selectall_arrayref("SHOW COLUMNS FROM $table WHERE Field = 'freesidestatus'");
69   #  if( ! @$status ) {
70   #    print "Adding freesidestatus column...\n";
71   #    $dbi->do("ALTER TABLE $table ADD COLUMN freesidestatus varchar(32)")
72   #      or die $dbi->errstr;
73   #  }
74   #  else {
75   #    print "freesidestatus column present\n";
76   #  }
77
78   #my @cols = values %{ $args{column_map} };
79   my $sql = "SELECT * FROM $table ". # join(',', @cols). " FROM $table ".
80
81             ' WHERE freesidestatus IS NULL ';
82   #$sql .= ' LIMIT '. $opt{L} if $opt{L};
83   my $sth = $dbi->prepare($sql);
84   $sth->execute or die $sth->errstr. " executing $sql";
85   #MySQL-specific print "Importing ".$sth->rows." records...\n";
86
87   my $cdr_batch = new FS::cdr_batch({ 
88       'cdrbatch' => 'IVR-import-'. time2str('%Y/%m/%d-%T',time),
89     });
90   my $error = $cdr_batch->insert;
91   die $error if $error;
92   my $cdrbatchnum = $cdr_batch->cdrbatchnum;
93   my $imported = 0;
94
95   my $row;
96   while ( $row = $sth->fetchrow_hashref ) {
97
98     my %hash = ( 'cdrbatchnum' => $cdrbatchnum );
99     foreach my $field ( keys %{ $args{column_map} } ) {
100       my $col_or_coderef = $args{column_map}->{$field};
101       if ( ref($col_or_coderef) eq 'CODE' ) {
102         $hash{$field} = &{ $col_or_coderef }( $row );
103       } else {
104         $hash{$field} = $row->{ $col_or_coderef };
105       }
106       $hash{$field} = '' if $hash{$field} =~ /^\s+$/; #IVR (MSSQL?) bs
107     }
108     my $cdr = FS::cdr->new(\%hash);
109
110     $cdr->cdrtypenum($opt{c}) if $opt{c};
111
112     #print $row->{$pkey},"\n" if $opt{v};
113     my $error = $cdr->insert;
114     if ($error) {
115       #die $row->{$pkey} . ": failed import: $error\n";
116       print $row->{$pkey} . ": failed import: $error\n";
117     } else {
118       $imported++;
119
120       my $updated = $dbi->do(
121         "UPDATE $table SET freesidestatus = 'done' WHERE $pkey = ?",
122         undef,
123         $row->{$pkey}
124       );
125       #$updates += $updated;
126       die "failed to set status: ".$dbi->errstr."\n" unless $updated;
127     }
128
129     if ( $opt{L} && $imported >= $opt{L} ) {
130       $sth->finish;
131       last;
132     }
133
134   }
135   print "Done.\n";
136   print "Imported $imported CDRs.\n" if $imported;
137
138   $dbi->disconnect;
139
140 }
141
142 sub cli_usage {
143   #"Usage: \n  $0\n\t[ -H hostname ]\n\t-D database\n\t-U user\n\t-P password\n\tfreesideuser\n";
144   #"Usage: \n  $0\n\t-H hostname\n\t-D database\n\t-U user\n\t-P password\n\t[ -c cdrtypenum ]\n\tfreesideuser\n";
145   "Usage: \n  $0\n\t-H hostname\n\t[ -D database ]\n\t-U user\n\t-P password\n\t[ -c cdrtypenum ]\n\t[ -L num_cdrs_limit ]\n\tfreesideuser\n";
146 }
147
148 =head1 BUGS
149
150 Not everything has been refactored out of the various bin/cdr-*.import scripts,
151 let alone other places.
152
153 Sparse documentation.
154
155 =head1 SEE ALSO
156
157 L<FS::cdr>
158
159 =cut
160
161 1;