+=item line
+
+Synonym for location_label
+
+=cut
+
+sub line {
+ my $self = shift;
+ $self->location_label(@_);
+}
+
+=item has_ship_address
+
+Returns false since cust_location objects do not have a separate shipping
+address.
+
+=cut
+
+sub has_ship_address {
+ '';
+}
+
+=item location_hash
+
+Returns a list of key/value pairs, with the following keys: address1, address2,
+city, county, state, zip, country, geocode, location_type, location_number,
+location_kind.
+
+=cut
+
+=item disable_if_unused
+
+Sets the "disabled" flag on the location if it is no longer in use as a
+prospect location, package location, or a customer's billing or default
+service address.
+
+=cut
+
+sub disable_if_unused {
+
+ my $self = shift;
+ my $locationnum = $self->locationnum;
+ return '' if FS::cust_main->count('bill_locationnum = '.$locationnum.' OR
+ ship_locationnum = '.$locationnum)
+ or FS::contact->count( 'locationnum = '.$locationnum)
+ or FS::cust_pkg->count('cancel IS NULL AND
+ locationnum = '.$locationnum)
+ ;
+ $self->disabled('Y');
+ $self->replace;
+
+}
+
+=item move_to
+
+Takes a new L<FS::cust_location> object. Moves all packages that use the
+existing location to the new one, then sets the "disabled" flag on the old
+location. Returns nothing on success, an error message on error.
+
+=cut
+
+sub move_to {
+ my $old = shift;
+ my $new = shift;
+
+ warn "move_to:\nFROM:".Dumper($old)."\nTO:".Dumper($new) if $DEBUG;
+
+ 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 = '';
+
+ # prevent this from failing because of pkg_svc quantity limits
+ local( $FS::cust_svc::ignore_quantity ) = 1;
+
+ if ( !$new->locationnum ) {
+ $error = $new->insert;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return "Error creating location: $error";
+ }
+ } elsif ( $new->locationnum == $old->locationnum ) {
+ # then they're the same location; the normal result of doing a minor
+ # location edit
+ $dbh->commit if $oldAutoCommit;
+ return '';
+ }
+
+ # find all packages that have the old location as their service address,
+ # and aren't canceled,
+ # and aren't supplemental to another package.
+ my @pkgs = qsearch('cust_pkg', {
+ 'locationnum' => $old->locationnum,
+ 'cancel' => '',
+ 'main_pkgnum' => '',
+ });
+ foreach my $cust_pkg (@pkgs) {
+ # don't move one-time charges that have already been charged
+ next if $cust_pkg->part_pkg->freq eq '0'
+ and ($cust_pkg->setup || 0) > 0;
+
+ $error = $cust_pkg->change(
+ 'locationnum' => $new->locationnum,
+ 'keep_dates' => 1
+ );
+ if ( $error and not ref($error) ) {
+ $dbh->rollback if $oldAutoCommit;
+ return "Error moving pkgnum ".$cust_pkg->pkgnum.": $error";
+ }
+ }
+
+ $error = $old->disable_if_unused;
+ if ( $error ) {
+ $dbh->rollback if $oldAutoCommit;
+ return "Error disabling old location: $error";
+ }
+
+ $dbh->commit if $oldAutoCommit;
+ '';
+}
+
+=item alternize
+
+Attempts to parse data for location_type and location_number from address1
+and address2.
+
+=cut
+
+sub alternize {
+ my $self = shift;
+
+ return '' if $self->get('location_type')
+ || $self->get('location_number');
+
+ my %parse;
+ if ( 1 ) { #ikano, switch on via config
+ { no warnings 'void';
+ eval { 'use FS::part_export::ikano;' };
+ die $@ if $@;
+ }
+ %parse = FS::part_export::ikano->location_types_parse;
+ } else {
+ %parse = (); #?
+ }
+
+ foreach my $from ('address1', 'address2') {
+ foreach my $parse ( keys %parse ) {
+ my $value = $self->get($from);
+ if ( $value =~ s/(^|\W+)$parse\W+(\w+)\W*$//i ) {
+ $self->set('location_type', $parse{$parse});
+ $self->set('location_number', $2);
+ $self->set($from, $value);
+ return '';
+ }
+ }
+ }
+
+ #nothing matched, no changes
+ $self->get('address2')
+ ? "Can't parse unit type and number from address2"
+ : '';
+}
+
+=item dealternize
+
+Moves data from location_type and location_number to the end of address1.
+
+=cut
+
+sub dealternize {
+ my $self = shift;
+
+ #false laziness w/geocode_Mixin.pm::line
+ my $lt = $self->get('location_type');
+ if ( $lt ) {
+
+ my %location_type;
+ if ( 1 ) { #ikano, switch on via config
+ { no warnings 'void';
+ eval { 'use FS::part_export::ikano;' };
+ die $@ if $@;
+ }
+ %location_type = FS::part_export::ikano->location_types;
+ } else {
+ %location_type = (); #?
+ }
+
+ $self->address1( $self->address1. ' '. $location_type{$lt} || $lt );
+ $self->location_type('');
+ }
+
+ if ( length($self->location_number) ) {
+ $self->address1( $self->address1. ' '. $self->location_number );
+ $self->location_number('');
+ }
+
+ '';
+}
+
+=item location_label