4 use base qw( FS::option_Common );
5 use FS::Record qw( qsearch qsearchs dbh );
9 FS::qual - Object methods for qual records
15 $record = new FS::qual \%hash;
16 $record = new FS::qual { 'column' => 'value' };
18 $error = $record->insert;
20 $error = $new_record->replace($old_record);
22 $error = $record->delete;
24 $error = $record->check;
28 An FS::qual object represents a qualification for service. FS::qual inherits from
29 FS::Record. The following fields are currently supported:
33 =item qualnum - primary key
41 =item phonenum - Service Telephone Number
43 =item exportnum - export instance providing service-qualification capabilities,
44 see L<FS::part_export>
46 =item vendor_qual_id - qualification id from vendor/telco
48 =item status - qualification status (e.g. (N)ew, (P)ending, (Q)ualifies)
59 Creates a new qualification. To add the qualification to the database, see L<"insert">.
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.
66 # the new method can be inherited from FS::Record, if a table method is defined
72 Adds this record to the database. If there is an error, returns the error,
73 otherwise returns false.
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';
88 my $oldAutoCommit = $FS::UID::AutoCommit;
89 local $FS::UID::AutoCommit = 0;
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();
97 $dbh->rollback if $oldAutoCommit;
100 $self->locationnum( $cust_location->locationnum );
103 my @qual_option = ();
104 if ( $self->exportnum ) {
105 my $export = qsearchs( 'part_export', { 'exportnum' => $self->exportnum } )
106 or die 'Invalid exportnum';
108 my $qres = $export->qual($self);
109 unless ( ref($qres) ) {
110 $dbh->rollback if $oldAutoCommit;
111 return "Qualification error: $qres";
114 $self->$_($qres->{$_}) foreach grep $qres->{$_}, qw(status vendor_qual_id);
115 @qual_option = ( $qres->{'options'} ) if ref($qres->{'options'});
118 my $error = $self->SUPER::insert(@qual_option);
120 $dbh->rollback if $oldAutoCommit;
124 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
130 Delete this record from the database.
134 # the delete method can be inherited from FS::Record
136 =item replace OLD_RECORD
138 Replaces the OLD_RECORD with this one in the database. If there is an error,
139 returns the error, otherwise returns false.
143 # the replace method can be inherited from FS::Record
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
153 # the check method should currently be supplied - FS::Record contains some
154 # data checking routines
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')
169 return $error if $error;
171 return "Invalid prospect/customer/location combination" if (
172 ( $self->locationnum && $self->prospectnum && $self->custnum ) ||
173 ( !$self->locationnum && !$self->prospectnum && !$self->custnum )
181 if ( $self->exportnum ) {
182 return qsearchs('part_export', { exportnum => $self->exportnum } )
183 or die 'invalid exportnum';
190 return '' unless $self->locationnum;
191 qsearchs('cust_location', { 'locationnum' => $self->locationnum } );
196 return '' unless $self->custnum;
197 qsearchs('cust_main', { 'custnum' => $self->custnum } );
203 if ( my $l = $self->cust_location ) {
204 my %loc_hash = $l->location_hash;
205 $loc_hash{locationnum} = $self->locationnum;
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';
217 warn "prospectnum does not imply any particular address! must specify locationnum";
221 sub cust_or_prospect {
223 if ( $self->locationnum ) {
224 my $l = qsearchs( 'cust_location',
225 { 'locationnum' => $self->locationnum });
226 return qsearchs('cust_main',{ 'custnum' => $l->custnum })
228 return qsearchs('prospect_main',{ 'prospectnum' => $l->prospectnum })
231 return qsearchs('cust_main', { 'custnum' => $self->custnum })
233 return qsearchs('prospect_main', { 'prospectnum' => $self->prospectnum })
234 if $self->prospectnum;
242 'D' => 'Does not Qualify',
245 return $s->{$self->status} if defined $s->{$self->status};
253 L<FS::Record>, schema.html from the base documentation.