[freeside-commits] freeside/FS/FS Conf.pm, 1.499, 1.500 Mason.pm, 1.88, 1.89 Schema.pm, 1.350, 1.351 cust_location.pm, 1.12, 1.13 cust_main.pm, 1.600, 1.601 cust_main_county.pm, 1.26, 1.27 geocode_Mixin.pm, 1.10, 1.11

Mark Wells mark at wavetail.420.am
Fri Jan 13 02:13:58 PST 2012


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv17776/FS/FS

Modified Files:
	Conf.pm Mason.pm Schema.pm cust_location.pm cust_main.pm 
	cust_main_county.pm geocode_Mixin.pm 
Log Message:
sales tax districts, #15089

Index: Conf.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Conf.pm,v
retrieving revision 1.499
retrieving revision 1.500
diff -u -w -d -r1.499 -r1.500
--- Conf.pm	3 Jan 2012 05:10:27 -0000	1.499
+++ Conf.pm	13 Jan 2012 10:13:54 -0000	1.500
@@ -13,6 +13,7 @@
 use FS::conf;
 use FS::Record qw(qsearch qsearchs);
 use FS::UID qw(dbh datasrc use_confcompat);
+use FS::Misc::Geo;
 
 $base_dir = '%%%FREESIDE_CONF%%%';
 
@@ -3748,6 +3749,14 @@
   },
 
   {
+    'key'         => 'tax_district_method',
+    'section'     => 'UI',
+    'description' => 'The method to use to look up tax district codes.',
+    'type'        => 'select',
+    'select_hash' => [ FS::Misc::Geo::get_district_methods() ],
+  },
+
+  {
     'key'         => 'company_latitude',
     'section'     => 'UI',
     'description' => 'Your company latitude (-90 through 90)',

Index: Mason.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Mason.pm,v
retrieving revision 1.88
retrieving revision 1.89
diff -u -w -d -r1.88 -r1.89
--- Mason.pm	30 Dec 2011 22:10:57 -0000	1.88
+++ Mason.pm	13 Jan 2012 10:13:55 -0000	1.89
@@ -64,7 +64,7 @@
   use DateTime;
   use DateTime::Format::Strptime;
   use FS::Misc::DateTime qw( parse_datetime );
-  use FS::Misc::Geo qw( get_censustract );
+  use FS::Misc::Geo qw( get_censustract get_district );
   use Lingua::EN::Inflect qw(PL);
   Lingua::EN::Inflect::classical names=>0; #Categorys
   use Tie::IxHash;

Index: cust_main_county.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main_county.pm,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -w -d -r1.26 -r1.27
--- cust_main_county.pm	22 Sep 2011 21:41:46 -0000	1.26
+++ cust_main_county.pm	13 Jan 2012 10:13:55 -0000	1.27
@@ -56,6 +56,8 @@
 
 =item taxnum - primary key (assigned automatically for new tax rates)
 
+=item district - tax district (optional)
+
 =item city
 
 =item county
@@ -119,6 +121,7 @@
   $self->exempt_amount(0) unless $self->exempt_amount;
 
   $self->ut_numbern('taxnum')
+    || $self->ut_alphan('district')
     || $self->ut_textn('city')
     || $self->ut_textn('county')
     || $self->ut_anything('state')

Index: cust_location.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_location.pm,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -w -d -r1.12 -r1.13
--- cust_location.pm	13 Dec 2011 05:09:30 -0000	1.12
+++ cust_location.pm	13 Jan 2012 10:13:55 -0000	1.13
@@ -76,6 +76,10 @@
 
 Geocode
 
+=item district
+
+Tax district code (optional)
+
 =item disabled
 
 Disabled flag; set to 'Y' to disable the location.
@@ -102,6 +106,26 @@
 Adds this record to the database.  If there is an error, returns the error,
 otherwise returns false.
 
+=cut
+
+sub insert {
+  my $self = shift;
+  my $error = $self->SUPER::insert(@_);
+
+  #false laziness with cust_main, will go away eventually
+  my $conf = new FS::Conf;
+  if ( !$error and $conf->config('tax_district_method') ) {
+
+    my $queue = new FS::queue {
+      'job' => 'FS::geocode_Mixin::process_district_update'
+    };
+    $error = $queue->insert( ref($self), $self->locationnum );
+
+  }
+
+  $error || '';
+}
+
 =item delete
 
 Delete this record from the database.
@@ -111,6 +135,30 @@
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
 
+=cut
+
+sub replace {
+  my $self = shift;
+  my $old = shift;
+  $old ||= $self->replace_old;
+  my $error = $self->SUPER::replace($old);
+
+  #false laziness with cust_main, will go away eventually
+  my $conf = new FS::Conf;
+  if ( !$error and $conf->config('tax_district_method') 
+    and $self->get('address1') ne $old->get('address1') ) {
+
+    my $queue = new FS::queue {
+      'job' => 'FS::geocode_Mixin::process_district_update'
+    };
+    $error = $queue->insert( ref($self), $self->locationnum );
+
+  }
+
+  $error || '';
+}
+
+
 =item check
 
 Checks all fields to make sure this is a valid location.  If there is
@@ -142,6 +190,7 @@
     || $self->ut_textn('location_number')
     || $self->ut_enum('location_kind', [ '', 'R', 'B' ] )
     || $self->ut_alphan('geocode')
+    || $self->ut_alphan('district')
   ;
   return $error if $error;
 

Index: cust_main.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/cust_main.pm,v
retrieving revision 1.600
retrieving revision 1.601
diff -u -w -d -r1.600 -r1.601
--- cust_main.pm	1 Jan 2012 02:58:38 -0000	1.600
+++ cust_main.pm	13 Jan 2012 10:13:55 -0000	1.601
@@ -609,6 +609,20 @@
     }
   }
 
