fee77a8b106b3eb730f9d6fc88e026cedb089e9a
[freeside.git] / FS / FS / cust_location.pm
1 package FS::cust_location;
2 use base qw( FS::geocode_Mixin FS::Record );
3
4 use strict;
5 use vars qw( $import $DEBUG $conf $label_prefix $allow_location_edit );
6 use Data::Dumper;
7 use Date::Format qw( time2str );
8 use FS::UID qw( dbh driver_name );
9 use FS::Record qw( qsearch qsearchs );
10 use FS::Conf;
11 use FS::prospect_main;
12 use FS::cust_main;
13 use FS::cust_main_county;
14 use FS::part_export;
15 use FS::GeocodeCache;
16
17 # Essential fields. Can't be modified in place, will be considered in
18 # deciding if a location is "new", and (because of that) can't have
19 # leading/trailing whitespace.
20 my @essential = (qw(custnum address1 address2 city county state zip country
21   location_number location_type location_kind disabled));
22
23 $import = 0;
24
25 $DEBUG = 0;
26
27 FS::UID->install_callback( sub {
28   $conf = FS::Conf->new;
29   $label_prefix = $conf->config('cust_location-label_prefix') || '';
30 });
31
32 =head1 NAME
33
34 FS::cust_location - Object methods for cust_location records
35
36 =head1 SYNOPSIS
37
38   use FS::cust_location;
39
40   $record = new FS::cust_location \%hash;
41   $record = new FS::cust_location { 'column' => 'value' };
42
43   $error = $record->insert;
44
45   $error = $new_record->replace($old_record);
46
47   $error = $record->delete;
48
49   $error = $record->check;
50
51 =head1 DESCRIPTION
52
53 An FS::cust_location object represents a customer (or prospect) location.
54 FS::cust_location inherits from FS::Record.  The following fields are currently
55 supported:
56
57 =over 4
58
59 =item locationnum
60
61 primary key
62
63 =item custnum
64
65 Customer (see L<FS::cust_main>).
66
67 =item prospectnum
68
69 Prospect (see L<FS::prospect_main>).
70
71 =item locationname
72
73 Optional location name.
74
75 =item address1
76
77 Address line one (required)
78
79 =item address2
80
81 Address line two (optional)
82
83 =item city
84
85 City (if cust_main-no_city_in_address config is set when inserting, this will be forced blank)
86
87 =item county
88
89 County (optional, see L<FS::cust_main_county>)
90
91 =item state
92
93 State (see L<FS::cust_main_county>)
94
95 =item zip
96
97 Zip
98
99 =item country
100
101 Country (see L<FS::cust_main_county>)
102
103 =item geocode
104
105 Geocode
106
107 =item latitude
108
109 =item longitude
110
111 =item coord_auto
112
113 Flag indicating whether coordinates were obtained automatically or manually
114 entered
115
116 =item addr_clean
117
118 Flag indicating whether address has been normalized
119
120 =item censustract
121
122 =item censusyear
123
124 =item district
125
126 Tax district code (optional)
127
128 =item incorporated
129
130 Incorporated city flag: set to 'Y' if the address is in the legal borders 
131 of an incorporated city.
132
133 =item disabled
134
135 Disabled flag; set to 'Y' to disable the location.
136
137 =back
138
139 =head1 METHODS
140
141 =over 4
142
143 =item new HASHREF
144
145 Creates a new location.  To add the location to the database, see L<"insert">.
146
147 Note that this stores the hash reference, not a distinct copy of the hash it
148 points to.  You can ask the object for a copy with the I<hash> method.
149
150 =cut
151
152 sub table { 'cust_location'; }
153
154 =item find_or_insert
155
156 Finds an existing location matching the customer and address values in this
157 location, if one exists, and sets the contents of this location equal to that
158 one (including its locationnum).
159
160 If an existing location is not found, this one I<will> be inserted.  (This is a
161 change from the "new_or_existing" method that this replaces.)
162
163 The following fields are considered "essential" and I<must> match: custnum,
164 address1, address2, city, county, state, zip, country, location_number,
165 location_type, location_kind.  Disabled locations will be found only if this
166 location is set to disabled.
167
168 All other fields are considered "non-essential" and will be ignored in 
169 finding a matching location.  If the existing location doesn't match 
170 in these fields, it will be updated in-place to match.
171
172 Returns an error string if inserting or updating a location failed.
173
174 It is unfortunately hard to determine if this created a new location or not.
175
176 =cut
177
178 sub find_or_insert {
179   my $self = shift;
180
181   warn "find_or_insert:\n".Dumper($self) if $DEBUG;
182
183   if ($conf->exists('cust_main-no_city_in_address')) {
184     warn "Warning: passed city to find_or_insert when cust_main-no_city_in_address is configured, ignoring it"
185       if $self->get('city');
186     $self->set('city','');
187   }
188
189   # I don't think this is necessary
190   #if ( !$self->coord_auto and $self->latitude and $self->longitude ) {
191   #  push @essential, qw(latitude longitude);
192   #  # but NOT coord_auto; if the latitude and longitude match the geocoded
193   #  # values then that's good enough
194   #}
195
196   # put nonempty, nonessential fields/values into this hash
197   my %nonempty = map { $_ => $self->get($_) }
198                  grep {$self->get($_)} $self->fields;
199   delete @nonempty{@essential};
200   delete $nonempty{'locationnum'};
201
202   my %hash = map { $_ => $self->get($_) } @essential;
203   foreach (values %hash) {
204     s/^\s+//;
205     s/\s+$//;
206   }
207   my @matches = qsearch('cust_location', \%hash);
208
209   # we no longer reject matches for having different values in nonessential
210   # fields; we just alter the record to match
211   if ( @matches ) {
212     my $old = $matches[0];
213     warn "found existing location #".$old->locationnum."\n" if $DEBUG;
214     foreach my $field (keys %nonempty) {
215       if ($old->get($field) ne $nonempty{$field}) {
216         warn "altering $field to match requested location" if $DEBUG;
217         $old->set($field, $nonempty{$field});
218       }
219     } # foreach $field
220
221     if ( $old->modified ) {
222       warn "updating non-essential fields\n" if $DEBUG;
223       my $error = $old->replace;
224       return $error if $error;
225     }
226     # set $self equal to $old
227     foreach ($self->fields) {
228       $self->set($_, $old->get($_));
229     }
230     return "";
231   }
232
233   # didn't find a match
234   warn "not found; inserting new location\n" if $DEBUG;
235   return $self->insert;
236 }
237
238 =item insert
239
240 Adds this record to the database.  If there is an error, returns the error,
241 otherwise returns false.
242
243 =cut
244
245 sub insert {
246   my $self = shift;
247
248   if ($conf->exists('cust_main-no_city_in_address')) {
249     warn "Warning: passed city to insert when cust_main-no_city_in_address is configured, ignoring it"
250       if $self->get('city');
251     $self->set('city','');
252   }
253
254   if ( $self->censustract ) {
255     $self->set('censusyear' => $conf->config('census_year') || 2012);
256   }
257
258   my $oldAutoCommit = $FS::UID::AutoCommit;
259   local $FS::UID::AutoCommit = 0;
260   my $dbh = dbh;
261
262   my $error = $self->SUPER::insert(@_);
263   if ( $error ) {
264     $dbh->rollback if $oldAutoCommit;
265     return $error;
266   }
267
268   #false laziness with cust_main, will go away eventually
269   if ( !$import and $conf->config('tax_district_method') ) {
270
271     my $queue = new FS::queue {
272       'job' => 'FS::geocode_Mixin::process_district_update'
273     };
274     $error = $queue->insert( ref($self), $self->locationnum );
275     if ( $error ) {
276       $dbh->rollback if $oldAutoCommit;
277       return $error;
278     }
279
280   }
281
282   # cust_location exports
283   #my $export_args = $options{'export_args'} || [];
284
285   # don't export custnum_pending cases, let follow-up replace handle that
286   if ($self->custnum || $self->prospectnum) {
287     my @part_export =
288       map qsearch( 'part_export', {exportnum=>$_} ),
289         $conf->config('cust_location-exports'); #, $agentnum
290
291     foreach my $part_export ( @part_export ) {
292       my $error = $part_export->export_insert($self); #, @$export_args);
293       if ( $error ) {
294         $dbh->rollback if $oldAutoCommit;
295         return "exporting to ". $part_export->exporttype.
296                " (transaction rolled back): $error";
297       }
298     }
299   }
300
301   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
302   '';
303 }
304
305 =item delete
306
307 Delete this record from the database.
308
309 =item replace OLD_RECORD
310
311 Replaces the OLD_RECORD with this one in the database.  If there is an error,
312 returns the error, otherwise returns false.
313
314 =cut
315
316 sub replace {
317   my $self = shift;
318   my $old = shift;
319   $old ||= $self->replace_old;
320
321   warn "Warning: passed city to replace when cust_main-no_city_in_address is configured"
322     if $conf->exists('cust_main-no_city_in_address') && $self->get('city');
323
324   # the following fields are immutable if this is a customer location. if
325   # it's a prospect location, then there are no active packages, no billing
326   # history, no taxes, and in general no reason to keep the old location
327   # around.
328   if ( !$allow_location_edit and $self->custnum ) {
329     foreach (qw(address1 address2 city state zip country)) {
330       if ( $self->$_ ne $old->$_ ) {
331         return "can't change cust_location field $_";
332       }
333     }
334   }
335
336   my $oldAutoCommit = $FS::UID::AutoCommit;
337   local $FS::UID::AutoCommit = 0;
338   my $dbh = dbh;
339
340   my $error = $self->SUPER::replace($old);
341   if ( $error ) {
342     $dbh->rollback if $oldAutoCommit;
343     return $error;
344   }
345
346   # cust_location exports
347   #my $export_args = $options{'export_args'} || [];
348
349   # don't export custnum_pending cases, let follow-up replace handle that
350   if ($self->custnum || $self->prospectnum) {
351     my @part_export =
352       map qsearch( 'part_export', {exportnum=>$_} ),
353         $conf->config('cust_location-exports'); #, $agentnum
354
355     foreach my $part_export ( @part_export ) {
356       my $error = $part_export->export_replace($self, $old); #, @$export_args);
357       if ( $error ) {
358         $dbh->rollback if $oldAutoCommit;
359         return "exporting to ". $part_export->exporttype.
360                " (transaction rolled back): $error";
361       }
362     }
363   }
364
365   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
366   '';
367 }
368
369
370 =item check
371
372 Checks all fields to make sure this is a valid location.  If there is
373 an error, returns the error, otherwise returns false.  Called by the insert
374 and replace methods.
375
376 =cut
377
378 sub check {
379   my $self = shift;
380
381   return '' if $self->disabled; # so that disabling locations never fails
382
383   # whitespace in essential fields leads to problems figuring out if a
384   # record is "new"; get rid of it.
385   $self->trim_whitespace(@essential);
386
387   my $error = 
388     $self->ut_numbern('locationnum')
389     || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
390     || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
391     || $self->ut_textn('locationname')
392     || $self->ut_text('address1')
393     || $self->ut_textn('address2')
394     || ($conf->exists('cust_main-no_city_in_address') 
395         ? $self->ut_textn('city') 
396         : $self->ut_text('city'))
397     || $self->ut_textn('county')
398     || $self->ut_textn('state')
399     || $self->ut_country('country')
400     || (!$import && $self->ut_zip('zip', $self->country))
401     || $self->ut_coordn('latitude')
402     || $self->ut_coordn('longitude')
403     || $self->ut_enum('coord_auto', [ '', 'Y' ])
404     || $self->ut_enum('addr_clean', [ '', 'Y' ])
405     || $self->ut_alphan('location_type')
406     || $self->ut_textn('location_number')
407     || $self->ut_enum('location_kind', [ '', 'R', 'B' ] )
408     || $self->ut_alphan('geocode')
409     || $self->ut_alphan('district')
410     || $self->ut_numbern('censusyear')
411     || $self->ut_flag('incorporated')
412   ;
413   return $error if $error;
414   if ( $self->censustract ne '' ) {
415     $self->censustract =~ /^\s*(\d{9})\.?(\d{2})\s*$/
416       or return "Illegal census tract: ". $self->censustract;
417
418     $self->censustract("$1.$2");
419   }
420
421   #yikes... this is ancient, pre-dates cust_location and will be harder to
422   # implement now... how do we know this location is a service location from
423   # here and not a billing? we can't just check locationnums, we might be new :/
424   return "Unit # is required"
425     if $conf->exists('cust_main-require_address2')
426     && ! $self->address2 =~ /\S/;
427
428   # tricky...we have to allow for the customer to not be inserted yet
429   return "No prospect or customer!" unless $self->prospectnum 
430                                         || $self->custnum
431                                         || $self->get('custnum_pending');
432   return "Prospect and customer!"       if $self->prospectnum && $self->custnum;
433
434   return 'Location kind is required'
435     if $self->prospectnum
436     && $conf->exists('prospect_main-alt_address_format')
437     && ! $self->location_kind;
438
439   unless ( $import or qsearch('cust_main_county', {
440     'country' => $self->country,
441     'state'   => '',
442    } ) ) {
443     return "Unknown state/county/country: ".
444       $self->state. "/". $self->county. "/". $self->country
445       unless qsearch('cust_main_county',{
446         'state'   => $self->state,
447         'county'  => $self->county,
448         'country' => $self->country,
449       } );
450   }
451
452   # set coordinates, unless we already have them
453   if (!$import and !$self->latitude and !$self->longitude) {
454     $self->set_coord;
455   }
456
457   $self->SUPER::check;
458 }
459
460 =item country_full
461
462 Returns this location's full country name
463
464 =cut
465
466 #moved to geocode_Mixin.pm
467
468 =item line
469
470 Synonym for location_label
471
472 =cut
473
474 sub line {
475   my $self = shift;
476   $self->location_label(@_);
477 }
478
479 =item has_ship_address
480
481 Returns false since cust_location objects do not have a separate shipping
482 address.
483
484 =cut
485
486 sub has_ship_address {
487   '';
488 }
489
490 =item location_hash
491
492 Returns a list of key/value pairs, with the following keys: address1, address2,
493 city, county, state, zip, country, geocode, location_type, location_number,
494 location_kind.
495
496 =cut
497
498 =item disable_if_unused
499
500 Sets the "disabled" flag on the location if it is no longer in use as a 
501 prospect location, package location, or a customer's billing or default
502 service address.
503
504 =cut
505
506 sub disable_if_unused {
507
508   my $self = shift;
509   my $locationnum = $self->locationnum;
510   return '' if FS::cust_main->count('bill_locationnum = '.$locationnum.' OR
511                                      ship_locationnum = '.$locationnum)
512             or FS::contact->count(      'locationnum  = '.$locationnum)
513             or FS::cust_pkg->count('cancel IS NULL AND 
514                                          locationnum  = '.$locationnum)
515           ;
516   $self->disabled('Y');
517   $self->replace;
518
519 }
520
521 =item move_pkgs
522
523 Returns array of cust_pkg objects that would have their location
524 updated by L</move_to> (all packages that have this location as 
525 their service address, and aren't canceled, and aren't supplemental 
526 to another package, and aren't one-time charges that have already been charged.)
527
528 =cut
529
530 sub move_pkgs {
531   my $self = shift;
532   my @pkgs = ();
533   # find all packages that have the old location as their service address,
534   # and aren't canceled,
535   # and aren't supplemental to another package
536   # and aren't one-time charges that have already been charged
537   foreach my $cust_pkg (
538     qsearch('cust_pkg', { 
539       'locationnum' => $self->locationnum,
540       'cancel'      => '',
541       'main_pkgnum' => '',
542     })
543   ) {
544     next if $cust_pkg->part_pkg->freq eq '0'
545             and ($cust_pkg->setup || 0) > 0;
546     push @pkgs, $cust_pkg;
547   }
548   return @pkgs;
549 }
550
551 =item move_to NEW [ move_pkgs => \@move_pkgs ]
552
553 Takes a new L<FS::cust_location> object.  Moves all packages that use the 
554 existing location to the new one, then sets the "disabled" flag on the old
555 location.  Returns nothing on success, an error message on error.
556
557 Use option I<move_pkgs> to override the list of packages to update
558 (see L</move_pkgs>.)
559
560 =cut
561
562 sub move_to {
563   my $old = shift;
564   my $new = shift;
565   my %opt = @_;
566   
567   warn "move_to:\nFROM:".Dumper($old)."\nTO:".Dumper($new) if $DEBUG;
568
569   local $SIG{HUP} = 'IGNORE';
570   local $SIG{INT} = 'IGNORE';
571   local $SIG{QUIT} = 'IGNORE';
572   local $SIG{TERM} = 'IGNORE';
573   local $SIG{TSTP} = 'IGNORE';
574   local $SIG{PIPE} = 'IGNORE';
575
576   my $oldAutoCommit = $FS::UID::AutoCommit;
577   local $FS::UID::AutoCommit = 0;
578   my $dbh = dbh;
579   my $error = '';
580
581   # prevent this from failing because of pkg_svc quantity limits
582   local( $FS::cust_svc::ignore_quantity ) = 1;
583
584   if ( !$new->locationnum ) {
585     $error = $new->insert;
586     if ( $error ) {
587       $dbh->rollback if $oldAutoCommit;
588       return "Error creating location: $error";
589     }
590   } elsif ( $new->locationnum == $old->locationnum ) {
591     # then they're the same location; the normal result of doing a minor
592     # location edit
593     $dbh->commit if $oldAutoCommit;
594     return '';
595   }
596
597   my @pkgs;
598   if ($opt{'move_pkgs'}) {
599     @pkgs = @{$opt{'move_pkgs'}};
600     my $pkgerr;
601     foreach my $pkg (@pkgs) {
602       my $pkgnum = $pkg->pkgnum;
603       $pkgerr = "cust_pkg $pkgnum has already been charged"
604         if $pkg->part_pkg->freq eq '0'
605           and ($pkg->setup || 0) > 0;
606       $pkgerr = "cust_pkg $pkgnum is supplemental"
607         if $pkg->main_pkgnum;
608       $pkgerr = "cust_pkg $pkgnum already cancelled"
609         if $pkg->cancel;
610       $pkgerr = "cust_pkg $pkgnum does not use this location"
611         unless $pkg->locationnum eq $old->locationnum;
612       last if $pkgerr;
613     }
614     if ($pkgerr) {
615       $dbh->rollback if $oldAutoCommit;
616       return "Cannot update package location: $pkgerr";
617     }
618   } else {
619     @pkgs = $old->move_pkgs;
620   }
621
622   foreach my $cust_pkg (@pkgs) {
623     $error = $cust_pkg->change(
624       'locationnum' => $new->locationnum,
625       'keep_dates'  => 1
626     );
627     if ( $error and not ref($error) ) {
628       $dbh->rollback if $oldAutoCommit;
629       return "Error moving pkgnum ".$cust_pkg->pkgnum.": $error";
630     }
631   }
632
633   $error = $old->disable_if_unused;
634   if ( $error ) {
635     $dbh->rollback if $oldAutoCommit;
636     return "Error disabling old location: $error";
637   }
638
639   $dbh->commit if $oldAutoCommit;
640   '';
641 }
642
643 =item alternize
644
645 Attempts to parse data for location_type and location_number from address1
646 and address2.
647
648 =cut
649
650 sub alternize {
651   my $self = shift;
652
653   return '' if $self->get('location_type')
654             || $self->get('location_number');
655
656   my %parse;
657   if ( 1 ) { #ikano, switch on via config
658     { no warnings 'void';
659       eval { 'use FS::part_export::ikano;' };
660       die $@ if $@;
661     }
662     %parse = FS::part_export::ikano->location_types_parse;
663   } else {
664     %parse = (); #?
665   }
666
667   foreach my $from ('address1', 'address2') {
668     foreach my $parse ( keys %parse ) {
669       my $value = $self->get($from);
670       if ( $value =~ s/(^|\W+)$parse\W+(\w+)\W*$//i ) {
671         $self->set('location_type', $parse{$parse});
672         $self->set('location_number', $2);
673         $self->set($from, $value);
674         return '';
675       }
676     }
677   }
678
679   #nothing matched, no changes
680   $self->get('address2')
681     ? "Can't parse unit type and number from address2"
682     : '';
683 }
684
685 =item dealternize
686
687 Moves data from location_type and location_number to the end of address1.
688
689 =cut
690
691 sub dealternize {
692   my $self = shift;
693
694   #false laziness w/geocode_Mixin.pm::line
695   my $lt = $self->get('location_type');
696   if ( $lt ) {
697
698     my %location_type;
699     if ( 1 ) { #ikano, switch on via config
700       { no warnings 'void';
701         eval { 'use FS::part_export::ikano;' };
702         die $@ if $@;
703       }
704       %location_type = FS::part_export::ikano->location_types;
705     } else {
706       %location_type = (); #?
707     }
708
709     $self->address1( $self->address1. ' '. $location_type{$lt} || $lt );
710     $self->location_type('');
711   }
712
713   if ( length($self->location_number) ) {
714     $self->address1( $self->address1. ' '. $self->location_number );
715     $self->location_number('');
716   }
717  
718   '';
719 }
720
721 =item location_label
722
723 Returns the label of the location object.
724
725 Options:
726
727 =over 4
728
729 =item cust_main
730
731 Customer object (see L<FS::cust_main>)
732
733 =item prospect_main
734
735 Prospect object (see L<FS::prospect_main>)
736
737 =item join_string
738
739 String used to join location elements
740
741 =item no_prefix
742
743 Don't label the default service location as "Default service location".
744 May become the default at some point.
745
746 =back
747
748 =cut
749
750 sub location_label {
751   my( $self, %opt ) = @_;
752
753   my $prefix = $self->label_prefix(%opt);
754   $prefix .= ($opt{join_string} ||  ': ') if $prefix;
755   $prefix = '' if $opt{'no_prefix'};
756
757   $prefix . $self->SUPER::location_label(%opt);
758 }
759
760 =item label_prefix
761
762 Returns the optional site ID string (based on the cust_location-label_prefix
763 config option), "Default service location", or the empty string.
764
765 Options:
766
767 =over 4
768
769 =item cust_main
770
771 Customer object (see L<FS::cust_main>)
772
773 =item prospect_main
774
775 Prospect object (see L<FS::prospect_main>)
776
777 =back
778
779 =cut
780
781 sub label_prefix {
782   my( $self, %opt ) = @_;
783
784   my $cust_or_prospect = $opt{cust_main} || $opt{prospect_main};
785   unless ( $cust_or_prospect ) {
786     if ( $self->custnum ) {
787       $cust_or_prospect = FS::cust_main->by_key($self->custnum);
788     } elsif ( $self->prospectnum ) {
789       $cust_or_prospect = FS::prospect_main->by_key($self->prospectnum);
790     }
791   }
792
793   my $prefix = '';
794   if ( $label_prefix eq 'CoStAg' ) {
795     my $agent = $conf->config('cust_main-custnum-display_prefix',
796                   $cust_or_prospect->agentnum)
797                 || $cust_or_prospect->agent->agent;
798     # else this location is invalid
799     $prefix = uc( join('',
800         $self->country,
801         ($self->state =~ /^(..)/),
802         ($agent =~ /^(..)/),
803         sprintf('%05d', $self->locationnum)
804     ) );
805
806   } elsif ( $label_prefix eq '_location' && $self->locationname ) {
807     $prefix = $self->locationname;
808
809   #} elsif (    ( $opt{'cust_main'} || $self->custnum )
810   #        && $self->locationnum == $cust_or_prospect->ship_locationnum ) {
811   #  $prefix = 'Default service location';
812   #}
813   } else {
814     $prefix = '';
815   }
816
817   $prefix;
818 }
819
820 =item county_state_country
821
822 Returns a string consisting of just the county, state and country.
823
824 =cut
825
826 sub county_state_country {
827   my $self = shift;
828   my $label = $self->country;
829   $label = $self->state.", $label" if $self->state;
830   $label = $self->county." County, $label" if $self->county;
831   $label;
832 }
833
834 =back
835
836 =head2 SUBROUTINES
837
838 =over 4
839
840 =item process_censustract_update LOCATIONNUM
841
842 Queueable function to update the census tract to the current year (as set in 
843 the 'census_year' configuration variable) and retrieve the new tract code.
844
845 =cut
846
847 sub process_censustract_update {
848   eval "use FS::GeocodeCache";
849   die $@ if $@;
850   my $locationnum = shift;
851   my $cust_location = 
852     qsearchs( 'cust_location', { locationnum => $locationnum })
853       or die "locationnum '$locationnum' not found!\n";
854
855   my $new_year = $conf->config('census_year') or return;
856   my $loc = FS::GeocodeCache->new( $cust_location->location_hash );
857   $loc->set_censustract;
858   my $error = $loc->get('censustract_error');
859   die $error if $error;
860   $cust_location->set('censustract', $loc->get('censustract'));
861   $cust_location->set('censusyear',  $new_year);
862   $error = $cust_location->replace;
863   die $error if $error;
864   return;
865 }
866
867 =item process_set_coord
868
869 Queueable function to find and fill in coordinates for all locations that 
870 lack them.  Because this uses the Google Maps API, it's internally rate
871 limited and must run in a single process.
872
873 =cut
874
875 sub process_set_coord {
876   my $job = shift;
877   # avoid starting multiple instances of this job
878   my @others = qsearch('queue', {
879       'status'  => 'locked',
880       'job'     => $job->job,
881       'jobnum'  => {op=>'!=', value=>$job->jobnum},
882   });
883   return if @others;
884
885   $job->update_statustext('finding locations to update');
886   my @missing_coords = qsearch('cust_location', {
887       'disabled'  => '',
888       'latitude'  => '',
889       'longitude' => '',
890   });
891   my $i = 0;
892   my $n = scalar @missing_coords;
893   for my $cust_location (@missing_coords) {
894     $cust_location->set_coord;
895     my $error = $cust_location->replace;
896     if ( $error ) {
897       warn "error geocoding location#".$cust_location->locationnum.": $error\n";
898     } else {
899       $i++;
900       $job->update_statustext("updated $i / $n locations");
901       dbh->commit; # so that we don't have to wait for the whole thing to finish
902       # Rate-limit to stay under the Google Maps usage limit (2500/day).
903       # 86,400 / 35 = 2,468 lookups per day.
904     }
905     sleep 35;
906   }
907   if ( $i < $n ) {
908     die "failed to update ".$n-$i." locations\n";
909   }
910   return;
911 }
912
913 =item process_standardize [ LOCATIONNUMS ]
914
915 Performs address standardization on locations with unclean addresses,
916 using whatever method you have configured.  If the standardize_* method 
917 returns a I<clean> address match, the location will be updated.  This is 
918 always an in-place update (because the physical location is the same, 
919 and is just being referred to by a more accurate name).
920
921 Disabled locations will be skipped, as nobody cares.
922
923 If any LOCATIONNUMS are provided, only those locations will be updated.
924
925 =cut
926
927 sub process_standardize {
928   my $job = shift;
929   my @others = qsearch('queue', {
930       'status'  => 'locked',
931       'job'     => $job->job,
932       'jobnum'  => {op=>'!=', value=>$job->jobnum},
933   });
934   return if @others;
935   my @locationnums = grep /^\d+$/, @_;
936   my $where = "AND locationnum IN(".join(',',@locationnums).")"
937     if scalar(@locationnums);
938   my @locations = qsearch({
939       table     => 'cust_location',
940       hashref   => { addr_clean => '', disabled => '' },
941       extra_sql => $where,
942   });
943   my $n_todo = scalar(@locations);
944   my $n_done = 0;
945
946   # special: log this
947   my $log;
948   eval "use Text::CSV";
949   open $log, '>', "$FS::UID::cache_dir/process_standardize-" . 
950                   time2str('%Y%m%d',time) .
951                   ".csv";
952   my $csv = Text::CSV->new({binary => 1, eol => "\n"});
953
954   foreach my $cust_location (@locations) {
955     $job->update_statustext( int(100 * $n_done/$n_todo) . ",$n_done / $n_todo locations" ) if $job;
956     my $result = FS::GeocodeCache->standardize($cust_location);
957     if ( $result->{addr_clean} and !$result->{error} ) {
958       my @cols = ($cust_location->locationnum);
959       foreach (keys %$result) {
960         push @cols, $cust_location->get($_), $result->{$_};
961         $cust_location->set($_, $result->{$_});
962       }
963       # bypass immutable field restrictions
964       my $error = $cust_location->FS::Record::replace;
965       warn "location ".$cust_location->locationnum.": $error\n" if $error;
966       $csv->print($log, \@cols);
967     }
968     $n_done++;
969     dbh->commit; # so that we can resume if interrupted
970   }
971   close $log;
972 }
973
974 sub _upgrade_data {
975   my $class = shift;
976
977   # are we going to need to update tax districts?
978   my $use_districts = $conf->config('tax_district_method') ? 1 : 0;
979
980   # trim whitespace on records that need it
981   local $allow_location_edit = 1;
982   foreach my $field (@essential) {
983     next if $field eq 'custnum';
984     next if $field eq 'disabled';
985     foreach my $location (qsearch({
986       table => 'cust_location',
987       extra_sql => " WHERE disabled IS NULL AND ($field LIKE ' %' OR $field LIKE '% ')"
988     })) {
989       my $error = $location->replace;
990       die "$error (fixing whitespace in $field, locationnum ".$location->locationnum.')'
991         if $error;
992
993       if ( $use_districts ) {
994         my $queue = new FS::queue {
995           'job' => 'FS::geocode_Mixin::process_district_update'
996         };
997         $error = $queue->insert( 'FS::cust_location' => $location->locationnum );
998         die $error if $error;
999       }
1000     } # foreach $location
1001   } # foreach $field
1002   '';
1003 }
1004
1005 =head1 BUGS
1006
1007 =head1 SEE ALSO
1008
1009 L<FS::cust_main_county>, L<FS::cust_pkg>, L<FS::Record>,
1010 schema.html from the base documentation.
1011
1012 =cut
1013
1014 1;
1015