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