+my $location_sort = sub {
+ $a->country cmp $b->country
+ or lc($a->city) cmp lc($b->city)
+ or lc($a->address1) cmp lc($b->address1)
+ or lc($a->address2) cmp lc($b->address2)
+};
+
+my @cust_location = ();
+push @cust_location, $cust_main->cust_location if $cust_main;
+push @cust_location, $prospect_main->cust_location if $prospect_main;
+push @cust_location, $cust_location
+ if !$cust_main && $cust_location && $cust_location->locationnum > 0
+ && ! grep { $_->locationnum == $cust_location->locationnum } @cust_location;
+
+@cust_location = sort $location_sort grep !$_->disabled, @cust_location;
+
+$cust_location = $cust_location[0]
+ if ( $prospect_main || $opt{'disable_empty'} )
+ && !$opt{'is_optional'}
+ && @cust_location;
+
+my $disabled =
+ ( $locationnum < 0
+ || ( $editable && $locationnum )
+ || ( ( $prospect_main || $opt{'disable_empty'} )
+ && !$opt{'is_optional'} && !@cust_location && $addnew
+ )
+ )
+ ? ''
+ : 'DISABLED';
+
+if ( $cust_main && $opt{'alt_format'} && ! @cust_location ) {
+ $cust_location->locationnum(-3);
+ $cust_location->alternize;
+ push @cust_location, $cust_location;
+}