rt# 74031 reworked realestate schema as locations and units
[freeside.git] / FS / FS / realestate_location.pm
1 package FS::realestate_location;
2 use strict;
3 use warnings;
4 use Carp qw(croak);
5
6 use base 'FS::Record';
7
8 use FS::Record qw(qsearchs qsearch);
9
10 =head1 NAME
11
12 FS::realestate_location - Object representing a realestate_location record
13
14 =head1 SYNOPSIS
15
16   use FS::realestate_location;
17
18   $location = new FS::realestate_location \%values;
19   $location = new FS::realestate_location {
20     agentnum => 1,
21     title    => 'Superdome',
22     address1 => '1500 Sugar Bowl Dr',
23     city     => 'New Orleans',
24     state    => 'LA',
25     zip      => '70112',
26   };
27
28   $error = $location->insert;
29   $error = $new_loc->replace($location);
30   $error = $record->check;
31
32   $error = $location->add_unit('Box Seat No. 42');
33   @units = $location->units;
34   @units = $location->active_units;
35
36 =head1 DESCRIPTION
37
38 An FS::realestate_location object represents a location for one or more
39 FS::realestate_unit objects.  Expected to contain at least one unit, as only
40 realestate_unit objects are assignable to packages via 
41 L<FS::svc_realestate>.
42
43 FS::realestate_location inherits from FS::Record.
44
45 The following fields are currently supported:
46
47 =over 4
48
49 =item realestatelocnum
50
51 =item agentnum
52
53 =item location_title
54
55 =item address1 (optional)
56
57 =item address2 (optional)
58
59 =item city (optional)
60
61 =item state (optional)
62
63 =item zip (optional)
64
65 =item disabled
66
67 =back
68
69 =head1 METHODS
70
71 =over 4
72
73 =item new HASHREF (see L<FS::Record>)
74
75 =cut
76
77 sub table {'realestate_location';}
78
79 =item insert (see L<FS::Record>)
80
81 =item delete
82
83   FS::realestate_location records should never be deleted, only disabled
84
85 =cut
86
87 sub delete {
88   # Once this record has been associated with a customer in any way, it
89   # should not be deleted.  todo perhaps, add a is_deletable function that
90   # checks if the record has ever actually been used, and allows deletion
91   # if it hasn't.  (entered in error, etc).
92   croak "FS::realestate_location records should never be deleted";
93 }
94
95 =item replace OLD_RECORD (see L<FS::Record>)
96
97 =item check (see L<FS::Record>)
98
99 =item agent
100
101 Returns the associated agent
102
103 =cut
104
105 sub agent {
106   my $self = shift;
107   return undef unless $self->agentnum;
108   return exists $self->{agent}
109   ? $self->{agent}
110   : $self->{agent} = qsearchs('agent', {agentnum => $self->agentnum} );
111 }
112
113
114 =item add_unit UNIT_TITLE
115
116 Create an associated L<FS::realestate_unit> record
117
118 =cut
119
120 sub add_unit {
121   my ($self, $unit_title) = @_;
122   croak "add_unit() requires a \$unit_title parameter" unless $unit_title;
123
124   if (
125     qsearchs('realestate_unit',{
126       realestatelocnum => $self->realestatelocnum,
127       unit_title => $unit_title,
128     })
129   ) {
130     return "Unit Title ($unit_title) has already been used for location (".
131       $self->location_title.")";
132   }
133
134   my $unit = FS::realestate_unit->new({
135     realestatelocnum => $self->realestatelocnum,
136     agentnum         => $self->agentnum,
137     unit_title       => $unit_title,
138   });
139   my $err = $unit->insert;
140   die "Error creating FS::realestate_new record: $err" if $err;
141
142   return;
143 }
144
145
146 =item units
147
148 Returns all units associated with this location
149
150 =cut
151
152 sub units {
153   my $self = shift;
154   return qsearch(
155     'realestate_unit',
156     {realestatelocnum => $self->realestatelocnum}
157   );
158 }
159
160
161 =head1 SUBROUTINES
162
163 =over 4
164
165 =cut
166
167
168
169
170 =back
171
172 =head1 SEE ALSO
173
174 L<FS::record>, L<FS::realestate_unit>, L<FS::svc_realestate>
175
176 =cut
177
178 1;