52fe313c641897c14c5e8e882c6b423ea584a2d8
[freeside.git] / FS / FS / cust_main / Location.pm
1 package FS::cust_main::Location;
2
3 use strict;
4 use vars qw( $DEBUG $me @location_fields );
5 use FS::Record qw(qsearch qsearchs);
6 use FS::UID qw(dbh);
7 use FS::cust_location;
8
9 use Carp qw(carp);
10
11 $DEBUG = 0;
12 $me = '[FS::cust_main::Location]';
13
14 my $init = 0;
15 BEGIN {
16   # set up accessors for location fields
17   if (!$init) {
18     no strict 'refs';
19     @location_fields = 
20       qw( address1 address2 city county state zip country district
21         latitude longitude coord_auto censustract censusyear geocode
22         addr_clean );
23
24     foreach my $f (@location_fields) {
25       *{"FS::cust_main::Location::$f"} = sub {
26         carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
27         shift->bill_location->$f
28       };
29       *{"FS::cust_main::Location::ship_$f"} = sub {
30         carp "WARNING: tried to set cust_main.ship_$f with accessor" if (@_ > 1);
31         shift->ship_location->$f
32       };
33     }
34     $init++;
35   }
36 }
37
38 #debugging shim--probably a performance hit, so remove this at some point
39 sub get {
40   my $self = shift;
41   my $field = shift;
42   if ( $DEBUG and grep (/^(ship_)?($field)$/, @location_fields) ) {
43     carp "WARNING: tried to get() location field $field";
44     $self->$field;
45   }
46   $self->FS::Record::get($field);
47 }
48
49 =head1 NAME
50
51 FS::cust_main::Location - Location-related methods for cust_main
52
53 =head1 DESCRIPTION
54
55 These methods are available on FS::cust_main objects;
56
57 =head1 METHODS
58
59 =over 4
60
61 =item bill_location
62
63 Returns an L<FS::cust_location> object for the customer's billing address.
64
65 =cut
66
67 sub bill_location {
68   my $self = shift;
69   $self->hashref->{bill_location} 
70     ||= FS::cust_location->by_key($self->bill_locationnum);
71 }
72
73 =item ship_location
74
75 Returns an L<FS::cust_location> object for the customer's service address.
76
77 =cut
78
79 sub ship_location {
80   my $self = shift;
81   $self->hashref->{ship_location}
82     ||= FS::cust_location->by_key($self->ship_locationnum);
83 }
84
85 =item location TYPE
86
87 An alternative way of saying "bill_location or ship_location, depending on 
88 if TYPE is 'bill' or 'ship'".
89
90 =cut
91
92 sub location {
93   my $self = shift;
94   return $self->bill_location if $_[0] eq 'bill';
95   return $self->ship_location if $_[0] eq 'ship';
96   die "bad location type '$_[0]'";
97 }
98
99 =back
100
101 =head1 CLASS METHODS
102
103 =over 4
104
105 =item location_fields
106
107 Returns a list of fields found in the location objects.  All of these fields
108 can be read (but not written) by calling them as methods on the 
109 L<FS::cust_main> object (prefixed with 'ship_' for the service address 
110 fields).
111
112 =cut
113
114 sub location_fields { @location_fields }
115
116 sub _upgrade_data {
117   my $class = shift;
118   eval "use FS::contact;
119         use FS::contact_class;
120         use FS::contact_phone;
121         use FS::phone_type";
122
123   local $FS::cust_location::import = 1;
124   local $DEBUG = 0;
125   my $error;
126
127   my $tax_prefix = 'bill_';
128   if ( FS::Conf->new->exists('tax-ship_address') ) {
129     $tax_prefix = 'ship_';
130   }
131
132   # Step 0: set up contact classes and phone types
133   my $service_contact_class = 
134     qsearchs('contact_class', { classname => 'Service'})
135     || new FS::contact_class { classname => 'Service'};
136
137   if ( !$service_contact_class->classnum ) {
138     $error = $service_contact_class->insert;
139     die "error creating contact class for Service: $error" if $error;
140   }
141   my %phone_type = ( # fudge slightly
142     daytime => 'Work',
143     night   => 'Home',
144     mobile  => 'Mobile',
145     fax     => 'Fax'
146   );
147   my $w = 10;
148   foreach (keys %phone_type) {
149     $phone_type{$_} = qsearchs('phone_type', { typename => $phone_type{$_}})
150                       || new FS::phone_type  { typename => $phone_type{$_},
151                                                weight   => $w };
152     # just in case someone still doesn't have these
153     if ( !$phone_type{$_}->phonetypenum ) {
154       $error = $phone_type{$_}->insert;
155       die "error creating phone type '$_': $error" if $error;
156     }
157   }
158
159   foreach my $cust_main (qsearch('cust_main', { bill_locationnum => '' })) {
160     # Step 1: extract billing and service addresses into cust_location
161     my $custnum = $cust_main->custnum;
162     my $bill_location = FS::cust_location->new(
163       {
164         custnum => $custnum,
165         map { $_ => $cust_main->get($_) } location_fields(),
166       }
167     );
168     $bill_location->set('censustract', '');
169     $bill_location->set('censusyear', '');
170      # properly goes with ship_location; if they're the same, will be set
171      # on ship_location before inserting either one
172     my $ship_location = $bill_location; # until proven otherwise
173
174     if ( $cust_main->get('ship_address1') ) {
175       # detect duplicates
176       my $same = 1;
177       foreach (location_fields()) {
178         if ( length($cust_main->get("ship_$_")) and
179              $cust_main->get($_) ne $cust_main->get("ship_$_") ) {
180           $same = 0;
181         }
182       }
183
184       if ( !$same ) {
185         $ship_location = FS::cust_location->new(
186           {
187             custnum => $custnum,
188             map { $_ => $cust_main->get("ship_$_") } location_fields()
189           }
190         );
191       } # else it stays equal to $bill_location
192
193       # Step 2: Extract shipping address contact fields into contact
194       my %unlike = map { $_ => 1 }
195         grep { $cust_main->get($_) ne $cust_main->get("ship_$_") }
196         qw( last first company daytime night fax mobile );
197
198       if ( %unlike ) {
199         # then there IS a service contact
200         my $contact = FS::contact->new({
201           'custnum'     => $custnum,
202           'classnum'    => $service_contact_class->classnum,
203           'locationnum' => $ship_location->locationnum,
204           'last'        => $cust_main->get('ship_last'),
205           'first'       => $cust_main->get('ship_first'),
206         });
207         if ( !$cust_main->get('ship_last') or !$cust_main->get('ship_first') )
208         {
209           warn "customer $custnum has no service contact name; substituting ".
210                "customer name\n";
211           $contact->set('last' => $cust_main->get('last'));
212           $contact->set('first' => $cust_main->get('first'));
213         }
214
215         if ( $unlike{'company'} ) {
216           # there's no contact.company field, but keep a record of it
217           $contact->set(comment => 'Company: '.$cust_main->get('ship_company'));
218         }
219         $error = $contact->insert;
220         die "error migrating service contact for customer $custnum: $error"
221           if $error;
222
223         foreach ( grep { $unlike{$_} } qw( daytime night fax mobile ) ) {
224           my $phone = $cust_main->get("ship_$_");
225           next if !$phone;
226           my $contact_phone = FS::contact_phone->new({
227             'contactnum'    => $contact->contactnum,
228             'phonetypenum'  => $phone_type{$_}->phonetypenum,
229             FS::contact::_parse_phonestring( $phone )
230           });
231           $error = $contact_phone->insert;
232           # die "whose responsible this"
233           die "error migrating service contact phone for customer $custnum: $error"
234             if $error;
235           $cust_main->set("ship_$_" => '');
236         }
237
238         $cust_main->set("ship_$_" => '') foreach qw(last first company);
239       } #if %unlike
240     } #if ship_address1
241
242     # special case: should go with whichever location is used to calculate
243     # taxes, because that's the one it originally came from
244     if ( my $geocode = $cust_main->get('geocode') ) {
245       $bill_location->set('geocode' => '');
246       $ship_location->set('geocode' => '');
247
248       if ( $tax_prefix eq 'bill_' ) {
249         $bill_location->set('geocode', $geocode);
250       } elsif ( $tax_prefix eq 'ship_' ) {
251         $ship_location->set('geocode', $geocode);
252       }
253     }
254
255     # this always goes with the ship_location (whether it's the same as
256     # bill_location or not)
257     $ship_location->set('censustract', $cust_main->get('censustract'));
258     $ship_location->set('censusyear',  $cust_main->get('censusyear'));
259
260     $error = $bill_location->insert;
261     die "error migrating billing address for customer $custnum: $error"
262       if $error;
263
264     $cust_main->set(bill_locationnum => $bill_location->locationnum);
265
266     if (!$ship_location->locationnum) {
267       $error = $ship_location->insert;
268       die "error migrating service address for customer $custnum: $error"
269         if $error;
270     }
271
272     $cust_main->set(ship_locationnum => $ship_location->locationnum);
273
274     # Step 3: Wipe the migrated fields and update the cust_main
275
276     $cust_main->set("ship_$_" => '') foreach location_fields();
277     $cust_main->set($_ => '') foreach location_fields();
278
279     $error = $cust_main->replace;
280     die "error migrating addresses for customer $custnum: $error"
281       if $error;
282
283     # Step 4: set packages at the "default service location" to ship_location
284     foreach my $cust_pkg (
285       qsearch('cust_pkg', { custnum => $custnum, locationnum => '' })  
286     ) {
287       # not a location change
288       $cust_pkg->set('locationnum', $cust_main->ship_locationnum);
289       $error = $cust_pkg->replace;
290       die "error migrating package ".$cust_pkg->pkgnum.": $error"
291         if $error;
292     }
293
294   } #foreach $cust_main
295
296   # repair an error in earlier upgrades
297   if (!FS::upgrade_journal->is_done('cust_location_censustract_repair')
298        and FS::Conf->new->exists('cust_main-require_censustract') ) {
299
300     foreach my $cust_location (
301       qsearch('cust_location', { 'censustract' => '' })
302     ) {
303       my $custnum = $cust_location->custnum;
304       next if !$custnum; # avoid doing this for prospect locations
305       my $address1 = $cust_location->address1;
306       # find the last history record that had that address
307       my $last_h = qsearchs({
308           table     => 'h_cust_main',
309           extra_sql => " WHERE custnum = $custnum AND address1 = ".
310                         dbh->quote($address1) .
311                         " AND censustract IS NOT NULL",
312           order_by  => " ORDER BY history_date DESC LIMIT 1",
313       });
314       if (!$last_h) {
315         # this is normal; just means it never had a census tract before
316         next;
317       }
318       $cust_location->set('censustract' => $last_h->get('censustract'));
319       $cust_location->set('censusyear'  => $last_h->get('censusyear'));
320       my $error = $cust_location->replace;
321       warn "Error setting census tract for customer #$custnum:\n  $error\n"
322         if $error;
323     } # foreach $cust_location
324     FS::upgrade_journal->set_done('cust_location_censustract_repair');
325   }
326
327 }
328
329 =back
330
331 =cut
332
333 1;