-tr-select-cust_location.html and elements/location.html: optionally support alternat...
[freeside.git] / FS / FS / qual.pm
1 package FS::qual;
2
3 use strict;
4 use base qw( FS::option_Common );
5 use FS::Record qw( qsearch qsearchs );
6
7 =head1 NAME
8
9 FS::qual - Object methods for qual records
10
11 =head1 SYNOPSIS
12
13   use FS::qual;
14
15   $record = new FS::qual \%hash;
16   $record = new FS::qual { 'column' => 'value' };
17
18   $error = $record->insert;
19
20   $error = $new_record->replace($old_record);
21
22   $error = $record->delete;
23
24   $error = $record->check;
25
26 =head1 DESCRIPTION
27
28 An FS::qual object represents a qualification for service.  FS::qual inherits from
29 FS::Record.  The following fields are currently supported:
30
31 =over 4
32
33 =item qualnum - primary key
34
35 =item prospectnum
36
37 =item custnum 
38
39 =item locationnum
40
41 =item phonenum - Service Telephone Number
42
43 =item exportnum - export instance providing service-qualification capabilities,
44 see L<FS::part_export>
45
46 =item vendor_qual_id - qualification id from vendor/telco
47
48 =item status - qualification status (e.g. (N)ew, (P)ending, (Q)ualifies)
49
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new qualification.  To add the qualification to the database, see L<"insert">.
60
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to.  You can ask the object for a copy with the I<hash> method.
63
64 =cut
65
66 # the new method can be inherited from FS::Record, if a table method is defined
67
68 sub table { 'qual'; }
69
70 =item insert
71
72 Adds this record to the database.  If there is an error, returns the error,
73 otherwise returns false.
74
75 =cut
76
77 # the insert method can be inherited from FS::Record
78
79 =item delete
80
81 Delete this record from the database.
82
83 =cut
84
85 # the delete method can be inherited from FS::Record
86
87 =item replace OLD_RECORD
88
89 Replaces the OLD_RECORD with this one in the database.  If there is an error,
90 returns the error, otherwise returns false.
91
92 =cut
93
94 # the replace method can be inherited from FS::Record
95
96 =item check
97
98 Checks all fields to make sure this is a valid qualification.  If there is
99 an error, returns the error, otherwise returns false.  Called by the insert
100 and replace methods.
101
102 =cut
103
104 # the check method should currently be supplied - FS::Record contains some
105 # data checking routines
106
107 sub check {
108   my $self = shift;
109
110   my $error = 
111     $self->ut_numbern('qualnum')
112     || $self->ut_foreign_keyn('custnum', 'cust_main', 'qualnum')
113     || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
114     || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
115     || $self->ut_numbern('phonenum')
116     || $self->ut_foreign_keyn('exportnum', 'part_export', 'exportnum')
117     || $self->ut_textn('vendor_qual_id')
118     || $self->ut_alpha('status')
119   ;
120   return $error if $error;
121
122   return "Invalid prospect/customer/location combination" if (
123    ( $self->locationnum && $self->prospectnum && $self->custnum ) ||
124    ( !$self->locationnum && !$self->prospectnum && !$self->custnum )
125   );
126
127   $self->SUPER::check;
128 }
129
130 sub location {
131     my $self = shift;
132     if ( $self->locationnum ) {
133         my $l = qsearchs( 'cust_location', 
134                     { 'locationnum' => $self->locationnum });
135         return $l->location_hash if $l;
136     }
137     if ( $self->custnum ) {
138         my $c = qsearchs( 'cust_main', { 'custnum' => $self->custnum });
139         return $c->location_hash if $c;
140     }
141   # prospectnum does not imply any particular address! must specify locationnum
142
143     '';
144 }
145
146 sub cust_or_prospect {
147     my $self = shift;
148     if ( $self->locationnum ) {
149         my $l = qsearchs( 'cust_location', 
150                     { 'locationnum' => $self->locationnum });
151         return qsearchs('cust_main',{ 'custnum' => $l->custnum })
152             if $l->custnum;
153         return qsearchs('prospect_main',{ 'prospectnum' => $l->prospectnum })
154             if $l->prospectnum;
155     }
156     return qsearchs('cust_main', { 'custnum' => $self->custnum }) 
157         if $self->custnum;
158     return qsearchs('prospect_main', { 'prospectnum' => $self->prospectnum })
159         if $self->prospectnum;
160 }
161
162 sub status_long {
163     my $self = shift;
164     my $s = {
165         'Q' => 'Qualified',
166         'D' => 'Does not Qualify',
167         'N' => 'New',
168     };
169     return $s->{$self->status} if defined $s->{$self->status};
170     return 'Unknown';
171 }
172
173 =back
174
175 =head1 SEE ALSO
176
177 L<FS::Record>, schema.html from the base documentation.
178
179 =cut
180
181 1;
182