991dd37dd6a954c01f9190950bd78b1f8b6ba337
[freeside.git] / FS / FS / cust_bill_pkg_void.pm
1 package FS::cust_bill_pkg_void;
2 use base qw( FS::TemplateItem_Mixin FS::reason_Mixin FS::Record );
3
4 use strict;
5 use vars qw( $me $DEBUG );
6 use FS::Record qw( qsearch qsearchs dbh fields );
7 use FS::cust_bill_void;
8 use FS::cust_bill_pkg_detail;
9 use FS::cust_bill_pkg_display;
10 use FS::cust_bill_pkg_discount;
11 use FS::cust_bill_pkg;
12 use FS::cust_bill_pkg_fee;
13 use FS::cust_bill_pkg_tax_location;
14 use FS::cust_bill_pkg_tax_rate_location;
15 use FS::cust_tax_exempt_pkg;
16
17 $me = '[ FS::cust_bill_pkg_void ]';
18 $DEBUG = 0;
19
20 =head1 NAME
21
22 FS::cust_bill_pkg_void - Object methods for cust_bill_pkg_void records
23
24 =head1 SYNOPSIS
25
26   use FS::cust_bill_pkg_void;
27
28   $record = new FS::cust_bill_pkg_void \%hash;
29   $record = new FS::cust_bill_pkg_void { 'column' => 'value' };
30
31   $error = $record->insert;
32
33   $error = $new_record->replace($old_record);
34
35   $error = $record->delete;
36
37   $error = $record->check;
38
39 =head1 DESCRIPTION
40
41 An FS::cust_bill_pkg_void object represents a voided invoice line item.
42 FS::cust_bill_pkg_void inherits from FS::Record.  The following fields are
43 currently supported:
44
45 =over 4
46
47 =item billpkgnum
48
49 primary key
50
51 =item invnum
52
53 invnum
54
55 =item pkgnum
56
57 pkgnum
58
59 =item pkgpart_override
60
61 pkgpart_override
62
63 =item setup
64
65 setup
66
67 =item recur
68
69 recur
70
71 =item sdate
72
73 sdate
74
75 =item edate
76
77 edate
78
79 =item itemdesc
80
81 itemdesc
82
83 =item itemcomment
84
85 itemcomment
86
87 =item section
88
89 section
90
91 =item freq
92
93 freq
94
95 =item quantity
96
97 quantity
98
99 =item unitsetup
100
101 unitsetup
102
103 =item unitrecur
104
105 unitrecur
106
107 =item hidden
108
109 hidden
110
111 =item reason 
112
113 freeform string (deprecated)
114
115 =item reasonnum 
116
117 reason for voiding the payment (see L<FS::reson>)
118
119 =back
120
121 =head1 METHODS
122
123 =over 4
124
125 =item new HASHREF
126
127 Creates a new record.  To add the record to the database, see L<"insert">.
128
129 Note that this stores the hash reference, not a distinct copy of the hash it
130 points to.  You can ask the object for a copy with the I<hash> method.
131
132 =cut
133
134 sub table { 'cust_bill_pkg_void'; }
135
136 sub detail_table            { 'cust_bill_pkg_detail_void'; }
137 sub display_table           { 'cust_bill_pkg_display_void'; }
138 sub discount_table          { 'cust_bill_pkg_discount_void'; }
139 #sub tax_location_table      { 'cust_bill_pkg_tax_location'; }
140 #sub tax_rate_location_table { 'cust_bill_pkg_tax_rate_location'; }
141 #sub tax_exempt_pkg_table    { 'cust_tax_exempt_pkg'; }
142
143 =item insert
144
145 Adds this record to the database.  If there is an error, returns the error,
146 otherwise returns false.
147
148 =item reason
149
150 Returns the text of the associated void reason (see L<FS::reason>) for this.
151
152 =item unvoid 
153
154 "Un-void"s this line item: Deletes the voided line item from the database and
155 adds back a normal line item (and related tables).
156
157 =cut
158
159 sub unvoid {
160   my $self = shift;
161
162   local $SIG{HUP} = 'IGNORE';
163   local $SIG{INT} = 'IGNORE';
164   local $SIG{QUIT} = 'IGNORE';
165   local $SIG{TERM} = 'IGNORE';
166   local $SIG{TSTP} = 'IGNORE';
167   local $SIG{PIPE} = 'IGNORE';
168
169   my $oldAutoCommit = $FS::UID::AutoCommit;
170   local $FS::UID::AutoCommit = 0;
171   my $dbh = dbh;
172
173   my $cust_bill_pkg = new FS::cust_bill_pkg ( {
174     map { $_ => $self->get($_) } fields('cust_bill_pkg')
175   } );
176   my $error = $cust_bill_pkg->insert;
177   if ( $error ) {
178     $dbh->rollback if $oldAutoCommit;
179     return $error;
180   }
181
182   foreach my $table (qw(
183     cust_bill_pkg_detail
184     cust_bill_pkg_display
185     cust_bill_pkg_discount
186     cust_bill_pkg_tax_location
187     cust_bill_pkg_tax_rate_location
188     cust_tax_exempt_pkg
189     cust_bill_pkg_fee
190   )) {
191
192     foreach my $voided (
193       qsearch($table.'_void', { billpkgnum=>$self->billpkgnum })
194     ) {
195
196       my $class = 'FS::'.$table;
197       my $unvoid = $class->new( {
198         map { $_ => $voided->get($_) } fields($table)
199       });
200       my $error = $unvoid->insert || $voided->delete;
201       if ( $error ) {
202         $dbh->rollback if $oldAutoCommit;
203         return $error;
204       }
205
206     }
207
208   }
209
210   $error = $self->delete;
211   if ( $error ) {
212     $dbh->rollback if $oldAutoCommit;
213     return $error;
214   }
215
216   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
217
218   '';
219
220 }
221
222 =item delete
223
224 Delete this record from the database.
225
226 =item replace OLD_RECORD
227
228 Replaces the OLD_RECORD with this one in the database.  If there is an error,
229 returns the error, otherwise returns false.
230
231 =item check
232
233 Checks all fields to make sure this is a valid record.  If there is
234 an error, returns the error, otherwise returns false.  Called by the insert
235 and replace methods.
236
237 =cut
238
239 sub check {
240   my $self = shift;
241
242   my $error = 
243     $self->ut_number('billpkgnum')
244     || $self->ut_snumber('pkgnum')
245     || $self->ut_number('invnum') #cust_bill or cust_bill_void, if we ever support line item voiding
246     || $self->ut_numbern('pkgpart_override')
247     || $self->ut_money('setup')
248     || $self->ut_money('recur')
249     || $self->ut_numbern('sdate')
250     || $self->ut_numbern('edate')
251     || $self->ut_textn('itemdesc')
252     || $self->ut_textn('itemcomment')
253     || $self->ut_textn('section')
254     || $self->ut_textn('freq')
255     || $self->ut_numbern('quantity')
256     || $self->ut_moneyn('unitsetup')
257     || $self->ut_moneyn('unitrecur')
258     || $self->ut_enum('hidden', [ '', 'Y' ])
259     || $self->ut_numbern('feepart')
260     || $self->ut_textn('reason')
261     || $self->ut_foreign_keyn('reasonnum', 'reason', 'reasonnum')
262   ;
263   return $error if $error;
264
265   $self->SUPER::check;
266 }
267
268 =item cust_bill
269
270 Returns the voided invoice (see L<FS::cust_bill_void>) for this voided line
271 item.
272
273 =cut
274
275 sub cust_bill {
276   my $self = shift;
277   #cust_bill or cust_bill_void, if we ever support line item voiding
278   qsearchs( 'cust_bill_void', { 'invnum' => $self->invnum } );
279 }
280
281 sub cust_bill_pkg_fee {
282   my $self = shift;
283   qsearch( 'cust_bill_pkg_fee_void', { 'billpkgnum' => $self->billpkgnum } );
284 }
285
286
287 # _upgrade_data
288 #
289 # Used by FS::Upgrade to migrate to a new database.
290 sub _upgrade_data {  # class method
291   my ($class, %opts) = @_;
292
293   warn "$me upgrading $class\n" if $DEBUG;
294
295   $class->_upgrade_reasonnum(%opts);
296 }
297
298 =back
299
300 =head1 BUGS
301
302 =head1 SEE ALSO
303
304 L<FS::Record>, schema.html from the base documentation.
305
306 =cut
307
308 1;
309