This commit was generated by cvs2svn to compensate for changes in r11022,
[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 dbh );
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 sub insert {
78   my $self = shift;
79   my %options = @_;
80
81   local $SIG{HUP} = 'IGNORE';
82   local $SIG{INT} = 'IGNORE';
83   local $SIG{QUIT} = 'IGNORE';
84   local $SIG{TERM} = 'IGNORE';
85   local $SIG{TSTP} = 'IGNORE';
86   local $SIG{PIPE} = 'IGNORE';
87
88   my $oldAutoCommit = $FS::UID::AutoCommit;
89   local $FS::UID::AutoCommit = 0;
90   my $dbh = dbh;
91
92   if ( $options{'cust_location'} ) {
93     my $cust_location = $options{'cust_location'};
94     my $method = $cust_location->locationnum ? 'replace' : 'insert';
95     my $error = $cust_location->$method();
96     if ( $error ) {
97       $dbh->rollback if $oldAutoCommit;
98       return $error;
99     }
100     $self->locationnum( $cust_location->locationnum );
101   }
102
103   my @qual_option = ();
104   if ( $self->exportnum ) {
105     my $export = qsearchs( 'part_export', { 'exportnum' => $self->exportnum } )
106       or die 'Invalid exportnum';
107
108     my $qres = $export->qual($self);
109     unless ( ref($qres) ) {
110       $dbh->rollback if $oldAutoCommit;
111       return "Qualification error: $qres";
112     }
113
114     $self->$_($qres->{$_}) foreach grep $qres->{$_}, qw(status vendor_qual_id);
115     @qual_option = ( $qres->{'options'} ) if ref($qres->{'options'});
116   }
117
118   my $error = $self->SUPER::insert(@qual_option);
119   if ( $error ) {
120     $dbh->rollback if $oldAutoCommit;
121     return $error;
122   }
123
124   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
125   '';
126 }
127
128 =item delete
129
130 Delete this record from the database.
131
132 =cut
133
134 # the delete method can be inherited from FS::Record
135
136 =item replace OLD_RECORD
137
138 Replaces the OLD_RECORD with this one in the database.  If there is an error,
139 returns the error, otherwise returns false.
140
141 =cut
142
143 # the replace method can be inherited from FS::Record
144
145 =item check
146
147 Checks all fields to make sure this is a valid qualification.  If there is
148 an error, returns the error, otherwise returns false.  Called by the insert
149 and replace methods.
150
151 =cut
152
153 # the check method should currently be supplied - FS::Record contains some
154 # data checking routines
155
156 sub check {
157   my $self = shift;
158
159   my $error = 
160     $self->ut_numbern('qualnum')
161     || $self->ut_foreign_keyn('custnum', 'cust_main', 'qualnum')
162     || $self->ut_foreign_keyn('prospectnum', 'prospect_main', 'prospectnum')
163     || $self->ut_foreign_keyn('locationnum', 'cust_location', 'locationnum')
164     || $self->ut_numbern('phonenum')
165     || $self->ut_foreign_keyn('exportnum', 'part_export', 'exportnum')
166     || $self->ut_textn('vendor_qual_id')
167     || $self->ut_alpha('status')
168   ;
169   return $error if $error;
170
171   return "Invalid prospect/customer/location combination" if (
172    ( $self->locationnum && $self->prospectnum && $self->custnum ) ||
173    ( !$self->locationnum && !$self->prospectnum && !$self->custnum )
174   );
175
176   $self->SUPER::check;
177 }
178
179 sub part_export {
180     my $self = shift;
181     if ( $self->exportnum ) {
182         return qsearchs('part_export', { exportnum => $self->exportnum } )
183                 or die 'invalid exportnum';
184     }
185     '';
186 }
187
188 sub cust_location {
189   my $self = shift;
190   return '' unless $self->locationnum;
191   qsearchs('cust_location', { 'locationnum' => $self->locationnum } );
192 }
193
194 sub cust_main {
195   my $self = shift;
196   return '' unless $self->custnum;
197   qsearchs('cust_main', { 'custnum' => $self->custnum } );
198 }
199
200 sub location_hash {
201   my $self = shift;
202
203   if ( my $l = $self->cust_location ) {
204     my %loc_hash = $l->location_hash;
205     $loc_hash{locationnum} = $self->locationnum;
206     return %loc_hash;
207   }
208
209   if ( my $c = $self->cust_main ) {
210     # always override location_kind as it would never be known in the 
211     # case of cust_main "default service address"
212     my %loc_hash = $c->location_hash;
213     $loc_hash{location_kind} = $c->company ? 'B' : 'R';
214     return %loc_hash;
215   }
216
217   warn "prospectnum does not imply any particular address! must specify locationnum";
218   return ();
219 }
220
221 sub cust_or_prospect {
222     my $self = shift;
223     if ( $self->locationnum ) {
224         my $l = qsearchs( 'cust_location', 
225                     { 'locationnum' => $self->locationnum });
226         return qsearchs('cust_main',{ 'custnum' => $l->custnum })
227             if $l->custnum;
228         return qsearchs('prospect_main',{ 'prospectnum' => $l->prospectnum })
229             if $l->prospectnum;
230     }
231     return qsearchs('cust_main', { 'custnum' => $self->custnum }) 
232         if $self->custnum;
233     return qsearchs('prospect_main', { 'prospectnum' => $self->prospectnum })
234         if $self->prospectnum;
235     '';
236 }
237
238 sub status_long {
239     my $self = shift;
240     my $s = {
241         'Q' => 'Qualified',
242         'D' => 'Does not Qualify',
243         'N' => 'New',
244     };
245     return $s->{$self->status} if defined $s->{$self->status};
246     return 'Unknown';
247 }
248
249 =back
250
251 =head1 SEE ALSO
252
253 L<FS::Record>, schema.html from the base documentation.
254
255 =cut
256
257 1;
258