default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / FS / FS / realestate_unit.pm
1 package FS::realestate_unit;
2 use strict;
3 use warnings;
4 use Carp qw(croak);
5
6 use base 'FS::Record';
7 use FS::Record qw(qsearch qsearchs);
8
9 =head1 NAME
10
11 FS::realestate_unit - Object representing a realestate_unit record
12
13 =head1 SYNOPSIS
14
15   use FS::realestate_unit;
16
17   $record = new FS:realestate_unit  \%values;
18   $record = new FS::realestate_unit {
19     realestatelocnum => 42,
20     agentnum         => 1,
21     unit_title       => 'Ste 404',
22   };
23
24   $error = $record->insert;
25   $error = $new_rec->replace($record)
26   $error = $record->check;
27
28   $location = $record->location;
29
30 =head1 DESCRIPTION
31
32 An FS::realestate_unit object represents an invoicable unit of real estate.
33 Object may represent a single property, such as a rental house.  It may also
34 represent a group of properties sharing a common address or identifier, such
35 as a shopping mall, apartment complex, or office building, concert hall.
36
37 A FS::realestate_unit object must be associated with a FS::realestate_location
38
39 FS::realestate_unit inherits from FS::Record.
40
41 The following fields are currently supported:
42
43 =over 4
44
45 =item realestatenum
46
47 =item realestatelocnum
48
49 =item agentnum
50
51 =item unit_title
52
53 =item disabled
54
55 =back
56
57 =head1 METHODS
58
59 =over 4
60
61 =item new HASHREF (see L<FS::Record>)
62
63 =cut
64
65 sub table {'realestate_unit';}
66
67 =item insert (see L<FS::Record>)
68
69 =item delete
70
71   FS::realestate_unit records should never be deleted, only disabled
72
73 =cut
74
75 sub delete {
76   # Once this record has been associated with a customer in any way, it
77   # should not be deleted.  todo perhaps, add a is_deletable function that
78   # checks if the record has ever actually been used, and allows deletion
79   # if it hasn't.  (entered in error, etc).
80   croak "FS::realestate_unit records should never be deleted";
81 }
82
83
84 =item replace OLD_RECORD (see L<FS::Record>)
85
86 =item check (see L<FS::Record>)
87
88 =item agent
89
90 Returns the associated agent, if any, for this object
91
92 =cut
93
94 sub agent {
95   my $self = shift;
96   return undef unless $self->agentnum;
97   return qsearchs('agent', {agentnum => $self->agentnum} );
98 }
99
100 =item location
101
102   Return the associated FS::realestate_location object
103
104 =cut
105
106 sub location {
107   my $self = shift;
108   return $self->{location} if exists $self->{location};
109   return $self->{location} = qsearchs(
110     'realestate_location',
111     {realestatelocnum => $self->realestatelocnum}
112   );
113 }
114
115 =back
116
117 =item custnum
118
119 Pull the assigned custnum for this unit, if provisioned
120
121 =cut
122
123 sub custnum {
124   my $self = shift;
125   return $self->{custnum}
126     if $self->{custnum};
127
128   # select cust_pkg.custnum
129   # from svc_realestate
130   # LEFT JOIN cust_svc ON svc_realestate.svcnum = cust_svc.svcnum
131   # LEFT JOIN cust_pkg ON cust_svc.pkgnum = cust_pkg.pkgnum
132   # WHERE svc_realestate.realestatenum = $realestatenum
133
134   my $row = qsearchs({
135     select    => 'cust_pkg.custnum',
136     table     => 'svc_realestate',
137     addl_from => 'LEFT JOIN cust_svc ON svc_realestate.svcnum = cust_svc.svcnum '
138                . 'LEFT JOIN cust_pkg ON cust_svc.pkgnum = cust_pkg.pkgnum ',
139     extra_sql => 'WHERE svc_realestate.realestatenum = '.$self->realestatenum,
140   });
141
142   return
143     unless $row && $row->custnum;
144
145   return $self->{custnum} = $row->custnum;
146 }
147
148 =head1 SUBROUTINES
149
150 =over 4
151
152 =cut
153
154
155 =back
156
157 =head1 SEE ALSO
158
159 L<FS::record>, L<FS::realestate_location>, L<FS::svc_realestate>
160
161 =cut
162
163 1;