ACL for hardware class config, RT#85057
[freeside.git] / FS / FS / qual.pm
1 package FS::qual;
2 use base qw( FS::option_Common );
3
4 use strict;
5 use FS::Record qw(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 = $self->part_export
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 location_hash {
180   my $self = shift;
181
182   if ( my $l = $self->cust_location ) {
183     my %loc_hash = $l->location_hash;
184     $loc_hash{locationnum} = $self->locationnum;
185     return %loc_hash;
186   }
187
188   if ( my $c = $self->cust_main ) {
189     # always override location_kind as it would never be known in the 
190     # case of cust_main "default service address"
191     my %loc_hash = $c->location_hash;
192     $loc_hash{location_kind} = $c->company ? 'B' : 'R';
193     return %loc_hash;
194   }
195
196   warn "prospectnum does not imply any particular address! must specify locationnum";
197   return ();
198 }
199
200 sub cust_or_prospect {
201     my $self = shift;
202     if ( $self->locationnum ) {
203         my $l = $self->cust_location;
204         return $l->cust_main     if $l->custnum;
205         return $l->prospect_main if $l->prospectnum;
206     }
207     return $self->cust_main     if $self->custnum;
208     return $self->cust_prospect if $self->prospectnum;
209     '';
210 }
211
212 sub status_long {
213     my $self = shift;
214     my $s = {
215         'Q' => 'Qualified',
216         'D' => 'Does not Qualify',
217         'N' => 'New',
218     };
219     return $s->{$self->status} if defined $s->{$self->status};
220     return 'Unknown';
221 }
222
223 =back
224
225 =head1 SEE ALSO
226
227 L<FS::Record>, schema.html from the base documentation.
228
229 =cut
230
231 1;
232