backup the schema for tables we don't need the data from. RT#85959
[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   # If using tax_district_method, for rows in state of Washington,
269   # without a tax district already specified, queue a job to find
270   # the tax district
271   if (
272        !$import
273     && !$self->district
274     && lc $self->state eq 'wa'
275     && $conf->config('tax_district_method')
276   ) {
277
278     my $queue = new FS::queue {
279       'job' => 'FS::geocode_Mixin::process_district_update'
280     };
281     $error = $queue->insert( ref($self), $self->locationnum );
282     if ( $error ) {
283       $dbh->rollback if $oldAutoCommit;
284       return $error;
285     }
286
287   }
288
289   # cust_location exports
290   #my $export_args = $options{'export_args'} || [];
291
292   # don't export custnum_pending cases, let follow-up replace handle that
293   if ($self->custnum || $self->prospectnum) {
294     my @part_export =
295       map qsearch( 'part_export', {exportnum=>$_} ),
296         $conf->config('cust_location-exports'); #, $agentnum
297
298     foreach my $part_export ( @part_export ) {
299       my $error = $part_export->export_insert($self); #, @$export_args);
300       if ( $error ) {
301         $dbh->rollback if $oldAutoCommit;
302         return "exporting to ". $part_export->exporttype.
303                " (transaction rolled back): $error";
304       }
305     }
306   }
307
308   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
309   '';
310 }
311
312 =item delete
313
314 Delete this record from the database.
315
316 =item replace OLD_RECORD
317
318 Replaces the OLD_RECORD with this one in the database.  If there is an error,
319 returns the error, otherwise returns false.
320
321 =cut
322
323 sub replace {
324   my $self = shift;
325   my $old = shift;
326   $old ||= $self->replace_old;
327
328   warn "Warning: passed city to replace when cust_main-no_city_in_address is configured"
329     if $conf->exists('cust_main-no_city_in_address') && $self->get('city');
330
331   # the following fields are immutable if this is a customer location. if
332   # it's a prospect location, then there are no active packages, no billing
333   # history, no taxes, and in general no reason to keep the old location
334   # around.
335   if ( !$allow_location_edit and $self->custnum ) {
336     foreach (qw(address1 address2 city state zip country)) {
337       if ( $self->$_ ne $old->$_ ) {
338         return "can't change cust_location field $_";
339       }
340     }
341   }
342
343   my $oldAutoCommit = $FS::UID::AutoCommit;
344   local $FS::UID::AutoCommit = 0;
345   my $dbh = dbh;
346
347   my $error = $self->SUPER::replace($old);
348   if ( $error ) {
349     $dbh->rollback if $oldAutoCommit;
350     return $error;
351   }
352
353   # cust_location exports
354   #my $export_args = $options{'export_args'} || [];
355
356   # don't export custnum_pending cases, let follow-up replace handle that
357   if ($self->custnum || $self->prospectnum) {
358     my @part_export =
359       map qsearch( 'part_export', {exportnum=>$_} ),
360         $conf->config('cust_location-exports'); #, $agentnum
361
362     foreach my $part_export ( @part_export ) {
363       my $error = $part_export->export_replace($self, $old); #, @$export_args);
364       if ( $error ) {
365         $dbh->rollback if $oldAutoCommit;
366         return "exporting to ". $part_export->exporttype.
367                " (transaction rolled back): $error";
368       }
369     }
370   }
371
372   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
373   '';
374 }
375
376
377 =item check
378
379 Checks all fields to make sure this is a valid location.  If there is
380 an error, returns the error, otherwise returns false.  Called by the insert
381 and replace methods.
382
383 =cut
384
385 sub check {
386   my $self = shift;
387
388   return '' if $self->disabled; # so that disabling locations never fails
389
390   # whitespace in essential fields leads to problems figuring out if a
391   # record is "new"; get rid of it.
392   $self->trim_whitespace(@essential);
393
394   my $error = 
395     $self->ut_numbern('locationnum')
396     || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
397     || $self->ut_foreign_keyn('custnum', 'cust_main', 'custnum')
398     || $self->ut_textn('locationname')
399     || $self->ut_text('address1')
400     || $self->ut_textn('address2')
401     || ($conf->exists('cust_main-no_city_in_address') 
402         ? $self->ut_textn('city') 
403         : $self->ut_text('city'))
404     || $self->ut_textn('county')
405     || $self->ut_textn('state')
406     || $self->ut_country('country')
407     || (!$import && $self->ut_zip('zip', $self->country))
408     || $self->ut_coordn('latitude')
409     || $self->ut_coordn('longitude')
410     || $self->ut_enum('coord_auto', [ '', 'Y' ])
411     || $self->ut_enum('addr_clean', [ '', 'Y' ])
412     || $self->ut_alphan('location_type')
413     || $self->ut_textn('location_number')
414     || $self->ut_enum('location_kind', [ '', 'R', 'B' ] )
415     || $self->ut_alphan('geocode')
416     || $self->ut_alphan('district')
417     || $self->ut_numbern('censusyear')
418     || $self->ut_flag('incorporated')
419   ;
420   return $error if $error;
421   if ( $self->censustract ne '' ) {
422     $self->censustract =~ /^\s*(\d{9})\.?(\d{2})\s*$/
423       or return "Illegal census tract: ". $self->censustract;
424
425     $self->censustract("$1.$2");
426   }
427
428   #yikes... this is ancient, pre-dates cust_location and will be harder to
429   # implement now... how do we know this location is a service location from
430   # here and not a billing? we can't just check locationnums, we might be new :/
431   return "Unit # is required"
432     if $conf->exists('cust_main-require_address2')
433     && ! $self->address2 =~ /\S/;
434
435   # tricky...we have to allow for the customer to not be inserted yet
436   return "No prospect or customer!" unless $self->prospectnum 
437                                         || $self->custnum
438                                         || $self->get('custnum_pending');
439   return "Prospect and customer!"       if $self->prospectnum && $self->custnum;
440
441   return 'Location kind is required'
442     if $self->prospectnum
443     && $conf->exists('prospect_main-alt_address_format')
444     && ! $self->location_kind;
445
446   # Do not allow bad tax district values in cust_location when
447   # using Washington State district sales tax calculation - would result
448   # in incorrect or missing sales tax on invoices.
449   my $tax_district_method = FS::Conf->new->config('tax_district_method');
450   if (
451     $tax_district_method
452     && $tax_district_method eq 'wa_sales'
453     && $self->district
454   ) {
455     my $cust_main_county = qsearchs(
456       cust_main_county => { district => $self->district }
457     );
458     unless ( ref $cust_main_county ) {
459       return sprintf (
460         'WA State tax district %s does not exist in tax table',
461         $self->district
462       );
463     }
464   }
465
466   unless ( $import or qsearch('cust_main_county', {
467     'country' => $self->country,
468     'state'   => '',
469    } ) ) {
470     return "Unknown state/county/country: ".
471       $self->state. "/". $self->county. "/". $self->country
472       unless qsearch('cust_main_county',{
473         'state'   => $self->state,
474         'county'  => $self->county,
475         'country' => $self->country,
476       } );
477   }
478
479   # set coordinates, unless we already have them
480   if (!$import and !$self->latitude and !$self->longitude) {
481     $self->set_coord;
482   }
483
484   $self->SUPER::check;
485 }
486
487 =item country_full
488
489 Returns this location's full country name
490
491 =cut
492
493 #moved to geocode_Mixin.pm
494
495 =item line
496
497 Synonym for location_label
498
499 =cut
500
501 sub line {
502   my $self = shift;
503   $self->location_label(@_);
504 }
505
506 =item has_ship_address
507
508 Returns false since cust_location objects do not have a separate shipping
509 address.
510
511 =cut
512
513 sub has_ship_address {
514   '';
515 }
516
517 =item location_hash
518
519 Returns a list of key/value pairs, with the following keys: address1, address2,
520 city, county, state, zip, country, geocode, location_type, location_number,
521 location_kind.
522
523 =cut
524
525 =item disable_if_unused
526
527 Sets the "disabled" flag on the location if it is no longer in use as a 
528 prospect location, package location, or a customer's billing or default
529 service address.
530
531 =cut
532
533 sub disable_if_unused {
534
535   my $self = shift;
536   my $locationnum = $self->locationnum;
537   return '' if FS::cust_main->count('bill_locationnum = '.$locationnum.' OR
538                                      ship_locationnum = '.$locationnum)
539             or FS::contact->count(      'locationnum  = '.$locationnum)
540             or FS::cust_pkg->count('cancel IS NULL AND 
541                                          locationnum  = '.$locationnum)
542           ;
543   $self->disabled('Y');
544   $self->replace;
545
546 }
547
548 =item move_pkgs
549
550 Returns array of cust_pkg objects that would have their location
551 updated by L</move_to> (all packages that have this location as 
552 their service address, and aren't canceled, and aren't supplemental 
553 to another package, and aren't one-time charges that have already been charged.)
554
555 =cut
556
557 sub move_pkgs {
558   my $self = shift;
559   my @pkgs = ();
560   # find all packages that have the old location as their service address,
561   # and aren't canceled,
562   # and aren't supplemental to another package
563   # and aren't one-time charges that have already been charged
564   foreach my $cust_pkg (
565     qsearch('cust_pkg', { 
566       'locationnum' => $self->locationnum,
567       'cancel'      => '',
568       'main_pkgnum' => '',
569     })
570   ) {
571     next if $cust_pkg->part_pkg->freq eq '0'
572             and ($cust_pkg->setup || 0) > 0;
573     push @pkgs, $cust_pkg;
574   }
575   return @pkgs;
576 }
577
578 =item move_to NEW [ move_pkgs => \@move_pkgs ]
579
580 Takes a new L<FS::cust_location> object.  Moves all packages that use the 
581 existing location to the new one, then sets the "disabled" flag on the old
582 location.  Returns nothing on success, an error message on error.
583
584 Use option I<move_pkgs> to override the list of packages to update
585 (see L</move_pkgs>.)
586
587 =cut
588
589 sub move_to {
590   my $old = shift;
591   my $new = shift;
592   my %opt = @_;
593   
594   warn "move_to:\nFROM:".Dumper($old)."\nTO:".Dumper($new) if $DEBUG;
595
596   local $SIG{HUP} = 'IGNORE';
597   local $SIG{INT} = 'IGNORE';
598   local $SIG{QUIT} = 'IGNORE';
599   local $SIG{TERM} = 'IGNORE';
600   local $SIG{TSTP} = 'IGNORE';
601   local $SIG{PIPE} = 'IGNORE';
602
603   my $oldAutoCommit = $FS::UID::AutoCommit;
604   local $FS::UID::AutoCommit = 0;
605   my $dbh = dbh;
606   my $error = '';
607
608   # prevent this from failing because of pkg_svc quantity limits
609   local( $FS::cust_svc::ignore_quantity ) = 1;
610
611   if ( !$new->locationnum ) {
612     $error = $new->insert;
613     if ( $error ) {
614       $dbh->rollback if $oldAutoCommit;
615       return "Error creating location: $error";
616     }
617   } elsif ( $new->locationnum == $old->locationnum ) {
618     # then they're the same location; the normal result of doing a minor
619     # location edit
620     $dbh->commit if $oldAutoCommit;
621     return '';
622   }
623
624   my @pkgs;
625   if ($opt{'move_pkgs'}) {
626     @pkgs = @{$opt{'move_pkgs'}};
627     my $pkgerr;
628     foreach my $pkg (@pkgs) {
629       my $pkgnum = $pkg->pkgnum;
630       $pkgerr = "cust_pkg $pkgnum has already been charged"
631         if $pkg->part_pkg->freq eq '0'
632           and ($pkg->setup || 0) > 0;
633       $pkgerr = "cust_pkg $pkgnum is supplemental"
634         if $pkg->main_pkgnum;
635       $pkgerr = "cust_pkg $pkgnum already cancelled"
636         if $pkg->cancel;
637       $pkgerr = "cust_pkg $pkgnum does not use this location"
638         unless $pkg->locationnum eq $old->locationnum;
639       last if $pkgerr;
640     }
641     if ($pkgerr) {
642       $dbh->rollback if $oldAutoCommit;
643       return "Cannot update package location: $pkgerr";
644     }
645   } else {
646     @pkgs = $old->move_pkgs;
647   }
648
649   foreach my $cust_pkg (@pkgs) {
650     $error = $cust_pkg->change(
651       'locationnum' => $new->locationnum,
652       'keep_dates'  => 1
653     );
654     if ( $error and not ref($error) ) {
655       $dbh->rollback if $oldAutoCommit;
656       return "Error moving pkgnum ".$cust_pkg->pkgnum.": $error";
657     }
658   }
659
660   $error = $old->disable_if_unused;
661   if ( $error ) {
662     $dbh->rollback if $oldAutoCommit;
663     return "Error disabling old location: $error";
664   }
665
666   $dbh->commit if $oldAutoCommit;
667   '';
668 }
669
670 =item alternize
671
672 Attempts to parse data for location_type and location_number from address1
673 and address2.
674
675 =cut
676
677 sub alternize {
678   my $self = shift;
679
680   return '' if $self->get('location_type')
681             || $self->get('location_number');
682
683   my %parse;
684   if ( 1 ) { #ikano, switch on via config
685     { no warnings 'void';
686       eval { 'use FS::part_export::ikano;' };
687       die $@ if $@;
688     }
689     %parse = FS::part_export::ikano->location_types_parse;
690   } else {
691     %parse = (); #?
692   }
693
694   foreach my $from ('address1', 'address2') {
695     foreach my $parse ( keys %parse ) {
696       my $value = $self->get($from);
697       if ( $value =~ s/(^|\W+)$parse\W+(\w+)\W*$//i ) {
698         $self->set('location_type', $parse{$parse});
699         $self->set('location_number', $2);
700         $self->set($from, $value);
701         return '';
702       }
703     }
704   }
705
706   #nothing matched, no changes
707   $self->get('address2')
708     ? "Can't parse unit type and number from address2"
709     : '';
710 }
711
712 =item dealternize
713
714 Moves data from location_type and location_number to the end of address1.
715
716 =cut
717
718 sub dealternize {
719   my $self = shift;
720
721   #false laziness w/geocode_Mixin.pm::line
722   my $lt = $self->get('location_type');
723   if ( $lt ) {
724
725     my %location_type;
726     if ( 1 ) { #ikano, switch on via config
727       { no warnings 'void';
728         eval { 'use FS::part_export::ikano;' };
729         die $@ if $@;
730       }
731       %location_type = FS::part_export::ikano->location_types;
732     } else {
733       %location_type = (); #?
734     }
735
736     $self->address1( $self->address1. ' '. $location_type{$lt} || $lt );
737     $self->location_type('');
738   }
739
740   if ( length($self->location_number) ) {
741     $self->address1( $self->address1. ' '. $self->location_number );
742     $self->location_number('');
743   }
744  
745   '';
746 }
747
748 =item location_label
749
750 Returns the label of the location object.
751
752 Options:
753
754 =over 4
755
756 =item cust_main
757
758 Customer object (see L<FS::cust_main>)
759
760 =item prospect_main
761
762 Prospect object (see L<FS::prospect_main>)
763
764 =item join_string
765
766 String used to join location elements
767
768 =item no_prefix
769
770 Don't label the default service location as "Default service location".
771 May become the default at some point.
772
773 =back
774
775 =cut
776
777 sub location_label {
778   my( $self, %opt ) = @_;
779
780   my $prefix = $self->label_prefix(%opt);
781   $prefix .= ($opt{join_string} ||  ': ') if $prefix;
782   $prefix = '' if $opt{'no_prefix'};
783
784   $prefix . $self->SUPER::location_label(%opt);
785 }
786
787 =item label_prefix
788
789 Returns the optional site ID string (based on the cust_location-label_prefix
790 config option), "Default service location", or the empty string.
791
792 Options:
793
794 =over 4
795
796 =item cust_main
797
798 Customer object (see L<FS::cust_main>)
799
800 =item prospect_main
801
802 Prospect object (see L<FS::prospect_main>)
803
804 =back
805
806 =cut
807
808 sub label_prefix {
809   my( $self, %opt ) = @_;
810
811   my $cust_or_prospect = $opt{cust_main} || $opt{prospect_main};
812   unless ( $cust_or_prospect ) {
813     if ( $self->custnum ) {
814       $cust_or_prospect = FS::cust_main->by_key($self->custnum);
815     } elsif ( $self->prospectnum ) {
816       $cust_or_prospect = FS::prospect_main->by_key($self->prospectnum);
817     }
818   }
819
820   my $prefix = '';
821   if ( $label_prefix eq 'CoStAg' ) {
822     my $agent = $conf->config('cust_main-custnum-display_prefix',
823                   $cust_or_prospect->agentnum)
824                 || $cust_or_prospect->agent->agent;
825     # else this location is invalid
826     $prefix = uc( join('',
827         $self->country,
828         ($self->state =~ /^(..)/),
829         ($agent =~ /^(..)/),
830         sprintf('%05d', $self->locationnum)
831     ) );
832
833   } elsif ( $label_prefix eq '_location' && $self->locationname ) {
834     $prefix = $self->locationname;
835
836   #} elsif (    ( $opt{'cust_main'} || $self->custnum )
837   #        && $self->locationnum == $cust_or_prospect->ship_locationnum ) {
838   #  $prefix = 'Default service location';
839   #}
840   } else {
841     $prefix = '';
842   }
843
844   $prefix;
845 }
846
847 =item county_state_country
848
849 Returns a string consisting of just the county, state and country.
850
851 =cut
852
853 sub county_state_country {
854   my $self = shift;
855   my $label = $self->country;
856   $label = $self->state.", $label" if $self->state;
857   $label = $self->county." County, $label" if $self->county;
858   $label;
859 }
860
861 =back
862
863 =head2 SUBROUTINES
864
865 =over 4
866
867 =item process_censustract_update LOCATIONNUM
868
869 Queueable function to update the census tract to the current year (as set in 
870 the 'census_year' configuration variable) and retrieve the new tract code.
871
872 =cut
873
874 sub process_censustract_update {
875   eval "use FS::GeocodeCache";
876   die $@ if $@;
877   my $locationnum = shift;
878   my $cust_location = 
879     qsearchs( 'cust_location', { locationnum => $locationnum })
880       or die "locationnum '$locationnum' not found!\n";
881
882   my $new_year = $conf->config('census_year') or return;
883   my $loc = FS::GeocodeCache->new( $cust_location->location_hash );
884   $loc->set_censustract;
885   my $error = $loc->get('censustract_error');
886   die $error if $error;
887   $cust_location->set('censustract', $loc->get('censustract'));
888   $cust_location->set('censusyear',  $new_year);
889   $error = $cust_location->replace;
890   die $error if $error;
891   return;
892 }
893
894 =item process_set_coord
895
896 Queueable function to find and fill in coordinates for all locations that 
897 lack them.  Because this uses the Google Maps API, it's internally rate
898 limited and must run in a single process.
899
900 =cut
901
902 sub process_set_coord {
903   my $job = shift;
904   # avoid starting multiple instances of this job
905   my @others = qsearch('queue', {
906       'status'  => 'locked',
907       'job'     => $job->job,
908       'jobnum'  => {op=>'!=', value=>$job->jobnum},
909   });
910   return if @others;
911
912   $job->update_statustext('finding locations to update');
913   my @missing_coords = qsearch('cust_location', {
914       'disabled'  => '',
915       'latitude'  => '',
916       'longitude' => '',
917   });
918   my $i = 0;
919   my $n = scalar @missing_coords;
920   for my $cust_location (@missing_coords) {
921     $cust_location->set_coord;
922     my $error = $cust_location->replace;
923     if ( $error ) {
924       warn "error geocoding location#".$cust_location->locationnum.": $error\n";
925     } else {
926       $i++;
927       $job->update_statustext("updated $i / $n locations");
928       dbh->commit; # so that we don't have to wait for the whole thing to finish
929       # Rate-limit to stay under the Google Maps usage limit (2500/day).
930       # 86,400 / 35 = 2,468 lookups per day.
931     }
932     sleep 35;
933   }
934   if ( $i < $n ) {
935     die "failed to update ".$n-$i." locations\n";
936   }
937   return;
938 }
939
940 =item process_standardize [ LOCATIONNUMS ]
941
942 Performs address standardization on locations with unclean addresses,
943 using whatever method you have configured.  If the standardize_* method 
944 returns a I<clean> address match, the location will be updated.  This is 
945 always an in-place update (because the physical location is the same, 
946 and is just being referred to by a more accurate name).
947
948 Disabled locations will be skipped, as nobody cares.
949
950 If any LOCATIONNUMS are provided, only those locations will be updated.
951
952 =cut
953
954 sub process_standardize {
955   my $job = shift;
956   my @others = qsearch('queue', {
957       'status'  => 'locked',
958       'job'     => $job->job,
959       'jobnum'  => {op=>'!=', value=>$job->jobnum},
960   });
961   return if @others;
962   my @locationnums = grep /^\d+$/, @_;
963   my $where = "AND locationnum IN(".join(',',@locationnums).")"
964     if scalar(@locationnums);
965   my @locations = qsearch({
966       table     => 'cust_location',
967       hashref   => { addr_clean => '', disabled => '' },
968       extra_sql => $where,
969   });
970   my $n_todo = scalar(@locations);
971   my $n_done = 0;
972
973   # special: log this
974   my $log;
975   eval "use Text::CSV";
976   open $log, '>', "$FS::UID::cache_dir/process_standardize-" . 
977                   time2str('%Y%m%d',time) .
978                   ".csv";
979   my $csv = Text::CSV->new({binary => 1, eol => "\n"});
980
981   foreach my $cust_location (@locations) {
982     $job->update_statustext( int(100 * $n_done/$n_todo) . ",$n_done / $n_todo locations" ) if $job;
983     my $result = FS::GeocodeCache->standardize($cust_location);
984     if ( $result->{addr_clean} and !$result->{error} ) {
985       my @cols = ($cust_location->locationnum);
986       foreach (keys %$result) {
987         push @cols, $cust_location->get($_), $result->{$_};
988         $cust_location->set($_, $result->{$_});
989       }
990       # bypass immutable field restrictions
991       my $error = $cust_location->FS::Record::replace;
992       warn "location ".$cust_location->locationnum.": $error\n" if $error;
993       $csv->print($log, \@cols);
994     }
995     $n_done++;
996     dbh->commit; # so that we can resume if interrupted
997   }
998   close $log;
999 }
1000
1001 sub _upgrade_data {
1002   my $class = shift;
1003
1004   # are we going to need to update tax districts?
1005   my $use_districts = $conf->config('tax_district_method') ? 1 : 0;
1006
1007   # trim whitespace on records that need it
1008   local $allow_location_edit = 1;
1009   foreach my $field (@essential) {
1010     next if $field eq 'custnum';
1011     next if $field eq 'disabled';
1012     foreach my $location (qsearch({
1013       table => 'cust_location',
1014       extra_sql => " WHERE disabled IS NULL AND ($field LIKE ' %' OR $field LIKE '% ')"
1015     })) {
1016       my $error = $location->replace;
1017       die "$error (fixing whitespace in $field, locationnum ".$location->locationnum.')'
1018         if $error;
1019
1020       if (
1021         $use_districts
1022         && !$location->district
1023         && lc $location->state eq 'wa'
1024       ) {
1025         my $queue = new FS::queue {
1026           'job' => 'FS::geocode_Mixin::process_district_update'
1027         };
1028         $error = $queue->insert( 'FS::cust_location' => $location->locationnum );
1029         die $error if $error;
1030       }
1031     } # foreach $location
1032   } # foreach $field
1033   '';
1034 }
1035
1036 =head1 BUGS
1037
1038 =head1 SEE ALSO
1039
1040 L<FS::cust_main_county>, L<FS::cust_pkg>, L<FS::Record>,
1041 schema.html from the base documentation.
1042
1043 =cut
1044
1045 1;
1046