this should fix credits pushing typeset invoices off the right
[freeside.git] / FS / FS / tax_rate.pm
index 021bce3..3323e00 100644 (file)
@@ -3,11 +3,14 @@ package FS::tax_rate;
 use strict;
 use vars qw( @ISA $DEBUG $me
              %tax_unittypes %tax_maxtypes %tax_basetypes %tax_authorities
-             %tax_passtypes );
+             %tax_passtypes %GetInfoType );
 use Date::Parse;
 use Storable qw( thaw );
 use MIME::Base64;
-use FS::Record qw( qsearch qsearchs dbh );
+use DBIx::DBSchema;
+use DBIx::DBSchema::Table;
+use DBIx::DBSchema::Column;
+use FS::Record qw( qsearch qsearchs dbh dbdef );
 use FS::tax_class;
 use FS::cust_bill_pkg;
 use FS::cust_tax_location;
@@ -471,7 +474,7 @@ sub _fatal_or_null {
   $name = 'Other surcharges'
     if ($self->passtype == 2);
 
-  if ($conf->exists('ignore_incalculable_tax')) {
+  if ($conf->exists('ignore_incalculable_taxes')) {
     warn $error;
     return { name => $name, amount => 0 };
   } else {
@@ -560,9 +563,11 @@ sub batch_import {
                             /^(\d{4})(\d{2})(\d{2})$/ && ($r="$1/$2/$3");
                             $r;
                           };
-    $column_callbacks[8] = $date_format;
+    my $trim = sub { my $r = shift; $r =~ s/^\s*//; $r =~ s/\s*$//; $r };
     push @column_lengths, qw( 10 1 1 8 8 5 8 8 8 1 2 2 30 8 8 10 2 8 2 1 2 2 );
     push @column_lengths, 1 if $format eq 'cch-update';
+    push @column_callbacks, $trim foreach (@column_lengths); # 5, 6, 15, 17 esp
+    $column_callbacks[8] = $date_format;
   }
   
   my $line;
@@ -913,10 +918,12 @@ sub process_batch_import {
                                 UNLINK   => 0,     #meh
                               ) or die "can't open temp file: $!\n";
 
+      my $insert_pattern = ($format eq 'cch-update') ? qr/"I"\s*$/ : qr/I\s*$/;
+      my $delete_pattern = ($format eq 'cch-update') ? qr/"D"\s*$/ : qr/D\s*$/;
       while(<$fh>) {
         my $handle = '';
-        $handle = $ifh if $_ =~ /"I"\s*$/;
-        $handle = $dfh if $_ =~ /"D"\s*$/;
+        $handle = $ifh if $_ =~ /$insert_pattern/;
+        $handle = $dfh if $_ =~ /$delete_pattern/;
         unless ($handle) {
           $error = "bad input line: $_" unless $handle;
           last;
@@ -1059,6 +1066,67 @@ sub browse_queries {
   return ($query, "SELECT COUNT(*) FROM tax_rate $extra_sql");
 }
 
+# _upgrade_data
+#
+# Used by FS::Upgrade to migrate to a new database.
+#
+#
+
+sub _upgrade_data {  # class method
+  my ($self, %opts) = @_;
+  my $dbh = dbh;
+
+  warn "$me upgrading $self\n" if $DEBUG;
+
+  my @column = qw ( tax excessrate usetax useexcessrate fee excessfee
+                    feebase feemax );
+
+  if ( $dbh->{Driver}->{Name} eq 'Pg' ) {
+
+    eval "use DBI::Const::GetInfoType;";
+    die $@ if $@;
+
+    my $major_version = 0;
+    $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ) =~ /^(\d{2})/
+      && ( $major_version = sprintf("%d", $1) );
+
+    if ( $major_version > 7 ) {
+
+      # ideally this would be supported in DBIx-DBSchema and friends
+
+      foreach my $column ( @column ) {
+        my $columndef = dbdef->table($self->table)->column($column);
+        unless ($columndef->type eq 'numeric') {
+
+          warn "updating tax_rate column $column to numeric\n" if $DEBUG;
+          my $sql = "ALTER TABLE tax_rate ALTER $column TYPE numeric(14,8)";
+          my $sth = $dbh->prepare($sql) or die $dbh->errstr;
+          $sth->execute or die $sth->errstr;
+
+          warn "updating h_tax_rate column $column to numeric\n" if $DEBUG;
+          $sql = "ALTER TABLE h_tax_rate ALTER $column TYPE numeric(14,8)";
+          $sth = $dbh->prepare($sql) or die $dbh->errstr;
+          $sth->execute or die $sth->errstr;
+
+        }
+      }
+
+    } else {
+
+      warn "WARNING: tax_rate table upgrade unsupported for this Pg version\n";
+
+    }
+
+  } else {
+
+    warn "WARNING: tax_rate table upgrade only supported for Pg 8+\n";
+
+  }
+
+  '';
+
+}
+
 =back
 
 =head1 BUGS