[freeside-commits] freeside/FS/FS Conf.pm, 1.468.2.30, 1.468.2.31 Mason.pm, 1.78.2.10, 1.78.2.11 Schema.pm, 1.317.2.29, 1.317.2.30 cust_location.pm, 1.11.2.1, 1.11.2.2 cust_main.pm, 1.586.2.13, 1.586.2.14 cust_main_county.pm, 1.25, 1.25.4.1 geocode_Mixin.pm, 1.8.2.2, 1.8.2.3
Mark Wells
mark at wavetail.420.am
Fri Jan 13 02:14:18 PST 2012
Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail.420.am:/tmp/cvs-serv18020/FS/FS
Modified Files:
Tag: FREESIDE_2_3_BRANCH
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.468.2.30
retrieving revision 1.468.2.31
diff -u -w -d -r1.468.2.30 -r1.468.2.31
--- Conf.pm 3 Jan 2012 05:10:25 -0000 1.468.2.30
+++ Conf.pm 13 Jan 2012 10:14:15 -0000 1.468.2.31
@@ -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.78.2.10
retrieving revision 1.78.2.11
diff -u -w -d -r1.78.2.10 -r1.78.2.11
--- Mason.pm 30 Dec 2011 22:11:51 -0000 1.78.2.10
+++ Mason.pm 13 Jan 2012 10:14:15 -0000 1.78.2.11
@@ -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.25
retrieving revision 1.25.4.1
diff -u -w -d -r1.25 -r1.25.4.1
--- cust_main_county.pm 22 Sep 2010 19:16:17 -0000 1.25
+++ cust_main_county.pm 13 Jan 2012 10:14:16 -0000 1.25.4.1
@@ -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.11.2.1
retrieving revision 1.11.2.2
diff -u -w -d -r1.11.2.1 -r1.11.2.2
--- cust_location.pm 13 Dec 2011 05:10:21 -0000 1.11.2.1
+++ cust_location.pm 13 Jan 2012 10:14:16 -0000 1.11.2.2
@@ -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.586.2.13
retrieving revision 1.586.2.14
diff -u -w -d -r1.586.2.13 -r1.586.2.14
--- cust_main.pm 1 Jan 2012 02:57:55 -0000 1.586.2.13
+++ cust_main.pm 13 Jan 2012 10:14:16 -0000 1.586.2.14
@@ -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.317.2.29
retrieving revision 1.317.2.30
diff -u -w -d -r1.317.2.29 -r1.317.2.30
--- Schema.pm 5 Jan 2012 06:05:14 -0000 1.317.2.29
+++ Schema.pm 13 Jan 2012 10:14:16 -0000 1.317.2.30
@@ -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.8.2.2
retrieving revision 1.8.2.3
diff -u -w -d -r1.8.2.2 -r1.8.2.3
--- geocode_Mixin.pm 21 Dec 2011 21:19:10 -0000 1.8.2.2
+++ geocode_Mixin.pm 13 Jan 2012 10:14:16 -0000 1.8.2.3
@@ -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