diff options
author | Mark Wells <mark@freeside.biz> | 2013-03-29 23:15:21 -0700 |
---|---|---|
committer | Mark Wells <mark@freeside.biz> | 2013-03-29 23:15:21 -0700 |
commit | a59da6000c99b8fa3f391122900c44735593f544 (patch) | |
tree | c6619bd01455cbe57df9b4b9fc532c1f8112c89e /FS/FS | |
parent | 75ddd5240b233dd45f1d748f628e01238be0e751 (diff) |
better detection of new locations that are the same as existing locations, #940, #13763, #14717
Diffstat (limited to 'FS/FS')
-rw-r--r-- | FS/FS/cust_bill_pkg.pm | 3 | ||||
-rw-r--r-- | FS/FS/cust_location.pm | 27 | ||||
-rw-r--r-- | FS/FS/cust_main/Packages.pm | 25 | ||||
-rw-r--r-- | FS/FS/cust_pkg.pm | 13 |
4 files changed, 53 insertions, 15 deletions
diff --git a/FS/FS/cust_bill_pkg.pm b/FS/FS/cust_bill_pkg.pm index 716c098..d8cbf59 100644 --- a/FS/FS/cust_bill_pkg.pm +++ b/FS/FS/cust_bill_pkg.pm @@ -1104,8 +1104,7 @@ sub upgrade_tax_location { delete @hash{qw(censustract censusyear latitude longitude coord_auto)}; $hash{custnum} = $h_cust_main->custnum; - my $tax_loc = qsearchs('cust_location', \%hash) # unlikely - || FS::cust_location->new({ %hash }); + my $tax_loc = FS::cust_location->new_or_existing(\%hash); if ( !$tax_loc->locationnum ) { $tax_loc->disabled('Y'); my $error = $tax_loc->insert; diff --git a/FS/FS/cust_location.pm b/FS/FS/cust_location.pm index b25163f..355bdb0 100644 --- a/FS/FS/cust_location.pm +++ b/FS/FS/cust_location.pm @@ -5,7 +5,7 @@ use strict; use vars qw( $import ); use Locale::Country; use FS::UID qw( dbh driver_name ); -use FS::Record qw( qsearch ); #qsearchs ); +use FS::Record qw( qsearch qsearchs ); use FS::Conf; use FS::prospect_main; use FS::cust_main; @@ -104,6 +104,31 @@ points to. You can ask the object for a copy with the I<hash> method. sub table { 'cust_location'; } +=item new_or_existing HASHREF + +Returns an existing location matching the customer and address fields in +HASHREF, if one exists; otherwise returns a new location containing those +fields. The following fields must match: address1, address2, city, county, +state, zip, country, geocode, disabled. Other fields are only required +to match if they're specified in HASHREF. + +The new location will not be inserted; the calling code must call C<insert> +(or a method such as C<move_to>) to insert it, and check for errors at that +point. + +=cut + +sub new_or_existing { + my $class = shift; + my %hash = ref($_[0]) ? %{$_[0]} : @_; + foreach ( qw(address1 address2 city county state zip country geocode + disabled ) ) { + # empty fields match only empty fields + $hash{$_} = '' if !defined($hash{$_}); + } + return qsearchs('cust_location', \%hash) || $class->new(\%hash); +} + =item insert Adds this record to the database. If there is an error, returns the error, diff --git a/FS/FS/cust_main/Packages.pm b/FS/FS/cust_main/Packages.pm index 588f8a1..a3809f6 100644 --- a/FS/FS/cust_main/Packages.pm +++ b/FS/FS/cust_main/Packages.pm @@ -100,17 +100,26 @@ sub order_pkg { local $FS::UID::AutoCommit = 0; my $dbh = dbh; - if ( $opt->{'cust_location'} && - ( ! $cust_pkg->locationnum || $cust_pkg->locationnum == -1 ) ) { - my $error = $opt->{'cust_location'}->insert; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return "inserting cust_location (transaction rolled back): $error"; + if ( $opt->{'locationnum'} and $opt->{'locationnum'} != -1 ) { + + $cust_pkg->locationnum($opt->{'locationnum'}); + + } elsif ( $opt->{'cust_location'} ) { + + if ( ! $opt->{'cust_location'}->locationnum ) { + # not inserted yet + my $error = $opt->{'cust_location'}->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "inserting cust_location (transaction rolled back): $error"; + } } $cust_pkg->locationnum($opt->{'cust_location'}->locationnum); - } - else { + + } else { + $cust_pkg->locationnum($self->ship_locationnum); + } $cust_pkg->custnum( $self->custnum ); diff --git a/FS/FS/cust_pkg.pm b/FS/FS/cust_pkg.pm index ed059d1..ab479dd 100644 --- a/FS/FS/cust_pkg.pm +++ b/FS/FS/cust_pkg.pm @@ -1749,12 +1749,17 @@ sub change { if ( $opt->{'cust_location'} && ( ! $opt->{'locationnum'} || $opt->{'locationnum'} == -1 ) ) { - $error = $opt->{'cust_location'}->insert; - if ( $error ) { - $dbh->rollback if $oldAutoCommit; - return "inserting cust_location (transaction rolled back): $error"; + + if ( ! $opt->{'cust_location'}->locationnum ) { + # not inserted yet + $error = $opt->{'cust_location'}->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return "inserting cust_location (transaction rolled back): $error"; + } } $opt->{'locationnum'} = $opt->{'cust_location'}->locationnum; + } my $unused_credit = 0; |