svc_hardware: better error messages for bad hw_addr when not validating as a MAC...
[freeside.git] / FS / FS / cust_credit_void.pm
1 package FS::cust_credit_void; 
2 use base qw( FS::otaker_Mixin FS::cust_main_Mixin FS::reason_Mixin FS::Record );
3
4 use strict;
5 use vars qw( $me $DEBUG );
6 use FS::Record qw(qsearchs); # qsearch qsearchs);
7 use FS::CurrentUser;
8 use FS::access_user;
9 use FS::cust_credit;
10 use FS::UID qw( dbh );
11
12 $me = '[ FS::cust_credit_void ]';
13 $DEBUG = 0;
14
15 =head1 NAME
16
17 FS::cust_credit_void - Object methods for cust_credit_void objects
18
19 =head1 SYNOPSIS
20
21   use FS::cust_credit_void;
22
23   $record = new FS::cust_credit_void \%hash;
24   $record = new FS::cust_credit_void { 'column' => 'value' };
25
26   $error = $record->insert;
27
28   $error = $new_record->replace($old_record);
29
30   $error = $record->delete;
31
32   $error = $record->check;
33
34 =head1 DESCRIPTION
35
36 An FS::cust_credit_void object represents a voided credit.  All fields in
37 FS::cust_credit are present, as well as:
38
39 =over 4
40
41 =item void_date - the date (unix timestamp) that the credit was voided
42
43 =item void_reason - the reason (a freeform string)
44
45 =item void_usernum - the user (L<FS::access_user>) who voided it
46
47 =back
48
49 =head1 METHODS
50
51 =over 4 
52
53 =item new HASHREF
54
55 Creates a new voided credit record.
56
57 =cut
58
59 sub table { 'cust_credit_void'; }
60
61 =item insert
62
63 Adds this voided credit to the database.
64
65 =item check
66
67 Checks all fields to make sure this is a valid voided credit.  If there is an
68 error, returns the error, otherwise returns false.  Called by the insert
69 method.
70
71 =cut
72
73 sub check {
74   my $self = shift;
75
76   my $error =
77     $self->ut_numbern('crednum')
78     || $self->ut_number('custnum')
79     || $self->ut_numbern('_date')
80     || $self->ut_money('amount')
81     || $self->ut_alphan('otaker')
82     || $self->ut_textn('reason')
83     || $self->ut_textn('addlinfo')
84     || $self->ut_enum('closed', [ '', 'Y' ])
85     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
86     || $self->ut_foreign_keyn('eventnum', 'cust_event', 'eventnum')
87     || $self->ut_foreign_keyn('commission_agentnum',  'agent', 'agentnum')
88     || $self->ut_foreign_keyn('commission_salesnum',  'sales', 'salesnum')
89     || $self->ut_foreign_keyn('commission_pkgnum', 'cust_pkg', 'pkgnum')
90     || $self->ut_numbern('void_date')
91     || $self->ut_textn('void_reason')
92     || $self->ut_foreign_keyn('void_usernum', 'access_user', 'usernum')
93     || $self->ut_foreign_keyn('void_reasonnum', 'reason', 'reasonnum')
94   ;
95   return $error if $error;
96
97   $self->void_date(time) unless $self->void_date;
98
99   $self->void_usernum($FS::CurrentUser::CurrentUser->usernum)
100     unless $self->void_usernum;
101
102   $self->SUPER::check;
103 }
104
105 =item unvoid 
106
107 "Un-void"s this credit: Deletes the voided credit from the database and adds
108 back (but does not re-apply) a normal credit.
109
110 =cut
111
112 sub unvoid {
113   my $self = shift;
114
115   local $SIG{HUP} = 'IGNORE';
116   local $SIG{INT} = 'IGNORE';
117   local $SIG{QUIT} = 'IGNORE';
118   local $SIG{TERM} = 'IGNORE';
119   local $SIG{TSTP} = 'IGNORE';
120   local $SIG{PIPE} = 'IGNORE';
121
122   my $oldAutoCommit = $FS::UID::AutoCommit;
123   local $FS::UID::AutoCommit = 0;
124   my $dbh = dbh;
125
126   my $cust_credit = new FS::cust_credit ( {
127     map { $_ => $self->get($_) } grep { $_ !~ /void/ } $self->fields
128   } );
129   my $error = $cust_credit->insert;
130
131   if ( $error ) {
132     $dbh->rollback if $oldAutoCommit;
133     return $error;
134   }
135
136   $error ||= $self->delete;
137   if ( $error ) {
138     $dbh->rollback if $oldAutoCommit;
139     return $error;
140   }
141
142   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
143
144   '';
145
146 }
147
148 =item cust_main
149
150 Returns the parent customer object (see L<FS::cust_main>).
151
152 =item void_access_user
153
154 Returns the voiding employee object (see L<FS::access_user>).
155
156 =cut
157
158 sub void_access_user {
159   my $self = shift;
160   qsearchs('access_user', { 'usernum' => $self->void_usernum } );
161 }
162
163 =item void_access_user_name
164
165 Returns the voiding employee name.
166
167 =cut
168
169 sub void_access_user_name {
170   my $self = shift;
171   my $user = $self->void_access_user;
172   return unless $user;
173   return $user->name;
174 }
175
176 =item void_reason
177
178 Returns the text of the associated void credit reason (see L<FS::reason>) for this voided credit.
179
180 The reason for the original credit remains accessible through the reason method.
181
182 =cut
183
184 sub void_reason {
185   my ($self, $value, %options) = @_;
186   my $reason_text;
187   if ( $self->void_reasonnum ) {
188     my $reason = FS::reason->by_key($self->void_reasonnum);
189     $reason_text = $reason->reason;
190   } else { # in case one of these somehow still exists
191     $reason_text = $self->get('void_reason');
192   }
193
194   return $reason_text;
195 }
196
197 # _upgrade_data
198 #
199 # Used by FS::Upgrade to migrate to a new database.
200 sub _upgrade_data {    # class method
201     my ( $class, %opts ) = @_;
202
203     warn "$me upgrading $class\n" if $DEBUG;
204
205     $class->_upgrade_reasonnum(%opts);
206 }
207
208 =back
209
210 =head1 BUGS
211
212 Doesn't yet support unvoid.
213
214 =head1 SEE ALSO
215
216 L<FS::cust_credit>, L<FS::Record>, schema.html from the base documentation.
217
218 =cut
219
220 1;
221