disabling a taxclass, RT#5472
authorivan <ivan>
Tue, 30 Jun 2009 19:38:57 +0000 (19:38 +0000)
committerivan <ivan>
Tue, 30 Jun 2009 19:38:57 +0000 (19:38 +0000)
FS/FS/Schema.pm
FS/FS/part_pkg_taxclass.pm
httemplate/browse/part_pkg_taxclass.html [new file with mode: 0644]
httemplate/edit/part_pkg_taxclass.html
httemplate/edit/process/part_pkg_taxclass.html
httemplate/elements/menu.html
httemplate/elements/select-taxclass.html
httemplate/elements/tr-select-taxclass.html

index 3c203b9..048fa0a 100644 (file)
@@ -1253,10 +1253,11 @@ sub tables_hashref {
       'columns' => [
         'taxclassnum',  'serial', '',       '', '', '',
         'taxclass',     'varchar', '', $char_d, '', '', 
+        'disabled', 'char',   'NULL',         1, '', '', 
       ],
       'primary_key' => 'taxclassnum',
       'unique'      => [ [ 'taxclass' ] ],
-      'index'       => [],
+      'index'       => [ [ 'disabled' ] ],
     },
 
     'part_pkg_taxproduct' => {
index fda200e..6e3acf2 100644 (file)
@@ -2,8 +2,9 @@ package FS::part_pkg_taxclass;
 
 use strict;
 use vars qw( @ISA );
-use FS::UID qw(dbh);
-use FS::Record qw( qsearch qsearchs );
+use FS::UID qw( dbh );
+use FS::Record; # qw( qsearch qsearchs );
+use FS::cust_main_county;
 
 @ISA = qw(FS::Record);
 
@@ -41,6 +42,10 @@ Primary key
 
 Tax class
 
+=item disabled
+
+Disabled flag, empty or 'Y'
+
 =back
 
 =head1 METHODS
@@ -67,7 +72,57 @@ otherwise returns false.
 
 =cut
 
-# the insert method can be inherited from FS::Record
+sub insert {
+  my $self = shift;
+
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE';
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
+
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
+
+  my $error = $self->SUPER::insert;
+  if ( $error ) {
+    $dbh->rollback if $oldAutoCommit;
+    return $error;
+  }
+
+  my $sth = dbh->prepare("
+    SELECT country, state, county FROM cust_main_county
+      WHERE taxclass IS NOT NULL AND taxclass != ''
+      GROUP BY country, state, county
+  ") or die dbh->errstr;
+  $sth->execute or die $sth->errstr;
+
+  while ( my $row = $sth->fetchrow_hashref ) {
+    #warn "inserting for $row";
+    my $cust_main_county = new FS::cust_main_county {
+      'country'  => $row->{country},
+      'state'    => $row->{state},
+      'county'   => $row->{county},
+      'tax'      => 0,
+      'taxclass' => $self->taxclass,
+      #exempt_amount
+      #taxname
+      #setuptax
+      #recurtax
+    };
+    $error = $cust_main_county->insert;
+    #last if $error;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
+
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
+  '';
+}
 
 =item delete
 
@@ -84,7 +139,18 @@ returns the error, otherwise returns false.
 
 =cut
 
-# the replace method can be inherited from FS::Record
+sub replace {
+  my $new = shift;
+
+  my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
+              ? shift
+              : $new->replace_old;
+
+  return "Can't change tax class name (disable and create anew)"
+    if $old->taxclass ne $new->taxclass;
+
+  $new->SUPER::replace(@_);
+}
 
 =item check
 
@@ -103,6 +169,7 @@ sub check {
   my $error = 
     $self->ut_numbern('taxclassnum')
     || $self->ut_text('taxclass')
+    || $self->ut_enum('disabled', [ '', 'Y' ] )
   ;
   return $error if $error;
 
diff --git a/httemplate/browse/part_pkg_taxclass.html b/httemplate/browse/part_pkg_taxclass.html
new file mode 100644 (file)
index 0000000..04e0e23
--- /dev/null
@@ -0,0 +1,27 @@
+<% include( 'elements/browse.html',
+                 'title'         => 'Tax Classes',
+                 'name_singular' => 'tax class',
+                 'menubar'       => [ 'Add a new tax class' =>
+                                        $p.'edit/part_pkg_taxclass.html',
+                                    ],
+                 'query'         => { 'table' => 'part_pkg_taxclass', },
+                 'count_query'   => 'SELECT COUNT(*) FROM part_pkg_taxclass',
+                 'header'        => [ '#', 'Device type' ],
+                 'fields'        => [ 'taxclassnum',
+                                      'taxclass',
+                                    ],
+                 'links'         => [ $link,
+                                      $link,
+                                    ],
+                 'disableable'   => 1,
+                 'disabled_statuspos' => 1,
+             )
+%>
+<%init>
+
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+my $link = [ "${p}edit/part_pkg_taxclass.html?", 'taxclassnum' ];
+
+</%init>
index e767057..ad03044 100644 (file)
@@ -1,32 +1,23 @@
-<% include('/elements/header.html', "$action taxclass") %>
-
-<% include('/elements/error.html') %>
-
-<FORM ACTION="<% $p1 %>process/part_pkg_taxclass.html" METHOD=POST>
-
-<INPUT TYPE="hidden" NAME="taxclassnum" VALUE="">
-
-Tax class <INPUT TYPE="text" NAME="taxclass" VALUE="<% $taxclass |h %>">
-
-<BR><BR>
-<INPUT TYPE="submit" VALUE="<% $action %> taxclass">
-
-</FORM>
-
-<% include('/elements/footer.html') %>
-
+<% include('elements/edit.html',
+     'name_singular' => 'tax class',
+     'table'         => 'part_pkg_taxclass',
+     'labels'        => {
+                          'taxclassnum' => 'Tax class',
+                          'taxclass'    => 'Tax class',
+                          'disabled'    => 'Disabled',
+                        },
+     'fields'        => [ 'taxclass',
+                          { 'field' => 'disabled',
+                            'type'  => 'checkbox',
+                            'value' => 'Y',
+                          },
+                        ],
+     'viewall_dir'   => 'browse',
+   )
+%>
 <%init>
 
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
 
-my $taxclass = '';
-if ( $cgi->param('error') ) {
-  $taxclass = $cgi->param('taxclass');
-}
-
-my $action = 'Add';
-
-my $p1 = popurl(1);
-
 </%init>
index 8f149bb..b37279f 100644 (file)
@@ -1,53 +1,17 @@
-% if ( $error ) {
-%  $cgi->param('error', $error);
-<% $cgi->redirect(popurl(2). "part_pkg_taxclass.html?". $cgi->query_string ) %>
-%} else {
-<% $cgi->redirect(popurl(3). "browse/cust_main_county.cgi?taxclass=". uri_escape($part_pkg_taxclass->taxclass) ) %>
-%}
+<% include( 'elements/process.html',
+              'table'    => 'part_pkg_taxclass',
+              'redirect' => sub {
+                my( $cgi, $part_pkg_taxclass ) = @_;
+
+                popurl(3). 'browse/cust_main_county.cgi?'.
+                  'taxclass='. uri_escape($part_pkg_taxclass->taxclass).
+                  ';dummy=';
+              },
+          )
+%>
 <%init>
 
 die "access denied"
   unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
 
-my $part_pkg_taxclass = new FS::part_pkg_taxclass {
-  'taxclass' => $cgi->param('taxclass'),
-};
-
-#maybe this whole thing should be in a transaction.  at some point, no biggie
-#none of the follow-up stuff will fail unless there's a more serious problem
-#than a hanging record in part_pkg_taxclass...
-
-my $error = $part_pkg_taxclass->insert;
-
-unless ( $error ) {
-  #auto-add the new taxclass to any regions that have taxclasses already
-
-  my $sth = dbh->prepare("
-    SELECT country, state, county FROM cust_main_county
-      WHERE taxclass IS NOT NULL AND taxclass != ''
-      GROUP BY country, state, county
-  ") or die dbh->errstr;
-  $sth->execute or die $sth->errstr;
-
-  while ( my $row = $sth->fetchrow_hashref ) {
-    warn "inserting for $row";
-    my $cust_main_county = new FS::cust_main_county {
-      'country'  => $row->{country},
-      'state'    => $row->{state},
-      'county'   => $row->{county},
-      'tax'      => 0,
-      'taxclass' => $part_pkg_taxclass->taxclass,
-      #exempt_amount
-      #taxname
-      #setuptax
-      #recurtax
-    };
-    $error = $cust_main_county->insert;
-    #last if $error;
-    die $error if $error;
-  }
-
-
-}
-
 </%init>
index b855f79..c9adb16 100644 (file)
@@ -319,6 +319,8 @@ if ( $curuser->access_right('Configuration') ) {
   $config_billing{'View/Edit call rates and regions'} = [ \%config_billing_rates, 'Manage rate plans, regions and prefixes for VoIP and call billing' ];
   $config_billing{'View/Edit locales and tax rates (old tax class system)'}  = [ $fsurl.'browse/cust_main_county.cgi', 'Change tax rates, or break down a country into states, or a state into counties and assign different tax rates to each' ];
   $config_billing{'View/Edit tax rates (new tax products system)'}  = [ $fsurl.'browse/tax_rate.cgi', 'Edit tax rates for the new tax products system' ];
+  $config_billing{'View/Edit tax classes'}  = [ $fsurl.'browse/part_pkg_taxclass.html', 'Edit tax classes' ]
+    if $conf->exists('enable_taxclasses');
   $config_billing{'View/Edit credit reason types'}  = [ $fsurl.'browse/reason_type.html?class=R', 'Credit reason types define groups of reasons, for reporting and convenience purposes.' ];
   $config_billing{'View/Edit credit reasons'}  = [ $fsurl.'browse/reason.html?class=R', 'Credit reasons explain why a credit was issued.' ];
 }
index 2504a5b..fb09cfd 100644 (file)
@@ -30,9 +30,9 @@ my $conf = new FS::Conf;
 unless ( $opt{'taxclasses'} ) {
 
   #my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
-  my $sth = dbh->prepare('SELECT taxclass FROM part_pkg_taxclass')
+  my $sth = dbh->prepare("SELECT taxclass FROM part_pkg_taxclass WHERE disabled IS NULL OR disabled = '' OR taxclass = ?")
     or die dbh->errstr;
-  $sth->execute or die $sth->errstr;
+  $sth->execute($selected_taxclass) or die $sth->errstr;
   my %taxclasses = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
   @{ $opt{'taxclasses'} } = grep $_, keys %taxclasses;
 
index 981c1a5..95cabf7 100644 (file)
@@ -23,9 +23,9 @@ my $selected_taxclass = $opt{'curr_value'}; # || $opt{'value'} necessary?
 unless ( $opt{'taxclasses'} ) {
 
   #my $sth = dbh->prepare('SELECT DISTINCT taxclass FROM cust_main_county')
-  my $sth = dbh->prepare('SELECT taxclass FROM part_pkg_taxclass')
+  my $sth = dbh->prepare("SELECT taxclass FROM part_pkg_taxclass WHERE disabled IS NULL OR disabled = '' OR taxclass = ?")
     or die dbh->errstr;
-  $sth->execute or die $sth->errstr;
+  $sth->execute($selected_taxclass) or die $sth->errstr;
   my %taxclasses = map { $_->[0] => 1 } @{$sth->fetchall_arrayref};
   @{ $opt{'taxclasses'} } = grep $_, keys %taxclasses;