proper cdr_batch table, RT#6386
authorivan <ivan>
Thu, 19 Nov 2009 09:47:02 +0000 (09:47 +0000)
committerivan <ivan>
Thu, 19 Nov 2009 09:47:02 +0000 (09:47 +0000)
16 files changed:
FS/FS.pm
FS/FS/Mason.pm
FS/FS/Record.pm
FS/FS/Schema.pm
FS/FS/Upgrade.pm
FS/FS/cdr.pm
FS/MANIFEST
FS/bin/freeside-cdr-sftp_and_import
bin/cdr-transnexus.import
bin/cdr.http_and_import
bin/cdr.import
httemplate/elements/select-cdrbatch.html
httemplate/elements/tr-select-cdrbatch.html
httemplate/search/cdr.html
httemplate/search/report_cdr.html
httemplate/view/svc_phone.cgi

index 9ec2602..c6f9ece 100644 (file)
--- a/FS/FS.pm
+++ b/FS/FS.pm
@@ -150,6 +150,8 @@ L<FS::phone_avail> - Phone number availability cache
 
 L<FS::cdr> - Call Detail Record class
 
+L<FS::cdr_batch> - Call Detail Record batch class
+
 L<FS::cdr_calltype> - CDR calltype class
 
 L<FS::cdr_carrier> - CDR carrier class
