X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fpart_export%2Fnena2.pm;h=f6a730ebc5d505eab921aaa302b634eba581b366;hb=389b6f1116c3309c2ee57a6c295ed1a793503095;hp=8d59425b71cedf5a14cd10f87e02b707cf83b90c;hpb=3146245f510ef873c4176bc06dc891f990db8f1e;p=freeside.git diff --git a/FS/FS/part_export/nena2.pm b/FS/FS/part_export/nena2.pm index 8d59425b7..f6a730ebc 100644 --- a/FS/FS/part_export/nena2.pm +++ b/FS/FS/part_export/nena2.pm @@ -13,6 +13,12 @@ use vars qw(%info %options $initial_load_hack $DEBUG); my %upload_targets; +tie our %parsing_rules, 'Tie::IxHash', ( + 'no_street_suffix' => 'Avoid street suffix', + 'no_postdir' => 'Avoid post directional', + # add others as we learn about them +); + tie %options, 'Tie::IxHash', ( 'company_name' => { label => 'Company name for header record', type => 'text', @@ -51,8 +57,15 @@ tie %options, 'Tie::IxHash', ( }, 'debug' => { label => 'Enable debugging', type => 'checkbox' }, + 'parsing_rules' => { label => 'Address parsing rules', + type => 'title' }, + + map({ $_ => { label => $parsing_rules{$_}, type => 'checkbox' } } + keys %parsing_rules + ), ); + %info = ( 'svc' => 'svc_phone', 'desc' => 'Export a NENA 2 E911 data file', @@ -237,27 +250,86 @@ sub data { $hash{calling_number} = $2; # street address + # some cleanup: + my $full_address = $cust_location->address1; + my $address2 = $cust_location->address2; + if (length($address2)) { + # correct 'Sp', 'Sp.', 'sp ', etc. to the word SPACE for convenience + $address2 =~ s/^sp\b\.? ?/SPACE /i; + # and join it to $full_address with a space, not a comma + $full_address .= ' ' . $address2; + } + + Geo::StreetAddress::US->avoid_redundant_street_type(1); + my $location_hash = Geo::StreetAddress::US->parse_address( - uc( join(', ', $cust_location->address1, - $cust_location->address2, - $cust_location->city, - $cust_location->state, - $cust_location->zip + uc( join(', ', $full_address, + $cust_location->city, + $cust_location->state, + $cust_location->zip ) ) ); - $hash{house_number} = $location_hash->{number}; - $hash{house_number_suffix} = ''; # we don't support this, do we? - $hash{prefix_directional} = $location_hash->{prefix}; - $hash{street_name} = $location_hash->{street}; - $hash{street_suffix} = $location_hash->{type}; - $hash{post_directional} = $location_hash->{suffix}; - $hash{community_name} = $location_hash->{city}; - $hash{state} = $location_hash->{state}; - if ($location_hash->{sec_unit_type}) { - $hash{location} = $location_hash->{sec_unit_type} . ' ' . - $location_hash->{sec_unit_num}; + if ( length($address2) ) { + # be careful how we handle this + if ( !defined $location_hash ) { + # then it did successfully parse. BUT. + # if there's no sec_unit_type, then the address2 was parsed as part + # of the street name, which is wrong. Then reparse. + if ( !$location_hash->{sec_unit_type} ) { + undef $location_hash; + } + } + # then parsing failed. Try again without the address2. + $location_hash = Geo::StreetAddress::US->parse_address( + uc( join(', ', + $cust_location->address1, + $cust_location->city, + $cust_location->state, + $cust_location->zip + ) ) + ); + # this should not produce an address with sec_unit_type, + # so 'location' will be set to address2 + } + if ( $location_hash ) { + # then store it + $hash{house_number} = $location_hash->{number}; + $hash{house_number_suffix} = ''; # we don't support this, do we? + $hash{prefix_directional} = $location_hash->{prefix}; + $hash{street_name} = $location_hash->{street}; + $hash{community_name} = $location_hash->{city}; + $hash{state} = $location_hash->{state}; + + if ($location_hash->{sec_unit_type}) { + $hash{location} = $location_hash->{sec_unit_type} . ' ' . + $location_hash->{sec_unit_num}; + } else { + # if sec_unit_type was not set, then put address2 in 'location' + $hash{location} = $address2; + } + + if ( $self->option('no_street_suffix') and $location_hash->{type} ) { + my $type = $location_hash->{type}; + $hash{street_name} .= ' ' . uc($location_hash->{type}); + } else { + $hash{street_suffix} = uc($location_hash->{type}); + } + + if ( $self->option('no_postdir') and $location_hash->{suffix} ) { + $hash{street_name} .= ' ' . $location_hash->{suffix}; + } else { + $hash{post_directional} = $location_hash->{suffix}; + } + } else { - $hash{location} = $cust_location->address2; + # then it still wouldn't parse; happens when the address has no house + # number (which is allowed in NENA 2 format). so just put all the + # information we have into the record. (Parse::FixedLength will trim + # it to fit if necessary.) + $hash{street_name} = uc($cust_location->address1); + $hash{location} = uc($address2); + $hash{community_name} = uc($cust_location->city); + $hash{state} = uc($cust_location->state); } # customer name and class