communigate provisioning phase 2: Domain:Account Defaults:Settings: RulesAllowed...
[freeside.git] / FS / FS / cust_location.pm
index 3a9cc7f..0c5c023 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use base qw( FS::Record );
 use Locale::Country;
 use FS::Record qw( qsearch ); #qsearchs );
+use FS::prospect_main;
 use FS::cust_main;
 use FS::cust_main_county;
 
@@ -119,7 +120,8 @@ sub check {
 
   my $error = 
     $self->ut_numbern('locationnum')
-    || $self->ut_foreign_key('custnum', 'cust_main', 'custnum')
+    || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
+    || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
     || $self->ut_text('address1')
     || $self->ut_textn('address2')
     || $self->ut_text('city')
@@ -131,6 +133,9 @@ sub check {
   ;
   return $error if $error;
 
+  return "No prospect or customer!" unless $self->prospectnum || $self->custnum;
+  return "Prospect and customer!"       if $self->prospectnum && $self->custnum;
+
   unless ( qsearch('cust_main_county', {
     'country' => $self->country,
     'state'   => '',
@@ -158,58 +163,89 @@ sub country_full {
   code2country($self->country);
 }
 
-=item line
+=item location_label [ OPTION => VALUE ... ]
 
-Returns this location on one line
+Returns the label of the service location for this customer.
 
-=cut
+Options are
 
-sub line {
-  my $self = shift;
-  my $cydefault = FS::conf->new->config('countrydefault') || 'US';
+=over 4
 
-  my $line =       $self->address1;
-  $line   .= ', '. $self->address2              if $self->address2;
-  $line   .= ', '. $self->city;
-  $line   .= ' ('. $self->county. ' county)'    if $self->county;
-  $line   .= ', '. $self->state                 if $self->state;
-  $line   .= '  '. $self->zip                   if $self->zip;
-  $line   .= '  '. code2country($self->country) if $self->country ne $cydefault;
+=item join_string
 
-  $line;
-}
+used to separate the address elements (defaults to ', ')
+
+=item escape_function
 
-=item line_short
 
-Returns this location on one line in a shortened form
+a callback used for escaping the text of the address elements
+
+=back
 
 =cut
 
-# configurable?
+# false laziness with FS::cust_main::location_label
 
-sub line_short {
+sub location_label {
   my $self = shift;
-  my $cydefault = FS::conf->new->config('countrydefault') || 'US';
-
-  my $line =       $self->address1;
-  #$line   .= ', '. $self->address2              if $self->address2;
-  $line   .= ', '. $self->city;
-  $line   .= ', '. $self->state                 if $self->state;
-  $line   .= '  '. $self->zip                   if $self->zip;
-  $line   .= '  '. code2country($self->country) if $self->country ne $cydefault;
+  my %opt = @_;
+
+  my $separator = $opt{join_string} || ', ';
+  my $escape = $opt{escape_function} || sub{ shift };
+  my $ds = $opt{double_space} || '  ';
+  my $line = '';
+  my $cydefault =
+    $opt{'countrydefault'} || FS::Conf->new->config('countrydefault') || 'US';
+  my $prefix = '';
+
+  my $notfirst = 0;
+  foreach (qw ( address1 address2 ) ) {
+    my $method = "$prefix$_";
+    $line .= ($notfirst ? $separator : ''). &$escape($self->$method)
+      if $self->$method;
+    $notfirst++;
+  }
+  $notfirst = 0;
+  foreach (qw ( city county state zip ) ) {
+    my $method = "$prefix$_";
+    if ( $self->$method ) {
+      $line .= ($notfirst ? ($method eq 'zip' ? $ds : ' ') : $separator);
+      $line .= '(' if $method eq 'county';
+      $line .= &$escape($self->$method);
+      $line .= ')' if $method eq 'county';
+      $notfirst++;
+    }
+    $line .= ',' if $method eq 'county';
+  }
+  $line .= $separator. &$escape(code2country($self->country))
+    if $self->country ne $cydefault;
 
   $line;
 }
 
-=item location_label_short
+=item line
+
+Synonym for location_label
+
+=cut
+
+sub line {
+  my $self = shift;
+  $self->location_label;
+}
+
+=item location_hash
 
-Synonym for line_short
+Returns a list of key/value pairs, with the following keys: address1, adddress2,
+city, county, state, zip, country.
 
 =cut
 
-sub location_label_short {
+#geocode?  not yet set
+
+sub location_hash {
   my $self = shift;
-  $self->line_short;
+  map { $_ => $self->$_ } qw( address1 address2 city county state zip country );
 }
 
 =back