+  # FS::geocode_Mixin::after_insert or something?
+  if ( $conf->config('tax_district_method') and !$import ) {
+    # if anything non-empty, try to look it up
+    my $queue = new FS::queue {
+      'job'     => 'FS::geocode_Mixin::process_district_update',
+      'custnum' => $self->custnum,
+    };
+    my $error = $queue->insert( ref($self), $self->custnum );
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "queueing tax district update: $error";
+    }
+  }
+
   # cust_main exports!
   warn "  exporting\n" if $DEBUG > 1;
 
@@ -1633,6 +1647,25 @@
     }
   }
 
+  # FS::geocode_Mixin::after_replace ?
+  # though this will go away anyway once we move customer bill/service 
+  # locations into cust_location
+  # We can trigger this on any address change--just have to make sure 
+  # not to trigger it on itself.
+  if ( $conf->config('tax_district_method') and !$import 
+      and ( $self->get('ship_address1') ne $old->get('ship_address1')
+        or  $self->get('address1')      ne $old->get('address1') ) ) {
+    my $queue = new FS::queue {
+      'job'     => 'FS::geocode_Mixin::process_district_update',
+      'custnum' => $self->custnum,
+    };
+    my $error = $queue->insert( ref($self), $self->custnum );
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return "queueing tax district update: $error";
+    }
+  }
+
   # cust_main exports!
 
   my $export_args = $options{'export_args'} || [];
@@ -1739,6 +1772,7 @@
     || $self->ut_textn('stateid_state')
     || $self->ut_textn('invoice_terms')
     || $self->ut_alphan('geocode')
+    || $self->ut_alphan('district')
     || $self->ut_floatn('cdr_termination_percentage')
     || $self->ut_floatn('credit_limit')
     || $self->ut_numbern('billday')
@@ -2143,8 +2177,9 @@
 
 =item location_hash
 
-Returns a list of key/value pairs, with the following keys: address1, adddress2,
-city, county, state, zip, country, and geocode.  The shipping address is used if present.
+Returns a list of key/value pairs, with the following keys: address1, 
+adddress2, city, county, state, zip, country, district, and geocode.  The 
+shipping address is used if present.
 
 =cut
 

Index: Schema.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Schema.pm,v
retrieving revision 1.350
retrieving revision 1.351
diff -u -w -d -r1.350 -r1.351
--- Schema.pm	5 Jan 2012 06:03:59 -0000	1.350
+++ Schema.pm	13 Jan 2012 10:13:55 -0000	1.351
@@ -896,6 +896,7 @@
         'geocode',  'varchar', 'NULL', 20,  '', '',
         'censustract', 'varchar', 'NULL', 20,  '', '', # 7 to save space?
         'censusyear', 'char', 'NULL', 4, '', '',
