1 package FS::cust_main::Location;
4 use vars qw( $DEBUG $me @location_fields );
5 use FS::Record qw(qsearch qsearchs);
13 $me = '[FS::cust_main::Location]';
17 # set up accessors for location fields
21 qw( address1 address2 city county state zip country district
22 latitude longitude coord_auto censustract censusyear geocode
25 foreach my $f (@location_fields) {
26 *{"FS::cust_main::Location::$f"} = sub {
27 carp "WARNING: tried to set cust_main.$f with accessor" if (@_ > 1);
28 my $l = shift->bill_location;
31 *{"FS::cust_main::Location::ship_$f"} = sub {
32 carp "WARNING: tried to set cust_main.ship_$f with accessor" if (@_ > 1);
33 my $l = shift->ship_location;
41 #debugging shim--probably a performance hit, so remove this at some point
45 if ( $DEBUG and grep (/^(ship_)?($field)$/, @location_fields) ) {
46 carp "WARNING: tried to get() location field $field";
49 $self->FS::Record::get($field);
54 FS::cust_main::Location - Location-related methods for cust_main
58 These methods are available on FS::cust_main objects;
66 Returns an L<FS::cust_location> object for the customer's billing address.
72 $self->hashref->{bill_location}
73 ||= FS::cust_location->by_key($self->bill_locationnum);
78 Returns an L<FS::cust_location> object for the customer's service address.
84 $self->hashref->{ship_location}
85 ||= FS::cust_location->by_key($self->ship_locationnum);
90 An alternative way of saying "bill_location or ship_location, depending on
91 if TYPE is 'bill' or 'ship'".
97 return $self->bill_location if $_[0] eq 'bill';
98 return $self->ship_location if $_[0] eq 'ship';
99 die "bad location type '$_[0]'";
108 =item location_fields
110 Returns a list of fields found in the location objects. All of these fields
111 can be read (but not written) by calling them as methods on the
112 L<FS::cust_main> object (prefixed with 'ship_' for the service address
117 sub location_fields { @location_fields }
121 eval "use FS::contact;
122 use FS::contact_class;
123 use FS::contact_phone;
126 local $FS::cust_location::import = 1;
130 my $tax_prefix = 'bill_';
131 if ( FS::Conf->new->exists('tax-ship_address') ) {
132 $tax_prefix = 'ship_';
135 # Step 0: set up contact classes and phone types
136 my $service_contact_class =
137 qsearchs('contact_class', { classname => 'Service'})
138 || new FS::contact_class { classname => 'Service'};
140 if ( !$service_contact_class->classnum ) {
141 warn "Creating service contact class.\n";
142 $error = $service_contact_class->insert;
143 die "error creating contact class for Service: $error" if $error;
145 my %phone_type = ( # fudge slightly
152 foreach (keys %phone_type) {
153 $phone_type{$_} = qsearchs('phone_type', { typename => $phone_type{$_}})
154 || new FS::phone_type { typename => $phone_type{$_},
156 # just in case someone still doesn't have these
157 if ( !$phone_type{$_}->phonetypenum ) {
158 $error = $phone_type{$_}->insert;
159 die "error creating phone type '$_': $error" if $error;
163 warn "Migrating customer locations.\n";
164 my $search = FS::Cursor->new('cust_main',
165 { bill_locationnum => '',
166 address1 => { op=>'!=', value=>'' }
168 while (my $cust_main = $search->fetch) {
169 # Step 1: extract billing and service addresses into cust_location
170 my $custnum = $cust_main->custnum;
171 my $bill_location = FS::cust_location->new(
174 map { $_ => $cust_main->get($_) } location_fields(),
177 $bill_location->set('censustract', '');
178 $bill_location->set('censusyear', '');
179 # properly goes with ship_location; if they're the same, will be set
180 # on ship_location before inserting either one
181 my $ship_location = $bill_location; # until proven otherwise
183 if ( $cust_main->get('ship_address1') ) {
186 foreach (location_fields()) {
187 if ( length($cust_main->get("ship_$_")) and
188 $cust_main->get($_) ne $cust_main->get("ship_$_") ) {
194 $ship_location = FS::cust_location->new(
197 map { $_ => $cust_main->get("ship_$_") } location_fields()
200 } # else it stays equal to $bill_location
202 # Step 2: Extract shipping address contact fields into contact
203 my %unlike = map { $_ => 1 }
204 grep { $cust_main->get($_) ne $cust_main->get("ship_$_") }
205 qw( last first company daytime night fax mobile );
208 # then there IS a service contact
209 my $contact = FS::contact->new({
210 'custnum' => $custnum,
211 'classnum' => $service_contact_class->classnum,
212 'locationnum' => $ship_location->locationnum,
213 'last' => $cust_main->get('ship_last'),
214 'first' => $cust_main->get('ship_first'),
216 if ( !$cust_main->get('ship_last') or !$cust_main->get('ship_first') )
218 warn "customer $custnum has no service contact name; substituting ".
220 $contact->set('last' => $cust_main->get('last'));
221 $contact->set('first' => $cust_main->get('first'));
224 if ( $unlike{'company'} ) {
225 # there's no contact.company field, but keep a record of it
226 $contact->set(comment => 'Company: '.$cust_main->get('ship_company'));
228 $error = $contact->insert;
229 die "error migrating service contact for customer $custnum: $error"
232 foreach ( grep { $unlike{$_} } qw( daytime night fax mobile ) ) {
233 my $phone = $cust_main->get("ship_$_");
235 my $contact_phone = FS::contact_phone->new({
236 'contactnum' => $contact->contactnum,
237 'phonetypenum' => $phone_type{$_}->phonetypenum,
238 FS::contact::_parse_phonestring( $phone )
240 $error = $contact_phone->insert;
241 # die "whose responsible this"
242 die "error migrating service contact phone for customer $custnum: $error"
244 $cust_main->set("ship_$_" => '');
247 $cust_main->set("ship_$_" => '') foreach qw(last first company);
251 # special case: should go with whichever location is used to calculate
252 # taxes, because that's the one it originally came from
253 if ( my $geocode = $cust_main->get('geocode') ) {
254 $bill_location->set('geocode' => '');
255 $ship_location->set('geocode' => '');
257 if ( $tax_prefix eq 'bill_' ) {
258 $bill_location->set('geocode', $geocode);
259 } elsif ( $tax_prefix eq 'ship_' ) {
260 $ship_location->set('geocode', $geocode);
264 # this always goes with the ship_location (whether it's the same as
265 # bill_location or not)
266 $ship_location->set('censustract', $cust_main->get('censustract'));
267 $ship_location->set('censusyear', $cust_main->get('censusyear'));
269 $error = $bill_location->insert;
270 die "error migrating billing address for customer $custnum: $error"
273 $cust_main->set(bill_locationnum => $bill_location->locationnum);
275 if (!$ship_location->locationnum) {
276 $error = $ship_location->insert;
277 die "error migrating service address for customer $custnum: $error"
281 $cust_main->set(ship_locationnum => $ship_location->locationnum);
283 # Step 3: Wipe the migrated fields and update the cust_main
285 $cust_main->set("ship_$_" => '') foreach location_fields();
286 $cust_main->set($_ => '') foreach location_fields();
288 $error = $cust_main->replace;
289 die "error migrating addresses for customer $custnum: $error"
292 # Step 4: set packages at the "default service location" to ship_location
294 FS::Cursor->new('cust_pkg', { custnum => $custnum, locationnum => '' });
295 while (my $cust_pkg = $pkg_search->fetch) {
296 # not a location change
297 $cust_pkg->set('locationnum', $cust_main->ship_locationnum);
298 $error = $cust_pkg->replace;
299 die "error migrating package ".$cust_pkg->pkgnum.": $error"
303 } #while (my $cust_main...)
305 # repair an error in earlier upgrades
306 if (!FS::upgrade_journal->is_done('cust_location_censustract_repair')
307 and FS::Conf->new->exists('cust_main-require_censustract') ) {
309 foreach my $cust_location (
310 qsearch('cust_location', { 'censustract' => '' })
312 my $custnum = $cust_location->custnum;
313 next if !$custnum; # avoid doing this for prospect locations
314 my $address1 = $cust_location->address1;
315 # find the last history record that had that address
316 my $last_h = qsearchs({
317 table => 'h_cust_main',
318 extra_sql => " WHERE custnum = $custnum AND address1 = ".
319 dbh->quote($address1) .
320 " AND censustract IS NOT NULL",
321 order_by => " ORDER BY history_date DESC LIMIT 1",
324 # this is normal; just means it never had a census tract before
327 $cust_location->set('censustract' => $last_h->get('censustract'));
328 $cust_location->set('censusyear' => $last_h->get('censusyear'));
329 my $error = $cust_location->replace;
330 warn "Error setting census tract for customer #$custnum:\n $error\n"
332 } # foreach $cust_location
333 FS::upgrade_journal->set_done('cust_location_censustract_repair');