show credit balance on invoices, #11564
[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         if ( $l ) {
145             my %loc_hash = $l->location_hash;
146             $loc_hash{locationnum} = $self->locationnum;
147             return %loc_hash;
148         }
149     }
150     if ( $self->custnum ) {
151         my $c = qsearchs( 'cust_main', { 'custnum' => $self->custnum });
152         
153         if($c) {
154             # always override location_kind as it would never be known in the 
155             # case of cust_main "default service address"
156             my %loc_hash = $c->location_hash;
157             $loc_hash{location_kind} = $c->company ? 'B' : 'R';
158             return %loc_hash;
159         }
160     }
161   # prospectnum does not imply any particular address! must specify locationnum
162
163     '';
164 }
165
166 sub cust_or_prospect {
167     my $self = shift;
168     if ( $self->locationnum ) {
169         my $l = qsearchs( 'cust_location', 
170                     { 'locationnum' => $self->locationnum });
171         return qsearchs('cust_main',{ 'custnum' => $l->custnum })
172             if $l->custnum;
173         return qsearchs('prospect_main',{ 'prospectnum' => $l->prospectnum })
174             if $l->prospectnum;
175     }
176     return qsearchs('cust_main', { 'custnum' => $self->custnum }) 
177         if $self->custnum;
178     return qsearchs('prospect_main', { 'prospectnum' => $self->prospectnum })
179         if $self->prospectnum;
180 }
181
182 sub status_long {
183     my $self = shift;
184     my $s = {
185         'Q' => 'Qualified',
186         'D' => 'Does not Qualify',
187         'N' => 'New',
188     };
189     return $s->{$self->status} if defined $s->{$self->status};
190     return 'Unknown';
191 }
192
193 =back
194
195 =head1 SEE ALSO
196
197 L<FS::Record>, schema.html from the base documentation.
198
199 =cut
200
201 1;
202