+        'district', 'varchar', 'NULL', 20, '', '',
         'tax',      'char', 'NULL', 1, '', '', 
         'otaker',   'varchar', 'NULL',    32, '', '', 
         'usernum',   'int', 'NULL', '', '', '',
@@ -1054,6 +1055,7 @@
         'coord_auto',         'char', 'NULL',       1, '', '',
         'country',            'char',     '',       2, '', '', 
         'geocode',         'varchar', 'NULL',      20, '', '',
+        'district',        'varchar', 'NULL',      20, '', '',
         'location_type',   'varchar', 'NULL',      20, '', '',
         'location_number', 'varchar', 'NULL',      20, '', '',
         'location_kind',      'char', 'NULL',       1, '', '',
@@ -1178,11 +1180,12 @@
       'index'       => [ [ 'custnum' ], [ 'billpkgnum' ] ],
     },
 
-    'cust_main_county' => { #county+state+country are checked off the
-                            #cust_main_county for validation and to provide
-                            # a tax rate.
+    'cust_main_county' => { #district+city+county+state+country are checked 
+                            #off the cust_main_county for validation and to 
+                            #provide a tax rate.
       'columns' => [
         'taxnum',   'serial',   '',    '', '', '', 
+        'district', 'varchar',  'NULL',    20, '', '',
         'city',     'varchar',  'NULL',    $char_d, '', '',
         'county',   'varchar',  'NULL',    $char_d, '', '', 
         'state',    'varchar',  'NULL',    $char_d, '', '', 
@@ -1197,7 +1200,8 @@
       'primary_key' => 'taxnum',
       'unique' => [],
   #    'unique' => [ ['taxnum'], ['state', 'county'] ],
-      'index' => [ [ 'city' ], [ 'county' ], [ 'state' ], [ 'country' ],
+      'index' => [ [ 'district' ], [ 'city' ], [ 'county' ], [ 'state' ], 
+                   [ 'country' ],
                    [ 'taxclass' ],
                  ],
     },

Index: geocode_Mixin.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/geocode_Mixin.pm,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -w -d -r1.10 -r1.11
--- geocode_Mixin.pm	21 Dec 2011 21:19:10 -0000	1.10
+++ geocode_Mixin.pm	13 Jan 2012 10:13:55 -0000	1.11
@@ -132,7 +132,13 @@
   $line;
 }
 
-=item set_coord
+=item set_coord [ PREFIX ]
+
+Look up the coordinates of the location using (currently) the Google Maps
+API and set the 'latitude' and 'longitude' fields accordingly.
+
+PREFIX, if specified, will be prepended to all location field names,
+including latitude and longitude.
 
 =cut
 
@@ -213,6 +219,54 @@
   $geocode;
 }
 
+=item process_district_update CLASS ID
+
+Queueable function to update the tax district code using the selected method 
+(config 'tax_district_method').  CLASS is either 'FS::cust_main' or 
+'FS::cust_location'; ID is the key in one of those tables.
+
+=cut
+
+sub process_district_update {
+  my $class = shift;
+  my $id = shift;
+
+  eval "use FS::Misc::Geo qw(get_district); use FS::Conf; use $class;";
+  die $@ if $@;
+  die "$class has no location data" if !$class->can('location_hash');
+
+  my $conf = FS::Conf->new;
+  my $method = $conf->config('tax_district_method')
+    or return; #nothing to do if null
+  my $self = $class->by_key($id) or die "object $id not found";
+
+  # dies on error, fine
+  my $tax_info = get_district({ $self->location_hash }, $method);
+  
+  if ( $tax_info ) {
+    $self->set('district', $tax_info->{'district'} );
+    my $error = $self->replace;
+    die $error if $error;
+
+    my %hash = map { $_ => $tax_info->{$_} } 
+      qw( district city county state country );
+    my $old = qsearchs('cust_main_county', \%hash);
+    if ( $old ) {
+      my $new = new FS::cust_main_county { $old->hash, %$tax_info };
+      warn "updating tax rate for district ".$tax_info->{'district'} if $DEBUG;
+      $error = $new->replace($old);
+    }
+    else {
+      my $new = new FS::cust_main_county $tax_info;
+      warn "creating tax rate for district ".$tax_info->{'district'} if $DEBUG;
+      $error = $new->insert;
+    }
+    die $error if $error;
+
+  }
+  return;
+}
+
 =back
 
 =head1 BUGS



More information about the freeside-commits mailing list