import torrus 1.0.9
[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 part_export {
131     my $self = shift;
132     if ( $self->exportnum ) {
133         return qsearchs('part_export', { exportnum => $self->exportnum } )
134                 or die 'invalid exportnum';
135     }
136     '';
137 }
138
139 sub location {
140     my $self = shift;
141     if ( $self->locationnum ) {
142         my $l = qsearchs( 'cust_location', 
143                     { 'locationnum' => $self->locationnum });
144         return $l->location_hash if $l;
145     }
146     if ( $self->custnum ) {
147         my $c = qsearchs( 'cust_main', { 'custnum' => $self->custnum });
148         return $c->location_hash if $c;
149     }
150   # prospectnum does not imply any particular address! must specify locationnum
151
152     '';
153 }
154
155 sub cust_or_prospect {
156     my $self = shift;
157     if ( $self->locationnum ) {
158         my $l = qsearchs( 'cust_location', 
159                     { 'locationnum' => $self->locationnum });
160         return qsearchs('cust_main',{ 'custnum' => $l->custnum })
161             if $l->custnum;
162         return qsearchs('prospect_main',{ 'prospectnum' => $l->prospectnum })
163             if $l->prospectnum;
164     }
165     return qsearchs('cust_main', { 'custnum' => $self->custnum }) 
166         if $self->custnum;
167     return qsearchs('prospect_main', { 'prospectnum' => $self->prospectnum })
168         if $self->prospectnum;
169 }
170
171 sub status_long {
172     my $self = shift;
173     my $s = {
174         'Q' => 'Qualified',
175         'D' => 'Does not Qualify',
176         'N' => 'New',
177     };
178     return $s->{$self->status} if defined $s->{$self->status};
179     return 'Unknown';
180 }
181
182 =back
183
184 =head1 SEE ALSO
185
186 L<FS::Record>, schema.html from the base documentation.
187
188 =cut
189
190 1;
191