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