index 460e1c3..bef0058 100644 (file)
@@ -185,6 +185,7 @@ if ( -e $addl_handler_use_file ) {
   use FS::XMLRPC;
   use FS::payby;
   use FS::cdr;
+  use FS::cdr_batch;
   use FS::inventory_class;
   use FS::inventory_item;
   use FS::pkg_category;
index 88d8ca9..e02a2cf 100644 (file)
@@ -1547,7 +1547,7 @@ sub process_batch_import {
   my($job, $opt) = ( shift, shift );
 
   my $table = $opt->{table};
-  my @pass_params = @{ $opt->{params} };
+  my @pass_params = $opt->{params} ? @{ $opt->{params} } : ();
   my %formats = %{ $opt->{formats} };
 
   my $param = thaw(decode_base64(shift));
@@ -1561,24 +1561,30 @@ sub process_batch_import {
   my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
   my $file = $dir. $files{'file'};
 
-  my $error =
-    FS::Record::batch_import( {
-      #class-static
-      table                      => $table,
-      formats                    => \%formats,
-      format_types               => $opt->{format_types},
-      format_headers             => $opt->{format_headers},
-      format_sep_chars           => $opt->{format_sep_chars},
-      format_fixedlength_formats => $opt->{format_fixedlength_formats},
-      #per-import
-      job                        => $job,
-      file                       => $file,
-      #type                       => $type,
-      format                     => $param->{format},
-      params                     => { map { $_ => $param->{$_} } @pass_params },
-      #?
-      default_csv                => $opt->{default_csv},
-    } );
+  my %iopt = (
+    #class-static
+    table                      => $table,
+    formats                    => \%formats,
+    format_types               => $opt->{format_types},
+    format_headers             => $opt->{format_headers},
+    format_sep_chars           => $opt->{format_sep_chars},
+    format_fixedlength_formats => $opt->{format_fixedlength_formats},
+    #per-import
+    job                        => $job,
+    file                       => $file,
+    #type                       => $type,
+    format                     => $param->{format},
+    params                     => { map { $_ => $param->{$_} } @pass_params },
+    #?
+    default_csv                => $opt->{default_csv},
+  );
+
+  if ( $opt->{'batch_namecol'} ) {
+    $iopt{'batch_namevalue'} = $param->{ $opt->{'batch_namecol'} };
+    $iopt{$_} = $opt->{$_} foreach qw( batch_keycol batch_table batch_namecol );
+  }
+
+  my $error = FS::Record::batch_import( \%iopt );
 
   unlink $file;
 
@@ -1731,6 +1737,24 @@ sub batch_import {
   my $oldAutoCommit = $FS::UID::AutoCommit;
   local $FS::UID::AutoCommit = 0;
   my $dbh = dbh;
+
+  if ( $param->{'batch_namecol'} && $param->{'batch_namevalue'} ) {
+    my $batch_col   = $param->{'batch_keycol'};
+
+    my $batch_class = 'FS::'. $param->{'batch_table'};
+    my $batch = $batch_class->new({
+      $param->{'batch_namecol'} => $param->{'batch_namevalue'}
+    });
+    my $error = $batch->insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "can't insert batch record: $error";
+    }
+    #primary key via dbdef? (so the column names don't have to match)
+    my $batch_value = $batch->get( $param->{'batch_keycol'} );
+
+    $params->{ $batch_col } = $batch_value;
+  }
   
   my $line;
   my $imported = 0;
index 2910c17..f4cbe1d 100644 (file)
@@ -2235,9 +2235,10 @@ sub tables_hashref {
         #an indexed place to put big numbers
         'cdrid',         'bigint',     'NULL',     '',  '', '', 
 
-        #i should become a table
+        #old
         'cdrbatch', 'varchar', 'NULL', 255, '', '',
-        #'cdrbatchnum', 'int', 'NULL', '', '', '',
+        #new
+        'cdrbatchnum', 'int', 'NULL', '', '', '',
 
       ],
       'primary_key' => 'acctid',
@@ -2250,16 +2251,16 @@ sub tables_hashref {
                  ],
     },
 
-    #'cdr_batch' => {
-    #  'columns' => [
-    #    'cdrbatchnum',   'serial',    '',   '', '', '', 
-    #    'cdrbatch', 'varchar', 'NULL', 255, '', '',
-    #    '_date',     @date_type, '', '', 
-    #  ],
-    #  'primary_key' => 'cdrbatchnum',
-    #  'unique' => [ [ 'cdrbatch' ] ],
-    #  'index' => [],
-    #},
+    'cdr_batch' => {
+      'columns' => [
+        'cdrbatchnum',   'serial',    '',   '', '', '', 
+        'cdrbatch', 'varchar', 'NULL', 255, '', '',
+        '_date',     @date_type, '', '', 
+      ],
+      'primary_key' => 'cdrbatchnum',
+      'unique' => [ [ 'cdrbatch' ] ],
+      'index' => [],
+    },
 
     'cdr_termination' => {
       'columns' => [
index e5cd5d3..c39680e 100644 (file)
@@ -138,6 +138,9 @@ sub upgrade_data {
     #add weights to pkg_category
     'pkg_category' => [],
 
+    #cdrbatch fixes
+    'cdr' => [],
+
   ;
 
   \%hash;
index cd42854..b9a4e41 100644 (file)
@@ -763,7 +763,11 @@ Set true to prevent throwing an error on empty imports
 =cut
 
 my %import_options = (
-  'table'   => 'cdr',
+  'table'         => 'cdr',
+
+  'batch_keycol'  => 'cdrbatchnum',
+  'batch_table'   => 'cdr_batch',
+  'batch_namecol' => 'cdrbatch',
 
   'formats' => { map { $_ => $cdr_info{$_}->{'import_fields'}; }
                      keys %cdr_info
@@ -810,7 +814,7 @@ sub process_batch_import {
   my $job = shift;
 
   my $opt = _import_options;
-  $opt->{'params'} = [ 'format', 'cdrbatch' ];
+#  $opt->{'params'} = [ 'format', 'cdrbatch' ];
 
   FS::Record::process_batch_import( $job, $opt, @_ );
 
index b84ba52..8a02a13 100644 (file)
@@ -453,6 +453,8 @@ FS/cust_attachment.pm
 t/cust_attachment.t
 FS/cust_statement.pm
 t/cust_statement.t
+FS/cdr_batch.pm
+t/cdr_batch.t
 FS/cust_class.pm
 t/cust_class.t
 FS/cust_category.pm
index e87698f..d90695f 100755 (executable)
@@ -84,10 +84,10 @@ foreach my $filename ( @$ls ) {
   warn "Processing $filename\n" if $opt_v;
 
   my $error = FS::cdr::batch_import( {
-    'file'     => "$cachedir/$filename",
-    'format'   => $format,
-    'params'   => { 'cdrbatch' => $filename, },
-    'empty_ok' => 1,
+    'file'            => "$cachedir/$filename",
+    'format'          => $format,
+    'batch_namevalue' => $filename,
+    'empty_ok'        => 1,
   } );
   die $error if $error;
 
index 9686f7d..b9fe41a 100755 (executable)
@@ -66,10 +66,10 @@ foreach my $dir ( @$lsdir ) {
     warn "Processing $filename\n" if $opt_v;
 
     my $error = FS::cdr::batch_import( {
-      'file'     => "$cachedir/$filename",
-      'format'   => $format,
-      'params'   => { 'cdrbatch' => $filename, },
-      'empty_ok' => 1,
+      'file'            => "$cachedir/$filename",
+      'format'          => $format,
+      'batch_namevalue' => $filename,
+      'empty_ok'        => 1,
     } );
     die $error if $error;
 
index 5637fa5..8910eec 100755 (executable)
@@ -85,10 +85,10 @@ foreach my $file ( @files ) {
   warn "Processing $filename\n" if $opt_v;
 
   my $error = FS::cdr::batch_import( {
-    'file'     => "$cachedir/$filename",
-    'format'   => $format,
-    'params'   => { 'cdrbatch' => $filename },
-    'empty_ok' => 1,
+    'file'            => "$cachedir/$filename",
+    'format'          => $format,
+    'batch_namevalue' => $filename,
+    'empty_ok'        => 1,
   } );
   die $error if $error;
 
index a17417b..36266ef 100644 (file)
@@ -16,9 +16,9 @@ my $format = shift or die &usage;
 my $file = shift;
 
 my $error = FS::cdr::batch_import( {
-  'file'   => $file,
-  'format' => $format,
-  'params' => { 'cdrbatch' => $file },
+  'file'            => $file,
+  'format'          => $format,
+  'batch_namevalue' => $file,
 } );
 die $error if $error;
 
index 866ba25..034db3a 100644 (file)
@@ -1,38 +1,14 @@
-% if ( scalar(@{ $opt{'cdrbatches'} }) ) {
-
-    <SELECT NAME="<% $opt{'name'} || 'cdrbatch' %>">
-
-    <OPTION VALUE="__ALL__">All
-    <OPTION VALUE="">(blank)
-
-%   foreach my $cdrbatch ( @{ $opt{'cdrbatches'} } ) { 
-        <OPTION VALUE="<% $cdrbatch %>"<% $cdrbatch eq $selected_cdrbatch ? ' SELECTED' : '' %>><% $cdrbatch %>
-%   } 
-
-    </SELECT>
-
-% } else {
-
-  <INPUT TYPE="hidden" NAME="cdrbatch" VALUE="__ALL__">
-
-% }
-
+<% include( '/elements/select-table.html',
+              'table'       => 'cdr_batch',
+              'name_col'    => 'cdrbatch',
+              'curr_value'  => $cdrbatchnum,
+              'empty_label' => '(none)',
+              'pre_options' => [ '__ALL__' => 'All' ],
+          )
+%>
 <%init>
 
 my %opt = @_;
-my $selected_cdrbatch = $opt{'curr_value'}; # || $opt{'value'} necessary?
-
-my $conf = new FS::Conf;
-
-unless ( $opt{'cdrbatches'} ) {
-
-  my $sth = dbh->prepare('SELECT DISTINCT cdrbatch FROM cdr')
-     or die dbh->errstr;
-  $sth->execute or die $sth->errstr;
-  my %cdrbatches = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
-  @{ $opt{'cdrbatches'} } = grep $_, keys %cdrbatches;
-
-}
+my $cdrbatchnum = $opt{'curr_value'}; # || $opt{'value'} necessary?
 
 </%init>
-
index 21cd004..d080e24 100644 (file)
@@ -1,6 +1,6 @@
-% if ( ! scalar(@{ $opt{'cdrbatches'} }) ) { 
+% if ( ! $show ) {
 
-  <INPUT TYPE="hidden" NAME="<% $opt{'element_name'} || $opt{'field'} || 'cdrbatch' %>" VALUE="<% $selected_cdrbatch %>">
+  <INPUT TYPE="hidden" NAME="<% $opt{'element_name'} || $opt{'field'} || 'cdrbatchnum' %>" VALUE="__ALL__">
 
 % } else { 
 
@@ -18,15 +18,13 @@ my( %opt ) = @_;
 my $conf = new FS::Conf;
 my $selected_cdrbatch = $opt{'curr_value'}; # || $opt{'value'} necessary?
 
-unless ( $opt{'cdrbatches'} ) {
+$opt{'records'} = delete $opt{'cdr_batch'}
+  if $opt{'cdr_batch'};
 
-  my $sth = dbh->prepare('SELECT cdrbatch FROM cdr')
-    or die dbh->errstr;
-  $sth->execute or die $sth->errstr;
-  my %cdrbatches = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
-  @{ $opt{'cdrbatches'} } = grep $_, keys %cdrbatches;
-
-}
+my $sth = dbh->prepare('SELECT COUNT(*) FROM cdr_batch LIMIT 1')
+  or die dbh->errstr;
+$sth->execute or die $sth->errstr;
+my $show = $sth->fetchrow_arrayref->[0];
 
 </%init>
 
index d1f68c5..cfbc5ed 100644 (file)
@@ -179,18 +179,30 @@ if ( $cgi->param('charged_party') =~ /^\s*([\d\-\+\ ]+)\s*$/ ) {
 }
 
 ###
-# cdrbatch
+# cdrbatchnum (or legacy cdrbatch)
 ###
 
-if ( $cgi->param('cdrbatch') ne '__ALL__' ) {
-  if ( $cgi->param('cdrbatch') eq '' ) {
-    my $search = "( cdrbatch IS NULL OR cdrbatch = '' )";
+if ( $cgi->param('cdrbatch') ) {
+
+  my $cdr_batch =
+    qsearchs('cdr_batch', { 'cdrbatch' => scalar($cgi->param('cdrbatch')) } );
+  if ( $cdr_batch ) {
+    push @search, 'cdrbatchnum = '. $cdr_batch->cdrbatchnum;
+  } else {
+    die "unknown cdrbatch ". $cgi->param('cdrbatch');
+  }
+
+} elsif ( $cgi->param('cdrbatchnum') ne '__ALL__' ) {
+
+  if ( $cgi->param('cdrbatchnum') eq '' ) {
+    my $search = "( cdrbatchnum IS NULL )";
     push @qsearch, $search;
     push @search,  $search;
-  } else {
-    $hashref->{cdrbatch} = $cgi->param('cdrbatch');
-    push @search, 'cdrbatch = '. dbh->quote($cgi->param('cdrbatch'));
+  } elsif ( $cgi->param('cdrbatchnum') =~ /^(\d+)$/ ) {
+    $hashref->{cdrbatchnum} = $1;
+    push @search, "cdrbatchnum = $1";
   }
+
 }
 
 ###
index c685198..744038d 100644 (file)
@@ -132,7 +132,7 @@ my @show_default = qw(
   startdate answerdate enddate duration billsec
   disposition amaflags accountcode userfield
   rated_price upstream_price carrierid 
-  svcnum freesidestatus freesiderewritestatus cdrbatch
+  svcnum freesidestatus freesiderewritestatus
 );
 my %show_default = map { $_=>1 } @show_default;
 
index 09d5be4..c5fce62 100644 (file)
@@ -107,11 +107,11 @@ my $html_foot = sub {
   #  "View $_ CDRs</A>";
   #} keys(%what);
   my @links = map {
-    qq(<A HREF="${p}search/cdr.html?cdrbatch=__ALL__;charged_party=$number;freesidestatus=$what{$_}">).
+    qq(<A HREF="${p}search/cdr.html?cdrbatchnum=__ALL__;charged_party=$number;freesidestatus=$what{$_}">).
     "View $_ CDRs</A>";
   } keys(%what);
 
-  my @ilinks = ( qq(<A HREF="${p}search/cdr.html?cdrbatch=__ALL__;dst=$number">).
+  my @ilinks = ( qq(<A HREF="${p}search/cdr.html?cdrbatchnum=__ALL__;dst=$number">).
                  'View incoming CDRs</A>' );
 
   ###